menios
menios copied to clipboard
Resume from Hibernation
Description
Implement the boot-time hibernation resume mechanism that detects a saved hibernate state, validates it, and restores the complete system to its pre-hibernation state.
Overview
Resume from hibernation is the counterpart to the hibernate save process. On boot, the kernel must:
- Detect if a valid hibernate image exists
- Validate the image integrity
- Restore all process state and memory
- Resume execution as if nothing happened
Boot Sequence with Hibernation
Normal Boot
- Bootloader loads kernel
- Kernel initializes hardware
- Mount root filesystem
- Launch init process
- Start userland
Hibernation Resume Boot
- Bootloader loads kernel with hibernate=
parameter - Kernel initializes minimal hardware
- Mount filesystem containing hibernate image
- Validate hibernate image
- Restore kernel state
- Restore all processes and memory
- Resume execution (skip init, userland already running)
Implementation
Detection
- Check kernel command line for hibernate parameter
- Look for hibernate image at specified path
- Validate magic number and version
Validation
- Check hibernate image magic number (HBRN)
- Verify kernel version matches
- Validate checksums/hashes
- Ensure image is complete (not truncated)
Restoration Order
- Open hibernate file and SQLite metadata
- Restore kernel heap (buddy allocator state)
- Restore process structures
- Restore memory pages (using issue 256)
- Restore file descriptors
- Restore signal handlers
- Restore CPU registers for each process
- Mark processes as runnable
- Resume scheduler
Critical State
- TSS (Task State Segment)
- GDT (Global Descriptor Table) entries
- Page tables
- Interrupt handlers (should already be set up)
- Timer state (reset, don't restore)
Kernel Command Line
Add hibernate parameter:
API
Check if resuming from hibernation and validate image.
Failure Handling
If resume fails:
- Log error to serial/console
- Delete corrupt hibernate image
- Continue with normal boot
- Prevent infinite resume loops
Tasks
- Add hibernate parameter parsing to kernel command line
- Implement hibernate image detection
- Add image validation (magic, version, checksums)
- Implement kernel state restoration
- Restore buddy allocator arenas and freelists
- Restore process structures
- Call memory restore (issue 256)
- Restore file descriptors
- Restore signal handlers
- Restore CPU context for all processes
- Mark processes runnable and start scheduler
- Add failure handling and fallback to normal boot
- Add resume progress reporting (serial/console)
- Test with various hibernate scenarios
Integration Points
- Bootloader (Limine) command line parsing
- Process management (restore process structures)
- Memory management (restore pages and mappings)
- File system (mount and read hibernate file)
- Scheduler (resume processes)
Acceptance Criteria
- Kernel detects hibernate image on boot
- Image validation catches corruption
- All processes restored correctly
- Memory contents match pre-hibernation state
- Open files resume at correct positions
- Processes continue execution seamlessly
- Failed resume falls back to normal boot cleanly
- No memory leaks or resource leaks
- Works with multiple processes
- Shell and user programs resume successfully
Priority
High - Completes the hibernation feature
Estimated Effort
2-3 weeks
Notes
- This is the most complex piece - restoration order matters
- Must be very careful with error handling
- Consider incremental checkpoints during restore
- Extensive testing needed with edge cases