Confused with task switching. What am I thinking wrong ?

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
gerenjo
Posts: 10
Joined: Tue Feb 03, 2009 12:16 pm

Confused with task switching. What am I thinking wrong ?

Post 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
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

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

Post 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.
Matthew
Member
Member
Posts: 48
Joined: Wed Jul 01, 2009 11:47 am

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

Post by Matthew »

Change your address space before fixing up the values on the stack for IRET.
octavio
Member
Member
Posts: 94
Joined: Wed Oct 25, 2006 5:12 am
Location: Barcelona España
Contact:

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

Post 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.
gerenjo
Posts: 10
Joined: Tue Feb 03, 2009 12:16 pm

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

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