I'm working on getting basic multitasking/threading going. It's working ok. I can jump around from task a, b, c, when I break() in each task. However, when I throw preemptive multitasking in, it doesn't work right. I've been working on this problem the past week and need some direction / help. Everything (so far) runs in kernel space. Some observations:
* When there are no breaks() other then the one initiated by the timer's isr, things will run fine for a few seconds and then tripple fault.
* When there are only breaks() in each tasks code, and none in the timer's isr, then things work as expected (however not as ultimately desired)
* When there are only breaks() in each tasks code, and a break() in my get_char() routine (while waiting for a keypress), a predefined and constant value of an integer, which is set programatically at runtime, changes.
* The stack won't stick - I can't seem to get each task's stack pointer to stay. When I set ESP to say 5000 for task 1, ESP will later appear around 1000, where 1000 being where my kernel's stack resides. HOWEVER, esp does reflect the correct set value when only the timer has a break() in it.
As you can no doubt tell, I'm quite perplexed and any help is appreciated. If you need to see more code, I can post.
Thanks much!
Here's my keyboard code:
Here's my ISR code (implemented as a macro w/nasm):
Code: Select all
push 0 ; Error No
push i ; Interrupt / Exception No
pusha
push gs
push fs
push es
push ds
mov eax, 0x10 ; Set to CPL 0 selectors
mov ds, eax
mov es, eax
cld
mov eax, esp ; EAX = Stack pointer, ie: register structure
push eax ; Push pointer to register structure
call interrupt_dispatcher
pop eax
pop ds
pop es
pop fs
pop gs
popa
add esp, 8 ; Do away with error no and interrupt no
iretdHere's my task switching code:
Code: Select all
t[cur_task].r.gs = r->gs; // Copy over the registers into a saved state
t[cur_task].r.fs = r->fs;
t[cur_task].r.es = r->es;
t[cur_task].r.ds = r->ds;
t[cur_task].r.eax = r->eax;
t[cur_task].r.ecx = r->ecx;
t[cur_task].r.edx = r->edx;
t[cur_task].r.ebx = r->ebx;
t[cur_task].r.esp = r->esp;
t[cur_task].r.ebp = r->ebp;
t[cur_task].r.esi = r->esi;
t[cur_task].r.edi = r->edi;
t[cur_task].r.int_no = r->int_no;
t[cur_task].r.error_code = r->error_code;
t[cur_task].r.eip = r->eip;
t[cur_task].r.cs = r->cs;
t[cur_task].r.eflags = r->eflags;
t[cur_task].r.user_esp = r->user_esp;
t[cur_task].r.user_ss = r->user_ss;
if( cur_task == no_tasks - 1) { // Iterate through the tasks based on no_tasks
cur_task = 0;
} else {
cur_task++;
}
sched_force_switch = 0; // Reset PIT force switch
r->gs = t[cur_task].r.gs; // Copy over the new task no to the current register set that
r->fs = t[cur_task].r.fs; // will be poped off the stack upon compleation.
r->es = t[cur_task].r.es;
r->ds = t[cur_task].r.ds;
r->eax = t[cur_task].r.eax;
r->ecx = t[cur_task].r.ecx;
r->edx = t[cur_task].r.edx;
r->ebx = t[cur_task].r.ebx;
r->esp = t[cur_task].r.esp;
r->ebp = t[cur_task].r.ebp;
r->esi = t[cur_task].r.esi;
r->edi = t[cur_task].r.edi;
r->int_no = t[cur_task].r.int_no;
r->error_code = t[cur_task].r.error_code;
r->eip = t[cur_task].r.eip;
r->cs = t[cur_task].r.cs;
r->eflags = t[cur_task].r.eflags;
r->user_esp = t[cur_task].r.user_esp;
r->user_ss = t[cur_task].r.user_ss;
Code: Select all
void timer_irh( registers *r ) {
if( sched_force_switch == 10 ) {
sched_force_switch = 0;
r->error_code = D_syscall_break;
sys_call(r);
} else {
sched_force_switch++;
}
outportb( 0x20, 0x20 );
}Code: Select all
void keyboard_irh( void ) {
unsigned int scan_code = 0;
int i;
static int pb_handler = 0;
scan_code = inportb( 0x60 );
scan_code = scan_code & 0xFF;
// Grab the pause/break key here...
if( pb_handler > 0 ) {
pb_handler++;
if( pb_handler == 6 ) {
pb_handler = 0;
// Call debugger handler
printf( "\nDebugger would be called here.\n" );
}
} else if ( scan_code == 0xE1 ) {
pb_handler = 1;
} else {
enque_scancode( &key_que, scan_code );
}
i = inportb( 0x61 );
outportb( 0x61, i | 0x80 );
outportb( 0x61, i & 0x7F );
outportb( 0x20, 0x20 );
}* Interrupts are disabled starting at the beginning of my master ISR (interrupt_dispatcher), enabled after the given isr has returned
* Memory model, gdt, etc... are flat. Works as expected.


