Page 1 of 1

The "Getting to Ring3" Wiki page - need precisions

Posted: Mon Jul 06, 2009 10:30 am
by gerenjo
Hi everyone,

I am currently trying to create ring 3 tasks. I did not understand a paragraph in the tutorial available on the wiki, which is:

Multitasking considerations

There are a lot of subtle things with user mode and task switching that you may not realize at first. First: Whenever a system call interrupt happens, the first thing that happens is the CPU changes to ESP0 stack. Then, it will push all the system information. So when you enter the interrupt handler, your working off of the ESP0 stack. This could become a problem with 2 ring 3 tasks going if all you do is merely push context info and change esp. Think about it. you will change the esp, which is the esp0 stack, to the other tasks esp, which is the same esp0 stack. So, what you must do is change the ESP0 stack (along with the interrupt pushed ESP stack) on each task switch, or you will end up overwriting yourself.
In the interruption code, I understand that the current stack is the one TSS.ESP0 points to. Since multitasking is implemented with the clock interruption, switching from task to task implies that I am in this situation. But now, what's the meaning of the bold sentence ? How come the "other task's esp" is the same esp0 stack ? Can someone enlighten me ?

Lionel

Re: The "Getting to Ring3" Wiki page - need precisions

Posted: Mon Jul 06, 2009 11:59 am
by frank
I think that it is saying that you need to have a separate ESP0 stack for each thread and that you should update the ESP0 in the TSS during each task switch. Imagine all of your tasks had the same ESP0 pointer when an interrupt occured, the CPU automatically pushes some information on the stack when the interrupt occurs. That information is needed to return to that task. If the TSS ESP0 wasn't changed then the next interrupt would overwrite the data on the ESP0 stack.