blob: 397ecff429befadfff2dd06622ce97657fcdf41e [file] [log] [blame]
Fuad Tabba5c738432019-12-02 11:02:42 +00001/*
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 Walbranb5ab43c2020-04-30 11:32:54 +010022#include "vmapi/hf/ffa.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000023
24/** The number of bits in each element of the interrupt bitfields. */
25#define INTERRUPT_REGISTER_BITS 32
26
27enum vcpu_state {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000028 /** The vCPU is switched off. */
Fuad Tabba5c738432019-12-02 11:02:42 +000029 VCPU_STATE_OFF,
30
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000031 /** The vCPU is ready to be run. */
Fuad Tabba5c738432019-12-02 11:02:42 +000032 VCPU_STATE_READY,
33
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000034 /** The vCPU is currently running. */
Fuad Tabba5c738432019-12-02 11:02:42 +000035 VCPU_STATE_RUNNING,
36
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000037 /** The vCPU is waiting for a message. */
Fuad Tabba5c738432019-12-02 11:02:42 +000038 VCPU_STATE_BLOCKED_MAILBOX,
39
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000040 /** The vCPU is waiting for an interrupt. */
Fuad Tabba5c738432019-12-02 11:02:42 +000041 VCPU_STATE_BLOCKED_INTERRUPT,
42
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000043 /** The vCPU has aborted. */
Fuad Tabba5c738432019-12-02 11:02:42 +000044 VCPU_STATE_ABORTED,
45};
46
47struct 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
60struct vcpu_fault_info {
61 ipaddr_t ipaddr;
62 vaddr_t vaddr;
63 vaddr_t pc;
64 uint32_t mode;
65};
66
67struct 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. */
91struct vcpu_locked {
92 struct vcpu *vcpu;
93};
94
95struct vcpu_locked vcpu_lock(struct vcpu *vcpu);
96void vcpu_unlock(struct vcpu_locked *locked);
97void vcpu_init(struct vcpu *vcpu, struct vm *vm);
98void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010099ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +0000100bool vcpu_is_off(struct vcpu_locked vcpu);
101bool vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry,
102 uintreg_t arg);
103
104bool vcpu_handle_page_fault(const struct vcpu *current,
105 struct vcpu_fault_info *f);