The Switch to (x86-64) user mode

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
pitust
Posts: 2
Joined: Mon Oct 26, 2020 1:12 am
Libera.chat IRC: pitust

The Switch to (x86-64) user mode

Post by pitust »

I am writing an OS in rust. Right now i am trying to implement user mode, i already mapped in some pages and now i am trying to perform the actual switch.
My code that switches to user mode:

Code: Select all

    mov ax,0x23
    mov ds,ax
    mov es,ax 
    mov fs,ax 
    mov gs,ax

    mov rdi, user
    mov rsi, rsp
    mov dx, 0x1b
    mov ax, 0x23
    push rax
    push rsi
    push 0x200
    push rdx
    push rdi
    iret
user:
    mov rcx, target_read + 0x1000
    mov rax, [rcx]
    jmp $
target_read: 
    dd 0xdeadbeaf
    dd 0xdeadbeaf
My OS is for the x86-64 architecture, and my GDT code is:

Code: Select all

let mut gdt = GlobalDescriptorTable::new();
let code_selector = gdt.add_entry(Descriptor::kernel_code_segment()); // 0x08
let data_selector = gdt.add_entry(Descriptor::kernel_data_segment()); // 0x10
let usercode = gdt.add_entry(Descriptor::user_code_segment()); // 0x18
let userdata = gdt.add_entry(Descriptor::user_data_segment()); // 0x20
let tss_selector = gdt.add_entry(Descriptor::tss_segment(&TSS)); // 0x28
(gdt, Selectors { code_selector, tss_selector, data_selector, usercode, userdata })
I can confirm that usercode is indeed 0x1b and userdata is 0x23.
The exact instruvtion failing is the iret, with the old code and stack segments, and CPU flags set to 0x10006. It is failing with a General Protection Fault, error code 32768.
pitust
Posts: 2
Joined: Mon Oct 26, 2020 1:12 am
Libera.chat IRC: pitust

Re: The Switch to (x86-64) user mode

Post by pitust »

I solved the issue, which was that i should have been using `iretq` and not `iret`. (Thank you geist for your help)
Now my userland works!
Post Reply