Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Star::write returning Err when using segments created by this crate #531

Open
shinobu-uwu opened this issue Jan 20, 2025 · 1 comment
Open

Comments

@shinobu-uwu
Copy link

Hello, I hope everyone is doing well!

I've been trying to enable the syscall/sysret instructions in my kernel, however when I try to use the segments I registered in my GDT, I get this error: Error setting STAR: Sysret CS and SS are not offset by 8. I find it confusing because I generated these segments like this:

pub static ref GDT: (GlobalDescriptorTable, Selectors) = {
    let mut gdt = GlobalDescriptorTable::new();
    let kernel_code_selector = gdt.append(Descriptor::kernel_code_segment());
    let kernel_data_selector = gdt.append(Descriptor::kernel_data_segment());
    let user_code_selector = gdt.append(Descriptor::user_code_segment());
    let user_data_selector = gdt.append(Descriptor::user_data_segment());
    let tss_selector = gdt.append(Descriptor::tss_segment(&TSS));
    (
        gdt,
        Selectors {
            kernel_code_selector,
            kernel_data_selector,
            user_code_selector,
            user_data_selector,
            tss_selector,
        },
    )
};

What is even more confusing is that cs_sysret and ss_sysret are in fact offset by 8. If anyone could point me in the right direction I would be thanked. Here is the code where I try to write to Star and its output:

let kernel_cs = GDT.1.kernel_code_selector;
let kernel_ss = GDT.1.kernel_data_selector;
let user_cs = GDT.1.user_code_selector;
let user_ss = GDT.1.user_data_selector;

debug!("User CS: {:#x?}", user_cs.0);
debug!("User SS: {:#x?}", user_ss.0);
debug!("Kernel CS: {:#x?}", kernel_cs.0);
debug!("Kernel SS: {:#x?}", kernel_ss.0);

match Star::write(user_cs, user_ss, kernel_cs, kernel_ss) {
    Ok(_) => {
        debug!("STAR MSR set successfully");
    }
    Err(e) => {
        panic!("Error setting STAR: {}", e)
    }
}
DEBUG [jon::syscall:28]: User CS: 0x1b
DEBUG [jon::syscall:29]: User SS: 0x23
DEBUG [jon::syscall:30]: Kernel CS: 0x8
DEBUG [jon::syscall:31]: Kernel SS: 0x10
ERROR [jon:61]: panicked at src/syscall/mod.rs:38:13:
Error setting STAR: Sysret CS and SS are not offset by 8.
@Freax13
Copy link
Member

Freax13 commented Jan 20, 2025

You need to swap the user data and code segments in the GDT. I'm sorry that the error message isn't very helpful here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants