Page 1 of 1

Switch to ring 3

Posted: Mon Jun 01, 2015 5:12 pm
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

Re: Switch to ring 3

Posted: Mon Jun 01, 2015 7:21 pm
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.

Re: Switch to ring 3

Posted: Mon Jun 01, 2015 9:01 pm
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.

Re: Switch to ring 3

Posted: Tue Jun 02, 2015 2:20 am
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.