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