tg-kernel-context
Kernel context management for the rCore tutorial operating system.
Overview
This crate provides context switching primitives for RISC-V based kernel development. It handles the low-level details of saving and restoring CPU state during context switches between kernel and user space.
Features
- LocalContext: Thread context structure containing all general-purpose registers, program counter, and control flags
- Context switching: Assembly-based efficient context switch implementation
- Foreign address space support: Optional support for context switching across different address spaces (via
foreignfeature) - no_std compatible: Designed for bare-metal kernel environments
Usage
use LocalContext;
// Create a user-mode context with entry point
let ctx = user;
// Create a kernel thread context
let ctx = thread;
// Execute the context (unsafe - modifies CPU state)
unsafe
Features
foreign- Enable support for context switching across different address spaces
Safety
The execute method is unsafe as it directly manipulates critical CSRs (sscratch, sepc, sstatus, stvec) and performs a privilege level switch.
License
Licensed under either of MIT license or Apache License, Version 2.0 at your option.