hermit/
macros.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/// Prints to the standard output.
///
/// Adapted from [`std::print`].
///
/// [`std::print`]: https://doc.rust-lang.org/stable/std/macro.print.html
#[cfg(target_os = "none")]
#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => {{
        $crate::console::_print(::core::format_args!($($arg)*));
    }};
}

/// Prints to the standard output, with a newline.
///
/// Adapted from [`std::println`].
///
/// [`std::println`]: https://doc.rust-lang.org/stable/std/macro.println.html
#[cfg(target_os = "none")]
#[macro_export]
macro_rules! println {
    () => {
        $crate::print!("\n")
    };
    ($($arg:tt)*) => {{
        $crate::console::_print(::core::format_args!("{}\n", format_args!($($arg)*)));
    }};
}

/// Prints and returns the value of a given expression for quick and dirty
/// debugging.
// Copied from std/macros.rs
#[cfg(target_os = "none")]
#[macro_export]
macro_rules! dbg {
    // NOTE: We cannot use `concat!` to make a static string as a format argument
    // of `eprintln!` because `file!` could contain a `{` or
    // `$val` expression could be a block (`{ .. }`), in which case the `eprintln!`
    // will be malformed.
    () => {
        $crate::println!("[{}:{}]", ::core::file!(), ::core::line!())
    };
    ($val:expr $(,)?) => {
        // Use of `match` here is intentional because it affects the lifetimes
        // of temporaries - https://stackoverflow.com/a/48732525/1063961
        match $val {
            tmp => {
                $crate::println!("[{}:{}] {} = {:#?}",
                    ::core::file!(), ::core::line!(), ::core::stringify!($val), &tmp);
                tmp
            }
        }
    };
    ($($val:expr),+ $(,)?) => {
        ($($crate::dbg!($val)),+,)
    };
}

/// Runs `f` on the kernel stack.
///
/// All arguments and return values have to fit into registers:
///
/// ```
/// assert!(mem::size_of::<T>() <= mem::size_of::<usize>());
/// ```
///
/// When working with bigger types, manually route the data over pointers:
///
/// ```
/// f(&arg1, &mut ret);
/// // instead of
/// let ret = f(arg);
/// ```
#[allow(unused_macro_rules)]
#[cfg(not(any(
	target_arch = "riscv64",
	all(target_arch = "x86_64", feature = "newlib"),
	feature = "common-os"
)))]
macro_rules! kernel_function {
	($f:ident()) => {{
		use $crate::errno::ToErrno;

		// This propagates any unsafety requirements of `f` to the caller.
		if false {
			$f();
		}

		#[allow(unreachable_code)]
		#[allow(unused_unsafe)]
		unsafe {
			$crate::arch::switch::kernel_function0($f).set_errno()
		}
	}};

	($f:ident($arg1:expr)) => {{
		use $crate::errno::ToErrno;

		// This propagates any unsafety requirements of `f` to the caller.
		if false {
			$f($arg1);
		}

		#[allow(unreachable_code)]
		#[allow(unused_unsafe)]
		unsafe {
			$crate::arch::switch::kernel_function1($f, $arg1).set_errno()
		}
	}};

	($f:ident($arg1:expr, $arg2:expr)) => {{
		use $crate::errno::ToErrno;

		// This propagates any unsafety requirements of `f` to the caller.
		if false {
			$f($arg1, $arg2);
		}

		#[allow(unreachable_code)]
		#[allow(unused_unsafe)]
		unsafe {
			$crate::arch::switch::kernel_function2($f, $arg1, $arg2).set_errno()
		}
	}};

	($f:ident($arg1:expr, $arg2:expr, $arg3:expr)) => {{
		use $crate::errno::ToErrno;

		// This propagates any unsafety requirements of `f` to the caller.
		if false {
			$f($arg1, $arg2, $arg3);
		}

		#[allow(unreachable_code)]
		#[allow(unused_unsafe)]
		unsafe {
			$crate::arch::switch::kernel_function3($f, $arg1, $arg2, $arg3).set_errno()
		}
	}};

	($f:ident($arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr)) => {{
		use $crate::errno::ToErrno;

		// This propagates any unsafety requirements of `f` to the caller.
		if false {
			$f($arg1, $arg2, $arg3, $arg4);
		}

		#[allow(unreachable_code)]
		#[allow(unused_unsafe)]
		unsafe {
			$crate::arch::switch::kernel_function4($f, $arg1, $arg2, $arg3, $arg4).set_errno()
		}
	}};

	($f:ident($arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr)) => {{
		use $crate::errno::ToErrno;

		// This propagates any unsafety requirements of `f` to the caller.
		if false {
			$f($arg1, $arg2, $arg3, $arg4, $arg5);
		}

		#[allow(unreachable_code)]
		#[allow(unused_unsafe)]
		unsafe {
			$crate::arch::switch::kernel_function5($f, $arg1, $arg2, $arg3, $arg4, $arg5)
				.set_errno()
		}
	}};

	($f:ident($arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr, $arg6:expr)) => {{
		use $crate::errno::ToErrno;

		// This propagates any unsafety requirements of `f` to the caller.
		if false {
			$f($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
		}

		#[allow(unreachable_code)]
		#[allow(unused_unsafe)]
		unsafe {
			$crate::arch::switch::kernel_function6($f, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6)
				.set_errno()
		}
	}};
}

// TODO: Properly switch kernel stack with newlib
// https://github.com/hermit-os/kernel/issues/471
// TODO: Switch kernel stack on RISC-V
#[cfg(any(
	target_arch = "riscv64",
	all(target_arch = "x86_64", feature = "newlib"),
	feature = "common-os"
))]
macro_rules! kernel_function {
	($f:ident($($x:tt)*)) => {{
		use $crate::errno::ToErrno;
		#[allow(unreachable_code)]
		$f($($x)*).set_errno()
	}};
}

/// Returns the value of the specified environment variable.
///
/// The value is fetched from the current runtime environment and, if not
/// present, falls back to the same environment variable set at compile time
/// (might not be present as well).
#[allow(unused_macros)]
macro_rules! hermit_var {
	($name:expr) => {{
		use alloc::borrow::Cow;

		match crate::env::var($name) {
			Some(val) => Some(Cow::from(val)),
			None => option_env!($name).map(Cow::Borrowed),
		}
	}};
}

/// Tries to fetch the specified environment variable with a default value.
///
/// Fetches according to [`hermit_var`] or returns the specified default value.
#[allow(unused_macros)]
macro_rules! hermit_var_or {
	($name:expr, $default:expr) => {{
		hermit_var!($name).as_deref().unwrap_or($default)
	}};
}