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