blob: af01151a39d2cc0bf7b1926c2e6297444f44b24e [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
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010028enum vcpu_state {
Andrew Scullaa039b32018-10-04 15:02:26 +010029 /* The vcpu is switched off. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010030 vcpu_state_off,
Andrew Scullaa039b32018-10-04 15:02:26 +010031
32 /* The vcpu is ready to be run. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010033 vcpu_state_ready,
Andrew Scullaa039b32018-10-04 15:02:26 +010034
35 /* The vcpu is currently running. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010036 vcpu_state_running,
Andrew Scullaa039b32018-10-04 15:02:26 +010037
38 /* The vcpu is waiting for a message. */
39 vcpu_state_blocked_mailbox,
40
41 /* The vcpu is waiting for an interrupt. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010042 vcpu_state_blocked_interrupt,
43};
44
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010045struct vcpu {
Wedson Almeida Filho87009642018-07-02 10:20:07 +010046 struct spinlock lock;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010047 enum vcpu_state state;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010048 struct cpu *cpu;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010049 struct vm *vm;
Andrew Scullaa039b32018-10-04 15:02:26 +010050 struct vcpu *mailbox_next;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010051 struct arch_regs regs;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010052};
53
54/* TODO: Update alignment such that cpus are in different cache lines. */
55struct cpu {
Andrew Scull020ae692018-07-19 16:20:14 +010056 /* CPU identifier. Doesn't have to be contiguous. */
57 size_t id;
58
59 /* Pointer to bottom of the stack. */
60 void *stack_bottom;
61
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010062 /*
63 * Enabling/disabling irqs are counted per-cpu. They are enabled when
64 * the count is zero, and disabled when it's non-zero.
65 */
66 uint32_t irq_disable_count;
67
Andrew Scull020ae692018-07-19 16:20:14 +010068 struct spinlock lock;
69
Wedson Almeida Filho87009642018-07-02 10:20:07 +010070 /* Determines whether or not the cpu is currently on. */
71 bool is_on;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010072};
73
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010074void cpu_module_init(void);
75
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010076void cpu_init(struct cpu *c);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010077size_t cpu_index(struct cpu *c);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010078void cpu_irq_enable(struct cpu *c);
79void cpu_irq_disable(struct cpu *c);
Andrew Scull37402872018-10-24 14:23:06 +010080bool cpu_on(struct cpu *c, ipaddr_t entry, uintreg_t arg);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010081void cpu_off(struct cpu *c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010082struct cpu *cpu_find(size_t id);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010083
Wedson Almeida Filho87009642018-07-02 10:20:07 +010084void vcpu_init(struct vcpu *vcpu, struct vm *vm);
Andrew Scull020ae692018-07-19 16:20:14 +010085void vcpu_on(struct vcpu *vcpu);
86void vcpu_off(struct vcpu *vcpu);