Skip to main content

hermit/
lib.rs

1//! The Hermit kernel.
2//!
3//! This _library operating system_ (libOS) compiles to a static library
4//! (libhermit.a) that applications can link against to create a _Unikernel_.
5//!
6//! The API documented here does not matter to such an application.
7//! Such an application would use it's languages standard library which
8//! internally calls this kernel's system call functions ([`syscalls`]).
9//!
10//! # Using Hermit
11//!
12//! To run a Rust application with Hermit, see [hermit-rs].
13//!
14//! To run a C or C++ application with Hermit, see [hermit-c].
15//!
16//! # Building the kernel manually
17//!
18//! You can build the kernel with default features for x86-64 like this:
19//!
20//! ```sh
21//! cargo xtask build --arch x86_64
22//! ```
23//!
24//! For more information, run:
25//!
26//! ```
27//! cargo xtask build --help
28//! ```
29//!
30//! # Using an initramfs with Hermit
31//!
32//! When the `initramfs` feature is enabled, Hermit supports loading an initramfs,
33//! which is expected to be an uncompressed [tar file in UStar format](https://en.wikipedia.org/wiki/Tar_(computing)#UStar_format).
34//! It is expected to contain a `hermit.toml` file in the archive root,
35//! conforming to the format of [`hermit_entry::config::Config`](https://docs.rs/hermit-entry/latest/hermit_entry/config/enum.Config.html).
36//!
37//! When using [Uhyve], this file should be furthermore compressed using `gzip`,
38//! and passed instead of the kernel.
39//!
40//! The tar file can be created using a command line like:
41//! ```shell
42//! tar -czv -H ustar f some-image.hermit.gz hermit.toml $inputs
43//! ```
44//!
45//! # Features
46//!
47#![cfg_attr(
48	not(feature = "document-features"),
49	doc = "Activate the `document-features` Cargo feature to see feature docs here."
50)]
51#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
52//!
53//! # Compile-time environment variables
54//!
55//! Compile-time environment variables are read from the build environment.
56//!
57//! - **`HERMIT_CAREFUL`** — Compiles the kernel with [`cargo careful`].
58//!
59//! [`cargo careful`]: https://github.com/RalfJung/cargo-careful
60//!
61//! # Run-time environment variables
62//!
63//! Run-time environment variables can be set via `env=KEY=VALUE` kernel args.
64//!
65//! ## Network environment variables
66//!
67//! - **`HERMIT_MTU`** — Sets the *maximum transmission unit* (MTU). Defaults to `1500`.
68//! - **`HERMIT_MRG_RXBUF_SIZE`** — Sets the receive buffer size. Useful for testing receive buffer merging of virtio-net devices when the feature `VIRTIO_NET_F_MRG_RXBUF` is negotiated. Defaults to unset.
69//! - **`HERMIT_PCAP_PATH`** — Sets the packet capture file path. Defaults to `/root/`. See the `write-pcap-file` feature for details.
70//!
71//! IP address, gateway and DNS are configured via the `ip=` command-line parameter for the kernel and do not have a default value:
72//!
73//! - `ip=none` or `ip=off` do not configure a network interface.
74//! - `ip=dhcp` uses DHCPv4 for configuring the network interface.
75//! - `ip=10.0.5.3/24:10.0.5.1::::1.1.1.1:1.0.0.1` would configure the static IP address `10.0.5.3`, set the gateway as `10.0.5.1` and configure two DNS servers.
76//!
77//! ## Output environment variables
78//!
79//! - **`NO_COLOR`** — Prevents the addition of ANSI colors to the kernel output. Defaults to unset. For details, see [`NO_COLOR`].
80//! - **`HERMIT_LOG_LEVEL_FILTER`** — Sets the lowest log level to print. Defaults to `info`.
81//!
82//! [`NO_COLOR`]: https://no-color.org/
83//!
84//! ## Deprecated environment variables
85//!
86//! - **`UHYVE_MOUNT`** — Sets the Uhyve mount point. Defaults to `/root`. Nowadays Uhyve supplies this to the kernel.
87//!
88//! [hermit-rs]: https://github.com/hermit-os/hermit-rs
89//! [hermit-c]: https://github.com/hermit-os/hermit-c
90//! [Uhyve]: https://github.com/hermit-os/uhyve
91
92#![allow(clippy::missing_safety_doc)]
93#![cfg_attr(
94	any(target_arch = "aarch64", target_arch = "riscv64"),
95	allow(incomplete_features)
96)]
97#![cfg_attr(target_arch = "x86_64", feature(abi_x86_interrupt))]
98#![cfg_attr(any(feature = "virtio", feature = "rtl8139"), feature(allocator_ext))]
99#![cfg_attr(docsrs, feature(doc_cfg))]
100#![cfg_attr(not(any(feature = "common-os", feature = "nostd")), feature(linkage))]
101#![cfg_attr(
102	any(target_arch = "aarch64", target_arch = "riscv64"),
103	feature(specialization)
104)]
105#![cfg_attr(
106	not(any(feature = "common-os", feature = "nostd")),
107	feature(thread_local)
108)]
109#![cfg_attr(target_os = "none", no_std)]
110#![cfg_attr(target_os = "none", feature(custom_test_frameworks))]
111#![cfg_attr(all(target_os = "none", test), test_runner(crate::rt::test_runner))]
112#![cfg_attr(
113	all(target_os = "none", test),
114	reexport_test_harness_main = "test_main"
115)]
116#![cfg_attr(all(target_os = "none", test), no_main)]
117// FIXME: move this to `Cargo.toml` once stable
118#![feature(strict_provenance_lints)]
119#![warn(implicit_provenance_casts)]
120
121// EXTERNAL CRATES
122#[macro_use]
123extern crate alloc;
124#[macro_use]
125extern crate bitflags;
126#[macro_use]
127extern crate log;
128#[cfg(not(target_os = "none"))]
129#[macro_use]
130extern crate std;
131
132#[macro_use]
133mod macros;
134
135#[macro_use]
136mod logging;
137
138#[cfg(feature = "acpi")]
139mod acpi;
140pub mod arch;
141#[cfg(all(feature = "common-os", target_arch = "x86_64"))]
142pub mod common_os;
143pub mod config;
144pub mod console;
145mod drivers;
146mod entropy;
147mod env;
148pub mod errno;
149mod executor;
150pub mod fd;
151pub mod fs;
152mod init_buf;
153mod init_cell;
154pub mod io;
155pub mod mm;
156#[cfg(target_os = "none")]
157pub mod rt;
158pub mod scheduler;
159#[cfg(feature = "shell")]
160mod shell;
161mod synch;
162pub mod syscalls;
163pub mod time;
164#[cfg(feature = "uhyve")]
165mod uhyve;
166
167pub(crate) mod built_info {
168	include!(concat!(env!("OUT_DIR"), "/built.rs"));
169}