blob: 2c585993db17e769061b887d3063c6247672cd06 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
2 * Copyright 2018 Google LLC
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 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"
25#include "hf/std.h"
26#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
43void cpu_module_init(void)
44{
45 size_t i;
46
47 /* Initialize all CPUs. */
48 for (i = 0; i < MAX_CPUS; i++) {
Andrew Scullf3d45592018-09-20 14:30:22 +010049 struct cpu *c = &cpus[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000050
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010051 cpu_init(c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010052 c->id = i; /* TODO: Initialize ID based on fdt. */
Andrew Scullf3d45592018-09-20 14:30:22 +010053 c->stack_bottom = &callstacks[i][STACK_SIZE];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010054 }
55}
56
57size_t cpu_index(struct cpu *c)
58{
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010059 return c - cpus;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010060}
61
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010062void cpu_init(struct cpu *c)
63{
64 /* TODO: Assumes that c is zeroed out already. */
65 sl_init(&c->lock);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010066 c->irq_disable_count = 1;
67}
68
69void cpu_irq_enable(struct cpu *c)
70{
71 c->irq_disable_count--;
Andrew Scull7364a8e2018-07-19 15:39:29 +010072 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010073 arch_irq_enable();
Andrew Scull7364a8e2018-07-19 15:39:29 +010074 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010075}
76
77void cpu_irq_disable(struct cpu *c)
78{
Andrew Scull7364a8e2018-07-19 15:39:29 +010079 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010080 arch_irq_disable();
Andrew Scull7364a8e2018-07-19 15:39:29 +010081 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010082 c->irq_disable_count++;
83}
84
Wedson Almeida Filho87009642018-07-02 10:20:07 +010085/**
86 * Turns CPU on and returns the previous state.
87 */
Andrew Scull37402872018-10-24 14:23:06 +010088bool cpu_on(struct cpu *c, ipaddr_t entry, uintreg_t arg)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010089{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010090 bool prev;
91
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010092 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010093 prev = c->is_on;
94 c->is_on = true;
95 sl_unlock(&c->lock);
96
97 if (!prev) {
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +010098 struct vm *vm = vm_get(HF_PRIMARY_VM_ID);
99 struct vcpu *vcpu = &vm->vcpus[cpu_index(c)];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000100
Andrew Scullc960c032018-10-24 15:13:35 +0100101 vcpu_on(vcpu, entry, arg);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100102 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100103
104 return prev;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100105}
106
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100107/**
108 * Prepares the CPU for turning itself off.
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100109 */
110void cpu_off(struct cpu *c)
111{
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100112 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100113 c->is_on = false;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100114 sl_unlock(&c->lock);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100115}
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100116
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100117/**
118 * Searches for a CPU based on its id.
119 */
120struct cpu *cpu_find(size_t id)
121{
122 size_t i;
123
124 for (i = 0; i < MAX_CPUS; i++) {
125 if (cpus[i].id == id) {
Andrew Scullf3d45592018-09-20 14:30:22 +0100126 return &cpus[i];
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100127 }
128 }
129
130 return NULL;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100131}
132
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100133void vcpu_init(struct vcpu *vcpu, struct vm *vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100134{
135 memset(vcpu, 0, sizeof(*vcpu));
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100136 sl_init(&vcpu->lock);
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000137 vcpu->regs_available = true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100138 vcpu->vm = vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100139 vcpu->state = vcpu_state_off;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100140}
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100141
Andrew Scullc960c032018-10-24 15:13:35 +0100142void vcpu_on(struct vcpu *vcpu, ipaddr_t entry, uintreg_t arg)
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100143{
Andrew Scullc960c032018-10-24 15:13:35 +0100144 arch_regs_set_pc_arg(&vcpu->regs, entry, arg);
145
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100146 sl_lock(&vcpu->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100147 vcpu->state = vcpu_state_ready;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100148 sl_unlock(&vcpu->lock);
149}
150
151void vcpu_off(struct vcpu *vcpu)
152{
153 sl_lock(&vcpu->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100154 vcpu->state = vcpu_state_off;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100155 sl_unlock(&vcpu->lock);
156}
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000157
Andrew Scull38772ab2019-01-24 15:16:50 +0000158size_t vcpu_index(const struct vcpu *vcpu)
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000159{
160 return vcpu - vcpu->vm->vcpus;
161}
Wedson Almeida Filho99d2d4c2019-02-14 12:53:46 +0000162
163/**
164 * Handles a page fault. It does so by determining if it's a legitimate or
165 * spurious fault, and recovering from the latter.
166 *
167 * Returns true if the caller should resume the current vcpu, or false if its VM
168 * should be aborted.
169 */
170bool vcpu_handle_page_fault(const struct vcpu *current,
171 struct vcpu_fault_info *f)
172{
173 struct vm *vm = current->vm;
174 ipaddr_t second_addr;
175 bool second;
176 int mode;
177 int mask = f->mode | MM_MODE_INVALID;
178 bool ret = false;
179
180 /* We can't recover if we don't know the size. */
181 if (f->size == 0) {
182 goto exit;
183 }
184
185 sl_lock(&vm->lock);
186
187 /*
188 * Check if this is a legitimate fault, i.e., if the page table doesn't
189 * allow the access attemped by the VM.
190 */
191 if (!mm_vm_get_mode(&vm->ptable, f->ipaddr, ipa_add(f->ipaddr, 1),
192 &mode) ||
193 (mode & mask) != f->mode) {
194 goto exit_unlock;
195 }
196
197 /*
198 * Do the same mode check on the second page, if the fault straddles two
199 * pages.
200 */
201 second_addr = ipa_add(f->ipaddr, f->size - 1);
202 second = (ipa_addr(f->ipaddr) >> PAGE_BITS) !=
203 (ipa_addr(second_addr) >> PAGE_BITS);
204 if (second) {
205 if (!mm_vm_get_mode(&vm->ptable, second_addr,
206 ipa_add(second_addr, 1), &mode) ||
207 (mode & mask) != f->mode) {
208 goto exit_unlock;
209 }
210 }
211
212 /*
213 * This is a spurious fault, likely because another CPU is updating the
214 * page table. It is responsible for issuing global tlb invalidations
215 * while holding the VM lock, so we don't need to do anything else to
216 * recover from it. (Acquiring/releasing the lock ensured that the
217 * invalidations have completed.)
218 */
219
220 ret = true;
221
222exit_unlock:
223 sl_unlock(&vm->lock);
224exit:
225 if (!ret) {
226 dlog("Stage-2 page fault: pc=0x%x, vmid=%u, vcpu=%u, "
227 "vaddr=0x%x, ipaddr=0x%x, mode=0x%x, size=%u\n",
228 f->pc, vm->id, vcpu_index(current), f->vaddr, f->ipaddr,
229 f->mode, f->size);
230 }
231 return ret;
232}