OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 2:09 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: The Switch to (x86-64) user mode
PostPosted: Fri Oct 30, 2020 1:18 am 
Offline

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


Top
 Profile  
 
 Post subject: Re: The Switch to (x86-64) user mode
PostPosted: Fri Oct 30, 2020 6:21 am 
Offline

Joined: Mon Oct 26, 2020 1:12 am
Posts: 2
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 97 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group