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 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 28 | enum vcpu_state { |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 29 | /* The vcpu is switched off. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 30 | vcpu_state_off, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 31 | |
| 32 | /* The vcpu is ready to be run. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 33 | vcpu_state_ready, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 34 | |
| 35 | /* The vcpu is currently running. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 36 | vcpu_state_running, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 37 | |
| 38 | /* The vcpu is waiting for a message. */ |
| 39 | vcpu_state_blocked_mailbox, |
| 40 | |
| 41 | /* The vcpu is waiting for an interrupt. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 42 | vcpu_state_blocked_interrupt, |
| 43 | }; |
| 44 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 45 | struct vcpu { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 46 | struct spinlock lock; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 47 | enum vcpu_state state; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 48 | struct vm *vm; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 49 | struct vcpu *mailbox_next; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 50 | struct arch_regs regs; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /* TODO: Update alignment such that cpus are in different cache lines. */ |
| 54 | struct cpu { |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame] | 55 | /* CPU identifier. Doesn't have to be contiguous. */ |
| 56 | size_t id; |
| 57 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 58 | struct vcpu *current; |
| 59 | |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame] | 60 | /* Pointer to bottom of the stack. */ |
| 61 | void *stack_bottom; |
| 62 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 63 | /* |
| 64 | * Enabling/disabling irqs are counted per-cpu. They are enabled when |
| 65 | * the count is zero, and disabled when it's non-zero. |
| 66 | */ |
| 67 | uint32_t irq_disable_count; |
| 68 | |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame] | 69 | struct spinlock lock; |
| 70 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 71 | /* Determines whether or not the cpu is currently on. */ |
| 72 | bool is_on; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 75 | void cpu_module_init(void); |
| 76 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 77 | void cpu_init(struct cpu *c); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 78 | size_t cpu_index(struct cpu *c); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 79 | void cpu_irq_enable(struct cpu *c); |
| 80 | void cpu_irq_disable(struct cpu *c); |
Andrew Scull | 1b8d044 | 2018-08-06 15:47:04 +0100 | [diff] [blame] | 81 | bool cpu_on(struct cpu *c, ipaddr_t entry, size_t arg); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 82 | void cpu_off(struct cpu *c); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 83 | struct cpu *cpu_find(size_t id); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 84 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 85 | void vcpu_init(struct vcpu *vcpu, struct vm *vm); |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame] | 86 | void vcpu_on(struct vcpu *vcpu); |
| 87 | void vcpu_off(struct vcpu *vcpu); |