Getting to Ring 3
Getting to Ring 3
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?
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
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.
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
Thats very clear, thank you very much.
Re: Getting to Ring 3
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?
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?
- thepowersgang
- 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
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.
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
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Re: Getting to Ring 3
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 ?
Re: Getting to Ring 3
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)
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
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?
Any idea why?
- Combuster
- 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
Ask Bochs' logs?
Re: Getting to Ring 3
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.
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.