From 6dec7db5158447b66f31a3f786ce2916cab83cec Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sun, 14 Mar 2021 16:06:57 +0100 Subject: Maaany fixes :) I don't have the motivation to write better commit messages... --- kernel/drivers/interrupts.c | 4 ++++ kernel/drivers/keyboard.c | 6 +++--- kernel/drivers/mouse.c | 6 +++--- kernel/features/fs.c | 4 ++-- kernel/features/mm.c | 4 +--- kernel/features/proc.c | 20 ++++++++++++++++---- kernel/inc/mm.h | 3 +++ 7 files changed, 32 insertions(+), 15 deletions(-) (limited to 'kernel') diff --git a/kernel/drivers/interrupts.c b/kernel/drivers/interrupts.c index fe2321a..2e1444f 100644 --- a/kernel/drivers/interrupts.c +++ b/kernel/drivers/interrupts.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -236,6 +237,9 @@ static void isr_install(void) // Set default routines for (u32 i = 0; i < 256; i++) isr_routines[i] = isr_panic; + + // Set page fault handler + isr_install_handler(14, page_fault_handler); } /** diff --git a/kernel/drivers/keyboard.c b/kernel/drivers/keyboard.c index 3ae3c0e..dbee8e1 100644 --- a/kernel/drivers/keyboard.c +++ b/kernel/drivers/keyboard.c @@ -66,10 +66,10 @@ static s32 keyboard_read(void *buf, u32 offset, u32 count, struct device *dev) if (stack_empty(queue)) return -1; - struct event *e = stack_pop(queue); - memcpy(buf, (u8 *)e + offset, count); + struct event_keyboard *e = stack_pop(queue); + memcpy(buf, (u8 *)e + offset, MIN(count, sizeof(*e))); free(e); - return count; + return MIN(count, sizeof(*e)); } static u8 keyboard_ready(void) diff --git a/kernel/drivers/mouse.c b/kernel/drivers/mouse.c index 40094d1..5c481da 100644 --- a/kernel/drivers/mouse.c +++ b/kernel/drivers/mouse.c @@ -94,10 +94,10 @@ static s32 mouse_read(void *buf, u32 offset, u32 count, struct device *dev) if (stack_empty(queue)) return -1; - struct event *e = stack_pop(queue); - memcpy(buf, (u8 *)e + offset, count); + struct event_mouse *e = stack_pop(queue); + memcpy(buf, (u8 *)e + offset, MIN(count, sizeof(*e))); free(e); - return count; + return MIN(count, sizeof(*e)); } void mouse_install(void) diff --git a/kernel/features/fs.c b/kernel/features/fs.c index 20d00e5..c8ad317 100644 --- a/kernel/features/fs.c +++ b/kernel/features/fs.c @@ -122,7 +122,7 @@ s32 vfs_mount(struct device *dev, const char *path) s32 vfs_read(const char *path, void *buf, u32 offset, u32 count) { - /* printf("%s READ: %s\n", proc_current()->name, path); */ + /* printf("%s READ: %s\n", proc_current() ? proc_current()->name : "Unknown", path); */ if (!count) return 0; @@ -148,7 +148,7 @@ s32 vfs_read(const char *path, void *buf, u32 offset, u32 count) s32 vfs_write(const char *path, void *buf, u32 offset, u32 count) { - /* printf("%s WRITE: %s\n", proc_current()->name, path); */ + /* printf("%s WRITE: %s\n", proc_current() ? proc_current()->name : "Unknown", path); */ if (!count) return 0; diff --git a/kernel/features/mm.c b/kernel/features/mm.c index bd32683..9eca438 100644 --- a/kernel/features/mm.c +++ b/kernel/features/mm.c @@ -32,7 +32,7 @@ static void paging_switch_dir(u32 dir) extern void paging_invalidate_tlb(void); -static void page_fault(struct regs *r) +void page_fault_handler(struct regs *r) { // Check error code const char *type = (r->err_code & 1) ? "present" : "non-present"; @@ -495,6 +495,4 @@ void memory_install(struct mem_info *mem_info, struct vid_info *vid_info) memory_switch_dir(&kernel_dir); paging_enable(); - - isr_install_handler(14, page_fault); } diff --git a/kernel/features/proc.c b/kernel/features/proc.c index 6bbe894..b93f7c8 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -305,6 +305,11 @@ static enum stream_defaults procfs_stream(const char *path) } } +struct procfs_message { + u8 *data; + u32 size; +}; + static s32 procfs_write(const char *path, void *buf, u32 offset, u32 count, struct device *dev) { u32 pid = 0; @@ -318,7 +323,10 @@ static s32 procfs_write(const char *path, void *buf, u32 offset, u32 count, stru if (!memcmp(path, "msg", 4)) { void *msg_data = malloc(count); memcpy(msg_data, buf, count); - stack_push_bot(p->messages, msg_data); // TODO: Use offset + struct procfs_message *msg = malloc(sizeof(*msg)); + msg->data = msg_data; + msg->size = count; + stack_push_bot(p->messages, msg); // TODO: Use offset proc_enable_waiting(pid, PROC_WAIT_MSG); return count; } else if (!memcmp(path, "io/", 3)) { @@ -371,12 +379,13 @@ static s32 procfs_read(const char *path, void *buf, u32 offset, u32 count, struc if (stack_empty(p->messages)) { return -1; // This shouldn't happen } else { - u8 *msg = stack_pop(p->messages); + struct procfs_message *msg = stack_pop(p->messages); if (!msg) return -1; - memcpy(buf, msg + offset, count); + memcpy(buf, msg->data + offset, MIN(count, msg->size)); + free(msg->data); free(msg); - return count; + return MIN(count, msg->size); } } else if (!memcmp(path, "io/", 3)) { path += 3; @@ -500,7 +509,10 @@ void proc_init(void) printf("Jumping to userspace!\n"); memory_switch_dir(((struct proc *)new->data)->page_dir); + + // You're waiting for a train. A train that will take you far away... proc_jump_userspace(); + while (1) { }; } diff --git a/kernel/inc/mm.h b/kernel/inc/mm.h index 590875c..d3e37f6 100644 --- a/kernel/inc/mm.h +++ b/kernel/inc/mm.h @@ -5,6 +5,7 @@ #include #include +#include struct memory_range { u32 base; @@ -102,4 +103,6 @@ void memory_backup_dir(struct page_dir **backup); void memory_install(struct mem_info *mem_info, struct vid_info *vid_info); +void page_fault_handler(struct regs *r); + #endif -- cgit v1.2.3