Page 1 of 1

Preemptive Multitasking?

Posted: Thu Feb 08, 2007 12:20 am
by pcmattman
My OS currently is using a co-operative multitasking system. However, I don't like it very much, it doesn't give me much control. However, when I try out preemptive multitasking, based on the timer interrupt, it doesn't work.

My timer ISR basically pops the IP off the stack into a register, then jumps to my function ('resched'). Resched then saves the current state (including the popped IP) then jumps to the next task's IP.

This works when I am using cooperative multitasking, but preemptive multitasking doesn't work at all :( . Any ideas? (The OS is written purely in NASM).

Note: I run this in Bochs, the scheduler prints out the value of SI (CS) and DI (IP) as well as the first task's IP (p1) and the second's (p2). Snapshot:

Code: Select all

si:80=di:1486=p1:1486=p2:1550
-ZAAAAAAAAAAAAA
si:61440=di:65247=p1:58347=p2:35080
+AAAAAAAAAAAAAAAAAAAAAAAAAA
The - and + are scheduler printed, the Z's and A's are the tasks.

You can see that the IP's of the two tasks have been really wrecked by something.

Any ideas?

Re: Preemptive Multitasking?

Posted: Thu Feb 08, 2007 1:17 am
by Brendan
Hi,
pcmattman wrote:My timer ISR basically pops the IP off the stack into a register, then jumps to my function ('resched'). Resched then saves the current state (including the popped IP) then jumps to the next task's IP.

This works when I am using cooperative multitasking, but preemptive multitasking doesn't work at all :( . Any ideas? (The OS is written purely in NASM).
Do you also switch stacks, and does the timer IRQ call "resched" before or after sending the EOI to the PIC chips?


Cheers,

Brendan

Posted: Thu Feb 08, 2007 1:21 am
by pcmattman
No, all tasks share the same stack. The timer calls resched, resched sends the 'EOI' to the chip. Should I have sent it before?

Update: EOI sent before resched, still not working.

Update 2: Stack switch happens, still now working. Now established that the problem lies in the ISR. How can I get the IP of the code that was being executed before the ISR fired?

Posted: Thu Feb 08, 2007 7:03 pm
by iammisc
the cpu pushes the cs:ip of the code that was executing when the interrupt occured to the stack. Read the Intel Manuals and Bran's Kernel Development Tutorial on osdever.net. Before working on multitasking maybe you should work on your exceptiong handler to at least print out the state of the cpu at interrupt time.

Posted: Sat Feb 10, 2007 3:15 am
by pcmattman
The only problem is in the timer ISR. The code segment I get from it is wrong, so I assume that the code being interrupted is not a task. Either way, I'm now moving to a C kernel and have found a good way to make co-operative multitasking work.

Thanks anyway.