Skip to content

Commit 0275efb

Browse files
danielverkampCommit Bot
authored and
Commit Bot
committed
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: rust-lang/rust#57808 (comment) 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 <[email protected]> Tested-by: kokoro <[email protected]> Commit-Queue: Daniel Verkamp <[email protected]>
1 parent f84c229 commit 0275efb

File tree

5 files changed

+15
-78
lines changed

5 files changed

+15
-78
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x86_64/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name = "x86_64"
33
version = "0.1.0"
44
authors = ["The Chromium OS Authors"]
55
edition = "2018"
6-
build = "build.rs"
76

87
[dependencies]
98
arch = { path = "../arch" }
@@ -20,6 +19,3 @@ remain = "*"
2019
resources = { path = "../resources" }
2120
sync = { path = "../sync" }
2221
sys_util = { path = "../sys_util" }
23-
24-
[build-dependencies]
25-
cc = "=1.0.25"

x86_64/build.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

x86_64/host_cpuid.c

Lines changed: 0 additions & 11 deletions
This file was deleted.

x86_64/src/cpuid.rs

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
use std::arch::x86_64::{__cpuid, __cpuid_count};
56
use std::fmt::{self, Display};
67
use std::result;
78

@@ -28,19 +29,6 @@ impl Display for Error {
2829
}
2930
}
3031

31-
// This function is implemented in C because stable rustc does not
32-
// support inline assembly.
33-
extern "C" {
34-
fn host_cpuid(
35-
func: u32,
36-
func2: u32,
37-
rEax: *mut u32,
38-
rEbx: *mut u32,
39-
rEcx: *mut u32,
40-
rEdx: *mut u32,
41-
) -> ();
42-
}
43-
4432
// CPUID bits in ebx, ecx, and edx.
4533
const EBX_CLFLUSH_CACHELINE: u32 = 8; // Flush a cache line size.
4634
const EBX_CLFLUSH_SIZE_SHIFT: u32 = 8; // Bytes flushed when executing CLFLUSH.
@@ -77,25 +65,19 @@ fn filter_cpuid(
7765
}
7866
}
7967
2 | 0x80000005 | 0x80000006 => unsafe {
80-
host_cpuid(
81-
entry.function,
82-
0,
83-
&mut entry.eax as *mut u32,
84-
&mut entry.ebx as *mut u32,
85-
&mut entry.ecx as *mut u32,
86-
&mut entry.edx as *mut u32,
87-
);
68+
let result = __cpuid(entry.function);
69+
entry.eax = result.eax;
70+
entry.ebx = result.ebx;
71+
entry.ecx = result.ecx;
72+
entry.edx = result.edx;
8873
},
8974
4 => {
9075
unsafe {
91-
host_cpuid(
92-
entry.function,
93-
entry.index,
94-
&mut entry.eax as *mut u32,
95-
&mut entry.ebx as *mut u32,
96-
&mut entry.ecx as *mut u32,
97-
&mut entry.edx as *mut u32,
98-
);
76+
let result = __cpuid_count(entry.function, entry.index);
77+
entry.eax = result.eax;
78+
entry.ebx = result.ebx;
79+
entry.ecx = result.ecx;
80+
entry.edx = result.edx;
9981
}
10082
entry.eax &= !0xFC000000;
10183
}
@@ -132,34 +114,12 @@ pub fn setup_cpuid(kvm: &kvm::Kvm, vcpu: &kvm::Vcpu, cpu_id: u64, nrcpus: u64) -
132114

133115
/// get host cpu max physical address bits
134116
pub fn phy_max_address_bits() -> u32 {
135-
let mut eax: u32 = 0;
136-
let mut ebx: u32 = 0;
137-
let mut ecx: u32 = 0;
138-
let mut edx: u32 = 0;
139117
let mut phys_bits: u32 = 36;
140118

141-
unsafe {
142-
host_cpuid(
143-
0x80000000,
144-
0,
145-
&mut eax as *mut u32,
146-
&mut ebx as *mut u32,
147-
&mut ecx as *mut u32,
148-
&mut edx as *mut u32,
149-
);
150-
}
151-
if eax >= 0x80000008 {
152-
unsafe {
153-
host_cpuid(
154-
0x80000008,
155-
0,
156-
&mut eax as *mut u32,
157-
&mut ebx as *mut u32,
158-
&mut ecx as *mut u32,
159-
&mut edx as *mut u32,
160-
);
161-
}
162-
phys_bits = eax & 0xff;
119+
let highest_ext_function = unsafe { __cpuid(0x80000000) };
120+
if highest_ext_function.eax >= 0x80000008 {
121+
let addr_size = unsafe { __cpuid(0x80000008) };
122+
phys_bits = addr_size.eax & 0xff;
163123
}
164124

165125
phys_bits

0 commit comments

Comments
 (0)