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 | |
| 19 | #include "hf/arch/init.h" |
| 20 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 21 | #include "hf/api.h" |
| 22 | #include "hf/cpu.h" |
| 23 | #include "hf/dlog.h" |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 24 | #include "hf/panic.h" |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 25 | #include "hf/spci.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 26 | #include "hf/vm.h" |
| 27 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 28 | #include "vmapi/hf/call.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 29 | |
| 30 | #include "msr.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 31 | #include "psci.h" |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 32 | #include "smc.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 33 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 34 | #define HCR_EL2_VI (1u << 7) |
| 35 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 36 | struct hvc_handler_return { |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 37 | uintreg_t user_ret; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 38 | struct vcpu *new; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 41 | void cpu_entry(struct cpu *c); |
| 42 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 43 | static uint32_t el3_psci_version = 0; |
| 44 | |
| 45 | /* Performs arch specific boot time initialisation. */ |
| 46 | void arch_one_time_init(void) |
| 47 | { |
| 48 | el3_psci_version = smc(PSCI_VERSION, 0, 0, 0); |
| 49 | |
| 50 | /* Check there's nothing unexpected about PSCI. */ |
| 51 | switch (el3_psci_version) { |
| 52 | case PSCI_VERSION_0_2: |
| 53 | case PSCI_VERSION_1_0: |
| 54 | case PSCI_VERSION_1_1: |
| 55 | /* Supported EL3 PSCI version. */ |
| 56 | dlog("Found PSCI version: 0x%x\n", el3_psci_version); |
| 57 | break; |
| 58 | |
| 59 | default: |
| 60 | /* Unsupported EL3 PSCI version. Log a warning but continue. */ |
| 61 | dlog("Warning: unknown PSCI version: 0x%x\n", el3_psci_version); |
| 62 | el3_psci_version = 0; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* Gets a reference to the currently executing vCPU. */ |
| 68 | static struct vcpu *current(void) |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 69 | { |
| 70 | return (struct vcpu *)read_msr(tpidr_el2); |
| 71 | } |
| 72 | |
Andrew Walbran | 1f8d487 | 2018-12-20 11:21:32 +0000 | [diff] [blame] | 73 | /** |
| 74 | * Saves the state of per-vCPU peripherals, such as the virtual timer, and |
| 75 | * informs the arch-independent sections that registers have been saved. |
| 76 | */ |
| 77 | void complete_saving_state(struct vcpu *vcpu) |
| 78 | { |
Andrew Walbran | 6480f8f | 2019-06-05 17:39:14 +0100 | [diff] [blame] | 79 | vcpu->regs.peripherals.cntv_cval_el0 = read_msr(cntv_cval_el0); |
| 80 | vcpu->regs.peripherals.cntv_ctl_el0 = read_msr(cntv_ctl_el0); |
Andrew Walbran | 1f8d487 | 2018-12-20 11:21:32 +0000 | [diff] [blame] | 81 | |
| 82 | api_regs_state_saved(vcpu); |
| 83 | |
| 84 | /* |
| 85 | * If switching away from the primary, copy the current EL0 virtual |
| 86 | * timer registers to the corresponding EL2 physical timer registers. |
| 87 | * This is used to emulate the virtual timer for the primary in case it |
| 88 | * should fire while the secondary is running. |
| 89 | */ |
| 90 | if (vcpu->vm->id == HF_PRIMARY_VM_ID) { |
| 91 | /* |
| 92 | * Clear timer control register before copying compare value, to |
| 93 | * avoid a spurious timer interrupt. This could be a problem if |
| 94 | * the interrupt is configured as edge-triggered, as it would |
| 95 | * then be latched in. |
| 96 | */ |
| 97 | write_msr(cnthp_ctl_el2, 0); |
| 98 | write_msr(cnthp_cval_el2, read_msr(cntv_cval_el0)); |
| 99 | write_msr(cnthp_ctl_el2, read_msr(cntv_ctl_el0)); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Restores the state of per-vCPU peripherals, such as the virtual timer. |
| 105 | */ |
| 106 | void begin_restoring_state(struct vcpu *vcpu) |
| 107 | { |
| 108 | /* |
| 109 | * Clear timer control register before restoring compare value, to avoid |
| 110 | * a spurious timer interrupt. This could be a problem if the interrupt |
| 111 | * is configured as edge-triggered, as it would then be latched in. |
| 112 | */ |
| 113 | write_msr(cntv_ctl_el0, 0); |
Andrew Walbran | 6480f8f | 2019-06-05 17:39:14 +0100 | [diff] [blame] | 114 | write_msr(cntv_cval_el0, vcpu->regs.peripherals.cntv_cval_el0); |
| 115 | write_msr(cntv_ctl_el0, vcpu->regs.peripherals.cntv_ctl_el0); |
Andrew Walbran | 1f8d487 | 2018-12-20 11:21:32 +0000 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * If we are switching (back) to the primary, disable the EL2 physical |
| 119 | * timer which was being used to emulate the EL0 virtual timer, as the |
| 120 | * virtual timer is now running for the primary again. |
| 121 | */ |
| 122 | if (vcpu->vm->id == HF_PRIMARY_VM_ID) { |
| 123 | write_msr(cnthp_ctl_el2, 0); |
| 124 | write_msr(cnthp_cval_el2, 0); |
| 125 | } |
| 126 | } |
| 127 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 128 | noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 129 | { |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 130 | (void)elr; |
| 131 | (void)spsr; |
| 132 | |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 133 | panic("IRQ from current"); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 136 | noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 137 | { |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 138 | (void)elr; |
| 139 | (void)spsr; |
| 140 | |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 141 | panic("FIQ from current"); |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 144 | noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr) |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 145 | { |
| 146 | (void)elr; |
| 147 | (void)spsr; |
| 148 | |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 149 | panic("SERR from current"); |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 152 | noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr) |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 153 | { |
| 154 | uintreg_t esr = read_msr(esr_el2); |
| 155 | |
| 156 | (void)spsr; |
| 157 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 158 | switch (esr >> 26) { |
| 159 | case 0x25: /* EC = 100101, Data abort. */ |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 160 | dlog("Data abort: pc=0x%x, esr=0x%x, ec=0x%x", elr, esr, |
| 161 | esr >> 26); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 162 | if (!(esr & (1u << 10))) { /* Check FnV bit. */ |
Andrew Scull | 0a029e8 | 2018-11-23 16:48:08 +0000 | [diff] [blame] | 163 | dlog(", far=0x%x", read_msr(far_el2)); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 164 | } else { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 165 | dlog(", far=invalid"); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 166 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 167 | |
| 168 | dlog("\n"); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 169 | break; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 170 | |
| 171 | default: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 172 | dlog("Unknown current sync exception pc=0x%x, esr=0x%x, " |
| 173 | "ec=0x%x\n", |
| 174 | elr, esr, esr >> 26); |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 175 | break; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 176 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 177 | |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 178 | panic("EL2 exception"); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 181 | /** |
| 182 | * Handles PSCI requests received via HVC or SMC instructions from the primary |
| 183 | * VM only. |
| 184 | * |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 185 | * A minimal PSCI 1.1 interface is offered which can make use of previous |
| 186 | * version of PSCI in EL3 by acting as an adapter. |
| 187 | * |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 188 | * Returns true if the request was a PSCI one, false otherwise. |
| 189 | */ |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 190 | static bool psci_handler(uint32_t func, uintreg_t arg0, uintreg_t arg1, |
| 191 | uintreg_t arg2, int32_t *ret) |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 192 | { |
| 193 | struct cpu *c; |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 194 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 195 | /* |
| 196 | * If there's a problem with the EL3 PSCI, block standard secure service |
| 197 | * calls by marking them as unknown. Other calls will be allowed to pass |
| 198 | * through. |
| 199 | * |
| 200 | * This blocks more calls than just PSCI so it may need to be made more |
| 201 | * lenient in future. |
| 202 | */ |
| 203 | if (el3_psci_version == 0) { |
| 204 | *ret = SMCCC_RETURN_UNKNOWN; |
| 205 | return (func & SMCCC_SERVICE_CALL_MASK) == |
| 206 | SMCCC_STANDARD_SECURE_SERVICE_CALL; |
| 207 | } |
| 208 | |
| 209 | switch (func & ~SMCCC_CONVENTION_MASK) { |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 210 | case PSCI_VERSION: |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 211 | *ret = PSCI_VERSION_1_1; |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 212 | break; |
| 213 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 214 | case PSCI_FEATURES: |
| 215 | switch (arg0 & ~SMCCC_CONVENTION_MASK) { |
| 216 | case PSCI_CPU_SUSPEND: |
| 217 | if (el3_psci_version == PSCI_VERSION_0_2) { |
| 218 | /* |
| 219 | * PSCI 0.2 doesn't support PSCI_FEATURES so |
| 220 | * report PSCI 0.2 compatible features. |
| 221 | */ |
| 222 | *ret = 0; |
| 223 | } else { |
| 224 | /* PSCI 1.x only defines two feature bits. */ |
| 225 | *ret = smc(func, arg0, 0, 0) & 0x3; |
| 226 | } |
| 227 | break; |
| 228 | |
| 229 | case PSCI_VERSION: |
| 230 | case PSCI_FEATURES: |
| 231 | case PSCI_SYSTEM_OFF: |
| 232 | case PSCI_SYSTEM_RESET: |
| 233 | case PSCI_AFFINITY_INFO: |
| 234 | case PSCI_CPU_OFF: |
| 235 | case PSCI_CPU_ON: |
| 236 | /* These are supported without special features. */ |
| 237 | *ret = 0; |
| 238 | break; |
| 239 | |
| 240 | default: |
| 241 | /* Everything else is unsupported. */ |
| 242 | *ret = PSCI_RETURN_NOT_SUPPORTED; |
| 243 | break; |
| 244 | } |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 245 | break; |
| 246 | |
| 247 | case PSCI_SYSTEM_OFF: |
| 248 | smc(PSCI_SYSTEM_OFF, 0, 0, 0); |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 249 | panic("System off failed"); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 250 | break; |
| 251 | |
| 252 | case PSCI_SYSTEM_RESET: |
| 253 | smc(PSCI_SYSTEM_RESET, 0, 0, 0); |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 254 | panic("System reset failed"); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 255 | break; |
| 256 | |
| 257 | case PSCI_AFFINITY_INFO: |
| 258 | c = cpu_find(arg0); |
| 259 | if (!c) { |
| 260 | *ret = PSCI_RETURN_INVALID_PARAMETERS; |
| 261 | break; |
| 262 | } |
| 263 | |
| 264 | if (arg1 != 0) { |
| 265 | *ret = PSCI_RETURN_NOT_SUPPORTED; |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | sl_lock(&c->lock); |
| 270 | if (c->is_on) { |
| 271 | *ret = 0; /* ON */ |
| 272 | } else { |
| 273 | *ret = 1; /* OFF */ |
| 274 | } |
| 275 | sl_unlock(&c->lock); |
| 276 | break; |
| 277 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 278 | case PSCI_CPU_SUSPEND: { |
| 279 | /* |
| 280 | * Update vcpu state to wake from the provided entry point but |
| 281 | * if suspend returns, for example because it failed or was a |
| 282 | * standby power state, the SMC will return and the updated |
| 283 | * vcpu registers will be ignored. |
| 284 | */ |
| 285 | struct vcpu *vcpu = current(); |
| 286 | |
| 287 | arch_regs_set_pc_arg(&vcpu->regs, ipa_init(arg1), arg2); |
| 288 | *ret = smc(PSCI_CPU_SUSPEND | SMCCC_64_BIT, arg0, |
| 289 | (uintreg_t)&cpu_entry, (uintreg_t)vcpu->cpu); |
| 290 | break; |
| 291 | } |
| 292 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 293 | case PSCI_CPU_OFF: |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 294 | cpu_off(current()->cpu); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 295 | smc(PSCI_CPU_OFF, 0, 0, 0); |
Andrew Scull | a9c172d | 2019-04-03 14:10:00 +0100 | [diff] [blame] | 296 | panic("CPU off failed"); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 297 | break; |
| 298 | |
| 299 | case PSCI_CPU_ON: |
| 300 | c = cpu_find(arg0); |
| 301 | if (!c) { |
| 302 | *ret = PSCI_RETURN_INVALID_PARAMETERS; |
| 303 | break; |
| 304 | } |
| 305 | |
Andrew Scull | 1b8d044 | 2018-08-06 15:47:04 +0100 | [diff] [blame] | 306 | if (cpu_on(c, ipa_init(arg1), arg2)) { |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 307 | *ret = PSCI_RETURN_ALREADY_ON; |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * There's a race when turning a CPU on when it's in the |
| 313 | * process of turning off. We need to loop here while it is |
| 314 | * reported that the CPU is on (because it's about to turn |
| 315 | * itself off). |
| 316 | */ |
| 317 | do { |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 318 | *ret = smc(PSCI_CPU_ON | SMCCC_64_BIT, arg0, |
| 319 | (uintreg_t)&cpu_entry, (uintreg_t)c); |
| 320 | } while (*ret == PSCI_RETURN_ALREADY_ON); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 321 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 322 | if (*ret != PSCI_RETURN_SUCCESS) { |
| 323 | cpu_off(c); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 324 | } |
| 325 | break; |
| 326 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 327 | case PSCI_MIGRATE: |
| 328 | case PSCI_MIGRATE_INFO_TYPE: |
| 329 | case PSCI_MIGRATE_INFO_UP_CPU: |
| 330 | case PSCI_CPU_FREEZE: |
| 331 | case PSCI_CPU_DEFAULT_SUSPEND: |
| 332 | case PSCI_NODE_HW_STATE: |
| 333 | case PSCI_SYSTEM_SUSPEND: |
| 334 | case PSCI_SET_SYSPEND_MODE: |
| 335 | case PSCI_STAT_RESIDENCY: |
| 336 | case PSCI_STAT_COUNT: |
| 337 | case PSCI_SYSTEM_RESET2: |
| 338 | case PSCI_MEM_PROTECT: |
| 339 | case PSCI_MEM_PROTECT_CHECK_RANGE: |
| 340 | /* Block all other known PSCI calls. */ |
| 341 | *ret = PSCI_RETURN_NOT_SUPPORTED; |
| 342 | break; |
| 343 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 344 | default: |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 351 | /** |
| 352 | * Sets or clears the VI bit in the HCR_EL2 register saved in the given |
| 353 | * arch_regs. |
| 354 | */ |
| 355 | static void set_virtual_interrupt(struct arch_regs *r, bool enable) |
| 356 | { |
| 357 | if (enable) { |
| 358 | r->lazy.hcr_el2 |= HCR_EL2_VI; |
| 359 | } else { |
| 360 | r->lazy.hcr_el2 &= ~HCR_EL2_VI; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Sets or clears the VI bit in the HCR_EL2 register. |
| 366 | */ |
| 367 | static void set_virtual_interrupt_current(bool enable) |
| 368 | { |
| 369 | uintreg_t hcr_el2 = read_msr(hcr_el2); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 370 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 371 | if (enable) { |
| 372 | hcr_el2 |= HCR_EL2_VI; |
| 373 | } else { |
| 374 | hcr_el2 &= ~HCR_EL2_VI; |
| 375 | } |
| 376 | write_msr(hcr_el2, hcr_el2); |
| 377 | } |
| 378 | |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 379 | struct hvc_handler_return hvc_handler(uintreg_t arg0, uintreg_t arg1, |
| 380 | uintreg_t arg2, uintreg_t arg3) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 381 | { |
| 382 | struct hvc_handler_return ret; |
| 383 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 384 | ret.new = NULL; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 385 | |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 386 | if (current()->vm->id == HF_PRIMARY_VM_ID) { |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 387 | int32_t psci_ret; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 388 | |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 389 | if (psci_handler(arg0, arg1, arg2, arg3, &psci_ret)) { |
| 390 | ret.user_ret = psci_ret; |
| 391 | return ret; |
| 392 | } |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 393 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 394 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 395 | switch ((uint32_t)arg0) { |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 396 | case SPCI_VERSION_32: |
| 397 | ret.user_ret = api_spci_version(); |
| 398 | break; |
| 399 | |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 400 | case HF_VM_GET_ID: |
| 401 | ret.user_ret = api_vm_get_id(current()); |
| 402 | break; |
| 403 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 404 | case HF_VM_GET_COUNT: |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 405 | ret.user_ret = api_vm_get_count(); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 406 | break; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 407 | |
| 408 | case HF_VCPU_GET_COUNT: |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 409 | ret.user_ret = api_vcpu_get_count(arg1, current()); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 410 | break; |
| 411 | |
| 412 | case HF_VCPU_RUN: |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 413 | ret.user_ret = hf_vcpu_run_return_encode( |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 414 | api_vcpu_run(arg1, arg2, current(), &ret.new)); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 415 | break; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 416 | |
Jose Marinho | 135dff3 | 2019-02-28 10:25:57 +0000 | [diff] [blame] | 417 | case SPCI_YIELD_32: |
| 418 | ret.user_ret = api_spci_yield(current(), &ret.new); |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 419 | break; |
| 420 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 421 | case HF_VM_CONFIGURE: |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 422 | ret.user_ret = api_vm_configure(ipa_init(arg1), ipa_init(arg2), |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 423 | current(), &ret.new); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 424 | break; |
| 425 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 426 | case SPCI_MSG_SEND_32: |
| 427 | ret.user_ret = api_spci_msg_send(arg1, current(), &ret.new); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 428 | break; |
| 429 | |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 430 | case SPCI_MSG_RECV_32: |
| 431 | ret.user_ret = api_spci_msg_recv(arg1, current(), &ret.new); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 432 | break; |
| 433 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 434 | case HF_MAILBOX_CLEAR: |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 435 | ret.user_ret = api_mailbox_clear(current(), &ret.new); |
| 436 | break; |
| 437 | |
| 438 | case HF_MAILBOX_WRITABLE_GET: |
| 439 | ret.user_ret = api_mailbox_writable_get(current()); |
| 440 | break; |
| 441 | |
| 442 | case HF_MAILBOX_WAITER_GET: |
| 443 | ret.user_ret = api_mailbox_waiter_get(arg1, current()); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 444 | break; |
| 445 | |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 446 | case HF_INTERRUPT_ENABLE: |
| 447 | ret.user_ret = api_interrupt_enable(arg1, arg2, current()); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 448 | break; |
| 449 | |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 450 | case HF_INTERRUPT_GET: |
| 451 | ret.user_ret = api_interrupt_get(current()); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 452 | break; |
| 453 | |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 454 | case HF_INTERRUPT_INJECT: |
| 455 | ret.user_ret = api_interrupt_inject(arg1, arg2, arg3, current(), |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 456 | &ret.new); |
| 457 | break; |
| 458 | |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 459 | case HF_SHARE_MEMORY: |
| 460 | ret.user_ret = |
| 461 | api_share_memory(arg1 >> 32, ipa_init(arg2), arg3, |
| 462 | arg1 & 0xffffffff, current()); |
| 463 | break; |
| 464 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 465 | default: |
| 466 | ret.user_ret = -1; |
| 467 | } |
| 468 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 469 | /* Set or clear VI bit. */ |
| 470 | if (ret.new == NULL) { |
| 471 | /* |
| 472 | * Not switching vCPUs, set the bit for the current vCPU |
| 473 | * directly in the register. |
| 474 | */ |
| 475 | set_virtual_interrupt_current( |
| 476 | current()->interrupts.enabled_and_pending_count > 0); |
| 477 | } else { |
| 478 | /* |
| 479 | * About to switch vCPUs, set the bit for the vCPU to which we |
| 480 | * are switching in the saved copy of the register. |
| 481 | */ |
| 482 | set_virtual_interrupt( |
| 483 | &ret.new->regs, |
| 484 | ret.new->interrupts.enabled_and_pending_count > 0); |
| 485 | } |
| 486 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 487 | return ret; |
| 488 | } |
| 489 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 490 | struct vcpu *irq_lower(void) |
| 491 | { |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 492 | /* |
| 493 | * Switch back to primary VM, interrupts will be handled there. |
| 494 | * |
| 495 | * If the VM has aborted, this vCPU will be aborted when the scheduler |
| 496 | * tries to run it again. This means the interrupt will not be delayed |
| 497 | * by the aborted VM. |
| 498 | * |
| 499 | * TODO: Only switch when the interrupt isn't for the current VM. |
| 500 | */ |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 501 | return api_preempt(current()); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 504 | struct vcpu *fiq_lower(void) |
| 505 | { |
| 506 | return irq_lower(); |
| 507 | } |
| 508 | |
| 509 | struct vcpu *serr_lower(void) |
| 510 | { |
| 511 | dlog("SERR from lower\n"); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 512 | return api_abort(current()); |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 515 | /** |
| 516 | * Initialises a fault info structure. It assumes that an FnV bit exists at |
| 517 | * bit offset 10 of the ESR, and that it is only valid when the bottom 6 bits of |
| 518 | * the ESR (the fault status code) are 010000; this is the case for both |
| 519 | * instruction and data aborts, but not necessarily for other exception reasons. |
| 520 | */ |
| 521 | static struct vcpu_fault_info fault_info_init(uintreg_t esr, |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 522 | const struct vcpu *vcpu, int mode) |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 523 | { |
| 524 | uint32_t fsc = esr & 0x3f; |
| 525 | struct vcpu_fault_info r; |
| 526 | |
| 527 | r.mode = mode; |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 528 | r.pc = va_init(vcpu->regs.pc); |
| 529 | |
| 530 | /* |
| 531 | * Check the FnV bit, which is only valid if dfsc/ifsc is 010000. It |
| 532 | * indicates that we cannot rely on far_el2. |
| 533 | */ |
| 534 | if (fsc == 0x10 && esr & (1u << 10)) { |
| 535 | r.vaddr = va_init(0); |
| 536 | r.ipaddr = ipa_init(read_msr(hpfar_el2) << 8); |
| 537 | } else { |
| 538 | r.vaddr = va_init(read_msr(far_el2)); |
| 539 | r.ipaddr = ipa_init((read_msr(hpfar_el2) << 8) | |
| 540 | (read_msr(far_el2) & (PAGE_SIZE - 1))); |
| 541 | } |
| 542 | |
| 543 | return r; |
| 544 | } |
| 545 | |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 546 | struct vcpu *sync_lower_exception(uintreg_t esr) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 547 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 548 | struct vcpu *vcpu = current(); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 549 | struct vcpu_fault_info info; |
Jose Marinho | 135dff3 | 2019-02-28 10:25:57 +0000 | [diff] [blame] | 550 | struct vcpu *new_vcpu; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 551 | |
| 552 | switch (esr >> 26) { |
| 553 | case 0x01: /* EC = 000001, WFI or WFE. */ |
Andrew Walbran | 48196eb | 2019-03-04 14:56:24 +0000 | [diff] [blame] | 554 | /* Skip the instruction. */ |
| 555 | vcpu->regs.pc += (esr & (1u << 25)) ? 4 : 2; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 556 | /* Check TI bit of ISS, 0 = WFI, 1 = WFE. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 557 | if (esr & 1) { |
Andrew Walbran | 48196eb | 2019-03-04 14:56:24 +0000 | [diff] [blame] | 558 | /* WFE */ |
| 559 | /* |
| 560 | * TODO: consider giving the scheduler more context, |
| 561 | * somehow. |
| 562 | */ |
Jose Marinho | 135dff3 | 2019-02-28 10:25:57 +0000 | [diff] [blame] | 563 | api_spci_yield(vcpu, &new_vcpu); |
| 564 | return new_vcpu; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 565 | } |
Andrew Walbran | 48196eb | 2019-03-04 14:56:24 +0000 | [diff] [blame] | 566 | /* WFI */ |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 567 | return api_wait_for_interrupt(vcpu); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 568 | |
| 569 | case 0x24: /* EC = 100100, Data abort. */ |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 570 | info = fault_info_init( |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 571 | esr, vcpu, (esr & (1u << 6)) ? MM_MODE_W : MM_MODE_R); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 572 | if (vcpu_handle_page_fault(vcpu, &info)) { |
| 573 | return NULL; |
| 574 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 575 | break; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 576 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 577 | case 0x20: /* EC = 100000, Instruction abort. */ |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 578 | info = fault_info_init(esr, vcpu, MM_MODE_X); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 579 | if (vcpu_handle_page_fault(vcpu, &info)) { |
| 580 | return NULL; |
| 581 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 582 | break; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 583 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 584 | case 0x17: /* EC = 010111, SMC instruction. */ { |
| 585 | uintreg_t smc_pc = vcpu->regs.pc; |
| 586 | int32_t ret; |
| 587 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 588 | if (vcpu->vm->id != HF_PRIMARY_VM_ID || |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 589 | !psci_handler(vcpu->regs.r[0], vcpu->regs.r[1], |
| 590 | vcpu->regs.r[2], vcpu->regs.r[3], &ret)) { |
| 591 | dlog("Unsupported SMC call: 0x%x\n", vcpu->regs.r[0]); |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 592 | ret = PSCI_RETURN_NOT_SUPPORTED; |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* Skip the SMC instruction. */ |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 596 | vcpu->regs.pc = smc_pc + (esr & (1u << 25) ? 4 : 2); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 597 | vcpu->regs.r[0] = ret; |
| 598 | return NULL; |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 599 | } |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 600 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 601 | default: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 602 | dlog("Unknown lower sync exception pc=0x%x, esr=0x%x, " |
| 603 | "ec=0x%x\n", |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 604 | vcpu->regs.pc, esr, esr >> 26); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 605 | break; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 606 | } |
| 607 | |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 608 | /* The exception wasn't handled so abort the VM. */ |
| 609 | return api_abort(vcpu); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 610 | } |