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" |
Daniel Boulby | 801f8ef | 2022-06-27 14:21:01 +0100 | [diff] [blame] | 12 | #include "hf/interrupt_desc.h" |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 13 | #include "hf/spinlock.h" |
| 14 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 15 | #include "vmapi/hf/ffa.h" |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 16 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 17 | enum vcpu_state { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 18 | /** The vCPU is switched off. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 19 | VCPU_STATE_OFF, |
| 20 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 21 | /** The vCPU is currently running. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 22 | VCPU_STATE_RUNNING, |
| 23 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 24 | /** The vCPU is waiting to be allocated CPU cycles to do work. */ |
| 25 | VCPU_STATE_WAITING, |
| 26 | |
| 27 | /** |
| 28 | * The vCPU is blocked and waiting for some work to complete on |
| 29 | * its behalf. |
| 30 | */ |
| 31 | VCPU_STATE_BLOCKED, |
| 32 | |
| 33 | /** The vCPU has been preempted by an interrupt. */ |
| 34 | VCPU_STATE_PREEMPTED, |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 35 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 36 | /** The vCPU is waiting for an interrupt. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 37 | VCPU_STATE_BLOCKED_INTERRUPT, |
| 38 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 39 | /** The vCPU has aborted. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 40 | VCPU_STATE_ABORTED, |
| 41 | }; |
| 42 | |
| 43 | struct interrupts { |
| 44 | /** Bitfield keeping track of which interrupts are enabled. */ |
| 45 | uint32_t interrupt_enabled[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS]; |
| 46 | /** Bitfield keeping track of which interrupts are pending. */ |
| 47 | uint32_t interrupt_pending[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS]; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 48 | /** Bitfield recording the interrupt pin configuration. */ |
| 49 | uint32_t interrupt_type[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS]; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 50 | /** |
| 51 | * The number of interrupts which are currently both enabled and |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 52 | * pending. Count independently virtual IRQ and FIQ interrupt types |
| 53 | * i.e. the sum of the two counters is the number of bits set in |
| 54 | * interrupt_enable & interrupt_pending. |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 55 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 56 | uint32_t enabled_and_pending_irq_count; |
| 57 | uint32_t enabled_and_pending_fiq_count; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | struct vcpu_fault_info { |
| 61 | ipaddr_t ipaddr; |
| 62 | vaddr_t vaddr; |
| 63 | vaddr_t pc; |
| 64 | uint32_t mode; |
| 65 | }; |
| 66 | |
| 67 | struct vcpu { |
| 68 | struct spinlock lock; |
| 69 | |
| 70 | /* |
| 71 | * The state is only changed in the context of the vCPU being run. This |
| 72 | * ensures the scheduler can easily keep track of the vCPU state as |
| 73 | * transitions are indicated by the return code from the run call. |
| 74 | */ |
| 75 | enum vcpu_state state; |
| 76 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 77 | bool is_bootstrapped; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 78 | struct cpu *cpu; |
| 79 | struct vm *vm; |
| 80 | struct arch_regs regs; |
| 81 | struct interrupts interrupts; |
| 82 | |
| 83 | /* |
| 84 | * Determine whether the 'regs' field is available for use. This is set |
| 85 | * to false when a vCPU is about to run on a physical CPU, and is set |
Olivier Deprez | 3caed1c | 2021-02-05 12:07:36 +0100 | [diff] [blame] | 86 | * back to true when it is descheduled. This is not relevant for the |
| 87 | * primary VM vCPUs in the normal world (or the "other world VM" vCPUs |
| 88 | * in the secure world) as they are pinned to physical CPUs and there |
| 89 | * is no contention to take care of. |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 90 | */ |
| 91 | bool regs_available; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * If the current vCPU is executing as a consequence of a |
| 95 | * FFA_MSG_SEND_DIRECT_REQ invocation, then this member holds the |
| 96 | * originating VM ID from which the call originated. |
| 97 | * The value HF_INVALID_VM_ID implies the vCPU is not executing as |
| 98 | * a result of a prior FFA_MSG_SEND_DIRECT_REQ invocation. |
| 99 | */ |
| 100 | ffa_vm_id_t direct_request_origin_vm_id; |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 101 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 102 | /** Determine whether partition is currently handling managed exit. */ |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 103 | bool processing_managed_exit; |
Madhukar Pappireddy | f675bb6 | 2021-08-03 12:57:10 -0500 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Determine whether vCPU is currently handling secure interrupt. |
| 107 | */ |
| 108 | bool processing_secure_interrupt; |
| 109 | bool secure_interrupt_deactivated; |
| 110 | |
| 111 | /** |
| 112 | * INTID of the current secure interrupt being processed by this vCPU. |
| 113 | */ |
| 114 | uint32_t current_sec_interrupt_id; |
| 115 | |
| 116 | /** |
| 117 | * Track current vCPU which got pre-empted when secure interrupt |
| 118 | * triggered. |
| 119 | */ |
| 120 | struct vcpu *preempted_vcpu; |
Madhukar Pappireddy | dd6fdfb | 2021-12-14 12:30:36 -0600 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * Current value of the Priority Mask register which is saved/restored |
| 124 | * during secure interrupt handling. |
| 125 | */ |
| 126 | uint8_t priority_mask; |
Madhukar Pappireddy | 0aaadbb | 2021-12-16 20:58:10 -0600 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * Per FF-A v1.1-Beta0 spec section 8.3, an SP can use multiple |
| 130 | * mechanisms to signal completion of secure interrupt handling. SP |
| 131 | * can invoke explicit FF-A ABIs, namely FFA_MSG_WAIT and FFA_RUN, |
| 132 | * when in WAITING/BLOCKED state respectively, but has to perform |
| 133 | * implicit signal completion mechanism by dropping the priority |
| 134 | * of the virtual secure interrupt when SPMC signaled the virtual |
| 135 | * interrupt in PREEMPTED state(The vCPU was preempted by a Self S-Int |
| 136 | * while running). This variable helps SPMC to keep a track of such |
| 137 | * mechanism and perform appropriate bookkeeping. |
| 138 | */ |
| 139 | bool implicit_completion_signal; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | /** Encapsulates a vCPU whose lock is held. */ |
| 143 | struct vcpu_locked { |
| 144 | struct vcpu *vcpu; |
| 145 | }; |
| 146 | |
Olivier Deprez | 0b6f10a | 2020-08-05 18:21:33 +0200 | [diff] [blame] | 147 | /** Container for two vcpu_locked structures. */ |
| 148 | struct two_vcpu_locked { |
| 149 | struct vcpu_locked vcpu1; |
| 150 | struct vcpu_locked vcpu2; |
| 151 | }; |
| 152 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 153 | struct vcpu_locked vcpu_lock(struct vcpu *vcpu); |
Olivier Deprez | 0b6f10a | 2020-08-05 18:21:33 +0200 | [diff] [blame] | 154 | 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] | 155 | void vcpu_unlock(struct vcpu_locked *locked); |
| 156 | void vcpu_init(struct vcpu *vcpu, struct vm *vm); |
| 157 | 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] | 158 | ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 159 | bool vcpu_is_off(struct vcpu_locked vcpu); |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 160 | bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked, |
| 161 | ipaddr_t entry, uintreg_t arg); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 162 | |
| 163 | bool vcpu_handle_page_fault(const struct vcpu *current, |
| 164 | struct vcpu_fault_info *f); |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 165 | |
Olivier Deprez | e6f7b9d | 2021-02-01 11:55:48 +0100 | [diff] [blame] | 166 | void vcpu_reset(struct vcpu *vcpu); |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 167 | |
J-Alves | 7ac4905 | 2022-02-08 17:20:53 +0000 | [diff] [blame] | 168 | void vcpu_set_phys_core_idx(struct vcpu *vcpu); |
| 169 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 170 | static inline void vcpu_irq_count_increment(struct vcpu_locked vcpu_locked) |
| 171 | { |
| 172 | vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count++; |
| 173 | } |
| 174 | |
| 175 | static inline void vcpu_irq_count_decrement(struct vcpu_locked vcpu_locked) |
| 176 | { |
| 177 | vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count--; |
| 178 | } |
| 179 | |
| 180 | static inline void vcpu_fiq_count_increment(struct vcpu_locked vcpu_locked) |
| 181 | { |
| 182 | vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count++; |
| 183 | } |
| 184 | |
| 185 | static inline void vcpu_fiq_count_decrement(struct vcpu_locked vcpu_locked) |
| 186 | { |
| 187 | vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count--; |
| 188 | } |
| 189 | |
| 190 | static inline uint32_t vcpu_interrupt_irq_count_get( |
| 191 | struct vcpu_locked vcpu_locked) |
| 192 | { |
| 193 | return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count; |
| 194 | } |
| 195 | |
| 196 | static inline uint32_t vcpu_interrupt_fiq_count_get( |
| 197 | struct vcpu_locked vcpu_locked) |
| 198 | { |
| 199 | return vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count; |
| 200 | } |
| 201 | |
| 202 | static inline uint32_t vcpu_interrupt_count_get(struct vcpu_locked vcpu_locked) |
| 203 | { |
| 204 | return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count + |
| 205 | vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count; |
| 206 | } |