Writing a driver
Safe hardware access, and how to survive a restart.
Preview interface
The app developer kit (SDK) is not public yet. The snippets here show the shape of the interface described in the v0.1 specification. Names, numbers and signatures can change before release. For early access, write to [email protected].
A driver in DurgX is a normal limited program. It never gets blanket hardware access. It gets exactly the capabilities for the device it manages.
What a driver is given
| Capability | What it is for |
|---|---|
| Device memory (Frame) | The device's registers, mapped into the driver's memory. |
| Interrupt (Irq) | A signal from the device. The driver waits for it; it never touches the interrupt table. |
| DMA region | A buffer the device is allowed to read and write. |
| Ring to clients | How other programs submit requests to the driver. |
The Device Manager, a trusted but unprivileged program, finds your hardware once at boot, then hands each driver only its own capabilities.
The shape of a driver
driver outline (illustrative)
fn driver_main(caps: DriverCaps) -> ! {
let regs = map_device_memory(caps.mmio); // SYS_VM_MAP
let ring = attach_ring(caps.client_ring); // requests from clients
loop {
// 1. Sleep until the device raises its interrupt.
irq_wait(caps.irq); // behaves like a message wait
// 2. Acknowledge the device and collect finished work.
let done = regs.take_completions();
// 3. Post results to the clients' completion queue.
for c in done { ring.complete(c.user_data, c.result); }
// 4. Start any new requests that arrived.
while let Some(req) = ring.next_request() { regs.start(req); }
}
}Design it to be restarted
- Be idempotent. After a restart, the same request may be replayed. Doing it twice must be safe.
- Keep state outside. Queues of unfinished work should live where a fresh copy of the driver can pick them up.
- Reset the device cleanly at the start, so a half-finished operation does not confuse it.
- Never assume you are the first run. Check the device state, then continue.
What the supervisor receives when you crash
| Field | Meaning |
|---|---|
| task id | Which program crashed. |
| fault reason | Memory error, illegal instruction, permission violation and so on. |
| fault address | The address involved, if any. |
| registers | A snapshot of the CPU registers for debugging. |
The supervisor can then restart you, switch you off or escalate. See Crashes and recovery.