Skip to content

Commit b4885cf

Browse files
committed
[kernel][lib][arch] Initial version
1 parent afb8fc8 commit b4885cf

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! Copyright 2025 The Drift Authors. All rights reserved.
2+
//! Use of this source code is governed by a BSD-style license that can be
3+
//! found in the LICENSE file.
4+
5+
/// Yield the processor momentarily. This should be used in busy waits.
6+
pub fn yield() void {
7+
asm volatile ("pause");
8+
}
9+
10+
/// Synchronize all memory accesses of all kinds.
11+
pub fn deviceMemoryBarrier() void {
12+
asm volatile ("mfence" ::: "memory");
13+
}
14+
15+
/// Synchronize the ordering of all memory accesses wrt other CPUs.
16+
pub fn threadMemoryBarrier() void {
17+
deviceMemoryBarrier();
18+
}
19+
20+
/// Force the processor to complete all modifications to register state and
21+
/// memory by previous instructions (including draining any buffered writes)
22+
/// before the next instruction is fetched.
23+
pub fn serializeInstructions() void {
24+
// [intel/vol3]: 8.3 Serializing Instructions.
25+
// [amd/vol2]: 7.6.4 Serializing Instructions.
26+
//
27+
// `cpuid` is a serializing instruction.
28+
var rax: usize = 0;
29+
asm volatile ("cpuid"
30+
: [rax] "+{ax}" (rax),
31+
:
32+
: "rbx", "rcx", "rdx"
33+
);
34+
}
35+
36+
/// Return the current CPU cycle count.
37+
pub fn cycles() u64 {
38+
var result: u64 = undefined;
39+
asm volatile ("rdtsc"
40+
: [result] "={ax}" (result),
41+
:
42+
: "rdx"
43+
);
44+
return result;
45+
}

0 commit comments

Comments
 (0)