Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include "hf/addr.h" |
| 20 | #include "hf/spinlock.h" |
| 21 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 22 | #include "vmapi/hf/ffa.h" |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 23 | |
| 24 | /** The number of bits in each element of the interrupt bitfields. */ |
| 25 | #define INTERRUPT_REGISTER_BITS 32 |
| 26 | |
| 27 | enum vcpu_state { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 28 | /** The vCPU is switched off. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 29 | VCPU_STATE_OFF, |
| 30 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 31 | /** The vCPU is ready to be run. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 32 | VCPU_STATE_READY, |
| 33 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 34 | /** The vCPU is currently running. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 35 | VCPU_STATE_RUNNING, |
| 36 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 37 | /** The vCPU is waiting for a message. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 38 | VCPU_STATE_BLOCKED_MAILBOX, |
| 39 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 40 | /** The vCPU is waiting for an interrupt. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 41 | VCPU_STATE_BLOCKED_INTERRUPT, |
| 42 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 43 | /** The vCPU has aborted. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 44 | VCPU_STATE_ABORTED, |
| 45 | }; |
| 46 | |
| 47 | struct interrupts { |
| 48 | /** Bitfield keeping track of which interrupts are enabled. */ |
| 49 | uint32_t interrupt_enabled[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS]; |
| 50 | /** Bitfield keeping track of which interrupts are pending. */ |
| 51 | uint32_t interrupt_pending[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS]; |
| 52 | /** |
| 53 | * The number of interrupts which are currently both enabled and |
| 54 | * pending. i.e. the number of bits set in interrupt_enable & |
| 55 | * interrupt_pending. |
| 56 | */ |
| 57 | uint32_t enabled_and_pending_count; |
| 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 | |
| 77 | struct cpu *cpu; |
| 78 | struct vm *vm; |
| 79 | struct arch_regs regs; |
| 80 | struct interrupts interrupts; |
| 81 | |
| 82 | /* |
| 83 | * Determine whether the 'regs' field is available for use. This is set |
| 84 | * to false when a vCPU is about to run on a physical CPU, and is set |
| 85 | * back to true when it is descheduled. |
| 86 | */ |
| 87 | bool regs_available; |
| 88 | }; |
| 89 | |
| 90 | /** Encapsulates a vCPU whose lock is held. */ |
| 91 | struct vcpu_locked { |
| 92 | struct vcpu *vcpu; |
| 93 | }; |
| 94 | |
| 95 | struct vcpu_locked vcpu_lock(struct vcpu *vcpu); |
| 96 | void vcpu_unlock(struct vcpu_locked *locked); |
| 97 | void vcpu_init(struct vcpu *vcpu, struct vm *vm); |
| 98 | 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^] | 99 | ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 100 | bool vcpu_is_off(struct vcpu_locked vcpu); |
| 101 | bool vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry, |
| 102 | uintreg_t arg); |
| 103 | |
| 104 | bool vcpu_handle_page_fault(const struct vcpu *current, |
| 105 | struct vcpu_fault_info *f); |