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 / arm64 / fwops.om

280 lines · 6190 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/arm64/fwops.om: Pure-Omega AArch64 bare-metal primitives over target a64efi fwop/fwsys.
3note Requires: target a64efi, om2.
4
5const FWOP_DSB_SY ulong 0
6const FWOP_DSB_ISH ulong 1
7const FWOP_ISB ulong 2
8const FWOP_TLBI_VMALLE1IS ulong 3
9const FWOP_DC_CIVAC ulong 4
10const FWOP_DC_CVAC ulong 5
11const FWOP_IC_IALLU ulong 6
12const FWOP_DAIF_MASK ulong 7
13const FWOP_IRQ_UNMASK ulong 8
14const FWOP_WFE ulong 9
15const FWOP_SEV ulong 10
16const FWOP_WFI ulong 11
17const FWOP_SYSRD ulong 16
18const FWOP_SYSWR ulong 17
19const FWOP_SMC ulong 18
20const FWOP_HVC ulong 19
21const FWOP_EL2_TO_EL1 ulong 20
22const FWOP_EL1_SWITCH ulong 21
23const FWOP_META ulong 22
24const FWOP_POST_EBS_SET ulong 23
25const FWOP_PSCI_CPU_ON_SMC ulong 26
26const FWOP_PSCI_CPU_ON_HVC ulong 27
27const FWOP_LDAR ulong 28
28const FWOP_STLR ulong 29
29
30const SYSREG_CURRENTEL ulong 0
31const SYSREG_MPIDR_EL1 ulong 1
32const SYSREG_CTR_EL0 ulong 2
33const SYSREG_ID_AA64MMFR0 ulong 3
34const SYSREG_ID_AA64PFR0 ulong 4
35const SYSREG_SCTLR_EL1 ulong 5
36const SYSREG_TTBR0_EL1 ulong 6
37const SYSREG_TCR_EL1 ulong 7
38const SYSREG_MAIR_EL1 ulong 8
39const SYSREG_VBAR_EL1 ulong 9
40const SYSREG_ICC_SRE_EL1 ulong 10
41const SYSREG_ICC_IAR1_EL1 ulong 11
42const SYSREG_ICC_EOIR1_EL1 ulong 11
43const SYSREG_ICC_PMR_EL1 ulong 12
44const SYSREG_ICC_BPR1_EL1 ulong 13
45const SYSREG_ICC_IGRPEN1_EL1 ulong 14
46const SYSREG_ICC_CTLR_EL1 ulong 15
47const SYSREG_HCR_EL2 ulong 16
48const SYSREG_SCTLR_EL2 ulong 17
49const SYSREG_TTBR0_EL2 ulong 18
50const SYSREG_ICC_SRE_EL2 ulong 19
51const SYSREG_ESR_EL1 ulong 20
52const SYSREG_ELR_EL1 ulong 21
53const SYSREG_FAR_EL1 ulong 22
54const SYSREG_DAIF ulong 23
55const SYSREG_SP ulong 24
56const SYSREG_CNTP_CTL_EL0 ulong 25
57const SYSREG_CNTP_TVAL_EL0 ulong 26
58
59const PSCI_CONDUIT_NONE ulong 0
60const PSCI_CONDUIT_SMC ulong 1
61const PSCI_CONDUIT_HVC ulong 2
62const PSCI_VERSION ulong 0x84000000
63const PSCI_SYSTEM_RESET ulong 0x84000009
64const PSCI_CPU_ON_64 ulong 0xc4000003
65
66const ARM64_MPIDR_AFF_MASK ulong 0x000000ff00ffffff
67
68fn arm64_dsb_sy to void
69x = fwop FWOP_DSB_SY 0 0 0
70end
71
72fn arm64_dsb_ish to void
73x = fwop FWOP_DSB_ISH 0 0 0
74end
75
76fn arm64_isb to void
77x = fwop FWOP_ISB 0 0 0
78end
79
80fn arm64_tlbi_vmalle1is to void
81x = fwop FWOP_TLBI_VMALLE1IS 0 0 0
82end
83
84fn arm64_daif_mask to void
85x = fwop FWOP_DAIF_MASK 0 0 0
86end
87
88fn arm64_irq_unmask to void
89x = fwop FWOP_IRQ_UNMASK 0 0 0
90end
91
92fn arm64_wfe to void
93x = fwop FWOP_WFE 0 0 0
94end
95
96fn arm64_sev to void
97x = fwop FWOP_SEV 0 0 0
98end
99
100fn arm64_sysrd reg ulong to ulong
101ret fwop FWOP_SYSRD reg 0 0
102end
103
104fn arm64_syswr reg ulong val ulong to void
105x = fwop FWOP_SYSWR reg val 0
106end
107
108fn arm64_current_el to ulong
109ret and shr arm64_sysrd SYSREG_CURRENTEL 2 3
110end
111
112fn arm64_mpidr_aff to ulong
113ret and arm64_sysrd SYSREG_MPIDR_EL1 ARM64_MPIDR_AFF_MASK
114end
115
116fn arm64_dcache_line_bytes to ulong
117ctr = arm64_sysrd SYSREG_CTR_EL0
118dmin = and shr ctr 16 15
119if gt dmin 12
120ret 64
121end
122ret shl ulong 4 dmin
123end
124
125note Clean and invalidate [pa, pa + n) to Point of Coherency (DC CIVAC + DSB SY).
126fn arm64_dc_civac_range pa ulong n ulong to void
127if eq n 0
128ret
129end
130if gt n sub 0xffffffffffffffff pa
131ret
132end
133line = arm64_dcache_line_bytes
134mask = sub line 1
135a = and pa xor 0xffffffffffffffff mask
136end_pa = add pa n
137note Bound 16777216 lines covers up to 1 GiB @ 64-byte cache lines.
138while lt a end_pa bound 16777216
139x = fwop FWOP_DC_CIVAC a 0 0
140if gt line sub 0xffffffffffffffff a
141break
142end
143a = add a line
144end
145arm64_dsb_sy
146end
147
148note Clean [pa, pa + n) to Point of Coherency (DC CVAC + DSB SY) before SMP CPU_ON.
149fn arm64_dc_cvac_range pa ulong n ulong to void
150if eq n 0
151ret
152end
153if gt n sub 0xffffffffffffffff pa
154ret
155end
156line = arm64_dcache_line_bytes
157mask = sub line 1
158a = and pa xor 0xffffffffffffffff mask
159end_pa = add pa n
160while lt a end_pa bound 16777216
161x = fwop FWOP_DC_CVAC a 0 0
162if gt line sub 0xffffffffffffffff a
163break
164end
165a = add a line
166end
167arm64_dsb_sy
168end
169
170fn arm64_ldar addr ulong to ulong
171ret fwop FWOP_LDAR addr 0 0
172end
173
174fn arm64_stlr addr ulong val ulong to void
175x = fwop FWOP_STLR addr val 0
176end
177
178note Ordered Device-nGnRnE MMIO accessors (DSB SY before and after fwld/fwst).
179fn arm64_mmio_r32 addr ulong to ulong
180arm64_dsb_sy
181v = fwld addr 4
182arm64_dsb_sy
183ret and v 0xffffffff
184end
185
186fn arm64_mmio_w32 addr ulong val ulong to void
187arm64_dsb_sy
188x = fwst addr 4 and val 0xffffffff
189arm64_dsb_sy
190end
191
192fn arm64_mmio_r64 addr ulong to ulong
193arm64_dsb_sy
194v = fwld addr 8
195arm64_dsb_sy
196ret v
197end
198
199fn arm64_mmio_w64 addr ulong val ulong to void
200arm64_dsb_sy
201x = fwst addr 8 val
202arm64_dsb_sy
203end
204
205fn arm64_meta key ulong to ulong
206ret fwop FWOP_META key 0 0
207end
208
209fn arm64_post_ebs_set uart_pa ulong conduit ulong to void
210x = fwop FWOP_POST_EBS_SET uart_pa conduit 0
211end
212
213fn arm64_psci_call conduit ulong fid ulong a1 ulong a2 ulong to ulong
214if eq conduit PSCI_CONDUIT_SMC
215ret fwop FWOP_SMC fid a1 a2
216end
217if eq conduit PSCI_CONDUIT_HVC
218ret fwop FWOP_HVC fid a1 a2
219end
220ret 0xffffffffffffffff
221end
222
223fn arm64_psci_cpu_on conduit ulong mpidr ulong entry ulong ctx ulong to ulong
224if eq conduit PSCI_CONDUIT_SMC
225ret fwop FWOP_PSCI_CPU_ON_SMC mpidr entry ctx
226end
227if eq conduit PSCI_CONDUIT_HVC
228ret fwop FWOP_PSCI_CPU_ON_HVC mpidr entry ctx
229end
230ret 0xffffffffffffffff
231end
232
233fn arm64_psci_reset conduit ulong to void
234x = arm64_psci_call conduit PSCI_SYSTEM_RESET 0 0
235end
236
237note Overflow-safe CNTVCT_EL0 monotonic clock helpers (shared contract for Pieces 1-6).
238fn arm64_ticks_to_ms t ulong frq ulong to ulong
239if lt frq 1000
240ret 0
241end
242sec = div t frq
243rem_t = rem t frq
244if gt rem_t 18446744073709551
245ret add mul sec 1000 div rem_t div frq 1000
246end
247ret add mul sec 1000 div mul rem_t 1000 frq
248end
249
250fn arm64_ticks_to_us t ulong frq ulong to ulong
251if lt frq 1000000
252ret 0
253end
254sec = div t frq
255rem_t = rem t frq
256if gt rem_t 18446744073709
257ret add mul sec 1000000 div rem_t div frq 1000000
258end
259ret add mul sec 1000000 div mul rem_t 1000000 frq
260end
261
262fn arm64_time_ms to ulong
263ret arm64_ticks_to_ms fwsys 2 fwsys 3
264end
265
266fn arm64_time_us to ulong
267ret arm64_ticks_to_us fwsys 2 fwsys 3
268end
269
270fn arm64_deadline_ticks ms ulong to ulong
271frq = fwsys 3
272now = fwsys 2
273if eq frq 0
274ret now
275end
276secs = div ms 1000
277rem_ms = rem ms 1000
278delta = add mul secs frq div mul rem_ms frq 1000
279ret add now delta
280end