Repository navigation
Home
How the aiBSD (NetBSD-derived) source tree and build work, how to hack
on the kernel, and what we learned bringing Linux DRM v7.3 to it. The
kernel API reference stays in the section 9 man pages
(share/man/man9); this guide explains and links to them rather than
repeating them.
Status markers: [written] = a full chapter; [stub] or abridged = verified facts, names and source pointers, enough to be written out later without re-research; [notes] = material gathered and partly written up; [todo] = not yet learned.
Start here:
- Tutorial: porting a Linux API — one real piece of work, end to end
- Part III, kernel fundamentals, NetBSD and Linux side by side: Kernel programming basics, Threads, processes and tasks, Devices and drivers, PCI, Virtual memory, Memory and DMA, Firmware, Exporting state and events, Booting and kernel modules, The console, Power management
- Building, Testing, The Linux compatibility layer, The DRM integration, Working with upstreams
- Appendices: Glossary, Source map
This wiki is a git repository (<repository>.wiki.git); pages are
Markdown files, and images or other files can be committed next to
them and linked relatively.
-
Orientation [notes]
- aiBSD vs NetBSD: what the fork is, what changes, what stays compatible
- Top-level layout:
sys/,lib/,bin/,usr.*,external/,distrib/,etc/,share/,tests/,tools/ -
external/andsys/external/: third-party code by license (bsd,mit,gpl2,gpl3,cddl, ...), vendor imports and local glue
-
Source control [notes]
- git, not CVS: branches, the
trunkbranch, topic branches - Vendor branches in git (
vendor/linux-drm, tagslinux-drm-vX.Y.Z), merging imports into a working branch -
$NetBSD$RCS ids: kept in existing files, not added to new ones - Local-only aids via
.git/info/exclude
- git, not CVS: branches, the
-
Licensing [notes]
- NetBSD's no-GPL-in-the-kernel rule and how aiBSD differs
- Reading SPDX expressions (
GPL-2.0 OR MITvsGPL-2.0 AND MIT) - GPL-only DRM files: build switch, allowlist,
tools/license-check - CDDL (ZFS, DTrace) next to GPL code
- Clean-room rule for compat code: write from documented behaviour, never translate GPL sources
-
build.sh [notes]
- Tools, distribution, release, kernel, live-image targets
-
-O/-Tobject and tool dirs,-uupdates,-j - Host compilers: building tools with clang vs gcc; the MPFR
_Float128failure and--disable-float128; never mixing compilers in one tools dir -
-Voverrides (MKKMOD=no,KERNEL_SETS,KERN_SET, ...) -
CTF: type data for DTrace [written]:
ctfconvert/ctfmergefrom illumos, the BTF-based merge, strict and lenient conversion, how the kernel, fbt and libdtrace read CTF; section renumbering when rewriting ELF files
-
Kernel configuration [notes]
- Config files,
include/no,options, attachments -
config(8)/nbconfig:files.*,define,file,makeoptions,defflag; per-fileCWARNFLAGS/COPTS - Why basenames must be unique across a kernel
- Kernel modules (
sys/modules) vs built-in drivers
- Config files,
-
Set lists and mtree [notes]
-
distrib/sets/lists/*, marking removed files obsolete etc/mtree/NetBSD.dist.*- Debug sets
-
-
Images and VMs [notes]
- The amd64 live image (GPT, EFI,
resize_gpt/resize_root) - The test VMs: q35, UEFI without secure boot, NVMe, TPM 2.0 over TIS (why not CRB), virtio-gpu, no vIOMMU
- Growing disks; GPU passthrough candidates [todo]
- The amd64 live image (GPT, EFI,
Each chapter: what NetBSD has (structures, handles, lifetimes), what Linux has for the same job, and how the compat layer makes the Linux one appear on NetBSD. [stub] chapters hold verified facts and source pointers, to be written out.
-
Kernel programming basics [written]
- Execution contexts and "may I sleep here?"; KNF,
KASSERT,DIAGNOSTIC; NetBSD positive vs Linux negative errnos
- Execution contexts and "may I sleep here?"; KNF,
-
Synchronization (in kernel basics) [written]
-
mutex(9)adaptive vs spin, IPLs,MUTEX_NODEBUGand LOCKDEBUG;condvar(9);pserialize(9),psref(9),localcount(9), RCU emulation; atomics and memory ordering, with worked examples
-
-
Threads, processes and tasks [written]
-
proc/lwp, specificdata, kthreads and joining, exit order; Linux tasks, pids, kthreads; per-LWP tasks in the compat layer
-
-
Devices and drivers [written]
- why devices form a tree, enumerated vs described hardware,
matching and binding, why detaching is the hard part; autoconf
(
cfdriver,cfattach,device_t, softc, deferred attach, references); Linux's driver model (kobject, device, driver, bus, class, devres); the twostruct devices
- why devices form a tree, enumerated vs described hardware,
matching and binding, why detaching is the hard part; autoconf
(
-
Files, descriptors and device nodes [written]
-
struct file,fileops, descriptors, the cdevsw and cloning devices (fd_clone,EMOVEFD); Linux's file/inode/file_operations,replace_fops; Linux files on NetBSD and/dev/dri; select/poll, kqueue, epoll, and kqueue on Linux poll methods
-
-
PCI [written]
- how PCI Express works: topology, config space (mechanism #1,
ECAM), enumeration, BAR sizing and windows, posted writes and
read flushes, MSI/MSI-X as memory writes, resizable BAR, links;
attach args,
bus_space(9), interrupts,struct pci_devbuilt on NetBSD
- how PCI Express works: topology, config space (mechanism #1,
ECAM), enumeration, BAR sizing and windows, posted writes and
read flushes, MSI/MSI-X as memory writes, resizable BAR, links;
attach args,
-
Virtual memory, from the ground up [written]
- address spaces, map entries, UVM's two layers (objects, amaps of anons), copy-on-write, the fault path and its locking, the life of a page, the page daemon (clock, intents), swap and where slots are recorded; Linux's mm side by side; how the compat layer sits on UVM
- Reading: Cranor's UVM thesis; Gorman's Linux VM book
-
Memory and DMA [written]
-
kmem(9), pools, UVM pages and objects,bus_dma(9); Linux allocators, pages and folios, shmem, the DMA API; mapping memory into processes (pgo_faultvs VMAs and fault handlers), taking mappings back (pv lists), caching attributes, shrinkers - Reading: Cranor's UVM thesis; Thorpe's
bus_dmapaper
-
-
Page tables, TLBs and memory types [written]
- x86-64 paging and entry bits, large pages, TLBs and shootdowns, PAT/MTRR memory types and the aliasing rule; NetBSD's pmap, the recursive mapping and the direct map; per-page cache attributes (M0) as worked example; pitfall: stale translations of page tables
- Reading: Intel SDM vol. 3 ch. 4 and 11; FreeBSD's amd64 pmap
-
Firmware [written]
-
firmload(9)vsrequest_firmware(); xz-compressed images (XZ Embedded, single-call); GPU firmware from linux-firmware per what drivers declare (MODULE_FIRMWARE), sets, builds without it
-
-
Exporting state and events [written]
- sysctl, drvctl/devpubd, sysmon vs sysfs, uevents, udev
-
Power management [written; Linux side abridged]
-
pmf(9)vsdev_pm_opsand runtime PM - Reading: Kunz, Writing Drivers for NetBSD
-
Also in Part III (unnumbered so the numbering below stays stable):
-
Booting and kernel modules [written]
- boot loader to kernel (bootinfo, priming), module sources and
classes, init order around autoconf,
_MODULEpitfalls, module properties fromboot.cfg, Linux module parameters as sysctl
- boot loader to kernel (bootinfo, priming), module sources and
classes, init order around autoconf,
-
The console: wscons, vcons, genfb and DRM [written]
- the layers, tty window size and
SIGWINCH, resizing in place, drmfb and the DRM console client; at boot, from the firmware's framebuffer to DRM (early console, match priorities, probe after root, takeover and give-back)
- the layers, tty window size and
-
How the layer works [notes]
- Header overrides first, then upstream;
linux_*symbol renaming - Matching Linux's transitive includes (
pm.h,device.h,kernel.h) - Namespace clashes:
ALIGN,struct rb_node/rb_left,RB_ROOT,free -
IS_ENABLEDwith Linux semantics (undefined = off)
- Header overrides first, then upstream;
-
Design notes per API [notes]
- Wait queues: generation counter, conditions evaluated unlocked, no
lost wakeups, no destroy,
selinfohook for poll -
cleanup.h:guard,scoped_guard,__free,CLASS - Red-black trees: textbook algorithm on NetBSD's
struct rb_node, augmented callbacks, interval trees -
dma_resv: usage model, packed entries, lockless iteration, poll - Fences (inline locks, 64-bit seqnos, detaching ops) and fence arrays
- Tasks: per-thread
task_struct, process pids, atomic context, exit order -
kthread_workeron ordered workqueues -
ww_mutexwith acquire contexts;iosys_map; allocation helpers - Stubs and why: device tree, debugfs/sysfs, dynamic debug, cgroups, kunit, eventfd
- Wait queues: generation counter, conditions evaluated unlocked, no
lost wakeups, no destroy,
-
Open problems [notes]
- GEM backing store: Linux shmem/folios over UVM
- Task state for sleeping (
set_current_state,wake_up_process) - File-descriptor layer
-
History and the cost of the old approach [notes]
- NetBSD's 5.6 import: edits in place, 1,725
#ifdefs, unmarked edits - Measuring the delta against pristine Linux; classifying hunks
- What happened to those files upstream by 7.x (rewritten/removed)
- NetBSD's 5.6 import: edits in place, 1,725
-
The pristine-
dist/model [notes]- Rules: never edit
dist/; compat first, glue second, replacement files last; generated renames; generated files outsidedist/ -
tools/import,tools/verify-dist,tools/paths - Importing a new Linux release step by step
- Shadowing headers: finding them, MIT → upstream, GPL → BSD replacement with the same API, deriving an API cleanly
- Stub policy: fail cleanly, do nothing, or panic — chosen by what breaks
- Rules: never edit
-
Build lists [notes]
-
drm2netbsd: evaluating Kbuild, composite objects, NetBSD replacements, skipped objects and why
-
-
What was removed and why [notes]
- Legacy DRM (
drmums), VIA, AGP ioctls; AGP still works for radeon/nouveau
- Legacy DRM (
-
Drivers [todo]
- i915, amdgpu, radeon, nouveau, vmwgfx; firmware; Rust drivers (nova, tyr) and what supporting them would take
-
Backlog: from working to fully capable
- Every stand-in, what replacing it takes; core changes; gates before real hardware
-
GPU drivers: lessons from amdgpu
- A GPU as many processors; rings, doorbells and fences
- Memory domains, memory types and the GPU's own MMU
- Failure handling; code shared across operating systems; what porting it teaches; a reading order
-
Compile testing [notes]
- The syntax probe (
tools/probe, temporary) and reading its output - Cross-port kernel builds [todo]
- The syntax probe (
-
Unit tests [notes]
- ATF; rump kernels; private rump libraries and
rumpns_symbols (tests/dev/usb/t_hid,tests/dev/drm) - A rump kernel on the build host (Linux), AddressSanitizer
- Rump pitfalls: 2 vCPUs, no preemption, cached
curlwp, implicit lwps - Writing stress tests that catch things
- Host-side randomized harnesses
- ATF; rump kernels; private rump libraries and
-
System tests [notes]
- Booting a built image in the VM; running tests unattended; collecting results over the serial console
- The dev VM: install once, copy and run tests over ssh
-
What each kind of test finds [written]
- Contracts, locking, randomized models, postconditions, stress loops, a second kernel, pmap stress, module loading
-
Reporting upstream [notes]
- Verify against current upstream first; describe fixes in words; disclose AI assistance; say whether reproduced
- Where: NetBSD send-pr/gnats, Linux dri-devel / freedesktop GitLab
- Drafts are kept outside the tree
-
Tracking upstream NetBSD [notes]
-
trunkmirrors NetBSD; sync, review rewritten areas, merge -
Local changes to imported code:
written as upstream code, no labels; found with
git log -- distandREADME.aibsd
-
- Porting a Linux API to the compat layer: the task API end to end — find the gap, learn the semantics cleanly, map onto NetBSD, implement, test in three loops, commit; plus a GPL-shadowed header [written]
- A. Glossary [notes]
- B. Further reading [notes]: section 9 man pages; Thorpe (bus_dma);
Cranor (UVM); Kantee (rump/anykernel); Kunz (drivers); McKusick et al.
(4.4BSD, FreeBSD); NetBSD Guide and Internals (in NetBSD's
htdocsrepository, not this one); tech-kern archives - C. Source map: what is where in NetBSD's sources, and what we verified there (gotchas marked) [notes]
aiBSD system development guide
Tutorial
II — Building
III — The kernel
- Kernel basics
- Booting and kernel modules
- Threads, processes, tasks
- Devices and drivers
- Files, descriptors, device nodes
- PCI
- Virtual memory
- Memory and DMA
- Page tables, TLBs, memory types
- Firmware
- State and events
- The console
- Power management
IV — Linux compat layer
V — DRM
VI — Testing
VII — Upstreams
Appendices