kernel/lib.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//! Core Tock Kernel
6//!
7//! The kernel crate implements the core features of Tock as well as shared
8//! code that many chips, capsules, and boards use. It also holds the Hardware
9//! Interface Layer (HIL) definitions.
10//!
11//! Most `unsafe` code is in this kernel crate.
12//!
13//!
14//! ## Core Kernel Visibility
15//!
16//! As the root crate in the Tock operating system, this crate serves multiple
17//! purposes:
18//!
19//! 1. It includes the logic for the core kernel, including process management,
20//! grants, scheduling, etc.
21//!
22//! 2. It includes important interfaces for hardware and other device
23//! abstractions. These are generally in the HIL and platform folders.
24//!
25//! 3. It includes utility functions used elsewhere in the kernel, generally by
26//! multiple different crates such that it makes sense to have shared
27//! implementations in the core kernel crate.
28//!
29//! Because of these different features of the core kernel, managing visibility
30//! of the various objects and functions is a bit tricky. In general, the kernel
31//! crate only exposes what it absolutely needs to. However, there are three
32//! cases where resources in this crate _must_ be exposed.
33//!
34//! 1. The shared utility functions and structs must be exposed. These are
35//! marked `pub` and are used by many other kernel crates.
36//!
37//! Some utility objects and abstractions, however, expose memory unsafe
38//! behavior. These are marked as `unsafe`, and require an `unsafe` block to
39//! use them. One example of this is `StaticRef` which is used for accessing
40//! memory-mapped I/O registers. Since accessing the addresses through just a
41//! memory address is potentially very unsafe, instantiating a `StaticRef`
42//! requires an `unsafe` block.
43//!
44//! 2. The core kernel types generally have to be exposed as other layers of the
45//! OS need to use them. However, generally only a very small interface is
46//! exposed, and using that interface cannot compromise the overall system or
47//! the core kernel. These functions are also marked `pub`. For example, the
48//! `ProcessBuffer` abstraction must be exposed to capsules to use shared memory
49//! between a process and the kernel. However, the constructor is not public,
50//! and the API exposed to capsules is very limited and confined by the Rust
51//! type system. The constructor and other sensitive interfaces are
52//! restricted to use only inside the kernel crate and are marked
53//! `pub(crate)`.
54//!
55//! In some cases, more sensitive core kernel interfaces must be exposed. For
56//! example, the kernel exposes a function for starting the main scheduling
57//! loop in the kernel. Since board crates must be able to start this loop
58//! after all initialization is finished, the kernel loop function must be
59//! exposed and marked `pub`. However, this interface is not generally safe
60//! to use, since starting the loop a second time would compromise the
61//! stability of the overall system. It's also not necessarily memory unsafe
62//! to call the start loop function again, so we do not mark it as `unsafe`.
63//! Instead, we require that the caller hold a `Capability` to call the
64//! public but sensitive functions. More information is in `capabilities.rs`.
65//! This allows the kernel crate to still expose functions as public while
66//! restricting their use. Another example of this is the `Grant`
67//! constructor, which must be called outside of the core kernel, but should
68//! not be called except during the board setup.
69//!
70//! 3. Certain internal core kernel interfaces must also be exposed. These are
71//! needed for extensions of the core kernel that happen to be implemented in
72//! crates outside of the kernel crate. For example, additional
73//! implementations of `Process` may live outside of the kernel crate. To
74//! successfully implement a new `Process` requires access to certain
75//! in-core-kernel APIs, and these must be marked `pub` so that outside
76//! crates can access them.
77//!
78//! These interfaces are highly sensitive, so again we require the caller
79//! hold a Capability to call them. This helps restrict their use and makes
80//! it very clear that calling them requires special permissions.
81//! Additionally, to differentiate these interfaces, which are for external
82//! extensions of core kernel functionality, from the other public but
83//! sensitive interfaces (item 2 above), we append the name `_external` to
84//! the function name.
85//!
86//! One note is that there are currently very few extensions to the core
87//! kernel that live outside of the kernel crate. That means we have not
88//! necessarily created `_extern` functions for all the interfaces needed for
89//! this use case. It is likely we will have to create new interfaces as new
90//! use cases are discovered.
91
92#![warn(unreachable_pub)]
93#![no_std]
94
95/// Kernel major version.
96///
97/// This is compiled with the crate to enable for checking of compatibility with
98/// loaded apps. Both major and minor version constants are updated during a
99/// release.
100pub const KERNEL_MAJOR_VERSION: u16 = 2;
101/// Kernel minor version.
102///
103/// This is compiled with the crate to enable for checking of compatibility with
104/// loaded apps.
105pub const KERNEL_MINOR_VERSION: u16 = 1;
106
107pub mod capabilities;
108pub mod collections;
109pub mod component;
110pub mod debug;
111pub mod deferred_call;
112pub mod errorcode;
113pub mod grant;
114pub mod hil;
115pub mod introspection;
116pub mod ipc;
117pub mod platform;
118pub mod process;
119pub mod process_checker;
120pub mod processbuffer;
121pub mod scheduler;
122pub mod storage_permissions;
123pub mod syscall;
124pub mod upcall;
125pub mod utilities;
126
127mod config;
128mod kernel;
129mod memop;
130mod process_binary;
131mod process_loading;
132mod process_policies;
133mod process_printer;
134mod process_standard;
135mod syscall_driver;
136
137// Core resources exposed as `kernel::Type`.
138pub use crate::errorcode::ErrorCode;
139pub use crate::kernel::Kernel;
140pub use crate::process::ProcessId;
141pub use crate::scheduler::Scheduler;