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. Major, minor and patch 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 = 3;
106/// Kernel patch version.
107pub const KERNEL_PATCH_VERSION: u16 = 0;
108/// Kernel in-development version counter.
109///
110/// Use to distinguish development kernels from release kernels, and can be used
111/// to have applications or tools depend on in-development kernel features that
112/// are not yet included in any release.
113///
114/// A value of `0` indicates that this a release, of the version indicated by
115/// [`KERNEL_MAJOR_VERSION`], [`KERNEL_MINOR_VERSION`] and
116/// [`KERNEL_PATCH_VERSION`].
117///
118/// A value other than `0` indicates that this is a development revision, before
119/// (older than) the next release described by [`KERNEL_MAJOR_VERSION`],
120/// [`KERNEL_MINOR_VERSION`] and [`KERNEL_PATCH_VERSION`].
121pub const KERNEL_PRERELEASE_VERSION: u16 = 1;
122
123/// Tock kernel attributes structure for version information.
124#[repr(C)]
125struct TockAttributesKernelVersion {
126    major: u16,
127    minor: u16,
128    patch: u16,
129    prerelease: u16,
130    tlv_type: u16,
131    tlv_len: u16,
132}
133
134/// Create a data structure which follows the Tock Kernel Attributes format
135/// for specifying the version of the Tock kernel.
136///
137/// More information on kernel attributes is
138/// [here](https://book.tockos.org/doc/kernel_attributes.html).
139///
140/// This is inserted into a specific section that the linker script includes at
141/// the correct location to be included in the attributes.
142#[link_section = ".tock.attr.kernel_version"]
143#[used]
144static TOCK_ATTRIBUTES_KERNEL_VERSION: TockAttributesKernelVersion = TockAttributesKernelVersion {
145    major: KERNEL_MAJOR_VERSION,
146    minor: KERNEL_MINOR_VERSION,
147    patch: KERNEL_PATCH_VERSION,
148    prerelease: KERNEL_PRERELEASE_VERSION,
149    tlv_type: 0x0103,
150    tlv_len: 8,
151};
152
153pub static TOCK_KERNEL_VERSION: &str = concat!(
154    stringify!(KERNEL_MAJOR_VERSION),
155    ".",
156    stringify!(KERNEL_MINOR_VERSION),
157    ".",
158    stringify!(KERNEL_PATCH_VERSION),
159    "-",
160    stringify!(KERNEL_PRERELEASE_VERSION),
161);
162
163pub mod capabilities;
164pub mod collections;
165pub mod component;
166pub mod debug;
167pub mod deferred_call;
168pub mod dynamic_binary_storage;
169pub mod errorcode;
170pub mod grant;
171pub mod hil;
172pub mod introspection;
173pub mod ipc;
174pub mod platform;
175pub mod process;
176pub mod process_checker;
177pub mod processbuffer;
178pub mod scheduler;
179pub mod storage_permissions;
180pub mod syscall;
181pub mod upcall;
182pub mod utilities;
183
184mod config;
185mod kernel;
186mod memop;
187mod process_array;
188mod process_binary;
189mod process_loading;
190mod process_policies;
191mod process_printer;
192mod process_standard;
193mod syscall_driver;
194
195// Core resources exposed as `kernel::Type`.
196pub use crate::errorcode::ErrorCode;
197pub use crate::kernel::Kernel;
198pub use crate::process::ProcessId;
199pub use crate::scheduler::Scheduler;