blob: d598296d8bd3ece59aaad652b42b2c469635aeec [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 {
Andrew Scullaa039b32018-10-04 15:02:26 +010013 /* The vcpu is switched off. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010014 vcpu_state_off,
Andrew Scullaa039b32018-10-04 15:02:26 +010015
16 /* The vcpu is ready to be run. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010017 vcpu_state_ready,
Andrew Scullaa039b32018-10-04 15:02:26 +010018
19 /* The vcpu is currently running. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010020 vcpu_state_running,
Andrew Scullaa039b32018-10-04 15:02:26 +010021
22 /* The vcpu is waiting for a message. */
23 vcpu_state_blocked_mailbox,
24
25 /* The vcpu is waiting for an interrupt. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010026 vcpu_state_blocked_interrupt,
27};
28
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010029struct vcpu {
Wedson Almeida Filho87009642018-07-02 10:20:07 +010030 struct spinlock lock;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010031 enum vcpu_state state;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010032 struct vm *vm;
Andrew Scullaa039b32018-10-04 15:02:26 +010033 struct vcpu *mailbox_next;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010034 struct arch_regs regs;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010035};
36
37/* TODO: Update alignment such that cpus are in different cache lines. */
38struct cpu {
Andrew Scull020ae692018-07-19 16:20:14 +010039 /* CPU identifier. Doesn't have to be contiguous. */
40 size_t id;
41
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010042 struct vcpu *current;
43
Andrew Scull020ae692018-07-19 16:20:14 +010044 /* Pointer to bottom of the stack. */
45 void *stack_bottom;
46
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010047 /*
48 * Enabling/disabling irqs are counted per-cpu. They are enabled when
49 * the count is zero, and disabled when it's non-zero.
50 */
51 uint32_t irq_disable_count;
52
Andrew Scull020ae692018-07-19 16:20:14 +010053 struct spinlock lock;
54
Wedson Almeida Filho87009642018-07-02 10:20:07 +010055 /* Determines whether or not the cpu is currently on. */
56 bool is_on;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010057};
58
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010059void cpu_module_init(void);
60
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010061void cpu_init(struct cpu *c);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010062size_t cpu_index(struct cpu *c);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010063void cpu_irq_enable(struct cpu *c);
64void cpu_irq_disable(struct cpu *c);
Andrew Scull1b8d0442018-08-06 15:47:04 +010065bool cpu_on(struct cpu *c, ipaddr_t entry, size_t arg);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010066void cpu_off(struct cpu *c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010067struct cpu *cpu_find(size_t id);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010068
Wedson Almeida Filho87009642018-07-02 10:20:07 +010069void vcpu_init(struct vcpu *vcpu, struct vm *vm);
Andrew Scull020ae692018-07-19 16:20:14 +010070void vcpu_on(struct vcpu *vcpu);
71void vcpu_off(struct vcpu *vcpu);