Preemptive Multitasking?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Preemptive Multitasking?

Post 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?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Preemptive Multitasking?

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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?
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Post 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.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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.
Post Reply