From 0984eb593f3d1a4c7dfdc6861fd2b7c42b766a55 Mon Sep 17 00:00:00 2001 From: Jacek Galowicz Date: Sat, 14 Apr 2012 09:57:18 +0200 Subject: [PATCH] Commented everything --- apps/tests.c | 4 ++-- arch/x86/kernel/entry.asm | 28 +++++++++++++++++++--------- arch/x86/kernel/gdt.c | 25 +++++++++++++++++++++++-- kernel/tasks.c | 3 +-- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/apps/tests.c b/apps/tests.c index 7f27eac3..26c1aba8 100644 --- a/apps/tests.c +++ b/apps/tests.c @@ -333,8 +333,8 @@ int test_init(void) mailbox_int32_init(&mbox); create_kernel_task(NULL, measure_ctx_switch, NULL, NORMAL_PRIO); - create_kernel_task(NULL, foo, "Hello from foo1", NORMAL_PRIO); - create_kernel_task(NULL, foo, "Hello from foo2", NORMAL_PRIO); + //create_kernel_task(NULL, foo, "Hello from foo1", NORMAL_PRIO); + //create_kernel_task(NULL, foo, "Hello from foo2", NORMAL_PRIO); //create_kernel_task(NULL, join_test, NULL, NORMAL_PRIO); //create_kernel_task(NULL, producer, , NORMAL_PRIO); //create_kernel_task(NULL, consumer, NULL, NORMAL_PRIO); diff --git a/arch/x86/kernel/entry.asm b/arch/x86/kernel/entry.asm index 637d5cdc..b1082278 100644 --- a/arch/x86/kernel/entry.asm +++ b/arch/x86/kernel/entry.asm @@ -507,22 +507,32 @@ hack: jmp 0x00 : 0xDEADBEAF ret +; This procedure is used by scheduler() to switch tasks. +; It is the software-equivalent to the hw-procedure switch_task from above. +; Call it in C with the following arguments: +; sw_switch_context(&old_tasks_stack_pointer, &new_tasks_stack_pointer) global sw_switch_context sw_switch_context: - ;pushf - push DWORD 0x8 - push DWORD [esp+4] - push DWORD 0 - push DWORD 0xc0edbabe - pusha + ; The stack layout looks like this: + ; [new stack pointer] + ; [old stack pointer] + ;pushf ; [this procedure's return address] overwritten by: EFLAGS (*1) + push DWORD 0x8 ; CS + push DWORD [esp+4] ; EIP + push DWORD 0 ; Interrupt number + push DWORD 0xc0edbabe ; Error code + pusha ; Registers... + ; ---- This will be popped off by iret later. + pushf pop eax - mov [esp+48], eax + mov [esp+48], eax ; Move EFLAGS to position (*1) by overwriting + ; the return address of sw_switch_context() mov ecx, [esp+52] - mov [ecx], esp + mov [ecx], esp ; Save stack position in old task structure mov ecx, [esp+56] - mov esp, [ecx] + mov esp, [ecx] ; Load new stack sw_rollback: popa diff --git a/arch/x86/kernel/gdt.c b/arch/x86/kernel/gdt.c index 5378515d..1ec3c24f 100644 --- a/arch/x86/kernel/gdt.c +++ b/arch/x86/kernel/gdt.c @@ -149,10 +149,29 @@ int create_default_frame(task_t* task, internal_entry_point_t ep, void* arg) #ifdef SW_TASK_SWITCH memset(kstacks[id], 0xCD, KERNEL_STACK_SIZE); + /* The difference between setting up a task for SW-task-switching + * and not for HW-task-switching is setting up a stack and not a TSS. + * This is the stack which will be activated and popped off for iret later. + */ stack = kstacks[id] +KERNEL_STACK_SIZE -sizeof(uint32_t); + + /* The next three things on the stack are a marker for debugging purposes, ... */ *stack-- = 0xDEADBEEF; + /* the first-function-to-be-called's arguments, ... */ *stack-- = arg; + /* and the "caller" we shall return to. + * This procedure cleans the task after exit. */ *stack = leave_kernel_task; + + /* Next bunch on the stack is the initial register state. + * The stack must look like the stack of a task which was + * scheduled away previously. */ + + /* short_state_size was introduced because the convenient "struct state" + * is used for filling the stack with initial values. But the problem is that + * "iret" will not remove the last two entries from the stack, since we're + * "returning" from kernel space to kernel space. Therefore it is shortened + * by its last two entries. */ stack -= short_state_size; stptr = stack; @@ -161,12 +180,14 @@ int create_default_frame(task_t* task, internal_entry_point_t ep, void* arg) stptr->int_no = 0xB16B00B5; stptr->error = 0xC03DB4B3; + /* The instruction pointer shall be set on the first function to be called + * after IRETing */ stptr->eip = ep; stptr->cs = cs; stptr->eflags = 0x1002; - //stptr->ss = ds; - //stptr->useresp = kstacks[id] +KERNEL_STACK_SIZE - 3*sizeof(uint32_t); + /* Set the task's stack pointer entry to the stack we have crafted right now. + * This is the pointer which will be used by sw_switch_task(old_task, new_task) later.*/ task->stack = stack; #else /* reset buffers */ diff --git a/kernel/tasks.c b/kernel/tasks.c index 34bc7cb6..c7c560df 100644 --- a/kernel/tasks.c +++ b/kernel/tasks.c @@ -1382,11 +1382,10 @@ get_task_out: // orig_task->id, curr_task->id, (uint32_t)curr_task->prio, CORE_ID); #ifndef SW_TASK_SWITCH switch_task(curr_task->id); -#else - write_cr3(virt_to_phys((size_t)curr_task->pgd)); #endif finish_task_switch(0); #ifdef SW_TASK_SWITCH + write_cr3(virt_to_phys((size_t)curr_task->pgd)); sw_switch_context(&orig_task->stack, &curr_task->stack); #endif }