/*
 * kido-vec-arm64.S — Kido ARM64 exception-vector module.
 *
 * The table the GIC driver (session 27) was waiting for: VBAR_EL1
 * + DAIF controls + the IRQ stub that calls kido_gic_dispatch().
 * Promotes session 27's scratch stub to a committed module, with
 * the one thing the scratch stub did not do: save the interrupted
 * context. A real IRQ can land mid-task; the stub saves the full
 * frame (x0-x30, 256 bytes) so the interrupted code resumes
 * bit-identical.
 *
 * LAYOUT (ARM AArch64 exception model, verified 2026-10-25):
 *   Table base 2048-byte aligned (.align 11); 16 entries, 128
 *   bytes apart (.balign 128 after each branch); four groups of
 *   four — EL1t (SP_EL0), EL1h (SP_EL1), EL0 AArch64, EL0
 *   AArch32 — each ordered Sync / IRQ / FIQ / SError.
 *   Kido runs EL1h (entry .S drops to EL1h, SPSR 0x3c5), so the
 *   live IRQ entry is entry 5 (EL1h group, second slot).
 *
 * WHICH ENTRIES ARE LIVE:
 *   Entry 5 (IRQ EL1h) -> the full stub below (frame save,
 *   dispatch, frame restore, eret).
 *   All 15 other entries -> a tiny handler that records its
 *   vector index and ESR_EL1 into BSS slots (kido_vec_last_kind
 *   / kido_vec_last_esr, read by a later console-owning layer)
 *   and spins. Kido has no EL0, no EL1t stack, and no FIQ source
 *   (the GIC is configured Group0 -> IRQ only), so any other
 *   entry firing means the system's own assumptions broke — the
 *   evidence is stashed, then the core stops loudly (console
 *   silence), never returns to a corrupted state.
 *
 * IRQ STUB (entry 5):
 *   sub sp, sp, #256; save x0-x30 in pairs at offsets 0..224
 *   plus x30 at 240 (frame 256, SP stays 16-aligned); bl
 *   kido_gic_dispatch; restore in reverse order; add sp, #256;
 *   eret. SPSR_EL1 / ELR_EL1 are banked by the hardware and
 *   untouched by the C call, so eret restores PC and PSTATE.
 *   SP itself is NOT saved: the stub moves SP and moves it back;
 *   the sched .S saves SP only because a context switch CHANGES
 *   stacks, and an IRQ return does not.
 *   No nesting: exception entry masks IRQ (PSTATE.I set), and
 *   the stub never unmasks — a second IRQ cannot land inside
 *   the first handler's frame. Handlers must stay short; a
 *   future IRQ-stack module is the named fix for long handlers.
 *
 * INSTALL / DAIF:
 *   kido_vec_install uses adr (PC-relative, true wherever the
 *   image runs, including after the phase-3 relocator) — no
 *   literal pool, no GOT, no relocation entry for the audit to
 *   classify. Order law: install FIRST, GIC init second, IRQ
 *   enable LAST — VBAR is undefined at reset, so enabling IRQ
 *   before install would vector to a stale or zero base.
 *   All four functions run at EL1 only; below EL1 the msr
 *   faults by architecture (caller contract, stated in the
 *   header). They cannot fail at EL1, so they return no
 *   status code — inventing one would repeat the sched-D1
 *   pattern for no reason.
 *
 * RELOCATION: the table holds branches only (relative), the
 * install uses adr, the unhandled handler uses adrp stores —
 * this file adds ZERO entries to .rela.dyn and needs ZERO
 * carve-outs from the phase-3 walk. That is a design property
 * to be confirmed by disassembly at the proof gate, NOT a
 * claim made today (see PROOF STATUS).
 *
 * SEAM: NOT wired into the entry .S or kido_main — a future
 * Gate item. The Gate session wires install after
 * kido_reloc_verify() OK and before GIC enable, and retires
 * the session-27 scratch stub from all records.
 *
 * PROOF STATUS (honest): WRITTEN, NOT ASSEMBLED, NEVER
 * EXECUTED. The cross toolchain is absent this session
 * (11th data point: no aarch64-linux-gnu-gcc, no
 * qemu-system-aarch64). Proof gate, queued on the toolchain:
 * assemble + disassembly (table base 2048-aligned, entries
 * 128 apart, save/restore lists match, install is
 * adr+msr+isb, no absolute literal) + QEMU virt
 * (gic-version=2) SGI proof through this committed module
 * (install -> VBAR readback == table address -> GIC init ->
 * SGI 0 -> handler marker -> control flow resumes past the
 * send -> one IRQ, one clean return, zero fault entries).
 * SPI delivery (UART RX IRQ 33) is the proof after that.
 */
    .arch armv8-a
    .text

    .macro vec_entry handler
    b       \handler
    .balign 128
    .endm

    .align 11
    .global kido_vec_table
    .type kido_vec_table, %function
