blob: d5bd29b913a4e890aed69df35009c5925caf5315 [file] [log] [blame]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01001#ifndef _CPU_H
2#define _CPU_H
3
4#include <stdbool.h>
5#include <stddef.h>
6#include <stdint.h>
7
8#include "arch_cpu.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01009#include "spinlock.h"
10
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010011enum vcpu_state {
12 vcpu_state_off,
13 vcpu_state_ready,
14 vcpu_state_running,
15 vcpu_state_blocked_rpc,
16 vcpu_state_blocked_interrupt,
17};
18
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010019struct vcpu {
Wedson Almeida Filho87009642018-07-02 10:20:07 +010020 struct spinlock lock;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010021 enum vcpu_state state;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010022 struct vm *vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010023 struct vcpu *rpc_next;
24 struct arch_regs regs;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010025};
26
27/* TODO: Update alignment such that cpus are in different cache lines. */
28struct cpu {
Andrew Scull020ae692018-07-19 16:20:14 +010029 /* CPU identifier. Doesn't have to be contiguous. */
30 size_t id;
31
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010032 struct vcpu *current;
33
Andrew Scull020ae692018-07-19 16:20:14 +010034 /* Pointer to bottom of the stack. */
35 void *stack_bottom;
36
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010037 /*
38 * Enabling/disabling irqs are counted per-cpu. They are enabled when
39 * the count is zero, and disabled when it's non-zero.
40 */
41 uint32_t irq_disable_count;
42
Andrew Scull020ae692018-07-19 16:20:14 +010043 struct spinlock lock;
44
Wedson Almeida Filho87009642018-07-02 10:20:07 +010045 /* Determines whether or not the cpu is currently on. */
46 bool is_on;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010047};
48
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010049void cpu_module_init(void);
50
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010051void cpu_init(struct cpu *c);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010052size_t cpu_index(struct cpu *c);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010053void cpu_irq_enable(struct cpu *c);
54void cpu_irq_disable(struct cpu *c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010055bool cpu_on(struct cpu *c, size_t entry, size_t arg);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010056void cpu_off(struct cpu *c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010057struct cpu *cpu_find(size_t id);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010058
Wedson Almeida Filho87009642018-07-02 10:20:07 +010059void vcpu_init(struct vcpu *vcpu, struct vm *vm);
Andrew Scull020ae692018-07-19 16:20:14 +010060void vcpu_on(struct vcpu *vcpu);
61void vcpu_off(struct vcpu *vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010062
Andrew Scull4f170f52018-07-19 12:58:20 +010063#endif /* _CPU_H */