From bbf700a0c6b2f8ca9a73c2a334973286d5b8afcc Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Fri, 12 Mar 2021 19:11:26 +0100 Subject: Started basic ioctl fb interface --- kernel/features/fs.c | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'kernel/features/fs.c') diff --git a/kernel/features/fs.c b/kernel/features/fs.c index 687d7ad..4d19dde 100644 --- a/kernel/features/fs.c +++ b/kernel/features/fs.c @@ -131,7 +131,8 @@ s32 vfs_read(const char *path, void *buf, u32 offset, u32 count) path++; struct mount_info *m = vfs_find_mount_info(path); - assert(m && m->dev && m->dev->vfs && m->dev->vfs->read && m->dev->vfs->perm); + if (!(m && m->dev && m->dev->vfs && m->dev->vfs->read && m->dev->vfs->perm)) + return -1; u32 len = strlen(m->path); if (len > 1) @@ -156,7 +157,8 @@ s32 vfs_write(const char *path, void *buf, u32 offset, u32 count) path++; struct mount_info *m = vfs_find_mount_info(path); - assert(m && m->dev && m->dev->vfs && m->dev->vfs->write && m->dev->vfs->perm); + if (!(m && m->dev && m->dev->vfs && m->dev->vfs->write && m->dev->vfs->perm)) + return -1; u32 len = strlen(m->path); if (len > 1) @@ -168,6 +170,25 @@ s32 vfs_write(const char *path, void *buf, u32 offset, u32 count) return m->dev->vfs->write(path, buf, offset, count, m->dev); } +s32 vfs_ioctl(const char *path, u32 request, void *arg1, void *arg2, void *arg3) +{ + while (*path == ' ') + path++; + + struct mount_info *m = vfs_find_mount_info(path); + if (!(m && m->dev && m->dev->vfs && m->dev->vfs->ioctl && m->dev->vfs->perm)) + return -1; + + u32 len = strlen(m->path); + if (len > 1) + path += len; + + if (!m->dev->vfs->perm(path, VFS_WRITE, m->dev) && !proc_super()) + return -1; + + return m->dev->vfs->ioctl(path, request, arg1, arg2, arg3, m->dev); +} + s32 vfs_stat(const char *path, struct stat *buf) { while (*path == ' ') @@ -177,12 +198,16 @@ s32 vfs_stat(const char *path, struct stat *buf) return -1; struct mount_info *m = vfs_find_mount_info(path); - assert(m && m->dev && m->dev->vfs && m->dev->vfs->stat); + if (!(m && m->dev && m->dev->vfs && m->dev->vfs->stat && m->dev->vfs->perm)) + return -1; u32 len = strlen(m->path); if (len > 1) path += len; + if (!m->dev->vfs->perm(path, VFS_WRITE, m->dev) && !proc_super()) + return -1; + return m->dev->vfs->stat(path, buf, m->dev); } @@ -192,7 +217,8 @@ s32 vfs_wait(const char *path, u32 func_ptr) path++; struct mount_info *m = vfs_find_mount_info(path); - assert(m && m->dev && m->dev->vfs); + if (!(m && m->dev && m->dev->vfs)) + return -1; // Default wait if (!m->dev->vfs->wait) { @@ -280,10 +306,19 @@ static s32 devfs_read(const char *path, void *buf, u32 offset, u32 count, struct { struct device *target = device_get_by_name(path + 1); if (!target || !target->read) - return 0; + return -1; return target->read(buf, offset, count, dev); } +static s32 devfs_ioctl(const char *path, u32 request, void *arg1, void *arg2, void *arg3, + struct device *dev) +{ + struct device *target = device_get_by_name(path + 1); + if (!target || !target->ioctl) + return -1; + return target->ioctl(request, arg1, arg2, arg3, dev); +} + static u8 devfs_perm(const char *path, enum vfs_perm perm, struct device *dev) { (void)path; @@ -309,6 +344,7 @@ void device_install(void) struct vfs *vfs = zalloc(sizeof(*vfs)); vfs->type = VFS_DEVFS; vfs->read = devfs_read; + vfs->ioctl = devfs_ioctl; vfs->perm = devfs_perm; vfs->ready = devfs_ready; struct device *dev = zalloc(sizeof(*dev)); -- cgit v1.2.3 From 8176351e1a3d1598bedbc007897d530475942275 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sat, 13 Mar 2021 13:33:26 +0100 Subject: Nicü --- Makefile | 4 +++- apps/init.c | 6 ++---- apps/wm.c | 8 +++++--- kernel/drivers/fb.c | 23 +++++++++++++++++++++-- kernel/features/fs.c | 6 ++++-- kernel/features/load.c | 6 +++--- kernel/features/mm.c | 11 ++++++----- kernel/features/proc.c | 32 +++++++++++++++++++------------- kernel/features/syscall.c | 12 ++---------- kernel/inc/mm.h | 2 +- kernel/inc/proc.h | 1 + kernel/main.c | 2 +- libc/alloc.c | 4 +++- 13 files changed, 71 insertions(+), 46 deletions(-) (limited to 'kernel/features/fs.c') diff --git a/Makefile b/Makefile index 1e20a7a..fa1954c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,9 @@ CFLAGS_DEFAULT = $(CFLAGS_WARNINGS) $(CFLAGS_OPTIMIZATION) -std=c99 -m32 -nostdl all: compile -debug: CFLAGS_DEFAULT += -Wno-error -ggdb3 -s -fsanitize=undefined # -fstack-protector-all # TODO: Fix stack protector in userspace +# TODO: Fix stack protector in userspace +# TODO: Fix ubsan in userspace (probably due to kernel size) +debug: CFLAGS_DEFAULT += -Wno-error -ggdb3 -s #-fsanitize=undefined # -fstack-protector-all debug: compile export diff --git a/apps/init.c b/apps/init.c index 705f178..f854a81 100644 --- a/apps/init.c +++ b/apps/init.c @@ -10,11 +10,9 @@ int main(int argc, char **argv) { UNUSED(argc); UNUSED(argv); - while (1) { - }; int wm = exec("/bin/wm", "wm", NULL); - int test = exec("/bin/window", "test", NULL); + /* int test = exec("/bin/window", "test", NULL); */ - return wm + test; + return wm; //+ test; } diff --git a/apps/wm.c b/apps/wm.c index becbf0a..8eb6f70 100644 --- a/apps/wm.c +++ b/apps/wm.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -361,11 +362,12 @@ static void handle_message(struct message *msg) int main(int argc, char **argv) { - (void)argc; - screen = *(struct vbe *)argv[1]; + UNUSED(argc); + UNUSED(argv); + assert(ioctl("/dev/fb", IO_FB_GET, &screen) == 0); + log("WM loaded: %dx%d\n", screen.width, screen.height); wm_client = (struct client){ .pid = getpid() }; bypp = (screen.bpp >> 3); - log("WM loaded: %dx%d\n", screen.width, screen.height); windows = list_new(); keymap = keymap_parse("/res/keymaps/en.keymap"); diff --git a/kernel/drivers/fb.c b/kernel/drivers/fb.c index a1a7729..8e73f5b 100644 --- a/kernel/drivers/fb.c +++ b/kernel/drivers/fb.c @@ -1,13 +1,25 @@ // MIT License, Copyright (c) 2021 Marvin Borner +#include #include #include #include #include #include +#include #include #include +struct vbe_basic { + u8 stuff1[16]; + u16 pitch; + u16 width; + u16 height; + u8 stuff2[18]; + u8 *fb; + u8 stuff3[212]; +}; + static u32 dev_id = 0; static struct vid_info *info = NULL; @@ -18,9 +30,16 @@ static s32 fb_ioctl(u32 request, void *arg1, void *arg2, void *arg3, struct devi UNUSED(dev); switch (request) { - case IO_FB_GET: - memcpy(arg1, info->vbe, 256); + case IO_FB_GET: { + if (!info) + return -1; + struct vbe_basic *vbe = (struct vbe_basic *)info->vbe; + memcpy(arg1, info->vbe, sizeof(struct vbe_basic)); + u32 size = vbe->height * vbe->pitch; + memory_map_identity(proc_current()->page_dir, + memory_range_around((u32)vbe->fb, size), MEMORY_USER); return 0; + } default: return -1; } diff --git a/kernel/features/fs.c b/kernel/features/fs.c index 4d19dde..20d00e5 100644 --- a/kernel/features/fs.c +++ b/kernel/features/fs.c @@ -62,13 +62,15 @@ static struct mount_info *vfs_recursive_find(char *path) static struct mount_info *vfs_find_mount_info(const char *path) { - assert(path[0] == '/'); + if (path[0] != '/') + return NULL; return vfs_recursive_find(strdup(path)); } struct device *vfs_find_dev(const char *path) { - assert(path[0] == '/'); + if (path[0] != '/') + return NULL; struct mount_info *m = vfs_find_mount_info(path); if (m->dev->vfs->type == VFS_DEVFS) // TODO: ? return device_get_by_name(path + strlen(m->path) + 1); diff --git a/kernel/features/load.c b/kernel/features/load.c index 4ad2cbf..9e6db79 100644 --- a/kernel/features/load.c +++ b/kernel/features/load.c @@ -13,6 +13,8 @@ int bin_load(const char *path, struct proc *proc) struct stat s = { 0 }; vfs_stat(path, &s); + strcpy(proc->name, path); + struct page_dir *prev; memory_backup_dir(&prev); memory_switch_dir(proc->page_dir); @@ -20,13 +22,11 @@ int bin_load(const char *path, struct proc *proc) u32 size = PAGE_ALIGN_UP(s.size); u32 data = (u32)memory_alloc(proc->page_dir, size, MEMORY_USER | MEMORY_CLEAR); - if (!vfs_read(path, (void *)data, 0, s.size)) { + if (!vfs_read(proc->name, (void *)data, 0, s.size)) { memory_switch_dir(prev); return 1; } - strcpy(proc->name, path); - u32 stack = (u32)memory_alloc(proc->page_dir, PROC_STACK_SIZE, MEMORY_USER | MEMORY_CLEAR); proc->regs.ebp = stack; proc->regs.useresp = stack; diff --git a/kernel/features/mm.c b/kernel/features/mm.c index b5fd33c..bd32683 100644 --- a/kernel/features/mm.c +++ b/kernel/features/mm.c @@ -35,9 +35,9 @@ extern void paging_invalidate_tlb(void); static void page_fault(struct regs *r) { // Check error code - const char *type = (r->err_code & 4) ? "present" : "non-present"; + const char *type = (r->err_code & 1) ? "present" : "non-present"; const char *operation = (r->err_code & 2) ? "write" : "read"; - const char *super = (r->err_code & 1) ? "User" : "Super"; + const char *super = (r->err_code & 4) ? "User" : "Super"; // Check cr2 address u32 vaddr; @@ -446,7 +446,7 @@ static struct memory_range kernel_memory_range(void) return memory_range_around((u32)&kernel_start, (u32)&kernel_end - (u32)&kernel_start); } -void memory_install(struct mem_info *mem_info) +void memory_install(struct mem_info *mem_info, struct vid_info *vid_info) { for (struct mmap_boot *p = mem_info->start; (u32)(p - mem_info->start) < mem_info->size; p++) { @@ -484,8 +484,9 @@ void memory_install(struct mem_info *mem_info) memory_map_identity(&kernel_dir, memory_range_around(STACK_START - STACK_SIZE, STACK_SIZE), MEMORY_NONE); - // TODO: Triple fault prevention? Probably bootloader stuff or something - memory_map_identity(&kernel_dir, memory_range_around(0x7000, 0x1000), MEMORY_NONE); + // Map VBE data + memory_map_identity(&kernel_dir, memory_range_around((u32)vid_info->vbe, 0x1000), + MEMORY_NONE); // Unmap NULL byte/page struct memory_range zero = memory_range(0, PAGE_SIZE); diff --git a/kernel/features/proc.c b/kernel/features/proc.c index e7ddf4b..6bbe894 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -157,6 +157,9 @@ void proc_yield(struct regs *r) void proc_enable_waiting(u32 id, enum proc_wait_type type) { + struct page_dir *dir_bak; + memory_backup_dir(&dir_bak); + struct proc *proc_bak = proc_current(); if (!proc_bak) return; @@ -179,8 +182,11 @@ void proc_enable_waiting(u32 id, enum proc_wait_type type) struct regs *r = &p->regs; u32 (*func)(u32, u32, u32, u32) = (u32(*)(u32, u32, u32, u32))w->ids[i].func_ptr; - if (w->ids[i].func_ptr) + if (w->ids[i].func_ptr) { + memory_switch_dir(p->page_dir); r->eax = func(r->ebx, r->ecx, r->edx, r->esi); + memory_switch_dir(dir_bak); + } memset(&w->ids[i], 0, sizeof(w->ids[i])); p->wait.id_cnt--; p->state = PROC_RUNNING; @@ -252,6 +258,18 @@ struct proc *proc_make(enum proc_priv priv) return proc; } +void proc_stack_push(struct proc *proc, u32 data) +{ + struct page_dir *prev; + memory_backup_dir(&prev); + memory_switch_dir(proc->page_dir); + + proc->regs.useresp -= sizeof(data); + *(u32 *)proc->regs.useresp = data; + + memory_switch_dir(prev); +} + // TODO: Procfs needs a simpler interface structure (memcmp and everything sucks) static const char *procfs_parse_path(const char **path, u32 *pid) @@ -437,18 +455,6 @@ static u8 procfs_ready(const char *path, struct device *dev) return 1; } -static void proc_stack_push(struct proc *proc, u32 data) -{ - struct page_dir *prev; - memory_backup_dir(&prev); - memory_switch_dir(proc->page_dir); - - proc->regs.useresp -= sizeof(data); - *(u32 *)proc->regs.useresp = data; - - memory_switch_dir(prev); -} - extern void proc_jump_userspace(void); u32 _esp, _eip; diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c index 31cdf5f..dc49d67 100644 --- a/kernel/features/syscall.c +++ b/kernel/features/syscall.c @@ -71,22 +71,14 @@ static void syscall_handler(struct regs *r) char *path = (char *)r->ebx; struct proc *proc = proc_make(PROC_PRIV_NONE); r->eax = (u32)bin_load(path, proc); - u32 argc = 3; // TODO: Add argc evaluator - char **argv = malloc(sizeof(*argv) * (argc + 1)); - argv[0] = (char *)r->ecx; - argv[1] = (char *)r->edx; - argv[2] = (char *)r->esi; - argv[3] = (char *)r->edi; - argv[4] = NULL; - ((u32 *)proc->regs.useresp)[0] = argc; - ((u32 *)proc->regs.useresp)[1] = (u32)argv; + // TODO: Reimplement argc,argv + proc_stack_push(proc, 0); if (r->eax) proc_exit(proc, (int)r->eax); proc_yield(r); break; } case SYS_EXIT: { - print("EXIT!\n"); proc_exit(proc_current(), (int)r->ebx); break; } diff --git a/kernel/inc/mm.h b/kernel/inc/mm.h index 6a1c063..590875c 100644 --- a/kernel/inc/mm.h +++ b/kernel/inc/mm.h @@ -100,6 +100,6 @@ void memory_free(struct page_dir *dir, struct memory_range vrange); void memory_switch_dir(struct page_dir *dir); void memory_backup_dir(struct page_dir **backup); -void memory_install(struct mem_info *mem_info); +void memory_install(struct mem_info *mem_info, struct vid_info *vid_info); #endif diff --git a/kernel/inc/proc.h b/kernel/inc/proc.h index de4eb84..a44fd68 100644 --- a/kernel/inc/proc.h +++ b/kernel/inc/proc.h @@ -70,5 +70,6 @@ void proc_clear_quantum(void); void proc_enable_waiting(u32 id, enum proc_wait_type type); void proc_wait_for(u32 id, enum proc_wait_type type, u32 func_ptr); struct proc *proc_make(enum proc_priv priv); +void proc_stack_push(struct proc *proc, u32 data); #endif diff --git a/kernel/main.c b/kernel/main.c index 118fe11..3b75c7d 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -30,7 +30,7 @@ void kernel_main(struct mem_info *mem_info, struct vid_info *vid_info) serial_print("\nKernel was compiled at " __TIME__ " on " __DATE__ "\n"); serial_print("Serial connected.\n"); - memory_install(mem_info); + memory_install(mem_info, vid_info); cpu_enable_features(); cpu_print(); diff --git a/libc/alloc.c b/libc/alloc.c index 083f2c0..4ccf35f 100644 --- a/libc/alloc.c +++ b/libc/alloc.c @@ -139,7 +139,7 @@ static void *_malloc(u32 req_size) l_mem_root = allocate_new_page(size); if (l_mem_root == NULL) { liballoc_unlock(); - return NULL; + panic("Malloc failed!\n"); } } @@ -295,6 +295,7 @@ static void *_malloc(u32 req_size) liballoc_unlock(); + panic("Malloc failed!\n"); return NULL; } @@ -361,6 +362,7 @@ static void *_realloc(void *ptr, u32 size) if (min->magic != LIBALLOC_MAGIC) { liballoc_unlock(); + panic("Malloc failed!\n"); return NULL; } -- cgit v1.2.3 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... --- apps/init.c | 6 ++-- apps/window.c | 4 +++ apps/wm.c | 71 ++++++++++++++++++++------------------------- 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 ++ libc/alloc.c | 2 -- libc/inc/def.h | 3 ++ libc/inc/sys.h | 4 ++- libgui/gfx.c | 9 +++--- libgui/gui.c | 18 ++++++++---- libgui/inc/gfx.h | 7 ++--- libgui/inc/gui.h | 3 +- libgui/inc/msg.h | 24 +++++++++++---- libgui/msg.c | 22 +++++++------- run | 9 +++++- 20 files changed, 135 insertions(+), 94 deletions(-) (limited to 'kernel/features/fs.c') diff --git a/apps/init.c b/apps/init.c index f854a81..c59e3bf 100644 --- a/apps/init.c +++ b/apps/init.c @@ -6,13 +6,15 @@ #include #include +#include + int main(int argc, char **argv) { UNUSED(argc); UNUSED(argv); int wm = exec("/bin/wm", "wm", NULL); - /* int test = exec("/bin/window", "test", NULL); */ + int test = exec("/bin/window", "test", NULL); - return wm; //+ test; + return wm + test; } diff --git a/apps/window.c b/apps/window.c index ac29ba1..90a414c 100644 --- a/apps/window.c +++ b/apps/window.c @@ -8,6 +8,9 @@ int main(void) { struct gui_window win = { 0 }; assert(gui_new_window(&win) > 0); + while (1) + ; +#if 0 gfx_fill(win.ctx, COLOR_GREEN); // Professional testing for (int i = 0; i < 12; i++) { @@ -16,5 +19,6 @@ int main(void) } assert(gui_redraw_window(win.id) > 0); log("%d\n", win.ctx->size.x); +#endif return 0; } diff --git a/apps/wm.c b/apps/wm.c index 8eb6f70..ecc7cb2 100644 --- a/apps/wm.c +++ b/apps/wm.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -308,46 +309,34 @@ static void handle_event_mouse(struct event_mouse *event) window_redraw(cursor); } -static void handle_message_new_window(struct message *msg) +static void handle_message_new_window(struct message_new_window *msg) { - if (!msg->data) { - msg_send(msg->src, GUI_NEW_WINDOW | MSG_FAILURE, NULL); - return; - } - struct gui_window *buf = msg->data; - struct window *win = window_new((struct client){ .pid = msg->src }, "idk", vec2(500, 600), - vec2(600, 400), 0); - buf->id = win->id; - buf->ctx = &win->ctx; - buf->pos = &win->pos; - msg_send(msg->src, GUI_NEW_WINDOW | MSG_SUCCESS, NULL); + struct window *win = window_new((struct client){ .pid = msg->header.src }, "idk", + vec2(500, 600), vec2(600, 400), 0); + msg->ctx = win->ctx; + msg->id = win->id; + msg_send(msg->header.src, GUI_NEW_WINDOW | MSG_SUCCESS, msg, sizeof(*msg)); /* window_redraw(win); */ } -static void handle_message_redraw_window(struct message *msg) +static void handle_message_redraw_window(struct message_redraw_window *msg) { - if (!msg->data) { - msg_send(msg->src, GUI_REDRAW_WINDOW | MSG_FAILURE, NULL); - return; - } - u32 id = *(u32 *)msg->data; + u32 id = msg->id; struct window *win = window_find(id); if (!win) { - msg_send(msg->src, GUI_REDRAW_WINDOW | MSG_FAILURE, NULL); + msg_send(msg->header.src, GUI_REDRAW_WINDOW | MSG_FAILURE, NULL, + sizeof(msg->header)); return; } - msg_send(msg->src, GUI_REDRAW_WINDOW | MSG_SUCCESS, NULL); + msg_send(msg->header.src, GUI_REDRAW_WINDOW | MSG_SUCCESS, NULL, sizeof(msg->header)); window_redraw(win); } -static void handle_message(struct message *msg) +static void handle_message(void *msg) { - if (msg->magic != MSG_MAGIC) { - log("Message magic doesn't match!\n"); - return; - } + struct message_header *header = msg; - switch (msg->type) { + switch (header->type) { case GUI_NEW_WINDOW: handle_message_new_window(msg); break; @@ -355,8 +344,8 @@ static void handle_message(struct message *msg) handle_message_redraw_window(msg); break; default: - log("Message type %d not implemented!\n", msg->type); - msg_send(msg->src, MSG_FAILURE, NULL); + log("Message type %d not implemented!\n", header->type); + msg_send(header->src, MSG_FAILURE, NULL, sizeof(*header)); } } @@ -389,32 +378,34 @@ int main(int argc, char **argv) gfx_load_wallpaper(&cursor->ctx, "/res/cursor.png"); window_redraw(wallpaper); - struct message msg = { 0 }; + u8 msg[1024] = { 0 }; struct event_keyboard event_keyboard = { 0 }; struct event_mouse event_mouse = { 0 }; - const char *listeners[] = { "/dev/kbd", "/dev/mouse", "/proc/self/msg" }; + const char *listeners[] = { "/dev/kbd", "/dev/mouse", "/proc/self/msg", NULL }; while (1) { int poll_ret = 0; if ((poll_ret = poll(listeners)) >= 0) { if (poll_ret == 0) { if (read(listeners[poll_ret], &event_keyboard, 0, - sizeof(event_keyboard)) > 0) + sizeof(event_keyboard)) > 0) { handle_event_keyboard(&event_keyboard); - continue; + continue; + } } else if (poll_ret == 1) { if (read(listeners[poll_ret], &event_mouse, 0, - sizeof(event_mouse)) > 0) + sizeof(event_mouse)) > 0) { handle_event_mouse(&event_mouse); - continue; + continue; + } } else if (poll_ret == 2) { - if (read(listeners[poll_ret], &msg, 0, sizeof(msg)) > 0) - handle_message(&msg); - continue; + if (msg_receive(msg, 1024) > 0) { + handle_message(msg); + continue; + } } - } else { - err(1, "POLL ERROR!\n"); } - }; + panic("Poll/read error!\n"); + } // TODO: Execute? free(keymap); 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 diff --git a/libc/alloc.c b/libc/alloc.c index 4ccf35f..be986dc 100644 --- a/libc/alloc.c +++ b/libc/alloc.c @@ -86,8 +86,6 @@ struct liballoc_minor { #define MAJOR_SIZE (ALIGN_UP(sizeof(struct liballoc_major), 16)) #define MINOR_SIZE (ALIGN_UP(sizeof(struct liballoc_minor), 16)) -#define MIN(__x, __y) ((__x) < (__y) ? (__x) : (__y)) -#define MAX(__x, __y) ((__x) > (__y) ? (__x) : (__y)) static struct liballoc_major *l_mem_root = NULL; static struct liballoc_major *l_best_bet = NULL; diff --git a/libc/inc/def.h b/libc/inc/def.h index db1c95e..c334fcb 100644 --- a/libc/inc/def.h +++ b/libc/inc/def.h @@ -25,6 +25,9 @@ typedef unsigned long long u64; #define UNUSED(a) ((void)(a)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + #define NORETURN __attribute__((noreturn)) #define NO_SANITIZE __attribute__((no_sanitize("undefined"))) #define PACKED __attribute__((packed)) diff --git a/libc/inc/sys.h b/libc/inc/sys.h index 3125cb0..8add0de 100644 --- a/libc/inc/sys.h +++ b/libc/inc/sys.h @@ -93,7 +93,9 @@ int sysv(enum sys num, ...); static inline u32 getpid(void) { - u32 buf = 0; + static u32 buf = 0; + if (buf) + return buf; read("/proc/self/pid", &buf, 0, sizeof(buf)); return buf; } diff --git a/libgui/gfx.c b/libgui/gfx.c index d9457c7..dad8b88 100644 --- a/libgui/gfx.c +++ b/libgui/gfx.c @@ -98,10 +98,11 @@ static void draw_rectangle(struct context *ctx, vec2 pos1, vec2 pos2, u32 c) struct context *gfx_new_ctx(struct context *ctx) { - struct message msg = { 0 }; - assert(msg_send(pidof(WM_PATH), GFX_NEW_CONTEXT, ctx) > 0); - assert(msg_receive(&msg) > 0); - memcpy(ctx, msg.data, sizeof(*ctx)); + /* struct message msg = { 0 }; */ + assert(0); + /* assert(msg_send(pidof(WM_PATH), GFX_NEW_CONTEXT, ctx) > 0); */ + /* assert(msg_receive(&msg) > 0); */ + /* memcpy(ctx, msg.data, sizeof(*ctx)); */ return ctx; } diff --git a/libgui/gui.c b/libgui/gui.c index 2083f23..bc8adb1 100644 --- a/libgui/gui.c +++ b/libgui/gui.c @@ -2,24 +2,30 @@ #include #include +#include #include #define WM_PATH "/bin/wm" s32 gui_new_window(struct gui_window *win) { - struct message msg = { 0 }; - if (msg_send(pidof(WM_PATH), GUI_NEW_WINDOW, win) > 0 && msg_receive(&msg) > 0 && - msg.type == (GUI_NEW_WINDOW | MSG_SUCCESS)) + struct message_new_window msg = { 0 }; + if (msg_send(pidof(WM_PATH), GUI_NEW_WINDOW, &msg, sizeof(msg)) > 0 && + msg_receive(&msg, sizeof(msg)) > 0 && + msg.header.type == (GUI_NEW_WINDOW | MSG_SUCCESS)) { + win->id = msg.id; + win->ctx = msg.ctx; return win->id; + } return -1; } s32 gui_redraw_window(u32 id) { - struct message msg = { 0 }; - if (msg_send(pidof(WM_PATH), GUI_REDRAW_WINDOW, &id) > 0 && msg_receive(&msg) > 0 && - msg.type == (GUI_REDRAW_WINDOW | MSG_SUCCESS)) + struct message_redraw_window msg = { .id = id }; + if (msg_send(pidof(WM_PATH), GUI_REDRAW_WINDOW, &msg, sizeof(msg)) > 0 && + msg_receive(&msg, sizeof(msg)) > 0 && + msg.header.type == (GUI_REDRAW_WINDOW | MSG_SUCCESS)) return id; return -1; } diff --git a/libgui/inc/gfx.h b/libgui/inc/gfx.h index 4a358ca..f3555a4 100644 --- a/libgui/inc/gfx.h +++ b/libgui/inc/gfx.h @@ -5,7 +5,6 @@ #define GFX_H #include -#include #include #include #include @@ -82,8 +81,8 @@ int gfx_font_width(enum font_type); * Wrappers */ -#define gfx_redraw() \ - (msg_send(pidof(WM_PATH), GFX_REDRAW, NULL)) // TODO: Partial redraw (optimization) -#define gfx_redraw_focused() (msg_send(pidof(WM_PATH), GFX_REDRAW_FOCUSED, NULL)) +/* #define gfx_redraw() \ */ +/* (msg_send(pidof(WM_PATH), GFX_REDRAW, NULL)) // TODO: Partial redraw (optimization) */ +/* #define gfx_redraw_focused() (msg_send(pidof(WM_PATH), GFX_REDRAW_FOCUSED, NULL)) */ #endif diff --git a/libgui/inc/gui.h b/libgui/inc/gui.h index 460bf88..d160333 100644 --- a/libgui/inc/gui.h +++ b/libgui/inc/gui.h @@ -8,8 +8,7 @@ struct gui_window { u32 id; - struct context *ctx; - vec2 *pos; + struct context ctx; }; s32 gui_new_window(struct gui_window *win); diff --git a/libgui/inc/msg.h b/libgui/inc/msg.h index db00460..7cbfa2c 100644 --- a/libgui/inc/msg.h +++ b/libgui/inc/msg.h @@ -4,16 +4,28 @@ #define MSG_H #include +#include #define MSG_MAGIC 0x42042069 #define MSG_SUCCESS (1 << 29) #define MSG_FAILURE (1 << 30) -struct message { +struct message_header { u32 magic; - int src; - int type; - void *data; + u32 src; + u32 type; + u32 size; +}; + +struct message_new_window { + struct message_header header; + u32 id; + struct context ctx; +}; + +struct message_redraw_window { + struct message_header header; + u32 id; }; enum message_type { @@ -32,7 +44,7 @@ enum message_type { GUI_MAX }; -int msg_send(u32 pid, enum message_type, void *data); -int msg_receive(struct message *msg); +int msg_send(u32 pid, enum message_type type, void *data, u32 size); +int msg_receive(void *buf, u32 size); #endif diff --git a/libgui/msg.c b/libgui/msg.c index 8448e73..3f58267 100644 --- a/libgui/msg.c +++ b/libgui/msg.c @@ -5,23 +5,23 @@ #include #include -int msg_send(u32 pid, enum message_type type, void *data) +int msg_send(u32 pid, enum message_type type, void *data, u32 size) { - struct message msg = { 0 }; - assert((signed)pid != -1); + assert((signed)pid != -1 && size >= sizeof(struct message_header)); char path[32] = { 0 }; sprintf(path, "/proc/%d/msg", pid); - msg.magic = MSG_MAGIC; - msg.src = getpid(); - msg.type = type; - msg.data = data; - return write(path, &msg, 0, sizeof(msg)); + struct message_header *header = data; + header->magic = MSG_MAGIC; + header->src = getpid(); + header->type = type; + return write(path, data, 0, size); } -int msg_receive(struct message *msg) +int msg_receive(void *buf, u32 size) { - int ret = read("/proc/self/msg", msg, 0, sizeof(*msg)); - if (msg->magic == MSG_MAGIC && ret == sizeof(*msg)) + int ret = read("/proc/self/msg", buf, 0, size); + struct message_header *header = buf; + if (header->magic == MSG_MAGIC) return ret; else return -1; diff --git a/run b/run index 1a8202a..cd03ba3 100755 --- a/run +++ b/run @@ -26,7 +26,7 @@ no_ask="${2}" # TODO: Support -enable-kvm: GPF?! qemu_with_flags() { network="rtl8139" - qemu-system-i386 -cpu max -no-reboot -vga std -rtc base=localtime -m 256M -netdev user,id=net0,hostfwd=tcp:127.0.0.1:8000-10.0.2.15:8000 -device $network,netdev=net0 -object filter-dump,id=dump,netdev=net0,file=dump.pcap "$@" + qemu-system-i386 -d guest_errors -cpu max -no-reboot -vga std -rtc base=localtime -m 256M -netdev user,id=net0,hostfwd=tcp:127.0.0.1:8000-10.0.2.15:8000 -device $network,netdev=net0 -object filter-dump,id=dump,netdev=net0,file=dump.pcap "$@" } make_cross() { @@ -189,6 +189,10 @@ make_addr() { addr2line -e build/"$1".elf -f -p "$2" } +make_cloc() { + cloc . --exclude-dir=build,iso,disk,res,cross +} + make_append_commands() { s="" while read -r data; do @@ -239,6 +243,8 @@ elif [ "${mode}" = "disasm" ]; then make_disasm "$2" "$3" elif [ "${mode}" = "addr" ]; then make_addr "$2" "$3" +elif [ "${mode}" = "cloc" ]; then + make_cloc elif [ "${mode}" = "sync" ]; then make_sync elif [ "${mode}" = "disk" ]; then @@ -267,6 +273,7 @@ else printf "again\t\tOpens QEMU again using the previous build\n" printf "disasm\t\tDisassembles a given part of Melvix\n" printf "addr\t\tResolves an address to a line of code\n" + printf "cloc\t\tCount the total lines of code\n" printf "sync\t\tSyncs the 'tags' and 'compile_commands.json' file\n" printf "disk\t\tPrepares the userspace disk (e.g. fonts)\n" printf "*\t\tAnything else prints this help\n" -- cgit v1.2.3