kido_vec_table:
    /* Group EL1t (SP_EL0): Sync / IRQ / FIQ / SError */
    vec_entry vec_sync_el1t     /* 0 */
    vec_entry vec_irq_el1t      /* 1 */
    vec_entry vec_fiq_el1t      /* 2 */
    vec_entry vec_err_el1t      /* 3 */
    /* Group EL1h (SP_EL1): Sync / IRQ / FIQ / SError */
    vec_entry vec_sync_el1h     /* 4 */
    vec_entry vec_irq_el1h      /* 5 — THE live entry */
    vec_entry vec_fiq_el1h      /* 6 */
    vec_entry vec_err_el1h      /* 7 */
    /* Group EL0 AArch64: Sync / IRQ / FIQ / SError */
    vec_entry vec_sync_el0_64   /* 8 */
    vec_entry vec_irq_el0_64    /* 9 */
    vec_entry vec_fiq_el0_64    /* 10 */
    vec_entry vec_err_el0_64    /* 11 */
    /* Group EL0 AArch32: Sync / IRQ / FIQ / SError */
    vec_entry vec_sync_el0_32   /* 12 */
    vec_entry vec_irq_el0_32    /* 13 */
    vec_entry vec_fiq_el0_32    /* 14 */
    vec_entry vec_err_el0_32    /* 15 */
    .size kido_vec_table, .-kido_vec_table

/* ---- Unhandled entries: stash index + ESR, then spin -------- */
vec_sync_el1t:
    mov     w0, #0
    b       vec_unhandled
vec_irq_el1t:
    mov     w0, #1
    b       vec_unhandled
vec_fiq_el1t:
    mov     w0, #2
    b       vec_unhandled
vec_err_el1t:
    mov     w0, #3
    b       vec_unhandled
vec_sync_el1h:
    mov     w0, #4
    b       vec_unhandled
vec_fiq_el1h:
    mov     w0, #6
    b       vec_unhandled
vec_err_el1h:
    mov     w0, #7
    b       vec_unhandled
vec_sync_el0_64:
    mov     w0, #8
    b       vec_unhandled
vec_irq_el0_64:
    mov     w0, #9
    b       vec_unhandled
vec_fiq_el0_64:
    mov     w0, #10
    b       vec_unhandled
vec_err_el0_64:
    mov     w0, #11
    b       vec_unhandled
vec_sync_el0_32:
    mov     w0, #12
    b       vec_unhandled
vec_irq_el0_32:
    mov     w0, #13
    b       vec_unhandled
vec_fiq_el0_32:
    mov     w0, #14
    b       vec_unhandled
vec_err_el0_32:
    mov     w0, #15
    b       vec_unhandled

