Repository navigation
Developer_Onboarding
This guide provides comprehensive information for new developers working on the rust-kernel project. It covers everything from initial setup to advanced development practices.
- Getting Started
- Project Overview
- Development Workflow
- Code Structure
- Key Concepts
- Testing
- Debugging
- Common Tasks
- Best Practices
- Troubleshooting
Before you begin, ensure you have the following installed:
- Docker - Required for cross-compilation
- QEMU - For testing the kernel
- Rust - Latest stable version
- NASM - For assembly code compilation
- GNU Make - For build orchestration
-
Clone the repository
git clone https://github.com/your-org/rust-kernel.git cd rust-kernel -
Build the Docker environment
cd buildenv docker build -t rust-kernel-env .
-
Start a development container
cd buildenv docker run --rm -it -v $(pwd)/..:/root/env rust-kernel-env bash
-
Build the kernel
cd /root/env make build -
Run the kernel in QEMU
qemu-system-x86_64 -cdrom dist/x86_64/kernel.iso -serial stdio
This is a bare-metal x86_64 operating system kernel written in Rust. It's designed as an educational project to demonstrate modern kernel development practices while maintaining clarity over optimization.
- Rust-based - Leverages Rust's safety features where possible
- No_std environment - No standard library, minimal dependencies
- Linux-inspired architecture - Follows Linux design patterns
- Modular design - Clear separation of concerns
- Comprehensive testing - Built-in test framework
- Virtual File System - Linux-like VFS layer
- Task management - Process and thread management
- System calls - Basic syscall interface
Phase 2.4 Complete:
- Context switching
- Scheduler (round-robin)
- Timer-based preemption
- TSS (Task State Segment)
- System calls (13 implemented)
- VFS inode interface
- Initramfs and Tmpfs filesystems
- Task structures with state management
- Linux-style ID allocator
Next Priorities:
- Fix VMM page fault handling
- Replace bump allocator with slab allocator
- Complete syscall implementation
- Add process isolation
All build commands should be run from the project root:
# Build the kernel
make build
# Build and run tests in QEMU
make run-tests
# Build ISO only
make iso
# Clean build artifacts
make clean- Make changes - Edit Rust source files
-
Build - Run
make buildto compile -
Test - Run
make run-testsfor comprehensive testing - Debug - Use QEMU serial output for debugging
- Iterate - Fix issues and repeat
- Create the module in the appropriate directory
- Add it to the
src/implementation/kernel/main.rsimports - Implement initialization in the init sequence
- Add tests in the corresponding test file
- Understand the module's responsibility
- Check for existing error handling patterns
- Add appropriate tests
- Verify build and test pass
rust-kernel/
├── src/implementation/
│ ├── kernel/ # Main kernel modules
│ │ ├── main.rs # Entry point and initialization
│ │ ├── error.rs # Unified error handling
│ │ ├── kernel.rs # Global kernel state
│ │ ├── panic.rs # Panic handler
│ │ ├── arch/ # Architecture-specific code
│ │ ├── memory/ # Memory management
│ │ ├── task/ # Process/thread management
│ │ ├── vfs/ # Virtual file system
│ │ ├── drivers/ # Hardware drivers
│ │ └── tests/ # Kernel tests
│ └── x86_64/boot/ # Assembly boot code
└── targets/x86_64/ # Target configuration
- main.rs - Entry point, module initialization
-
error.rs - Unified error handling with
KernelError - kernel.rs - Global kernel state management
- panic.rs - Custom panic handler
- x86/interrupts.rs - IDT, PIC, interrupt handlers
- x86/tss.rs - Task State Segment implementation
- init.rs - Architecture initialization
- pmm.rs - Physical Memory Manager
- vmm.rs - Virtual Memory Manager
- allocator.rs - Heap allocator
- init.rs - Memory subsystem initialization
- task.rs - Task structure and state
- scheduler.rs - Round-robin scheduler
- id_allocator.rs - PID allocation
- switch.rs - Context switching
- inode.rs - VFS inode interface
- file.rs - File abstraction
- mount.rs - Mount namespace
- initramfs.rs - Embedded filesystem
- tmpfs.rs - In-memory filesystem
- vga.rs - VGA text mode driver
- serial.rs - Serial port driver
- init.rs - Driver initialization
This kernel runs without the Rust standard library. Key implications:
- No heap allocation until memory manager is initialized
- No standard I/O - use custom drivers (VGA, serial)
- No panic recovery - implement custom panic handler
- Limited collections - only core collections available
All modules use unified error handling:
use crate::error::{KernelError, KernelResult};
fn some_operation() -> KernelResult<T> {
// Instead of custom errors:
// return Err(MyCustomError::Something);
// Use KernelError:
Err(KernelError::NotFound)
}Rust's safety features are used where possible, but unsafe blocks are necessary for:
- Hardware access (memory-mapped I/O)
- Context switching assembly
- Raw pointer manipulation
- Inline assembly
Every unsafe block must be documented with // SAFETY: comments explaining the invariants.
The kernel uses spin locks for synchronization:
use spin::Mutex;
static GLOBAL_DATA: Mutex<GlobalData> = Mutex::new(GlobalData::default());The kernel has a built-in test framework that runs in QEMU:
# Run all tests
make run-tests
# Tests output to serial port
# Look for "TESTS PASSED" or "TESTS FAILED" messagesTests are organized by subsystem:
- test_allocator.rs - Heap allocator tests
- test_pmm.rs - Physical memory tests
- test_vmm.rs - Virtual memory tests
- test_task.rs - Task management tests
- test_vfs.rs - VFS tests
- test_tss.rs - TSS tests
- Create test functions in the appropriate test file
- Use
#[test_case]or standard test attributes - Tests run in kernel context with serial output
- Test failures cause kernel halt with error message
All debugging information goes to the serial port:
use crate::drivers::serial::write_string;
write_string("Debug message\n");Run QEMU with serial output for debugging:
qemu-system-x86_64 -cdrom dist/x86_64/kernel.iso -serial stdio-
Serial logging - Use
write_string()for debug messages -
VGA output - Use
vga::println!()for visual debugging - Panic handler - Custom panic provides file/line info
- Breakpoints - Use QEMU's debugging features if needed
- Add syscall number to
syscall/numbers.rs - Implement handler in
syscall/handler.rs - Add to syscall dispatch table
- Write tests in
tests/test_syscall.rs
- Create module in
vfs/directory - Implement
Inodetrait - Add to VFS mount system
- Write tests
- Create driver in
drivers/directory - Implement initialization in
drivers/init.rs - Add to driver initialization sequence
- Write tests
-
Naming:
snake_casefor functions,PascalCasefor types -
Error handling: Always use
Result<T, KernelError>for high level kernel calls. (= is called in/nearmain.rs)
- Install GNU Make
- On Ubuntu:
sudo apt-get install make
- Install Docker Desktop or Docker Engine
- Ensure Docker daemon is running
- Install QEMU
- On Ubuntu:
sudo apt-get install qemu-system-x86
- Check serial output for error message
- Verify memory initialization
- Check for null pointer dereferences
- Tests may be causing memory corruption
- Check for memory leaks in test code
- Consider running tests in isolated contexts
- Check serial output - Most errors print to serial
- Review error logs - Look for specific error messages
- Check documentation - This guide and code comments
- Ask questions - Reach out to me
Now that you're familiar with the project structure and development workflow, you can:
- Start with small changes - Fix a bug or add a simple feature
- Read the code - Understand how existing modules work
- Run the tests - Verify everything works as expected
- Contribute - Submit a pull request with your changes