Skip to content

Add CPUID instruction #372

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

Closed
adsnaider opened this issue Apr 8, 2022 · 6 comments · Fixed by #382
Closed

Add CPUID instruction #372

adsnaider opened this issue Apr 8, 2022 · 6 comments · Fixed by #382

Comments

@adsnaider
Copy link

Not sure why this isn't already part of the API, it seems like a very natural instruction to have. Am I missing something?

@josephlr
Copy link
Contributor

josephlr commented Apr 8, 2022

Nope, it's mostly just figuring out the correct abstraction around the instruction. CPUID does a lot of things, and returns a bunch of different types of data. We will likely need a combination of a "raw" and "non-raw" CPUID function.

@adsnaider
Copy link
Author

Tbh, I was just thinking of a low level function like this one

use core::arch::asm;

/// Runs the CPUID instruction with the provided `eax` register.
///
/// # Returns
///
/// A 4-tuple (eax, ebx, ecx, edx). It's up to the user to interpret these values based on the
/// input.
pub fn cpuid(mut eax: u32) -> (u32, u32, u32, u32) {
    let ebx: u32;
    let ecx: u32;
    let edx: u32;
    unsafe {
        asm!(
            "push rbx",
            "cpuid",
            "mov esi, ebx",
            "pop rbx",
             out("esi") ebx,
             inout("eax") eax,
             out("ecx") ecx,
             out("edx") edx
        );
    }
    (eax, ebx, ecx, edx)
}

Even this I think would be useful as it's non-trivial given that LLVM uses rbx.

@josephlr
Copy link
Contributor

josephlr commented Apr 8, 2022

So they're already are some functions related to CPUID in the standard library core::arch::x86_64 module:

I would want to make sure we're doing something more then just reimplementing them. That could just be providing a safe and/or stable API.

Regardless, we should reuse the CpuidResult type.

@Freax13
Copy link
Member

Freax13 commented Apr 8, 2022

Just wanted to point out that there's already a very good crate that implements abstractions over cpuid: raw-cpuid.

@josephlr
Copy link
Contributor

josephlr commented Apr 8, 2022

@Freax13 That's a really good point. Should we just add a note to the README mentioning that crate? It could be in a "other helper crates" section.

@adsnaider
Copy link
Author

@Freax13 Thank you for the info. I agree that having that linked in the README would be sufficient in my opinion.

josephlr added a commit that referenced this issue Apr 20, 2022
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

Successfully merging a pull request may close this issue.

3 participants