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 | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/cpu.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 19 | #include <stdalign.h> |
| 20 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 21 | #include "hf/arch/cpu.h" |
| 22 | |
| 23 | #include "hf/api.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 24 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 25 | #include "hf/dlog.h" |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 26 | #include "hf/spci.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 27 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 28 | #include "hf/vm.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 29 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 30 | #include "vmapi/hf/call.h" |
| 31 | |
Andrew Scull | 23e93a8 | 2018-10-26 14:56:04 +0100 | [diff] [blame] | 32 | #define STACK_SIZE PAGE_SIZE |
| 33 | |
Andrew Scull | eed724b | 2019-09-12 09:57:19 +0100 | [diff] [blame^] | 34 | /** |
| 35 | * The stacks to be used by the CPUs. |
| 36 | * |
| 37 | * Align to page boundaries to ensure that cache lines are not shared between a |
| 38 | * CPU's stack and data that can be accessed from other CPUs. If this did |
| 39 | * happen, there may be coherency problems when the stack is being used before |
| 40 | * caching is enabled. |
| 41 | */ |
| 42 | alignas(PAGE_SIZE) static char callstacks[MAX_CPUS][STACK_SIZE]; |
| 43 | |
| 44 | /* NOLINTNEXTLINE(misc-redundant-expression) */ |
| 45 | static_assert((STACK_SIZE % PAGE_SIZE) == 0, "Keep each stack page aligned."); |
| 46 | static_assert((PAGE_SIZE % STACK_ALIGN) == 0, |
| 47 | "Page alignment is too weak for the stack."); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 48 | |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 49 | /** |
| 50 | * Internal buffer used to store SPCI messages from a VM Tx. Its usage prevents |
| 51 | * TOCTOU issues while Hafnium performs actions on information that would |
| 52 | * otherwise be re-writable by the VM. |
| 53 | * |
| 54 | * Each buffer is owned by a single cpu. The buffer can only be used for |
| 55 | * spci_msg_send. The information stored in the buffer is only valid during the |
| 56 | * spci_msg_send request is performed. |
| 57 | */ |
| 58 | alignas(PAGE_SIZE) uint8_t cpu_message_buffer[MAX_CPUS][PAGE_SIZE]; |
| 59 | |
| 60 | uint8_t *cpu_get_buffer(cpu_id_t cpu_id) |
| 61 | { |
| 62 | CHECK(cpu_id < MAX_CPUS); |
| 63 | |
| 64 | return cpu_message_buffer[cpu_id]; |
| 65 | } |
| 66 | |
| 67 | uint32_t cpu_get_buffer_size(cpu_id_t cpu_id) |
| 68 | { |
| 69 | CHECK(cpu_id < MAX_CPUS); |
| 70 | |
| 71 | return sizeof(cpu_message_buffer[cpu_id]); |
| 72 | } |
| 73 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 74 | /* State of all supported CPUs. The stack of the first one is initialized. */ |
| 75 | struct cpu cpus[MAX_CPUS] = { |
| 76 | { |
| 77 | .is_on = 1, |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 78 | .stack_bottom = &callstacks[0][STACK_SIZE], |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 79 | }, |
| 80 | }; |
| 81 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 82 | static uint32_t cpu_count = 1; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 83 | |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 84 | void cpu_module_init(const cpu_id_t *cpu_ids, size_t count) |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 85 | { |
| 86 | uint32_t i; |
| 87 | uint32_t j; |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 88 | cpu_id_t boot_cpu_id = cpus[0].id; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 89 | bool found_boot_cpu = false; |
| 90 | |
| 91 | cpu_count = count; |
| 92 | |
| 93 | /* |
| 94 | * Initialize CPUs with the IDs from the configuration passed in. The |
| 95 | * CPUs after the boot CPU are initialized in reverse order. The boot |
| 96 | * CPU is initialized when it is found or in place of the last CPU if it |
| 97 | * is not found. |
| 98 | */ |
| 99 | j = cpu_count; |
| 100 | for (i = 0; i < cpu_count; ++i) { |
| 101 | struct cpu *c; |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 102 | cpu_id_t id = cpu_ids[i]; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 103 | |
| 104 | if (found_boot_cpu || id != boot_cpu_id) { |
Andrew Scull | 4897398 | 2019-08-16 17:40:28 +0100 | [diff] [blame] | 105 | --j; |
| 106 | c = &cpus[j]; |
| 107 | c->stack_bottom = &callstacks[j][STACK_SIZE]; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 108 | } else { |
| 109 | found_boot_cpu = true; |
| 110 | c = &cpus[0]; |
Andrew Scull | 4897398 | 2019-08-16 17:40:28 +0100 | [diff] [blame] | 111 | CHECK(c->stack_bottom == &callstacks[0][STACK_SIZE]); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 112 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 113 | |
Andrew Scull | 4897398 | 2019-08-16 17:40:28 +0100 | [diff] [blame] | 114 | sl_init(&c->lock); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 115 | c->id = id; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 116 | } |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 117 | |
| 118 | if (!found_boot_cpu) { |
| 119 | /* Boot CPU was initialized but with wrong ID. */ |
David Brazdil | 7d85345 | 2019-09-12 10:07:36 +0100 | [diff] [blame] | 120 | dlog("Boot CPU's ID not found in config.\n"); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 121 | cpus[0].id = boot_cpu_id; |
| 122 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | size_t cpu_index(struct cpu *c) |
| 126 | { |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 127 | return c - cpus; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 130 | /** |
| 131 | * Turns CPU on and returns the previous state. |
| 132 | */ |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 133 | bool cpu_on(struct cpu *c, ipaddr_t entry, uintreg_t arg) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 134 | { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 135 | bool prev; |
| 136 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 137 | sl_lock(&c->lock); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 138 | prev = c->is_on; |
| 139 | c->is_on = true; |
| 140 | sl_unlock(&c->lock); |
| 141 | |
| 142 | if (!prev) { |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 143 | struct vm *vm = vm_find(HF_PRIMARY_VM_ID); |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 144 | struct vcpu *vcpu = vm_get_vcpu(vm, cpu_index(c)); |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 145 | struct vcpu_locked vcpu_locked; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 146 | |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 147 | vcpu_locked = vcpu_lock(vcpu); |
| 148 | vcpu_on(vcpu_locked, entry, arg); |
| 149 | vcpu_unlock(&vcpu_locked); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 150 | } |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 151 | |
| 152 | return prev; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 155 | /** |
| 156 | * Prepares the CPU for turning itself off. |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 157 | */ |
| 158 | void cpu_off(struct cpu *c) |
| 159 | { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 160 | sl_lock(&c->lock); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 161 | c->is_on = false; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 162 | sl_unlock(&c->lock); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 163 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 164 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 165 | /** |
| 166 | * Searches for a CPU based on its id. |
| 167 | */ |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 168 | struct cpu *cpu_find(cpu_id_t id) |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 169 | { |
| 170 | size_t i; |
| 171 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 172 | for (i = 0; i < cpu_count; i++) { |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 173 | if (cpus[i].id == id) { |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 174 | return &cpus[i]; |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | return NULL; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 181 | /** |
| 182 | * Locks the given vCPU and updates `locked` to hold the newly locked vCPU. |
| 183 | */ |
| 184 | struct vcpu_locked vcpu_lock(struct vcpu *vcpu) |
| 185 | { |
| 186 | struct vcpu_locked locked = { |
| 187 | .vcpu = vcpu, |
| 188 | }; |
| 189 | |
| 190 | sl_lock(&vcpu->lock); |
| 191 | |
| 192 | return locked; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to |
| 197 | * reflect the fact that the vCPU is no longer locked. |
| 198 | */ |
| 199 | void vcpu_unlock(struct vcpu_locked *locked) |
| 200 | { |
| 201 | sl_unlock(&locked->vcpu->lock); |
| 202 | locked->vcpu = NULL; |
| 203 | } |
| 204 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 205 | void vcpu_init(struct vcpu *vcpu, struct vm *vm) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 206 | { |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame] | 207 | memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu)); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 208 | sl_init(&vcpu->lock); |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 209 | vcpu->regs_available = true; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 210 | vcpu->vm = vm; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 211 | vcpu->state = VCPU_STATE_OFF; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 212 | } |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 213 | |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 214 | /** |
| 215 | * Initialise the registers for the given vCPU and set the state to |
| 216 | * VCPU_STATE_READY. The caller must hold the vCPU lock while calling this. |
| 217 | */ |
| 218 | void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg) |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 219 | { |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 220 | arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg); |
| 221 | vcpu.vcpu->state = VCPU_STATE_READY; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 224 | spci_vcpu_index_t vcpu_index(const struct vcpu *vcpu) |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 225 | { |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 226 | size_t index = vcpu - vcpu->vm->vcpus; |
| 227 | |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 228 | CHECK(index < UINT16_MAX); |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 229 | return index; |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 230 | } |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 231 | |
| 232 | /** |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 233 | * Check whether the given vcpu_state is an off state, for the purpose of |
| 234 | * turning vCPUs on and off. Note that aborted still counts as on in this |
| 235 | * context. |
Andrew Walbran | 9a43fee | 2019-04-18 17:42:32 +0100 | [diff] [blame] | 236 | */ |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 237 | bool vcpu_is_off(struct vcpu_locked vcpu) |
| 238 | { |
| 239 | switch (vcpu.vcpu->state) { |
| 240 | case VCPU_STATE_OFF: |
| 241 | return true; |
| 242 | case VCPU_STATE_READY: |
| 243 | case VCPU_STATE_RUNNING: |
| 244 | case VCPU_STATE_BLOCKED_MAILBOX: |
| 245 | case VCPU_STATE_BLOCKED_INTERRUPT: |
| 246 | case VCPU_STATE_ABORTED: |
| 247 | /* |
| 248 | * Aborted still counts as ON for the purposes of PSCI, |
| 249 | * because according to the PSCI specification (section |
| 250 | * 5.7.1) a core is only considered to be off if it has |
| 251 | * been turned off with a CPU_OFF call or hasn't yet |
| 252 | * been turned on with a CPU_ON call. |
| 253 | */ |
| 254 | return false; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Starts a vCPU of a secondary VM. |
| 260 | * |
| 261 | * Returns true if the secondary was reset and started, or false if it was |
| 262 | * already on and so nothing was done. |
| 263 | */ |
| 264 | bool vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry, |
Andrew Walbran | 9a43fee | 2019-04-18 17:42:32 +0100 | [diff] [blame] | 265 | uintreg_t arg) |
| 266 | { |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 267 | struct vcpu_locked vcpu_locked; |
Andrew Walbran | 9a43fee | 2019-04-18 17:42:32 +0100 | [diff] [blame] | 268 | struct vm *vm = vcpu->vm; |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 269 | bool vcpu_was_off; |
Andrew Walbran | 9a43fee | 2019-04-18 17:42:32 +0100 | [diff] [blame] | 270 | |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 271 | CHECK(vm->id != HF_PRIMARY_VM_ID); |
Andrew Walbran | 9a43fee | 2019-04-18 17:42:32 +0100 | [diff] [blame] | 272 | |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 273 | vcpu_locked = vcpu_lock(vcpu); |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 274 | vcpu_was_off = vcpu_is_off(vcpu_locked); |
| 275 | if (vcpu_was_off) { |
| 276 | /* |
| 277 | * Set vCPU registers to a clean state ready for boot. As this |
| 278 | * is a secondary which can migrate between pCPUs, the ID of the |
| 279 | * vCPU is defined as the index and does not match the ID of the |
| 280 | * pCPU it is running on. |
| 281 | */ |
| 282 | arch_regs_reset(&vcpu->regs, false, vm->id, vcpu_index(vcpu), |
| 283 | vm->ptable.root); |
| 284 | vcpu_on(vcpu_locked, entry, arg); |
| 285 | } |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 286 | vcpu_unlock(&vcpu_locked); |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 287 | |
| 288 | return vcpu_was_off; |
Andrew Walbran | 9a43fee | 2019-04-18 17:42:32 +0100 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /** |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 292 | * Handles a page fault. It does so by determining if it's a legitimate or |
| 293 | * spurious fault, and recovering from the latter. |
| 294 | * |
| 295 | * Returns true if the caller should resume the current vcpu, or false if its VM |
| 296 | * should be aborted. |
| 297 | */ |
| 298 | bool vcpu_handle_page_fault(const struct vcpu *current, |
| 299 | struct vcpu_fault_info *f) |
| 300 | { |
| 301 | struct vm *vm = current->vm; |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 302 | int mode; |
| 303 | int mask = f->mode | MM_MODE_INVALID; |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 304 | bool resume; |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 305 | |
| 306 | sl_lock(&vm->lock); |
| 307 | |
| 308 | /* |
| 309 | * Check if this is a legitimate fault, i.e., if the page table doesn't |
| 310 | * allow the access attemped by the VM. |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 311 | * |
| 312 | * Otherwise, this is a spurious fault, likely because another CPU is |
| 313 | * updating the page table. It is responsible for issuing global TLB |
| 314 | * invalidations while holding the VM lock, so we don't need to do |
| 315 | * anything else to recover from it. (Acquiring/releasing the lock |
| 316 | * ensured that the invalidations have completed.) |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 317 | */ |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 318 | resume = mm_vm_get_mode(&vm->ptable, f->ipaddr, ipa_add(f->ipaddr, 1), |
| 319 | &mode) && |
| 320 | (mode & mask) == f->mode; |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 321 | |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 322 | sl_unlock(&vm->lock); |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 323 | |
| 324 | if (!resume) { |
Andrew Walbran | ac5b261 | 2019-07-12 16:44:19 +0100 | [diff] [blame] | 325 | dlog("Stage-2 page fault: pc=%#x, vmid=%u, vcpu=%u, " |
| 326 | "vaddr=%#x, ipaddr=%#x, mode=%#x\n", |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 327 | f->pc, vm->id, vcpu_index(current), f->vaddr, f->ipaddr, |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 328 | f->mode); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 329 | } |
Andrew Scull | d3cfaad | 2019-04-04 11:34:10 +0100 | [diff] [blame] | 330 | |
| 331 | return resume; |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 332 | } |