Skip to content

Commit 9ae4c91

Browse files
committed
Add runtime feature detection for keylocker
1 parent cf10e91 commit 9ae4c91

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

crates/std_detect/src/detect/arch/x86.rs

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ features! {
103103
/// * `"xsaves"`
104104
/// * `"xsavec"`
105105
/// * `"cmpxchg16b"`
106+
/// * `"kl"`
107+
/// * `"widekl"`
106108
/// * `"adx"`
107109
/// * `"rtm"`
108110
/// * `"movbe"`
@@ -241,6 +243,10 @@ features! {
241243
/// XSAVEC (Save Processor Extended States Compacted)
242244
@FEATURE: #[stable(feature = "simd_x86", since = "1.27.0")] cmpxchg16b: "cmpxchg16b";
243245
/// CMPXCH16B (16-byte compare-and-swap instruction)
246+
@FEATURE: #[unstable(feature = "keylocker_x86", issue = "134813")] kl: "kl";
247+
/// Intel Key Locker
248+
@FEATURE: #[unstable(feature = "keylocker_x86", issue = "134813")] widekl: "widekl";
249+
/// Intel Key Locker Wide
244250
@FEATURE: #[stable(feature = "simd_x86_adx", since = "1.33.0")] adx: "adx";
245251
/// ADX, Intel ADX (Multi-Precision Add-Carry Instruction Extensions)
246252
@FEATURE: #[stable(feature = "simd_x86", since = "1.27.0")] rtm: "rtm";

crates/std_detect/src/detect/os/x86.rs

+7
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ pub(crate) fn detect_features() -> cache::Initializer {
141141

142142
enable(extended_features_ebx, 9, Feature::ermsb);
143143

144+
// Detect if CPUID.19h available
145+
if bit::test(extended_features_ecx as usize, 23) {
146+
let CpuidResult { ebx, .. } = unsafe { __cpuid(0x19) };
147+
enable(ebx, 0, Feature::kl);
148+
enable(ebx, 2, Feature::widekl);
149+
}
150+
144151
// `XSAVE` and `AVX` support:
145152
let cpu_xsave = bit::test(proc_info_ecx as usize, 26);
146153
if cpu_xsave {

crates/std_detect/tests/cpu-detection.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![cfg_attr(target_arch = "s390x", feature(stdarch_s390x_feature_detection))]
88
#![cfg_attr(
99
any(target_arch = "x86", target_arch = "x86_64"),
10-
feature(sha512_sm_x86, x86_amx_intrinsics, xop_target_feature)
10+
feature(sha512_sm_x86, x86_amx_intrinsics, xop_target_feature, keylocker_x86)
1111
)]
1212
#![allow(clippy::unwrap_used, clippy::use_debug, clippy::print_stdout)]
1313

@@ -318,6 +318,8 @@ fn x86_all() {
318318
println!("amx-fp16: {:?}", is_x86_feature_detected!("amx-fp16"));
319319
println!("amx-complex: {:?}", is_x86_feature_detected!("amx-complex"));
320320
println!("xop: {:?}", is_x86_feature_detected!("xop"));
321+
println!("kl: {:?}", is_x86_feature_detected!("kl"));
322+
println!("widekl: {:?}", is_x86_feature_detected!("widekl"));
321323
}
322324

323325
#[test]

crates/std_detect/tests/x86-specific.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
avx512_target_feature,
66
sha512_sm_x86,
77
x86_amx_intrinsics,
8-
xop_target_feature
8+
xop_target_feature,
9+
keylocker_x86
910
)]
1011

1112
extern crate cupid;
@@ -94,6 +95,8 @@ fn dump() {
9495
println!("amx-fp16: {:?}", is_x86_feature_detected!("amx-fp16"));
9596
println!("amx-complex: {:?}", is_x86_feature_detected!("amx-complex"));
9697
println!("xop: {:?}", is_x86_feature_detected!("xop"));
98+
println!("kl: {:?}", is_x86_feature_detected!("kl"));
99+
println!("widekl: {:?}", is_x86_feature_detected!("widekl"));
97100
}
98101

99102
#[cfg(feature = "std_detect_env_override")]

0 commit comments

Comments
 (0)