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