Switch 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
Reverted
Posts: 3
Joined: Sat Feb 28, 2015 7:25 am

Switch to ring 3

Post by Reverted »

Hello, I've (at last) successfully created basic of os - software interrupts in ring 3. Now I am aiming to crate hardware handling for for ex. PIT interrupt. I get back to user mode switch code and pushed corrected eflags on to stack, so the iret will pop ant set the register. After doing this my code fails.

Code: Select all

.global usermode_switch
.type usermode_switch, @function
usermode_switch:
    #Data segment setup
    mov $0x13, %ax
    mov %ax, %ds
    mov %ax, %gs
    mov %ax, %fs
    mov %ax, %es
    #Stack save
    mov %esp, %eax
    #stack setup for iret and user space return
    pushl $0x13
    pushl %eax
    pushf
    #enable ints after switch to ring 3
    pop %eax
    or $0x200, %eax
    pushl %eax
    #CS selector
    pushl $0x0b
    pushl $end_switch
    iret
.extern upperKernelCode
end_switch:
    call upperKernelCode
bochs log says only :

Code: Select all

interrupt(): gate descriptor is not valid sys seg (vector=0x08)
and the two more faults.
Edit: I should mention that

Code: Select all

call upperKernelCode
generates GPF
Last edited by Reverted on Tue Jun 02, 2015 12:22 am, edited 2 times in total.
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Switch to ring 3

Post by SpyderTL »

I dont think 13 (or even 0x13, for that matter) are valid segment numbers. They start at 0x08, and go up by 0x08, and you need at least two entries in the GDT.

So, you should probably be using segments 0x08 and 0x10.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Switch to ring 3

Post by gerryg400 »

SpyderTL wrote:I dont think 13 (or even 0x13, for that matter) are valid segment numbers. They start at 0x08, and go up by 0x08, and you need at least two entries in the GDT.

So, you should probably be using segments 0x08 and 0x10.
The lowest 2 bits are the RPL so 0x13 could be a valid selector.
If a trainstation is where trains stop, what is a workstation ?
GreaseMonkey
Posts: 5
Joined: Tue May 26, 2015 11:53 pm
Libera.chat IRC: GreaseMonkey

Re: Switch to ring 3

Post by GreaseMonkey »

First things first, have you set up a TSS? You need SS0 and ESP0 at the very least, and I believe there's a few other things.

Another thing is that when transitioning to ring 3, IRET not only pops EIP, CS, and EFLAGS, but it also pops the user-mode ESP that you will be using.

I would highly recommend using a separate stack, even if you need to set one up purely for returning to ring 0.
Post Reply