Getting to Ring 3

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
serge2k
Posts: 13
Joined: Mon Jun 08, 2009 1:00 am

Getting to Ring 3

Post by serge2k »

I've reached the point in my OS where I want to have a user mode. I've been reading the wiki, and looking throiugh the intel manuals but I can't seem to find out how to actually switch to ring 3.

http://wiki.osdev.org/Getting_to_Ring_3
that tells me to setup the GDT entries, which I have done. It says I will need a TSS as well for the ESP0 entry.

I've found sections on SYSENTER/SYSEXIT in the intel manuals.

I still don't know how the switch to use mode is actually done.

Can someone briefly explain how you actually switch back and forth?
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: Getting to Ring 3

Post by Hangin10 »

You could use SYSEXIT. You can also setup the stack like a CPL3 -> 0 changing interrupt has occurred and IRET. Basically push user SS, ESP, EFLAGS, CS, EIP, then execute an IRET, then you'll be in user mode. Don't forget to set the RPL in the selectors and that you're actually jumping to user mode pages (if you're using paging).

The TSS is needed because it provides the kernel mode SS and ESP for when an interrupt occurs. SYSENTER/SYSEXIT has MSRs for these values and don't use the TSS.
serge2k
Posts: 13
Joined: Mon Jun 08, 2009 1:00 am

Re: Getting to Ring 3

Post by serge2k »

Thats very clear, thank you very much.
serge2k
Posts: 13
Joined: Mon Jun 08, 2009 1:00 am

Re: Getting to Ring 3

Post by serge2k »

okay, I mapped a page for the user at 1MB.

My stack looks like this

0x00100000 (EIP)
0x00000018 (user code entry in GDT)
0x00000286 (eflags)
0xBFFFFFFF (user ESP)
0x00000020 (user data entry in GDT)

as soon as I iret I get a general protection fault.

anything I should be looking for?
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Getting to Ring 3

Post by thepowersgang »

The selector values need to be OR'ed with 3 to be valid in user mode

CS/SS/... are made up of three fields. There's the selector from bit 3 onwards, bit 2 selects the LDT or the GDT (1 means use the LDT) and bits 1 & 0 are the current privilege level.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Getting to Ring 3

Post by gerryg400 »

What error code are you getting with the GPF ? That will help track down the problem.
If a trainstation is where trains stop, what is a workstation ?
eXeCuTeR
Member
Member
Posts: 63
Joined: Tue Dec 09, 2008 12:43 pm

Re: Getting to Ring 3

Post by eXeCuTeR »

OR the segment selector with 0x3 in order to make RPL = 3 and make them user accessible. (this is probably what have been causing the GPF)
Also make sure that the user stack is also allocated, mapped and user accessible (in order to prevent page faults)
serge2k
Posts: 13
Joined: Mon Jun 08, 2009 1:00 am

Re: Getting to Ring 3

Post by serge2k »

I'm jumping to the correct location now (in user mode I guess?) but the problem I'm having is that even with interrupts off I get an immediate restart.

Any idea why?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Getting to Ring 3

Post by Combuster »

Ask Bochs' logs?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
serge2k
Posts: 13
Joined: Mon Jun 08, 2009 1:00 am

Re: Getting to Ring 3

Post by serge2k »

okay, still getting triple faults but I figured out what one issue was.

when creating my GDT entries I was doing this

base >> 16 & 0xF

instead of

base >> 16 & 0xFF


error wasn't apparent before because my base was always 0.

edit: Well I feel a bit silly.

I forgot that I had to set the user bit in both the table and the directory. I had only set the table.

Thanks for helping everybody, it is working now.
Post Reply