Scheduling & Task Switching
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Scheduling & Task Switching
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?
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?
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
Re:Scheduling & Task Switching
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 ?
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 ?
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Scheduling & Task Switching
of course you 'll need user code segment and user data segment too. (cs,ds for cpl3)
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
Re:Scheduling & Task Switching
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 . I'm off to fix it......
Anyways, this is why I have yet to put it anywhere else, I was waiting for something like this to happen . I'm off to fix it......
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Scheduling & Task Switching
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)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.
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.
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Scheduling & Task Switching
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*
*kickshimselfinthearse*
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
Re:Scheduling & Task Switching
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
What is ESP3 AND SS3 and how I can set them ?Pype.Clicker wrote: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)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.
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.
And a silly question how to return to DPL3 code segment ?
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Scheduling & Task Switching
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.
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.