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