From 6c926175afbf1f9ec2715fb007acc28588b36c4a Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 21 Aug 2018 17:54:28 +0200 Subject: cleaning up multitasking --- asm/int_mouse_handler.asm | 5 +-- asm/int_syscall_handler.asm | 4 +-- asm/task.s | 75 +++++++++++++++++++++++++++++++++++++++++++++ driver/keyboard.c | 1 - kernel/interrupts.c | 1 + kernel/kernel.c | 8 ++--- kernel/task.c | 67 ++++++++++++++++++++++++++++++++++++---- kernel/usermode.c | 15 ++++++--- lib/logger/log.c | 2 -- 9 files changed, 156 insertions(+), 22 deletions(-) create mode 100644 asm/task.s diff --git a/asm/int_mouse_handler.asm b/asm/int_mouse_handler.asm index 6872af0..9816950 100644 --- a/asm/int_mouse_handler.asm +++ b/asm/int_mouse_handler.asm @@ -7,9 +7,10 @@ int_mouse_handler: call mouse_handler - mov al, 0x20 ;Port number AND command number to Acknowledge IRQ + mov al, 0x20 ; Port number AND command number to Acknowledge IRQ out 0xa0, al ; came from slave - out 0x20, al ;Acknowledge IRQ, so we keep getting interrupts + out 0x20, al ; Acknowledge IRQ, so we keep getting interrupts + popa iret ;Interrupt-Return diff --git a/asm/int_syscall_handler.asm b/asm/int_syscall_handler.asm index 952faae..a26592f 100644 --- a/asm/int_syscall_handler.asm +++ b/asm/int_syscall_handler.asm @@ -221,8 +221,8 @@ call_unhandled: ;; TODO: redesign this shit! call_read: - mov al, 0x20 ;Port number AND command number to Acknowledge IRQ - out 0x20, al ;Acknowledge IRQ, so we keep getting interrupts + ;//mov al, 0x20 ;Port number AND command number to Acknowledge IRQ + ;//out 0x20, al ;Acknowledge IRQ, so we keep getting interrupts sti call syscall_read diff --git a/asm/task.s b/asm/task.s new file mode 100644 index 0000000..7611d4a --- /dev/null +++ b/asm/task.s @@ -0,0 +1,75 @@ +.global task_pusha +task_pusha: + + pushf + + push $0x8 + push $userfunc + + pusha + /* + now stack looks like: + + param // esp+48 + returnaddy + + eflags + code segment + userfunc + + eax // rest by popa + ecx + edx + ebx + esp + ebp + esi + edi + + */ + + mov 48(%esp),%eax // get address of alternative stack where we want to simulate the pusha + + mov (%esp),%ecx + mov %ecx,(%eax) + + mov 4(%esp),%ecx + mov %ecx,4(%eax) + + mov 8(%esp),%ecx + mov %ecx,8(%eax) + + mov 12(%esp),%ecx + mov %ecx,12(%eax) + + mov 16(%esp),%ecx + mov %ecx,16(%eax) + + mov 20(%esp),%ecx + mov %ecx,20(%eax) + + mov 24(%esp),%ecx + mov %ecx,24(%eax) + + mov 28(%esp),%ecx + mov %ecx,28(%eax) + + mov 32(%esp),%ecx + mov %ecx,32(%eax) + + mov 36(%esp),%ecx + mov %ecx,36(%eax) + + mov 40(%esp),%ecx + mov %ecx,40(%eax) + + mov 44(%esp),%ecx + mov %ecx,44(%eax) + + pop %eax + pop %eax + pop %eax + + popa + + ret diff --git a/driver/keyboard.c b/driver/keyboard.c index d35ef6d..52e6454 100644 --- a/driver/keyboard.c +++ b/driver/keyboard.c @@ -21,7 +21,6 @@ static void put(uint8_t c) void keyboard_init(uint32_t s) { kb_stream=s; - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Keyboard Initialized"); } void keyboard_handle(uint8_t in) diff --git a/kernel/interrupts.c b/kernel/interrupts.c index 96446f3..9b08475 100644 --- a/kernel/interrupts.c +++ b/kernel/interrupts.c @@ -51,6 +51,7 @@ void exception_handle() void int_default() { log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"default handler"); + panic(FOOLOS_MODULE_NAME,"unhandled interrupt (is this a panic or should just iognore?)"); } void show_error(uint32_t err) diff --git a/kernel/kernel.c b/kernel/kernel.c index ea14869..e1076a9 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -35,6 +35,7 @@ void kernel_main(uint32_t eax,uint32_t ebx) // KEYBOARD DRIVER keyboard_init(0); //sstdin + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Keyboard Initialized"); // MOUSE DRIVER mouse_init(); @@ -80,13 +81,10 @@ void kernel_main(uint32_t eax,uint32_t ebx) uint32_t sstdout = syscall_open("term",0,0); // stdout 1 uint32_t sstderr = syscall_open("stderr",0,0); // stderr 2 - // START INTERRUPTS (code segment: 0x08) + // INSTALL INTERRUPTS (code segment: 0x08) int_init(0x08); - // INIT MULTITASKING (and switch to our pseudo-usermode) + // INIT MULTITASKING (and start scheduler) task_init(dir); - // we should never reach this - panic(FOOLOS_MODULE_NAME,"reached end of kernel.c !!"); - } diff --git a/kernel/task.c b/kernel/task.c index c281821..395c196 100644 --- a/kernel/task.c +++ b/kernel/task.c @@ -45,7 +45,6 @@ volatile int add_task(uint32_t esp, uint32_t vmem) task_list[i].waiting=false; task_list[i].skipwait=false; task_list[i].brk=task_list[current_task].brk; - return i; } } @@ -53,9 +52,29 @@ volatile int add_task(uint32_t esp, uint32_t vmem) panic(FOOLOS_MODULE_NAME,"out of task slots!"); } +// +// REMEMBER WE ARE INSIDE AN INTERRUPT HERE - DON'T WASTE TIME! +// +// oldesp - is the adress of the stack pointer when pit_interrupt_handler was entered. +// registers have been pushed with pusha to this old stack. +// +// stack pointer was moved to the 16kb stack we have from multiboot.s +// +// we need to return a NEW stack pointer where popa will get the registers the new task requires +// +static int first=1; volatile uint32_t my_scheduler(uint32_t oldesp) { - task_list[current_task].esp=oldesp; + + // allows skipping scheduling from gdb + volatile int skippy=0; + if(skippy)return oldesp; + + if(!first) + { + task_list[current_task].esp=oldesp; + } + first=0; for(int i=0;i=2) syscall_write(2,str,strlen(str)); - while(*str!=0) { serial_write(*str++); } - } void log(char *module_name, int log_level, char *format_string, ...) -- cgit v1.2.3