blob: 6b1e161cf01e04cb2c5487123fc9b6503bffbd29 [file] [log] [blame]
Fuad Tabba5c738432019-12-02 11:02:42 +00001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Fuad Tabba5c738432019-12-02 11:02:42 +00007 */
8
9#include "hf/vcpu.h"
10
Olivier Depreze6f7b9d2021-02-01 11:55:48 +010011#include "hf/arch/cpu.h"
12
Fuad Tabba5c738432019-12-02 11:02:42 +000013#include "hf/check.h"
14#include "hf/dlog.h"
15#include "hf/std.h"
16#include "hf/vm.h"
17
18/**
19 * Locks the given vCPU and updates `locked` to hold the newly locked vCPU.
20 */
21struct vcpu_locked vcpu_lock(struct vcpu *vcpu)
22{
23 struct vcpu_locked locked = {
24 .vcpu = vcpu,
25 };
26
27 sl_lock(&vcpu->lock);
28
29 return locked;
30}
31
32/**
Olivier Deprez0b6f10a2020-08-05 18:21:33 +020033 * Locks two vCPUs ensuring that the locking order is according to the locks'
34 * addresses.
35 */
36struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2)
37{
38 struct two_vcpu_locked dual_lock;
39
40 sl_lock_both(&vcpu1->lock, &vcpu2->lock);
41 dual_lock.vcpu1.vcpu = vcpu1;
42 dual_lock.vcpu2.vcpu = vcpu2;
43
44 return dual_lock;
45}
46
47/**
Fuad Tabba5c738432019-12-02 11:02:42 +000048 * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to
49 * reflect the fact that the vCPU is no longer locked.
50 */
51void vcpu_unlock(struct vcpu_locked *locked)
52{
53 sl_unlock(&locked->vcpu->lock);
54 locked->vcpu = NULL;
55}
56
57void vcpu_init(struct vcpu *vcpu, struct vm *vm)
58{
59 memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu));
60 sl_init(&vcpu->lock);
61 vcpu->regs_available = true;
62 vcpu->vm = vm;
63 vcpu->state = VCPU_STATE_OFF;
Olivier Deprezee9d6a92019-11-26 09:14:11 +000064 vcpu->direct_request_origin_vm_id = HF_INVALID_VM_ID;
Fuad Tabba5c738432019-12-02 11:02:42 +000065}
66
67/**
68 * Initialise the registers for the given vCPU and set the state to
69 * VCPU_STATE_READY. The caller must hold the vCPU lock while calling this.
70 */
71void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg)
72{
73 arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg);
74 vcpu.vcpu->state = VCPU_STATE_READY;
75}
76
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010077ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu)
Fuad Tabba5c738432019-12-02 11:02:42 +000078{
79 size_t index = vcpu - vcpu->vm->vcpus;
80
81 CHECK(index < UINT16_MAX);
82 return index;
83}
84
85/**
86 * Check whether the given vcpu_state is an off state, for the purpose of
87 * turning vCPUs on and off. Note that aborted still counts as on in this
88 * context.
89 */
90bool vcpu_is_off(struct vcpu_locked vcpu)
91{
92 switch (vcpu.vcpu->state) {
93 case VCPU_STATE_OFF:
94 return true;
95 case VCPU_STATE_READY:
96 case VCPU_STATE_RUNNING:
97 case VCPU_STATE_BLOCKED_MAILBOX:
98 case VCPU_STATE_BLOCKED_INTERRUPT:
99 case VCPU_STATE_ABORTED:
100 /*
101 * Aborted still counts as ON for the purposes of PSCI,
102 * because according to the PSCI specification (section
103 * 5.7.1) a core is only considered to be off if it has
104 * been turned off with a CPU_OFF call or hasn't yet
105 * been turned on with a CPU_ON call.
106 */
107 return false;
108 }
109}
110
111/**
112 * Starts a vCPU of a secondary VM.
113 *
114 * Returns true if the secondary was reset and started, or false if it was
115 * already on and so nothing was done.
116 */
117bool vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry,
118 uintreg_t arg)
119{
120 struct vcpu_locked vcpu_locked;
121 struct vm *vm = vcpu->vm;
122 bool vcpu_was_off;
123
124 CHECK(vm->id != HF_PRIMARY_VM_ID);
125
126 vcpu_locked = vcpu_lock(vcpu);
127 vcpu_was_off = vcpu_is_off(vcpu_locked);
128 if (vcpu_was_off) {
129 /*
130 * Set vCPU registers to a clean state ready for boot. As this
131 * is a secondary which can migrate between pCPUs, the ID of the
132 * vCPU is defined as the index and does not match the ID of the
133 * pCPU it is running on.
134 */
135 arch_regs_reset(vcpu);
136 vcpu_on(vcpu_locked, entry, arg);
137 }
138 vcpu_unlock(&vcpu_locked);
139
140 return vcpu_was_off;
141}
142
143/**
144 * Handles a page fault. It does so by determining if it's a legitimate or
145 * spurious fault, and recovering from the latter.
146 *
Fuad Tabbaed294af2019-12-20 10:43:01 +0000147 * Returns true if the caller should resume the current vCPU, or false if its VM
Fuad Tabba5c738432019-12-02 11:02:42 +0000148 * should be aborted.
149 */
150bool vcpu_handle_page_fault(const struct vcpu *current,
151 struct vcpu_fault_info *f)
152{
153 struct vm *vm = current->vm;
154 uint32_t mode;
155 uint32_t mask = f->mode | MM_MODE_INVALID;
156 bool resume;
157
158 sl_lock(&vm->lock);
159
160 /*
161 * Check if this is a legitimate fault, i.e., if the page table doesn't
162 * allow the access attempted by the VM.
163 *
164 * Otherwise, this is a spurious fault, likely because another CPU is
165 * updating the page table. It is responsible for issuing global TLB
166 * invalidations while holding the VM lock, so we don't need to do
167 * anything else to recover from it. (Acquiring/releasing the lock
168 * ensured that the invalidations have completed.)
169 */
170 resume = mm_vm_get_mode(&vm->ptable, f->ipaddr, ipa_add(f->ipaddr, 1),
171 &mode) &&
172 (mode & mask) == f->mode;
173
174 sl_unlock(&vm->lock);
175
176 if (!resume) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000177 dlog_warning(
Olivier Deprez952988a2020-12-11 21:25:58 +0100178 "Stage-2 page fault: pc=%#x, vmid=%#x, vcpu=%u, "
Andrew Walbran17eebf92020-02-05 16:35:49 +0000179 "vaddr=%#x, ipaddr=%#x, mode=%#x\n",
180 f->pc, vm->id, vcpu_index(current), f->vaddr, f->ipaddr,
181 f->mode);
Fuad Tabba5c738432019-12-02 11:02:42 +0000182 }
183
184 return resume;
185}
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200186
187/**
188 * Return the vCPU corresponding to the other world vCPU whose index
189 * matches current vCPU index.
190 */
191struct vcpu *vcpu_get_other_world_counterpart(struct vcpu *current)
192{
193 struct vm *vm = vm_find(HF_OTHER_WORLD_ID);
194 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
195
196 return vm_get_vcpu(vm, current_cpu_index);
197}
Olivier Depreze6f7b9d2021-02-01 11:55:48 +0100198
199void vcpu_reset(struct vcpu *vcpu)
200{
201 arch_cpu_init(vcpu->cpu, ipa_init(0));
202
203 /* Reset the registers to give a clean start for vCPU. */
204 arch_regs_reset(vcpu);
205}