Skip to main content

kernel_api/
lib.rs

1//! Popcorn2 kernel public API
2//! 
3//! Provides a mix of utility types and functions to replace `std`, as well as a stable API to kernel internals.
4
5#![feature(min_specialization)]
6#![feature(step_trait)]
7#![feature(doc_cfg)]
8#![feature(arbitrary_self_types_pointers)]
9#![feature(cfg_target_has_atomic)]
10#![feature(const_trait_impl)]
11#![feature(const_convert)]
12#![feature(const_ops)]
13#![feature(const_option_ops)]
14
15#![cfg_attr(target_has_atomic_load_store = "128", feature(integer_atomics))]
16
17#![cfg_attr(feature = "full", feature(type_changing_struct_update))]
18#![cfg_attr(feature = "full", feature(never_type))]
19#![cfg_attr(feature = "full", feature(debug_closure_helpers))]
20#![cfg_attr(feature = "full", feature(unsize))]
21#![cfg_attr(feature = "full", feature(dispatch_from_dyn))]
22#![cfg_attr(feature = "full", feature(coerce_unsized))]
23#![cfg_attr(feature = "full", feature(pointer_is_aligned_to))]
24#![cfg_attr(feature = "full", feature(slice_ptr_get))]
25#![cfg_attr(feature = "full", feature(ptr_metadata))]
26#![cfg_attr(feature = "full", feature(maybe_uninit_fill))]
27#![cfg_attr(feature = "full", feature(sanitize))]
28#![cfg_attr(feature = "full", feature(negative_impls))]
29#![cfg_attr(feature = "full", feature(decl_macro))]
30
31#![allow(type_alias_bounds)]
32
33#![cfg_attr(not(feature = "use_std"), no_std)]
34
35extern crate alloc;
36
37pub mod memory;
38
39#[cfg(feature = "full")]
40pub mod sync;
41
42#[cfg(feature = "full")]
43mod bridge;
44
45pub mod ptr;
46
47#[cfg(feature = "full")]
48pub mod time;
49
50#[cfg(feature = "full")]
51pub mod detect;
52
53#[cfg(feature = "full")]
54pub mod address_space;
55
56pub mod mapping;
57
58#[cfg(feature = "full")]
59pub mod allocator;
60
61#[cfg(feature = "full")]
62pub mod threading;
63
64#[cfg(feature = "full")]
65pub mod syscall;
66
67#[cfg(feature = "full")]
68pub mod executor;
69
70#[cfg(feature = "full")]
71pub mod channel;
72
73#[cfg(feature = "full")]
74pub mod modules;
75
76mod sealed {
77    pub trait Sealed {}
78}
79
80/// Prints and returns the value of a given expression for quick and dirty debugging
81#[macro_export]
82macro_rules! dbg {
83    ($val:expr $(,)?) => {
84        // Use of `match` here is intentional because it affects the lifetimes
85        // of temporaries - https://stackoverflow.com/a/48732525/1063961
86        match $val {
87            tmp => {
88                ::log::debug!("{} = {:#x?}",
89                    ::core::stringify!($val), &tmp);
90                tmp
91            }
92        }
93    };
94    ($($val:expr),+ $(,)?) => {
95        ($($crate::dbg!($val)),+,)
96    };
97}
98
99#[macro_export]
100macro_rules! newtype_enum {
101    (
102        $(#[$type_attrs:meta])*
103        $visibility:vis enum $type:ident : $base_vis:vis $base_integer:ty => $(#[$impl_attrs:meta])* {
104            $(
105                $(#[$variant_attrs:meta])*
106                $variant:ident = $value:expr,
107            )*
108        }
109    ) => {
110        $(#[$type_attrs])*
111        #[repr(transparent)]
112        #[derive(Clone, Copy, Eq, PartialEq)]
113        $visibility struct $type($base_vis $base_integer);
114
115        $(#[$impl_attrs])*
116        #[allow(unused)]
117        impl $type {
118            $(
119                $(#[$variant_attrs])*
120                pub const $variant: $type = $type($value);
121            )*
122        }
123
124        #[allow(unused)]
125        impl core::fmt::Debug for $type {
126            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
127                match *self {
128                    // Display variants by their name, like Rust enums do
129                    $(
130                        $type::$variant => write!(f, stringify!($variant)),
131                    )*
132
133                    // Display unknown variants in tuple struct format
134                    $type(unknown) => {
135                        write!(f, "{}({})", stringify!($type), unknown)
136                    }
137                }
138            }
139        }
140    }
141}
142