blob: 9b44d9750b2a10c99673bc3558543b69c3b33d9b [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"
Andrew Scull877ae4b2019-07-02 12:52:33 +010024#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010025#include "hf/dlog.h"
Andrew Walbranb037d5b2019-06-25 17:19:41 +010026#include "hf/spci.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010027#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010028#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010029
Andrew Scull19503262018-09-20 14:48:39 +010030#include "vmapi/hf/call.h"
31
Andrew Scull23e93a82018-10-26 14:56:04 +010032#define STACK_SIZE PAGE_SIZE
33
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010034/* The stack to be used by the CPUs. */
Andrew Scull37402872018-10-24 14:23:06 +010035alignas(2 * sizeof(uintreg_t)) static char callstacks[MAX_CPUS][STACK_SIZE];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010036
37/* State of all supported CPUs. The stack of the first one is initialized. */
38struct cpu cpus[MAX_CPUS] = {
39 {
40 .is_on = 1,
Andrew Scullf3d45592018-09-20 14:30:22 +010041 .stack_bottom = &callstacks[0][STACK_SIZE],
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010042 },
43};
44
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000045static uint32_t cpu_count = 1;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010046
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000047static void cpu_init(struct cpu *c)
48{
49 /* TODO: Assumes that c is zeroed out already. */
50 sl_init(&c->lock);
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000051}
52
Andrew Walbran4d3fa282019-06-26 13:31:15 +010053void cpu_module_init(const cpu_id_t *cpu_ids, size_t count)
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000054{
55 uint32_t i;
56 uint32_t j;
Andrew Walbran4d3fa282019-06-26 13:31:15 +010057 cpu_id_t boot_cpu_id = cpus[0].id;
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000058 bool found_boot_cpu = false;
59
60 cpu_count = count;
61
62 /*
63 * Initialize CPUs with the IDs from the configuration passed in. The
64 * CPUs after the boot CPU are initialized in reverse order. The boot
65 * CPU is initialized when it is found or in place of the last CPU if it
66 * is not found.
67 */
68 j = cpu_count;
69 for (i = 0; i < cpu_count; ++i) {
70 struct cpu *c;
Andrew Walbran4d3fa282019-06-26 13:31:15 +010071 cpu_id_t id = cpu_ids[i];
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000072
73 if (found_boot_cpu || id != boot_cpu_id) {
74 c = &cpus[--j];
75 } else {
76 found_boot_cpu = true;
77 c = &cpus[0];
78 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000079
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010080 cpu_init(c);
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000081 c->id = id;
Andrew Scullf3d45592018-09-20 14:30:22 +010082 c->stack_bottom = &callstacks[i][STACK_SIZE];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010083 }
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000084
85 if (!found_boot_cpu) {
86 /* Boot CPU was initialized but with wrong ID. */
87 dlog("Boot CPU's ID not found in config.");
88 cpus[0].id = boot_cpu_id;
89 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010090}
91
92size_t cpu_index(struct cpu *c)
93{
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010094 return c - cpus;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010095}
96
Wedson Almeida Filho87009642018-07-02 10:20:07 +010097/**
98 * Turns CPU on and returns the previous state.
99 */
Andrew Scull37402872018-10-24 14:23:06 +0100100bool cpu_on(struct cpu *c, ipaddr_t entry, uintreg_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100101{
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100102 bool prev;
103
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100104 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100105 prev = c->is_on;
106 c->is_on = true;
107 sl_unlock(&c->lock);
108
109 if (!prev) {
Andrew Walbran42347a92019-05-09 13:59:03 +0100110 struct vm *vm = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100111 struct vcpu *vcpu = vm_get_vcpu(vm, cpu_index(c));
Andrew Walbranb58f8992019-04-15 12:29:31 +0100112 struct vcpu_locked vcpu_locked;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000113
Andrew Walbranb58f8992019-04-15 12:29:31 +0100114 vcpu_locked = vcpu_lock(vcpu);
115 vcpu_on(vcpu_locked, entry, arg);
116 vcpu_unlock(&vcpu_locked);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100117 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100118
119 return prev;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100120}
121
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100122/**
123 * Prepares the CPU for turning itself off.
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100124 */
125void cpu_off(struct cpu *c)
126{
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100127 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100128 c->is_on = false;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100129 sl_unlock(&c->lock);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100130}
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100131
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100132/**
133 * Searches for a CPU based on its id.
134 */
Andrew Walbran4d3fa282019-06-26 13:31:15 +0100135struct cpu *cpu_find(cpu_id_t id)
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100136{
137 size_t i;
138
Andrew Scullbb3ab6c2018-11-26 20:38:49 +0000139 for (i = 0; i < cpu_count; i++) {
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100140 if (cpus[i].id == id) {
Andrew Scullf3d45592018-09-20 14:30:22 +0100141 return &cpus[i];
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100142 }
143 }
144
145 return NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100146}
147
Andrew Walbranb58f8992019-04-15 12:29:31 +0100148/**
149 * Locks the given vCPU and updates `locked` to hold the newly locked vCPU.
150 */
151struct vcpu_locked vcpu_lock(struct vcpu *vcpu)
152{
153 struct vcpu_locked locked = {
154 .vcpu = vcpu,
155 };
156
157 sl_lock(&vcpu->lock);
158
159 return locked;
160}
161
162/**
163 * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to
164 * reflect the fact that the vCPU is no longer locked.
165 */
166void vcpu_unlock(struct vcpu_locked *locked)
167{
168 sl_unlock(&locked->vcpu->lock);
169 locked->vcpu = NULL;
170}
171
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100172void vcpu_init(struct vcpu *vcpu, struct vm *vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100173{
Andrew Scull2b5fbad2019-04-05 13:55:56 +0100174 memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100175 sl_init(&vcpu->lock);
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000176 vcpu->regs_available = true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100177 vcpu->vm = vm;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100178 vcpu->state = VCPU_STATE_OFF;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100179}
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100180
Andrew Walbranb58f8992019-04-15 12:29:31 +0100181/**
182 * Initialise the registers for the given vCPU and set the state to
183 * VCPU_STATE_READY. The caller must hold the vCPU lock while calling this.
184 */
185void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg)
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100186{
Andrew Walbranb58f8992019-04-15 12:29:31 +0100187 arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg);
188 vcpu.vcpu->state = VCPU_STATE_READY;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100189}
190
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100191spci_vcpu_index_t vcpu_index(const struct vcpu *vcpu)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000192{
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100193 size_t index = vcpu - vcpu->vm->vcpus;
194
Andrew Scull877ae4b2019-07-02 12:52:33 +0100195 CHECK(index < UINT16_MAX);
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100196 return index;
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000197}
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000198
199/**
Andrew Walbran33645652019-04-15 12:29:31 +0100200 * Check whether the given vcpu_state is an off state, for the purpose of
201 * turning vCPUs on and off. Note that aborted still counts as on in this
202 * context.
Andrew Walbran9a43fee2019-04-18 17:42:32 +0100203 */
Andrew Walbran33645652019-04-15 12:29:31 +0100204bool vcpu_is_off(struct vcpu_locked vcpu)
205{
206 switch (vcpu.vcpu->state) {
207 case VCPU_STATE_OFF:
208 return true;
209 case VCPU_STATE_READY:
210 case VCPU_STATE_RUNNING:
211 case VCPU_STATE_BLOCKED_MAILBOX:
212 case VCPU_STATE_BLOCKED_INTERRUPT:
213 case VCPU_STATE_ABORTED:
214 /*
215 * Aborted still counts as ON for the purposes of PSCI,
216 * because according to the PSCI specification (section
217 * 5.7.1) a core is only considered to be off if it has
218 * been turned off with a CPU_OFF call or hasn't yet
219 * been turned on with a CPU_ON call.
220 */
221 return false;
222 }
223}
224
225/**
226 * Starts a vCPU of a secondary VM.
227 *
228 * Returns true if the secondary was reset and started, or false if it was
229 * already on and so nothing was done.
230 */
231bool vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry,
Andrew Walbran9a43fee2019-04-18 17:42:32 +0100232 uintreg_t arg)
233{
Andrew Walbranb58f8992019-04-15 12:29:31 +0100234 struct vcpu_locked vcpu_locked;
Andrew Walbran9a43fee2019-04-18 17:42:32 +0100235 struct vm *vm = vcpu->vm;
Andrew Walbran33645652019-04-15 12:29:31 +0100236 bool vcpu_was_off;
Andrew Walbran9a43fee2019-04-18 17:42:32 +0100237
Andrew Scull877ae4b2019-07-02 12:52:33 +0100238 CHECK(vm->id != HF_PRIMARY_VM_ID);
Andrew Walbran9a43fee2019-04-18 17:42:32 +0100239
Andrew Walbranb58f8992019-04-15 12:29:31 +0100240 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbran33645652019-04-15 12:29:31 +0100241 vcpu_was_off = vcpu_is_off(vcpu_locked);
242 if (vcpu_was_off) {
243 /*
244 * Set vCPU registers to a clean state ready for boot. As this
245 * is a secondary which can migrate between pCPUs, the ID of the
246 * vCPU is defined as the index and does not match the ID of the
247 * pCPU it is running on.
248 */
249 arch_regs_reset(&vcpu->regs, false, vm->id, vcpu_index(vcpu),
250 vm->ptable.root);
251 vcpu_on(vcpu_locked, entry, arg);
252 }
Andrew Walbranb58f8992019-04-15 12:29:31 +0100253 vcpu_unlock(&vcpu_locked);
Andrew Walbran33645652019-04-15 12:29:31 +0100254
255 return vcpu_was_off;
Andrew Walbran9a43fee2019-04-18 17:42:32 +0100256}
257
258/**
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000259 * Handles a page fault. It does so by determining if it's a legitimate or
260 * spurious fault, and recovering from the latter.
261 *
262 * Returns true if the caller should resume the current vcpu, or false if its VM
263 * should be aborted.
264 */
265bool vcpu_handle_page_fault(const struct vcpu *current,
266 struct vcpu_fault_info *f)
267{
268 struct vm *vm = current->vm;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000269 int mode;
270 int mask = f->mode | MM_MODE_INVALID;
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100271 bool resume;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000272
273 sl_lock(&vm->lock);
274
275 /*
276 * Check if this is a legitimate fault, i.e., if the page table doesn't
277 * allow the access attemped by the VM.
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100278 *
279 * Otherwise, this is a spurious fault, likely because another CPU is
280 * updating the page table. It is responsible for issuing global TLB
281 * invalidations while holding the VM lock, so we don't need to do
282 * anything else to recover from it. (Acquiring/releasing the lock
283 * ensured that the invalidations have completed.)
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000284 */
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100285 resume = mm_vm_get_mode(&vm->ptable, f->ipaddr, ipa_add(f->ipaddr, 1),
286 &mode) &&
287 (mode & mask) == f->mode;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000288
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000289 sl_unlock(&vm->lock);
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100290
291 if (!resume) {
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000292 dlog("Stage-2 page fault: pc=0x%x, vmid=%u, vcpu=%u, "
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100293 "vaddr=0x%x, ipaddr=0x%x, mode=0x%x\n",
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000294 f->pc, vm->id, vcpu_index(current), f->vaddr, f->ipaddr,
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100295 f->mode);
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000296 }
Andrew Sculld3cfaad2019-04-04 11:34:10 +0100297
298 return resume;
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000299}