diff options
author | Daniel Verkamp <dverkamp@chromium.org> | 2020-02-20 12:13:09 -0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-02-22 08:41:29 +0000 |
commit | 0275efb3a00b2cbe15ec92a314bf163a3ca1433e (patch) | |
tree | 0913044d798176f4715fda6d4cad933dae02b8be /x86_64/src/cpuid.rs | |
parent | f84c2298e9d7138be0998c289825128144234862 (diff) | |
download | crosvm-0275efb3a00b2cbe15ec92a314bf163a3ca1433e.tar crosvm-0275efb3a00b2cbe15ec92a314bf163a3ca1433e.tar.gz crosvm-0275efb3a00b2cbe15ec92a314bf163a3ca1433e.tar.bz2 crosvm-0275efb3a00b2cbe15ec92a314bf163a3ca1433e.tar.lz crosvm-0275efb3a00b2cbe15ec92a314bf163a3ca1433e.tar.xz crosvm-0275efb3a00b2cbe15ec92a314bf163a3ca1433e.tar.zst crosvm-0275efb3a00b2cbe15ec92a314bf163a3ca1433e.zip |
x86_64: use __cpuid intrinsic
Use the Rust __cpuid and __cpuid_count intrinsics to replace the C implementation in host_cpuid.c. These are defined in core, but they are also re-exported in std, despite being undocumented there due to technical reasons: https://github.com/rust-lang/rust/pull/57808#issuecomment-457390549 Use the std version for consistency (we don't currently use anything from core anywhere else in crosvm). BUG=None TEST=cargo test -p x86_64 TEST=Boot crosvm on x86_64 Change-Id: Ic7a1094d1b804304a2944f8ee1fe55c5e2db23e9 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2067159 Reviewed-by: Zach Reizner <zachr@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Diffstat (limited to 'x86_64/src/cpuid.rs')
-rw-r--r-- | x86_64/src/cpuid.rs | 70 |
1 files changed, 15 insertions, 55 deletions
diff --git a/x86_64/src/cpuid.rs b/x86_64/src/cpuid.rs index a42b662..46294b2 100644 --- a/x86_64/src/cpuid.rs +++ b/x86_64/src/cpuid.rs @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +use std::arch::x86_64::{__cpuid, __cpuid_count}; use std::fmt::{self, Display}; use std::result; @@ -28,19 +29,6 @@ impl Display for Error { } } -// This function is implemented in C because stable rustc does not -// support inline assembly. -extern "C" { - fn host_cpuid( - func: u32, - func2: u32, - rEax: *mut u32, - rEbx: *mut u32, - rEcx: *mut u32, - rEdx: *mut u32, - ) -> (); -} - // CPUID bits in ebx, ecx, and edx. const EBX_CLFLUSH_CACHELINE: u32 = 8; // Flush a cache line size. const EBX_CLFLUSH_SIZE_SHIFT: u32 = 8; // Bytes flushed when executing CLFLUSH. @@ -77,25 +65,19 @@ fn filter_cpuid( } } 2 | 0x80000005 | 0x80000006 => unsafe { - host_cpuid( - entry.function, - 0, - &mut entry.eax as *mut u32, - &mut entry.ebx as *mut u32, - &mut entry.ecx as *mut u32, - &mut entry.edx as *mut u32, - ); + let result = __cpuid(entry.function); + entry.eax = result.eax; + entry.ebx = result.ebx; + entry.ecx = result.ecx; + entry.edx = result.edx; }, 4 => { unsafe { - host_cpuid( - entry.function, - entry.index, - &mut entry.eax as *mut u32, - &mut entry.ebx as *mut u32, - &mut entry.ecx as *mut u32, - &mut entry.edx as *mut u32, - ); + let result = __cpuid_count(entry.function, entry.index); + entry.eax = result.eax; + entry.ebx = result.ebx; + entry.ecx = result.ecx; + entry.edx = result.edx; } entry.eax &= !0xFC000000; } @@ -132,34 +114,12 @@ pub fn setup_cpuid(kvm: &kvm::Kvm, vcpu: &kvm::Vcpu, cpu_id: u64, nrcpus: u64) - /// get host cpu max physical address bits pub fn phy_max_address_bits() -> u32 { - let mut eax: u32 = 0; - let mut ebx: u32 = 0; - let mut ecx: u32 = 0; - let mut edx: u32 = 0; let mut phys_bits: u32 = 36; - unsafe { - host_cpuid( - 0x80000000, - 0, - &mut eax as *mut u32, - &mut ebx as *mut u32, - &mut ecx as *mut u32, - &mut edx as *mut u32, - ); - } - if eax >= 0x80000008 { - unsafe { - host_cpuid( - 0x80000008, - 0, - &mut eax as *mut u32, - &mut ebx as *mut u32, - &mut ecx as *mut u32, - &mut edx as *mut u32, - ); - } - phys_bits = eax & 0xff; + let highest_ext_function = unsafe { __cpuid(0x80000000) }; + if highest_ext_function.eax >= 0x80000008 { + let addr_size = unsafe { __cpuid(0x80000008) }; + phys_bits = addr_size.eax & 0xff; } phys_bits |