diff --git a/.crumb b/.crumb index dd44b4f..6805525 100644 --- a/.crumb +++ b/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-28T21:31:30Z", + "compiled_at": "2026-11-05T22:59:48Z", "compiler": "crumb-compile/1", - "digest": "sha256:3744d81e46b7555795e7e293dcb4ad6a879aaee58a7b67b2d703f0bd1b7fc658", + "digest": "sha256:ecc0821a81d578d3a8db91d4523238a024836548718094a4c12865976f945ce1", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 14, - "generated_at_commit": "bf98036d4d46d45b4ecb7d6157333bbc7ab74793", - "source_tree": "sha256:5afbb60c360650fc27ad12f5fe59b4c68499b9785c4e488a1ca7f8e2d6ab8620" + "files": 67, + "generated_at_commit": "0f0a2a576556c1bf8cd63c090040141fbd372a39", + "source_tree": "sha256:eb2c0635d43b9570b04a70a14e1c0433505a5da030d9fb8665794661388ee5eb" } }, "layer": "driver", diff --git a/DESIGN.md b/DESIGN.md new file mode 100644 index 0000000..8fc8003 --- /dev/null +++ b/DESIGN.md @@ -0,0 +1,118 @@ +# omega-gpu-driver design notes + +Status: **PROPOSED.** None of this is implemented yet. Nothing here has run on the GB10. + +The bring-up design (GSP boot chain, RPC surface, the `ogd` launcher and manifest, the D2 window) is in the +lane's design document and is summarised in `README.md` and `window/README.md`. This file holds the design +decisions for the driver's isolation model and its userspace interface. + +Source: an accel-driver survey of 2026-10-27 (lessons 1-3). It read the two Linux accel NPU drivers at +torvalds/linux `d135ab453a740`. All `drivers/accel/...` and `include/uapi/...` cites below are file:line in +that tree. `NV:` cites are NVIDIA open-gpu-kernel-modules 580.173.02, `src/common/sdk/nvidia/inc/class/`. + +## 1. Isolation: one private GPU address space per client, no pushbuffer parser + +**The two Linux models.** The two drivers protect the device in different ways: + +- **ethosu has no MMU**, so the kernel checks every command stream. + - It copies the stream into a buffer object marked no-mmap (`ethosu_drv.c:130`, enforced at + `ethosu_gem.c:26-27`), so userspace cannot change it after the check. + - It replays the stream in an interpreter (`ethosu_gem_cmdstream_copy_and_validate`, `ethosu_gem.c:385-707`). + - Submit refuses a region that does not fit its buffer (`ethosu_job.c:398-496`). +- **rocket has an MMU** and never parses commands. + - `rocket_open` gives each open file its own IOMMU paging domain (`rocket_drv.c:72,89`; created at 38-56). + - Buffers are mapped into that domain (`rocket_gem.c:96`). + - The domain is attached to the core for each job and detached afterwards (`rocket_job.c:348`, 382, 406). + +**What we adopt.** The GB10 translates GPU virtual addresses through its GMMU, so OwnDriver follows rocket: + +- Every client gets its own GPU VA space (FERMI_VASPACE_A). That client's page tables come from local GMMU + code (`chan/gmmu.om`) and are installed with SET_PAGE_DIRECTORY. +- A client's buffers are mapped only into its own VA space. +- The driver does **not** parse or check pushbuffers or GPFIFO contents. Its job is address-space setup, + buffer bounds, fences and teardown. + +**Paths the GMMU may not translate.** On these paths ethosu-style copy-then-check still applies. The paths +come from the docs and none has been checked on the chip (UNVERIFIED): + +1. **Copy-engine physical mode.** Ampere's copy class has `LAUNCH_DMA_SRC_TYPE_PHYSICAL` and + `DST_TYPE_PHYSICAL` (`NV:clc7b5.h:121-126`). The published Blackwell copy headers (`NV:clc9b5.h`, + `NV:clcab5.h`) list too few methods to say whether the mode still exists. +2. **Structures the GPU reads by bus address.** + - These are the instance block, RAMFC, USERD and the GPFIFO ring, whose IOVAs travel in the channel alloc + RPC (lane design §3). + - ogd allocates and writes them itself; a client never supplies their contents. That is the copy-then-check + rule applied at allocation. +3. **The SMMU.** It translates every device access (`iommu.passthrough=0`; the GPU is alone in IOMMU group 20). + But one VFIO container gives the whole device a single IOMMU domain. A physical-mode access is therefore + bounded by the union of all clients' DMA mappings, not by one client's mappings [INFERENCE]. Until (1) is + settled, either refuse physical-mode copy methods, or run only one client at a time. + +**Scope in v1.** In v1 the workload inherits the VFIO device fd (lane design §4), so it has BAR0 read/write +access. v1 therefore has one trusted workload per `ogd` session and claims no isolation between clients. The +per-client VA space model starts to protect something only once `ogd` serves more than one client and the +workload no longer holds the VFIO fd. That is v2: a request pipe, plus a mapping of only the doorbell and +USERD pages. + +## 2. Submit ABI + +The rules come from ethosu's job path and rocket's uapi. Each one is required from the first version of the ABI: + +- **Per-buffer read/write flags; the write fence goes only on outputs.** + - ethosu takes an implicit dependency for every buffer, marked as a write only for output regions + (`ethosu_job.c:91-99`). + - It attaches the done fence with `DMA_RESV_USAGE_WRITE` only to outputs (`ethosu_job.c:102-114`). + - It locks all buffers as one group (`drm_gem_lock_reservations`, `ethosu_job.c:122-142`). + - Each submit buffer entry in our ABI carries `READ`, `WRITE` or both. A read-only input never waits on + another reader. +- **A watchdog that checks progress before resetting.** + - ethosu runs one job in flight (`credit_limit = 1`, `ethosu_job.c:327`). + - Its timeout handler reads the queue read pointer (`NPU_REG_QREAD`, `ethosu_job.c:280`). If the pointer + still moves, it lets the job run on (`ethosu_job.c:287-292`) and resets only after that (`ethosu_job.c:305`). + - Our counterpart is GPGet in USERD and the semaphore sequence. The watchdog reads one of them before it + tears a channel down. A timeout with that counter still advancing is not a hang. +- **Reserved, pad and flags fields must be zero.** Refuse the call otherwise: ethosu `ethosu_drv.c:125`; + rocket `rocket_gem.c:143`, `rocket_gem.c:174` and `rocket_job.c:643`. An unknown flag bit is refused, never + ignored. +- **Self-sized structs.** + - rocket's submit carries `job_struct_size` and `task_struct_size` (`rocket_accel.h:142-145`, 177-180). + - The kernel refuses sizes smaller than the struct it knows (`rocket_job.c:77`, 638-639) and steps through + the arrays by the size the caller declared (`rocket_job.c:95`, 656). + - Our records start with a byte size and a version. The ABI can then grow at the tail without breaking old + callers. + +## 3. Ordering and cache sync: fences, not cache-maintenance calls + +rocket brackets CPU access with explicit calls: + +- `PREP_BO` waits for the write fence with a timeout, then calls `dma_sync_sgtable_for_cpu` + (`rocket_gem.c:152-161`). +- `FINI_BO` calls `dma_sync_sgtable_for_device` (`rocket_gem.c:186`). + +rocket needs these because the RK3588 NPU's DMA is not cache-coherent. That is the reason given in review; it +was not checked against RK3588 documentation here. ethosu syncs implicitly when a buffer is waited on or mapped. + +The GB10 is a self-hosted part with a coherent C2C link. NVIDIA's RM treats it as such: + +- Local EGM is set up only when `gpuIsSelfHosted(pGpu)` and `PDB_PROP_KBIF_IS_C2C_LINK_UP` hold + (`NV2:mem_mgr.c:4022-4023`, with the comment "EGM can be enabled only in C2C path"). +- When `kbusIsBar1Disabled` (`NV2:mem_mgr_gm107.c:619`), BAR1 is reported as size 0, with the comment "When + coherent C2C path is enabled, BAR1 is disabled" (`NV2:mem_mgr_gm107.c:688-694`). + +`NV2:` is `src/nvidia/src/kernel/gpu/mem_mgr/` (with `arch/maxwell/` for the gm107 file) in the same +580.173.02 tree. Neither function has been run on our chip. The lines say how RM treats a self-hosted C2C +GPU, not that every GB10 access path is coherent. + +What this design takes from that: + +1. **The ordering model on GB10 is fences plus memory ordering.** There are no CPU/GPU cache-maintenance + ioctls. + - The CPU reads an output only after that job's write fence (§2) has signalled. + - The submit path keeps its barriers: ring write, `dsb sy`, GPPut, `dsb sy`, doorbell. + - Completion is a semaphore release that the CPU reads after the fence. + - `prep`/`fini` remain only as an ABI slot for non-coherent targets. On GB10 they check their arguments + (§2 rules) and do nothing else. +2. **No BAR1 VRAM mapping.** BAR1 is off on this chip: RM reports it as size 0 (above), and sysfs shows BAR0 + only (lane design §1). + - All buffers are sysmem: pinned pages, VFIO MAP_DMA, and GMMU PTEs with the snooped sysmem aperture. + - The CPU's view of a buffer is the CPU's own mapping of those pages. diff --git a/README.md b/README.md index 43af3f7..50ad413 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,17 @@ # omega-gpu-driver -The GB10 GPU driver written in seeded Omega. Status: **W0 IMPLEMENTED and EXERCISED in QEMU only. -Not run on GB10 hardware; nothing here is QUALIFIED.** +The GB10 GPU driver written in seeded Omega. Status: **W0 IMPLEMENTED and EXERCISED in QEMU. The driver +units below are IMPLEMENTED and EXERCISED on the host against C referees and simulators only. Nothing has +run on GB10 hardware; nothing here is QUALIFIED. GSP boot (`gb10_gsp_boot.c`) is not ported.** Parent: [aien-dev/omega-mv](https://github.com/aien-dev/omega-mv), the seeded Omega compiler. This repo pins one omega-mv commit and the sha256 of the `omc` it births (`tools/omc.pin`). The pin is currently -the draft branch `feat/vfio-device-calls` (commit `7ae2aef9b`), which adds the `vfioctl` and `vfiomap` -words this driver needs; it moves to omega-mv main after that PR is reviewed and merged. +the draft branch `feat/vfio-dma-bar` (omega-mv#890, commit `397e6704`, on #878 `feat/vfio-device-calls`). +It adds the `vfioctl` and `vfiomap` words plus IOMMU_MAP_DMA/UNMAP_DMA and the writable `vfiomapw` this +driver needs. The pin moves to omega-mv main after those PRs are reviewed and merged. + +`DESIGN.md` (PROPOSED, no code yet) records the isolation model, the submit ABI and the ordering model: +one GPU VA space per client with no pushbuffer parser, per-buffer read/write fences, and no BAR1. ## W0: bind, read identity, release @@ -48,7 +53,9 @@ traps. See omega-mv `seed/host/MAPPING.md`. ## Build and test - sh build.sh # births the pinned omc from seed-0, checks its sha256, writes build/w0 + sh build.sh # births the pinned omc from seed-0, checks its sha256, writes build/w0, build/fwprep + OGKM= sh build.sh + # also runs build/fwprep on the installed gsp_ga10x.bin into build/fw (fw.manifest) KERNEL=img/boot/vmlinuz-7.0.0-1019-nvidia sh qemu/test.sh # QEMU virt + SMMUv3, edu device on vfio-pci, 9 commands, all results checked KERNEL=... DEVICES='-device pcie-root-port,id=rp1,chassis=1,slot=1 -device edu,bus=rp1' \ @@ -66,16 +73,37 @@ Every QEMU boot and test command runs under `nice -n 19 taskset -c "${AIEN_TEST_ cores 10-14, which the shared Spark reserves for its live service; set it to use other cores, e.g. `AIEN_TEST_CPUS=0-3 KERNEL=... sh qemu/test.sh`. -Host tools in the build and test path (disclosed; not part of the driver): `sh`, `git`, `sha256sum`, -QEMU 8.2.2, busybox, zstd, cpio, gzip. The driver itself is `w0.om` only. +## Driver units (host-exercised ports of aienux PR #441 C) + +Each unit is a `.om` port of one aienux reference file; each gate compares the `.om` output with the C +reference byte for byte, and each mutant script changes one line at a time and requires the gate to fail. + +| Unit | Ported from | Gate | Mutants | +|---|---|---|---| +| `gsp/mmio.om`, `gsp/sim.om` (register-level GSP start-up via SEC2, runtime-PM gate, SEC2+GSP falcon simulator) | `gpu/gsp_mmio.c`, `gpu/gsp_sim.c` | `tests/mmio_diff.sh`, `tests/pm_gate.sh` | `tests/mutants-mmio.sh` | +| `gsp/sysinfo.om` (GSP_SET_SYSTEM_INFO payload) | `tools/ck_gsp_sysinfo_encode.c` | `tests/sysinfo_diff.sh` | `tests/mutants-sysinfo.sh` | +| `gsp/seq.om` (GSP_RUN_CPU_SEQUENCER, CPU side) | `gpu/gsp_seq.c` | `tests/seq_diff.sh` | `tests/mutants-seq.sh` | +| `gsp/sha2.om`, `gsp/inflate.om`, `gsp/fw.om`, `gsp/bindata.om`, `fwprep.om` (firmware inputs) | `ck_gsp_fw_inspect.c`, `ck_gsp_bindata_extract.c`, `argus/sha256.c` | `tests/fw_diff.sh` | `tests/mutants-fw.sh` | +| `chan/gmmu.om`, `chan/mem.om`, `chan/chan.om`, `chan/sim.om` (page tables, buffers, channels; simulator mode only) | `gpu/gmmu.c`, `gpu_mem.c`, `gpu_chan.c`, `gpu_sim.c` | `tests/chan_diff.sh` | `tests/mutants-chan.sh` | +| `chan/params.om` (RM alloc/control records for GSP-RM, NVIDIA 580.173.02 layouts) | NVIDIA headers | `tests/params_diff.sh` | `tests/mutants-chan.sh` | + +`fwprep FWFILE OGKMSRC OUTDIR` reads the signed `gsp_ga10x.bin` (unchanged) and the NVIDIA open-source +bindata tables, and writes `fmc.img`, `fmc.hash`, `fmc.pubkey`, `fmc.sig`, `bl.img` and, last, +`fw.manifest`. Channel ops other than the simulator (mode 0) trap; on the GB10 they go through GSP RPC. -## On the GB10 (operator-run, authorized window only) +Test-only referees and harnesses (disclosed; never linked into the driver): `tests/seq_ref.c` and +`tests/params_ref.c` (C), the aienux C tools built fresh by the diff scripts, and the shell scripts in +`tests/`. Host tools in the build and test path: `sh`, `git`, `sha256sum`, `cc`, QEMU 8.2.2, busybox, +zstd, cpio, gzip. -Main binds the GPU to `vfio-pci` (000f:01:00.0, IOMMU group 20, alone in its group) and runs: +## On the GB10 (authorized exclusive window only) - build/w0 000f:01:00.0 20 +`window/README.md` is the procedure: W0 read-only checks, W1 reboot with nvidia.ko held back, W2 bind to +`vfio-pci`, W3 `build/w0 000f:01:00.0 20`, W7 release, then R restores nvidia.ko and the resident 35B. +`tests/window_sim.sh` runs those scripts unchanged against a simulated machine, and +`tests/mutants-window.sh` checks that it catches each removed safety step. None of it has run on the GB10. -Expected: config vendor/device 0x2e1210de (lspci reports [10de:2e12]) and matching map and pread +Expected from W3: config vendor/device 0x2e1210de (lspci reports [10de:2e12]) and matching map and pread values. The BOOT_0 architecture field (bits 28:24) is defined at nv_ref.h:129-136 (GB100 0x1A, GB200 0x1B); which value the GB10 reports is what this run measures, not something this repo asserts. diff --git a/build.sh b/build.sh index d332ae6..ced002e 100644 --- a/build.sh +++ b/build.sh @@ -1,12 +1,33 @@ #!/bin/sh -# build.sh -- birth the pinned omc (tools/getomc.sh) and compile w0.om into build/w0. -# A failed compile leaves no build/w0 behind. +# build.sh -- birth the pinned omc (tools/getomc.sh) and compile w0.om into build/w0 and the firmware +# input tool (gsp/sha2.om gsp/inflate.om gsp/fw.om gsp/bindata.om fwprep.om) into build/fwprep. +# With OGKM=, it then runs build/fwprep on the installed +# GSP firmware into build/fw (checked by window/d2-pre.sh against build/fw/fw.manifest). +# A failed step leaves no output behind. set -eu R=$(CDPATH= cd -- "$(dirname "$0")" && pwd) cd "$R" sh tools/getomc.sh -rm -f build/w0 build/w0.tmp -if ! build/omc < w0.om > build/w0.tmp; then rm -f build/w0.tmp; exit 1; fi -chmod +x build/w0.tmp -mv build/w0.tmp build/w0 -echo build/w0 +# compile OUT SRC... : concatenate the units and compile them into build/OUT +compile() { + out=build/$1; shift + rm -f "$out" "$out.tmp" + if ! cat "$@" | build/omc > "$out.tmp"; then rm -f "$out.tmp"; exit 1; fi + chmod +x "$out.tmp" + mv "$out.tmp" "$out" + echo "$out" +} +compile w0 w0.om +compile fwprep gsp/sha2.om gsp/inflate.om gsp/fw.om gsp/bindata.om fwprep.om +FW=${FW:-/lib/firmware/nvidia/580.173.02/gsp_ga10x.bin} +if [ -n "${OGKM:-}" ]; then + rm -rf build/fw build/fw.tmp + mkdir build/fw.tmp + if ! build/fwprep "$FW" "$OGKM" build/fw.tmp > build/fwprep.log; then + rm -rf build/fw.tmp; cat build/fwprep.log >&2; exit 1 + fi + mv build/fw.tmp build/fw + echo build/fw +else + echo "build/fw not built: set OGKM to an open-gpu-kernel-modules 580.173.02 checkout" >&2 +fi diff --git a/chan/chan.om b/chan/chan.om new file mode 100644 index 0000000..01fc04a --- /dev/null +++ b/chan/chan.om @@ -0,0 +1,292 @@ +note chan/chan.om: GPFIFO compute channel (ring, USERD, instance block, doorbell), ported from aienux +note PR #441 native/kernel/gpu/gpu_chan.c and gpu_chan.h (branch pr441 @ c5bb3a4b). Library unit: no main. +note Prefix ch / CH. Needs chan/gmmu.om and chan/mem.om before it, and chan/sim.om (mode 0 ops). +note +note A channel is a slot 0..3 (CHSLOTS); its buffers are chan/mem.om slots. The CPU writes the ring, +note USERD GPPut and the instance block through mmwin; the doorbell goes through mmregwr. +note +note Channel ops (C: a function-pointer table): chbindop / chdisop / chidlop. mmmode 0 calls the +note simulator (chan/sim.om gsbind / gsdisable / gsidle). On GB10 the channel is allocated, scheduled +note and torn down by GSP-RM (RM_ALLOC BLACKWELL_CHANNEL_GPFIFO_A, encoded by chan/params.om), not by +note register writes, so any other mode traps 9 here: the hardware ops are the GSP RPC path. +note +note API (state in globals; a global is never an argument): +note chinit -> void all slots closed and empty +note chcreate c chid rl ent -> long ck_gpu_chan_create (chid, rl, ent uint) +note chsubmit c pb off n -> long ck_gpu_chan_submit (pb a mem slot, off and n uint) +note chretire c upto -> void ck_gpu_chan_retire (upto uint) +note chteardown c budget nx -> long ck_gpu_chan_teardown; extras: the first nx slots of chx +note (GMNONE entries are skipped, as C null pointers) +note chtoken rl chid -> uint ck_gpu_submit_token +note chrluserd pa -> long ck_gpu_rl_userd, the two words in chrlo +note chgpent va n -> ulong ck_gpu_gp_entry; chmthd: ck_gpu_mthd +note per slot: chchid chrl chtok chent chpdb chgpva chupa chipa chrl0 chrl1 (descriptor), chgp chud +note chin (ring, USERD, instance mem slots), chput chret (uint, wrap as uint32), chopen +note codes (enum ck_gpu_chan_status): 0 CHOK, 1 CHEINVAL, 2 CHENOMEM, 3 CHESTATE, 4 CHEFULL, 5 CHEBIND, +note 6 CHETIME +const CHOK long 0 +const CHEINVAL long 1 +const CHENOMEM long 2 +const CHESTATE long 3 +const CHEFULL long 4 +const CHEBIND long 5 +const CHETIME long 6 +const CHSLOTS ulong 4 +note CHID_MAX 0xFFF, RUNLIST_INVALID 0x7F, GP length max 0x1FFFFF, GP VA limit 1<<40 +const CHIDMAX uint 4095 +const CHRLINV uint 127 +const CHLENMX uint 2097151 +const CHVALIM ulong 1099511627776 +note USERD 512 bytes, GPPut at 0x8C; instance block 4096 bytes, page directory at word 128 (byte 512) +const CHUDSZ ulong 512 +const CHGPPUT ulong 140 +const CHINSZ ulong 4096 +const CHPDBW ulong 512 +note RAMIN PDB: target sysmem coherent 2, BIG_PAGE_SIZE_64K bit 11 (2048); low address mask 0xFFFFF000 +const CHTSYS uint 2 +const CHBIG64 uint 2048 +const CHLOM uint 4294963200 +note doorbell VF base 0xB80000 + 0x30090 = 0xBB0090 +const CHDBELL ulong 12255376 +note GP entry: va & ~3 (mask 2^64-4), LENGTH at bit 42, LEVEL bit 41 set as physics nvrm_gp_entry does +const CHVAM ulong 18446744073709551612 +const CHGPB41 ulong 2199023255552 +note method header (nvrm_mthd, INC_METHOD): 2 << 28 +const CHNINC uint 536870912 +global chchid uint 4 +global chrl uint 4 +global chtok uint 4 +global chent uint 4 +global chpdb ulong 4 +global chgpva ulong 4 +global chupa ulong 4 +global chipa ulong 4 +global chrl0 uint 4 +global chrl1 uint 4 +global chgp ulong 4 +global chud ulong 4 +global chin ulong 4 +global chput uint 4 +global chret uint 4 +global chopen uchar 4 +global chrlo uint 2 +global chx ulong 8 +fn chtoken rl uint chid uint to uint +ret or shl and rl CHRLINV 16 and chid CHIDMAX +end +fn chrluserd pa ulong to long +if or ne and pa 255 0 ge pa MMPALIM +ret CHEINVAL +end +put chrlo 0 and uint and pa 4294967295 4294967040 +put chrlo 1 and uint and shr pa 32 4294967295 1048575 +ret CHOK +end +fn chgpent va ulong n uint to ulong +ret or or and va CHVAM shl and ulong n 2097151 42 CHGPB41 +end +fn chmthd subc uint mthd uint count uint to uint +ret or or or CHNINC shl count 16 shl subc 13 shr mthd 2 +end +fn chbindop c ulong to long +if ne get mmmode 0 0 +trap 9 +end +ret gsbind get chchid c get chtok c get chent c get chgpva c get chupa c get chipa c +end +fn chdisop chid uint to long +if ne get mmmode 0 0 +trap 9 +end +ret gsdisable chid +end +fn chidlop chid uint to long +if ne get mmmode 0 0 +trap 9 +end +ret gsidle chid +end +fn chinit to void +for c ulong CHSLOTS +put chopen c 0 +put chgp c GMNONE +put chud c GMNONE +put chin c GMNONE +put chput c 0 +put chret c 0 +end +end +note create's error path: free whichever of the three buffers exist, ENOMEM +fn chnomem c ulong to long +if ne get chin c GMNONE +r = gmfree get chin c +end +if ne get chud c GMNONE +r = gmfree get chud c +end +if ne get chgp c GMNONE +r = gmfree get chgp c +end +put chgp c GMNONE +put chud c GMNONE +put chin c GMNONE +ret CHENOMEM +end +fn chcreate c ulong chid uint rl uint ent uint to long +if ge c CHSLOTS +ret CHEINVAL +end +if or gt chid CHIDMAX ge rl CHRLINV +ret CHEINVAL +end +if or or lt ent 2 gt ent 65536 ne and ent sub ent 1 0 +ret CHEINVAL +end +put chopen c 0 +put chput c 0 +put chret c 0 +put chgp c GMNONE +put chud c GMNONE +put chin c GMNONE +if ne gmalloc mul ulong ent 8 1 GMGPURO GMOK +ret chnomem c +end +put chgp c get gmout 0 +if ne gmalloc CHUDSZ 1 GMHPOLL GMOK +ret chnomem c +end +put chud c get gmout 0 +if ne gmalloc CHINSZ 1 0 GMOK +ret chnomem c +end +put chin c get gmout 0 +ret chfill c chid rl ent +end +note zero gpfifo, USERD and instance block; write the RAMIN page-directory words +fn chzero c ulong to ulong +g = get chgp c +u = get chud c +n = get chin c +mmzero get gmbpa g get gmbsz g +mmzero get gmbpa u get gmbsz u +mmzero get gmbpa n get gmbsz n +pdb = get mmroot 0 +ip = get gmbpa n +mmwr32 add ip CHPDBW ulong or or and uint and pdb 4294967295 CHLOM CHBIG64 CHTSYS +mmwr32 add ip add CHPDBW 4 shr pdb 32 +ret pdb +end +note fill the descriptor, the runlist entry, then bind through the channel ops +fn chfill c ulong chid uint rl uint ent uint to long +pdb = chzero c +put chchid c chid +put chrl c rl +put chtok c chtoken rl chid +put chent c ent +put chpdb c pdb +put chgpva c get gmbva get chgp c +put chupa c get gmbpa get chud c +put chipa c get gmbpa get chin c +if ne chrluserd get gmbpa get chud c CHOK +ret chnomem c +end +put chrl0 c get chrlo 0 +put chrl1 c get chrlo 1 +if ne chbindop c 0 +r = gmfree get chin c +r = gmfree get chud c +r = gmfree get chgp c +ret CHEBIND +end +put chopen c 1 +ret CHOK +end +fn chsubmit c ulong pb ulong off uint n uint to long +if or ge c CHSLOTS eq get chopen c 0 +ret CHESTATE +end +if not gmlive pb +ret CHEINVAL +end +if or or ne and off 3 0 eq n 0 gt n CHLENMX +ret CHEINVAL +end +span = mul ulong n 4 +sz = get gmbsz pb +if or gt ulong off sz gt span sub sz ulong off +ret CHEINVAL +end +va = add get gmbva pb ulong off +if or ge va CHVALIM gt span sub CHVALIM va +ret CHEINVAL +end +ret chpush c va n +end +note ring full check, GP entry at put % entries, GPPut in USERD, doorbell +fn chpush c ulong va ulong n uint to long +p = get chput c +e = get chent c +if ge sub p get chret c sub e 1 +ret CHEFULL +end +mmwr64 add get gmbpa get chgp c mul ulong rem p e 8 chgpent va n +p = add p 1 +put chput c p +mmwr32 add get gmbpa get chud c CHGPPUT ulong rem p e +mmregwr CHDBELL ulong get chtok c +ret CHOK +end +fn chretire c ulong upto uint to void +if ge c CHSLOTS +ret +end +r = get chret c +if le sub upto r sub get chput c r +put chret c upto +end +end +fn chteardown c ulong budget uint nx ulong to long +if or ge c CHSLOTS eq get chopen c 0 +ret CHESTATE +end +put chopen c 0 +chid = get chchid c +d = chdisop chid +idle = false +for i uint budget +if ne chidlop chid 0 +idle = true +break +end +end +rc = CHOK +for k ulong 3 +b = get chin c +if eq k 1 +b = get chud c +end +if eq k 2 +b = get chgp c +end +if not idle +r = gmquar b +else +if ne gmfree b GMOK +rc = CHETIME +end +end +end +if not idle +for i ulong nx +b = get chx i +if gmlive b +r = gmquar b +end +end +rc = CHETIME +end +put chgp c GMNONE +put chud c GMNONE +put chin c GMNONE +ret rc +end diff --git a/chan/gmmu.om b/chan/gmmu.om new file mode 100644 index 0000000..5eb24b6 --- /dev/null +++ b/chan/gmmu.om @@ -0,0 +1,507 @@ +note chan/gmmu.om: GB10 (Blackwell, ver3) GPU page tables and TLB invalidate, ported from aienux PR #441 +note native/kernel/gpu/gmmu.c and gmmu.h (branch pr441 @ c5bb3a4b). Library unit: no main. Prefix mm / MM. +note Concatenate with chan/mem.om, chan/chan.om, chan/sim.om (the mode 0 device) before a test or driver. +note +note Window: the C takes any caller window (cpu pointer, pa_base, size). Omega cannot hand out a pointer, +note so the window is the one global mmwin (8 MiB), at bus address mmwb with mmws bytes in use; mmwinit +note sets them. The page-table pool, the buffers of chan/mem.om and the channel's rings all live inside it, +note and chan/sim.om reads GPU-visible memory from the same array (in the C the simulator's mem is the +note window too). On hardware the integrator DMA-maps mmwin and passes its IOVA as mmwb. +note +note Register seam (as gsp/mmio.om gsprd / gspwr): mmmode 0 = simulator (chan/sim.om gsrd / gswr), +note mmmode 1 = 4-byte mapget / mapset on the vfiomapw BAR0 handle in mmbar, any other mode traps 9. +note +note API (state in globals; a global is never an argument): +note mmwinit base size -> long window at bus address base, size bytes (1..8388608) +note mmasinit pool pages -> long ck_gmmu_as_init: page-table pool of pages 4 KiB pages at pool +note mmmap va pa size page ap pcf ck_gmmu_map; mmunmap va size page: ck_gmmu_unmap +note mminval budget -> long ck_gmmu_invalidate (poll the trigger register budget times) +note mmptfree -> ulong ck_gmmu_pt_free +note mmpde mmdualb mmduals mmpte the entry encoders; mmlvidx lv va: ck_gmmu_index +note mmrd64 mmwr64 mmrd32 mmwr32 mmwr8 mmzero: little-endian window access by bus address +note globals out: mmroot (root page directory PA), mmdead (1 after an invalidate timeout), +note mmptpa / mmptus / mmptn (pool), mmleaf (last walk's leaf PA) +note codes (enum ck_gmmu_status): 0 MMOK, 1 MMEINVAL, 2 MMEALIGN, 3 MMERANGE, 4 MMEBUSY, 5 MMENOMEM, +note 6 MMENOENT, 7 MMETIME (ETIMEDOUT), 8 MMEDEAD +note Bounds this port adds: mminval polls at most 1000000 times (trap 4 past that; the C takes any +note uint32 budget); mmzero takes at most 1000000 bytes per call (trap 4). +const MMOK long 0 +const MMEINVAL long 1 +const MMEALIGN long 2 +const MMERANGE long 3 +const MMEBUSY long 4 +const MMENOMEM long 5 +const MMENOENT long 6 +const MMETIME long 7 +const MMEDEAD long 8 +note walk results: free, mapped, missing (create 0), conflict +const MMWFREE ulong 0 +const MMWMAP ulong 1 +const MMWMISS ulong 2 +const MMWCONF ulong 3 +note VA limit 1<<57, PA limit 1<<52, PDE/PTE address mask 0x000FFFFFFFFFF000, +note dual-PDE big-table mask 0x000FFFFFFFFFFF00 +const MMVALIM ulong 144115188075855872 +const MMPALIM ulong 4503599627370496 +const MMADDR ulong 4503599627366400 +const MMBIGM ulong 4503599627370240 +const MMSZ4K ulong 4096 +const MMSZ64K ulong 65536 +const MMSZ2M ulong 2097152 +const MMPTB ulong 4096 +const MMMAXPT ulong 64 +note aperture 2 = system memory coherent (PDE and PTE); PDE PCF 2 = cached, no ATS +const MMAPSYS ulong 2 +const MMPDEPCF ulong 2 +note GB10 virtual-function MMU invalidate registers: VF base 0xB80000, PDB 0xB830A0, UPPER_PDB 0xB830A4, +note INVALIDATE 0xB830B0; trigger bit 31, ALL_VA bit 0, PDB aperture sysmem 2 +const MMRPDB ulong 12071072 +const MMRUPR ulong 12071076 +const MMRINV ulong 12071088 +const MMTRIG ulong 2147483648 +const MMALLVA ulong 1 +const MMPDBAP ulong 2 +const MMWINMAX ulong 8388608 +global mmwin uchar 8388608 +global mmwb ulong 1 +global mmws ulong 1 +global mmmode ulong 1 +global mmbar long 1 +global mmptpa ulong 64 +global mmptus uchar 64 +global mmptn ulong 1 +global mmroot ulong 1 +global mmdead ulong 1 +global mmleaf ulong 1 +global mmmiss ulong 1 +global mmnpa ulong 1 +global mmkey ulong 7 +global mmwres ulong 1 +fn mmregrd off ulong to ulong +m = get mmmode 0 +if eq m 0 +ret gsrd off +end +if ne m 1 +trap 9 +end +ret mapget get mmbar 0 off 4 +end +fn mmregwr off ulong v ulong to void +m = get mmmode 0 +if eq m 0 +gswr off v +ret +end +if ne m 1 +trap 9 +end +r = mapset get mmbar 0 off 4 v +if ne r 0 +trap 9 +end +end +fn mmwinit base ulong size ulong to long +if or eq size 0 gt size MMWINMAX +ret MMEINVAL +end +if lt add base size base +ret MMERANGE +end +put mmwb 0 base +put mmws 0 size +ret MMOK +end +fn mmrd64 pa ulong to ulong +o = sub pa get mmwb 0 +v = ulong 0 +for i ulong 8 +v = or shl v 8 ulong get mmwin add o sub 7 i +end +ret v +end +fn mmwr64 pa ulong v ulong to void +o = sub pa get mmwb 0 +for i ulong 8 +put mmwin add o i uchar and shr v mul i 8 255 +end +end +fn mmrd32 pa ulong to ulong +o = sub pa get mmwb 0 +v = ulong 0 +for i ulong 4 +v = or shl v 8 ulong get mmwin add o sub 3 i +end +ret v +end +fn mmwr32 pa ulong v ulong to void +o = sub pa get mmwb 0 +for i ulong 4 +put mmwin add o i uchar and shr v mul i 8 255 +end +end +fn mmwr8 pa ulong v uchar to void +put mmwin sub pa get mmwb 0 v +end +fn mmzero pa ulong n ulong to void +o = sub pa get mmwb 0 +for i ulong n +put mmwin add o i 0 +end +end +note entry encoders (gmmu.c ck_gmmu_pde / ck_gmmu_dual_pde / ck_gmmu_pte) +fn mmpde pa ulong ap ulong to ulong +ret or or and pa MMADDR shl MMPDEPCF 3 shl and ap 3 1 +end +note dual PDE word 0 (big-page table, 64 KiB pages); 0 when the half is absent +fn mmdualb pa ulong ap ulong to ulong +if eq ap 0 +ret 0 +end +ret or or and pa MMBIGM shl MMPDEPCF 3 shl and ap 3 1 +end +note dual PDE word 1 (small-page table, 4 KiB pages) +fn mmduals pa ulong ap ulong to ulong +if eq ap 0 +ret 0 +end +ret or or and pa MMADDR shl MMPDEPCF 3 shl and ap 3 1 +end +fn mmpte pa ulong ap ulong pcf ulong kind ulong to ulong +v = or 1 shl and ap 3 1 +v = or v shl and pcf 31 3 +v = or v shl and kind 15 8 +ret or v and pa MMADDR +end +note level bit ranges: PDE4..PDE0 = 0..4, big PT = 5, small PT = 6 +fn mmlvlo lv ulong to ulong +if eq lv 0 +ret 56 +end +if eq lv 1 +ret 47 +end +if eq lv 2 +ret 38 +end +if eq lv 3 +ret 29 +end +if eq lv 4 +ret 21 +end +if eq lv 5 +ret 16 +end +ret 12 +end +fn mmlvhi lv ulong to ulong +if eq lv 0 +ret 56 +end +if eq lv 1 +ret 55 +end +if eq lv 2 +ret 46 +end +if eq lv 3 +ret 37 +end +if eq lv 4 +ret 28 +end +ret 20 +end +fn mmlvidx lv ulong va ulong to ulong +lo = mmlvlo lv +b = add sub mmlvhi lv lo 1 +ret and shr va lo sub shl 1 b 1 +end +fn mminwin pa ulong size ulong to bool +b = get mmwb 0 +s = get mmws 0 +if or eq size 0 lt pa b +ret false +end +o = sub pa b +if gt o s +ret false +end +ret le size sub s o +end +fn mmptalloc to long +n = get mmptn 0 +for i ulong n +if eq get mmptus i 0 +put mmptus i 1 +p = get mmptpa i +mmzero p MMPTB +put mmnpa 0 p +ret MMOK +end +end +ret MMENOMEM +end +fn mmptfree to ulong +c = ulong 0 +for i ulong get mmptn 0 +if eq get mmptus i 0 +c = add c 1 +end +end +ret c +end +fn mmasinit pool ulong pages ulong to long +if or or eq get mmws 0 0 or lt pages 2 gt pages MMMAXPT ne and pool 4095 0 +ret MMEINVAL +end +ok = mminwin pool mul pages MMPTB +if not ok +ret MMERANGE +end +put mmptn 0 pages +put mmdead 0 0 +for i ulong MMMAXPT +if lt i pages +put mmptpa i add pool mul i MMPTB +else +put mmptpa i 0 +end +put mmptus i 0 +end +r = mmptalloc +if eq r MMOK +put mmroot 0 get mmnpa 0 +end +ret r +end +note PDE4..PDE1: the PD0 table PA when mmwres is MMWFREE; otherwise mmwres is MMWCONF, or MMWMISS with +note the first missing level in mmmiss +fn mmwalkup va ulong create bool to ulong +put mmwres 0 MMWFREE +tbl = get mmroot 0 +for lv ulong 4 +ea = add tbl mul mmlvidx lv va 8 +v = mmrd64 ea +if ne and v 1 0 +put mmwres 0 MMWCONF +ret 0 +end +if eq and shr v 1 3 0 +if not create +put mmmiss 0 add lv 1 +put mmwres 0 MMWMISS +ret 0 +end +r = mmptalloc +if ne r MMOK +put mmwres 0 MMWCONF +ret 0 +end +mmwr64 ea mmpde get mmnpa 0 MMAPSYS +v = mmrd64 ea +end +tbl = and v MMADDR +end +ret tbl +end +note walk(va, page, create): MMWFREE / MMWMAP with the leaf PA in mmleaf, MMWMISS with the first missing +note level in mmmiss, MMWCONF +fn mmwalk va ulong page ulong create bool to ulong +tbl = mmwalkup va create +if ne get mmwres 0 MMWFREE +ret get mmwres 0 +end +d = add tbl mul mmlvidx 4 va 16 +w0 = mmrd64 d +w1 = mmrd64 add d 8 +bap = and shr w0 1 3 +sap = and shr w1 1 3 +if eq page MMSZ2M +put mmleaf 0 d +if ne and w0 1 0 +ret MMWMAP +end +if ne or bap sap 0 +ret MMWCONF +end +ret MMWFREE +end +if ne and w0 1 0 +ret MMWCONF +end +big = eq page MMSZ64K +if big +if ne sap 0 +ret MMWCONF +end +else +if ne bap 0 +ret MMWCONF +end +end +ret mmwalkpt d va big create +end +note walk, last stage: the big (64 KiB) or small (4 KiB) page table under the dual PDE0 at d +fn mmwalkpt d ulong va ulong big bool create bool to ulong +w0 = mmrd64 d +w1 = mmrd64 add d 8 +ap = and shr w1 1 3 +lvl = ulong 6 +pt = and w1 MMADDR +if big +ap = and shr w0 1 3 +lvl = 5 +pt = and w0 MMBIGM +end +if eq ap 0 +if not create +put mmmiss 0 lvl +ret MMWMISS +end +r = mmptalloc +if ne r MMOK +ret MMWCONF +end +pt = get mmnpa 0 +if big +mmwr64 add d 8 mmduals 0 0 +mmwr64 d mmdualb pt MMAPSYS +else +mmwr64 add d 8 mmduals pt MMAPSYS +mmwr64 d mmdualb 0 0 +end +end +p = add pt mul mmlvidx lvl va 8 +put mmleaf 0 p +if ne and mmrd64 p 1 0 +ret MMWMAP +end +ret MMWFREE +end +fn mmchk va ulong size ulong page ulong to long +if ne get mmdead 0 0 +ret MMEDEAD +end +if and and ne page MMSZ4K ne page MMSZ64K ne page MMSZ2M +ret MMEINVAL +end +if eq size 0 +ret MMEINVAL +end +if ne and or va size sub page 1 0 +ret MMEALIGN +end +if or ge va MMVALIM gt size sub MMVALIM va +ret MMERANGE +end +ret MMOK +end +note dry run of map: page tables still needed, or 2^64-1 when a page is mapped or conflicts. The window +note bounds a map to 2048 pages, so the loop bound is never reached. +fn mmneed va ulong size ulong page ulong to ulong +for j ulong 7 +put mmkey j 18446744073709551615 +end +last = ulong 6 +if eq page MMSZ2M +last = 4 +end +if eq page MMSZ64K +last = 5 +end +need = ulong 0 +off = ulong 0 +while lt off size bound 999999 +w = mmwalk add va off page false +if or eq w MMWMAP eq w MMWCONF +ret 18446744073709551615 +end +if eq w MMWMISS +j = get mmmiss 0 +while le j last bound 8 +k = shr add va off add mmlvhi j 1 +if ne k get mmkey j +put mmkey j k +need = add need 1 +end +if and eq j 4 eq last 6 +j = 5 +end +j = add j 1 +end +end +off = add off page +end +ret need +end +fn mmmap va ulong pa ulong size ulong page ulong ap ulong pcf ulong to long +rc = mmchk va size page +if ne rc MMOK +ret rc +end +if or gt ap 3 gt pcf 31 +ret MMEINVAL +end +if ne and pa sub page 1 0 +ret MMEALIGN +end +ok = mminwin pa size +if or ge pa MMPALIM not ok +ret MMERANGE +end +need = mmneed va size page +if eq need 18446744073709551615 +ret MMEBUSY +end +if gt need mmptfree +ret MMENOMEM +end +off = ulong 0 +while lt off size bound 999999 +w = mmwalk add va off page true +if ne w MMWFREE +ret MMEBUSY +end +mmwr64 get mmleaf 0 mmpte add pa off ap pcf 0 +off = add off page +end +ret MMOK +end +fn mmunmap va ulong size ulong page ulong to long +rc = mmchk va size page +if ne rc MMOK +ret rc +end +off = ulong 0 +while lt off size bound 999999 +if ne mmwalk add va off page false MMWMAP +ret MMENOENT +end +off = add off page +end +off = 0 +while lt off size bound 999999 +w = mmwalk add va off page false +mmwr64 get mmleaf 0 0 +off = add off page +end +ret MMOK +end +fn mminval budget ulong to long +if ne get mmdead 0 0 +ret MMEDEAD +end +pdb = shr get mmroot 0 12 +mmregwr MMRPDB or shl and pdb 268435455 4 MMPDBAP +mmregwr MMRUPR and shr pdb 28 1048575 +mmregwr MMRINV or MMALLVA MMTRIG +for i ulong budget +if eq and mmregrd MMRINV MMTRIG 0 +ret MMOK +end +end +put mmdead 0 1 +ret MMETIME +end diff --git a/chan/mem.om b/chan/mem.om new file mode 100644 index 0000000..5c48aec --- /dev/null +++ b/chan/mem.om @@ -0,0 +1,228 @@ +note chan/mem.om: GPU buffer allocator over chan/gmmu.om, ported from aienux PR #441 +note native/kernel/gpu/gpu_mem.c and gpu_mem.h (branch pr441 @ c5bb3a4b). Library unit: no main. +note Prefix gm / GM. Needs chan/gmmu.om before it. +note +note A buffer is a slot index 0..63 (the C hands out struct pointers into the same 64-slot table); +note GMNONE (64) is "no buffer". Any index past 63 is a foreign pointer and is refused like one. +note The allocator serves the address space of chan/gmmu.om (one per program; the C takes it as an arg). +note +note API (state in globals; a global is never an argument): +note gminit poll pab pages vab -> long ck_gmem_init: pages 4 KiB pages at bus address pab (inside the +note window, clear of the page-table pool), mapped at GPU VA vab +note gmalloc size cache flags -> long ck_gmem_alloc; the new slot in gmout (GMNONE on refusal) +note gmfree b / gmquar b -> long ck_gmem_free / ck_gmem_quarantine +note gmcount st -> ulong ck_gmem_count (pages in state st) +note gmlive b -> bool slot b is a buffer in state LIVE +note per slot: gmbpa (bus address; the CPU view is mmwin at gmbpa - mmwb), gmbva (GPU VA), gmbsz, +note gmbca, gmbfl, gmbst, gmbgen (uint, wraps as the C uint32) +note codes (enum ck_gmem_status): 0 GMOK, 1 GMEINVAL, 2 GMENOMEM, 3 GMESTATE, 4 GMEMAP, 5 GMETIME +note cache: 0 cached, 1 uncached; flags: 1 host polled (needs uncached), 2 GPU read-only +note states: 0 free, 1 live, 2 quarantined (never handed out again) +const GMOK long 0 +const GMEINVAL long 1 +const GMENOMEM long 2 +const GMESTATE long 3 +const GMEMAP long 4 +const GMETIME long 5 +const GMFREE uchar 0 +const GMLIVE uchar 1 +const GMQUAR uchar 2 +const GMHPOLL ulong 1 +const GMGPURO ulong 2 +const GMMAXB ulong 64 +const GMMAXP ulong 1024 +const GMNONE ulong 64 +global gmpoll ulong 1 +global gmpab ulong 1 +global gmvab ulong 1 +global gmpages ulong 1 +global gmpgst uchar 1024 +global gmbpa ulong 64 +global gmbva ulong 64 +global gmbsz ulong 64 +global gmbca ulong 64 +global gmbfl ulong 64 +global gmbst uchar 64 +global gmbgen uint 64 +global gmout ulong 1 +fn gminit poll ulong pab ulong pages ulong vab ulong to long +if or eq get mmptn 0 0 eq get mmws 0 0 +ret GMEINVAL +end +if or or or eq pages 0 gt pages GMMAXP ne and pab 4095 0 ne and vab sub MMSZ2M 1 0 +ret GMEINVAL +end +if eq poll 0 +ret GMEINVAL +end +if ne gmrange pab mul pages 4096 vab GMOK +ret GMEINVAL +end +put gmpoll 0 poll +put gmpab 0 pab +put gmvab 0 vab +put gmpages 0 pages +for i ulong GMMAXP +put gmpgst i GMFREE +end +for i ulong GMMAXB +put gmbst i GMFREE +put gmbgen i 0 +end +ret GMOK +end +note gminit's range checks: n bytes at pab inside the window, at vab inside the 57-bit VA space, and +note clear of every page-table page +fn gmrange pab ulong n ulong vab ulong to long +b = get mmwb 0 +s = get mmws 0 +if or or lt pab b gt sub pab b s gt n sub s sub pab b +ret GMEINVAL +end +if or ge vab MMVALIM gt n sub MMVALIM vab +ret GMEINVAL +end +ret gmptclr pab n +end +fn gmptclr pab ulong n ulong to long +for i ulong get mmptn 0 +p = get mmptpa i +if and gt add p MMPTB pab lt p add pab n +ret GMEINVAL +end +end +ret GMOK +end +fn gmlive b ulong to bool +if ge b GMMAXB +ret false +end +ret eq get gmbst b GMLIVE +end +fn gmsetpg b ulong st uchar to void +first = div sub get gmbpa b get gmpab 0 4096 +n = div get gmbsz b 4096 +for i ulong n +put gmpgst add first i st +end +end +fn gmalloc size ulong cache ulong flags ulong to long +note flags & ~3 != 0 is flags > 3 for an unsigned value +if or or or eq size 0 gt size mul GMMAXP 4096 gt cache 1 gt flags 3 +ret GMEINVAL +end +if and ne and flags GMHPOLL 0 ne cache 1 +ret GMEINVAL +end +put gmout 0 GMNONE +n = div add size 4095 4096 +b = gmslot +if eq b GMNONE +ret GMENOMEM +end +st = gmfit n +if eq st GMMAXP +ret GMENOMEM +end +ret gmplace b st n cache flags +end +note first free buffer slot, GMNONE when all 64 are taken +fn gmslot to ulong +for i ulong GMMAXB +if eq get gmbst i GMFREE +ret i +end +end +ret GMNONE +end +note first fit: the first page of the first run of n FREE pages, GMMAXP when there is none +fn gmfit n ulong to ulong +run = ulong 0 +st = ulong 0 +for i ulong get gmpages 0 +if ne get gmpgst i GMFREE +run = 0 +continue +end +if eq run 0 +st = i +end +run = add run 1 +if eq run n +ret st +end +end +ret GMMAXP +end +note map n pages from page st into slot b (the slot stays FREE, so its fields stay unused, until the +note map succeeds) +fn gmplace b ulong st ulong n ulong cache ulong flags ulong to long +put gmbpa b add get gmpab 0 mul st 4096 +put gmbva b add get gmvab 0 mul st 4096 +put gmbsz b mul n 4096 +pcf = gmpcf cache flags +r = mmmap get gmbva b get gmbpa b get gmbsz b MMSZ4K MMAPSYS pcf +if ne r MMOK +ret GMEMAP +end +put gmbca b cache +put gmbfl b flags +put gmbst b GMLIVE +put gmbgen b add get gmbgen b 1 +gmsetpg b GMLIVE +put gmout 0 b +ret GMOK +end +note PTE PCF: uncached 1 (GPU read-only 5), cached 0 (GPU read-only 4) +fn gmpcf cache ulong flags ulong to ulong +ro = ne and flags GMGPURO 0 +if eq cache 1 +if ro +ret 5 +end +ret 1 +end +if ro +ret 4 +end +ret 0 +end +fn gmretire b ulong reuse bool to long +if not gmlive b +ret GMESTATE +end +um = mmunmap get gmbva b get gmbsz b MMSZ4K +iv = um +if eq um MMOK +iv = mminval get gmpoll 0 +end +if and reuse eq iv MMOK +gmsetpg b GMFREE +put gmbst b GMFREE +ret GMOK +end +gmsetpg b GMQUAR +put gmbst b GMFREE +if not reuse +ret GMOK +end +if or eq iv MMETIME eq iv MMEDEAD +ret GMETIME +end +ret GMEMAP +end +fn gmfree b ulong to long +ret gmretire b true +end +fn gmquar b ulong to long +ret gmretire b false +end +fn gmcount st uchar to ulong +c = ulong 0 +for i ulong get gmpages 0 +if eq get gmpgst i st +c = add c 1 +end +end +ret c +end diff --git a/chan/params.om b/chan/params.om new file mode 100644 index 0000000..f32dce4 --- /dev/null +++ b/chan/params.om @@ -0,0 +1,300 @@ +note chan/params.om: the RM-alloc and RM-control parameter records the W5 "hello" chain forwards +note to GSP-RM (GSP_RM_ALLOC 103 / GSP_RM_CONTROL 76 carry them verbatim: rpcRmApiAlloc_GSP and +note rpcRmApiControl_GSP memcpy the caller's params) plus the two page-directory RPC bodies. +note Layout source of record: NVIDIA open-gpu-kernel-modules 580.173.02 (MIT), struct offsets and +note sizes printed by tests/params_ref.c from offsetof/sizeof on the NVIDIA headers and compared +note byte for byte by tests/params_diff.sh. Library unit, no main. Prefix pa. +note Every encoder zeroes pabuf, writes one record little-endian, sets palen and returns its size, +note or a negative error and leaves pabuf untouched: PAEFIT a value does not fit its field, +note PAERANGE a value outside the field's legal set, PAELEN a reply of the wrong length. +const PAOK long 0 +const PAEFIT long -1 +const PAERANGE long -2 +const PAELEN long -3 +note 0xFFFFFFFF +const PAU32 ulong 4294967295 +note channel ids are 12 bits (0xFFF) +const PACHMAX ulong 4095 +note RPC function numbers (nvidia/inc/kernel/vgpu/rpc_global_enums.h:64,86,89,113) +const PARSETPD ulong 54 +const PARCTRL ulong 76 +const PARUNSPD ulong 79 +const PARALLOC ulong 103 +note classes: NV01_ROOT 0x0, NV01_DEVICE_0 0x80, NV20_SUBDEVICE_0 0x2080, FERMI_VASPACE_A 0x90f1, +note KEPLER_CHANNEL_GROUP_A 0xa06c, FERMI_CONTEXT_SHARE_A 0x9067, BLACKWELL_CHANNEL_GPFIFO_A 0xc96f, +note BLACKWELL_COMPUTE_B 0xcec0 +const PACROOT ulong 0 +const PACDEV ulong 128 +const PACSUB ulong 8320 +const PACVAS ulong 37105 +const PACTSG ulong 41068 +const PACCTX ulong 36967 +const PACGPF ulong 51567 +const PACCMP ulong 52928 +note controls: NVA06C_CTRL_CMD_GPFIFO_SCHEDULE 0xa06c0101 (ctrla06c.h:97), +note NV2080_CTRL_CMD_INTERNAL_STATIC_KGR_GET_INFO 0x20800a2a (ctrl2080internal.h:422) +const PACMSCH ulong 2691432705 +const PACMGRI ulong 545262122 +note NV2080_ENGINE_TYPE_GRAPHICS 0x1, NV_CTXSHARE_ALLOCATION_FLAGS_SUBCONTEXT_ASYNC 0x1 +const PAENGGR ulong 1 +const PACTXAS ulong 1 +note record sizes (sizeof on the 580.173.02 headers) +const PASZROOT ulong 120 +const PASZDEV ulong 56 +const PASZSUB ulong 4 +const PASZVAS ulong 56 +const PASZTSG ulong 20 +const PASZCTX ulong 12 +const PASZCHAN ulong 368 +const PASZSCH ulong 3 +const PASZSPD ulong 48 +const PASZUPD ulong 16 +note GR info reply: 8 engines x 0x3a entries x 8 bytes = 3712; one engine is 58 x 8 = 464 bytes; +note SM_VERSION is index 0xC, its data word at 12 x 8 + 4 = 100 +const PAGRSZ ulong 3712 +const PAGRENG ulong 464 +const PAGRMAX ulong 8 +const PASMOFF ulong 100 +note NVOS04 channel flags: clear USERD_INDEX_VALUE 10:8, USERD_INDEX_FIXED 11, USERD_INDEX_PAGE_VALUE +note 20:12 and USERD_INDEX_PAGE_FIXED 21 (mask 0xFFC000FF), then set PAGE_FIXED (1 << 21) +const PAFLMSK ulong 4290773247 +const PAPGFIX ulong 2097152 +text PANAME "aien-ogd" +global pabuf uchar 4096 +global palen ulong 1 +global pain ulong 48 +global pareply uchar 4096 +fn paclr n ulong to void +for i ulong 4096 +put pabuf i 0 +end +put palen 0 n +end +fn paput32 o ulong v ulong to void +for i ulong 4 +put pabuf add o i uchar and shr v mul i 8 255 +end +end +fn paput64 o ulong v ulong to void +for i ulong 8 +put pabuf add o i uchar and shr v mul i 8 255 +end +end +fn paget32 o ulong to ulong +v = ulong 0 +for i ulong 4 +v = or shl v 8 ulong get pareply add o sub 3 i +end +ret v +end +fn paf4 a ulong b ulong c ulong d ulong to bool +ret and and le a PAU32 le b PAU32 and le c PAU32 le d PAU32 +end +note NV0000_ALLOC_PARAMETERS: hClient 0, processID 4, processName[100] 8 ("aien-ogd", NUL padded), +note pOsPidInfo 112 (0) +fn paroot hcli ulong pid ulong to long +if not paf4 hcli pid 0 0 +ret PAEFIT +end +paclr PASZROOT +paput32 0 hcli +paput32 4 pid +for i ulong len PANAME +put pabuf add 8 i get PANAME i +end +ret long PASZROOT +end +note NV0080_ALLOC_PARAMETERS: deviceId 0, hClientShare 4, hTargetClient 8 (0), hTargetDevice 12 (0), +note flags 16, vaSpaceSize 24, vaStartInternal 32 (0), vaLimitInternal 40 (0), vaMode 48 +fn padevice devid ulong hshare ulong flags ulong vamode ulong vasize ulong to long +if not paf4 devid hshare flags vamode +ret PAEFIT +end +paclr PASZDEV +paput32 0 devid +paput32 4 hshare +paput32 16 flags +paput64 24 vasize +paput32 48 vamode +ret long PASZDEV +end +note NV2080_ALLOC_PARAMETERS: subDeviceId 0 +fn pasubdev subid ulong to long +if gt subid PAU32 +ret PAEFIT +end +paclr PASZSUB +paput32 0 subid +ret long PASZSUB +end +note NV_VASPACE_ALLOCATION_PARAMETERS: index 0, flags 4, vaSize 8, vaStartInternal 16 (0), +note vaLimitInternal 24 (0), bigPageSize 32, vaBase 40, pasid 48 (0) +fn pavaspace index ulong flags ulong vasize ulong vabase ulong bigpg ulong to long +if not paf4 index flags bigpg 0 +ret PAEFIT +end +paclr PASZVAS +paput32 0 index +paput32 4 flags +paput64 8 vasize +paput32 32 bigpg +paput64 40 vabase +ret long PASZVAS +end +note NV_CHANNEL_GROUP_ALLOCATION_PARAMETERS: hObjectError 0, hObjectEccError 4, hVASpace 8, +note engineType 12, bIsCallingContextVgpuPlugin 16 (NV_FALSE) +fn patsg herr ulong hecc ulong hvas ulong eng ulong to long +if not paf4 herr hecc hvas eng +ret PAEFIT +end +paclr PASZTSG +paput32 0 herr +paput32 4 hecc +paput32 8 hvas +paput32 12 eng +ret long PASZTSG +end +note NV_CTXSHARE_ALLOCATION_PARAMETERS: hVASpace 0, flags 4, subctxId 8 +fn pactxsh hvas ulong flags ulong subctx ulong to long +if not paf4 hvas flags subctx 0 +ret PAEFIT +end +paclr PASZCTX +paput32 0 hvas +paput32 4 flags +paput32 8 subctx +ret long PASZCTX +end +note pain indices that hold NvU64 fields in the channel layout: gpFifoOffset 1, userdOffset[0] 7, +note base/size of the six NV_MEMORY_DESC_PARAMS (16,17 20,21 24,25 28,29 32,33 38,39) +fn pawide k ulong to bool +if or eq k 1 eq k 7 +ret true +end +if and ge k 16 le k 35 +ret lt rem sub k 16 4 2 +end +ret or eq k 38 eq k 39 +end +note NV_MEMORY_DESC_PARAMS at o from pain[k..k+3]: base 0, size 8, addressSpace 16, cacheAttrib 20 +fn pamd o ulong k ulong to void +paput64 o get pain k +paput64 add o 8 get pain add k 1 +paput32 add o 16 get pain add k 2 +paput32 add o 20 get pain add k 3 +end +note the GSP-client flag rewrite of kernel_channel.c:2814-2821: USERD_INDEX_FIXED false, +note USERD_INDEX_PAGE_FIXED true, USERD_INDEX_VALUE = chid % 8, USERD_INDEX_PAGE_VALUE = chid / 8 +fn pachfl fl ulong chid ulong to ulong +v = or and fl PAFLMSK PAPGFIX +ret or v or shl rem chid 8 8 shl and div chid 8 511 12 +end +note NV_CHANNEL_ALLOC_PARAMS (368 bytes) from pain: +note 0 hObjectError, 1 gpFifoOffset, 2 gpFifoEntries, 3 client flags, 4 hContextShare, 5 hVASpace, +note 6 hUserdMemory[0], 7 userdOffset[0], 8 engineType, 9 subDeviceId, 10 hObjectEccError, +note 11 hPhysChannelGroup, 12 error notifier type, 13 ECC error notifier type, 14 privilege level, +note 15 chid, 16-19 instanceMem, 20-23 userdMem, 24-27 ramfcMem, 28-31 mthdbufMem, +note 32-35 errorNotifierMem, 36 ProcessID, 37 SubProcessID, 38-41 eccErrorNotifierMem. +note hObjectBuffer, cid, hUserdMemory[1..7], userdOffset[1..7], the IVs, hmacNonce and tpcConfigID +note are 0. internalFlags = PRIVILEGE 1:0 | ERROR_NOTIFIER_TYPE 3:2 | ECC_ERROR_NOTIFIER_TYPE 5:4. +fn pachan to long +for k ulong 42 +if and not pawide k gt get pain k PAU32 +ret PAEFIT +end +end +if or or gt get pain 12 3 gt get pain 13 3 gt get pain 14 3 +ret PAERANGE +end +chid = get pain 15 +if gt chid PACHMAX +ret PAERANGE +end +paclr PASZCHAN +paput32 0 get pain 0 +paput64 8 get pain 1 +paput32 16 get pain 2 +paput32 20 pachfl get pain 3 chid +paput32 24 get pain 4 +paput32 28 get pain 5 +paput32 32 get pain 6 +paput64 64 get pain 7 +paput32 128 get pain 8 +paput32 136 get pain 9 +paput32 140 get pain 10 +pamd 144 16 +pamd 168 20 +pamd 192 24 +pamd 216 28 +paput32 240 get pain 11 +paput32 244 or or get pain 14 shl get pain 12 2 shl get pain 13 4 +pamd 248 32 +pamd 272 38 +paput32 296 get pain 36 +paput32 300 get pain 37 +ret long PASZCHAN +end +note NVA06C_CTRL_GPFIFO_SCHEDULE_PARAMS: bEnable 0, bSkipSubmit 1, bSkipEnable 2 (NvBool each) +fn pasched en ulong skipsub ulong skipen ulong to long +if or or gt en 1 gt skipsub 1 gt skipen 1 +ret PAERANGE +end +paclr PASZSCH +put pabuf 0 uchar en +put pabuf 1 uchar skipsub +put pabuf 2 uchar skipen +ret long PASZSCH +end +note NV2080_CTRL_INTERNAL_STATIC_GR_GET_INFO_PARAMS request: all zero, GSP-RM fills the reply +fn pagrinfo to long +paclr PAGRSZ +ret long PAGRSZ +end +note SM_VERSION of engine eng from a GR_GET_INFO reply of n bytes copied into pareply: +note engineInfo[eng].infoList[0xC].data +fn pasmver eng ulong n ulong to long +if ne n PAGRSZ +ret PAELEN +end +if ge eng PAGRMAX +ret PAERANGE +end +ret long paget32 add mul eng PAGRENG PASMOFF +end +note rpc_set_page_directory_v1E_05 (48 bytes) from pain: 0 hClient 0, 1 hDevice 4, 2 pasid 8, +note params at 16: 3 physAddress 16, 4 numEntries 24, flags 28 = 5 APERTURE 1:0 (0..2) | +note 6 PRESERVE_PDES 2:2 (0..1), 7 hVASpace 32, 8 chId 36, 9 subDeviceId 40, 10 params.pasid 44 +fn pasetpd to long +for k ulong 11 +if and ne k 3 gt get pain k PAU32 +ret PAEFIT +end +end +if or gt get pain 5 2 gt get pain 6 1 +ret PAERANGE +end +paclr PASZSPD +paput32 0 get pain 0 +paput32 4 get pain 1 +paput32 8 get pain 2 +paput64 16 get pain 3 +paput32 24 get pain 4 +paput32 28 or get pain 5 shl get pain 6 2 +paput32 32 get pain 7 +paput32 36 get pain 8 +paput32 40 get pain 9 +paput32 44 get pain 10 +ret long PASZSPD +end +note rpc_unset_page_directory_v1E_05: hClient 0, hDevice 4, params at 8: hVASpace 8, subDeviceId 12 +fn paunsetpd hcli ulong hdev ulong hvas ulong subdev ulong to long +if not paf4 hcli hdev hvas subdev +ret PAEFIT +end +paclr PASZUPD +paput32 0 hcli +paput32 4 hdev +paput32 8 hvas +paput32 12 subdev +ret long PASZUPD +end diff --git a/chan/sim.om b/chan/sim.om new file mode 100644 index 0000000..a5a40fa --- /dev/null +++ b/chan/sim.om @@ -0,0 +1,537 @@ +note chan/sim.om: host simulator of the GB10 pieces the channel path touches, ported from aienux PR #441 +note native/kernel/gpu/gpu_sim.c and gpu_sim.h (branch pr441 @ c5bb3a4b). Library unit: no main. +note Prefix gs / GS (never gsp). It is the mode 0 device of chan/gmmu.om mmregrd / mmregwr and of the +note channel ops in chan/chan.om. Needs chan/gmmu.om (the window array mmwin) before it. +note +note It models: the VF MMU invalidate registers (a request completes after gsi[4] polls of INVALIDATE, +note never when gsi[5] "stuck" is set), a 16-entry TLB filled by its own ver3 page walker, the fault log, +note the doorbell (a submit token consumes that channel's ring: USERD GPPut, GP entries, push buffer +note words, all through the GPU MMU, so a missing mapping or a write to read-only memory faults) and the +note channel bind / disable / idle protocol. Every register access is logged (512 entries, then counted +note as dropped). The walker, offsets and bit fields are this file's own copies, not chan/gmmu.om's, so +note a wrong driver constant shows up as a wrong access or a fault. +note +note State (the fields of struct ck_gsim): +note gsmem 0 base, 1 size of the memory the GPU sees (inside mmwin, at bus address gsmem 0) +note gsi 0 pdb_lo, 1 pdb_hi, 2 pending, 3 polls left, 4 latency (3), 5 stuck, 6 done, 7 all +note gslogo / gslogv / gslogw, gsnlog, gsldrop: the register log +note gstv gstpdb gstva gstpa gstpg gstlo, gstn (next), gsthits: the TLB +note gsfva gsfpdb gsfkind gsfw, gsnf: faults (16 kept, all counted) +note per channel slot (4): gscb bound, gscid, gsctok, gscent, gscgpva, gscupa, gscipa, gscpdb, +note gscget gp_get, gscwr words_read, gscflt faulted, gscdis disabled +note gsdb doorbells, gsdbunk doorbell_unknown, gshang (set by a test: the engine never idles), +note gsidlep idle_polls +note fault kinds: 1 unmapped, 2 bad PDE (a PTE above PDE2), 3 aperture, 4 write to read-only, 5 outside +note memory +const GSRPDB ulong 12071072 +const GSRUPR ulong 12071076 +const GSRINV ulong 12071088 +const GSDBELL ulong 12255376 +const GSTRIG ulong 2147483648 +const GSADDR ulong 4503599627366400 +const GSBIGM ulong 4503599627370240 +const GSUDPUT ulong 140 +const GSLOGMX ulong 512 +const GSNONE ulong 4 +note gshalf's "try the other half" result; fault kinds are 1..5, so 255 never collides +const GSNEXT ulong 255 +note gspdbof's refusal: 2^64-1, never a page-aligned PDB +const GSBADPD ulong 18446744073709551615 +global gsmem ulong 2 +global gsi ulong 8 +global gslogo uint 512 +global gslogv uint 512 +global gslogw uchar 512 +global gsnlog ulong 1 +global gsldrop ulong 1 +global gstv uchar 16 +global gstpdb ulong 16 +global gstva ulong 16 +global gstpa ulong 16 +global gstpg ulong 16 +global gstlo ulong 16 +global gstn ulong 1 +global gsthits ulong 1 +global gsfva ulong 16 +global gsfpdb ulong 16 +global gsfkind ulong 16 +global gsfw ulong 16 +global gsnf ulong 1 +global gscb uchar 4 +global gscid uint 4 +global gsctok uint 4 +global gscent uint 4 +global gscgpva ulong 4 +global gscupa ulong 4 +global gscipa ulong 4 +global gscpdb ulong 4 +global gscget uint 4 +global gscwr uint 4 +global gscflt uchar 4 +global gscdis uchar 4 +global gsdb ulong 1 +global gsdbunk ulong 1 +global gshang ulong 1 +global gsidlep ulong 1 +note walk / translate results: 0 pa, 1 page size, 2 pte low 12 bits, 3 the word read +global gsw ulong 4 +fn gsinit base ulong size ulong to void +put gsmem 0 base +put gsmem 1 size +for i ulong 8 +put gsi i 0 +end +put gsi 4 3 +for i ulong GSLOGMX +put gslogo i 0 +put gslogv i 0 +put gslogw i 0 +end +put gsnlog 0 0 +put gsldrop 0 0 +for i ulong 16 +put gstv i 0 +put gstpdb i 0 +put gstva i 0 +put gstpa i 0 +put gstpg i 0 +put gstlo i 0 +put gsfva i 0 +put gsfpdb i 0 +put gsfkind i 0 +put gsfw i 0 +end +put gstn 0 0 +put gsthits 0 0 +put gsnf 0 0 +for i ulong 4 +put gscb i 0 +put gscid i 0 +put gsctok i 0 +put gscent i 0 +put gscgpva i 0 +put gscupa i 0 +put gscipa i 0 +put gscpdb i 0 +put gscget i 0 +put gscwr i 0 +put gscflt i 0 +put gscdis i 0 +end +put gsdb 0 0 +put gsdbunk 0 0 +put gshang 0 0 +put gsidlep 0 0 +for i ulong 4 +put gsw i 0 +end +end +fn gslog off ulong v ulong w uchar to void +n = get gsnlog 0 +if lt n GSLOGMX +put gslogo n uint off +put gslogv n uint v +put gslogw n w +put gsnlog 0 add n 1 +else +put gsldrop 0 add get gsldrop 0 1 +end +end +fn gsinmem pa ulong n ulong to bool +b = get gsmem 0 +s = get gsmem 1 +if lt pa b +ret false +end +o = sub pa b +if gt o s +ret false +end +ret le n sub s o +end +fn gsrd64 pa ulong to ulong +o = add sub pa get gsmem 0 sub get gsmem 0 get mmwb 0 +v = ulong 0 +for i ulong 8 +v = or shl v 8 ulong get mmwin add o sub 7 i +end +ret v +end +fn gsrd32 pa ulong to ulong +o = add sub pa get gsmem 0 sub get gsmem 0 get mmwb 0 +v = ulong 0 +for i ulong 4 +v = or shl v 8 ulong get mmwin add o sub 3 i +end +ret v +end +fn gslo lv ulong to ulong +if eq lv 0 +ret 56 +end +if eq lv 1 +ret 47 +end +if eq lv 2 +ret 38 +end +if eq lv 3 +ret 29 +end +ret 21 +end +fn gshi lv ulong to ulong +if eq lv 0 +ret 56 +end +if eq lv 1 +ret 55 +end +if eq lv 2 +ret 46 +end +if eq lv 3 +ret 37 +end +ret 28 +end +fn gschkpte pte ulong wf ulong to ulong +ap = and shr pte 1 3 +pcf = and shr pte 3 31 +if or eq ap 0 eq ap 1 +ret 3 +end +if and ne wf 0 ne and pcf 4 0 +ret 4 +end +ret 0 +end +note a valid PTE of page size psz: translation into gsw 0..2; 5 when the target is outside memory +fn gsleaf e ulong psz ulong va ulong to ulong +pa = add and and e GSADDR not sub psz 1 and va sub psz 1 +put gsw 0 pa +put gsw 1 psz +put gsw 2 and e 4095 +if gsinmem pa 4 +ret 0 +end +ret 5 +end +note the dual PDE0 at tbl + idx*16: small half (4 KiB pages) first, then big half (64 KiB) +fn gsdual tbl ulong idx ulong e ulong va ulong wf ulong to ulong +e1 = gsrd64 add add tbl mul idx 16 8 +f = gshalf and shr e1 1 3 and e1 GSADDR 12 va wf +if ne f GSNEXT +ret f +end +f = gshalf and shr e 1 3 and e GSBIGM 16 va wf +if ne f GSNEXT +ret f +end +ret 1 +end +note one half of the dual PDE0: aperture ap, page table pt, page shift sh; GSNEXT means try the next half +fn gshalf ap ulong pt ulong sh ulong va ulong wf ulong to ulong +if eq ap 0 +ret GSNEXT +end +if eq ap 1 +ret 3 +end +a = add pt mul and shr va sh sub shl 1 sub 21 sh 1 8 +if not gsinmem a 8 +ret 5 +end +pte = gsrd64 a +if eq and pte 1 0 +ret GSNEXT +end +f = gschkpte pte wf +if ne f 0 +ret f +end +ret gsleaf pte shl 1 sh va +end +fn gswalk pdb ulong va ulong wf ulong to ulong +if ne shr va 57 0 +ret 1 +end +tbl = pdb +for lv ulong 5 +esz = ulong 8 +if eq lv 4 +esz = 16 +end +lo = gslo lv +idx = and shr va lo sub shl 1 add sub gshi lv lo 1 1 +ea = add tbl mul idx esz +if not gsinmem ea esz +ret 5 +end +e = gsrd64 ea +if ne and e 1 0 +if lt lv 2 +ret 2 +end +f = gschkpte e wf +if ne f 0 +ret f +end +ret gsleaf e shl 1 lo va +end +if lt lv 4 +ap = and shr e 1 3 +if eq ap 0 +ret 1 +end +if eq ap 1 +ret 3 +end +tbl = and e GSADDR +continue +end +ret gsdual tbl idx e va wf +end +ret 1 +end +fn gsfault pdb ulong va ulong kind ulong wf ulong to void +n = get gsnf 0 +if lt n 16 +put gsfva n va +put gsfpdb n pdb +put gsfkind n kind +put gsfw n wf +end +put gsnf 0 add n 1 +end +fn gsxlate pdb ulong va ulong wf ulong to ulong +i = gstlbhit pdb va +if lt i 16 +f = gschkpte get gstlo i wf +if ne f 0 +gsfault pdb va f wf +ret f +end +put gsthits 0 add get gsthits 0 1 +put gsw 0 add get gstpa i sub va get gstva i +ret 0 +end +f = gswalk pdb va wf +if ne f 0 +gsfault pdb va f wf +ret f +end +gstfill pdb va +ret 0 +end +note the first valid TLB slot covering (pdb, va), 16 when none +fn gstlbhit pdb ulong va ulong to ulong +for i ulong 16 +if and ne get gstv i 0 eq get gstpdb i pdb +if lt sub va get gstva i get gstpg i +ret i +end +end +end +ret 16 +end +note fill TLB slot gstn % 16 from the walk result in gsw (pa, page, pte_lo) +fn gstfill pdb ulong va ulong to void +n = rem get gstn 0 16 +put gstn 0 add get gstn 0 1 +pg = get gsw 1 +vp = and va not sub pg 1 +put gstv n 1 +put gstpdb n pdb +put gstpg n pg +put gstva n vp +put gstpa n sub get gsw 0 sub va vp +put gstlo n get gsw 2 +end +note a GPU read of 4 bytes at va: fault kind, the word in gsw 3 +fn gsgrd32 pdb ulong va ulong to ulong +f = gsxlate pdb va 0 +if ne f 0 +ret f +end +put gsw 3 gsrd32 get gsw 0 +ret 0 +end +fn gschan chid uint to ulong +for i ulong 4 +if and ne get gscb i 0 eq get gscid i chid +ret i +end +end +ret GSNONE +end +fn gsconsume c ulong to void +if or or ne get gscflt c 0 ne get gscdis c 0 ne get gshang 0 0 +ret +end +upa = add get gscupa c GSUDPUT +if not gsinmem upa 4 +put gscflt c 1 +ret +end +pv = uint gsrd32 upa +ent = get gscent c +if ge pv ent +put gscflt c 1 +ret +end +pdb = get gscpdb c +while ne get gscget c pv bound 999999 +if not gsent c pdb add get gscgpva c mul ulong get gscget c 8 +put gscflt c 1 +ret +end +put gscget c rem add get gscget c 1 ent +end +end +note one GP entry at eva: read both words, then every pushbuffer word; false on a fault +fn gsent c ulong pdb ulong eva ulong to bool +if ne gsgrd32 pdb eva 0 +ret false +end +lo = get gsw 3 +if ne gsgrd32 pdb add eva 4 0 +ret false +end +hi = get gsw 3 +ret gspb c pdb or and lo 4294967292 shl and hi 255 32 and shr hi 10 2097151 +end +note read the n pushbuffer words at pbva (counted in gscwr); false on a fault +fn gspb c ulong pdb ulong pbva ulong n ulong to bool +for a ulong div add n 1023 1024 +for b ulong 1024 +w = add mul a 1024 b +if ge w n +break +end +if ne gsgrd32 pdb add pbva mul w 4 0 +ret false +end +put gscwr c add get gscwr c 1 +end +end +ret true +end +fn gsrd off ulong to ulong +v = ulong 0 +if eq off GSRINV +if and ne get gsi 2 0 eq get gsi 5 0 +if eq get gsi 3 0 +pdb = shl or and shr get gsi 0 4 268435455 shl and get gsi 1 1048575 28 12 +for i ulong 16 +if or ne get gsi 7 0 eq get gstpdb i pdb +put gstv i 0 +end +end +put gsi 2 0 +put gsi 6 add get gsi 6 1 +else +put gsi 3 sub get gsi 3 1 +end +end +if ne get gsi 2 0 +v = GSTRIG +end +end +gslog off v 0 +ret v +end +fn gswr off ulong v ulong to void +gslog off v 1 +if eq off GSRPDB +put gsi 0 v +ret +end +if eq off GSRUPR +put gsi 1 v +ret +end +if and eq off GSRINV ne and v GSTRIG 0 +put gsi 2 1 +put gsi 3 get gsi 4 +put gsi 7 0 +if ne and v 2 0 +put gsi 7 1 +end +ret +end +if eq off GSDBELL +put gsdb 0 add get gsdb 0 1 +for i ulong 4 +if and ne get gscb i 0 eq ulong get gsctok i v +gsconsume i +ret +end +end +put gsdbunk 0 add get gsdbunk 0 1 +end +end +note bind: the channel's page directory comes from its instance block (RAMIN word 128/129), as the +note hardware reads it; target must be sysmem (2 coherent, 3 non-coherent), bit 10 (VOL) clear +fn gsbind chid uint tok uint ent uint gpva ulong upa ulong ipa ulong to long +if ne gschan chid GSNONE +ret -1 +end +pdb = gspdbof ipa +if eq pdb GSBADPD +ret -1 +end +for i ulong 4 +if eq get gscb i 0 +put gscid i chid +put gsctok i tok +put gscent i ent +put gscgpva i gpva +put gscupa i upa +put gscipa i ipa +put gscpdb i pdb +put gscget i 0 +put gscwr i 0 +put gscflt i 0 +put gscdis i 0 +put gscb i 1 +ret 0 +end +end +ret -1 +end +note the page directory from RAMIN word 128/129 of the instance block at ipa; GSBADPD when the words +note are outside memory, the target is not sysmem or VOL is set (a real PDB has its low 12 bits clear) +fn gspdbof ipa ulong to ulong +a = add ipa 512 +if not gsinmem a 8 +ret GSBADPD +end +w = gsrd64 a +lo = and w 4294967295 +t = and lo 3 +if or and ne t 2 ne t 3 ne and lo 1024 0 +ret GSBADPD +end +ret or shl shr w 32 32 and lo 4294963200 +end +fn gsdisable chid uint to long +c = gschan chid +if eq c GSNONE +ret -1 +end +put gscdis c 1 +ret 0 +end +fn gsidle chid uint to long +put gsidlep 0 add get gsidlep 0 1 +c = gschan chid +if eq c GSNONE +ret 1 +end +if or ne get gshang 0 0 eq get gscdis c 0 +ret 0 +end +put gscb c 0 +ret 1 +end diff --git a/fwprep.om b/fwprep.om new file mode 100644 index 0000000..635bb2f --- /dev/null +++ b/fwprep.om @@ -0,0 +1,545 @@ +note fwprep.om: the GB10 firmware inputs for the bring-up chain, from files only (OM-1 seeded omc). +note Usage: fwprep FWFILE OGKMSRC OUTDIR +note FWFILE gsp_ga10x.bin (580.173.02), inspected as ck_gsp_fw_inspect inspect does +note OGKMSRC open-gpu-kernel-modules-580.173.02 tree; its two GB10B bindata files are read as +note ck_gsp_bindata_extract extract does (src/nvidia/generated/g_bindata_kgspGetBinArchive*) +note OUTDIR gets fmc.img fmc.hash fmc.pubkey fmc.sig bl.img, then fw.manifest (written to +note fw.manifest.tmp and renamed; fw.manifest is unlinked first, so a refused or failed run +note never leaves a manifest behind; consumers read the outputs only through it) +note stdout: the inspect report, the FMC extract report, the boot extract report (the C tools' exact +note bytes, without --out), then "fwprep ..." lines. Exit 0 ok, 1 usage or FWFILE open/read error +note (stderr), 2 refused, 3 output write failure. +note Build: cat gsp/sha2.om gsp/inflate.om gsp/fw.om gsp/bindata.om fwprep.om | build/omc > fwprep +note Prefix fwp. Host only: reads files, writes OUTDIR; no device. +global fwpok ulong 1 +global fwpsgb ulong 80 +global fwpsgl ulong 80 +global fwpst uchar 128 +global fwpsrc uchar 4096 +global fwpsrcn ulong 1 +global fwpout uchar 4096 +global fwpoutn ulong 1 +global fwpp uchar 4096 +global fwppn ulong 1 +global fwprn uchar 8200 +global fwpoo ulong 5 +global fwpon ulong 5 +global fwpoh uchar 320 +global fwpdo ulong 1 +global fwpsz ulong 2 +global fwpsh uchar 128 +note FWPTXT segments: 1 "fwprep ", 2 usage, 3..5 bindata paths, 6..7 functions, 8..13 labels, +note 14..20 output names, 21.. messages, 33.. manifest pieces, 43..63 the 21 RM_RISCV_UCODE_DESC +note words in struct order (C riscv_desc), 64.. more pieces +text FWPTXT "|fwprep |usage: fwprep FWFILE OGKMSRC OUTDIR|/src/nvidia/generated/|g_bindata_kgspGetBinArchiveGspRmFmcGfwProdSigned_GB10B.c|g_bindata_kgspGetBinArchiveGspRmBoot_GB10B.c|kgspGetBinArchiveGspRmFmcGfwProdSigned_GB10B|kgspGetBinArchiveGspRmBoot_GB10B|UCODE_IMAGE|UCODE_HASH|UCODE_PKEY|UCODE_SIG|UCODE_IMAGE_PROD|UCODE_DESC_PROD|fmc.img|fmc.hash|fmc.pubkey|fmc.sig|bl.img|fw.manifest|fw.manifest.tmp|REFUSED unexpected function | in |REFUSED missing label |wrote | bytes=| sha256=|RESULT OK|FAILED |makedir |write |rename |: |fwmanifest 1|fwversion |build_id |fw_file bytes=|fwimage offset=| size=|fwsignature .fwsignature_gb10y offset=|fmc_src |boot_src |bl_desc UCODE_DESC_PROD| version=| bootloader_off=| bootloader_size=| bl_param_off=| bl_param_size=| elf_off=| elf_size=| app_version=| manifest_off=| manifest_size=| mdata_off=| mdata_size=| mcode_off=| mcode_size=| monitor_enabled=| swbrom_code_off=| swbrom_code_size=| swbrom_data_off=| swbrom_data_size=| fb_reserved=| signed_as_code=|out |end|open |short read|REFUSED firmware|REFUSED bindata |" +fn fwpsetup to void +if eq get fwpok 0 1 +ret +end +put fwpok 0 1 +seg = ulong 0 +for i ulong len FWPTXT +c = get FWPTXT i +if eq c 124 +seg = add seg 1 +put fwpsgb seg add i 1 +put fwpsgl seg 0 +else +put fwpsgl seg add get fwpsgl seg 1 +end +end +end +fn fwpos k ulong to void +b = get fwpsgb k +for i ulong get fwpsgl k +fwoc get FWPTXT add b i +end +end +fn fwppath to void +for i ulong get fwppn 0 +fwoc get fwpp i +end +end +note fwpp = OUTDIR "/" segment k +fn fwpmk k ulong to void +n = get fwpoutn 0 +for i ulong n +put fwpp i get fwpout i +end +put fwpp n 47 +n = add n 1 +b = get fwpsgb k +for i ulong get fwpsgl k +put fwpp n get FWPTXT add b i +n = add n 1 +end +put fwppn 0 n +end +note bdpath = OGKMSRC "/src/nvidia/generated/" segment k +fn fwpbdp k ulong to void +n = get fwpsrcn 0 +for i ulong n +put bdpath i get fwpsrc i +end +for s ulong 2 +g = ulong 3 +if eq s 1 +g = k +end +b = get fwpsgb g +for i ulong get fwpsgl g +put bdpath n get FWPTXT add b i +n = add n 1 +end +end +put bdpathn 0 n +end +note one line to stderr: segment k, then the path in fwpath (when wp 1), then ": " strerror e (e > 0) +fn fwperr k ulong wp ulong e ulong to void +z = fwoflush +put fwofd 0 2 +fwpos k +if eq wp 1 +fwopath +end +if gt e 0 +fwpos 32 +fwoerrno e +end +fwoc 10 +z = fwoflush +put fwofd 0 1 +end +note arguments into fwpath (FWFILE), fwpsrc, fwpout: 0, or 1 on a usage error +fn fwpargs to ulong +if ne argcnt 4 +ret 1 +end +a = argread 1 fwpath 0 4096 +if lt a 1 +ret 1 +end +put fwpathn 0 ulong a +b = argread 2 fwpsrc 0 4000 +if lt b 1 +ret 1 +end +put fwpsrcn 0 ulong b +c = argread 3 fwpout 0 4000 +if lt c 1 +ret 1 +end +put fwpoutn 0 ulong c +ret 0 +end +note C cmd_inspect on FWFILE: 0 OK, 2 refused, 1 open or read error (C stderr wording) +fn fwpfw to ulong +fd = fopen fwpath 0 get fwpathn 0 524288 0 +if lt fd 0 +fwperr 66 1 ulong neg fd +ret 1 +end +s = fstat int fd fwpst 0 128 +if lt s 0 +c = fclose int fd +fwperr 66 1 ulong neg s +ret 1 +end +sz = ulong 0 +for i ulong 8 +sz = or sz shl ulong get fwpst add 48 i mul i 8 +end +md = ulong 0 +for i ulong 4 +md = or md shl ulong get fwpst add 16 i mul i 8 +end +note S_IFMT 0170000 = 61440, S_IFDIR 0040000 = 16384: a directory is "Is a directory" (21) +if eq and md 61440 16384 +c = fclose int fd +fwperr 66 1 21 +ret 1 +end +e = fwinsp int fd sz +c = fclose int fd +if eq e 1 +fwperr 67 0 0 +ret 1 +end +ret e +end +note does the label of block i equal FWPTXT segment k? +fn fwpleq i ulong k ulong to ulong +b = get fwpsgb k +l = get fwpsgl k +if ne get bdlbl i l +ret 0 +end +a = mul i 64 +m = ulong 1 +for j ulong l +if ne get bdlb add a j get FWPTXT add b j +m = 0 +end +end +ret m +end +note block whose label equals FWPTXT segment k, or BDNONE +fn fwpfind k ulong to ulong +for i ulong get bdn 0 +if eq fwpleq i k 1 +ret i +end +end +ret BDNONE +end +note "fwprep REFUSED missing label L in FILE" +fn fwpmiss k ulong f ulong to ulong +fwpos 1 +fwpos 23 +fwpos k +fwpos 22 +fwpos f +fwoc 10 +ret 2 +end +note record block for label segment k as output slot o; 2 (refused) when it is missing +fn fwpslot k ulong f ulong o ulong to ulong +i = fwpfind k +if eq i BDNONE +ret fwpmiss k f +end +put fwpoo o get bdpo i +put fwpon o get bdpn i +ret 0 +end +note extract bindata file f (segment 4 FMC or 5 boot) whose function must be segment fn +fn fwpbd f ulong fs ulong w ulong to ulong +fwpbdp f +e = bdfile +put fwpsz w get bdfsz 0 +for i ulong 64 +put fwpsh add mul w 64 i get bdfsh i +end +if ne e 0 +fwpos 1 +fwpos 69 +fwpos f +fwoc 10 +ret 2 +end +b = get fwpsgb fs +l = get fwpsgl fs +m = ulong 1 +if ne get bdfnl 0 l +m = 0 +else +for j ulong l +if ne get bdfn j get FWPTXT add b j +m = 0 +end +end +end +if eq m 0 +fwpos 1 +fwpos 21 +bdofn 0 +fwpos 22 +fwpos f +fwoc 10 +ret 2 +end +ret 0 +end +fn fwpfmc to ulong +e = fwpbd 4 6 0 +if ne e 0 +ret e +end +e = fwpslot 8 4 0 +if ne e 0 +ret e +end +e = fwpslot 9 4 1 +if ne e 0 +ret e +end +e = fwpslot 10 4 2 +if ne e 0 +ret e +end +ret fwpslot 11 4 3 +end +fn fwpboot to ulong +e = fwpbd 5 7 1 +if ne e 0 +ret e +end +e = fwpslot 12 5 4 +if ne e 0 +ret e +end +i = fwpfind 13 +if eq i BDNONE +ret fwpmiss 13 5 +end +put fwpdo 0 get bdpo i +ret 0 +end +note "fwprep FAILED : " on stdout +fn fwpfail k ulong e ulong to ulong +fwpos 1 +fwpos 28 +fwpos k +fwppath +fwpos 32 +fwoerrno e +fwoc 10 +ret 3 +end +note write bdpl[o..o+n) to OUTDIR/segment k: 0 or a positive errno (5 for a zero-byte write) +fn fwpwf k ulong o ulong n ulong to ulong +fwpmk k +fd = fopen fwpp 0 get fwppn 0 524865 420 +if lt fd 0 +ret ulong neg fd +end +q = ulong 0 +while lt q n bound 999999 +r = write int fd bdpl add o q sub n q +if lt r 1 +c = fclose int fd +if lt r 0 +ret ulong neg r +end +ret 5 +end +q = add q ulong r +end +c = fclose int fd +if lt c 0 +ret ulong neg c +end +ret 0 +end +note write output slot o as segment k and print its line; 0 or 3 +fn fwpput o ulong k ulong to ulong +e = fwpwf k get fwpoo o get fwpon o +if ne e 0 +ret fwpfail 30 e +end +bdsh256 get fwpoo o get fwpon o +shhex 32 +for i ulong 64 +put fwpoh add mul o 64 i get shhx i +end +fwpos 1 +fwpos 24 +fwpos k +fwpos 25 +fwodec get fwpon o +fwpos 26 +fwohx 32 +fwoc 10 +ret 0 +end +fn fwpohx o ulong to void +for i ulong 64 +fwoc get fwpoh add mul o 64 i +end +end +note little-endian u32 word w of the boot UCODE_DESC_PROD plain bytes +fn fwpw32 w ulong to ulong +p = add get fwpdo 0 mul w 4 +v = ulong 0 +for k ulong 4 +v = or v shl ulong get bdpl add p k mul k 8 +end +ret v +end +note manifest lines 1..6: the firmware and the two bindata inputs +fn fwpman1 to void +fwpos 33 +fwoc 10 +fwpos 34 +for i ulong get fwvern 0 +fwoc get fwver i +end +fwoc 10 +fwpos 35 +for i ulong get fwbidn 0 +fwoc get fwbidh i +end +fwoc 10 +fwpos 36 +fwodec get fwlen 0 +fwpos 26 +for i ulong 64 +fwoc get fwhxf i +end +fwoc 10 +fwpos 37 +fwodec get fwsof get fwimx 0 +fwpos 38 +fwodec get fwssz get fwimx 0 +fwpos 26 +for i ulong 64 +fwoc get fwhxi i +end +fwoc 10 +fwpos 39 +fwodec get fwsof get fwsx 0 +fwpos 38 +fwodec get fwssz get fwsx 0 +fwpos 26 +for i ulong 64 +fwoc get fwhxs i +end +fwoc 10 +for w ulong 2 +fwpos add 40 w +fwpos add 4 w +fwpos 25 +fwodec get fwpsz w +fwpos 26 +for i ulong 64 +fwoc get fwpsh add mul w 64 i +end +fwoc 10 +end +end +note manifest: the descriptor words, the outputs, "end" +fn fwpman2 to void +fwpos 42 +for w ulong 21 +fwpos add 43 w +fwodec fwpw32 w +end +fwoc 10 +for o ulong 5 +fwpos 64 +fwpos add 14 o +fwpos 25 +fwodec get fwpon o +fwpos 26 +fwpohx o +fwoc 10 +end +fwpos 65 +fwoc 10 +end +note write fw.manifest.tmp through the fw.om writer, then rename it to fw.manifest; 0 or 3 +fn fwpman to ulong +z = fwoflush +oe = get fwoerr 0 +fwpmk 20 +fd = fopen fwpp 0 get fwppn 0 524865 420 +if lt fd 0 +ret fwpfail 30 ulong neg fd +end +put fwofd 0 int fd +put fwoerr 0 0 +fwpman1 +fwpman2 +z = fwoflush +we = get fwoerr 0 +put fwofd 0 1 +put fwoerr 0 oe +c = fclose int fd +if eq we 1 +ret fwpfail 30 5 +end +if lt c 0 +ret fwpfail 30 ulong neg c +end +ret fwpren +end +note rename fw.manifest.tmp to fw.manifest (renameat2 packed list: u16 count, then u16 len + bytes) +fn fwpren to ulong +put fwprn 0 2 +put fwprn 1 0 +n = ulong 2 +for s ulong 2 +fwpmk sub 20 s +l = get fwppn 0 +put fwprn n uchar rem l 256 +put fwprn add n 1 uchar div l 256 +n = add n 2 +for i ulong l +put fwprn add n i get fwpp i +end +n = add n l +end +r = renames fwprn 0 n 0 +if lt r 0 +ret fwpfail 31 ulong neg r +end +fwpos 1 +fwpos 24 +fwpos 19 +fwoc 10 +ret 0 +end +note make OUTDIR (an existing one is fine) and write every output, the manifest last +fn fwpwrite to ulong +r = makedir fwpout 0 get fwpoutn 0 493 +if lt r 0 +if ne r -17 +for i ulong get fwpoutn 0 +put fwpp i get fwpout i +end +put fwppn 0 get fwpoutn 0 +ret fwpfail 29 ulong neg r +end +end +for o ulong 5 +e = fwpput o add 14 o +if ne e 0 +ret e +end +end +ret fwpman +end +fn fwprun to ulong +e = fwpfw +if eq e 2 +fwpos 1 +fwpos 68 +fwoc 10 +end +if ne e 0 +ret e +end +e = fwpfmc +if ne e 0 +ret e +end +e = fwpboot +if ne e 0 +ret e +end +e = fwpwrite +if ne e 0 +ret e +end +fwpos 1 +fwpos 27 +fwoc 10 +ret 0 +end +fn main to int +fwsetup +bdsetup +fwpsetup +if ne fwpargs 0 +fwperr 2 0 0 +ret 1 +end +fwpmk 19 +u = unlink fwpp 0 get fwppn 0 +e = fwprun +z = fwoflush +if and eq e 0 eq get fwoerr 0 1 +ret 3 +end +ret int e +end diff --git a/gsp/bindata.om b/gsp/bindata.om new file mode 100644 index 0000000..986331f --- /dev/null +++ b/gsp/bindata.om @@ -0,0 +1,1526 @@ +note gsp/bindata.om: the NVIDIA generated bindata reader in seeded Omega (OM-1). Ports the extract path +note of aienux PR #441 native/kernel/tools/ck_gsp_bindata_extract.c: refuse, next_line, after, +note parse_u32, copy_field, parse_hex_array, parse_pvt, parse_archive, parse_file, find_label, +note decode_desc, region_ok, check_semantics, report (stdout lines; no --out writing) and the read +note step of cmd_extract. Every refusal text is the C's, "line N: " prefix included. +note Library unit: no main. Prefix bd. Needs gsp/sha2.om, gsp/inflate.om and gsp/fw.om first +note (SHA-256/384, DEFLATE + CRC-32 + ISIZE, and the fwo* stdout writer). +note The text is read through a 1 MiB pread window (the C reads the whole file, at most 64 MiB, first; +note this port hashes the whole file first, so a read failure is still reported before any parse). +note Deviations (documented): arrays live in two 16 MiB pools, bdraw for compressed arrays as stored and +note bdpl for plain bytes (uncompressed arrays are parsed straight into bdpl). Running out of a pool is +note the C's own "out of memory for LABEL" refusal; the C would malloc up to MAX_ARRAY per array and +note DATA SIZE per inflated array. A directory path is refused as "File too large", the text the C +note prints on this host (glibc ftell on a directory). +const BDMAXB ulong 16 +const BDPOOL ulong 16777216 +const BDMAXA ulong 8388608 +const BDNONE ulong 999 +const BDUNS ulong 4294967295 +const BDWIN ulong 1048576 +global bdok ulong 1 +global bdsgb ulong 160 +global bdsgl ulong 160 +global bdpath uchar 4096 +global bdpathn ulong 1 +global bdfd int 1 +global bdst uchar 128 +global bdfsz ulong 1 +global bdfpos ulong 1 +global bdtp ulong 1 +global bdte ulong 1 +global bdtx uchar 1048576 +global bdln uchar 520 +global bdll ulong 1 +global bdlno ulong 1 +global bdrs uchar 256 +global bdrsn ulong 1 +global bdn ulong 1 +global bdarc ulong 1 +global bdcmp ulong 16 +global bdds ulong 16 +global bdcs ulong 16 +global bdhh ulong 16 +global bdhd ulong 16 +global bdhp ulong 16 +global bdnb ulong 16 +global bdbo ulong 16 +global bdpo ulong 16 +global bdpn ulong 16 +global bdfn uchar 1536 +global bdfnl ulong 16 +global bdlb uchar 1024 +global bdlbl ulong 16 +global bdfm uchar 2560 +global bdfml ulong 16 +global bdid uchar 3072 +global bdidl ulong 16 +global bdraw uchar 16777216 +global bdrtop ulong 1 +global bdpl uchar 16777216 +global bdptop ulong 1 +global bdimg uchar 64 +global bdimgn ulong 1 +global bdfsh uchar 64 +note segment k of BDTXT is the text between the k-th and (k+1)-th '|': 1..76 refusal pieces in C +note wording, 77..114 the C's line prefixes and tokens, 115.. report pieces +text BDTXT "|line |: |line longer than 511 characters|array size out of range for |expected '{' after the array declaration of |out of memory for |file ends inside the array of |non-hex token in the array of |byte token without a trailing comma in |more bytes than the declared size in |fewer bytes than the declared size in |expected '{' in the storage record of |storage record: bad uncompressed size for |storage record: uncompressed size differs from DATA SIZE for |storage record: bad compressed size for |storage record: compressed size differs from the header for |storage record: missing pointer line for |storage record: data pointer is not the array of |storage record: missing compressed flag for |storage record: bad compressed flag for |storage record: compressed flag disagrees with COMPRESSION for |second archive table|archive table name is not the file's function|expected '{' after the archive declaration|archive: bad entryNum|archive: entryNum differs from the number of arrays|archive: expected entries block|archive: expected the entries comment|archive: missing entry |archive: malformed entry for |archive: entry order or label differs from the arrays at |archive: entry without a storage index for |archive: storage index is not this array for |archive: expected end of entries|archive: expected end of table|array | has no data or no storage record|more than 16 arrays|FUNCTION line without a BINDATA_LABEL|FUNCTION line not terminated|bad function name|bad label|array belongs to a different function: |duplicate label |function is not a kgspGetBinArchive function: |identifier too long for |data before the first FUNCTION header|bad file name for |FILE TYPE is not BINARY for |COMPRESSION is neither YES nor NO for |COMPLEX_STRUCT is not NO for |bad DATA SIZE for |bad COMPRESSED SIZE for |second array for |array header incomplete for |COMPRESSED SIZE missing with COMPRESSION YES for |COMPRESSED SIZE given with COMPRESSION NO for |array identifier is not the expected one for |storage record before the array of |second storage record for |no arrays found|no archive table|descriptor is not 84 bytes: |descriptor version is not 5: |descriptor monitor not enabled: |descriptor without a matching image: |descriptor manifest region outside the image or unaligned: |descriptor monitor-data region outside the image or unaligned: |descriptor monitor-code region outside the image or unaligned: |hash array is not 48 bytes|hash array without UCODE_IMAGE|UCODE_HASH is not SHA-384 of UCODE_IMAGE| is not 384 bytes|unknown label | (|)|// FUNCTION: |(\"BINDATA_LABEL_|\")|kgspGetBinArchive|kgspBinArchive|_BINDATA_LABEL_|_data|// FILE NAME: |// FILE TYPE: |BINARY|// COMPRESSION: |YES|NO|// COMPLEX_STRUCT: |// DATA SIZE (bytes): |// COMPRESSED SIZE (bytes): |N/A|static BINDATA_CONST NvU8 |[] = |#if defined(BINDATA_INCLUDE_STORAGE_PVT_DEFN)|static const BINDATA_ARCHIVE __|static BINDATA_CONST|static const BINDATA_ARCHIVE|{|};|NV_TRUE,|NV_FALSE,| =|// entries[]|{ BINDATA_LABEL_|&g_bindata_pvt[__idx_|}|UCODE_DESC|UCODE_IMAGE|UCODE_HASH|UCODE_SIG|UCODE_PKEY|//|function | arrays=|desc | version=| manifest=0x|+0x| monitor_data=0x| monitor_code=0x| monitor_enabled=| fb_reserved=0x| signed_as_code=| app_version=| image=|/|hash384 UCODE_HASH equals sha384(UCODE_IMAGE)| file=| stored=| compressed=|yes|no| size=| sha256=|RESULT OK arrays=|REFUSED: |REFUSED: cannot read | |" +note build the segment table and reset the pools (call once per program) +fn bdsetup to void +if eq get bdok 0 1 +ret +end +put bdok 0 1 +fwsetup +infsetup +seg = ulong 0 +for i ulong len BDTXT +c = get BDTXT i +if eq c 124 +seg = add seg 1 +put bdsgb seg add i 1 +put bdsgl seg 0 +else +put bdsgl seg add get bdsgl seg 1 +end +end +put bdrtop 0 0 +put bdptop 0 0 +end +note ---- printing through the fw.om writer ---- +fn bdos k ulong to void +b = get bdsgb k +for i ulong get bdsgl k +fwoc get BDTXT add b i +end +end +fn bdolab i ulong to void +b = mul i 64 +for j ulong get bdlbl i +fwoc get bdlb add b j +end +end +fn bdofn i ulong to void +b = mul i 96 +for j ulong get bdfnl i +fwoc get bdfn add b j +end +end +fn bdofm i ulong to void +b = mul i 160 +for j ulong get bdfml i +fwoc get bdfm add b j +end +end +fn bdopath to void +for i ulong get bdpathn 0 +fwoc get bdpath i +end +end +note ---- the refusal reason (C f->reason, 256 bytes: at most 255 characters kept) ---- +fn bdrc c uchar to void +n = get bdrsn 0 +if lt n 255 +put bdrs n c +put bdrsn 0 add n 1 +end +end +fn bdrseg k ulong to void +b = get bdsgb k +for i ulong get bdsgl k +bdrc get BDTXT add b i +end +end +fn bdrlab i ulong to void +b = mul i 64 +for j ulong get bdlbl i +bdrc get bdlb add b j +end +end +fn bdrfn i ulong to void +b = mul i 96 +for j ulong get bdfnl i +bdrc get bdfn add b j +end +end +note "line N: " (C refuse: N is the cursor's line count, 0 when the cursor is NULL) +fn bdrhd ln ulong to void +put bdrsn 0 0 +bdrseg 1 +d = new uchar 24 0 +n = ulong 0 +x = ln +while lt n 24 bound 24 +put d n add uchar rem x 10 48 +n = add n 1 +x = div x 10 +if eq x 0 +break +end +end +while gt n 0 bound 24 +n = sub n 1 +bdrc get d n +end +bdrseg 2 +end +note refuse at the current line with reason k, then the label of block b unless b is BDNONE +fn bdrl k ulong b ulong to ulong +bdrhd get bdlno 0 +bdrseg k +if ne b BDNONE +bdrlab b +end +ret 2 +end +note refuse at the current line with reason k and the function name of block b +fn bdrf k ulong b ulong to ulong +bdrhd get bdlno 0 +bdrseg k +bdrfn b +ret 2 +end +note refuse with line 0 (semantic checks) with reason k, then the label of b unless BDNONE +fn bdr0 k ulong b ulong to ulong +bdrhd 0 +bdrseg k +if ne b BDNONE +bdrlab b +end +ret 2 +end +note "array LABEL has no data or no storage record" at the current line +fn bdrnd b ulong to ulong +bdrhd get bdlno 0 +bdrseg 36 +bdrlab b +bdrseg 37 +ret 2 +end +note ---- the text window and C next_line ---- +note keep at least 512 unread bytes in bdtx unless the file is used up: 0, or 3 on a read error +fn bdfill to ulong +tp = get bdtp 0 +av = sub get bdte 0 tp +if ge av 512 +ret 0 +end +fp = get bdfpos 0 +if ge fp get bdfsz 0 +ret 0 +end +for i ulong av +put bdtx i get bdtx add tp i +end +put bdtp 0 0 +put bdte 0 av +w = sub BDWIN av +lf = sub get bdfsz 0 fp +if lt lf w +w = lf +end +q = ulong 0 +while lt q w bound 999999 +r = pread get bdfd 0 bdtx add av q sub w q long add fp q +if lt r 1 +ret 3 +end +q = add q ulong r +end +put bdte 0 add av w +put bdfpos 0 add fp w +ret 0 +end +note C next_line into bdln[0..bdll): 1 = a line, 0 = end of text, 2 = longer than 511 characters +note (reason set, line count advanced), 3 = read error. A trailing CR is dropped and the line ends +note at its first NUL (the C's string view of its buffer). +fn bdnext to ulong +e = bdfill +if ne e 0 +ret 3 +end +tp = get bdtp 0 +te = get bdte 0 +if ge tp te +ret 0 +end +lim = add tp 512 +if gt lim te +lim = te +end +j = tp +nl = ulong 0 +while lt j lim bound 600 +if eq get bdtx j 10 +nl = 1 +break +end +j = add j 1 +end +put bdlno 0 add get bdlno 0 1 +n = sub j tp +if ge n 512 +bdrhd get bdlno 0 +bdrseg 3 +ret 2 +end +for i ulong n +put bdln i get bdtx add tp i +end +if gt n 0 +if eq get bdln sub n 1 13 +n = sub n 1 +end +end +k = ulong 0 +while lt k n bound 600 +if eq get bdln k 0 +break +end +k = add k 1 +end +put bdln k 0 +put bdll 0 k +if eq nl 1 +put bdtp 0 add j 1 +else +put bdtp 0 te +end +ret 1 +end +note ---- string helpers over the line ---- +fn bdlc i ulong to uchar +if ge i get bdll 0 +ret 0 +end +ret get bdln i +end +note does the line at p start with segment k (C after / strncmp)? +fn bdpre p ulong k ulong to ulong +b = get bdsgb k +l = get bdsgl k +if gt add p l get bdll 0 +ret 0 +end +for i ulong l +if ne get bdln add p i get BDTXT add b i +ret 0 +end +end +ret 1 +end +note is the line from p to its end exactly segment k (C strcmp)? +fn bdeqs p ulong k ulong to ulong +L = get bdll 0 +if gt p L +ret 0 +end +if ne sub L p get bdsgl k +ret 0 +end +ret bdpre p k +end +note C strspn(line + p, " ") +fn bdsp p ulong to ulong +q = p +L = get bdll 0 +while lt q L bound 600 +if ne get bdln q 32 +break +end +q = add q 1 +end +ret q +end +note first position at or after p where segment k occurs (C strstr), BDNONE if none +fn bdfind p ulong k ulong to ulong +q = p +L = get bdll 0 +while le q L bound 600 +if eq bdpre q k 1 +ret q +end +q = add q 1 +end +ret BDNONE +end +note first '(' at or after p (C strchr), BDNONE if none +fn bdfpar p ulong to ulong +q = p +L = get bdll 0 +while lt q L bound 600 +if eq get bdln q 40 +ret q +end +q = add q 1 +end +ret BDNONE +end +note C parse_u32 at p: decimal at most 4294967295, then spaces, commas or tabs, then the end or "//" +fn bdu32 p ulong to long +c = bdlc p +if lt c 48 +ret -1 +end +if gt c 57 +ret -1 +end +acc = ulong 0 +q = p +while lt q get bdll 0 bound 600 +c = get bdln q +if lt c 48 +break +end +if gt c 57 +break +end +acc = add mul acc 10 sub ulong c 48 +if gt acc BDUNS +ret -1 +end +q = add q 1 +end +while lt q get bdll 0 bound 600 +c = get bdln q +if and and ne c 32 ne c 44 ne c 9 +break +end +q = add q 1 +end +if lt q get bdll 0 +if eq bdpre q 114 0 +ret -1 +end +end +ret long acc +end +note does the line at p hold the first n characters of block i's identifier? +fn bdlnid p ulong i ulong n ulong to ulong +if gt add p n get bdll 0 +ret 0 +end +b = mul i 192 +for j ulong n +if ne get bdln add p j get bdid add b j +ret 0 +end +end +ret 1 +end +note does the line at p start with block i's label? +fn bdlnlb p ulong i ulong to ulong +n = get bdlbl i +if gt add p n get bdll 0 +ret 0 +end +b = mul i 64 +for j ulong n +if ne get bdln add p j get bdlb add b j +ret 0 +end +end +ret 1 +end +note does the line at p start with block i's function name? +fn bdlnfn p ulong i ulong to ulong +n = get bdfnl i +if gt add p n get bdll 0 +ret 0 +end +b = mul i 96 +for j ulong n +if ne get bdln add p j get bdfn add b j +ret 0 +end +end +ret 1 +end +note labels of blocks i and j equal? +fn bdlbeq i ulong j ulong to ulong +n = get bdlbl i +if ne n get bdlbl j +ret 0 +end +a = mul i 64 +b = mul j 64 +for k ulong n +if ne get bdlb add a k get bdlb add b k +ret 0 +end +end +ret 1 +end +note function names of blocks i and j equal? +fn bdfneq i ulong j ulong to ulong +n = get bdfnl i +if ne n get bdfnl j +ret 0 +end +a = mul i 96 +b = mul j 96 +for k ulong n +if ne get bdfn add a k get bdfn add b k +ret 0 +end +end +ret 1 +end +note label of block i starts with segment k (sw 0) or equals it (sw 1)? +fn bdlbsg i ulong k ulong sw ulong to ulong +n = get bdlbl i +l = get bdsgl k +if lt n l +ret 0 +end +if eq sw 1 +if ne n l +ret 0 +end +end +a = mul i 64 +b = get bdsgb k +for j ulong l +if ne get bdlb add a j get BDTXT add b j +ret 0 +end +end +ret 1 +end +note function name of block i starts with segment k? +fn bdfnsg i ulong k ulong to ulong +n = get bdfnl i +l = get bdsgl k +if lt n l +ret 0 +end +a = mul i 96 +b = get bdsgb k +for j ulong l +if ne get bdfn add a j get BDTXT add b j +ret 0 +end +end +ret 1 +end +note C find_label for the name in bdimg[0..bdimgn): block index or BDNONE +fn bdfimg to ulong +for i ulong get bdn 0 +n = get bdlbl i +if eq n get bdimgn 0 +a = mul i 64 +m = ulong 1 +for j ulong n +if ne get bdlb add a j get bdimg j +m = 0 +end +end +if eq m 1 +ret i +end +end +end +ret BDNONE +end +note C find_label for segment k: block index or BDNONE +fn bdfseg k ulong to ulong +for i ulong get bdn 0 +if eq bdlbsg i k 1 1 +ret i +end +end +ret BDNONE +end +note ---- C copy_field into the block's fields (len checked by the caller) ---- +fn bdsetfn i ulong p ulong n ulong to void +b = mul i 96 +for j ulong n +put bdfn add b j get bdln add p j +end +put bdfnl i n +end +fn bdsetlb i ulong p ulong n ulong to void +b = mul i 64 +for j ulong n +put bdlb add b j get bdln add p j +end +put bdlbl i n +end +fn bdsetfm i ulong p ulong n ulong to void +b = mul i 160 +for j ulong n +put bdfm add b j get bdln add p j +end +put bdfml i n +end +note append segment k to block i's identifier at q; returns the new end +fn bdidsg i ulong q ulong k ulong to ulong +b = mul i 192 +s = get bdsgb k +for j ulong get bdsgl k +put bdid add b add q j get BDTXT add s j +end +ret add q get bdsgl k +end +note identifier kgspBinArchive_BINDATA_LABEL_