Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * 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 Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "hf/addr.h" |
| 12 | #include "hf/spinlock.h" |
| 13 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 14 | #include "vmapi/hf/ffa.h" |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 15 | |
| 16 | /** The number of bits in each element of the interrupt bitfields. */ |
| 17 | #define INTERRUPT_REGISTER_BITS 32 |
| 18 | |
| 19 | enum vcpu_state { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 20 | /** The vCPU is switched off. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 21 | VCPU_STATE_OFF, |
| 22 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 23 | /** The vCPU is ready to be run. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 24 | VCPU_STATE_READY, |
| 25 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 26 | /** The vCPU is currently running. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 27 | VCPU_STATE_RUNNING, |
| 28 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 29 | /** The vCPU is waiting for a message. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 30 | VCPU_STATE_BLOCKED_MAILBOX, |
| 31 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 32 | /** The vCPU is waiting for an interrupt. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 33 | VCPU_STATE_BLOCKED_INTERRUPT, |
| 34 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 35 | /** The vCPU has aborted. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 36 | VCPU_STATE_ABORTED, |
| 37 | }; |
| 38 | |
| 39 | struct interrupts { |
| 40 | /** Bitfield keeping track of which interrupts are enabled. */ |
| 41 | uint32_t interrupt_enabled[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS]; |
| 42 | /** Bitfield keeping track of which interrupts are pending. */ |
| 43 | uint32_t interrupt_pending[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS]; |
| 44 | /** |
| 45 | * The number of interrupts which are currently both enabled and |
| 46 | * pending. i.e. the number of bits set in interrupt_enable & |
| 47 | * interrupt_pending. |
| 48 | */ |
| 49 | uint32_t enabled_and_pending_count; |
| 50 | }; |
| 51 | |
| 52 | struct vcpu_fault_info { |
| 53 | ipaddr_t ipaddr; |
| 54 | vaddr_t vaddr; |
| 55 | vaddr_t pc; |
| 56 | uint32_t mode; |
| 57 | }; |
| 58 | |
| 59 | struct vcpu { |
| 60 | struct spinlock lock; |
| 61 | |
| 62 | /* |
| 63 | * The state is only changed in the context of the vCPU being run. This |
| 64 | * ensures the scheduler can easily keep track of the vCPU state as |
| 65 | * transitions are indicated by the return code from the run call. |
| 66 | */ |
| 67 | enum vcpu_state state; |
| 68 | |
| 69 | struct cpu *cpu; |
| 70 | struct vm *vm; |
| 71 | struct arch_regs regs; |
| 72 | struct interrupts interrupts; |
| 73 | |
| 74 | /* |
| 75 | * Determine whether the 'regs' field is available for use. This is set |
| 76 | * to false when a vCPU is about to run on a physical CPU, and is set |
| 77 | * back to true when it is descheduled. |
| 78 | */ |
| 79 | bool regs_available; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * If the current vCPU is executing as a consequence of a |
| 83 | * FFA_MSG_SEND_DIRECT_REQ invocation, then this member holds the |
| 84 | * originating VM ID from which the call originated. |
| 85 | * The value HF_INVALID_VM_ID implies the vCPU is not executing as |
| 86 | * a result of a prior FFA_MSG_SEND_DIRECT_REQ invocation. |
| 87 | */ |
| 88 | ffa_vm_id_t direct_request_origin_vm_id; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /** Encapsulates a vCPU whose lock is held. */ |
| 92 | struct vcpu_locked { |
| 93 | struct vcpu *vcpu; |
| 94 | }; |
| 95 | |
Olivier Deprez | 0b6f10a | 2020-08-05 18:21:33 +0200 | [diff] [blame] | 96 | /** Container for two vcpu_locked structures. */ |
| 97 | struct two_vcpu_locked { |
| 98 | struct vcpu_locked vcpu1; |
| 99 | struct vcpu_locked vcpu2; |
| 100 | }; |
| 101 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 102 | struct vcpu_locked vcpu_lock(struct vcpu *vcpu); |
Olivier Deprez | 0b6f10a | 2020-08-05 18:21:33 +0200 | [diff] [blame] | 103 | struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 104 | void vcpu_unlock(struct vcpu_locked *locked); |
| 105 | void vcpu_init(struct vcpu *vcpu, struct vm *vm); |
| 106 | void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 107 | ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 108 | bool vcpu_is_off(struct vcpu_locked vcpu); |
| 109 | bool vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry, |
| 110 | uintreg_t arg); |
| 111 | |
| 112 | bool vcpu_handle_page_fault(const struct vcpu *current, |
| 113 | struct vcpu_fault_info *f); |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 114 | |
| 115 | struct vcpu *vcpu_get_other_world_counterpart(struct vcpu *current); |
Olivier Deprez | e6f7b9d | 2021-02-01 11:55:48 +0100 | [diff] [blame^] | 116 | void vcpu_reset(struct vcpu *vcpu); |