rust-kernel
Rustbelt: a hobby x86_64 kernel written in Rust
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | //! The kernel as a library.
//!
//! Everything lives here so that both the bootable binary (src/main.rs) and
//! the integration tests (tests/*.rs, each its own tiny kernel) can use it.
//!
//! Testing: Rust's normal test harness needs `std`, so we use the nightly
//! `custom_test_frameworks` feature. `cargo test` builds each test kernel,
//! boots it in QEMU (see `test-args` in Cargo.toml), results go to serial,
//! and the kernel exits QEMU through the `isa-debug-exit` device.
// when testing the lib itself, it is a kernel too
// nightly: our own test runner
// nightly: `extern "x86-interrupt" fn`
// name the generated test entry fn
// `alloc` = the heap-using half of the standard library (Box, Vec, Rc, ...).
// It works once we register a global allocator (see allocator.rs).
extern crate alloc;
use PanicInfo;
// Stage 22: the per-CPU-family code (src/arch/). Everything else below is
// still PC-only for now, so it is behind `cfg(target_arch = "x86_64")`;
// the arm64 kernel (src/arch/aarch64/main.rs) only uses `arch`.
// ACPI tables: just enough to list the CPUs (MADT)
// heap: maps heap pages + the #[global_allocator]
// local APIC: per-CPU timer, inter-processor interrupts
// stage 21: boot contract v2 -- pid 1 from the root partition
// block devices: BlockDevice trait, virtio-blk, partitions
// /dev: null, random, hvc0, fb0, tty, ptmx, pts/N
// ELF64 loader for user programs
// Linux-compatible error numbers
// text console drawn on the framebuffer
// FPU/SSE/AVX registers: per-thread save/restore (XSAVE or FXSAVE)
// 8x16 bitmap font (Spleen) for fbcon
// futexes: user-space locks' sleep/wake (threads)
// VFS: Inode/FileSystem/File traits, mount table, ramfs
// GDT (kernel + user segments) + TSS (RSP0, double-fault stack)
// IDT, exception handlers, PIC, timer + keyboard
// networking: virtio-net driver + smoltcp TCP/IP, sockets
// paging, frame allocator, page tables, shared-frame counts
// a process's memory map: regions, demand paging, copy-on-write
// PCI bus enumeration (config space via ports 0xCF8/0xCFC)
// poll, select, epoll: waiting for many files at once
// processes: fork, execve, exit, wait4, process groups
// COM1 serial port
// signals: kill, handlers, signal frames, default actions
// multi-core: start the other CPUs, per-CPU data
// kernel stacks with guard pages
// syscall/sysret entry + system call implementations
// stage 17's syscalls for coreutils/busybox: links, owners, statfs, ids, timers...
// cooperative multitasking: async tasks + executor
// TrapFrame: saved user registers, the common return to ring 3
// nanosecond clock (TSC calibrated against the PIT)
// alarm, setitimer, POSIX timers: signals at a deadline (stage 17)
// threads + the multi-core scheduler (sleep/wake, wait queues)
// terminals: line discipline, termios, console/hvc0/ptys
// stage 19: the vDSO + vvar page, time without a syscall
// the console: VGA text mode, or fbcon on a GPU
// modern virtio transport + virtio-rng, -console, -gpu
/// Set up everything interrupt-related. Order matters:
/// GDT/TSS first (the IDT refers to the IST stack), then the IDT, then the
/// PICs, and only then enable interrupts (`sti`).
/// Paging + heap + the global frame allocator, in one call (what the test
/// kernels need; src/main.rs does the same steps one by one with demos).
/// Multi-core bring-up: the local APIC and its timer (preemption), CPU 0's
/// idle thread, and every other CPU the ACPI tables list (see smp.rs).
/// Needs `init` and `init_memory`. Returns the number of running CPUs.
/// Storage bring-up: scan the PCI bus, start the disk drivers, read the
/// partition tables and mount FAT volumes under /mnt. Needs the heap, the
/// frame allocator and `fs::init()`. Returns (PCI devices, log lines).
/// The stage-13 virtual devices: virtio-rng, virtio-console (hvc0) and
/// virtio-gpu. With a GPU the text console moves onto its framebuffer.
/// Call before `init_net` (smoltcp's random seed comes from the rng).
/// Returns boot log lines.
/// Networking: loopback, plus the virtio-net card if there is one (smoltcp,
/// DHCP and the netd thread). Needs the scheduler (call after `init_smp`).
/// Returns the boot log lines.
/// Print a line to *both* the VGA screen and the serial port.
/// Sleep the CPU until the next interrupt, forever (cheaper than `loop {}`,
/// which would keep a host CPU core at 100%).
!
/// Exit codes for QEMU's `isa-debug-exit` device. QEMU exits with
/// status `(value << 1) | 1`, so Success = 0x10 -> shell exit code 33.
/// Write to I/O port 0xf4 (where we put `isa-debug-exit`) to quit QEMU.
// ---------------------------------------------------------------------------
// Test framework
// ---------------------------------------------------------------------------
/// Anything callable as a test: prints its name, runs it, prints [ok].
/// (If it panics, the panic handler prints [failed] instead.)
/// Called with every `#[test_case]` function in the crate being tested.
/// Panic handler for test kernels: report the failure and exit QEMU.
!
// When running `cargo test --lib`, the library itself is booted as a kernel:
entry_point!;
!
!
|