blob: a7796793969f51e2c9c27d55c3bce592e22f4912 [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
Andrew Scull18c78fc2018-08-20 12:57:41 +01008#include "hf/arch/cpu.h"
9
10#include "hf/addr.h"
11#include "hf/spinlock.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010012
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010013enum vcpu_state {
14 vcpu_state_off,
15 vcpu_state_ready,
16 vcpu_state_running,
17 vcpu_state_blocked_rpc,
18 vcpu_state_blocked_interrupt,
19};
20
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010021struct vcpu {
Wedson Almeida Filho87009642018-07-02 10:20:07 +010022 struct spinlock lock;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010023 enum vcpu_state state;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010024 struct vm *vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010025 struct vcpu *rpc_next;
26 struct arch_regs regs;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010027};
28
29/* TODO: Update alignment such that cpus are in different cache lines. */
30struct cpu {
Andrew Scull020ae692018-07-19 16:20:14 +010031 /* CPU identifier. Doesn't have to be contiguous. */
32 size_t id;
33
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010034 struct vcpu *current;
35
Andrew Scull020ae692018-07-19 16:20:14 +010036 /* Pointer to bottom of the stack. */
37 void *stack_bottom;
38
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010039 /*
40 * Enabling/disabling irqs are counted per-cpu. They are enabled when
41 * the count is zero, and disabled when it's non-zero.
42 */
43 uint32_t irq_disable_count;
44
Andrew Scull020ae692018-07-19 16:20:14 +010045 struct spinlock lock;
46
Wedson Almeida Filho87009642018-07-02 10:20:07 +010047 /* Determines whether or not the cpu is currently on. */
48 bool is_on;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010049};
50
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010051void cpu_module_init(void);
52
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010053void cpu_init(struct cpu *c);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010054size_t cpu_index(struct cpu *c);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010055void cpu_irq_enable(struct cpu *c);
56void cpu_irq_disable(struct cpu *c);
Andrew Scull1b8d0442018-08-06 15:47:04 +010057bool cpu_on(struct cpu *c, ipaddr_t entry, size_t arg);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010058void cpu_off(struct cpu *c);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010059struct cpu *cpu_find(size_t id);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010060
Wedson Almeida Filho87009642018-07-02 10:20:07 +010061void vcpu_init(struct vcpu *vcpu, struct vm *vm);
Andrew Scull020ae692018-07-19 16:20:14 +010062void vcpu_on(struct vcpu *vcpu);
63void vcpu_off(struct vcpu *vcpu);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010064
Andrew Scull4f170f52018-07-19 12:58:20 +010065#endif /* _CPU_H */