Page 1 of 1

How to execute the first "userland" code ?

Posted: Sat Aug 18, 2012 3:00 am
by online
Hello,

I have a code (my little kernel written in 80386 assembly) running in ring 0 and I'd like to transfer the execution to some other code in the less privileged ring (ring 3) to start executing the userspace code.
What is the best way to do it ? Is the using of task switching (using TSS) needed to do this ?
I'd appreciate also a link to a good documentation on this so I can study the theory myself.

Thanks a lot !
online

Re: How to execute the first "userland" code ?

Posted: Sat Aug 18, 2012 3:30 am
by jnc100
The simplest way to get to ring 3 is to use the iret instruction. You set up the stack to how it would look if an interrupt had been triggered from a lower privilege level (see Intel 3A:6.12.1 - Exception and Interrupt Handling) and then issue iret. Prior to this you need to set up user (dpl = 3) code and data segments. A tss is required for interrupt handling in user mode - it is not required to get there in the first place. Essentially you need the ss0 and esp0 entries set so that if an interrupt is triggered where the interrupt code executed in ring 0 then the system has a valid stack to use. See Getting to Ring 3 for more details.

Regards,
John.

Re: How to execute the first "userland" code ?

Posted: Sat Aug 18, 2012 4:24 am
by online
Thanks, that's exactly what I was looking for :-) !