From 8176351e1a3d1598bedbc007897d530475942275 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sat, 13 Mar 2021 13:33:26 +0100 Subject: Nicü --- 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 +- 9 files changed, 58 insertions(+), 37 deletions(-) (limited to 'kernel') 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(); -- cgit v1.2.3