Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 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 | |
Andrew Scull | fbc938a | 2018-08-20 14:09:28 +0100 | [diff] [blame] | 17 | #pragma once |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
| 19 | #include <stdbool.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 23 | #include "hf/arch/cpu.h" |
| 24 | |
| 25 | #include "hf/addr.h" |
| 26 | #include "hf/spinlock.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 27 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 28 | #include "vmapi/hf/types.h" |
| 29 | |
| 30 | /** The number of bits in each element of the interrupt bitfields. */ |
| 31 | #define INTERRUPT_REGISTER_BITS 32 |
| 32 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 33 | enum vcpu_state { |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 34 | /** The vcpu is switched off. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 35 | VCPU_STATE_OFF, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 36 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 37 | /** The vcpu is ready to be run. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 38 | VCPU_STATE_READY, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 39 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 40 | /** The vcpu is currently running. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 41 | VCPU_STATE_RUNNING, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 42 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 43 | /** The vcpu is waiting for a message. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 44 | VCPU_STATE_BLOCKED_MAILBOX, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 45 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 46 | /** The vcpu is waiting for an interrupt. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 47 | VCPU_STATE_BLOCKED_INTERRUPT, |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 48 | |
| 49 | /** The vcpu has aborted. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 50 | VCPU_STATE_ABORTED, |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 53 | struct interrupts { |
| 54 | /** Bitfield keeping track of which interrupts are enabled. */ |
| 55 | uint32_t interrupt_enabled[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS]; |
| 56 | /** Bitfield keeping track of which interrupts are pending. */ |
| 57 | uint32_t interrupt_pending[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS]; |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 58 | /** |
| 59 | * The number of interrupts which are currently both enabled and |
| 60 | * pending. i.e. the number of bits set in interrupt_enable & |
| 61 | * interrupt_pending. |
| 62 | */ |
| 63 | uint32_t enabled_and_pending_count; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 66 | struct vcpu_fault_info { |
| 67 | ipaddr_t ipaddr; |
| 68 | vaddr_t vaddr; |
| 69 | vaddr_t pc; |
| 70 | int mode; |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 73 | struct vcpu { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 74 | struct spinlock lock; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 75 | |
| 76 | /* |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 77 | * The state is only changed in the context of the vCPU being run. This |
| 78 | * ensures the scheduler can easily keep track of the vCPU state as |
| 79 | * transitions are indicated by the return code from the run call. |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 80 | */ |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 81 | enum vcpu_state state; |
| 82 | |
| 83 | struct cpu *cpu; |
| 84 | struct vm *vm; |
| 85 | struct arch_regs regs; |
| 86 | struct interrupts interrupts; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * Determine whether the 'regs' field is available for use. This is set |
| 90 | * to false when a vCPU is about to run on a physical CPU, and is set |
| 91 | * back to true when it is descheduled. |
| 92 | */ |
| 93 | bool regs_available; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 96 | /** Encapsulates a vCPU whose lock is held. */ |
| 97 | struct vcpu_locked { |
| 98 | struct vcpu *vcpu; |
| 99 | }; |
| 100 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 101 | /* TODO: Update alignment such that cpus are in different cache lines. */ |
| 102 | struct cpu { |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 103 | /** CPU identifier. Doesn't have to be contiguous. */ |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame^] | 104 | cpu_id_t id; |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame] | 105 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 106 | /** Pointer to bottom of the stack. */ |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame] | 107 | void *stack_bottom; |
| 108 | |
Andrew Walbran | 0d7a068 | 2018-12-06 16:48:47 +0000 | [diff] [blame] | 109 | /** See api.c for the partial ordering on locks. */ |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame] | 110 | struct spinlock lock; |
| 111 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 112 | /** Determines whether or not the cpu is currently on. */ |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 113 | bool is_on; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame^] | 116 | void cpu_module_init(const cpu_id_t *cpu_ids, size_t count); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 117 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 118 | size_t cpu_index(struct cpu *c); |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 119 | bool cpu_on(struct cpu *c, ipaddr_t entry, uintreg_t arg); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 120 | void cpu_off(struct cpu *c); |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame^] | 121 | struct cpu *cpu_find(cpu_id_t id); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 122 | |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 123 | struct vcpu_locked vcpu_lock(struct vcpu *vcpu); |
| 124 | void vcpu_unlock(struct vcpu_locked *locked); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 125 | void vcpu_init(struct vcpu *vcpu, struct vm *vm); |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 126 | void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg); |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 127 | spci_vcpu_index_t vcpu_index(const struct vcpu *vcpu); |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 128 | bool vcpu_is_off(struct vcpu_locked vcpu); |
| 129 | bool vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry, |
Andrew Walbran | 9a43fee | 2019-04-18 17:42:32 +0100 | [diff] [blame] | 130 | uintreg_t arg); |
Wedson Almeida Filho | 99d2d4c | 2019-02-14 12:53:46 +0000 | [diff] [blame] | 131 | |
| 132 | bool vcpu_handle_page_fault(const struct vcpu *current, |
| 133 | struct vcpu_fault_info *f); |