Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 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. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +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. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +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. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +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 | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 44 | vcpu_state_blocked_mailbox, |
| 45 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 46 | /** The vcpu is waiting for an interrupt. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 47 | vcpu_state_blocked_interrupt, |
| 48 | }; |
| 49 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 50 | struct interrupts { |
| 51 | /** Bitfield keeping track of which interrupts are enabled. */ |
| 52 | uint32_t interrupt_enabled[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS]; |
| 53 | /** Bitfield keeping track of which interrupts are pending. */ |
| 54 | uint32_t interrupt_pending[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS]; |
| 55 | }; |
| 56 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 57 | struct vcpu { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 58 | struct spinlock lock; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 59 | enum vcpu_state state; |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 60 | struct cpu *cpu; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 61 | struct vm *vm; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 62 | struct vcpu *mailbox_next; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 63 | struct arch_regs regs; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 64 | struct interrupts interrupts; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /* TODO: Update alignment such that cpus are in different cache lines. */ |
| 68 | struct cpu { |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 69 | /** CPU identifier. Doesn't have to be contiguous. */ |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame] | 70 | size_t id; |
| 71 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 72 | /** Pointer to bottom of the stack. */ |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame] | 73 | void *stack_bottom; |
| 74 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 75 | /** |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 76 | * Enabling/disabling irqs are counted per-cpu. They are enabled when |
| 77 | * the count is zero, and disabled when it's non-zero. |
| 78 | */ |
| 79 | uint32_t irq_disable_count; |
| 80 | |
Andrew Walbran | 0d7a068 | 2018-12-06 16:48:47 +0000 | [diff] [blame^] | 81 | /** See api.c for the partial ordering on locks. */ |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame] | 82 | struct spinlock lock; |
| 83 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 84 | /** Determines whether or not the cpu is currently on. */ |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 85 | bool is_on; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 88 | void cpu_module_init(void); |
| 89 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 90 | void cpu_init(struct cpu *c); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 91 | size_t cpu_index(struct cpu *c); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 92 | void cpu_irq_enable(struct cpu *c); |
| 93 | void cpu_irq_disable(struct cpu *c); |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 94 | 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] | 95 | void cpu_off(struct cpu *c); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 96 | struct cpu *cpu_find(size_t id); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 97 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 98 | void vcpu_init(struct vcpu *vcpu, struct vm *vm); |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame] | 99 | void vcpu_on(struct vcpu *vcpu); |
| 100 | void vcpu_off(struct vcpu *vcpu); |
Wedson Almeida Filho | 9d5040f | 2018-10-29 08:41:27 +0000 | [diff] [blame] | 101 | size_t vcpu_index(struct vcpu *vcpu); |