blob: 10bb01f7f9b90f183005cfb39639f5d2344e9ef0 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
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 Scullfbc938a2018-08-20 14:09:28 +010017#pragma once
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
19#include <stdbool.h>
20#include <stddef.h>
21#include <stdint.h>
22
Andrew Scull18c78fc2018-08-20 12:57:41 +010023#include "hf/arch/cpu.h"
24
25#include "hf/addr.h"
26#include "hf/spinlock.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010027
Andrew Walbran318f5732018-11-20 16:23:42 +000028#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 Filho2f94ec12018-07-26 16:00:48 +010033enum vcpu_state {
Andrew Walbranc3910f72018-11-27 14:24:36 +000034 /** The vcpu is switched off. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010035 vcpu_state_off,
Andrew Scullaa039b32018-10-04 15:02:26 +010036
Andrew Walbranc3910f72018-11-27 14:24:36 +000037 /** The vcpu is ready to be run. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010038 vcpu_state_ready,
Andrew Scullaa039b32018-10-04 15:02:26 +010039
Andrew Walbranc3910f72018-11-27 14:24:36 +000040 /** The vcpu is currently running. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010041 vcpu_state_running,
Andrew Scullaa039b32018-10-04 15:02:26 +010042
Andrew Walbranc3910f72018-11-27 14:24:36 +000043 /** The vcpu is waiting for a message. */
Andrew Scullaa039b32018-10-04 15:02:26 +010044 vcpu_state_blocked_mailbox,
45
Andrew Walbranc3910f72018-11-27 14:24:36 +000046 /** The vcpu is waiting for an interrupt. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010047 vcpu_state_blocked_interrupt,
48};
49
Andrew Walbran318f5732018-11-20 16:23:42 +000050struct 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];
Andrew Walbran3d84a262018-12-13 14:41:19 +000055 /**
56 * The number of interrupts which are currently both enabled and
57 * pending. i.e. the number of bits set in interrupt_enable &
58 * interrupt_pending.
59 */
60 uint32_t enabled_and_pending_count;
Andrew Walbran318f5732018-11-20 16:23:42 +000061};
62
Wedson Almeida Filho03306112018-11-26 00:08:03 +000063struct retval_state {
64 uintptr_t value;
65 bool force;
66};
67
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010068struct vcpu {
Wedson Almeida Filho87009642018-07-02 10:20:07 +010069 struct spinlock lock;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010070 enum vcpu_state state;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010071 struct cpu *cpu;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010072 struct vm *vm;
Andrew Scullaa039b32018-10-04 15:02:26 +010073 struct vcpu *mailbox_next;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010074 struct arch_regs regs;
Andrew Walbran318f5732018-11-20 16:23:42 +000075 struct interrupts interrupts;
Wedson Almeida Filho03306112018-11-26 00:08:03 +000076
77 /*
78 * The following field is used to force a return value to be set the
79 * next time a vCPU belonging to a secondary VM runs. For primary VMs,
80 * 'regs' can be set directly.
81 */
82 struct retval_state retval;
83
84 /*
85 * Determine whether the 'regs' field is available for use. This is set
86 * to false when a vCPU is about to run on a physical CPU, and is set
87 * back to true when it is descheduled.
88 */
89 bool regs_available;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010090};
91
92/* TODO: Update alignment such that cpus are in different cache lines. */
93struct cpu {
Andrew Walbranc3910f72018-11-27 14:24:36 +000094 /** CPU identifier. Doesn't have to be contiguous. */
Andrew Scull020ae692018-07-19 16:20:14 +010095 size_t id;
96
Andrew Walbranc3910f72018-11-27 14:24:36 +000097 /** Pointer to bottom of the stack. */
Andrew Scull020ae692018-07-19 16:20:14 +010098 void *stack_bottom;
99
Andrew Walbranc3910f72018-11-27 14:24:36 +0000100 /**
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100101 * Enabling/disabling irqs are counted per-cpu. They are enabled when
102 * the count is zero, and disabled when it's non-zero.
103 */
104 uint32_t irq_disable_count;
105
Andrew Walbran0d7a0682018-12-06 16:48:47 +0000106 /** See api.c for the partial ordering on locks. */
Andrew Scull020ae692018-07-19 16:20:14 +0100107 struct spinlock lock;
108
Andrew Walbranc3910f72018-11-27 14:24:36 +0000109 /** Determines whether or not the cpu is currently on. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100110 bool is_on;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100111};
112
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100113void cpu_module_init(void);
114
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100115void cpu_init(struct cpu *c);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100116size_t cpu_index(struct cpu *c);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100117void cpu_irq_enable(struct cpu *c);
118void cpu_irq_disable(struct cpu *c);
Andrew Scull37402872018-10-24 14:23:06 +0100119bool cpu_on(struct cpu *c, ipaddr_t entry, uintreg_t arg);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100120void cpu_off(struct cpu *c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100121struct cpu *cpu_find(size_t id);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100122
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100123void vcpu_init(struct vcpu *vcpu, struct vm *vm);
Andrew Scull020ae692018-07-19 16:20:14 +0100124void vcpu_on(struct vcpu *vcpu);
125void vcpu_off(struct vcpu *vcpu);
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000126size_t vcpu_index(struct vcpu *vcpu);