code wiki / _hdl_build / nx_nishi_usb_preempt.nx

nx_nishi_usb_preempt.nx

buildroot/runtime/_hdl_build/nx_nishi_usb_preempt.nx

9767 B148 linesdepth 2pulls 2 transitivereach 0 importersview sourcekind tooltopic nishi
docsdependenciesstructsconstsfunctions

about

nx_nishi_usb_preempt.nx -- x86 ladder R-KERN-3: PREEMPTIVE multitasking (timer IRQ0 preempts tasks). The capstone of the kernel-internals thread: "a kernel that runs tasks". nx_kernel_sched proved a COOPERATIVE scheduler (tasks yield). This proves a PREEMPTIVE one: a timer (IRQ0, IF-gated like the real PIC/PIT, delivered between instructions) forces a context switch between two tasks that NEVER yield -- each runs an infinite increment loop, and the timer alone makes both progress, round-robin. It composes onto R-KERN-1's IDT: the timer handler is the IDT[0x20] vector; here the scheduler's context switch + the IF-gated timer delivery are modeled (same level as nx_kernel_sched's emu_resume), the tasks are real x86 the emu executes. The proof that the context switch is REAL (registers preserved across preemption): each task sets its counter pointer rbx ONCE at entry, then loops reading/writing [rbx] without re-setting it. The timer preempts mid-loop repeatedly; both counters keep advancing correctly ONLY if each task's rbx (and rax/rcx) survive every switch -- i.e. a genuine per-task context, not shared state. KAT: (T1) BOTH tasks made real progress (both counters > 0) = preemptive multitasking; (T2) round-robin fairness (the two counters are close, neither starved); (T3) the timer actually drove the switching (>= ~the requested number of ticks occurred). NEG/liar-kill (T4): with the timer MASKED (the kernel's STI is a no-op), task B NEVER runs (counter B == 0) -- proving progress is driven by real IF-gated timer preemption, not by the tasks cooperating. HONEST SCOPE: the context-switch mechanism + the IF-gated periodic timer are modeled (as in nx_kernel_sched / nx_nishios_irq); a real 8259 PIC + 8254 PIT + a full x86 context-switch handler that saves/restores via the stack are the hardware refinements. No /dev, no hardware writes (Rule 26). expect_exit: 0 license_tier: ORIGINAL

dependencies 1 imports · 0 importers

nx_syscalls.nx nx_nishi_usb_preempt.nx

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

main pe_puts sys_write sys_mmap pe_mov pe_b emu_preempt sys_mmap ↻ pe_ld64 pe_st64 pe_ld64 ↻ pe_num sys_mmap ↻ sys_write ↻ sys_exit

structs

none

consts

27const CTR_MAGIC_5000000: i64 = 5000000
28const CTR_MAGIC_65536: i64 = 65536
79const CTR_A: i64 = 0xA000
80const CTR_B: i64 = 0xB000

functions

30func pe_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
called by 1: main calls 1: sys_write
31func pe_num(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;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{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
called by 1: main calls 2: sys_mmapsys_write
32func pe_st64(mem: *u8, a: i64, v: i64) -> i64 { var i: i64=0; while i<8 { mem[a+i]=((v>>(i*8))&0xff) as u8; i=i+1 } return 0 }
called by 1: emu_preempt
33func pe_ld64(mem: *u8, a: i64) -> i64 { var v: i64=0; var i: i64=0; while i<8 { v=v|((mem[a+i] as i64)<<(i*8)); i=i+1 } return v }
called by 2: emu_preemptmain
34func pe_b(c: *u8, o: i64, b: i64) -> i64 { c[o]=(b & 0xff) as u8; return o+1 }
called by 1: main
35func pe_mov(c: *u8, o: i64, modrm: i64, imm: i64) -> i64
called by 1: main
44func emu_preempt(code: *u8, mem: *u8, thresh: i64, maxticks: i64, sti_en: i64, pcA: i64, pcB: i64, out: *i64) -> i64
called by 1: main calls 3: sys_mmappe_ld64pe_st64
82func main() -> i64