summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-03-25 21:02:58 +0100
committerAnton Kling <anton@kling.gg>2024-03-25 21:04:10 +0100
commit3deb2df8e62a5f0a5535ee734a5aa13b0959f53f (patch)
treeaf8841076c95ae3de7dcd4a006026be2607a0fbf /kernel
parent6baa733f5682f660143c851a635a53dc2c2df7ae (diff)
Random changes
Diffstat (limited to 'kernel')
-rw-r--r--kernel/Makefile2
-rw-r--r--kernel/arch/i386/mmu.c8
-rw-r--r--kernel/cpu/syscall.c1
-rw-r--r--kernel/drivers/keyboard.c22
-rw-r--r--kernel/fs/shm.c9
-rw-r--r--kernel/fs/vfs.c4
-rw-r--r--kernel/fs/vfs.h1
-rw-r--r--kernel/includes/syscalls.h3
-rw-r--r--kernel/kmalloc.c2
-rw-r--r--kernel/process.s2
-rw-r--r--kernel/sched/scheduler.c11
-rw-r--r--kernel/syscalls/munmap.c6
12 files changed, 38 insertions, 33 deletions
diff --git a/kernel/Makefile b/kernel/Makefile
index 71ea761..0b59509 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -1,6 +1,6 @@
CC="i686-sb-gcc"
AS="i686-sb-as"
-OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o syscalls/isatty.o syscalls/randomfill.o syscalls/open.o syscalls/write.o syscalls/pwrite.o ipc.o syscalls/ipc.o syscalls/port.o syscalls/map_frames.o syscalls/virtual_to_physical.o syscalls/install_irq.o arch/i386/interrupts.o cpu/isr.o lib/stack.o lib/buffered_write.o lib/list.o cpu/arch_inst.o cpu/int_syscall.o queue.o syscalls/queue.o
+OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o syscalls/isatty.o syscalls/randomfill.o syscalls/open.o syscalls/write.o syscalls/pwrite.o ipc.o syscalls/ipc.o syscalls/port.o syscalls/map_frames.o syscalls/virtual_to_physical.o syscalls/install_irq.o arch/i386/interrupts.o cpu/isr.o lib/stack.o lib/buffered_write.o lib/list.o cpu/arch_inst.o cpu/int_syscall.o queue.o syscalls/queue.o syscalls/munmap.o
CFLAGS = -std=c99 -Ofast -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL
#CFLAGS = -std=c99 -Ofast -flto -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL
#CFLAGS = -std=c99 -O0 -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL
diff --git a/kernel/arch/i386/mmu.c b/kernel/arch/i386/mmu.c
index 8761dd8..9b63a9a 100644
--- a/kernel/arch/i386/mmu.c
+++ b/kernel/arch/i386/mmu.c
@@ -156,11 +156,11 @@ void write_to_frame(u32 frame_address, u8 on) {
num_allocated_frames++;
tmp_small_frames[INDEX_FROM_BIT(frame)] |=
((u32)0x1 << OFFSET_FROM_BIT(frame));
- return;
+ } else {
+ num_allocated_frames--;
+ tmp_small_frames[INDEX_FROM_BIT(frame)] &=
+ ~((u32)0x1 << OFFSET_FROM_BIT(frame));
}
- num_allocated_frames--;
- tmp_small_frames[INDEX_FROM_BIT(frame)] &=
- ~((u32)0x1 << OFFSET_FROM_BIT(frame));
}
PageDirectory *get_active_pagedirectory(void) {
diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c
index 6b260d3..7734f4e 100644
--- a/kernel/cpu/syscall.c
+++ b/kernel/cpu/syscall.c
@@ -205,6 +205,7 @@ int (*syscall_functions[])() = {
(void(*))syscall_queue_create,
(void(*))syscall_queue_add,
(void(*))syscall_queue_wait,
+ (void(*))syscall_munmap,
};
void int_syscall(reg_t *r);
diff --git a/kernel/drivers/keyboard.c b/kernel/drivers/keyboard.c
index 24ebbb0..fe4fe16 100644
--- a/kernel/drivers/keyboard.c
+++ b/kernel/drivers/keyboard.c
@@ -155,26 +155,10 @@ void int_keyboard(reg_t *frame) {
ev.mode |= is_alt_down << 1;
ev.mode |= is_ctrl_down << 2;
fifo_object_write((u8 *)&ev, 0, sizeof(ev), keyboard_fifo);
- kb_inode->has_data = keyboard_fifo->has_data;
-}
-
-#define PS2_WAIT_RECV \
- { \
- for (;;) { \
- u8 status = inb(PS2_REG_STATUS); \
- if (status & 0x1) \
- break; \
- } \
- }
-
-#define PS2_WAIT_SEND \
- { \
- for (;;) { \
- u8 status = inb(PS2_REG_STATUS); \
- if (!(status & (0x1 << 1))) \
- break; \
- } \
+ if (kb_inode) {
+ kb_inode->has_data = keyboard_fifo->has_data;
}
+}
void install_keyboard(void) {
keyboard_fifo = create_fifo_object();
diff --git a/kernel/fs/shm.c b/kernel/fs/shm.c
index e0c0211..9215b24 100644
--- a/kernel/fs/shm.c
+++ b/kernel/fs/shm.c
@@ -55,9 +55,10 @@ vfs_vm_object_t *shm_get_vm_object(u64 length, u64 offset, vfs_fd_t *fd) {
int shm_ftruncate(vfs_fd_t *fd, size_t length) {
vfs_vm_object_t *p = fd->inode->internal_object;
p->size = length;
- p->virtual_object = ksbrk(length);
+ p->real_pointer = krealloc(p->real_pointer, length + 0x2000);
+ p->virtual_object = align_page(p->real_pointer);
int n = (uintptr_t)align_page((void *)(u32)length) / 0x1000;
- p->object = kmalloc(sizeof(void *) * n);
+ p->object = krealloc(p->object, sizeof(void *) * n);
for (int i = 0; i < n; i++) {
p->object[i] =
(void *)(get_page(p->virtual_object + (i * 0x1000), NULL, 0, 0)->frame *
@@ -73,9 +74,7 @@ int shm_open(const char *name, int oflag, mode_t mode) {
if (!internal_object) {
// if (!(oflag & O_CREAT))
// return -EMFILE;
- internal_object = kmalloc(sizeof(vfs_vm_object_t));
- internal_object->object = NULL;
- internal_object->size = 0;
+ internal_object = kcalloc(1, sizeof(vfs_vm_object_t));
hashmap_add_entry(shared_memory_objects, name, internal_object, NULL, 0);
}
diff --git a/kernel/fs/vfs.c b/kernel/fs/vfs.c
index 6a8dfd1..143dc9d 100644
--- a/kernel/fs/vfs.c
+++ b/kernel/fs/vfs.c
@@ -379,6 +379,10 @@ vfs_vm_object_t *vfs_get_vm_object(int fd, u64 length, u64 offset) {
if (!vfs_fd) {
return NULL;
}
+ assert(vfs_fd->inode);
+ if(!vfs_fd->inode->get_vm_object) {
+ return NULL;
+ }
vfs_vm_object_t *r = vfs_fd->inode->get_vm_object(length, offset, vfs_fd);
return r;
}
diff --git a/kernel/fs/vfs.h b/kernel/fs/vfs.h
index bee3179..74e5bf4 100644
--- a/kernel/fs/vfs.h
+++ b/kernel/fs/vfs.h
@@ -29,6 +29,7 @@ typedef struct vfs_mounts vfs_mounts_t;
#define FS_TYPE_LINK 7
struct vfs_vm_object {
+ void *real_pointer;
void *virtual_object;
void **object;
u64 size;
diff --git a/kernel/includes/syscalls.h b/kernel/includes/syscalls.h
index a0be694..cd49a1b 100644
--- a/kernel/includes/syscalls.h
+++ b/kernel/includes/syscalls.h
@@ -1,4 +1,5 @@
#include <fs/vfs.h>
+#include <queue.h>
#include <signal.h>
#include <socket.h>
#include <stddef.h>
@@ -6,7 +7,6 @@
#include <time.h>
#include <typedefs.h>
#include <types.h>
-#include <queue.h>
void syscall_outw(u16 port, u16 word);
u32 syscall_inl(u16 port);
@@ -68,6 +68,7 @@ typedef struct SYS_MMAP_PARAMS {
} __attribute__((packed)) SYS_MMAP_PARAMS;
void *syscall_mmap(SYS_MMAP_PARAMS *args);
+int syscall_munmap(void *addr, size_t length);
#ifndef MSLEEP_H
#define MSLEEP_H
void syscall_msleep(u32 ms);
diff --git a/kernel/kmalloc.c b/kernel/kmalloc.c
index f75ab33..48fb4c7 100644
--- a/kernel/kmalloc.c
+++ b/kernel/kmalloc.c
@@ -184,7 +184,7 @@ void *krealloc(void *ptr, size_t size) {
size_t l = get_mem_size(ptr);
size_t to_copy = min(l, size);
memcpy(rc, ptr, to_copy);
- // kfree(ptr);
+ kfree(ptr);
return rc;
}
diff --git a/kernel/process.s b/kernel/process.s
index 934d627..ed87959 100644
--- a/kernel/process.s
+++ b/kernel/process.s
@@ -101,8 +101,6 @@ insert_eip_on_stack:
# WARNING: Caller is expected to disable IRQs before calling, and enable IRQs again after function returns
switch_to_task:
-# cli
-
# Save previous task's state
# Notes:
diff --git a/kernel/sched/scheduler.c b/kernel/sched/scheduler.c
index e202be2..ebf2c32 100644
--- a/kernel/sched/scheduler.c
+++ b/kernel/sched/scheduler.c
@@ -535,6 +535,17 @@ void *create_physical_mapping(void **physical_addresses, size_t length) {
return rc;
}
+int munmap(void *addr, size_t length) {
+ for (int i = 0; i < 100; i++) {
+ MemoryMap *m = current_task->maps[i];
+ if (addr == m->u_address) {
+ current_task->maps[i] = NULL;
+ return 0;
+ }
+ }
+ return 0;
+}
+
void *mmap(void *addr, size_t length, int prot, int flags, int fd,
size_t offset) {
(void)addr;
diff --git a/kernel/syscalls/munmap.c b/kernel/syscalls/munmap.c
new file mode 100644
index 0000000..82f7918
--- /dev/null
+++ b/kernel/syscalls/munmap.c
@@ -0,0 +1,6 @@
+#include <sched/scheduler.h>
+#include <syscalls.h>
+
+int syscall_munmap(void *addr, size_t length) {
+return munmap(addr, length);
+}