From 3ca8b5a232ab0fbdfa046aae79c5ef77f6b3f609 Mon Sep 17 00:00:00 2001 From: pjht Date: Sun, 25 Aug 2019 17:32:08 -0500 Subject: [PATCH] Add a very basic exit function and fix kmalloc. Exit function does not deallocate task memory except for the data block, and does not are about the exit code except for logging. --- .gdbinit | 22 ---------------------- kernel/cpu/i386/isr.c | 2 ++ kernel/cpu/i386/kmalloc.c | 2 +- kernel/cpu/i386/tasking.c | 23 +++++++++++++++++++++++ kernel/cpu/tasking.h | 1 + kernel/kernel.c | 1 + libc/tasking.c | 7 +++++++ sysroot/usr/include/stdlib.h | 3 +++ 8 files changed, 38 insertions(+), 23 deletions(-) diff --git a/.gdbinit b/.gdbinit index 62ff712..c4c2cf0 100644 --- a/.gdbinit +++ b/.gdbinit @@ -1,25 +1,3 @@ set pagination off target remote localhost:1234 symbol-file kernel/kernel.elf -add-symbol-file vfs/vfs -b kernel/cpu/i386/tasking.c:132 -b kernel/cpu/i386/tasking.c:143 -commands 1 2 -silent - disable breakpoints - symbol-file kernel/kernel.elf - p task->pid - if task->pid==2 - add-symbol-file vfs/vfs - enable breakpoints - else - enable breakpoints 1 - c - end - c -end - -b main -commands - disable breakpoints 1 -end diff --git a/kernel/cpu/i386/isr.c b/kernel/cpu/i386/isr.c index e7337da..5a2c241 100644 --- a/kernel/cpu/i386/isr.c +++ b/kernel/cpu/i386/isr.c @@ -230,6 +230,8 @@ void isr_handler(registers_t r) { tasking_yieldToPID(r.ebx); } else if (r.eax==16) { serial_write_string((char*)r.ebx); + } else if (r.eax==17) { + tasking_exit((uint8_t)r.ebx); } break; } diff --git a/kernel/cpu/i386/kmalloc.c b/kernel/cpu/i386/kmalloc.c index 2ac51cd..a19a1dd 100644 --- a/kernel/cpu/i386/kmalloc.c +++ b/kernel/cpu/i386/kmalloc.c @@ -81,7 +81,7 @@ void* kmalloc(uint32_t size) { } void kfree(void* mem) { - uint32_t* info=(uint32_t*)((uint32_t)mem-12); + uint32_t* info=(uint32_t*)((uint32_t)mem-8); uint32_t num_4b_grps=info[0]; uint32_t bmap_index=info[1]; for (uint32_t i=0;ipid,code); + if (currentTask->prev) { + currentTask->prev->next=currentTask->next; + } + if (currentTask->next) { + currentTask->next->prev=currentTask->prev; + } + if (headTask==currentTask) { + if (!currentTask->next) { + serial_write_string("ERROR! Head task exited with no child tasks! Halting!\n"); + vga_write_string("ERROR! Head task exited with no child tasks! Halting!\n"); + halt(); + } + headTask=currentTask->next; + } + Task* task=currentTask->next; + kfree(currentTask); + serial_printf("Exit yielding to PID %d.\n",task->pid); + load_smap(task->cr3); + switch_to_task(task); +} + uint32_t getPID() { return currentTask->pid; } diff --git a/kernel/cpu/tasking.h b/kernel/cpu/tasking.h index 7673c10..28cb0d3 100644 --- a/kernel/cpu/tasking.h +++ b/kernel/cpu/tasking.h @@ -11,4 +11,5 @@ Task* tasking_createTask(void* eip); Task* tasking_createTaskCr3KmodeParam(void* eip,void* cr3,char kmode,char param1_exists,uint32_t param1_arg,char param2_exists,uint32_t param2_arg); char isPrivleged(uint32_t pid); uint32_t getPID(); +void tasking_exit(uint8_t code); #endif diff --git a/kernel/kernel.c b/kernel/kernel.c index 1b25ea3..836f72a 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -106,6 +106,7 @@ void kmain(struct multiboot_boot_header_tag* hdr) { memcpy(initrd2,initrd,initrd_sz); initrd2=put_data(cr3,initrd2,initrd_sz); createTaskCr3Param((void*)header.entry,cr3,(uint32_t)initrd2,initrd_sz); + exit(1); for(;;) { yield(); } diff --git a/libc/tasking.c b/libc/tasking.c index 67a47be..7ff70a8 100644 --- a/libc/tasking.c +++ b/libc/tasking.c @@ -34,3 +34,10 @@ void yieldToPID(uint32_t pid) { int $80; \ "::"b"(pid)); } + +void exit(uint8_t code) { + asm volatile(" \ + mov $17, %%eax; \ + int $80; \ + "::"b"(code)); +} diff --git a/sysroot/usr/include/stdlib.h b/sysroot/usr/include/stdlib.h index 9160012..4a66d5f 100644 --- a/sysroot/usr/include/stdlib.h +++ b/sysroot/usr/include/stdlib.h @@ -2,11 +2,14 @@ #define STDLIB_H #include +#include + #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1 void* malloc(size_t size); void* realloc(void *mem, size_t new_sz); void free(void* mem); +void exit(uint8_t code); #endif