Skip to content

Architecture

KDAL is a six-layer kernel framework plus a domain-specific language (DSL) toolchain that decouples device drivers from hardware specifics, enabling portable driver development across embedded platforms.

flowchart TB US["User space<br/>kdality (CLI), testapp and benchmark (tests/userspace),<br/>kdaltest (tests/integration)<br/>(implemented)"] CP["Control plane: src/core/<br/>chardev.c: /dev/kdal, ioctl dispatch<br/>debugfs.c: introspection and the demand report"] CORE["Core runtime: src/core/<br/>registry, event, power, demand, kdal, module"] DRV["Example drivers: src/drivers/example/<br/>uart, i2c, spi, gpu (accelerator ops)"] BE["Backends: src/backends/<br/>generic: devicetree, platdev, socglue<br/>qemu: qemubackend, virtioio, virtioaccel, mmio"] HW["Devices: MMIO, platform devices,<br/>device tree nodes, virtio"] US -->|ioctl, read, write| CP CP --> CORE CORE --> DRV DRV --> BE BE --> HW classDef observed stroke:#0ca30c,stroke-width:2px classDef implemented stroke:#fab219,stroke-width:2px class CP,CORE,DRV,BE observed class US implemented

The same diagram as KDAL’s README (key: Start here, Reading the diagrams): green parts are built into kdal.ko, which CI loads and tests with its KUnit suites; the user-space programs build, but CI does not run them against the module.

compiler/, lang/

  • .kdh device headers - declarative register maps, signals, capabilities, power states
  • .kdc driver files - imperative event handlers (probe, read, write, signal, power)
  • Compiler pipeline - lexer → parser → AST → semantic analysis → C code generation
  • Standard library - 7 .kdh files in lang/stdlib/ (common, UART, I2C, SPI, GPIO, GPU, NPU)
  • Output - kernel C (platform_driver, devm_*, OF match table) + Makefile.kbuild; the generated module does not build yet (open finding O7)

include/kdal/

Stable API contracts that drivers and backends program against:

Header Purpose
types.h Device classes, power states, event types, core structs, struct kdal_demand
api/driver.h Conventional driver ops (probe/remove/read/write/ioctl)
api/accel.h Accelerator ops (queue/buffer/submit)
backend.h Backend ops (init/exit/enumerate/read/write/ioctl)
ioctl.h Userspace ABI (ioctl magic 'K')
core/kdal.h Lifecycle, registration, lookup, the resource demand

src/core/

Module Purpose
module.c Kernel module entry/exit
kdal.c Init cascade (chardev → debugfs → backends → drivers), attach and lookup
chardev.c /dev/kdal misc device with per-fd state
debugfs.c /sys/kernel/debug/kdal/ diagnostic files
registry.c Thread-safe registration of backends/drivers/devices
event.c Bounded circular event log (256 entries, mutex); wakes a wait queue nothing waits on yet
power.c Power state transitions with event emission
demand.c The resource demand as JSON (kdal_demand_format)

src/backends/

Backend Description
qemu/ QEMU virt machine backend with software ring buffers (qemubackend.c), virtio I/O and accelerator, MMIO
generic/ Platform device, Device Tree (kdal,device nodes), and SoC glue for real hardware

src/drivers/example/

Example drivers that find their backend through the registry (kdal_find_backend):

Driver File Description
UART uartdriver.c Byte-stream I/O; a baud rate field, set to its default at probe
I2C i2cdriver.c Bus speed (100 kHz default) and slave address
SPI spidriver.c SPI emulation with a bus clock field
GPU gpudriver.c Accelerator driver with queue/buffer semantics

tests/, tools/

  • KUnit test suites for kernel-side validation
  • Userspace integration tests and benchmarks
  • kdality CLI for device management and driver compilation
write(fd, "hello", 5)
→ kdal_cdev_write() [chardev.c]
→ file_ctx->selected->driver->ops->write() [uartdriver.c]
→ copy_from_user() into kernel buffer
→ backend->ops->write() [qemubackend.c]
→ qemu_ring_write() [ring buffer]
read(fd, buf, 5)
→ kdal_cdev_read() [chardev.c]
→ file_ctx->selected->driver->ops->read() [uartdriver.c]
→ backend->ops->read() [qemubackend.c]
→ qemu_ring_read() [ring buffer]
→ copy_to_user() back to userspace

Module load triggers kdal_core_init() which brings up subsystems in order:

  1. kdal_chardev_init() - registers /dev/kdal
  2. kdal_debugfs_init() - creates debugfs tree (non-fatal if unavailable)
  3. kdal_qemu_backend_init() - registers + inits QEMU backend
  4. kdal_soc_glue_init(), kdal_generic_platdev_init(), kdal_devicetree_scan() - the generic backend and the devices described in the device tree
  5. kdal_uart_driver_init() - registers driver + device + attaches
  6. kdal_i2c_driver_init() - same pattern
  7. kdal_spi_driver_init() - same pattern
  8. kdal_gpu_driver_init() - same pattern

A failure unwinds the steps already done. Teardown in kdal_core_exit() reverses this order and then resets the event log.

Decision Rationale
misc_register over raw char dev Simpler, automatic minor allocation
Per-fd state via file->private_data Independent device selection per open
Ring buffer over DMA No real hardware on QEMU first milestone
Mutex over spinlock in registry Sleep-safe for module init path
Circular event log Bounded memory, no allocation per event
Forward-declared driver inits Avoids header coupling between drivers