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 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 9 | #include <stdnoreturn.h> |
| 10 | |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 11 | #include "hf/arch/barriers.h" |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 12 | #include "hf/arch/init.h" |
Olivier Deprez | 98ad2d2 | 2020-05-20 09:52:43 +0200 | [diff] [blame] | 13 | #include "hf/arch/mmu.h" |
Andrew Scull | 07b6bd3 | 2019-12-12 17:19:55 +0000 | [diff] [blame] | 14 | #include "hf/arch/plat/smc.h" |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 15 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 16 | #include "hf/api.h" |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 17 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 18 | #include "hf/cpu.h" |
| 19 | #include "hf/dlog.h" |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 20 | #include "hf/ffa.h" |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 21 | #include "hf/panic.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 22 | #include "hf/vm.h" |
| 23 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 24 | #include "vmapi/hf/call.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 25 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 26 | #include "debug_el1.h" |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 27 | #include "feature_id.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 28 | #include "msr.h" |
Fuad Tabba | f1d6dc5 | 2019-09-18 17:33:14 +0100 | [diff] [blame] | 29 | #include "perfmon.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 30 | #include "psci.h" |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 31 | #include "psci_handler.h" |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 32 | #include "smc.h" |
Fuad Tabba | ba8c44d | 2019-09-23 14:38:58 +0100 | [diff] [blame] | 33 | #include "sysregs.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 34 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 35 | /** |
Olivier Deprez | 98ad2d2 | 2020-05-20 09:52:43 +0200 | [diff] [blame] | 36 | * Hypervisor Fault Address Register Non-Secure. |
| 37 | */ |
| 38 | #define HPFAR_EL2_NS (UINT64_C(0x1) << 63) |
| 39 | |
| 40 | /** |
| 41 | * Hypervisor Fault Address Register Faulting IPA. |
| 42 | */ |
| 43 | #define HPFAR_EL2_FIPA (UINT64_C(0xFFFFFFFFFF0)) |
| 44 | |
| 45 | /** |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 46 | * Gets the value to increment for the next PC. |
| 47 | * The ESR encodes whether the instruction is 2 bytes or 4 bytes long. |
| 48 | */ |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 49 | #define GET_NEXT_PC_INC(esr) (GET_ESR_IL(esr) ? 4 : 2) |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 50 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 51 | /** |
Andrew Walbran | 0dd67ff | 2019-09-12 16:38:50 +0100 | [diff] [blame] | 52 | * The Client ID field within X7 for an SMC64 call. |
| 53 | */ |
| 54 | #define CLIENT_ID_MASK UINT64_C(0xffff) |
| 55 | |
| 56 | /** |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 57 | * Returns a reference to the currently executing vCPU. |
| 58 | */ |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 59 | static struct vcpu *current(void) |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 60 | { |
| 61 | return (struct vcpu *)read_msr(tpidr_el2); |
| 62 | } |
| 63 | |
Andrew Walbran | 1f8d487 | 2018-12-20 11:21:32 +0000 | [diff] [blame] | 64 | /** |
| 65 | * Saves the state of per-vCPU peripherals, such as the virtual timer, and |
| 66 | * informs the arch-independent sections that registers have been saved. |
| 67 | */ |
| 68 | void complete_saving_state(struct vcpu *vcpu) |
| 69 | { |
Andrew Walbran | 6480f8f | 2019-06-05 17:39:14 +0100 | [diff] [blame] | 70 | vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0); |
| 71 | vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0); |
Andrew Walbran | 1f8d487 | 2018-12-20 11:21:32 +0000 | [diff] [blame] | 72 | |
| 73 | api_regs_state_saved(vcpu); |
| 74 | |
| 75 | /* |
| 76 | * If switching away from the primary, copy the current EL0 virtual |
| 77 | * timer registers to the corresponding EL2 physical timer registers. |
| 78 | * This is used to emulate the virtual timer for the primary in case it |
| 79 | * should fire while the secondary is running. |
| 80 | */ |
| 81 | if (vcpu->vm->id == HF_PRIMARY_VM_ID) { |
| 82 | /* |
| 83 | * Clear timer control register before copying compare value, to |
| 84 | * avoid a spurious timer interrupt. This could be a problem if |
| 85 | * the interrupt is configured as edge-triggered, as it would |
| 86 | * then be latched in. |
| 87 | */ |
| 88 | write_msr(cnthp_ctl_el2, 0); |
| 89 | write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0)); |
| 90 | write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Restores the state of per-vCPU peripherals, such as the virtual timer. |
| 96 | */ |
| 97 | void begin_restoring_state(struct vcpu *vcpu) |
| 98 | { |
| 99 | /* |
| 100 | * Clear timer control register before restoring compare value, to avoid |
| 101 | * a spurious timer interrupt. This could be a problem if the interrupt |
| 102 | * is configured as edge-triggered, as it would then be latched in. |
| 103 | */ |
| 104 | write_msr(cntv_ctl_el0, 0); |
Andrew Walbran | 6480f8f | 2019-06-05 17:39:14 +0100 | [diff] [blame] | 105 | write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0); |
| 106 | write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0); |
Andrew Walbran | 1f8d487 | 2018-12-20 11:21:32 +0000 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * If we are switching (back) to the primary, disable the EL2 physical |
| 110 | * timer which was being used to emulate the EL0 virtual timer, as the |
| 111 | * virtual timer is now running for the primary again. |
| 112 | */ |
| 113 | if (vcpu->vm->id == HF_PRIMARY_VM_ID) { |
| 114 | write_msr(cnthp_ctl_el2, 0); |
| 115 | write_msr(cnthp_cval_el2, 0); |
| 116 | } |
| 117 | } |
| 118 | |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 119 | /** |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 120 | * Invalidate all stage 1 TLB entries on the current (physical) CPU for the |
| 121 | * current VMID. |
| 122 | */ |
| 123 | static void invalidate_vm_tlb(void) |
| 124 | { |
Andrew Walbran | cff1f68 | 2019-07-04 14:52:45 +0100 | [diff] [blame] | 125 | /* |
| 126 | * Ensure that the last VTTBR write has taken effect so we invalidate |
| 127 | * the right set of TLB entries. |
| 128 | */ |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 129 | isb(); |
Andrew Walbran | cff1f68 | 2019-07-04 14:52:45 +0100 | [diff] [blame] | 130 | |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 131 | __asm__ volatile("tlbi vmalle1"); |
Andrew Walbran | cff1f68 | 2019-07-04 14:52:45 +0100 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * Ensure that no instructions are fetched for the VM until after the |
| 135 | * TLB invalidation has taken effect. |
| 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 | |
| 139 | /* |
| 140 | * 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] | 141 | * TLB invalidation has taken effect. Non-shareable is enough because |
| 142 | * the TLB is local to the CPU. |
Andrew Walbran | cff1f68 | 2019-07-04 14:52:45 +0100 | [diff] [blame] | 143 | */ |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 144 | dsb(nsh); |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Invalidates the TLB if a different vCPU is being run than the last vCPU of |
| 149 | * the same VM which was run on the current pCPU. |
| 150 | * |
| 151 | * This is necessary because VMs may (contrary to the architecture |
| 152 | * specification) use inconsistent ASIDs across vCPUs. c.f. KVM's similar |
| 153 | * workaround: |
| 154 | * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=94d0e5980d6791b9 |
| 155 | */ |
| 156 | void maybe_invalidate_tlb(struct vcpu *vcpu) |
| 157 | { |
| 158 | size_t current_cpu_index = cpu_index(vcpu->cpu); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 159 | ffa_vcpu_index_t new_vcpu_index = vcpu_index(vcpu); |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 160 | |
| 161 | if (vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] != |
| 162 | new_vcpu_index) { |
| 163 | /* |
| 164 | * The vCPU has changed since the last time this VM was run on |
| 165 | * this pCPU, so we need to invalidate the TLB. |
| 166 | */ |
| 167 | invalidate_vm_tlb(); |
| 168 | |
| 169 | /* Record the fact that this vCPU is now running on this CPU. */ |
| 170 | vcpu->vm->arch.last_vcpu_on_cpu[current_cpu_index] = |
| 171 | new_vcpu_index; |
| 172 | } |
| 173 | } |
| 174 | |
David Brazdil | 768f69c | 2019-12-19 15:46:12 +0000 | [diff] [blame] | 175 | 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] | 176 | { |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 177 | (void)elr; |
| 178 | (void)spsr; |
| 179 | |
Fuad Tabba | d1d6798 | 2020-01-08 11:28:29 +0000 | [diff] [blame] | 180 | panic("IRQ from current exception level."); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 181 | } |
| 182 | |
David Brazdil | 768f69c | 2019-12-19 15:46:12 +0000 | [diff] [blame] | 183 | 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] | 184 | { |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 185 | (void)elr; |
| 186 | (void)spsr; |
| 187 | |
Fuad Tabba | d1d6798 | 2020-01-08 11:28:29 +0000 | [diff] [blame] | 188 | panic("FIQ from current exception level."); |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 189 | } |
| 190 | |
David Brazdil | 768f69c | 2019-12-19 15:46:12 +0000 | [diff] [blame] | 191 | 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] | 192 | { |
| 193 | (void)elr; |
| 194 | (void)spsr; |
| 195 | |
Fuad Tabba | d1d6798 | 2020-01-08 11:28:29 +0000 | [diff] [blame] | 196 | panic("SError from current exception level."); |
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 sync_current_exception_noreturn(uintreg_t elr, uintreg_t spsr) |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 200 | { |
| 201 | uintreg_t esr = read_msr(esr_el2); |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 202 | uintreg_t ec = GET_ESR_EC(esr); |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 203 | |
| 204 | (void)spsr; |
| 205 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 206 | switch (ec) { |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 207 | case EC_DATA_ABORT_SAME_EL: |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 208 | if (!(esr & (1U << 10))) { /* Check FnV bit. */ |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 209 | dlog_error( |
| 210 | "Data abort: pc=%#x, esr=%#x, ec=%#x, " |
| 211 | "far=%#x\n", |
| 212 | elr, esr, ec, read_msr(far_el2)); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 213 | } else { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 214 | dlog_error( |
| 215 | "Data abort: pc=%#x, esr=%#x, ec=%#x, " |
| 216 | "far=invalid\n", |
| 217 | elr, esr, ec); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 218 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 219 | |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 220 | break; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 221 | |
| 222 | default: |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 223 | dlog_error( |
| 224 | "Unknown current sync exception pc=%#x, esr=%#x, " |
| 225 | "ec=%#x\n", |
| 226 | elr, esr, ec); |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 227 | break; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 228 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 229 | |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 230 | panic("EL2 exception"); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 233 | /** |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 234 | * Sets or clears the VI bit in the HCR_EL2 register saved in the given |
| 235 | * arch_regs. |
| 236 | */ |
| 237 | static void set_virtual_interrupt(struct arch_regs *r, bool enable) |
| 238 | { |
| 239 | if (enable) { |
| 240 | r->lazy.hcr_el2 |= HCR_EL2_VI; |
| 241 | } else { |
| 242 | r->lazy.hcr_el2 &= ~HCR_EL2_VI; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Sets or clears the VI bit in the HCR_EL2 register. |
| 248 | */ |
| 249 | static void set_virtual_interrupt_current(bool enable) |
| 250 | { |
| 251 | uintreg_t hcr_el2 = read_msr(hcr_el2); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 252 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 253 | if (enable) { |
| 254 | hcr_el2 |= HCR_EL2_VI; |
| 255 | } else { |
| 256 | hcr_el2 &= ~HCR_EL2_VI; |
| 257 | } |
| 258 | write_msr(hcr_el2, hcr_el2); |
| 259 | } |
| 260 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 261 | /** |
| 262 | * Checks whether to block an SMC being forwarded from a VM. |
| 263 | */ |
| 264 | static bool smc_is_blocked(const struct vm *vm, uint32_t func) |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 265 | { |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 266 | bool block_by_default = !vm->smc_whitelist.permissive; |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 267 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 268 | for (size_t i = 0; i < vm->smc_whitelist.smc_count; ++i) { |
| 269 | if (func == vm->smc_whitelist.smcs[i]) { |
| 270 | return false; |
| 271 | } |
| 272 | } |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 273 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 274 | dlog_notice("SMC %#010x attempted from VM %d, blocked=%d\n", func, |
| 275 | vm->id, block_by_default); |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 276 | |
| 277 | /* Access is still allowed in permissive mode. */ |
| 278 | return block_by_default; |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 282 | * Applies SMC access control according to manifest and forwards the call if |
| 283 | * access is granted. |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 284 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 285 | static void smc_forwarder(const struct vm *vm, struct ffa_value *args) |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 286 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 287 | struct ffa_value ret; |
Andrew Walbran | 9dadaf2 | 2019-12-05 16:50:55 +0000 | [diff] [blame] | 288 | uint32_t client_id = vm->id; |
| 289 | uintreg_t arg7 = args->arg7; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 290 | |
Andrew Walbran | 9dadaf2 | 2019-12-05 16:50:55 +0000 | [diff] [blame] | 291 | if (smc_is_blocked(vm, args->func)) { |
| 292 | args->func = SMCCC_ERROR_UNKNOWN; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
Andrew Walbran | 0dd67ff | 2019-09-12 16:38:50 +0100 | [diff] [blame] | 296 | /* |
| 297 | * Set the Client ID but keep the existing Secure OS ID and anything |
| 298 | * else (currently unspecified) that the client may have passed in the |
| 299 | * upper bits. |
| 300 | */ |
Andrew Walbran | 9dadaf2 | 2019-12-05 16:50:55 +0000 | [diff] [blame] | 301 | args->arg7 = client_id | (arg7 & ~CLIENT_ID_MASK); |
Andrew Scull | 07b6bd3 | 2019-12-12 17:19:55 +0000 | [diff] [blame] | 302 | ret = smc_forward(args->func, args->arg1, args->arg2, args->arg3, |
| 303 | args->arg4, args->arg5, args->arg6, args->arg7); |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 304 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 305 | /* |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 306 | * Preserve the value passed by the caller, rather than the generated |
| 307 | * client_id. Note that this would also overwrite any return value that |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 308 | * may be in x7, but the SMCs that we are forwarding are legacy calls |
| 309 | * from before SMCCC 1.2 so won't have more than 4 return values anyway. |
| 310 | */ |
Andrew Scull | 07b6bd3 | 2019-12-12 17:19:55 +0000 | [diff] [blame] | 311 | ret.arg7 = arg7; |
| 312 | |
| 313 | plat_smc_post_forward(*args, &ret); |
| 314 | |
| 315 | *args = ret; |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 318 | static bool ffa_handler(struct ffa_value *args, struct vcpu **next) |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 319 | { |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 320 | uint32_t func = args->func & ~SMCCC_CONVENTION_MASK; |
| 321 | |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 322 | /* |
| 323 | * NOTE: When adding new methods to this handler update |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 324 | * api_ffa_features accordingly. |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 325 | */ |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 326 | switch (func) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 327 | case FFA_VERSION_32: |
| 328 | *args = api_ffa_version(args->arg1); |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 329 | return true; |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 330 | case FFA_PARTITION_INFO_GET_32: { |
| 331 | struct ffa_uuid uuid; |
| 332 | |
| 333 | ffa_uuid_init(args->arg1, args->arg2, args->arg3, args->arg4, |
| 334 | &uuid); |
| 335 | *args = api_ffa_partition_info_get(current(), &uuid); |
| 336 | return true; |
| 337 | } |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 338 | case FFA_ID_GET_32: |
| 339 | *args = api_ffa_id_get(current()); |
Andrew Walbran | d230f66 | 2019-10-07 18:03:36 +0100 | [diff] [blame] | 340 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 341 | case FFA_FEATURES_32: |
| 342 | *args = api_ffa_features(args->arg1); |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 343 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 344 | case FFA_RX_RELEASE_32: |
| 345 | *args = api_ffa_rx_release(current(), next); |
Andrew Walbran | 8a0f5ca | 2019-11-05 13:12:23 +0000 | [diff] [blame] | 346 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 347 | case FFA_RXTX_MAP_32: |
| 348 | *args = api_ffa_rxtx_map(ipa_init(args->arg1), |
| 349 | ipa_init(args->arg2), args->arg3, |
| 350 | current(), next); |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 351 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 352 | case FFA_YIELD_32: |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame^] | 353 | *args = api_yield(current(), next); |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 354 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 355 | case FFA_MSG_SEND_32: |
| 356 | *args = api_ffa_msg_send( |
| 357 | ffa_msg_send_sender(*args), |
| 358 | ffa_msg_send_receiver(*args), ffa_msg_send_size(*args), |
| 359 | ffa_msg_send_attributes(*args), current(), next); |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 360 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 361 | case FFA_MSG_WAIT_32: |
| 362 | *args = api_ffa_msg_recv(true, current(), next); |
Andrew Walbran | 0de4f16 | 2019-09-03 16:44:20 +0100 | [diff] [blame] | 363 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 364 | case FFA_MSG_POLL_32: |
| 365 | *args = api_ffa_msg_recv(false, current(), next); |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 366 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 367 | case FFA_RUN_32: |
| 368 | *args = api_ffa_run(ffa_vm_id(*args), ffa_vcpu_index(*args), |
| 369 | current(), next); |
Andrew Walbran | f0c314d | 2019-10-02 14:24:26 +0100 | [diff] [blame] | 370 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 371 | case FFA_MEM_DONATE_32: |
| 372 | case FFA_MEM_LEND_32: |
| 373 | case FFA_MEM_SHARE_32: |
| 374 | *args = api_ffa_mem_send(func, args->arg1, args->arg2, |
| 375 | ipa_init(args->arg3), args->arg4, |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 376 | current()); |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 377 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 378 | case FFA_MEM_RETRIEVE_REQ_32: |
| 379 | *args = api_ffa_mem_retrieve_req(args->arg1, args->arg2, |
| 380 | ipa_init(args->arg3), |
| 381 | args->arg4, current()); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 382 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 383 | case FFA_MEM_RELINQUISH_32: |
| 384 | *args = api_ffa_mem_relinquish(current()); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 385 | return true; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 386 | case FFA_MEM_RECLAIM_32: |
| 387 | *args = api_ffa_mem_reclaim( |
Andrew Walbran | 1bbe940 | 2020-04-30 16:47:13 +0100 | [diff] [blame] | 388 | ffa_assemble_handle(args->arg1, args->arg2), args->arg3, |
| 389 | current()); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 390 | return true; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 391 | case FFA_MEM_FRAG_RX_32: |
| 392 | *args = api_ffa_mem_frag_rx(ffa_frag_handle(*args), args->arg3, |
| 393 | (args->arg4 >> 16) & 0xffff, |
| 394 | current()); |
| 395 | return true; |
| 396 | case FFA_MEM_FRAG_TX_32: |
| 397 | *args = api_ffa_mem_frag_tx(ffa_frag_handle(*args), args->arg3, |
| 398 | (args->arg4 >> 16) & 0xffff, |
| 399 | current()); |
| 400 | return true; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame^] | 401 | case FFA_MSG_SEND_DIRECT_REQ_32: |
| 402 | *args = api_ffa_msg_send_direct_req( |
| 403 | ffa_msg_send_sender(*args), |
| 404 | ffa_msg_send_receiver(*args), *args, current(), next); |
| 405 | return true; |
| 406 | case FFA_MSG_SEND_DIRECT_RESP_32: |
| 407 | *args = api_ffa_msg_send_direct_resp( |
| 408 | ffa_msg_send_sender(*args), |
| 409 | ffa_msg_send_receiver(*args), *args, current(), next); |
| 410 | return true; |
Andrew Walbran | f0c314d | 2019-10-02 14:24:26 +0100 | [diff] [blame] | 411 | } |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 412 | |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Set or clear VI bit according to pending interrupts. |
| 418 | */ |
| 419 | static void update_vi(struct vcpu *next) |
| 420 | { |
| 421 | if (next == NULL) { |
| 422 | /* |
| 423 | * Not switching vCPUs, set the bit for the current vCPU |
| 424 | * directly in the register. |
| 425 | */ |
| 426 | struct vcpu *vcpu = current(); |
| 427 | |
| 428 | sl_lock(&vcpu->lock); |
| 429 | set_virtual_interrupt_current( |
| 430 | vcpu->interrupts.enabled_and_pending_count > 0); |
| 431 | sl_unlock(&vcpu->lock); |
| 432 | } else { |
| 433 | /* |
| 434 | * About to switch vCPUs, set the bit for the vCPU to which we |
| 435 | * are switching in the saved copy of the register. |
| 436 | */ |
| 437 | sl_lock(&next->lock); |
| 438 | set_virtual_interrupt( |
| 439 | &next->regs, |
| 440 | next->interrupts.enabled_and_pending_count > 0); |
| 441 | sl_unlock(&next->lock); |
| 442 | } |
| 443 | } |
| 444 | |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 445 | /** |
| 446 | * Processes SMC instruction calls. |
| 447 | */ |
Andrew Walbran | 9dadaf2 | 2019-12-05 16:50:55 +0000 | [diff] [blame] | 448 | static struct vcpu *smc_handler(struct vcpu *vcpu) |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 449 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 450 | struct ffa_value args = { |
Andrew Walbran | 85c3766 | 2019-12-05 16:29:33 +0000 | [diff] [blame] | 451 | .func = vcpu->regs.r[0], |
| 452 | .arg1 = vcpu->regs.r[1], |
| 453 | .arg2 = vcpu->regs.r[2], |
| 454 | .arg3 = vcpu->regs.r[3], |
| 455 | .arg4 = vcpu->regs.r[4], |
| 456 | .arg5 = vcpu->regs.r[5], |
| 457 | .arg6 = vcpu->regs.r[6], |
| 458 | .arg7 = vcpu->regs.r[7], |
| 459 | }; |
Andrew Walbran | 9dadaf2 | 2019-12-05 16:50:55 +0000 | [diff] [blame] | 460 | struct vcpu *next = NULL; |
Fuad Tabba | 8176e3e | 2019-08-01 10:40:36 +0100 | [diff] [blame] | 461 | |
Andrew Walbran | 85c3766 | 2019-12-05 16:29:33 +0000 | [diff] [blame] | 462 | if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3, |
Andrew Walbran | 9dadaf2 | 2019-12-05 16:50:55 +0000 | [diff] [blame] | 463 | &vcpu->regs.r[0], &next)) { |
| 464 | return next; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 467 | if (ffa_handler(&args, &next)) { |
Andrew Walbran | 9dadaf2 | 2019-12-05 16:50:55 +0000 | [diff] [blame] | 468 | arch_regs_set_retval(&vcpu->regs, args); |
| 469 | update_vi(next); |
| 470 | return next; |
Andrew Walbran | 4579f700 | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Andrew Walbran | 85c3766 | 2019-12-05 16:29:33 +0000 | [diff] [blame] | 473 | switch (args.func & ~SMCCC_CONVENTION_MASK) { |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 474 | case HF_DEBUG_LOG: |
Andrew Walbran | 9dadaf2 | 2019-12-05 16:50:55 +0000 | [diff] [blame] | 475 | vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu); |
Andrew Scull | 07b6bd3 | 2019-12-12 17:19:55 +0000 | [diff] [blame] | 476 | return NULL; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Andrew Walbran | 9dadaf2 | 2019-12-05 16:50:55 +0000 | [diff] [blame] | 479 | smc_forwarder(vcpu->vm, &args); |
| 480 | arch_regs_set_retval(&vcpu->regs, args); |
Andrew Scull | 07b6bd3 | 2019-12-12 17:19:55 +0000 | [diff] [blame] | 481 | return NULL; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 484 | /* |
| 485 | * Exception vector offsets. |
| 486 | * See Arm Architecture Reference Manual Armv8-A, D1.10.2. |
| 487 | */ |
| 488 | |
| 489 | /** |
| 490 | * Offset for synchronous exceptions at current EL with SPx. |
| 491 | */ |
| 492 | #define OFFSET_CURRENT_SPX UINT64_C(0x200) |
| 493 | |
| 494 | /** |
| 495 | * Offset for synchronous exceptions at lower EL using AArch64. |
| 496 | */ |
| 497 | #define OFFSET_LOWER_EL_64 UINT64_C(0x400) |
| 498 | |
| 499 | /** |
| 500 | * Offset for synchronous exceptions at lower EL using AArch32. |
| 501 | */ |
| 502 | #define OFFSET_LOWER_EL_32 UINT64_C(0x600) |
| 503 | |
| 504 | /** |
| 505 | * Returns the address for the exception handler at EL1. |
| 506 | */ |
| 507 | static uintreg_t get_el1_exception_handler_addr(const struct vcpu *vcpu) |
| 508 | { |
| 509 | uintreg_t base_addr = read_msr(vbar_el1); |
| 510 | uintreg_t pe_mode = vcpu->regs.spsr & PSR_PE_MODE_MASK; |
| 511 | bool is_arch32 = vcpu->regs.spsr & PSR_ARCH_MODE_32; |
| 512 | |
| 513 | if (pe_mode == PSR_PE_MODE_EL0T) { |
| 514 | if (is_arch32) { |
| 515 | base_addr += OFFSET_LOWER_EL_32; |
| 516 | } else { |
| 517 | base_addr += OFFSET_LOWER_EL_64; |
| 518 | } |
| 519 | } else { |
| 520 | CHECK(!is_arch32); |
| 521 | base_addr += OFFSET_CURRENT_SPX; |
| 522 | } |
| 523 | |
| 524 | return base_addr; |
| 525 | } |
| 526 | |
| 527 | /** |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 528 | * Injects an exception with the specified Exception Syndrom Register value into |
| 529 | * the EL1. |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 530 | * |
| 531 | * NOTE: This function assumes that the lazy registers haven't been saved, and |
| 532 | * writes to the lazy registers of the CPU directly instead of the vCPU. |
| 533 | */ |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 534 | static void inject_el1_exception(struct vcpu *vcpu, uintreg_t esr_el1_value, |
| 535 | uintreg_t far_el1_value) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 536 | { |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 537 | uintreg_t handler_address = get_el1_exception_handler_addr(vcpu); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 538 | |
| 539 | /* Update the CPU state to inject the exception. */ |
| 540 | write_msr(esr_el1, esr_el1_value); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 541 | write_msr(far_el1, far_el1_value); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 542 | write_msr(elr_el1, vcpu->regs.pc); |
| 543 | write_msr(spsr_el1, vcpu->regs.spsr); |
| 544 | |
| 545 | /* |
| 546 | * Mask (disable) interrupts and run in EL1h mode. |
| 547 | * EL1h mode is used because by default, taking an exception selects the |
| 548 | * stack pointer for the target Exception level. The software can change |
| 549 | * that later in the handler if needed. |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 550 | */ |
| 551 | vcpu->regs.spsr = PSR_D | PSR_A | PSR_I | PSR_F | PSR_PE_MODE_EL1H; |
| 552 | |
| 553 | /* Transfer control to the exception hander. */ |
| 554 | vcpu->regs.pc = handler_address; |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Injects a Data Abort exception (same exception level). |
| 559 | */ |
| 560 | static void inject_el1_data_abort_exception(struct vcpu *vcpu, |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 561 | uintreg_t esr_el2, |
| 562 | uintreg_t far_el2) |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 563 | { |
| 564 | /* |
| 565 | * ISS encoding remains the same, but the EC is changed to reflect |
| 566 | * where the exception came from. |
| 567 | * See Arm Architecture Reference Manual Armv8-A, pages D13-2943/2982. |
| 568 | */ |
| 569 | uintreg_t esr_el1_value = GET_ESR_ISS(esr_el2) | GET_ESR_IL(esr_el2) | |
| 570 | (EC_DATA_ABORT_SAME_EL << ESR_EC_OFFSET); |
| 571 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 572 | dlog_notice("Injecting Data Abort exception into VM%d.\n", |
| 573 | vcpu->vm->id); |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 574 | |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 575 | inject_el1_exception(vcpu, esr_el1_value, far_el2); |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Injects a Data Abort exception (same exception level). |
| 580 | */ |
| 581 | static void inject_el1_instruction_abort_exception(struct vcpu *vcpu, |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 582 | uintreg_t esr_el2, |
| 583 | uintreg_t far_el2) |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 584 | { |
| 585 | /* |
| 586 | * ISS encoding remains the same, but the EC is changed to reflect |
| 587 | * where the exception came from. |
| 588 | * See Arm Architecture Reference Manual Armv8-A, pages D13-2941/2980. |
| 589 | */ |
| 590 | uintreg_t esr_el1_value = |
| 591 | GET_ESR_ISS(esr_el2) | GET_ESR_IL(esr_el2) | |
| 592 | (EC_INSTRUCTION_ABORT_SAME_EL << ESR_EC_OFFSET); |
| 593 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 594 | dlog_notice("Injecting Instruction Abort exception into VM%d.\n", |
| 595 | vcpu->vm->id); |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 596 | |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 597 | inject_el1_exception(vcpu, esr_el1_value, far_el2); |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Injects an exception with an unknown reason into the EL1. |
| 602 | */ |
| 603 | static void inject_el1_unknown_exception(struct vcpu *vcpu, uintreg_t esr_el2) |
| 604 | { |
| 605 | uintreg_t esr_el1_value = |
| 606 | GET_ESR_IL(esr_el2) | (EC_UNKNOWN << ESR_EC_OFFSET); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 607 | |
| 608 | /* |
| 609 | * The value of the far_el2 register is UNKNOWN in this case, |
| 610 | * therefore, don't propagate it to avoid leaking sensitive information. |
| 611 | */ |
| 612 | uintreg_t far_el1_value = 0; |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 613 | char *direction_str; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 614 | |
| 615 | direction_str = ISS_IS_READ(esr_el2) ? "read" : "write"; |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 616 | dlog_notice( |
| 617 | "Trapped access to system register %s: op0=%d, op1=%d, crn=%d, " |
| 618 | "crm=%d, op2=%d, rt=%d.\n", |
| 619 | direction_str, GET_ISS_OP0(esr_el2), GET_ISS_OP1(esr_el2), |
| 620 | GET_ISS_CRN(esr_el2), GET_ISS_CRM(esr_el2), |
| 621 | GET_ISS_OP2(esr_el2), GET_ISS_RT(esr_el2)); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 622 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 623 | dlog_notice("Injecting Unknown Reason exception into VM%d.\n", |
| 624 | vcpu->vm->id); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 625 | |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 626 | inject_el1_exception(vcpu, esr_el1_value, far_el1_value); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 629 | struct vcpu *hvc_handler(struct vcpu *vcpu) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 630 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 631 | struct ffa_value args = { |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 632 | .func = vcpu->regs.r[0], |
| 633 | .arg1 = vcpu->regs.r[1], |
| 634 | .arg2 = vcpu->regs.r[2], |
| 635 | .arg3 = vcpu->regs.r[3], |
| 636 | .arg4 = vcpu->regs.r[4], |
| 637 | .arg5 = vcpu->regs.r[5], |
| 638 | .arg6 = vcpu->regs.r[6], |
| 639 | .arg7 = vcpu->regs.r[7], |
| 640 | }; |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 641 | struct vcpu *next = NULL; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 642 | |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 643 | if (psci_handler(vcpu, args.func, args.arg1, args.arg2, args.arg3, |
| 644 | &vcpu->regs.r[0], &next)) { |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 645 | return next; |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 646 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 647 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 648 | if (ffa_handler(&args, &next)) { |
Andrew Walbran | 6f56d7b | 2019-12-05 16:27:34 +0000 | [diff] [blame] | 649 | arch_regs_set_retval(&vcpu->regs, args); |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 650 | update_vi(next); |
| 651 | return next; |
Andrew Walbran | 7d28d9a | 2019-08-30 16:24:58 +0100 | [diff] [blame] | 652 | } |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 653 | |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 654 | switch (args.func) { |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 655 | case HF_MAILBOX_WRITABLE_GET: |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 656 | vcpu->regs.r[0] = api_mailbox_writable_get(vcpu); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 657 | break; |
| 658 | |
| 659 | case HF_MAILBOX_WAITER_GET: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 660 | vcpu->regs.r[0] = api_mailbox_waiter_get(args.arg1, vcpu); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 661 | break; |
| 662 | |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 663 | case HF_INTERRUPT_ENABLE: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 664 | vcpu->regs.r[0] = |
| 665 | api_interrupt_enable(args.arg1, args.arg2, vcpu); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 666 | break; |
| 667 | |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 668 | case HF_INTERRUPT_GET: |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 669 | vcpu->regs.r[0] = api_interrupt_get(vcpu); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 670 | break; |
| 671 | |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 672 | case HF_INTERRUPT_INJECT: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 673 | vcpu->regs.r[0] = api_interrupt_inject(args.arg1, args.arg2, |
| 674 | args.arg3, vcpu, &next); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 675 | break; |
| 676 | |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 677 | case HF_DEBUG_LOG: |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 678 | vcpu->regs.r[0] = api_debug_log(args.arg1, vcpu); |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 679 | break; |
| 680 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 681 | default: |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 682 | vcpu->regs.r[0] = SMCCC_ERROR_UNKNOWN; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 683 | } |
| 684 | |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 685 | update_vi(next); |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 686 | |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 687 | return next; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 690 | struct vcpu *irq_lower(void) |
| 691 | { |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 692 | /* |
| 693 | * Switch back to primary VM, interrupts will be handled there. |
| 694 | * |
| 695 | * If the VM has aborted, this vCPU will be aborted when the scheduler |
| 696 | * tries to run it again. This means the interrupt will not be delayed |
| 697 | * by the aborted VM. |
| 698 | * |
| 699 | * TODO: Only switch when the interrupt isn't for the current VM. |
| 700 | */ |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 701 | return api_preempt(current()); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 704 | struct vcpu *fiq_lower(void) |
| 705 | { |
| 706 | return irq_lower(); |
| 707 | } |
| 708 | |
Fuad Tabba | d1d6798 | 2020-01-08 11:28:29 +0000 | [diff] [blame] | 709 | noreturn struct vcpu *serr_lower(void) |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 710 | { |
Fuad Tabba | d1d6798 | 2020-01-08 11:28:29 +0000 | [diff] [blame] | 711 | /* |
| 712 | * SError exceptions should be isolated and handled by the responsible |
| 713 | * VM/exception level. Getting here indicates a bug, that isolation is |
| 714 | * not working, or a processor that does not support ARMv8.2-IESB, in |
| 715 | * which case Hafnium routes SError exceptions to EL2 (here). |
| 716 | */ |
| 717 | panic("SError from a lower exception level."); |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 720 | /** |
| 721 | * Initialises a fault info structure. It assumes that an FnV bit exists at |
| 722 | * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of |
| 723 | * the ESR (the fault status code) are 010000; this is the case for both |
| 724 | * instruction and data aborts, but not necessarily for other exception reasons. |
| 725 | */ |
| 726 | static struct vcpu_fault_info fault_info_init(uintreg_t esr, |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 727 | const struct vcpu *vcpu, |
| 728 | uint32_t mode) |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 729 | { |
| 730 | uint32_t fsc = esr & 0x3f; |
| 731 | struct vcpu_fault_info r; |
Olivier Deprez | 98ad2d2 | 2020-05-20 09:52:43 +0200 | [diff] [blame] | 732 | uint64_t hpfar_el2_val; |
| 733 | uint64_t hpfar_el2_fipa; |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 734 | |
| 735 | r.mode = mode; |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 736 | r.pc = va_init(vcpu->regs.pc); |
| 737 | |
Olivier Deprez | 98ad2d2 | 2020-05-20 09:52:43 +0200 | [diff] [blame] | 738 | /* Get Hypervisor IPA Fault Address value. */ |
| 739 | hpfar_el2_val = read_msr(hpfar_el2); |
| 740 | |
| 741 | /* Extract Faulting IPA. */ |
| 742 | hpfar_el2_fipa = (hpfar_el2_val & HPFAR_EL2_FIPA) << 8; |
| 743 | |
| 744 | #if SECURE_WORLD == 1 |
| 745 | |
| 746 | /** |
| 747 | * Determine if faulting IPA targets NS space. |
| 748 | * At NS-EL2 hpfar_el2 bit 63 is RES0. At S-EL2, this bit determines if |
| 749 | * the faulting Stage-1 address output is a secure or non-secure IPA. |
| 750 | */ |
| 751 | if ((hpfar_el2_val & HPFAR_EL2_NS) != 0) { |
| 752 | r.mode |= MM_MODE_NS; |
| 753 | } |
| 754 | |
| 755 | #endif |
| 756 | |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 757 | /* |
| 758 | * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It |
| 759 | * indicates that we cannot rely on far_el2. |
| 760 | */ |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 761 | if (fsc == 0x10 && esr & (1U << 10)) { |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 762 | r.vaddr = va_init(0); |
Olivier Deprez | 98ad2d2 | 2020-05-20 09:52:43 +0200 | [diff] [blame] | 763 | r.ipaddr = ipa_init(hpfar_el2_fipa); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 764 | } else { |
| 765 | r.vaddr = va_init(read_msr(far_el2)); |
Olivier Deprez | 98ad2d2 | 2020-05-20 09:52:43 +0200 | [diff] [blame] | 766 | r.ipaddr = ipa_init(hpfar_el2_fipa | |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 767 | (read_msr(far_el2) & (PAGE_SIZE - 1))); |
| 768 | } |
| 769 | |
| 770 | return r; |
| 771 | } |
| 772 | |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 773 | struct vcpu *sync_lower_exception(uintreg_t esr, uintreg_t far) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 774 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 775 | struct vcpu *vcpu = current(); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 776 | struct vcpu_fault_info info; |
Jose Marinho | 135dff3 | 2019-02-28 10:25:57 +0000 | [diff] [blame] | 777 | struct vcpu *new_vcpu; |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 778 | uintreg_t ec = GET_ESR_EC(esr); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 779 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 780 | switch (ec) { |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 781 | case EC_WFI_WFE: |
Andrew Walbran | 48196eb | 2019-03-04 14:56:24 +0000 | [diff] [blame] | 782 | /* Skip the instruction. */ |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 783 | vcpu->regs.pc += GET_NEXT_PC_INC(esr); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 784 | /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 785 | if (esr & 1) { |
Andrew Walbran | 48196eb | 2019-03-04 14:56:24 +0000 | [diff] [blame] | 786 | /* WFE */ |
| 787 | /* |
| 788 | * TODO: consider giving the scheduler more context, |
| 789 | * somehow. |
| 790 | */ |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 791 | api_yield(vcpu, &new_vcpu); |
Jose Marinho | 135dff3 | 2019-02-28 10:25:57 +0000 | [diff] [blame] | 792 | return new_vcpu; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 793 | } |
Andrew Walbran | 48196eb | 2019-03-04 14:56:24 +0000 | [diff] [blame] | 794 | /* WFI */ |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 795 | return api_wait_for_interrupt(vcpu); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 796 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 797 | case EC_DATA_ABORT_LOWER_EL: |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 798 | info = fault_info_init( |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 799 | esr, vcpu, (esr & (1U << 6)) ? MM_MODE_W : MM_MODE_R); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 800 | if (vcpu_handle_page_fault(vcpu, &info)) { |
| 801 | return NULL; |
| 802 | } |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 803 | /* Inform the EL1 of the data abort. */ |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 804 | inject_el1_data_abort_exception(vcpu, esr, far); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 805 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 806 | /* Schedule the same VM to continue running. */ |
| 807 | return NULL; |
| 808 | |
| 809 | case EC_INSTRUCTION_ABORT_LOWER_EL: |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 810 | info = fault_info_init(esr, vcpu, MM_MODE_X); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 811 | if (vcpu_handle_page_fault(vcpu, &info)) { |
| 812 | return NULL; |
| 813 | } |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 814 | /* Inform the EL1 of the instruction abort. */ |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 815 | inject_el1_instruction_abort_exception(vcpu, esr, far); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 816 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 817 | /* Schedule the same VM to continue running. */ |
| 818 | return NULL; |
| 819 | |
| 820 | case EC_HVC: |
Andrew Walbran | 59182d5 | 2019-09-23 17:55:39 +0100 | [diff] [blame] | 821 | return hvc_handler(vcpu); |
| 822 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 823 | case EC_SMC: { |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 824 | uintreg_t smc_pc = vcpu->regs.pc; |
Andrew Walbran | 9dadaf2 | 2019-12-05 16:50:55 +0000 | [diff] [blame] | 825 | struct vcpu *next = smc_handler(vcpu); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 826 | |
| 827 | /* Skip the SMC instruction. */ |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 828 | vcpu->regs.pc = smc_pc + GET_NEXT_PC_INC(esr); |
Andrew Walbran | 9dadaf2 | 2019-12-05 16:50:55 +0000 | [diff] [blame] | 829 | |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 830 | return next; |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 831 | } |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 832 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 833 | case EC_MSR: |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 834 | /* |
| 835 | * NOTE: This should never be reached because it goes through a |
| 836 | * separate path handled by handle_system_register_access(). |
| 837 | */ |
| 838 | panic("Handled by handle_system_register_access()."); |
| 839 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 840 | default: |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 841 | dlog_notice( |
| 842 | "Unknown lower sync exception pc=%#x, esr=%#x, " |
| 843 | "ec=%#x\n", |
| 844 | vcpu->regs.pc, esr, ec); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 845 | break; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 846 | } |
| 847 | |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 848 | /* |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 849 | * The exception wasn't handled. Inject to the VM to give it chance to |
| 850 | * handle as an unknown exception. |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 851 | */ |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 852 | inject_el1_unknown_exception(vcpu, esr); |
| 853 | |
| 854 | /* Schedule the same VM to continue running. */ |
| 855 | return NULL; |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 858 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 859 | * Handles EC = 011000, MSR, MRS instruction traps. |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 860 | * 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] | 861 | */ |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 862 | void handle_system_register_access(uintreg_t esr_el2) |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 863 | { |
| 864 | struct vcpu *vcpu = current(); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 865 | ffa_vm_id_t vm_id = vcpu->vm->id; |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 866 | uintreg_t ec = GET_ESR_EC(esr_el2); |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 867 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 868 | CHECK(ec == EC_MSR); |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 869 | /* |
Fuad Tabba | f1d6dc5 | 2019-09-18 17:33:14 +0100 | [diff] [blame] | 870 | * Handle accesses to debug and performance monitor registers. |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 871 | * Inject an exception for unhandled/unsupported registers. |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 872 | */ |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 873 | if (debug_el1_is_register_access(esr_el2)) { |
| 874 | if (!debug_el1_process_access(vcpu, vm_id, esr_el2)) { |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 875 | inject_el1_unknown_exception(vcpu, esr_el2); |
| 876 | return; |
Fuad Tabba | f1d6dc5 | 2019-09-18 17:33:14 +0100 | [diff] [blame] | 877 | } |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 878 | } else if (perfmon_is_register_access(esr_el2)) { |
| 879 | if (!perfmon_process_access(vcpu, vm_id, esr_el2)) { |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 880 | inject_el1_unknown_exception(vcpu, esr_el2); |
| 881 | return; |
Fuad Tabba | f1d6dc5 | 2019-09-18 17:33:14 +0100 | [diff] [blame] | 882 | } |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 883 | } else if (feature_id_is_register_access(esr_el2)) { |
| 884 | if (!feature_id_process_access(vcpu, esr_el2)) { |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 885 | inject_el1_unknown_exception(vcpu, esr_el2); |
| 886 | return; |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 887 | } |
Fuad Tabba | f1d6dc5 | 2019-09-18 17:33:14 +0100 | [diff] [blame] | 888 | } else { |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 889 | inject_el1_unknown_exception(vcpu, esr_el2); |
| 890 | return; |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Fuad Tabba | f1d6dc5 | 2019-09-18 17:33:14 +0100 | [diff] [blame] | 893 | /* Instruction was fulfilled. Skip it and run the next one. */ |
Fuad Tabba | 3e9b022 | 2019-11-11 16:47:50 +0000 | [diff] [blame] | 894 | vcpu->regs.pc += GET_NEXT_PC_INC(esr_el2); |
Fuad Tabba | c76466d | 2019-09-06 10:42:12 +0100 | [diff] [blame] | 895 | } |