code wiki / _hdl_build / nx_ctxsw_emit.nx
nx_ctxsw_emit.nx
buildroot/runtime/_hdl_build/nx_ctxsw_emit.nx
about
nx_ctxsw_emit.nx -- CONTEXT SWITCH between two address spaces (satp swap, X-PAGE-CTXSW-001). The
essence of multiprocess virtual memory: two DISTINCT page tables map the SAME virtual address
(0xC0009000) to DIFFERENT physical pages; swapping satp between them makes one VA read each process's
own memory. Needs 4KB-page leaves (X-PAGE-4K-001) since a gigapage can't distinguish PAs within RAM.
Table A @0x80001000 (ppn .1/.2/.3 -> data @0x80004000 sentinel 0x0A0A1111);
Table B @0x80005000 (ppn .5/.6/.7 -> data @0x80008000 sentinel 0x0B0B2222). Both carry the identity
code-map (root[VPN2=2]) so the S-mode code stays fetchable across the swap. Sequence (S-mode):
load VA 0xC0009000 (satp=A) -> sentinel_A ; csrrw satp=B (CONTEXT SWITCH) ; load SAME VA -> sentinel_B
-> verify A!=B and each matches -> "CSW".
satp2_ppn (argv[1]) -- the 2nd satp's root ppn. default 0x80005 = table B (real switch -> CSW). The
gate's control passes 0x80001 = table A (NO switch) -> same VA reads the same
value -> CSW never prints, proving the swap is load-bearing.
out-path (argv[2]).
Sovereign, no gcc/.sh. license_tier: ORIGINAL
dependencies 1 imports · 0 importers
imports: nx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 17 | const CS_MAGIC_8192: i64 = 8192 |
| 19 | const CS_OUT: *u8 = "runtime/_hdl_build/_ctxsw_virt.bin" |
| 20 | const CS_GOLD: *u8 = "runtime/_hdl_build/_ctxsw_virt.bin.gold" |
| 21 | const CS_LOG: *u8 = "knowledge/status/priv.log" |
| 23 | const CS_UART: i64 = 0x10000000 |
| 24 | const CS_FIN: i64 = 0x100000 |
| 25 | const CS_PASS: i64 = 0x5555 |
| 26 | const CS_MEM_BASE: i64 = 0x80000000 |
| 27 | const CS_CSR_SATP: i64 = 0x180 |
| 28 | const CS_CSR_MSTATUS: i64 = 0x300 |
| 29 | const CS_CSR_MEPC: i64 = 0x341 |
| 30 | const CS_MRET: i64 = 0x30200073 |
| 31 | const CS_MPP_S: i64 = 0x800 |
| 33 | const RV_X0: i64 = 0 |
| 34 | const RV_T0: i64 = 5 |
| 35 | const RV_T1: i64 = 6 |
| 36 | const RV_T2: i64 = 7 |
| 37 | const RV_T3: i64 = 28 |
| 38 | const RV_T4: i64 = 29 |
| 39 | const RV_T5: i64 = 30 |
| 41 | const CS_VA: i64 = 0xC0009000 // VPN2=3, VPN1=0, VPN0=9 -> 4KB leaf in each table |
| 42 | const CS_TBLA: i64 = 0x80001000 // table A root (ppn 0x80001) |
| 43 | const CS_TBLB: i64 = 0x80005000 // table B root (ppn 0x80005) |
| 44 | const CS_SENT_A: i64 = 0x0A0A1111 // bit31 clear (lw sign-extends to a positive that li32 matches) |
| 45 | const CS_SENT_B: i64 = 0x0B0B2222 |
| 46 | const CS_SV39: i64 = 8 |
| 47 | const CS_SATP_A_PPN: i64 = 0x80001 |
functions
| 49 | func cs_lui(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x37 } |
| 50 | func cs_addi(rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (rd << 7) | 0x13 } |
| 51 | func cs_load(rd: i64, rs1: i64, f3: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (f3 << 12) | (rd << 7) | 0x03 } called by 1: cs_emit_image |
| 52 | func cs_store(rs2: i64, rs1: i64, f3: i64, imm: i64) -> i64 |
| 57 | func cs_branch(rs1: i64, rs2: i64, f3: i64, imm: i64) -> i64 called by 1: cs_emit_image |
| 64 | func cs_jal(rd: i64, imm: i64) -> i64 called by 1: cs_emit_image |
| 71 | func cs_slli(rd: i64, rs1: i64, shamt: i64) -> i64 { return ((shamt & 0x3f) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x13 } |
| 72 | func cs_srli(rd: i64, rs1: i64, shamt: i64) -> i64 { return ((shamt & 0x3f) << 20) | (rs1 << 15) | (5 << 12) | (rd << 7) | 0x13 } called by 1: cs_li32u |
| 73 | func cs_or(rd: i64, rs1: i64, rs2: i64) -> i64 { return (rs2 << 20) | (rs1 << 15) | (6 << 12) | (rd << 7) | 0x33 } called by 1: cs_set_satp |
| 74 | func cs_csrrw(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xfff) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x73 } |
| 75 | func cs_w32(buf: *u8, off: i64, w: i64) -> i64 { buf[off]=(w&0xff) as u8; buf[off+1]=((w>>8)&0xff) as u8; buf[off+2]=((w>>16)&0xff) as u8; buf[off+3]=((w>>24)&0xff) as u8; return off+4 } |
| 76 | func cs_li32(buf: *u8, off: i64, rd: i64, val: i64) -> i64 |
| 84 | func cs_li32u(buf: *u8, off: i64, rd: i64, val: i64) -> i64 |
| 90 | func cs_emit_str(buf: *u8, off: i64, s: *u8, n: i64) -> i64 |
| 96 | func cs_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 97 | func cs_fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 } |
| 101 | func cs_lay_table(buf: *u8, off: i64, root_base: i64, sentinel: i64) -> i64 |
| 120 | func cs_set_satp(buf: *u8, off: i64, ppn: i64) -> i64 |
| 131 | func cs_emit_image(buf: *u8, cont_off: i64, fail_off: i64, halt_off: i64, pos_out: *i64, satp2_ppn: i64) -> i64 |
| 166 | func cs_parse_num(s: *u8) -> i64 called by 1: main |
| 179 | func main(argc: i64, argv: *i64) -> i64 |