Page 1 of 1
Getting to Ring 3
Posted: Sat Aug 07, 2010 9:47 pm
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?
Re: Getting to Ring 3
Posted: Sat Aug 07, 2010 10:35 pm
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.
Re: Getting to Ring 3
Posted: Sun Aug 08, 2010 1:23 am
by serge2k
Thats very clear, thank you very much.
Re: Getting to Ring 3
Posted: Sun Aug 08, 2010 8:27 pm
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?
Re: Getting to Ring 3
Posted: Sun Aug 08, 2010 8:54 pm
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.
Re: Getting to Ring 3
Posted: Sun Aug 08, 2010 10:40 pm
by gerryg400
What error code are you getting with the GPF ? That will help track down the problem.
Re: Getting to Ring 3
Posted: Wed Aug 18, 2010 4:50 am
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)
Re: Getting to Ring 3
Posted: Wed Aug 18, 2010 5:15 am
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?
Re: Getting to Ring 3
Posted: Wed Aug 18, 2010 10:20 am
by Combuster
Ask Bochs' logs?
Re: Getting to Ring 3
Posted: Wed Aug 18, 2010 8:25 pm
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.