code wiki / _hdl_build / nx_driver_gen.nx
nx_driver_gen.nx
buildroot/runtime/_hdl_build/nx_driver_gen.nx
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
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
structs
| none |
consts
| 29 | const R_MAGIC: i64 = 0 |
| 30 | const R_VERSION: i64 = 1 // 0x04 |
| 31 | const R_DEVID: i64 = 2 // 0x08 |
| 32 | const R_VENDOR: i64 = 3 // 0x0c |
| 33 | const R_DEVFEAT: i64 = 4 // 0x10 |
| 34 | const R_DRVFEAT: i64 = 8 // 0x20 |
| 35 | const R_QSEL: i64 = 12 // 0x30 |
| 36 | const R_QNUMMAX: i64 = 13 // 0x34 |
| 37 | const R_QNUM: i64 = 14 // 0x38 |
| 38 | const R_QREADY: i64 = 17 // 0x44 |
| 39 | const R_QNOTIFY: i64 = 20 // 0x50 |
| 40 | const R_STATUS: i64 = 28 // 0x70 |
| 41 | const R_USEDCNT: i64 = 40 // 0xA0 (model: used-ring completion counter) |
| 42 | const R_LASTCMD: i64 = 41 // model: last command the device processed |
| 43 | const NREG: i64 = 64 |
| 44 | const VIRTIO_MAGIC: i64 = 0x74726976 |
| 46 | const ST_ACK: i64 = 1 |
| 47 | const ST_DRIVER: i64 = 2 |
| 48 | const ST_DRIVER_OK: i64 = 4 |
| 49 | const ST_FEATURES_OK: i64 = 8 |
| 50 | const ST_FAILED: i64 = 128 |
| 52 | const DEV_NET: i64 = 1 |
| 53 | const DEV_BLK: i64 = 2 |
| 54 | const DEV_CONSOLE: i64 = 3 |
| 55 | const DEV_RNG: i64 = 4 |
| 57 | const OP_RESET: i64 = 1 |
| 58 | const OP_SETSTATUS: i64 = 2 // arg = bit to OR into STATUS |
| 59 | const OP_NEGFEAT: i64 = 3 // read DEVFEAT -> write DRVFEAT |
| 60 | const OP_CHECKFEAT: i64 = 4 // verify FEATURES_OK still set (device may clear it to reject) |
| 61 | const OP_QSETUP: i64 = 5 // QSEL=0, QNUM=arg, QREADY=1 |
| 62 | const OP_SUBMIT: i64 = 6 // arg = device-specific command code; write + QNOTIFY |
| 63 | const OP_WAITUSED: i64 = 7 // poll the used-ring counter |
| 64 | const OP_FW_WRITE: i64 = 100 // firmware write -- NOT in the driver set; only to prove refusal |
functions
| 24 | func g_pn(v: i64) -> i64 { nxi_out(v); return 0 } |
| 25 | func 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 } |
| 26 | func 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 } |
| 65 | func op_safe(op: i64) -> i64 { if op>=1 { if op<=7 { return 1 } } return 0 } |
| 67 | func 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 } |
| 68 | func 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 |
| 71 | func dev_new(devid: i64) -> *i64 |
| 80 | func dev_mmio_w(d: *i64, reg: i64, val: i64, reject_featok: i64) -> i64 |
| 93 | func dev_mmio_r(d: *i64, reg: i64) -> i64 { return d[reg] } |
| 96 | func gen_driver(devid: i64, op: *i64, arg: *i64) -> i64 |
| 112 | func run_driver(d: *i64, op: *i64, arg: *i64, n: i64) -> i64 |
| 130 | func run_driver_reject(d: *i64, op: *i64, arg: *i64, n: i64) -> i64 |
| 147 | func 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 } |
| 149 | func main() -> i64 |