Page 2 of 2

Re:Scheduling & Task Switching

Posted: Tue Nov 29, 2005 11:42 am
by distantvoices
that's easy:

if you want a task to operate in cpl3

1. assign a user stack for cpl3 and a stack for cpl0 (which is for syscalls)

2. put 0x3202 into eflags. This tells the cpu: he, you, I'm a user task.
ou have to assign ss3,esp3 on the initial stack frame for the task too.

3. upon switch to that task, you load esp0 and ss0 of the system tss with the kernel stack you've assigned for the task. This is for following: upon int 0xyy, the transition from ring3 to ring0 is performed. the kernel stack for the task is loaded into esp (cpu finds it in the system tss->esp0) as well as ss register is loaded with ss0 from system tss. Then, crucial registers are pushed: ss3,esp3,eflags,eip,cs3. Not exactly in this order, have to look up, but - you get the point?

Does this help? Do you still have some specific questions?

Re:Scheduling & Task Switching

Posted: Tue Nov 29, 2005 12:04 pm
by Humble
i've tried this before, but bochs keeps telling me some words like RPL & CPL should be <= DPL and so on
but the good thing that the cpl3 switch works, as the 1st cpl3 taks works but when an interrupt occurs (cpl3 -> cpl0) the kernel stops with these errors.
i think this is something with the TSS as the SS0 & ESP0 may not be set correctly.
what about the TR should DPL be 11b or 00b ?

Re:Scheduling & Task Switching

Posted: Tue Nov 29, 2005 2:40 pm
by distantvoices
of course you 'll need user code segment and user data segment too. (cs,ds for cpl3)

Re:Scheduling & Task Switching

Posted: Tue Nov 29, 2005 3:47 pm
by Cjmovie
Come to think of it, maybe I should have updated the code in my tutorial as I found mistakes in my own code ;D.

Anyways, this is why I have yet to put it anywhere else, I was waiting for something like this to happen :P. I'm off to fix it......

Re:Scheduling & Task Switching

Posted: Wed Nov 30, 2005 6:17 am
by Pype.Clicker
beyond infinity wrote: 2. put 0x3202 into eflags. This tells the cpu: he, you, I'm a user task.
ou have to assign ss3,esp3 on the initial stack frame for the task too.
Hmm ... not quite 0x3202 into eflags means "IOPL=3, Interrupts enabled". That just means once the task will be running, it will be allowed to do fancy stuff with I/O ports (which is mandatory for BI's VM86 mode implementation)

But that doesn't tell the CPU the program should be user-mode. For that, you need to "return" to a DPL3 code segment. As soon as (e.g. when doing an IRET) the processor detects a DPL3 code segment on the stack, it knows it should also retrieve SS3 and ESP3 on the current stack.

Re:Scheduling & Task Switching

Posted: Wed Nov 30, 2005 8:54 am
by distantvoices
silly me. Of course, that's why cs3 & eip are stored on the stack upon interruption. Should be more careful ere posting crucial stuff, honestly.

*kickshimselfinthearse*

Re:Scheduling & Task Switching

Posted: Sat Dec 03, 2005 7:49 am
by tiger
Correct code for pusha/popa should be like this

Code: Select all

         *--esp=EFLAGS;
         *--esp=CS;
         *--esp=EIP;
         *--esp=EAX;
         *--esp=ECX;
         *--esp=EDX;
         *--esp=EBX;
         //////////////////////////////////////////////
         // Segment/Selection
         // Registers pushed below
         //

Re:Scheduling & Task Switching

Posted: Sun Dec 04, 2005 10:47 am
by beginner
Pype.Clicker wrote:
beyond infinity wrote: 2. put 0x3202 into eflags. This tells the cpu: he, you, I'm a user task.
ou have to assign ss3,esp3 on the initial stack frame for the task too.
Hmm ... not quite 0x3202 into eflags means "IOPL=3, Interrupts enabled". That just means once the task will be running, it will be allowed to do fancy stuff with I/O ports (which is mandatory for BI's VM86 mode implementation)

But that doesn't tell the CPU the program should be user-mode. For that, you need to "return" to a DPL3 code segment. As soon as (e.g. when doing an IRET) the processor detects a DPL3 code segment on the stack, it knows it should also retrieve SS3 and ESP3 on the current stack.
What is ESP3 AND SS3 and how I can set them ?

And a silly question how to return to DPL3 code segment ?

Re:Scheduling & Task Switching

Posted: Sun Dec 04, 2005 2:59 pm
by Pype.Clicker
okay, the big thing with IA-32 and user level is that there's no mechanism to 'call' user level code from kernel level code. You can only call kernel level code from user level. period.

However, when in kernel mode (e.g. creating a new thread), nothing prevent you to arrange the stack content so that it looks like if you were just being called from user level. The stack should contain the EIP and CS values of the user level code, the stack pointer from user world and a few other things. Just put them on stack and issue an IRET instruction and the processor will have no way to detect you've never been called by that user-mode code.

(SS3 and ESP3 are values of SS and ESP to be used at user-level. Unlike SS0 and ESP0, there's no need for them stored in a TSS but as soon as a _stack switch_ due to a priviledge level change occurs, they're pushed on the callee stack for the return process.