blob: b263e39bc4037cc5fb83771ca1ac38571a4ac503 [file] [log] [blame]
Andrew Scullfbc938a2018-08-20 14:09:28 +01001#pragma once
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01002
3#include <stdbool.h>
4#include <stddef.h>
5#include <stdint.h>
6
Andrew Scull18c78fc2018-08-20 12:57:41 +01007#include "hf/arch/cpu.h"
8
9#include "hf/addr.h"
10#include "hf/spinlock.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010011
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010012enum vcpu_state {
13 vcpu_state_off,
14 vcpu_state_ready,
15 vcpu_state_running,
16 vcpu_state_blocked_rpc,
17 vcpu_state_blocked_interrupt,
18};
19
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010020struct vcpu {
Wedson Almeida Filho87009642018-07-02 10:20:07 +010021 struct spinlock lock;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010022 enum vcpu_state state;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010023 struct vm *vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010024 struct vcpu *rpc_next;
25 struct arch_regs regs;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010026};
27
28/* TODO: Update alignment such that cpus are in different cache lines. */
29struct cpu {
Andrew Scull020ae692018-07-19 16:20:14 +010030 /* CPU identifier. Doesn't have to be contiguous. */
31 size_t id;
32
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010033 struct vcpu *current;
34
Andrew Scull020ae692018-07-19 16:20:14 +010035 /* Pointer to bottom of the stack. */
36 void *stack_bottom;
37
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010038 /*
39 * Enabling/disabling irqs are counted per-cpu. They are enabled when
40 * the count is zero, and disabled when it's non-zero.
41 */
42 uint32_t irq_disable_count;
43
Andrew Scull020ae692018-07-19 16:20:14 +010044 struct spinlock lock;
45
Wedson Almeida Filho87009642018-07-02 10:20:07 +010046 /* Determines whether or not the cpu is currently on. */
47 bool is_on;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010048};
49
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010050void cpu_module_init(void);
51
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010052void cpu_init(struct cpu *c);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010053size_t cpu_index(struct cpu *c);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010054void cpu_irq_enable(struct cpu *c);
55void cpu_irq_disable(struct cpu *c);
Andrew Scull1b8d0442018-08-06 15:47:04 +010056bool cpu_on(struct cpu *c, ipaddr_t entry, size_t arg);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010057void cpu_off(struct cpu *c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010058struct cpu *cpu_find(size_t id);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010059
Wedson Almeida Filho87009642018-07-02 10:20:07 +010060void vcpu_init(struct vcpu *vcpu, struct vm *vm);
Andrew Scull020ae692018-07-19 16:20:14 +010061void vcpu_on(struct vcpu *vcpu);
62void vcpu_off(struct vcpu *vcpu);