Skip to content

Commit be0a653

Browse files
authored
Merge pull request #1799 from hermit-os/kernel_function
refactor: inline kernel_function macro to allow non-Copy arguments
2 parents 8fc1f9d + f7a6327 commit be0a653

File tree

3 files changed

+48
-132
lines changed

3 files changed

+48
-132
lines changed

hermit-macro/src/system.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,26 @@ fn emit_func(func: ItemFn, sig: &ParsedSig, errno: bool) -> Result<ItemFn> {
165165
ret
166166
}},
167167
};
168+
let kernel_function_ident =
169+
Ident::new(&format!("kernel_function{}", args.len()), Span::call_site());
168170

169171
let sys_func = ItemFn {
170172
block: parse_quote! {{
171173
#inner_func
172174

173175
#kernel_func
174176

175-
#unsafety { kernel_function!(#kernel_ident(#(#args),*)) }
177+
#[cfg(not(any(
178+
target_arch = "riscv64",
179+
feature = "common-os"
180+
)))]
181+
unsafe { crate::arch::switch::#kernel_function_ident(#kernel_ident, #(#args),*) }
182+
183+
#[cfg(any(
184+
target_arch = "riscv64",
185+
feature = "common-os"
186+
))]
187+
#unsafety { #kernel_ident(#(#args),*) }
176188
}},
177189
..func
178190
};
@@ -243,7 +255,17 @@ mod tests {
243255
ret
244256
}
245257

246-
{ kernel_function!(_sys_test(a, b)) }
258+
#[cfg(not(any(
259+
target_arch = "riscv64",
260+
feature = "common-os"
261+
)))]
262+
unsafe { crate::arch::switch::kernel_function2(_sys_test, a, b) }
263+
264+
#[cfg(any(
265+
target_arch = "riscv64",
266+
feature = "common-os"
267+
))]
268+
{ _sys_test(a, b) }
247269
}
248270
};
249271

@@ -302,7 +324,17 @@ mod tests {
302324
ret
303325
}
304326

305-
unsafe { kernel_function!(_sys_test(a, b)) }
327+
#[cfg(not(any(
328+
target_arch = "riscv64",
329+
feature = "common-os"
330+
)))]
331+
unsafe { crate::arch::switch::kernel_function2(_sys_test, a, b) }
332+
333+
#[cfg(any(
334+
target_arch = "riscv64",
335+
feature = "common-os"
336+
))]
337+
unsafe { _sys_test(a, b) }
306338
}
307339
};
308340

@@ -363,7 +395,17 @@ mod tests {
363395
ret
364396
}
365397

366-
{ kernel_function!(_sys_test(a, b)) }
398+
#[cfg(not(any(
399+
target_arch = "riscv64",
400+
feature = "common-os"
401+
)))]
402+
unsafe { crate::arch::switch::kernel_function2(_sys_test, a, b) }
403+
404+
#[cfg(any(
405+
target_arch = "riscv64",
406+
feature = "common-os"
407+
))]
408+
{ _sys_test(a, b) }
367409
}
368410
};
369411

src/macros.rs

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -76,129 +76,6 @@ macro_rules! dbg {
7676
};
7777
}
7878

