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