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