Skip to content

Commit 89a67b4

Browse files
committed
cpu/x86/x86_64: Fix detection of extended features.
Because the name `r` is used for multiple results, `r.eax` in the check for leaf 7 was using the results of the leaf 1 query instead of the results of the leaf 0 query. Fix this.
1 parent 7105c9d commit 89a67b4

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

src/cpu/x86.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,17 @@ unsafe fn cpuid_all() -> CpuidSummary {
104104
// see https://github.com/rust-lang/rust/pull/101861.
105105

106106
// Intel: "21.1.1 Notes on Where to Start".
107-
let r = unsafe { arch::__cpuid(0) };
107+
let leaf0 = unsafe { arch::__cpuid(0) };
108108

109-
let leaf1_edx;
110-
let leaf1_ecx;
111-
112-
if r.eax >= 1 {
109+
let (leaf1_edx, leaf1_ecx) = if leaf0.eax >= 1 {
113110
// SAFETY: `r.eax >= 1` indicates leaf 1 is available.
114-
let r = unsafe { arch::__cpuid(1) };
115-
leaf1_edx = r.edx;
116-
leaf1_ecx = r.ecx;
111+
let leaf1 = unsafe { arch::__cpuid(1) };
112+
(leaf1.edx, leaf1.ecx)
117113
} else {
118114
// Expected to be unreachable on any environment we currently
119115
// support.
120-
leaf1_edx = 0;
121-
leaf1_ecx = 0;
122-
}
116+
(0, 0)
117+
};
123118

124119
CpuidSummary {
125120
leaf1_edx,

src/cpu/x86_64.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,35 +111,28 @@ unsafe fn cpuid_all() -> CpuidSummary {
111111
// see https://github.com/rust-lang/rust/pull/101861.
112112

113113
// Intel: "21.1.1 Notes on Where to Start".
114-
let r = unsafe { arch::__cpuid(0) };
114+
let leaf0 = unsafe { arch::__cpuid(0) };
115115

116-
let leaf1_ecx;
116+
let is_intel =
117+
(leaf0.ebx == 0x756e6547) && (leaf0.edx == 0x49656e69) && (leaf0.ecx == 0x6c65746e);
117118

118-
let is_intel = (r.ebx == 0x756e6547) && (r.edx == 0x49656e69) && (r.ecx == 0x6c65746e);
119-
120-
let (extended_features_ecx, extended_features_ebx);
121-
122-
if r.eax >= 1 {
123-
// SAFETY: `r.eax >= 1` indicates leaf 1 is available.
124-
let r = unsafe { arch::__cpuid(1) };
125-
leaf1_ecx = r.ecx;
126-
127-
if r.eax >= 7 {
128-
// SAFETY: `r.eax >= 7` implies we can execute this.
129-
let r = unsafe { arch::__cpuid(7) };
130-
extended_features_ecx = r.ecx;
131-
extended_features_ebx = r.ebx;
132-
} else {
133-
extended_features_ecx = 0;
134-
extended_features_ebx = 0;
135-
}
119+
let leaf1_ecx = if leaf0.eax >= 1 {
120+
// SAFETY: `r0.eax >= 1` indicates leaf 1 is available.
121+
let leaf1 = unsafe { arch::__cpuid(1) };
122+
leaf1.ecx
136123
} else {
137124
// Expected to be unreachable on any environment we currently
138125
// support.
139-
leaf1_ecx = 0;
140-
extended_features_ecx = 0;
141-
extended_features_ebx = 0;
142-
}
126+
0
127+
};
128+
129+
let (extended_features_ecx, extended_features_ebx) = if leaf0.eax >= 7 {
130+
// SAFETY: `leaf0.eax >= 7` indicates leaf 7 is available.
131+
let leaf7 = unsafe { arch::__cpuid(7) };
132+
(leaf7.ecx, leaf7.ebx)
133+
} else {
134+
(0, 0)
135+
};
143136

144137
let xcr0 = if check(leaf1_ecx, 27) {
145138
unsafe { arch::_xgetbv(0) }

0 commit comments

Comments
 (0)