blob: 809c21e7940a33246c5f5577171bb415dd5f7a40 [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 Filho987c0ff2018-06-20 16:34:38 +010048 struct vm *vm;
Andrew Scullaa039b32018-10-04 15:02:26 +010049 struct vcpu *mailbox_next;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010050 struct arch_regs regs;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010051};
52
53/* TODO: Update alignment such that cpus are in different cache lines. */
54struct cpu {
Andrew Scull020ae692018-07-19 16:20:14 +010055 /* CPU identifier. Doesn't have to be contiguous. */
56 size_t id;
57
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010058 struct vcpu *current;
59
Andrew Scull020ae692018-07-19 16:20:14 +010060 /* Pointer to bottom of the stack. */
61 void *stack_bottom;
62
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010063 /*
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 Scull020ae692018-07-19 16:20:14 +010069 struct spinlock lock;
70
Wedson Almeida Filho87009642018-07-02 10:20:07 +010071 /* Determines whether or not the cpu is currently on. */
72 bool is_on;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010073};
74
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010075void cpu_module_init(void);
76
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010077void cpu_init(struct cpu *c);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010078size_t cpu_index(struct cpu *c);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010079void cpu_irq_enable(struct cpu *c);
80void cpu_irq_disable(struct cpu *c);
Andrew Scull1b8d0442018-08-06 15:47:04 +010081bool cpu_on(struct cpu *c, ipaddr_t entry, size_t arg);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010082void cpu_off(struct cpu *c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010083struct cpu *cpu_find(size_t id);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010084
Wedson Almeida Filho87009642018-07-02 10:20:07 +010085void vcpu_init(struct vcpu *vcpu, struct vm *vm);
Andrew Scull020ae692018-07-19 16:20:14 +010086void vcpu_on(struct vcpu *vcpu);
87void vcpu_off(struct vcpu *vcpu);