blob: 7d181dc7436492bc189368b9aa6d3d83add894bb [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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 Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/cpu.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
Andrew Scull04502e42018-09-03 14:54:52 +010019#include <stdalign.h>
20
Andrew Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/arch/cpu.h"
22
23#include "hf/api.h"
24#include "hf/dlog.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010025#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010026#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010027
Andrew Scull19503262018-09-20 14:48:39 +010028#include "vmapi/hf/call.h"
29
Andrew Scull23e93a82018-10-26 14:56:04 +010030#define STACK_SIZE PAGE_SIZE
31
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010032/* The stack to be used by the CPUs. */
Andrew Scull37402872018-10-24 14:23:06 +010033alignas(2 * sizeof(uintreg_t)) static char callstacks[MAX_CPUS][STACK_SIZE];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010034
35/* State of all supported CPUs. The stack of the first one is initialized. */
36struct cpu cpus[MAX_CPUS] = {
37 {
38 .is_on = 1,
Andrew Scullf3d45592018-09-20 14:30:22 +010039 .stack_bottom = &callstacks[0][STACK_SIZE],
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010040 },
41};
42
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000043static uint32_t cpu_count = 1;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010044
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000045static 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
52void 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 Filho81568c42019-01-04 13:33:02 +000078
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010079 cpu_init(c);
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000080 c->id = id;
Andrew Scullf3d45592018-09-20 14:30:22 +010081 c->stack_bottom = &callstacks[i][STACK_SIZE];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010082 }
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000083
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 Filho3fcbcff2018-07-10 23:53:39 +010089}
90
91size_t cpu_index(struct cpu *c)
92{
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010093 return c - cpus;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010094}
95
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010096void cpu_irq_enable(struct cpu *c)
97{
98 c->irq_disable_count--;
Andrew Scull7364a8e2018-07-19 15:39:29 +010099 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100100 arch_irq_enable();
Andrew Scull7364a8e2018-07-19 15:39:29 +0100101 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100102}
103
104void cpu_irq_disable(struct cpu *c)
105{
Andrew Scull7364a8e2018-07-19 15:39:29 +0100106 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100107 arch_irq_disable();
Andrew Scull7364a8e2018-07-19 15:39:29 +0100108 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100109 c->irq_disable_count++;
110}
111
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100112/**
113 * Turns CPU on and returns the previous state.
114 */
Andrew Scull37402872018-10-24 14:23:06 +0100115bool cpu_on(struct cpu *c, ipaddr_t entry, uintreg_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100116{
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100117 bool prev;
118
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100119 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100120 prev = c->is_on;
121 c->is_on = true;
122 sl_unlock(&c->lock);
123
124 if (!prev) {
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100125 struct vm *vm = vm_get(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100126 struct vcpu *vcpu = vm_get_vcpu(vm, cpu_index(c));
Andrew Walbranb58f8992019-04-15 12:29:31 +0100127 struct vcpu_locked vcpu_locked;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000128
Andrew Walbranb58f8992019-04-15 12:29:31 +0100129 vcpu_locked = vcpu_lock(vcpu);
130 vcpu_on(vcpu_locked, entry, arg);
131 vcpu_unlock(&vcpu_locked);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100132 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100133
134 return prev;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100135}
136
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100137/**
138 * Prepares the CPU for turning itself off.
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100139 */
140void cpu_off(struct cpu *c)
141{
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100142 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100143 c->is_on = false;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100144 sl_unlock(&c->lock);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100145}
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100146
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100147/**
148 * Searches for a CPU based on its id.
149 */
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000150struct cpu *cpu_find(uint64_t id)
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100151{
152 size_t i;
153
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000154 for (i = 0; i < cpu_count; i++) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100155 if (cpus[i].id == id) {
Andrew Scullf3d45592018-09-20 14:30:22 +0100156 return &cpus[i];
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100157 }
158 }
159
160 return NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100161}
162
Andrew Walbranb58f8992019-04-15 12:29:31 +0100163/**
164 * Locks the given vCPU and updates `locked` to hold the newly locked vCPU.
165 */
166struct vcpu_locked vcpu_lock(struct vcpu *vcpu)
167{
168 struct vcpu_locked locked = {
169 .vcpu = vcpu,
170 };
171
172 sl_lock(&vcpu->lock);
173
174 return locked;
175}
176
177/**
178 * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to
179 * reflect the fact that the vCPU is no longer locked.
180 */
181void vcpu_unlock(struct vcpu_locked *locked)
182{
183 sl_unlock(&locked->vcpu->lock);
184 locked->vcpu = NULL;
185}
186
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100187void vcpu_init(struct vcpu *vcpu, struct vm *vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100188{
Andrew Scull2b5fbad2019-04-05 13:55:56 +0100189 memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100190 sl_init(&vcpu->lock);
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000191 vcpu->regs_available = true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100192 vcpu->vm = vm;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100193 vcpu->state = VCPU_STATE_OFF;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100194}
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100195
Andrew Walbranb58f8992019-04-15 12:29:31 +0100196/**
197 * Initialise the registers for the given vCPU and set the state to
198 * VCPU_STATE_READY. The caller must hold the vCPU lock while calling this.
199 */
200void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg)
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100201{
Andrew Walbranb58f8992019-04-15 12:29:31 +0100202 arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg);
203 vcpu.vcpu->state = VCPU_STATE_READY;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100204}
205
Andrew Scull38772ab2019-01-24 15:16:50 +0000206size_t vcpu_index(const struct vcpu *vcpu)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000207{
208 return vcpu - vcpu->vm->vcpus;
209}
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000210
211/**
Andrew Walbran9a43fee2019-04-18 17:42:32 +0100212 * Starts a vCPU of a secondary VM.
213 */
214void vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry,
215 uintreg_t arg)
216{
Andrew Walbranb58f8992019-04-15 12:29:31 +0100217 struct vcpu_locked vcpu_locked;
Andrew Walbran9a43fee2019-04-18 17:42:32 +0100218 struct vm *vm = vcpu->vm;
219
220 assert(vm->id != HF_PRIMARY_VM_ID);
221
Andrew Walbranb58f8992019-04-15 12:29:31 +0100222 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbran9a43fee2019-04-18 17:42:32 +0100223 /*
224 * Set vCPU registers to a clean state ready for boot. As this is a
225 * secondary which can migrate between pCPUs, the ID of the vCPU is
226 * defined as the index and does not match the ID of the pCPU it is
227 * running on.
228 */
229 arch_regs_reset(&vcpu->regs, false, vm->id, vcpu_index(vcpu),
230 vm->ptable.root);
Andrew Walbranb58f8992019-04-15 12:29:31 +0100231 vcpu_on(vcpu_locked, entry, arg);
232 vcpu_unlock(&vcpu_locked);
Andrew Walbran9a43fee2019-04-18 17:42:32 +0100233}
234
235/**
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000236 * Handles a page fault. It does so by determining if it's a legitimate or
237 * spurious fault, and recovering from the latter.
238 *
239 * Returns true if the caller should resume the current vcpu, or false if its VM
240 * should be aborted.
241 */
242bool vcpu_handle_page_fault(const struct vcpu *current,
243 struct vcpu_fault_info *f)
244{
245 struct vm *vm = current->vm;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000246 int mode;
247 int mask = f->mode | MM_MODE_INVALID;
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100248 bool resume;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000249
250 sl_lock(&vm->lock);
251
252 /*
253 * Check if this is a legitimate fault, i.e., if the page table doesn't
254 * allow the access attemped by the VM.
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100255 *
256 * Otherwise, this is a spurious fault, likely because another CPU is
257 * updating the page table. It is responsible for issuing global TLB
258 * invalidations while holding the VM lock, so we don't need to do
259 * anything else to recover from it. (Acquiring/releasing the lock
260 * ensured that the invalidations have completed.)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000261 */
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100262 resume = mm_vm_get_mode(&vm->ptable, f->ipaddr, ipa_add(f->ipaddr, 1),
263 &mode) &&
264 (mode & mask) == f->mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000265
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000266 sl_unlock(&vm->lock);
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100267
268 if (!resume) {
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000269 dlog("Stage-2 page fault: pc=0x%x, vmid=%u, vcpu=%u, "
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100270 "vaddr=0x%x, ipaddr=0x%x, mode=0x%x\n",
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000271 f->pc, vm->id, vcpu_index(current), f->vaddr, f->ipaddr,
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100272 f->mode);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000273 }
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100274
275 return resume;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000276}