diff options
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | apps/Makefile | 7 | ||||
-rw-r--r-- | apps/init.c | 5 | ||||
-rw-r--r-- | apps/window.c | 3 | ||||
-rw-r--r-- | kernel/features/load.c | 29 | ||||
-rw-r--r-- | kernel/features/mm.c | 11 | ||||
-rw-r--r-- | kernel/features/proc.c | 4 | ||||
-rw-r--r-- | kernel/features/syscall.c | 2 | ||||
-rw-r--r-- | kernel/main.c | 2 | ||||
-rwxr-xr-x | run | 6 |
10 files changed, 40 insertions, 30 deletions
@@ -27,6 +27,7 @@ This project is somewhat of a coding playground for me. It doesn't have any usef - Fast boot time (< 1s) - TCP/IP stack and rtl8139 driver - Small size (< 100KiB) +- Sandboxed processes (paging, ring 3, iomap) - Full UBSan support - Compiles with `-Wall -Wextra -Werror -pedantic-errors -std=c99 -Ofast` diff --git a/apps/Makefile b/apps/Makefile index 1a7aff3..9ad268e 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -5,16 +5,15 @@ CC = ccache ../cross/opt/bin/i686-elf-gcc LD = ccache ../cross/opt/bin/i686-elf-ld OC = ccache ../cross/opt/bin/i686-elf-objcopy -CFLAGS = $(CFLAGS_DEFAULT) -I../libc/inc/ -I../libgui/inc/ -I../libtxt/inc/ -fPIE -Duserspace +CFLAGS = $(CFLAGS_DEFAULT) -I../libc/inc/ -I../libgui/inc/ -I../libtxt/inc/ -Duserspace all: $(COBJS) %.o: %.c @mkdir -p ../build/apps/ @$(CC) -c $(CFLAGS) $< -o $@ - @$(LD) -o $(@:.o=.elf) -Tlink.ld -L../build/ $@ -lgui -ltxt -lc - @$(OC) -O binary $(@:.o=.elf) ../build/apps/$(@:.o=) - @cp $(@:.o=.elf) ../build/apps/$(@:.o=.elf) + @$(LD) -o $(@:.o=.elf) -L../build/ $@ -lgui -ltxt -lc + @cp $(@:.o=.elf) ../build/apps/$(@:.o=) # %.o: %.c # @mkdir -p ../build/apps/ diff --git a/apps/init.c b/apps/init.c index 35d296b..c26e3ad 100644 --- a/apps/init.c +++ b/apps/init.c @@ -8,9 +8,12 @@ int main(int argc, char **argv) { UNUSED(argc); UNUSED(argv); + log("Arrived!\n"); + while (1) + ; assert(exec("/bin/wm", "wm", NULL) == 0); - /* assert(exec("/bin/window", "test", NULL) == 0); */ + assert(exec("/bin/window", "test", NULL) == 0); return 0; } diff --git a/apps/window.c b/apps/window.c index 90a414c..0f9e7d1 100644 --- a/apps/window.c +++ b/apps/window.c @@ -8,8 +8,7 @@ int main(void) { struct gui_window win = { 0 }; assert(gui_new_window(&win) > 0); - while (1) - ; + return 0; #if 0 gfx_fill(win.ctx, COLOR_GREEN); // Professional testing diff --git a/kernel/features/load.c b/kernel/features/load.c index 7d0c3c0..079cbbe 100644 --- a/kernel/features/load.c +++ b/kernel/features/load.c @@ -13,6 +13,10 @@ s32 bin_load(const char *path, struct proc *proc) { + UNUSED(path); + UNUSED(proc); + panic("Deprecated!\n"); +#if 0 if (!path || !memory_valid(path) || !proc) return -EFAULT; @@ -48,6 +52,7 @@ s32 bin_load(const char *path, struct proc *proc) memory_switch_dir(prev); return 0; +#endif } s32 elf_load(const char *path, struct proc *proc) @@ -56,14 +61,10 @@ s32 elf_load(const char *path, struct proc *proc) return -EFAULT; struct stat s = { 0 }; - memory_bypass_enable(); s32 stat = vfs_stat(path, &s); - memory_bypass_disable(); if (stat != 0) return stat; - strcpy(proc->name, path); - struct elf_header header = { 0 }; s32 read = vfs_read(path, &header, 0, sizeof(header)); if (read < 0) @@ -71,6 +72,8 @@ s32 elf_load(const char *path, struct proc *proc) if (read != sizeof(header)) return -ENOEXEC; + strcpy(proc->name, path); + // Valid? u8 *magic = header.ident; u8 valid_magic = magic[ELF_IDENT_MAG0] == ELF_MAG0 && magic[ELF_IDENT_MAG1] == ELF_MAG1 && @@ -81,9 +84,6 @@ s32 elf_load(const char *path, struct proc *proc) header.version != 1 || header.machine != ELF_MACHINE_386) return -ENOEXEC; - // I need to implement ext2 offset reading first - panic("TUDU, Marvin!\n"); - for (u32 i = 0; i < header.phnum; i++) { struct elf_program program = { 0 }; if (vfs_read(path, &program, header.phoff + header.phentsize * i, @@ -100,12 +100,13 @@ s32 elf_load(const char *path, struct proc *proc) memory_backup_dir(&prev); memory_switch_dir(proc->page_dir); - struct memory_range range = memory_range_around(program.vaddr, program.memsz); - struct memory_range prange = physical_alloc(range.size); - virtual_map(proc->page_dir, prange, range.base, MEMORY_CLEAR | MEMORY_USER); + struct memory_range vrange = memory_range_around(program.vaddr, program.memsz); + struct memory_range prange = physical_alloc(vrange.size); + virtual_map(proc->page_dir, prange, vrange.base, MEMORY_CLEAR | MEMORY_USER); - if ((u32)vfs_read(proc->name, (void *)program.vaddr, program.offset, - program.filesz) != program.filesz) { + if ((u32)vfs_read(path, (void *)program.vaddr, program.offset, program.filesz) != + program.filesz) { + print("OH NOSE!\n"); memory_switch_dir(prev); return -ENOEXEC; } @@ -118,8 +119,8 @@ s32 elf_load(const char *path, struct proc *proc) memory_switch_dir(proc->page_dir); u32 stack = (u32)memory_alloc(proc->page_dir, PROC_STACK_SIZE, MEMORY_USER | MEMORY_CLEAR); - proc->regs.ebp = stack; - proc->regs.useresp = stack; + proc->regs.ebp = stack + PROC_STACK_SIZE - 1; + proc->regs.useresp = stack + PROC_STACK_SIZE - 1; proc->regs.eip = header.entry; proc->entry = header.entry; diff --git a/kernel/features/mm.c b/kernel/features/mm.c index d007039..af5e070 100644 --- a/kernel/features/mm.c +++ b/kernel/features/mm.c @@ -208,11 +208,15 @@ void virtual_map(struct page_dir *dir, struct memory_range prange, u32 vaddr, u3 struct page_table *table = (struct page_table *)(dir_entry->bits.address * PAGE_SIZE); - if (!dir_entry->bits.present) { + if (dir_entry->bits.present) { + // TODO: Is this a security risk? + if (flags & MEMORY_USER) + dir_entry->bits.user = 1; + } else { table = memory_alloc_identity(dir, MEMORY_CLEAR); dir_entry->bits.present = 1; dir_entry->bits.writable = 1; - dir_entry->bits.user = 1; + dir_entry->bits.user = flags & MEMORY_USER; dir_entry->bits.address = (u32)(table) >> 12; } @@ -424,8 +428,9 @@ void memory_bypass_disable(void) // TODO: Limit by proc stack and data range u8 memory_valid(const void *addr) { + /* return ((u32)addr) / PAGE_SIZE / PAGE_COUNT >= PAGE_KERNEL_COUNT; */ if (proc_current() && !memory_bypass_validity) - return ((u32)addr) / PAGE_SIZE / PAGE_COUNT >= PAGE_KERNEL_COUNT; + return (u32)addr >= 0x100000; else return 1; } diff --git a/kernel/features/proc.c b/kernel/features/proc.c index cd0fc8d..5c38fff 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -497,13 +497,13 @@ void proc_init(void) // Idle proc struct proc *kernel_proc = proc_make(PROC_PRIV_NONE); - assert(bin_load("/bin/idle", kernel_proc) == 0); + assert(elf_load("/bin/idle", kernel_proc) == 0); kernel_proc->state = PROC_SLEEPING; idle_proc = list_add(proc_list, kernel_proc); // Init proc (root) struct node *new = list_add(proc_list, proc_make(PROC_PRIV_ROOT)); - assert(bin_load("/bin/init", new->data) == 0); + assert(elf_load("/bin/init", new->data) == 0); current = new; proc_stack_push(new->data, 0); diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c index b0b37ac..2796372 100644 --- a/kernel/features/syscall.c +++ b/kernel/features/syscall.c @@ -72,7 +72,7 @@ static void syscall_handler(struct regs *r) case SYS_EXEC: { char *path = (char *)r->ebx; struct proc *proc = proc_make(PROC_PRIV_NONE); - r->eax = (u32)bin_load(path, proc); + r->eax = (u32)elf_load(path, proc); if (r->eax != 0) proc_exit(proc, -r->eax); // TODO: Reimplement argc,argv diff --git a/kernel/main.c b/kernel/main.c index 3b75c7d..d1afc01 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -18,6 +18,8 @@ #include <syscall.h> #include <timer.h> +#include <print.h> + void kernel_main(struct mem_info *mem_info, struct vid_info *vid_info); // Decl void kernel_main(struct mem_info *mem_info, struct vid_info *vid_info) { @@ -26,7 +26,7 @@ no_ask="${2}" # TODO: Support -enable-kvm: GPF?! qemu_with_flags() { network="rtl8139" - 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 "$@" + 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 "$@" } make_cross() { @@ -173,10 +173,10 @@ make_debug() { make_disasm() { if [ -z "$1" ]; then - echo "Usage: './run disasm {kernel, apps/wm, ...} [-S]'" + echo "Usage: './run disasm {kernel.elf, apps/wm, ...} [-S]'" exit 1 fi - objdump -drwC "$2" -Mintel build/"$1".elf | less -R + objdump -drwC "$2" -Mintel build/"$1" | less -R #hexdump -C build/kernel.bin | less -R } |