omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / kernel / arch / trap_irq.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/kernel/arch/trap_irq.om: Pure-Omega AArch64 Exception Vector / Probe Fixup, |
| 3 | note IRQ Registration & Dispatcher, and CNTP Physical Generic Timer Model. |
| 4 | note Converted from upstream aienux native/kernel/arch/exception.c and native/kernel/arch/timer.c. |
| 5 | |
| 6 | note AArch64 vector indices (0..15) |
| 7 | const VEC_SYNC_EL_SP0 ulong 0 |
| 8 | const VEC_SYNC_EL_SPX ulong 4 |
| 9 | const VEC_SYNC_LOWER64 ulong 8 |
| 10 | const VEC_COUNT ulong 16 |
| 11 | |
| 12 | note ESR_EL1 exception classes |
| 13 | const ESR_EC_SVC64 ulong 0x15 |
| 14 | const ESR_EC_DABT_CUR ulong 0x25 |
| 15 | const ESR_EC_IABT_LOW ulong 0x20 |
| 16 | const ESR_EC_DABT_LOW ulong 0x24 |
| 17 | |
| 18 | note Exception handling disposition codes |
| 19 | const EXC_DISP_FATAL int 0 |
| 20 | const EXC_DISP_LOWER64 int 1 |
| 21 | const EXC_DISP_PROBE_FIXUP int 2 |
| 22 | |
| 23 | note IRQ registration table (INTID 0..1019, INTID 30 reserved for CNTP EL1 physical timer) |
| 24 | const IRQ_MAX uint 1020 |
| 25 | const IRQ_TIMER_PPI uint 30 |
| 26 | const IRQ_OK int 0 |
| 27 | const IRQ_ERR_BAD int -1 |
| 28 | const IRQ_ERR_DUP int -2 |
| 29 | |
| 30 | note Global IRQ handler table (handler_id 0 = unregistered, >0 = registered handler token) |
| 31 | global irq_handlers uint 1020 |
| 32 | global irq_args ulong 1020 |
| 33 | note irq_stats: 0=spurious, 1=unhandled, 2=timer_irqs, 3=dispatched, 4=last_handler, 5=last_arg |
| 34 | global irq_stats ulong 6 |
| 35 | |
| 36 | fn exc_esr_ec esr ulong to ulong |
| 37 | ret and shr esr 26 0x3f |
| 38 | end |
| 39 | |
| 40 | fn exc_probe_arm ps edit ProbeState insn ulong fixup ulong to void |
| 41 | put ps insn insn |
| 42 | put ps fixup fixup |
| 43 | put ps esr 0 |
| 44 | put ps far 0 |
| 45 | put ps armed 1 |
| 46 | end |
| 47 | |
| 48 | note Classify an EL1 exception: returns EXC_DISP_LOWER64 (vector 8 with hook), |
| 49 | note EXC_DISP_PROBE_FIXUP (EL1 vector 4 DABT_CUR at armed probe insn), or EXC_DISP_FATAL. |
| 50 | fn exc_classify el uint vector ulong esr ulong far ulong elr ulong ps edit ProbeState to int |
| 51 | if eq vector VEC_SYNC_LOWER64 |
| 52 | ret EXC_DISP_LOWER64 |
| 53 | end |
| 54 | ec = exc_esr_ec esr |
| 55 | if and eq el 1 and eq vector VEC_SYNC_EL_SPX eq ec ESR_EC_DABT_CUR |
| 56 | if and ne get ps armed 0 eq elr get ps insn |
| 57 | put ps esr esr |
| 58 | put ps far far |
| 59 | put ps armed 0 |
| 60 | ret EXC_DISP_PROBE_FIXUP |
| 61 | end |
| 62 | end |
| 63 | ret EXC_DISP_FATAL |
| 64 | end |
| 65 | |
| 66 | fn irq_table_reset to void |
| 67 | for i ulong 1020 |
| 68 | put irq_handlers i 0 |
| 69 | put irq_args i 0 |
| 70 | end |
| 71 | for s ulong 6 |
| 72 | put irq_stats s 0 |
| 73 | end |
| 74 | end |
| 75 | |
| 76 | fn irq_register intid uint handler_id uint arg ulong to int |
| 77 | if or ge intid IRQ_MAX or eq intid IRQ_TIMER_PPI eq handler_id 0 |
| 78 | ret IRQ_ERR_BAD |
| 79 | end |
| 80 | if ne get irq_handlers ulong intid 0 |
| 81 | ret IRQ_ERR_DUP |
| 82 | end |
| 83 | put irq_handlers ulong intid handler_id |
| 84 | put irq_args ulong intid arg |
| 85 | ret IRQ_OK |
| 86 | end |
| 87 | |
| 88 | note Dispatch one acknowledged GIC INTID. Returns 1 if timer preempt check should run, 0 otherwise. |
| 89 | fn irq_dispatch intid uint to uint |
| 90 | if ge intid IRQ_MAX |
| 91 | put irq_stats 0 add get irq_stats 0 1 |
| 92 | ret 0 |
| 93 | end |
| 94 | if eq intid IRQ_TIMER_PPI |
| 95 | put irq_stats 2 add get irq_stats 2 1 |
| 96 | ret 1 |
| 97 | end |
| 98 | hid = get irq_handlers ulong intid |
| 99 | if ne hid 0 |
| 100 | put irq_stats 3 add get irq_stats 3 1 |
| 101 | put irq_stats 4 ulong hid |
| 102 | put irq_stats 5 get irq_args ulong intid |
| 103 | ret 0 |
| 104 | end |
| 105 | put irq_stats 1 add get irq_stats 1 1 |
| 106 | ret 0 |
| 107 | end |
| 108 | |
| 109 | fn irq_stat_get idx ulong to ulong |
| 110 | if ge idx 6 |
| 111 | ret 0 |
| 112 | end |
| 113 | ret get irq_stats idx |
| 114 | end |
| 115 | |
| 116 | note ---- CNTP Timer math & tick window (overflow-safe without 128-bit division) ---- |
| 117 | |
| 118 | fn timer_counter_to_us cnt ulong freq_hz ulong to ulong |
| 119 | if eq freq_hz 0 |
| 120 | ret 0 |
| 121 | end |
| 122 | sec = div cnt freq_hz |
| 123 | rem_c = rem cnt freq_hz |
| 124 | if gt rem_c 18446744073709 |
| 125 | ret add mul sec 1000000 div rem_c div freq_hz 1000000 |
| 126 | end |
| 127 | ret add mul sec 1000000 div mul rem_c 1000000 freq_hz |
| 128 | end |
| 129 | |
| 130 | fn timer_us_to_ticks us ulong freq_hz ulong to ulong |
| 131 | q = div freq_hz 1000000 |
| 132 | r = rem freq_hz 1000000 |
| 133 | ret add mul us q div mul us r 1000000 |
| 134 | end |
| 135 | |
| 136 | fn timer_state_init ts edit TimerState start_cnt ulong freq_hz ulong to void |
| 137 | put ts ticks 0 |
| 138 | put ts last start_cnt |
| 139 | put ts tmin 0xffffffffffffffff |
| 140 | put ts tmax 0 |
| 141 | put ts freq_hz freq_hz |
| 142 | end |
| 143 | |
| 144 | note Record one timer IRQ at counter value now; returns next CNTP_CVAL deadline (now + 10ms). |
| 145 | fn timer_tick_record ts edit TimerState now ulong to ulong |
| 146 | hz = get ts freq_hz |
| 147 | period = ulong 625000 |
| 148 | if ne hz 0 |
| 149 | period = div hz 100 |
| 150 | end |
| 151 | deadline = add now period |
| 152 | interval = sub now get ts last |
| 153 | put ts last now |
| 154 | put ts ticks add get ts ticks 1 |
| 155 | if lt interval get ts tmin |
| 156 | put ts tmin interval |
| 157 | end |
| 158 | if gt interval get ts tmax |
| 159 | put ts tmax interval |
| 160 | end |
| 161 | ret deadline |
| 162 | end |
| 163 | |
| 164 | note M1 invariant check: at least 5 ticks and no interval shorter than half a 10ms period. |
| 165 | fn timer_window_ok ts view TimerState to bool |
| 166 | if lt get ts ticks 5 |
| 167 | ret false |
| 168 | end |
| 169 | hz = get ts freq_hz |
| 170 | if eq hz 0 |
| 171 | ret false |
| 172 | end |
| 173 | half_period = div hz 200 |
| 174 | ret ge get ts tmin half_period |
| 175 | end |