How to execute the first "userland" code ?

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
User avatar
online
Posts: 16
Joined: Fri Jan 20, 2012 8:26 am
Location: Obrnice, Czech Republic
Contact:

How to execute the first "userland" code ?

Post 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
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

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

Post 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.
User avatar
online
Posts: 16
Joined: Fri Jan 20, 2012 8:26 am
Location: Obrnice, Czech Republic
Contact:

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

Post by online »

Thanks, that's exactly what I was looking for :-) !
Post Reply