Skip to content

Commit aad95e7

Browse files
committed
[trace-guest] move invariant_tsc logic to separate module
Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 5fe8c72 commit aad95e7

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2025 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/// Module for checking invariant TSC support and reading the timestamp counter
18+
use core::arch::x86_64::{__cpuid, _rdtsc};
19+
20+
/// Check if the processor supports invariant TSC
21+
///
22+
/// Returns true if CPUID.80000007H:EDX[8] is set, indicating invariant TSC support
23+
pub fn has_invariant_tsc() -> bool {
24+
// Check if extended CPUID functions are available
25+
let max_extended = unsafe { __cpuid(0x80000000) };
26+
if max_extended.eax < 0x80000007 {
27+
return false;
28+
}
29+
30+
// Query CPUID.80000007H for invariant TSC support
31+
let cpuid_result = unsafe { __cpuid(0x80000007) };
32+
33+
// Check bit 8 of EDX register for invariant TSC support
34+
(cpuid_result.edx & (1 << 8)) != 0
35+
}
36+
37+
/// Read the timestamp counter
38+
///
39+
/// This function provides a high-performance timestamp by reading the TSC.
40+
/// Should only be used when invariant TSC is supported for reliable timing.
41+
///
42+
/// # Safety
43+
/// This function uses unsafe assembly instructions but is safe to call.
44+
/// However, the resulting timestamp is only meaningful if invariant TSC is supported.
45+
pub fn read_tsc() -> u64 {
46+
unsafe { _rdtsc() }
47+
}

src/hyperlight_guest_tracing/src/lib.rs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,9 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
#![no_std]
17-
18-
/// Module for checking invariant TSC support and reading the timestamp counter
19-
pub mod invariant_tsc {
20-
use core::arch::x86_64::{__cpuid, _rdtsc};
21-
22-
/// Check if the processor supports invariant TSC
23-
///
24-
/// Returns true if CPUID.80000007H:EDX[8] is set, indicating invariant TSC support
25-
pub fn has_invariant_tsc() -> bool {
26-
// Check if extended CPUID functions are available
27-
let max_extended = unsafe { __cpuid(0x80000000) };
28-
if max_extended.eax < 0x80000007 {
29-
return false;
30-
}
3116

32-
// Query CPUID.80000007H for invariant TSC support
33-
let cpuid_result = unsafe { __cpuid(0x80000007) };
34-
35-
// Check bit 8 of EDX register for invariant TSC support
36-
(cpuid_result.edx & (1 << 8)) != 0
37-
}
17+
#![no_std]
18+
use heapless as hl;
3819

39-
/// Read the timestamp counter
40-
///
41-
/// This function provides a high-performance timestamp by reading the TSC.
42-
/// Should only be used when invariant TSC is supported for reliable timing.
43-
///
44-
/// # Safety
45-
/// This function uses unsafe assembly instructions but is safe to call.
46-
/// However, the resulting timestamp is only meaningful if invariant TSC is supported.
47-
pub fn read_tsc() -> u64 {
48-
unsafe { _rdtsc() }
49-
}
50-
}
20+
/// Expose invariant TSC module
21+
pub mod invariant_tsc;

0 commit comments

Comments
 (0)