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