code wiki / _hdl_build / nx_driver_gen.nx

nx_driver_gen.nx

buildroot/runtime/_hdl_build/nx_driver_gen.nx

14099 B202 linesdepth 3pulls 4 transitivereach 0 importersview sourcekind tooltopic driver
docsdependenciesstructsconstsfunctions

about

nx_driver_gen.nx -- GATE: AUTO-GENERATE a virtio-mmio DEVICE DRIVER from a discovered device + VALIDATE it drives the device. Consumes a device discovered by nx_hw_discover (a virtio-mmio device + its DeviceID) and GENERATES the register-protocol driver the device needs -- the piece nx_driver_emit (probe/bind only) left open. The driver is the real virtio-mmio spec sequence: reset -> ACKNOWLEDGE -> DRIVER -> feature-negotiate -> FEATURES_OK -> virtqueue setup -> DRIVER_OK -> submit a request -> device fills the used ring -> completion. The SUBMIT command is derived from the DeviceID (blk=read-sector, console=write, rng=fill-entropy) => a NEW device class = a NEW generated driver, no new hand-code. Validated against a faithful virtio-mmio device model. ★ NEVER-BRICK (cardinal 26): the generated driver's opcode set has ZERO firmware/flash-write ops; the executor REFUSES any firmware-write op. A driver writes device registers -- the firmware-write path does not exist here. T1 generate+run a virtio-blk driver -> the handshake drives the device to DRIVER_OK (status 0xf). T2 the driver submits a request -> device processes the virtqueue -> used ring updated -> completion read. T3 different DeviceIDs generate DIFFERENT drivers (blk/console/rng), each reaching DRIVER_OK (generated, not hard-coded). T4 teeth: a firmware-write op in the driver stream is REFUSED (never-brick) + 0 fw-write ops in a normal driver. T5 FAILED path: a device that rejects FEATURES_OK -> driver aborts cleanly (FAILED, no hang) + determinism. expect_exit: 0 Sovereign: nx_syscalls. Grounded: the virtio-mmio spec (hw_mmio/hw_pcie research).

dependencies 3 imports · 0 importers

nx_syscalls.nx nx_itoa_lib.nx nx_g_puts_lib.nx nx_driver_gen.nx

imports: nx_syscalls.nxnx_itoa_lib.nxnx_g_puts_lib.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main g_puts sys_write sys_mmap dev_new sys_mmap ↻ gen_driver dev_qnum dev_cmd run_driver op_safe dev_mmio_w dev_mmio_r dev_mmio_r ↻ g_pn nxi_out nxi_fd sys_mmap ↻ ccz_cat_num sys_write ↻ sys_munmap g_hex g_puts ↻ sys_mmap ↻ sys_write ↻ ck g_puts ↻ dev_cmd ↻ count_unsafe op_safe ↻ op_safe ↻ run_driver_reject op_safe ↻ dev_mmio_w ↻ dev_mmio_r ↻ sys_openat_append sys_write ↻ sys_close sys_exit

structs

none

consts

29const R_MAGIC: i64 = 0
30const R_VERSION: i64 = 1 // 0x04
31const R_DEVID: i64 = 2 // 0x08
32const R_VENDOR: i64 = 3 // 0x0c
33const R_DEVFEAT: i64 = 4 // 0x10
34const R_DRVFEAT: i64 = 8 // 0x20
35const R_QSEL: i64 = 12 // 0x30
36const R_QNUMMAX: i64 = 13 // 0x34
37const R_QNUM: i64 = 14 // 0x38
38const R_QREADY: i64 = 17 // 0x44
39const R_QNOTIFY: i64 = 20 // 0x50
40const R_STATUS: i64 = 28 // 0x70
41const R_USEDCNT: i64 = 40 // 0xA0 (model: used-ring completion counter)
42const R_LASTCMD: i64 = 41 // model: last command the device processed
43const NREG: i64 = 64
44const VIRTIO_MAGIC: i64 = 0x74726976
46const ST_ACK: i64 = 1
47const ST_DRIVER: i64 = 2
48const ST_DRIVER_OK: i64 = 4
49const ST_FEATURES_OK: i64 = 8
50const ST_FAILED: i64 = 128
52const DEV_NET: i64 = 1
53const DEV_BLK: i64 = 2
54const DEV_CONSOLE: i64 = 3
55const DEV_RNG: i64 = 4
57const OP_RESET: i64 = 1
58const OP_SETSTATUS: i64 = 2 // arg = bit to OR into STATUS
59const OP_NEGFEAT: i64 = 3 // read DEVFEAT -> write DRVFEAT
60const OP_CHECKFEAT: i64 = 4 // verify FEATURES_OK still set (device may clear it to reject)
61const OP_QSETUP: i64 = 5 // QSEL=0, QNUM=arg, QREADY=1
62const OP_SUBMIT: i64 = 6 // arg = device-specific command code; write + QNOTIFY
63const OP_WAITUSED: i64 = 7 // poll the used-ring counter
64const OP_FW_WRITE: i64 = 100 // firmware write -- NOT in the driver set; only to prove refusal

functions

24func g_pn(v: i64) -> i64 { nxi_out(v); return 0 }
called by 1: main calls 1: nxi_out
25func g_hex(v: i64) -> i64 { g_puts("0x" as *u8); let b: *u8=sys_mmap(20); var st: i64=0; var k: i64=0; var i: i64=60; while i>=0 { let nib: i64=(v>>i)&15; if nib!=0 { st=1 } if st==1 { if nib<10 { b[k]=(48+nib) as u8 } else { b[k]=(87+nib) as u8 } k=k+1 } i=i-4 } if k==0 { b[0]=48 as u8; k=1 } sys_write(1,b,k); return 0 }
called by 1: main calls 3: g_putssys_mmapsys_write
26func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c }
called by 1: main calls 1: g_puts
65func op_safe(op: i64) -> i64 { if op>=1 { if op<=7 { return 1 } } return 0 }
67func dev_cmd(devid: i64) -> i64 { if devid==DEV_BLK { return 0x101 } if devid==DEV_CONSOLE { return 0x202 } if devid==DEV_RNG { return 0x404 } return 0x001 }
called by 2: gen_drivermain
68func dev_qnum(devid: i64) -> i64 { if devid==DEV_BLK { return 8 } if devid==DEV_CONSOLE { return 4 } if devid==DEV_RNG { return 2 } return 1 }
called by 1: gen_driver
71func dev_new(devid: i64) -> *i64
called by 1: main calls 1: sys_mmap
80func dev_mmio_w(d: *i64, reg: i64, val: i64, reject_featok: i64) -> i64
93func dev_mmio_r(d: *i64, reg: i64) -> i64 { return d[reg] }
96func gen_driver(devid: i64, op: *i64, arg: *i64) -> i64
called by 1: main calls 2: dev_qnumdev_cmd
112func run_driver(d: *i64, op: *i64, arg: *i64, n: i64) -> i64
called by 1: main calls 3: op_safedev_mmio_wdev_mmio_r
130func run_driver_reject(d: *i64, op: *i64, arg: *i64, n: i64) -> i64
called by 1: main calls 3: op_safedev_mmio_wdev_mmio_r
147func count_unsafe(op: *i64, n: i64) -> i64 { var c: i64=0; var i: i64=0; while i<n { if op_safe(op[i])==0 { c=c+1 } i=i+1 } return c }
called by 1: main calls 1: op_safe
149func main() -> i64