blob: f21cb175e8411dd2e82e306085e529c3613f58c7 [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-Alves67f5ba32024-09-27 18:07:11 +010018static struct list_entry boot_list = LIST_INIT(boot_list);
Olivier Deprez181074b2023-02-02 14:53:23 +010019
J-Alves7ac49052022-02-08 17:20:53 +000020/** GP register to be used to pass the current vCPU ID, at core bring up. */
21#define PHYS_CORE_IDX_GP_REG 4
22
Fuad Tabba5c738432019-12-02 11:02:42 +000023/**
24 * Locks the given vCPU and updates `locked` to hold the newly locked vCPU.
25 */
26struct vcpu_locked vcpu_lock(struct vcpu *vcpu)
27{
28 struct vcpu_locked locked = {
29 .vcpu = vcpu,
30 };
31
32 sl_lock(&vcpu->lock);
33
34 return locked;
35}
36
37/**
Olivier Deprez0b6f10a2020-08-05 18:21:33 +020038 * Locks two vCPUs ensuring that the locking order is according to the locks'
39 * addresses.
40 */
41struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2)
42{
43 struct two_vcpu_locked dual_lock;
44
45 sl_lock_both(&vcpu1->lock, &vcpu2->lock);
46 dual_lock.vcpu1.vcpu = vcpu1;
47 dual_lock.vcpu2.vcpu = vcpu2;
48
49 return dual_lock;
50}
51
52/**
Fuad Tabba5c738432019-12-02 11:02:42 +000053 * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to
54 * reflect the fact that the vCPU is no longer locked.
55 */
56void vcpu_unlock(struct vcpu_locked *locked)
57{
58 sl_unlock(&locked->vcpu->lock);
59 locked->vcpu = NULL;
60}
61
62void vcpu_init(struct vcpu *vcpu, struct vm *vm)
63{
64 memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu));
65 sl_init(&vcpu->lock);
66 vcpu->regs_available = true;
67 vcpu->vm = vm;
68 vcpu->state = VCPU_STATE_OFF;
Kathleen Capellae468c112023-12-13 17:56:28 -050069 vcpu->direct_request_origin.is_ffa_req2 = false;
70 vcpu->direct_request_origin.vm_id = HF_INVALID_VM_ID;
Olivier Deprezb2808332023-02-02 15:25:40 +010071 vcpu->rt_model = RTM_SP_INIT;
J-Alves67f5ba32024-09-27 18:07:11 +010072 list_init(&vcpu->boot_list_node);
Madhukar Pappireddyeed861e2024-09-25 13:50:54 -050073 list_init(&vcpu->timer_node);
Fuad Tabba5c738432019-12-02 11:02:42 +000074}
75
76/**
77 * Initialise the registers for the given vCPU and set the state to
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050078 * VCPU_STATE_WAITING. The caller must hold the vCPU lock while calling this.
Fuad Tabba5c738432019-12-02 11:02:42 +000079 */
80void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg)
81{
82 arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050083 vcpu.vcpu->state = VCPU_STATE_WAITING;
Fuad Tabba5c738432019-12-02 11:02:42 +000084}
85
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010086ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu)
Fuad Tabba5c738432019-12-02 11:02:42 +000087{
88 size_t index = vcpu - vcpu->vm->vcpus;
89
90 CHECK(index < UINT16_MAX);
91 return index;
92}
93
94/**
95 * Check whether the given vcpu_state is an off state, for the purpose of
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050096 * turning vCPUs on and off. Note that Aborted still counts as ON for the
97 * purposes of PSCI, because according to the PSCI specification (section
Olivier Depreze7eb1682022-03-16 17:09:03 +010098 * 5.7.1) a core is only considered to be off if it has been turned off
99 * with a CPU_OFF call or hasn't yet been turned on with a CPU_ON call.
Fuad Tabba5c738432019-12-02 11:02:42 +0000100 */
101bool vcpu_is_off(struct vcpu_locked vcpu)
102{
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500103 return (vcpu.vcpu->state == VCPU_STATE_OFF);
Fuad Tabba5c738432019-12-02 11:02:42 +0000104}
105
106/**
107 * Starts a vCPU of a secondary VM.
108 *
109 * Returns true if the secondary was reset and started, or false if it was
110 * already on and so nothing was done.
111 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100112bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked,
113 ipaddr_t entry, uintreg_t arg)
Fuad Tabba5c738432019-12-02 11:02:42 +0000114{
Max Shvetsov40108e72020-08-27 12:39:50 +0100115 struct vm *vm = vcpu_locked.vcpu->vm;
Fuad Tabba5c738432019-12-02 11:02:42 +0000116 bool vcpu_was_off;
117
118 CHECK(vm->id != HF_PRIMARY_VM_ID);
119
Fuad Tabba5c738432019-12-02 11:02:42 +0000120 vcpu_was_off = vcpu_is_off(vcpu_locked);
121 if (vcpu_was_off) {
122 /*
123 * Set vCPU registers to a clean state ready for boot. As this
124 * is a secondary which can migrate between pCPUs, the ID of the
125 * vCPU is defined as the index and does not match the ID of the
126 * pCPU it is running on.
127 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100128 arch_regs_reset(vcpu_locked.vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +0000129 vcpu_on(vcpu_locked, entry, arg);
130 }
Fuad Tabba5c738432019-12-02 11:02:42 +0000131
132 return vcpu_was_off;
133}
134
135/**
136 * Handles a page fault. It does so by determining if it's a legitimate or
137 * spurious fault, and recovering from the latter.
138 *
Fuad Tabbaed294af2019-12-20 10:43:01 +0000139 * Returns true if the caller should resume the current vCPU, or false if its VM
Fuad Tabba5c738432019-12-02 11:02:42 +0000140 * should be aborted.
141 */
142bool vcpu_handle_page_fault(const struct vcpu *current,
143 struct vcpu_fault_info *f)
144{
145 struct vm *vm = current->vm;
146 uint32_t mode;
147 uint32_t mask = f->mode | MM_MODE_INVALID;
148 bool resume;
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800149 struct vm_locked locked_vm;
Fuad Tabba5c738432019-12-02 11:02:42 +0000150
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800151 locked_vm = vm_lock(vm);
Fuad Tabba5c738432019-12-02 11:02:42 +0000152 /*
153 * Check if this is a legitimate fault, i.e., if the page table doesn't
154 * allow the access attempted by the VM.
155 *
156 * Otherwise, this is a spurious fault, likely because another CPU is
157 * updating the page table. It is responsible for issuing global TLB
158 * invalidations while holding the VM lock, so we don't need to do
159 * anything else to recover from it. (Acquiring/releasing the lock
160 * ensured that the invalidations have completed.)
161 */
Raghu Krishnamurthyb5775d22021-02-26 18:54:40 -0800162 if (!locked_vm.vm->el0_partition) {
163 resume = vm_mem_get_mode(locked_vm, f->ipaddr,
164 ipa_add(f->ipaddr, 1), &mode) &&
165 (mode & mask) == f->mode;
166 } else {
167 /*
168 * For EL0 partitions we need to get the mode for the faulting
169 * vaddr.
170 */
171 resume =
172 vm_mem_get_mode(locked_vm, ipa_init(va_addr(f->vaddr)),
173 ipa_add(ipa_init(va_addr(f->vaddr)), 1),
174 &mode) &&
175 (mode & mask) == f->mode;
Raghu Krishnamurthyf16b2ce2021-11-02 07:48:38 -0700176
177 /*
178 * For EL0 partitions, if there is an instruction abort and the
179 * mode of the page is RWX, we don't resume since Hafnium does
180 * not allow write and executable pages.
181 */
182 if ((f->mode == MM_MODE_X) &&
183 ((mode & MM_MODE_W) == MM_MODE_W)) {
184 resume = false;
185 }
Raghu Krishnamurthyb5775d22021-02-26 18:54:40 -0800186 }
Fuad Tabba5c738432019-12-02 11:02:42 +0000187
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800188 vm_unlock(&locked_vm);
Fuad Tabba5c738432019-12-02 11:02:42 +0000189
190 if (!resume) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000191 dlog_warning(
Karl Meakine8937d92024-03-19 16:04:25 +0000192 "Stage-%d page fault: pc=%#lx, vmid=%#x, vcpu=%u, "
193 "vaddr=%#lx, ipaddr=%#lx, mode=%#x %#x\n",
194 current->vm->el0_partition ? 1 : 2, va_addr(f->pc),
195 vm->id, vcpu_index(current), va_addr(f->vaddr),
196 ipa_addr(f->ipaddr), f->mode, mode);
Fuad Tabba5c738432019-12-02 11:02:42 +0000197 }
198
199 return resume;
200}
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200201
J-Alves7ac49052022-02-08 17:20:53 +0000202void vcpu_set_phys_core_idx(struct vcpu *vcpu)
203{
204 arch_regs_set_gp_reg(&vcpu->regs, cpu_index(vcpu->cpu),
205 PHYS_CORE_IDX_GP_REG);
206}
Olivier Deprez181074b2023-02-02 14:53:23 +0100207
208/**
Olivier Deprez632249e2022-09-26 09:18:31 +0200209 * Sets the designated GP register through which the vCPU expects to receive the
210 * boot info's address.
211 */
212void vcpu_set_boot_info_gp_reg(struct vcpu *vcpu)
213{
214 struct vm *vm = vcpu->vm;
215 uint32_t gp_register_num = vm->boot_info.gp_register_num;
216
217 if (vm->boot_info.blob_addr.ipa != 0U) {
218 arch_regs_set_gp_reg(&vcpu->regs,
219 ipa_addr(vm->boot_info.blob_addr),
220 gp_register_num);
221 }
222}
223
224/**
J-Alves67f5ba32024-09-27 18:07:11 +0100225 * The 'boot_list' is used as the start and end of the list.
226 * Start: the nodes it points to is the first vCPU to boot.
227 * End: the last node's next points to the entry.
228 */
229static bool vcpu_is_boot_list_end(struct vcpu *vcpu)
230{
231 return vcpu->boot_list_node.next == &boot_list;
232}
233
234/**
Olivier Deprez181074b2023-02-02 14:53:23 +0100235 * Gets the first partition to boot, according to Boot Protocol from FFA spec.
236 */
237struct vcpu *vcpu_get_boot_vcpu(void)
238{
J-Alves67f5ba32024-09-27 18:07:11 +0100239 assert(!list_empty(&boot_list));
240
241 return CONTAINER_OF(boot_list.next, struct vcpu, boot_list_node);
242}
243
244/**
245 * Returns the next element in the boot order list, if there is one.
246 */
247struct vcpu *vcpu_get_next_boot(struct vcpu *vcpu)
248{
249 return vcpu_is_boot_list_end(vcpu)
250 ? NULL
251 : CONTAINER_OF(vcpu->boot_list_node.next, struct vcpu,
252 boot_list_node);
Olivier Deprez181074b2023-02-02 14:53:23 +0100253}
254
255/**
256 * Insert in boot list, sorted by `boot_order` parameter in the vm structure
257 * and rooted in `first_boot_vm`.
258 */
259void vcpu_update_boot(struct vcpu *vcpu)
260{
261 struct vcpu *current = NULL;
Olivier Deprez181074b2023-02-02 14:53:23 +0100262
J-Alves67f5ba32024-09-27 18:07:11 +0100263 if (list_empty(&boot_list)) {
264 list_prepend(&boot_list, &vcpu->boot_list_node);
Olivier Deprez181074b2023-02-02 14:53:23 +0100265 return;
266 }
267
J-Alves67f5ba32024-09-27 18:07:11 +0100268 /*
269 * When getting to this point the first insertion should have
270 * been done.
271 */
272 current = vcpu_get_boot_vcpu();
273 assert(current != NULL);
Olivier Deprez181074b2023-02-02 14:53:23 +0100274
J-Alves67f5ba32024-09-27 18:07:11 +0100275 /*
276 * Iterate until the position is found according to boot order, or
277 * until we reach end of the list.
278 */
279 while (!vcpu_is_boot_list_end(current) &&
Olivier Deprez181074b2023-02-02 14:53:23 +0100280 current->vm->boot_order <= vcpu->vm->boot_order) {
J-Alves67f5ba32024-09-27 18:07:11 +0100281 current = vcpu_get_next_boot(current);
Olivier Deprez181074b2023-02-02 14:53:23 +0100282 }
283
J-Alves67f5ba32024-09-27 18:07:11 +0100284 current->vm->boot_order > vcpu->vm->boot_order
285 ? list_prepend(&current->boot_list_node, &vcpu->boot_list_node)
286 : list_append(&current->boot_list_node, &vcpu->boot_list_node);
Olivier Deprez181074b2023-02-02 14:53:23 +0100287}
J-Alves12cedae2023-08-04 14:37:37 +0100288
J-Alvesb8730e92024-08-07 18:28:55 +0100289void vcpu_interrupt_clear_decrement(struct vcpu_locked vcpu_locked,
290 uint32_t intid)
291{
292 struct interrupts *interrupts = &(vcpu_locked.vcpu->interrupts);
293
Daniel Boulby1f2babf2024-08-29 16:39:47 +0100294 /* Clear any specifics for the current intid. */
295 switch (intid) {
296 case HF_IPI_INTID:
297 vcpu_ipi_clear_info_get_retrieved(vcpu_locked);
298 break;
299 default:
300 /* Do no additional work. */
301 break;
302 }
303
J-Alvesb8730e92024-08-07 18:28:55 +0100304 vcpu_virt_interrupt_clear_pending(interrupts, intid);
305 vcpu_interrupt_count_decrement(vcpu_locked, interrupts, intid);
306}
307
J-Alves0247fe62024-02-23 10:21:46 +0000308/**
309 * Sets the vcpu in the VCPU_STATE_RUNNING.
J-Alves67a79262024-07-17 12:01:39 +0100310 * With that, its register are set as "not available".
311 * If there are registers to be written to vCPU's context, do so.
312 * However, this action is restricted to WAITING and BLOCKED states,
313 * as such, assert accordingly.
J-Alves0247fe62024-02-23 10:21:46 +0000314 */
J-Alves478faac2024-10-23 10:35:57 +0100315void vcpu_set_running(struct vcpu_locked target_locked,
316 const struct ffa_value *args)
J-Alves12cedae2023-08-04 14:37:37 +0100317{
318 struct vcpu *target_vcpu = target_locked.vcpu;
319
J-Alves67a79262024-07-17 12:01:39 +0100320 if (args != NULL) {
J-Alves0247fe62024-02-23 10:21:46 +0000321 CHECK(target_vcpu->regs_available);
J-Alves67a79262024-07-17 12:01:39 +0100322 assert(target_vcpu->state == VCPU_STATE_WAITING ||
323 target_vcpu->state == VCPU_STATE_BLOCKED);
J-Alves0247fe62024-02-23 10:21:46 +0000324
325 arch_regs_set_retval(&target_vcpu->regs, *args);
326 }
J-Alves12cedae2023-08-04 14:37:37 +0100327
328 /* Mark the registers as unavailable now. */
329 target_vcpu->regs_available = false;
330
331 /* We are about to resume target vCPU. */
332 target_vcpu->state = VCPU_STATE_RUNNING;
333}
334
335/**
J-Alves12cedae2023-08-04 14:37:37 +0100336 * It injects a virtual interrupt in the vcpu if is enabled and is not pending.
337 */
338void vcpu_interrupt_inject(struct vcpu_locked target_locked, uint32_t intid)
339{
340 struct vcpu *target_vcpu = target_locked.vcpu;
341 struct interrupts *interrupts = &target_vcpu->interrupts;
342
343 /*
344 * We only need to change state and (maybe) trigger a virtual interrupt
345 * if it is enabled and was not previously pending. Otherwise we can
346 * skip everything except setting the pending bit.
347 */
348 if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) &&
349 !vcpu_is_virt_interrupt_pending(interrupts, intid))) {
350 goto out;
351 }
352
353 /* Increment the count. */
354 vcpu_interrupt_count_increment(target_locked, interrupts, intid);
355
356 /*
357 * Only need to update state if there was not already an
358 * interrupt enabled and pending.
359 */
360 if (vcpu_interrupt_count_get(target_locked) != 1) {
361 goto out;
362 }
363
364out:
365 /* Either way, make it pending. */
366 vcpu_virt_interrupt_set_pending(interrupts, intid);
367}
368
J-Alves12cedae2023-08-04 14:37:37 +0100369void vcpu_enter_secure_interrupt_rtm(struct vcpu_locked vcpu_locked)
370{
371 struct vcpu *target_vcpu = vcpu_locked.vcpu;
372
373 assert(target_vcpu->scheduling_mode == NONE);
374 assert(target_vcpu->call_chain.prev_node == NULL);
375 assert(target_vcpu->call_chain.next_node == NULL);
376 assert(target_vcpu->rt_model == RTM_NONE);
377
378 target_vcpu->scheduling_mode = SPMC_MODE;
379 target_vcpu->rt_model = RTM_SEC_INTERRUPT;
380}
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500381
382static uint16_t queue_increment_index(uint16_t current_idx)
383{
384 /* Look at the next index. Wrap around if necessary. */
385 if (current_idx == VINT_QUEUE_MAX - 1) {
386 return 0;
387 }
388
389 return current_idx + 1;
390}
391
392static bool is_queue_empty(struct interrupt_queue *q)
393{
394 if (q->head == q->tail) {
395 return true;
396 }
397
398 return false;
399}
400
401/**
402 * Queue the pending virtual interrupt for target vCPU.
403 *
404 * Returns true if successful in pushing a new entry to the queue, or false
405 * otherwise.
406 */
407bool vcpu_interrupt_queue_push(struct vcpu_locked vcpu_locked, uint32_t vint_id)
408{
409 struct interrupt_queue *q;
410 uint16_t new_tail;
411
412 assert(vint_id != HF_INVALID_INTID);
413
414 q = &vcpu_locked.vcpu->interrupts.vint_q;
415
416 /*
417 * A new entry is pushed at the tail of the queue. Upon successful
418 * push operation, the tail increments or wraps around.
419 */
420 new_tail = queue_increment_index(q->tail);
421
422 /* If new_tail reaches head of the queue, then the queue is full. */
423 if (new_tail == q->head) {
424 return false;
425 }
426
427 /* Add the virtual interrupt to the queue. */
428 q->vint_buffer[q->tail] = vint_id;
429 q->tail = new_tail;
430
431 return true;
432}
433
434/**
435 * Remove an entry from the specified vCPU's queue at the head.
436 *
437 * Returns true if successful in removing the entry, or false otherwise.
438 */
439bool vcpu_interrupt_queue_pop(struct vcpu_locked vcpu_locked, uint32_t *vint_id)
440{
441 struct interrupt_queue *q;
442 uint16_t new_head;
443
444 assert(vint_id != NULL);
445
446 q = &vcpu_locked.vcpu->interrupts.vint_q;
447
448 /* Check if queue is empty. */
449 if (is_queue_empty(q)) {
450 return false;
451 }
452
453 /*
454 * An entry is removed from the head of the queue. Once successful, the
455 * head is incremented or wrapped around if needed.
456 */
457 new_head = queue_increment_index(q->head);
458 *vint_id = q->vint_buffer[q->head];
459 q->head = new_head;
460
461 return true;
462}
463
464/**
465 * Look for the first pending virtual interrupt from the vcpu's queue. Note
466 * that the entry is not removed from the queue.
467 *
468 * Returns true if a valid entry exists in the queue, or false otherwise.
469 */
470bool vcpu_interrupt_queue_peek(struct vcpu_locked vcpu_locked,
471 uint32_t *vint_id)
472{
473 struct interrupt_queue *q;
474 uint32_t queued_vint;
475
476 assert(vint_id != NULL);
477
478 q = &vcpu_locked.vcpu->interrupts.vint_q;
479
480 /* Check if queue is empty. */
481 if (is_queue_empty(q)) {
482 return false;
483 }
484
485 queued_vint = q->vint_buffer[q->head];
486 assert(queued_vint != HF_INVALID_INTID);
487
488 *vint_id = queued_vint;
489 return true;
490}
491
492/**
493 * Find if a specific virtual interrupt exists in the specified vCPU's queue.
494 *
495 * Returns true if such an entry exists in the queue, or false otherwise.
496 */
497bool vcpu_is_interrupt_in_queue(struct vcpu_locked vcpu_locked,
498 uint32_t vint_id)
499{
500 struct interrupt_queue *q;
501 uint16_t next;
502
503 assert(vint_id != HF_INVALID_INTID);
504
505 q = &vcpu_locked.vcpu->interrupts.vint_q;
506
507 /* Check if the queue is empty. */
508 if (is_queue_empty(q)) {
509 return false;
510 }
511
512 next = q->head;
513 while (true) {
514 /* Match found. */
515 if (q->vint_buffer[next] == vint_id) {
516 return true;
517 }
518
519 next = queue_increment_index(next);
520
521 /* Reached the end of queue. */
522 if (next == q->tail) {
523 break;
524 }
525 }
526
527 return false;
528}
529
530/**
531 * Check if there are any entries in the interrupt queue.
532 *
533 * Returns true if queue is empty, or false otherwise.
534 */
535bool vcpu_is_interrupt_queue_empty(struct vcpu_locked vcpu_locked)
536{
537 struct interrupt_queue *q;
538
539 q = &vcpu_locked.vcpu->interrupts.vint_q;
540
541 if (is_queue_empty(q)) {
542 return true;
543 }
544
545 return false;
546}
J-Alves3b31f092024-08-07 13:26:29 +0100547
548/**
549 * When interrupt handling is complete the preempted_vcpu field should go back
550 * to NULL.
551 */
552void vcpu_secure_interrupt_complete(struct vcpu_locked vcpu_locked)
553{
554 struct vcpu *vcpu;
555
556 vcpu = vcpu_locked.vcpu;
557 vcpu->preempted_vcpu = NULL;
J-Alvesac940752024-08-07 14:02:51 +0100558 vcpu->requires_deactivate_call = false;
J-Alves3b31f092024-08-07 13:26:29 +0100559}