I've decided to test my multitasking subsystem with two methods linked into my kernel that just print different characters (a and b) on the screen. However, when I just jump to the methods, I'm still in CPL0, am I not? So, what can I do to set these threads up in CPL3? I suppose I have to set up their own stack, cr3 and segment registers, don't I? Is it that what makes the thread actually run in a different privilege level?
And then, once the code is actually running in CPL3, there won't be any printk function anymore, so do I have to implement system calls from now on?
Candamir
How to start a process in Ring 3
Re:How to start a process in Ring 3
Yes, if you have kernel code set as 'supervisor', lower privelage code shouldn't be able to run it by jumping to it.
When you have Ring 3 code you'll also _need_ at least one TSS, because when the task is interrupted by eg. the PIT or the Floppy, or whatever other interrupt/IRQ, it needs to know where the kernel's stack is, and have a place to store all the data about the current task. I recommend looking up TSS's to start with.
(As usualy, the disclaimer this is probably at least a little wrong )
When you have Ring 3 code you'll also _need_ at least one TSS, because when the task is interrupted by eg. the PIT or the Floppy, or whatever other interrupt/IRQ, it needs to know where the kernel's stack is, and have a place to store all the data about the current task. I recommend looking up TSS's to start with.
(As usualy, the disclaimer this is probably at least a little wrong )
Re:How to start a process in Ring 3
IIUC, I need a TSS in my GDT to be able to handle interrupts (Ring 0) when the CPU is executing userland programs (Ring 3). Is this right?
But anyway, you told me what the requirements are in order to perform switch 3 => 0, but not what must be done to do 0 => 3...
Thanks
Candamir
But anyway, you told me what the requirements are in order to perform switch 3 => 0, but not what must be done to do 0 => 3...
Thanks
Candamir
Re:How to start a process in Ring 3
I think as for going to ring 3, it's a simple as setting the lower 4 bits of the Code and Data segments to 3 (for ring-3), and having a GDT entry for them that's also ring-3. Eg, create a code 0x18 and a data 0x20. Then OR each one with 3, just set the task up to use these segments and you should be fine. Again, probably wrong, but I beleive that's it .
And yes, you're correct in that the TSS is so that interrupts can be handed while the CPU is executing Ring-3 coe.
And yes, you're correct in that the TSS is so that interrupts can be handed while the CPU is executing Ring-3 coe.
Re:How to start a process in Ring 3
Well you need at the minimum a task gate that points to a TSS descriptor that is set up with a DPL3 code and stack descriptors. SS0,SS1,SS2, in the TSS are for switching privilege levels during task execution.
Intel manuals explains this topic very well in my opinion and I would be just retyping what they have.
Btw. It sortof depends how you want to set up on how to handle interrupts in multi-tasking, you could have a task gate in replacement or not. I believe if a interrupt gate is used, the SS will be set from TSS.SS# or the corresponding DPL of the interrupt gate, but requires to restore the registers that was used apon return.
Intel manuals explains this topic very well in my opinion and I would be just retyping what they have.
Btw. It sortof depends how you want to set up on how to handle interrupts in multi-tasking, you could have a task gate in replacement or not. I believe if a interrupt gate is used, the SS will be set from TSS.SS# or the corresponding DPL of the interrupt gate, but requires to restore the registers that was used apon return.
Re:How to start a process in Ring 3
well as for 0 => 3 you dont jmp to it, what you do is put the eip of your code on the Kstack, later you switcher will iret into ring 3 code
(just making sure you know that)
(just making sure you know that)
Re:How to start a process in Ring 3
Well to jump to CPL3 you have to set the RPL of all the segment registers to CPL 3 (i.e) just 'OR' with 11b. Make an IRET. If you set RPL3 for code segment in the stack, then it will pop the stack pointer and stack segment ( which should also be at RPL3 ) also.
While returning it will need a TSS to find the kernel stack for that process, just SS0 and ESP0 is enough to jump from CPL3=>CPL0.
While returning it will need a TSS to find the kernel stack for that process, just SS0 and ESP0 is enough to jump from CPL3=>CPL0.
Code: Select all
*--stacksetup=USER_DATA_SEG | 3;
*--stacksetup=(unsigned int)0xBFFFFFFF;
*--stacksetup=0x0202;
*--stacksetup=USER_CODE_SEG | 3;
*--stacksetup=(unsigned int)start;
Re:How to start a process in Ring 3
just some thoughts...
remember to laod the task register with your tss descriptor before you try to switch. i think in c you'll need to use inline asm...
make sure that the interrupt flag bit is set in eflags is set or preemptive task switching won't work. (though longHorn's code does this)
remember to laod the task register with your tss descriptor before you try to switch. i think in c you'll need to use inline asm...
Code: Select all
mov ax, ;what ever your tss selector is in the gdt
ltr ax ;load the task register with it
Re:How to start a process in Ring 3
I noticed everyone does the iret method, its not the only way. I assumed he wanted a jump method which means a TSS is required, that is a far jump from ring 0 to ring 3. Those cases you need to jmp far to a TSS selector, or a task gate/system gates which points to a TSS (there are reasons for this). In both cases TSS must be setup properly with RPL3 segment registers which should be DPL3 descriptors. Ofcourse to get back you need to also should have setup TSS.SS0 and TSS.ESP0. The task register should not have same TSS selector at the time of the jump, as recursive is not allowed. From my experience, you may also switch from a TR=0 (default state of TR), which the previous register state will not be saved to any TSS.
The iret method is more used for a software switch, you probably require a stub to setup registers when entering CPL3. Using the TSS, all registers are loaded automatically.
Just thought I'd mention this, as depending on what your looking for, iret may not the best option.
The iret method is more used for a software switch, you probably require a stub to setup registers when entering CPL3. Using the TSS, all registers are loaded automatically.
Just thought I'd mention this, as depending on what your looking for, iret may not the best option.