tg-kernel-context

Crates.iotg-kernel-context
lib.rstg-kernel-context
version0.1.0-preview.1
created_at2026-01-22 12:47:58.894586+00
updated_at2026-01-22 12:47:58.894586+00
descriptionKernel context management for rCore tutorial OS, including task context switching.
homepagehttps://github.com/rcore-os/rCore-Tutorial-in-single-workspace
repositoryhttps://github.com/rcore-os/rCore-Tutorial-in-single-workspace/tree/tg-preview
max_upload_size
id2061476
size24,960
Yiren Zhang (DeathWish5)

documentation

https://docs.rs/tg-kernel-context

README

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 foreign feature)
  • no_std compatible: Designed for bare-metal kernel environments

Usage

use tg_kernel_context::LocalContext;

// Create a user-mode context with entry point
let ctx = LocalContext::user(entry_point);

// Create a kernel thread context
let ctx = LocalContext::thread(entry_point, true);

// Execute the context (unsafe - modifies CPU state)
unsafe {
    let sstatus = ctx.execute();
}

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.

Commit count: 242

cargo fmt