Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 17 | #include <stdnoreturn.h> |
| 18 | |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 19 | #include "hf/arch/barriers.h" |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 20 | #include "hf/arch/init.h" |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 21 | #include "hf/arch/mm.h" |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 22 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 23 | #include "hf/api.h" |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 24 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 25 | #include "hf/cpu.h" |
| 26 | #include "hf/dlog.h" |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 27 | #include "hf/panic.h" |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 28 | #include "hf/spci.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 29 | #include "hf/vm.h" |
| 30 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 31 | #include "vmapi/hf/call.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 32 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 33 | #include "debug_el1.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 34 | #include "msr.h" |
Fuad Tabba | f1d6dc5 | 2019-09-18 17:33:14 +0100 | [diff] [blame] | 35 | #include "perfmon.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 36 | #include "psci.h" |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 37 | #include "psci_handler.h" |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 38 | #include "smc.h" |
Fuad Tabba | ba8c44d | 2019-09-23 14:38:58 +0100 | [diff] [blame] | 39 | #include "sysregs.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 40 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 41 | #define HCR_EL2_VI (1u << 7) |
| 42 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 43 | /** |
| 44 | * Gets the Exception Class from the ESR. |
| 45 | */ |
| 46 | #define GET_EC(esr) ((esr) >> 26) |
| 47 | |
| 48 | /** |
| 49 | * Gets the value to increment for the next PC. |
| 50 | * The ESR encodes whether the instruction is 2 bytes or 4 bytes long. |
| 51 | */ |
| 52 | #define GET_NEXT_PC_INC(esr) (((esr) & (1u << 25)) ? 4 : 2) |
| 53 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 54 | /** |
Andrew Walbran | 0dd67ff | 2019-09-12 16:38:50 +0100 | [diff] [blame] | 55 | * The Client ID field within X7 for an SMC64 call. |
| 56 | */ |
| 57 | #define CLIENT_ID_MASK UINT64_C(0xffff) |
| 58 | |
| 59 | /** |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 60 | * Returns a reference to the currently executing vCPU. |
| 61 | */ |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 62 | static struct vcpu *current(void) |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 63 | { |
| 64 | return (struct vcpu *)read_msr(tpidr_el2); |
| 65 | } |
| 66 | |
Andrew Walbran | 1f8d487 | 2018-12-20 11:21:32 +0000 | [diff] [blame] | 67 | /** |
| 68 | * Saves the state of per-vCPU peripherals, such as the virtual timer, and |
| 69 | * informs the arch-independent sections that registers have been saved. |
| 70 | */ |
| 71 | void complete_saving_state(struct vcpu *vcpu) |
| 72 | { |
Andrew Walbran | 6480f8f | 2019-06-05 17:39:14 +0100 | [diff] [blame] | 73 | vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0); |
| 74 | vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0); |
Andrew Walbran | 1f8d487 | 2018-12-20 11:21:32 +0000 | [diff] [blame] | 75 | |
| 76 | api_regs_state_saved(vcpu); |
| 77 | |
| 78 | /* |
| 79 | * If switching away from the primary, copy the current EL0 virtual |
| 80 | * timer registers to the corresponding EL2 physical timer registers. |
| 81 | * This is used to emulate the virtual timer for the primary in case it |
| 82 | * should fire while the secondary is running. |
| 83 | */ |
| 84 | if (vcpu->vm->id == HF_PRIMARY_VM_ID) { |
| 85 | /* |
| 86 | * Clear timer control register before copying compare value, to |
| 87 | * avoid a spurious timer interrupt. This could be a problem if |
| 88 | * the interrupt is configured as edge-triggered, as it would |
| 89 | * then be latched in. |
| 90 | */ |
| 91 | write_msr(cnthp_ctl_el2, 0); |
| 92 | write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0)); |
| 93 | write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Restores the state of per-vCPU peripherals, such as the virtual timer. |
| 99 | */ |
| 100 | void begin_restoring_state(struct vcpu *vcpu) |
| 101 | { |
| 102 | /* |
| 103 | * Clear timer control register before restoring compare value, to avoid |
| 104 | * a spurious timer interrupt. This could be a problem if the interrupt |
| 105 | * is configured as edge-triggered, as it would then be latched in. |
| 106 | */ |
| 107 | write_msr(cntv_ctl_el0, 0); |
Andrew Walbran | 6480f8f | 2019-06-05 17:39:14 +0100 | [diff] [blame] | 108 | write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0); |
| 109 | write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0); |
Andrew Walbran | 1f8d487 | 2018-12-20 11:21:32 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * If we are switching (back) to the primary, disable the EL2 physical |
| 113 | * timer which was being used to emulate the EL0 virtual timer, as the |
| 114 | * virtual timer is now running for the primary again. |
| 115 | */ |
| 116 | if (vcpu->vm->id == HF_PRIMARY_VM_ID) { |
| 117 | write_msr(cnthp_ctl_el2, 0); |
| 118 | write_msr(cnthp_cval_el2, 0); |
| 119 | } |
| 120 | } |
| 121 | |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 122 | /** |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 123 | * Invalidate all stage 1 TLB entries on the current (physical) CPU for the |
| 124 | * current VMID. |
| 125 | */ |
| 126 | static void invalidate_vm_tlb(void) |
| 127 | { |
Andrew Walbran | cff1f68 | 2019-07-04 14:52:45 +0100 | [diff] [blame] | 128 | /* |
| 129 | * Ensure that the last VTTBR write has taken effect so we invalidate |
| 130 | * the right set of TLB entries. |
| 131 | */ |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 132 | isb(); |
Andrew Walbran | cff1f68 | 2019-07-04 14:52:45 +0100 | [diff] [blame] | 133 | |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 134 | __asm__ volatile("tlbi vmalle1"); |
Andrew Walbran | cff1f68 | 2019-07-04 14:52:45 +0100 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Ensure that no instructions are fetched for the VM until after the |
| 138 | * TLB invalidation has taken effect. |
| 139 | */ |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 140 | isb(); |
Andrew Walbran | cff1f68 | 2019-07-04 14:52:45 +0100 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Ensure that no data reads or writes for the VM happen until after the |
| 144 | * TLB invalidation has taken effect. Non-sharable is enough because the |
| 145 | * TLB is local to the CPU. |
| 146 | */ |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 147 | dsb(nsh); |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Invalidates the TLB if a different vCPU is being run than the last vCPU of |
| 152 | * the same VM which was run on the current pCPU. |
| 153 | * |
| 154 | * This is necessary because VMs may (contrary to the architecture |
| 155 | * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar |
| 156 | * workaround: |
| 157 | * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9 |
| 158 | */ |
| 159 | void maybe_invalidate_tlb(struct vcpu *vcpu) |
| 160 | { |
| 161 | size_t current_cpu_index = cpu_index(vcpu->cpu); |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 162 | spci_vcpu_index_t new_vcpu_index = vcpu_index(vcpu); |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 163 | |
| 164 | if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] != |
| 165 | new_vcpu_index) { |
| 166 | /* |
| 167 | * The vCPU has changed since the last time this VM was run on |
| 168 | * this pCPU, so we need to invalidate the TLB. |
| 169 | */ |
| 170 | invalidate_vm_tlb(); |
| 171 | |
| 172 | /* Record the fact that this vCPU is now running on this CPU. */ |
| 173 | vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] = |
| 174 | new_vcpu_index; |
| 175 | } |
| 176 | } |
| 177 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 178 | noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 179 | { |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 180 | (void)elr; |
| 181 | (void)spsr; |
| 182 | |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 183 | panic("IRQ from current"); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 186 | noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 187 | { |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 188 | (void)elr; |
| 189 | (void)spsr; |
| 190 | |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 191 | panic("FIQ from current"); |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 194 | noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr) |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 195 | { |
| 196 | (void)elr; |
| 197 | (void)spsr; |
| 198 | |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 199 | panic("SERR from current"); |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 202 | noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr) |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 203 | { |
| 204 | uintreg_t esr = read_msr(esr_el2); |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 205 | uintreg_t ec = GET_EC(esr); |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 206 | |
| 207 | (void)spsr; |
| 208 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 209 | switch (ec) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 210 | case 0x25: /* EC = 100101, Data abort. */ |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 211 | dlog("Data abort: pc=%#x, esr=%#x, ec=%#x", elr, esr, ec); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 212 | if (!(esr & (1u << 10))) { /* Check FnV bit. */ |
Andrew Walbran | ac5b261 | 2019-07-12 16:44:19 +0100 | [diff] [blame] | 213 | dlog(", far=%#x", read_msr(far_el2)); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 214 | } else { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 215 | dlog(", far=invalid"); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 216 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 217 | |
| 218 | dlog("\n"); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 219 | break; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 220 | |
| 221 | default: |
Andrew Walbran | ac5b261 | 2019-07-12 16:44:19 +0100 | [diff] [blame] | 222 | dlog("Unknown current sync exception pc=%#x, esr=%#x, " |
| 223 | "ec=%#x\n", |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 224 | elr, esr, ec); |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 225 | break; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 226 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 227 | |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 228 | panic("EL2 exception"); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 231 | /** |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 232 | * Sets or clears the VI bit in the HCR_EL2 register saved in the given |
| 233 | * arch_regs. |
| 234 | */ |
| 235 | static void set_virtual_interrupt(struct arch_regs *r, bool enable) |
| 236 | { |
| 237 | if (enable) { |
| 238 | r->lazy.hcr_el2 |= HCR_EL2_VI; |
| 239 | } else { |
| 240 | r->lazy.hcr_el2 &= ~HCR_EL2_VI; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Sets or clears the VI bit in the HCR_EL2 register. |
| 246 | */ |
| 247 | static void set_virtual_interrupt_current(bool enable) |
| 248 | { |
| 249 | uintreg_t hcr_el2 = read_msr(hcr_el2); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 250 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 251 | if (enable) { |
| 252 | hcr_el2 |= HCR_EL2_VI; |
| 253 | } else { |
| 254 | hcr_el2 &= ~HCR_EL2_VI; |
| 255 | } |
| 256 | write_msr(hcr_el2, hcr_el2); |
| 257 | } |
| 258 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 259 | /** |
| 260 | * Checks whether to block an SMC being forwarded from a VM. |
| 261 | */ |
| 262 | static bool smc_is_blocked(const struct vm *vm, uint32_t func) |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 263 | { |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 264 | bool block_by_default = !vm->smc_whitelist.permissive; |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 265 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 266 | for (size_t i = 0; i < vm->smc_whitelist.smc_count; ++i) { |
| 267 | if (func == vm->smc_whitelist.smcs[i]) { |
| 268 | return false; |
| 269 | } |
| 270 | } |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 271 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 272 | dlog("SMC %#010x attempted from VM %d, blocked=%d\n", func, vm->id, |
| 273 | block_by_default); |
| 274 | |
| 275 | /* Access is still allowed in permissive mode. */ |
| 276 | return block_by_default; |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 280 | * Applies SMC access control according to manifest and forwards the call if |
| 281 | * access is granted. |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 282 | */ |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 283 | static void smc_forwarder(const struct vcpu *vcpu, struct smc_result *ret) |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 284 | { |
| 285 | uint32_t func = vcpu->regs.r[0]; |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 286 | uint32_t client_id = vcpu->vm->id; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 287 | uintreg_t arg7; |
| 288 | |
| 289 | if (smc_is_blocked(vcpu->vm, func)) { |
| 290 | ret->res0 = SMCCC_ERROR_UNKNOWN; |
| 291 | return; |
| 292 | } |
| 293 | |
Andrew Walbran | 0dd67ff | 2019-09-12 16:38:50 +0100 | [diff] [blame] | 294 | /* |
| 295 | * Set the Client ID but keep the existing Secure OS ID and anything |
| 296 | * else (currently unspecified) that the client may have passed in the |
| 297 | * upper bits. |
| 298 | */ |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 299 | arg7 = client_id | (vcpu->regs.r[7] & ~CLIENT_ID_MASK); |
| 300 | *ret = smc_forward(func, vcpu->regs.r[1], vcpu->regs.r[2], |
| 301 | vcpu->regs.r[3], vcpu->regs.r[4], vcpu->regs.r[5], |
| 302 | vcpu->regs.r[6], arg7); |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 303 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 304 | /* |
| 305 | * Preserve the value passed by the caller, rather than the client_id we |
| 306 | * generated. Note that this would also overwrite any return value that |
| 307 | * may be in x7, but the SMCs that we are forwarding are legacy calls |
| 308 | * from before SMCCC 1.2 so won't have more than 4 return values anyway. |
| 309 | */ |
| 310 | ret->res7 = vcpu->regs.r[7]; |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 313 | static bool spci_handler(struct spci_value *args, struct vcpu **next) |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 314 | { |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 315 | switch (args->func & ~SMCCC_CONVENTION_MASK) { |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 316 | case SPCI_VERSION_32: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 317 | *args = api_spci_version(); |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 318 | return true; |
Andrew Walbran | d230f66 | 2019-10-07 18:03:36 +0100 | [diff] [blame^] | 319 | case SPCI_ID_GET_32: |
| 320 | *args = api_spci_id_get(current()); |
| 321 | return true; |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 322 | case SPCI_YIELD_32: |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 323 | api_yield(current(), next); |
| 324 | |
| 325 | /* SPCI_YIELD always returns SPCI_SUCCESS. */ |
| 326 | *args = (struct spci_value){.func = SPCI_SUCCESS_32}; |
| 327 | |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 328 | return true; |
| 329 | case SPCI_MSG_SEND_32: |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 330 | *args = api_spci_msg_send(spci_msg_send_sender(*args), |
| 331 | spci_msg_send_receiver(*args), |
| 332 | spci_msg_send_size(*args), |
| 333 | spci_msg_send_attributes(*args), |
| 334 | current(), next); |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 335 | return true; |
Andrew Walbran | 0de4f16 | 2019-09-03 16:44:20 +0100 | [diff] [blame] | 336 | case SPCI_MSG_WAIT_32: |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 337 | *args = api_spci_msg_recv(true, current(), next); |
Andrew Walbran | 0de4f16 | 2019-09-03 16:44:20 +0100 | [diff] [blame] | 338 | return true; |
| 339 | case SPCI_MSG_POLL_32: |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 340 | *args = api_spci_msg_recv(false, current(), next); |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 341 | return true; |
| 342 | } |
| 343 | |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Set or clear VI bit according to pending interrupts. |
| 349 | */ |
| 350 | static void update_vi(struct vcpu *next) |
| 351 | { |
| 352 | if (next == NULL) { |
| 353 | /* |
| 354 | * Not switching vCPUs, set the bit for the current vCPU |
| 355 | * directly in the register. |
| 356 | */ |
| 357 | struct vcpu *vcpu = current(); |
| 358 | |
| 359 | sl_lock(&vcpu->lock); |
| 360 | set_virtual_interrupt_current( |
| 361 | vcpu->interrupts.enabled_and_pending_count > 0); |
| 362 | sl_unlock(&vcpu->lock); |
| 363 | } else { |
| 364 | /* |
| 365 | * About to switch vCPUs, set the bit for the vCPU to which we |
| 366 | * are switching in the saved copy of the register. |
| 367 | */ |
| 368 | sl_lock(&next->lock); |
| 369 | set_virtual_interrupt( |
| 370 | &next->regs, |
| 371 | next->interrupts.enabled_and_pending_count > 0); |
| 372 | sl_unlock(&next->lock); |
| 373 | } |
| 374 | } |
| 375 | |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 376 | /** |
| 377 | * Processes SMC instruction calls. |
| 378 | */ |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 379 | static void smc_handler(struct vcpu *vcpu, struct smc_result *ret, |
Andrew Scull | ce688f0 | 2019-09-30 12:54:14 +0100 | [diff] [blame] | 380 | struct vcpu **next) |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 381 | { |
| 382 | uint32_t func = vcpu->regs.r[0]; |
| 383 | |
| 384 | if (psci_handler(vcpu, func, vcpu->regs.r[1], vcpu->regs.r[2], |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 385 | vcpu->regs.r[3], &ret->res0, next)) { |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 386 | return; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | switch (func & ~SMCCC_CONVENTION_MASK) { |
| 390 | case HF_DEBUG_LOG: |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 391 | ret->res0 = api_debug_log(vcpu->regs.r[1], vcpu); |
| 392 | return; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 395 | smc_forwarder(vcpu, ret); |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 396 | } |
| 397 | |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 398 | struct vcpu *hvc_handler(struct vcpu *vcpu) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 399 | { |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 400 | struct spci_value args = { |
| 401 | .func = vcpu->regs.r[0], |
| 402 | .arg1 = vcpu->regs.r[1], |
| 403 | .arg2 = vcpu->regs.r[2], |
| 404 | .arg3 = vcpu->regs.r[3], |
| 405 | .arg4 = vcpu->regs.r[4], |
| 406 | .arg5 = vcpu->regs.r[5], |
| 407 | .arg6 = vcpu->regs.r[6], |
| 408 | .arg7 = vcpu->regs.r[7], |
| 409 | }; |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 410 | struct vcpu *next = NULL; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 411 | |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 412 | if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3, |
| 413 | &vcpu->regs.r[0], &next)) { |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 414 | return next; |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 415 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 416 | |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 417 | if (spci_handler(&args, &next)) { |
| 418 | vcpu->regs.r[0] = args.func; |
| 419 | vcpu->regs.r[1] = args.arg1; |
| 420 | vcpu->regs.r[2] = args.arg2; |
| 421 | vcpu->regs.r[3] = args.arg3; |
| 422 | vcpu->regs.r[4] = args.arg4; |
| 423 | vcpu->regs.r[5] = args.arg5; |
| 424 | vcpu->regs.r[6] = args.arg6; |
| 425 | vcpu->regs.r[7] = args.arg7; |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 426 | update_vi(next); |
| 427 | return next; |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 428 | } |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 429 | |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 430 | switch (args.func) { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 431 | case HF_VM_GET_COUNT: |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 432 | vcpu->regs.r[0] = api_vm_get_count(); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 433 | break; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 434 | |
| 435 | case HF_VCPU_GET_COUNT: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 436 | vcpu->regs.r[0] = api_vcpu_get_count(args.arg1, vcpu); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 437 | break; |
| 438 | |
| 439 | case HF_VCPU_RUN: |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 440 | vcpu->regs.r[0] = hf_vcpu_run_return_encode( |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 441 | api_vcpu_run(args.arg1, args.arg2, vcpu, &next)); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 442 | break; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 443 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 444 | case HF_VM_CONFIGURE: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 445 | vcpu->regs.r[0] = api_vm_configure( |
| 446 | ipa_init(args.arg1), ipa_init(args.arg2), vcpu, &next); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 447 | break; |
| 448 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 449 | case HF_MAILBOX_CLEAR: |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 450 | vcpu->regs.r[0] = api_mailbox_clear(vcpu, &next); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 451 | break; |
| 452 | |
| 453 | case HF_MAILBOX_WRITABLE_GET: |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 454 | vcpu->regs.r[0] = api_mailbox_writable_get(vcpu); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 455 | break; |
| 456 | |
| 457 | case HF_MAILBOX_WAITER_GET: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 458 | vcpu->regs.r[0] = api_mailbox_waiter_get(args.arg1, vcpu); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 459 | break; |
| 460 | |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 461 | case HF_INTERRUPT_ENABLE: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 462 | vcpu->regs.r[0] = |
| 463 | api_interrupt_enable(args.arg1, args.arg2, vcpu); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 464 | break; |
| 465 | |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 466 | case HF_INTERRUPT_GET: |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 467 | vcpu->regs.r[0] = api_interrupt_get(vcpu); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 468 | break; |
| 469 | |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 470 | case HF_INTERRUPT_INJECT: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 471 | vcpu->regs.r[0] = api_interrupt_inject(args.arg1, args.arg2, |
| 472 | args.arg3, vcpu, &next); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 473 | break; |
| 474 | |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 475 | case HF_SHARE_MEMORY: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 476 | vcpu->regs.r[0] = api_share_memory( |
| 477 | args.arg1 >> 32, ipa_init(args.arg2), args.arg3, |
| 478 | args.arg1 & 0xffffffff, vcpu); |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 479 | break; |
| 480 | |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 481 | case HF_DEBUG_LOG: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 482 | vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu); |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 483 | break; |
| 484 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 485 | default: |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 486 | vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 489 | update_vi(next); |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 490 | |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 491 | return next; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 492 | } |
| 493 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 494 | struct vcpu *irq_lower(void) |
| 495 | { |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 496 | /* |
| 497 | * Switch back to primary VM, interrupts will be handled there. |
| 498 | * |
| 499 | * If the VM has aborted, this vCPU will be aborted when the scheduler |
| 500 | * tries to run it again. This means the interrupt will not be delayed |
| 501 | * by the aborted VM. |
| 502 | * |
| 503 | * TODO: Only switch when the interrupt isn't for the current VM. |
| 504 | */ |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 505 | return api_preempt(current()); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 508 | struct vcpu *fiq_lower(void) |
| 509 | { |
| 510 | return irq_lower(); |
| 511 | } |
| 512 | |
| 513 | struct vcpu *serr_lower(void) |
| 514 | { |
| 515 | dlog("SERR from lower\n"); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 516 | return api_abort(current()); |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 519 | /** |
| 520 | * Initialises a fault info structure. It assumes that an FnV bit exists at |
| 521 | * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of |
| 522 | * the ESR (the fault status code) are 010000; this is the case for both |
| 523 | * instruction and data aborts, but not necessarily for other exception reasons. |
| 524 | */ |
| 525 | static struct vcpu_fault_info fault_info_init(uintreg_t esr, |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 526 | const struct vcpu *vcpu, int mode) |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 527 | { |
| 528 | uint32_t fsc = esr & 0x3f; |
| 529 | struct vcpu_fault_info r; |
| 530 | |
| 531 | r.mode = mode; |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 532 | r.pc = va_init(vcpu->regs.pc); |
| 533 | |
| 534 | /* |
| 535 | * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It |
| 536 | * indicates that we cannot rely on far_el2. |
| 537 | */ |
| 538 | if (fsc == 0x10 && esr & (1u << 10)) { |
| 539 | r.vaddr = va_init(0); |
| 540 | r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8); |
| 541 | } else { |
| 542 | r.vaddr = va_init(read_msr(far_el2)); |
| 543 | r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) | |
| 544 | (read_msr(far_el2) & (PAGE_SIZE - 1))); |
| 545 | } |
| 546 | |
| 547 | return r; |
| 548 | } |
| 549 | |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 550 | struct vcpu *sync_lower_exception(uintreg_t esr) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 551 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 552 | struct vcpu *vcpu = current(); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 553 | struct vcpu_fault_info info; |
Jose Marinho | 135dff3 | 2019-02-28 10:25:57 +0000 | [diff] [blame] | 554 | struct vcpu *new_vcpu; |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 555 | uintreg_t ec = GET_EC(esr); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 556 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 557 | switch (ec) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 558 | case 0x01: /* EC = 000001, WFI or WFE. */ |
Andrew Walbran | 48196eb | 2019-03-04 14:56:24 +0000 | [diff] [blame] | 559 | /* Skip the instruction. */ |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 560 | vcpu->regs.pc += GET_NEXT_PC_INC(esr); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 561 | /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 562 | if (esr & 1) { |
Andrew Walbran | 48196eb | 2019-03-04 14:56:24 +0000 | [diff] [blame] | 563 | /* WFE */ |
| 564 | /* |
| 565 | * TODO: consider giving the scheduler more context, |
| 566 | * somehow. |
| 567 | */ |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 568 | api_yield(vcpu, &new_vcpu); |
Jose Marinho | 135dff3 | 2019-02-28 10:25:57 +0000 | [diff] [blame] | 569 | return new_vcpu; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 570 | } |
Andrew Walbran | 48196eb | 2019-03-04 14:56:24 +0000 | [diff] [blame] | 571 | /* WFI */ |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 572 | return api_wait_for_interrupt(vcpu); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 573 | |
| 574 | case 0x24: /* EC = 100100, Data abort. */ |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 575 | info = fault_info_init( |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 576 | esr, vcpu, (esr & (1u << 6)) ? MM_MODE_W : MM_MODE_R); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 577 | if (vcpu_handle_page_fault(vcpu, &info)) { |
| 578 | return NULL; |
| 579 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 580 | break; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 581 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 582 | case 0x20: /* EC = 100000, Instruction abort. */ |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 583 | info = fault_info_init(esr, vcpu, MM_MODE_X); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 584 | if (vcpu_handle_page_fault(vcpu, &info)) { |
| 585 | return NULL; |
| 586 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 587 | break; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 588 | |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 589 | case 0x16: /* EC = 010110, HVC instruction */ |
| 590 | return hvc_handler(vcpu); |
| 591 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 592 | case 0x17: /* EC = 010111, SMC instruction. */ { |
| 593 | uintreg_t smc_pc = vcpu->regs.pc; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 594 | struct vcpu *next = NULL; |
Andrew Walbran | 121f88b | 2019-10-03 14:29:03 +0100 | [diff] [blame] | 595 | struct smc_result ret = {.res4 = vcpu->regs.r[4], |
| 596 | .res5 = vcpu->regs.r[5], |
| 597 | .res6 = vcpu->regs.r[6], |
| 598 | .res7 = vcpu->regs.r[7]}; |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 599 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 600 | smc_handler(vcpu, &ret, &next); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 601 | |
| 602 | /* Skip the SMC instruction. */ |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 603 | vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr); |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 604 | vcpu->regs.r[0] = ret.res0; |
| 605 | vcpu->regs.r[1] = ret.res1; |
| 606 | vcpu->regs.r[2] = ret.res2; |
| 607 | vcpu->regs.r[3] = ret.res3; |
Andrew Walbran | 121f88b | 2019-10-03 14:29:03 +0100 | [diff] [blame] | 608 | vcpu->regs.r[4] = ret.res4; |
| 609 | vcpu->regs.r[5] = ret.res5; |
| 610 | vcpu->regs.r[6] = ret.res6; |
| 611 | vcpu->regs.r[7] = ret.res7; |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 612 | return next; |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 613 | } |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 614 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 615 | /* |
| 616 | * EC = 011000, MSR, MRS or System instruction execution that is not |
| 617 | * reported using EC 000000, 000001 or 000111. |
| 618 | */ |
| 619 | case 0x18: |
| 620 | /* |
| 621 | * NOTE: This should never be reached because it goes through a |
| 622 | * separate path handled by handle_system_register_access(). |
| 623 | */ |
| 624 | panic("Handled by handle_system_register_access()."); |
| 625 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 626 | default: |
Andrew Walbran | ac5b261 | 2019-07-12 16:44:19 +0100 | [diff] [blame] | 627 | dlog("Unknown lower sync exception pc=%#x, esr=%#x, " |
| 628 | "ec=%#x\n", |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 629 | vcpu->regs.pc, esr, ec); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 630 | break; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 631 | } |
| 632 | |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 633 | /* The exception wasn't handled so abort the VM. */ |
| 634 | return api_abort(vcpu); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 635 | } |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 636 | |
| 637 | /** |
| 638 | * Handles EC = 011000, msr, mrs instruction traps. |
| 639 | * Returns non-null ONLY if the access failed and the vcpu is changing. |
| 640 | */ |
| 641 | struct vcpu *handle_system_register_access(uintreg_t esr) |
| 642 | { |
| 643 | struct vcpu *vcpu = current(); |
| 644 | spci_vm_id_t vm_id = vcpu->vm->id; |
| 645 | uintreg_t ec = GET_EC(esr); |
Fuad Tabba | ba8c44d | 2019-09-23 14:38:58 +0100 | [diff] [blame] | 646 | char *direction_str; |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 647 | |
| 648 | CHECK(ec == 0x18); |
| 649 | |
| 650 | /* |
Fuad Tabba | f1d6dc5 | 2019-09-18 17:33:14 +0100 | [diff] [blame] | 651 | * Handle accesses to debug and performance monitor registers. |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 652 | * Abort when encountering unhandled register accesses. |
| 653 | */ |
Fuad Tabba | f1d6dc5 | 2019-09-18 17:33:14 +0100 | [diff] [blame] | 654 | if (debug_el1_is_register_access(esr)) { |
| 655 | if (!debug_el1_process_access(vcpu, vm_id, esr)) { |
| 656 | goto fail; |
| 657 | } |
| 658 | } else if (perfmon_is_register_access(esr)) { |
| 659 | if (!perfmon_process_access(vcpu, vm_id, esr)) { |
| 660 | goto fail; |
| 661 | } |
| 662 | } else { |
| 663 | goto fail; |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Fuad Tabba | f1d6dc5 | 2019-09-18 17:33:14 +0100 | [diff] [blame] | 666 | /* Instruction was fulfilled. Skip it and run the next one. */ |
| 667 | vcpu->regs.pc += GET_NEXT_PC_INC(esr); |
| 668 | return NULL; |
| 669 | |
| 670 | fail: |
Fuad Tabba | ba8c44d | 2019-09-23 14:38:58 +0100 | [diff] [blame] | 671 | direction_str = ISS_IS_READ(esr) ? "read" : "write"; |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 672 | |
Fuad Tabba | ba8c44d | 2019-09-23 14:38:58 +0100 | [diff] [blame] | 673 | dlog("Unhandled system register %s: op0=%d, op1=%d, crn=%d, " |
| 674 | "crm=%d, op2=%d, rt=%d.\n", |
| 675 | direction_str, GET_ISS_OP0(esr), GET_ISS_OP1(esr), |
| 676 | GET_ISS_CRN(esr), GET_ISS_CRM(esr), GET_ISS_OP2(esr), |
| 677 | GET_ISS_RT(esr)); |
| 678 | |
| 679 | /* Abort if unable to fulfill the register access. */ |
| 680 | return api_abort(vcpu); |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 681 | } |