# 09. Runtime, FFI and ABI ## 1. Runtime principle The runtime should be small, explicit and auditable. Core responsibilities may include: - allocator adapters; - task scheduler; - synchronization primitives; - I/O integration; - capability materialization; - platform intrinsics; - deterministic/replay hooks. ## 2. System calls Direct syscalls are runtime/unsafe-only by default. A syscall wrapper must describe: - argument types; - memory read/write regions; - created/consumed resources; - effects; - failure behavior; - blocking behavior. ## 3. FFI Foreign calls are explicit and unsafe unless wrapped in a verified safe abstraction. Illustrative declaration: ```aji extern "C" unsafe fn read( fd: i32, buf: addr, len: usize ) -> isize effects [ffi]; ``` Safe wrappers must state and uphold ownership, aliasing and thread-safety assumptions. ## 4. Volatile `volatile.load` and `volatile.store` affect compiler access elimination/reordering as defined by the volatile contract. Volatile does not itself imply atomicity or inter-thread synchronization. ## 5. MMIO Device memory should use a distinct `Mmio` abstraction obtained through `cap`. CPU-memory atomics and device-ordering barriers are separate concepts. ## 6. DMA DMA buffers are explicitly pinned. A buffer cannot be freed, reset or physically relocated while a device operation owns it. ## 7. Signal/interrupt profiles Signal and interrupt handlers use restricted capabilities/effects. Allocation, ordinary locking and blocking I/O are forbidden unless a profile explicitly defines a safe mechanism. ## 8. Backend The initial native backend should lower AIR to LLVM IR. A custom native backend is deferred until AIR and verifier semantics stabilize. ## 9. Target description Every execution, interpreted or native, runs against an explicit target description. Target-dependent semantics come only from it. Format version 0 defines one field: | Field | Meaning | |---|---| | `pointer_bits` | width of `usize`/`isize` in bits: 32 or 64 | | `atomic_widths` | the AtomicScalar widths, in bits, that the target implements natively and lock-free (feature `atomics`, spec 04 ยง23) | A module that uses `atomic` for a width the target does not list is rejected for that target at build time (`E4203`, `atomic.unsupported_width`). An implementation MUST NOT emulate an atomic with a lock. The reference interpreter uses the target `air64` (`pointer_bits = 64`, `atomic_widths = {8, 16, 32, 64}`) unless a module declares target requirements (a later format version).