kernel/utilities/
helpers.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Helper functions and macros.
6//!
7//! These are various utility functions and macros that are useful throughout
8//! the Tock kernel and are provided here for convenience.
9//!
10//! The macros are exported through the top level of the `kernel` crate.
11
12/// Create an object with the given capabilities.
13///
14/// ```
15/// # use kernel::capabilities::{ProcessManagementCapability, MemoryAllocationCapability};
16/// # use kernel::create_capability;
17/// let process_mgmt_cap = create_capability!(ProcessManagementCapability);
18/// let unified_cap = create_capability!(ProcessManagementCapability, MemoryAllocationCapability);
19/// ```
20///
21/// This helper macro cannot be called from `#![forbid(unsafe_code)]` crates,
22/// and is used by trusted code to generate a capability that it can either use
23/// or pass to another module.
24///
25/// # Safety
26///
27/// This macro can only be used in a context that is allowed to use
28/// `unsafe`. Specifically, an internal `allow(unsafe_code)` directive
29/// will conflict with any `forbid(unsafe_code)` at the crate or block
30/// level.
31///
32/// ```compile_fail
33/// # use kernel::capabilities::ProcessManagementCapability;
34/// # use kernel::create_capability;
35/// #[forbid(unsafe_code)]
36/// fn untrusted_fn() {
37///     let process_mgmt_cap = create_capability!(ProcessManagementCapability);
38/// }
39/// ```
40#[macro_export]
41macro_rules! create_capability {
42    ($($T:ty),+) => {{
43        #[allow(unsafe_code)]
44        struct Cap(());
45        $(
46            unsafe impl $T for Cap {}
47        )*
48        Cap(())
49    }};
50}
51
52/// Count the number of passed expressions.
53///
54/// Useful for constructing variable sized arrays in other macros.
55/// Taken from the Little Book of Rust Macros.
56///
57/// ```ignore
58/// use kernel:count_expressions;
59///
60/// let count: usize = count_expressions!(1+2, 3+4);
61/// ```
62#[macro_export]
63macro_rules! count_expressions {
64    () => (0usize);
65    ($head:expr $(,)?) => (1usize);
66    ($head:expr, $($tail:expr),* $(,)?) => (1usize + count_expressions!($($tail),*));
67}
68
69/// Executables must specify their stack size by using the `stack_size!` macro.
70/// It takes a single argument, the desired stack size in bytes. Example:
71/// ```
72/// kernel::stack_size!{0x1000}
73/// ```
74// stack_size works by putting a symbol equal to the size of the stack in the
75// .stack_buffer section. The linker script uses the .stack_buffer section to
76// size the stack.
77#[macro_export]
78macro_rules! stack_size {
79    {$size:expr} => {
80        /// Size to allocate for the stack.
81        #[no_mangle]
82        #[link_section = ".stack_buffer"]
83        static mut STACK_MEMORY: [u8; $size] = [0; $size];
84    }
85}
86
87/// Compute a POSIX-style CRC32 checksum of a slice.
88///
89/// Online calculator: <https://crccalc.com/>
90pub fn crc32_posix(b: &[u8]) -> u32 {
91    let mut crc: u32 = 0;
92
93    for c in b {
94        crc ^= (*c as u32) << 24;
95
96        for _i in 0..8 {
97            if crc & (0b1 << 31) > 0 {
98                crc = (crc << 1) ^ 0x04c11db7;
99            } else {
100                crc <<= 1;
101            }
102        }
103    }
104    !crc
105}