79-
cfg_if::cfg_if! {
80-
if #[cfg(not(any(
81-
target_arch = "riscv64",
82-
feature = "common-os"
83-
)))] {
84-
/// Runs `f` on the kernel stack.
85-
///
86-
/// All arguments and return values have to fit into registers:
87-
///
88-
/// ```
89-
/// assert!(mem::size_of::<T>() <= mem::size_of::<usize>());
90-
/// ```
91-
///
92-
/// When working with bigger types, manually route the data over pointers:
93-
///
94-
/// ```
95-
/// f(&arg1, &mut ret);
96-
/// // instead of
97-
/// let ret = f(arg);
98-
/// ```
99-
#[allow(unused_macro_rules)]
100-
macro_rules! kernel_function {
101-
($f:ident()) => {{
102-
// This propagates any unsafety requirements of `f` to the caller.
103-
if false {
104-
$f();
105-
}
106-
107-
#[allow(unreachable_code)]
108-
#[allow(unused_unsafe)]
109-
unsafe {
110-
$crate::arch::switch::kernel_function0($f)
111-
}
112-
}};
113-
114-
($f:ident($arg1:expr)) => {{
115-
// This propagates any unsafety requirements of `f` to the caller.
116-
if false {
117-
$f($arg1);
118-
}
119-
120-
#[allow(unreachable_code)]
121-
#[allow(unused_unsafe)]
122-
unsafe {
123-
$crate::arch::switch::kernel_function1($f, $arg1)
124-
}
125-
}};
126-
127-
($f:ident($arg1:expr, $arg2:expr)) => {{
128-
// This propagates any unsafety requirements of `f` to the caller.
129-
if false {
130-
$f($arg1, $arg2);
131-
}
132-
133-
#[allow(unreachable_code)]
134-
#[allow(unused_unsafe)]
135-
unsafe {
136-
$crate::arch::switch::kernel_function2($f, $arg1, $arg2)
137-
}
138-
}};
139-
140-
($f:ident($arg1:expr, $arg2:expr, $arg3:expr)) => {{
141-
// This propagates any unsafety requirements of `f` to the caller.
142-
if false {
143-
$f($arg1, $arg2, $arg3);
144-
}
145-
146-
#[allow(unreachable_code)]
147-
#[allow(unused_unsafe)]
148-
unsafe {
149-
$crate::arch::switch::kernel_function3($f, $arg1, $arg2, $arg3)
150-
}
151-
}};
152-
153-
($f:ident($arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr)) => {{
154-
// This propagates any unsafety requirements of `f` to the caller.
155-
if false {
156-
$f($arg1, $arg2, $arg3, $arg4);
157-
}
158-
159-
#[allow(unreachable_code)]
160-
#[allow(unused_unsafe)]
161-
unsafe {
162-
$crate::arch::switch::kernel_function4($f, $arg1, $arg2, $arg3, $arg4)
163-
}
164-
}};
165-
166-
($f:ident($arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr)) => {{
167-
// This propagates any unsafety requirements of `f` to the caller.
168-
if false {
169-
$f($arg1, $arg2, $arg3, $arg4, $arg5);
170-
}
171-
172-
#[allow(unreachable_code)]
173-
#[allow(unused_unsafe)]
174-
unsafe {
175-
$crate::arch::switch::kernel_function5($f, $arg1, $arg2, $arg3, $arg4, $arg5)
176-
}
177-
}};
178-
179-
($f:ident($arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr, $arg6:expr)) => {{
180-
// This propagates any unsafety requirements of `f` to the caller.
181-
if false {
182-
$f($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
183-
}
184-
185-
#[allow(unreachable_code)]
186-
#[allow(unused_unsafe)]
187-
unsafe {
188-
$crate::arch::switch::kernel_function6($f, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6)
189-
}
190-
}};
191-
}
192-
} else {
193-
// TODO: Switch kernel stack on RISC-V
194-
macro_rules! kernel_function {
195-
($f:ident($($x:tt)*)) => {{
196-
$f($($x)*)
197-
}};
198-
}
199-
}
200-
}
201-
20279
/// Returns the value of the specified environment variable.
20380
///
20481
/// The value is fetched from the current runtime environment and, if not

src/syscalls/entropy.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,15 @@ unsafe fn read_entropy(buf: *mut u8, len: usize, flags: u32) -> isize {
4545
}
4646
}
4747

48-
unsafe extern "C" fn __sys_read_entropy(buf: *mut u8, len: usize, flags: u32) -> isize {
49-
unsafe { read_entropy(buf, len, flags) }
50-
}
51-
5248
/// Fill `len` bytes in `buf` with cryptographically secure random data.
5349
///
5450
/// Returns either the number of bytes written to buf (a positive value) or
5551
/// * `-EINVAL` if `flags` contains unknown flags.
5652
/// * `-ENOSYS` if the system does not support random data generation.
53+
#[hermit_macro::system]
5754
#[unsafe(no_mangle)]
5855
pub unsafe extern "C" fn sys_read_entropy(buf: *mut u8, len: usize, flags: u32) -> isize {
59-
unsafe { kernel_function!(__sys_read_entropy(buf, len, flags)) }
56+
unsafe { read_entropy(buf, len, flags) }
6057
}
6158

6259
/// Create a cryptographicly secure 32bit random number with the support of

0 commit comments

Comments
 (0)