The HAL (helix-hal) is the only crate that touches hardware directly. Every other crate in Helix accesses hardware through HAL traits. This means adding support for a new architecture (ARM, RISC-V) requires implementing just the HAL traits — zero changes to core, modules, or any subsystem.
The HAL defines four core abstractions: CPU, MMU, Interrupt Controller, and Firmware Interface.
This is the top-level trait that ties all hardware abstractions together. Each architecture provides a single struct that implements HardwareAbstractionLayer with its four associated types.
Helix uses newtype wrappers for physical and virtual addresses. This prevents accidentally passing a physical address where a virtual one is expected — a common source of bugs in kernel code.
The CPU trait exposes processor features, context management, and low-level operations. It's the bridge between Helix's architecture-independent scheduling and the actual hardware register manipulation.
hal/src/cpu.rs
218rust
pubtraitCpuAbstraction:Send+Sync{
2
typeContext:CpuContext;// Saved CPU state (trait, not struct!)
3
typeCpuId:Copy+Eq+Debug;
4
fncurrent_cpu_id(&self)->Self::CpuId;
fncpu_count(&self)->usize;
fnis_bsp(&self)->bool;// Bootstrap processor?
8
unsafefnenable_interrupts(&self);// Safety: system must be ready
The MMU abstraction provides virtual memory mapping, unmapping, and flag management. The PageFlags bitflags type provides type-safe, readable permission control.
hal/src/mmu.rs
11212rust
1
bitflags!{
5 refs
pubstructPageFlags:u64{
3
constPRESENT=1<<0;
4
constWRITABLE=1<<1;
5
constUSER=1<<2;
6
constWRITE_THROUGH=1<<3;
7
constNO_CACHE=1<<4;
8
constACCESSED=1<<5;
9
constDIRTY=1<<6;
10
constHUGE_PAGE=1<<7;// 2MB pages
11
constGLOBAL=1<<8;// Survives TLB flush
12
constNO_EXECUTE=1<<63;// NX bit
13
}
14
}
15
5 refs
implPageFlags{
17
pubconstfnkernel_code()->Self;// PRESENT | GLOBAL
18
pubconstfnkernel_data()->Self;// PRESENT | WRITABLE | NO_EXECUTE | GLOBAL
19
pubconstfnkernel_rodata()->Self;// PRESENT | NO_EXECUTE | GLOBAL
20
pubconstfnuser_code()->Self;// PRESENT | USER
21
pubconstfnuser_data()->Self;// PRESENT | WRITABLE | USER | NO_EXECUTE
22
pubconstfnuser_rodata()->Self;// PRESENT | USER | NO_EXECUTE
The interrupt controller trait abstracts hardware interrupt management — registering handlers, enabling/disabling IRQs, and acknowledging interrupts after handling.
pubpitch:u32,// Bytes per row (may include padding)
20
pubbpp:u8,// Bits per pixel (typically 32)
21
pubformat:PixelFormat,
22
}
23
2 refs
pubenumPixelFormat{Rgb,Bgr,Rgba,Bgra,Unknown}
Index
To port Helix to a new architecture, implement these four traits: CpuAbstraction, MmuAbstraction + PageTable, InterruptController, and FirmwareInterface. Bundle them into a struct implementing HardwareAbstractionLayer, and the entire kernel runs on your new hardware.