atlas_define_syscall/
lib.rs1#![no_std]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4pub mod definitions;
5
6#[cfg(any(
7 target_feature = "static-syscalls",
8 all(target_arch = "bpf", feature = "unstable-static-syscalls")
9))]
10#[macro_export]
11macro_rules! define_syscall {
12 (fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
13 #[inline]
14 pub unsafe fn $name($($arg: $typ),*) -> $ret {
15 #[repr(usize)]
17 enum Syscall {
18 Code = $crate::sys_hash(stringify!($name)),
19 }
20
21 let syscall: extern "C" fn($($arg: $typ),*) -> $ret = core::mem::transmute(Syscall::Code);
22 syscall($($arg),*)
23 }
24
25 };
26 (fn $name:ident($($arg:ident: $typ:ty),*)) => {
27 define_syscall!(fn $name($($arg: $typ),*) -> ());
28 }
29}
30
31#[cfg(not(any(
32 target_feature = "static-syscalls",
33 all(target_arch = "bpf", feature = "unstable-static-syscalls")
34)))]
35#[macro_export]
36macro_rules! define_syscall {
37 (fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
38 extern "C" {
39 pub fn $name($($arg: $typ),*) -> $ret;
40 }
41 };
42 (fn $name:ident($($arg:ident: $typ:ty),*)) => {
43 define_syscall!(fn $name($($arg: $typ),*) -> ());
44 }
45}
46
47#[cfg(any(
48 target_feature = "static-syscalls",
49 all(target_arch = "bpf", feature = "unstable-static-syscalls")
50))]
51pub const fn sys_hash(name: &str) -> usize {
52 murmur3_32(name.as_bytes(), 0) as usize
53}
54
55#[cfg(any(
56 target_feature = "static-syscalls",
57 all(target_arch = "bpf", feature = "unstable-static-syscalls")
58))]
59const fn murmur3_32(buf: &[u8], seed: u32) -> u32 {
60 const fn pre_mix(buf: [u8; 4]) -> u32 {
61 u32::from_le_bytes(buf)
62 .wrapping_mul(0xcc9e2d51)
63 .rotate_left(15)
64 .wrapping_mul(0x1b873593)
65 }
66
67 let mut hash = seed;
68
69 let mut i = 0;
70 while i < buf.len() / 4 {
71 let buf = [buf[i * 4], buf[i * 4 + 1], buf[i * 4 + 2], buf[i * 4 + 3]];
72 hash ^= pre_mix(buf);
73 hash = hash.rotate_left(13);
74 hash = hash.wrapping_mul(5).wrapping_add(0xe6546b64);
75
76 i += 1;
77 }
78
79 match buf.len() % 4 {
80 0 => {}
81 1 => {
82 hash = hash ^ pre_mix([buf[i * 4], 0, 0, 0]);
83 }
84 2 => {
85 hash = hash ^ pre_mix([buf[i * 4], buf[i * 4 + 1], 0, 0]);
86 }
87 3 => {
88 hash = hash ^ pre_mix([buf[i * 4], buf[i * 4 + 1], buf[i * 4 + 2], 0]);
89 }
90 _ => { }
91 }
92
93 hash = hash ^ buf.len() as u32;
94 hash = hash ^ (hash.wrapping_shr(16));
95 hash = hash.wrapping_mul(0x85ebca6b);
96 hash = hash ^ (hash.wrapping_shr(13));
97 hash = hash.wrapping_mul(0xc2b2ae35);
98 hash = hash ^ (hash.wrapping_shr(16));
99
100 hash
101}