sel4_microkit

Macro var

Source
macro_rules! var {
    ($(#[$attrs:meta])* $symbol:ident: $ty:ty = $default:expr) => { ... };
}
Expand description

Declares a symbol via which the microkit tool can inject a variable declared by e.g. setvar_vaddr, and returns the variable’s value at runtime.

This macro is represents a lower-level interface than memory_region_symbol.

The following fragment demonstrates its usage:

let my_var: &'static T = var!(my_var_symbol_name: T = MY_DEFAULT_VALUE)

where MY_DEFAULT_VALUE is the value that the variable will be given at compile-time, before the protection domain image is passed to the microkit tool.

The patching mechanism used by the microkit tool requires that the symbol be allocated space in the protection domain’s ELF file, so we declare the symbol as part of the .data section.

For more detail, see this macro’s definition.

§Examples

let foo = bar + *var!(baz: usize = 0);

§Note

The microkit tool requires memory region address symbols to be present in protection domain binaries. To prevent Rust from optimizing them out in cases where it is not used, add the unstable #[used(linker)] attribute. For example:

#![feature(used_with_arg)]

// might be optimized away if not used
memory_region_symbol!(foo: usize = 0)

// won't be optimized away
memory_region_symbol! {
    #[used(linker)]
    foo: usize = 0
}