blob: 1f154a847bd4d733c7f6df145f20654b93f21873 [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
J-Alves7ac49052022-02-08 17:20:53 +000018/** GP register to be used to pass the current vCPU ID, at core bring up. */
19#define PHYS_CORE_IDX_GP_REG 4
20
Fuad Tabba5c738432019-12-02 11:02:42 +000021/**
22 * Locks the given vCPU and updates `locked` to hold the newly locked vCPU.
23 */
24struct vcpu_locked vcpu_lock(struct vcpu *vcpu)
25{
26 struct vcpu_locked locked = {
27 .vcpu = vcpu,
28 };
29
30 sl_lock(&vcpu->lock);
31
32 return locked;
33}
34
35/**
Olivier Deprez0b6f10a2020-08-05 18:21:33 +020036 * Locks two vCPUs ensuring that the locking order is according to the locks'
37 * addresses.
38 */
39struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2)
40{
41 struct two_vcpu_locked dual_lock;
42
43 sl_lock_both(&vcpu1->lock, &vcpu2->lock);
44 dual_lock.vcpu1.vcpu = vcpu1;
45 dual_lock.vcpu2.vcpu = vcpu2;
46
47 return dual_lock;
48}
49
50/**
Fuad Tabba5c738432019-12-02 11:02:42 +000051 * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to
52 * reflect the fact that the vCPU is no longer locked.
53 */
54void vcpu_unlock(struct vcpu_locked *locked)
55{
56 sl_unlock(&locked->vcpu->lock);
57 locked->vcpu = NULL;
58}
59
60void vcpu_init(struct vcpu *vcpu, struct vm *vm)
61{
62 memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu));
63 sl_init(&vcpu->lock);
64 vcpu->regs_available = true;
65 vcpu->vm = vm;
66 vcpu->state = VCPU_STATE_OFF;
Olivier Deprezee9d6a92019-11-26 09:14:11 +000067 vcpu->direct_request_origin_vm_id = HF_INVALID_VM_ID;
Madhukar Pappireddy84154052022-06-21 18:30:25 -050068 vcpu->present_action_ns_interrupts = NS_ACTION_INVALID;
Fuad Tabba5c738432019-12-02 11:02:42 +000069}
70
71/**
72 * Initialise the registers for the given vCPU and set the state to
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050073 * VCPU_STATE_WAITING. The caller must hold the vCPU lock while calling this.
Fuad Tabba5c738432019-12-02 11:02:42 +000074 */
75void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg)
76{
77 arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050078 vcpu.vcpu->state = VCPU_STATE_WAITING;
Fuad Tabba5c738432019-12-02 11:02:42 +000079}
80
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010081ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu)
Fuad Tabba5c738432019-12-02 11:02:42 +000082{
83 size_t index = vcpu - vcpu->vm->vcpus;
84
85 CHECK(index < UINT16_MAX);
86 return index;
87}
88
89/**
90 * Check whether the given vcpu_state is an off state, for the purpose of
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050091 * turning vCPUs on and off. Note that Aborted still counts as ON for the
92 * purposes of PSCI, because according to the PSCI specification (section
Olivier Depreze7eb1682022-03-16 17:09:03 +010093 * 5.7.1) a core is only considered to be off if it has been turned off
94 * with a CPU_OFF call or hasn't yet been turned on with a CPU_ON call.
Fuad Tabba5c738432019-12-02 11:02:42 +000095 */
96bool vcpu_is_off(struct vcpu_locked vcpu)
97{
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050098 return (vcpu.vcpu->state == VCPU_STATE_OFF);
Fuad Tabba5c738432019-12-02 11:02:42 +000099}
100
101/**
102 * Starts a vCPU of a secondary VM.
103 *
104 * Returns true if the secondary was reset and started, or false if it was
105 * already on and so nothing was done.
106 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100107bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked,
108 ipaddr_t entry, uintreg_t arg)
Fuad Tabba5c738432019-12-02 11:02:42 +0000109{
Max Shvetsov40108e72020-08-27 12:39:50 +0100110 struct vm *vm = vcpu_locked.vcpu->vm;
Fuad Tabba5c738432019-12-02 11:02:42 +0000111 bool vcpu_was_off;
112
113 CHECK(vm->id != HF_PRIMARY_VM_ID);
114
Fuad Tabba5c738432019-12-02 11:02:42 +0000115 vcpu_was_off = vcpu_is_off(vcpu_locked);
116 if (vcpu_was_off) {
117 /*
118 * Set vCPU registers to a clean state ready for boot. As this
119 * is a secondary which can migrate between pCPUs, the ID of the
120 * vCPU is defined as the index and does not match the ID of the
121 * pCPU it is running on.
122 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100123 arch_regs_reset(vcpu_locked.vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +0000124 vcpu_on(vcpu_locked, entry, arg);
125 }
Fuad Tabba5c738432019-12-02 11:02:42 +0000126
127 return vcpu_was_off;
128}
129
130/**
131 * Handles a page fault. It does so by determining if it's a legitimate or
132 * spurious fault, and recovering from the latter.
133 *
Fuad Tabbaed294af2019-12-20 10:43:01 +0000134 * Returns true if the caller should resume the current vCPU, or false if its VM
Fuad Tabba5c738432019-12-02 11:02:42 +0000135 * should be aborted.
136 */
137bool vcpu_handle_page_fault(const struct vcpu *current,
138 struct vcpu_fault_info *f)
139{
140 struct vm *vm = current->vm;
141 uint32_t mode;
142 uint32_t mask = f->mode | MM_MODE_INVALID;
143 bool resume;
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800144 struct vm_locked locked_vm;
Fuad Tabba5c738432019-12-02 11:02:42 +0000145
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800146 locked_vm = vm_lock(vm);
Fuad Tabba5c738432019-12-02 11:02:42 +0000147 /*
148 * Check if this is a legitimate fault, i.e., if the page table doesn't
149 * allow the access attempted by the VM.
150 *
151 * Otherwise, this is a spurious fault, likely because another CPU is
152 * updating the page table. It is responsible for issuing global TLB
153 * invalidations while holding the VM lock, so we don't need to do
154 * anything else to recover from it. (Acquiring/releasing the lock
155 * ensured that the invalidations have completed.)
156 */
Raghu Krishnamurthyb5775d22021-02-26 18:54:40 -0800157 if (!locked_vm.vm->el0_partition) {
158 resume = vm_mem_get_mode(locked_vm, f->ipaddr,
159 ipa_add(f->ipaddr, 1), &mode) &&
160 (mode & mask) == f->mode;
161 } else {
162 /*
163 * For EL0 partitions we need to get the mode for the faulting
164 * vaddr.
165 */
166 resume =
167 vm_mem_get_mode(locked_vm, ipa_init(va_addr(f->vaddr)),
168 ipa_add(ipa_init(va_addr(f->vaddr)), 1),
169 &mode) &&
170 (mode & mask) == f->mode;
Raghu Krishnamurthyf16b2ce2021-11-02 07:48:38 -0700171
172 /*
173 * For EL0 partitions, if there is an instruction abort and the
174 * mode of the page is RWX, we don't resume since Hafnium does
175 * not allow write and executable pages.
176 */
177 if ((f->mode == MM_MODE_X) &&
178 ((mode & MM_MODE_W) == MM_MODE_W)) {
179 resume = false;
180 }
Raghu Krishnamurthyb5775d22021-02-26 18:54:40 -0800181 }
Fuad Tabba5c738432019-12-02 11:02:42 +0000182
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800183 vm_unlock(&locked_vm);
Fuad Tabba5c738432019-12-02 11:02:42 +0000184
185 if (!resume) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000186 dlog_warning(
Raghu Krishnamurthyb5775d22021-02-26 18:54:40 -0800187 "Stage-%d page fault: pc=%#x, vmid=%#x, vcpu=%u, "
188 "vaddr=%#x, ipaddr=%#x, mode=%#x %#x\n",
189 current->vm->el0_partition ? 1 : 2, f->pc, vm->id,
190 vcpu_index(current), f->vaddr, f->ipaddr, f->mode,
191 mode);
Fuad Tabba5c738432019-12-02 11:02:42 +0000192 }
193
194 return resume;
195}
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200196
Olivier Depreze6f7b9d2021-02-01 11:55:48 +0100197void vcpu_reset(struct vcpu *vcpu)
198{
Max Shvetsov40108e72020-08-27 12:39:50 +0100199 arch_cpu_init(vcpu->cpu, vcpu->vm->secondary_ep);
Olivier Depreze6f7b9d2021-02-01 11:55:48 +0100200
201 /* Reset the registers to give a clean start for vCPU. */
202 arch_regs_reset(vcpu);
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500203 vcpu->rt_model = RTM_SP_INIT;
Olivier Depreze6f7b9d2021-02-01 11:55:48 +0100204}
J-Alves7ac49052022-02-08 17:20:53 +0000205
206void vcpu_set_phys_core_idx(struct vcpu *vcpu)
207{
208 arch_regs_set_gp_reg(&vcpu->regs, cpu_index(vcpu->cpu),
209 PHYS_CORE_IDX_GP_REG);
210}