vec_unhandled:
    /* w0 = vector index (the caller set it); x1 = ESR_EL1.
     * adrp stores are PC-relative, so they land in the real BSS
     * wherever the image runs. Nothing here returns: the
     * interrupted state is not trusted after an unexpected
     * exception, and there is no console contract at this layer
     * to report on (the HAL owns the UART). */
    mrs     x1, esr_el1
    adrp    x2, kido_vec_last_kind
    str     w0, [x2, #:lo12:kido_vec_last_kind]
    adrp    x2, kido_vec_last_esr
    str     x1, [x2, #:lo12:kido_vec_last_esr]
vec_unhandled_spin:
    b       vec_unhandled_spin

/* ---- The live entry: IRQ from EL1h --------------------------- */
    .global vec_irq_el1h
    .type vec_irq_el1h, %function
vec_irq_el1h:
    sub     sp, sp, #256
    stp     x0, x1, [sp, #0]
    stp     x2, x3, [sp, #16]
    stp     x4, x5, [sp, #32]
    stp     x6, x7, [sp, #48]
    stp     x8, x9, [sp, #64]
    stp     x10, x11, [sp, #80]
    stp     x12, x13, [sp, #96]
    stp     x14, x15, [sp, #112]
    stp     x16, x17, [sp, #128]
    stp     x18, x19, [sp, #144]
    stp     x20, x21, [sp, #160]
    stp     x22, x23, [sp, #176]
    stp     x24, x25, [sp, #192]
    stp     x26, x27, [sp, #208]
    stp     x28, x29, [sp, #224]
    str     x30, [sp, #240]
    bl      kido_gic_dispatch
    ldp     x0, x1, [sp, #0]
    ldp     x2, x3, [sp, #16]
    ldp     x4, x5, [sp, #32]
    ldp     x6, x7, [sp, #48]
    ldp     x8, x9, [sp, #64]
    ldp     x10, x11, [sp, #80]
    ldp     x12, x13, [sp, #96]
    ldp     x14, x15, [sp, #112]
    ldp     x16, x17, [sp, #128]
    ldp     x18, x19, [sp, #144]
    ldp     x20, x21, [sp, #160]
    ldp     x22, x23, [sp, #176]
    ldp     x24, x25, [sp, #192]
    ldp     x26, x27, [sp, #208]
    ldp     x28, x29, [sp, #224]
    ldr     x30, [sp, #240]
    add     sp, sp, #256
    eret
    .size vec_irq_el1h, .-vec_irq_el1h

/* ---- VBAR / DAIF controls (EL1 only, cannot fail at EL1) ---- */
    .global kido_vec_install
    .type kido_vec_install, %function
kido_vec_install:
    adr     x0, kido_vec_table
    msr     vbar_el1, x0
    isb
    ret
    .size kido_vec_install, .-kido_vec_install

    .global kido_vec_irq_enable
    .type kido_vec_irq_enable, %function
kido_vec_irq_enable:
    msr     daifclr, #2             /* clear PSTATE.I: IRQs unmasked */
    ret
    .size kido_vec_irq_enable, .-kido_vec_irq_enable

    .global kido_vec_irq_disable
    .type kido_vec_irq_disable, %function
kido_vec_irq_disable:
    msr     daifset, #2             /* set PSTATE.I: IRQs masked */
    ret
    .size kido_vec_irq_disable, .-kido_vec_irq_disable

    .global kido_vec_irq_masked
    .type kido_vec_irq_masked, %function
kido_vec_irq_masked:
    mrs     x0, daif
    and     x0, x0, #2              /* isolate PSTATE.I */
    lsr     x0, x0, #1              /* 1 = masked, 0 = unmasked */
    ret
    .size kido_vec_irq_masked, .-kido_vec_irq_masked

/* ---- Unhandled-exception evidence (BSS; entry .S zeroes it) - */
    .bss
    .align 3
    .global kido_vec_last_kind
    .type kido_vec_last_kind, %object
kido_vec_last_kind:
    .quad   0                       /* vector index 0-15, or 0 = none yet */
    .global kido_vec_last_esr
    .type kido_vec_last_esr, %object
kido_vec_last_esr:
    .quad   0                       /* ESR_EL1 at the unexpected exception */
