rialo_s_define_syscall/
lib.rs1#[cfg(not(target_arch = "riscv64"))]
2mod ebpf;
3
4#[cfg(target_arch = "riscv64")]
5mod risc_v;
6
7#[cfg(not(target_arch = "riscv64"))]
8pub mod definitions {
9 pub use crate::ebpf::*;
10}
11
12#[cfg(target_arch = "riscv64")]
13pub mod definitions {
14 pub use crate::risc_v::*;
15}
16
17#[cfg(target_arch = "riscv64")]
19#[doc(hidden)]
20pub mod __private {
21 pub use polkavm_derive::{self, default_abi};
22}
23
24#[cfg(all(target_feature = "static-syscalls", not(target_arch = "riscv64")))]
25#[macro_export]
26macro_rules! define_syscall {
27 (fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
28 #[inline]
29 pub unsafe fn $name($($arg: $typ),*) -> $ret {
30 #[repr(usize)]
32 enum Syscall {
33 Code = $crate::sys_hash(stringify!($name)),
34 }
35
36 let syscall: extern "C" fn($($arg: $typ),*) -> $ret = core::mem::transmute(Syscall::Code);
37 syscall($($arg),*)
38 }
39
40 };
41 (fn $name:ident($($arg:ident: $typ:ty),*)) => {
42 define_syscall!(fn $name($($arg: $typ),*) -> ());
43 }
44}
45
46#[cfg(all(not(target_feature = "static-syscalls"), not(target_arch = "riscv64")))]
47#[macro_export]
48macro_rules! define_syscall {
49 (fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
50 extern "C" {
51 pub fn $name($($arg: $typ),*) -> $ret;
52 }
53 };
54 (fn $name:ident($($arg:ident: $typ:ty),*)) => {
55 define_syscall!(fn $name($($arg: $typ),*) -> ());
56 }
57}
58
59#[cfg(target_arch = "riscv64")]
64#[macro_export]
65macro_rules! define_syscall_riscv {
66 (fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
67 #[$crate::__private::polkavm_derive::polkavm_import(abi = $crate::__private::polkavm_derive::default_abi)]
72 extern "C" {
73 pub fn $name($($arg: $typ),*) -> $ret;
74 }
75 };
76 (fn $name:ident($($arg:ident: $typ:ty),*)) => {
77 $crate::define_syscall_riscv!(fn $name($($arg: $typ),*) -> ());
78 }
79}
80
81#[cfg(not(target_arch = "riscv64"))]
83#[macro_export]
84macro_rules! define_syscall_riscv {
85 ($($tt:tt)*) => {
86 compile_error!("define_syscall_riscv! is only available on RISC-V targets");
87 };
88}
89
90#[cfg(all(target_feature = "static-syscalls", not(target_arch = "riscv64")))]
91pub const fn sys_hash(name: &str) -> usize {
92 murmur3_32(name.as_bytes(), 0) as usize
93}
94
95#[cfg(all(target_feature = "static-syscalls", not(target_arch = "riscv64")))]
96const fn murmur3_32(buf: &[u8], seed: u32) -> u32 {
97 const fn pre_mix(buf: [u8; 4]) -> u32 {
98 u32::from_le_bytes(buf)
99 .wrapping_mul(0xcc9e2d51)
100 .rotate_left(15)
101 .wrapping_mul(0x1b873593)
102 }
103
104 let mut hash = seed;
105
106 let mut i = 0;
107 while i < buf.len() / 4 {
108 let buf = [buf[i * 4], buf[i * 4 + 1], buf[i * 4 + 2], buf[i * 4 + 3]];
109 hash ^= pre_mix(buf);
110 hash = hash.rotate_left(13);
111 hash = hash.wrapping_mul(5).wrapping_add(0xe6546b64);
112
113 i += 1;
114 }
115
116 match buf.len() % 4 {
117 0 => {}
118 1 => {
119 hash = hash ^ pre_mix([buf[i * 4], 0, 0, 0]);
120 }
121 2 => {
122 hash = hash ^ pre_mix([buf[i * 4], buf[i * 4 + 1], 0, 0]);
123 }
124 3 => {
125 hash = hash ^ pre_mix([buf[i * 4], buf[i * 4 + 1], buf[i * 4 + 2], 0]);
126 }
127 _ => { }
128 }
129
130 hash = hash ^ buf.len() as u32;
131 hash = hash ^ (hash.wrapping_shr(16));
132 hash = hash.wrapping_mul(0x85ebca6b);
133 hash = hash ^ (hash.wrapping_shr(13));
134 hash = hash.wrapping_mul(0xc2b2ae35);
135 hash = hash ^ (hash.wrapping_shr(16));
136
137 hash
138}