blob: e949db72d72d509834277df3b9f4a41724a89af3 [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 Scull1b8d0442018-08-06 15:47:04 +01008#include "addr.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01009#include "arch_cpu.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010010#include "spinlock.h"
11
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);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010063
Andrew Scull4f170f52018-07-19 12:58:20 +010064#endif /* _CPU_H */