Skip to main content

os/process/
switch.rs

1//! Context switch assembly — bootstrap only.
2//!
3//! Runtime context switches are handled through TrapFrame in
4//! `process::scheduler_tick`; the old `__switch_context` is no
5//! longer needed.
6//!
7//! ProcessContext layout (repr(C)):
8//!   offset   0..248: x[31] — GPRs x1-x31 (skipping x0)
9//!   offset 248:       sepc: usize
10//!   offset 256:       sstatus: usize
11//!   offset 264:       satp: usize
12
13use core::arch::global_asm;
14
15global_asm!(
16    ".section .text.switch, \"ax\"",
17
18    // ── __start_first_task ───────────────────────────────────────
19    // a0 = *const ProcessContext
20    // Bootstraps the very first task — no save needed.
21    // sscratch must be set by the caller to point to the top of the
22    // kernel stack (see process::start_first_task).
23    ".globl __start_first_task",
24    "__start_first_task:",
25
26    "   mv t6, a0",
27
28    // Restore CSRs
29    "   ld t0, {satp_offset}(t6)",
30    "   csrw satp, t0",
31    "   sfence.vma",
32
33    "   ld t0, {sstatus_offset}(t6)",
34    "   csrw sstatus, t0",
35
36    "   ld t0, {sepc_offset}(t6)",
37    "   csrw sepc, t0",
38
39    // Restore GPRs
40    "   ld x1, 0(t6)",
41    "   ld x2, 8(t6)",
42    "   ld x3, 16(t6)",
43    "   ld x4, 24(t6)",
44    "   ld x5, 32(t6)",
45    "   ld x6, 40(t6)",
46    "   ld x7, 48(t6)",
47    "   ld x8, 56(t6)",
48    "   ld x9, 64(t6)",
49    "   ld x10, 72(t6)",
50    "   ld x11, 80(t6)",
51    "   ld x12, 88(t6)",
52    "   ld x13, 96(t6)",
53    "   ld x14, 104(t6)",
54    "   ld x15, 112(t6)",
55    "   ld x16, 120(t6)",
56    "   ld x17, 128(t6)",
57    "   ld x18, 136(t6)",
58    "   ld x19, 144(t6)",
59    "   ld x20, 152(t6)",
60    "   ld x21, 160(t6)",
61    "   ld x22, 168(t6)",
62    "   ld x23, 176(t6)",
63    "   ld x24, 184(t6)",
64    "   ld x25, 192(t6)",
65    "   ld x26, 200(t6)",
66    "   ld x27, 208(t6)",
67    "   ld x28, 216(t6)",
68    "   ld x29, 224(t6)",
69    "   ld x30, 232(t6)",
70    "   ld x31, 240(t6)",
71
72    "   sret",
73    satp_offset = const super::PROCESS_CONTEXT_SATP_OFFSET,
74    sstatus_offset = const super::PROCESS_CONTEXT_SSTATUS_OFFSET,
75    sepc_offset = const super::PROCESS_CONTEXT_SEPC_OFFSET,
76);
77
78extern "C" {
79    pub fn __start_first_task(ctx: *const super::ProcessContext);
80}