Page 1 of 1

Confused with task switching. What am I thinking wrong ?

Posted: Wed Jul 08, 2009 6:54 am
by gerenjo
Hello,
I have great trouble understanding something about the software task switching. I read a lot of tutorials and all the threads in here, but nothing could enlighten me.
This is how I currently understand the thing.

In each process page directory, I have code, data, and stack areas. Also, I have a page allocated for the kernel stack which address is contained in the TSS I set up. This address is the same in all the page directories of all the processes; in other words, for every process, a kernel stack is located at virtual address 0xwhatever.

Let's say I'm in ring0. Here comes the clock interruption, in which I'm supposed to switch task. Since I just got an interrupt, my stack has been changed to the one in the TSS right ? I go through my linked list of tasks, and finds the next one to execute.
Now I got a real problem: I have to push some registers and IRET on the task. But before iret'ing, I have to switch the page directory to the task's one ! But if I change the page directory, I won't be able to address the values I pushed before, since the virtual address of esp now points to another physical one.

I feel that I got something wrong, but I can't see what. :(

Thanks a lot

Re: Confused with task switching. What am I thinking wrong ?

Posted: Wed Jul 08, 2009 10:19 am
by yemista
I think how it works is that in every processes directory, the kernel tables should be linked in. When an interrupt happens you dont have to change the processes directory, you change the privledge level, and you just execute kernel code, which is mapped into the processes directory. You have to switch to the kernels stack to execute kernel code yes, but the kernel code should be linked into the directory, so basically all your doing is changing the value of esp.

Re: Confused with task switching. What am I thinking wrong ?

Posted: Wed Jul 08, 2009 10:21 am
by Matthew
Change your address space before fixing up the values on the stack for IRET.

Re: Confused with task switching. What am I thinking wrong ?

Posted: Wed Jul 08, 2009 10:28 am
by octavio
The stack only changes if there is a change in privilege level, and the new page directory is loaded using the tss after the iret instruction.

Re: Confused with task switching. What am I thinking wrong ?

Posted: Wed Jul 08, 2009 1:42 pm
by gerenjo
Thanks for your replies.
I managed to turn around that problem, but now I get a general protection at address 0x0 right after my iret.
At the same time, Bochs tells me this: "Iret: return CS selector null". What does it exactly mean ? When I print my cs register in the GPF handler, it is 0x8 (just as it is supposed to be), and when I shut down Bochs, the register dump says 0x08 as well.

I made some research in this forum about this error but couldn't find something that could help me :/

Edit: I fixed something and I have a really weird problem now. After my first task switching, I keep getting an interruption number 50 ( Oo). I really debugged the whole thing and there is no doubt : Anyone heard of this before ?