interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / kernel / arch / trap_irq.om

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