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