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" |
| 24 | #include "hf/dlog.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 25 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 26 | #include "hf/vm.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 27 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 28 | #include "vmapi/hf/call.h" |
| 29 | |
Andrew Scull | 23e93a8 | 2018-10-26 14:56:04 +0100 | [diff] [blame] | 30 | #define STACK_SIZE PAGE_SIZE |
| 31 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 32 | /* The stack to be used by the CPUs. */ |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 33 | alignas(2 * sizeof(uintreg_t)) static char callstacks[MAX_CPUS][STACK_SIZE]; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 34 | |
| 35 | /* State of all supported CPUs. The stack of the first one is initialized. */ |
| 36 | struct cpu cpus[MAX_CPUS] = { |
| 37 | { |
| 38 | .is_on = 1, |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 39 | .stack_bottom = &callstacks[0][STACK_SIZE], |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 40 | }, |
| 41 | }; |
| 42 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 43 | static uint32_t cpu_count = 1; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 44 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 45 | static void cpu_init(struct cpu *c) |
| 46 | { |
| 47 | /* TODO: Assumes that c is zeroed out already. */ |
| 48 | sl_init(&c->lock); |
| 49 | c->irq_disable_count = 1; |
| 50 | } |
| 51 | |
| 52 | void cpu_module_init(const uint64_t *cpu_ids, size_t count) |
| 53 | { |
| 54 | uint32_t i; |
| 55 | uint32_t j; |
| 56 | uint64_t boot_cpu_id = cpus[0].id; |
| 57 | bool found_boot_cpu = false; |
| 58 | |
| 59 | cpu_count = count; |
| 60 | |
| 61 | /* |
| 62 | * Initialize CPUs with the IDs from the configuration passed in. The |
| 63 | * CPUs after the boot CPU are initialized in reverse order. The boot |
| 64 | * CPU is initialized when it is found or in place of the last CPU if it |
| 65 | * is not found. |
| 66 | */ |
| 67 | j = cpu_count; |
| 68 | for (i = 0; i < cpu_count; ++i) { |
| 69 | struct cpu *c; |
| 70 | uint64_t id = cpu_ids[i]; |
| 71 | |
| 72 | if (found_boot_cpu || id != boot_cpu_id) { |
| 73 | c = &cpus[--j]; |
| 74 | } else { |
| 75 | found_boot_cpu = true; |
| 76 | c = &cpus[0]; |
| 77 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 78 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 79 | cpu_init(c); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 80 | c->id = id; |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 81 | c->stack_bottom = &callstacks[i][STACK_SIZE]; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 82 | } |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 83 | |
| 84 | if (!found_boot_cpu) { |
| 85 | /* Boot CPU was initialized but with wrong ID. */ |
| 86 | dlog("Boot CPU's ID not found in config."); |
| 87 | cpus[0].id = boot_cpu_id; |
| 88 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | size_t cpu_index(struct cpu *c) |
| 92 | { |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 93 | return c - cpus; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 96 | void cpu_irq_enable(struct cpu *c) |
| 97 | { |
| 98 | c->irq_disable_count--; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 99 | if (!c->irq_disable_count) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 100 | arch_irq_enable(); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 101 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void cpu_irq_disable(struct cpu *c) |
| 105 | { |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 106 | if (!c->irq_disable_count) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 107 | arch_irq_disable(); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 108 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 109 | c->irq_disable_count++; |
| 110 | } |
| 111 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 112 | /** |
| 113 | * Turns CPU on and returns the previous state. |
| 114 | */ |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 115 | 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] | 116 | { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 117 | bool prev; |
| 118 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 119 | sl_lock(&c->lock); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 120 | prev = c->is_on; |
| 121 | c->is_on = true; |
| 122 | sl_unlock(&c->lock); |
| 123 | |
| 124 | if (!prev) { |
Wedson Almeida Filho | 1f81b75 | 2018-10-24 15:15:49 +0100 | [diff] [blame] | 125 | struct vm *vm = vm_get(HF_PRIMARY_VM_ID); |
| 126 | struct vcpu *vcpu = &vm->vcpus[cpu_index(c)]; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 127 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 128 | vcpu_on(vcpu, entry, arg); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 129 | } |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 130 | |
| 131 | return prev; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 134 | /** |
| 135 | * Prepares the CPU for turning itself off. |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 136 | */ |
| 137 | void cpu_off(struct cpu *c) |
| 138 | { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 139 | sl_lock(&c->lock); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 140 | c->is_on = false; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 141 | sl_unlock(&c->lock); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 142 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 143 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 144 | /** |
| 145 | * Searches for a CPU based on its id. |
| 146 | */ |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 147 | struct cpu *cpu_find(uint64_t id) |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 148 | { |
| 149 | size_t i; |
| 150 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 151 | for (i = 0; i < cpu_count; i++) { |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 152 | if (cpus[i].id == id) { |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 153 | return &cpus[i]; |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | return NULL; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 160 | void vcpu_init(struct vcpu *vcpu, struct vm *vm) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 161 | { |
Andrew Scull | 2b5fbad | 2019-04-05 13:55:56 +0100 | [diff] [blame^] | 162 | memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu)); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 163 | sl_init(&vcpu->lock); |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 164 | vcpu->regs_available = true; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 165 | vcpu->vm = vm; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 166 | vcpu->state = vcpu_state_off; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 167 | } |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 168 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 169 | void vcpu_on(struct vcpu *vcpu, ipaddr_t entry, uintreg_t arg) |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 170 | { |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 171 | arch_regs_set_pc_arg(&vcpu->regs, entry, arg); |
| 172 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 173 | sl_lock(&vcpu->lock); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 174 | vcpu->state = vcpu_state_ready; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 175 | sl_unlock(&vcpu->lock); |
| 176 | } |
| 177 | |
| 178 | void vcpu_off(struct vcpu *vcpu) |
| 179 | { |
| 180 | sl_lock(&vcpu->lock); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 181 | vcpu->state = vcpu_state_off; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 182 | sl_unlock(&vcpu->lock); |
| 183 | } |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 184 | |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 185 | size_t vcpu_index(const struct vcpu *vcpu) |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 186 | { |
| 187 | return vcpu - vcpu->vm->vcpus; |
| 188 | } |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 189 | |
| 190 | /** |
| 191 | * Handles a page fault. It does so by determining if it's a legitimate or |
| 192 | * spurious fault, and recovering from the latter. |
| 193 | * |
| 194 | * Returns true if the caller should resume the current vcpu, or false if its VM |
| 195 | * should be aborted. |
| 196 | */ |
| 197 | bool vcpu_handle_page_fault(const struct vcpu *current, |
| 198 | struct vcpu_fault_info *f) |
| 199 | { |
| 200 | struct vm *vm = current->vm; |
| 201 | ipaddr_t second_addr; |
| 202 | bool second; |
| 203 | int mode; |
| 204 | int mask = f->mode | MM_MODE_INVALID; |
| 205 | bool ret = false; |
| 206 | |
| 207 | /* We can't recover if we don't know the size. */ |
| 208 | if (f->size == 0) { |
| 209 | goto exit; |
| 210 | } |
| 211 | |
| 212 | sl_lock(&vm->lock); |
| 213 | |
| 214 | /* |
| 215 | * Check if this is a legitimate fault, i.e., if the page table doesn't |
| 216 | * allow the access attemped by the VM. |
| 217 | */ |
| 218 | if (!mm_vm_get_mode(&vm->ptable, f->ipaddr, ipa_add(f->ipaddr, 1), |
| 219 | &mode) || |
| 220 | (mode & mask) != f->mode) { |
| 221 | goto exit_unlock; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Do the same mode check on the second page, if the fault straddles two |
| 226 | * pages. |
| 227 | */ |
| 228 | second_addr = ipa_add(f->ipaddr, f->size - 1); |
| 229 | second = (ipa_addr(f->ipaddr) >> PAGE_BITS) != |
| 230 | (ipa_addr(second_addr) >> PAGE_BITS); |
| 231 | if (second) { |
| 232 | if (!mm_vm_get_mode(&vm->ptable, second_addr, |
| 233 | ipa_add(second_addr, 1), &mode) || |
| 234 | (mode & mask) != f->mode) { |
| 235 | goto exit_unlock; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * This is a spurious fault, likely because another CPU is updating the |
| 241 | * page table. It is responsible for issuing global tlb invalidations |
| 242 | * while holding the VM lock, so we don't need to do anything else to |
| 243 | * recover from it. (Acquiring/releasing the lock ensured that the |
| 244 | * invalidations have completed.) |
| 245 | */ |
| 246 | |
| 247 | ret = true; |
| 248 | |
| 249 | exit_unlock: |
| 250 | sl_unlock(&vm->lock); |
| 251 | exit: |
| 252 | if (!ret) { |
| 253 | dlog("Stage-2 page fault: pc=0x%x, vmid=%u, vcpu=%u, " |
| 254 | "vaddr=0x%x, ipaddr=0x%x, mode=0x%x, size=%u\n", |
| 255 | f->pc, vm->id, vcpu_index(current), f->vaddr, f->ipaddr, |
| 256 | f->mode, f->size); |
| 257 | } |
| 258 | return ret; |
| 259 | } |