blob: cab40b82f22b23c4c9aeee454ec36410de0972cd [file] [log] [blame]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01001#include "cpu.h"
2
3#include "arch_cpu.h"
4#include "dlog.h"
5#include "std.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01006#include "vm.h"
7
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01008/* The stack to be used by the CPUs. */
9alignas(2 * sizeof(size_t)) static char callstacks[STACK_SIZE * MAX_CPUS];
10
11/* State of all supported CPUs. The stack of the first one is initialized. */
12struct cpu cpus[MAX_CPUS] = {
13 {
14 .is_on = 1,
15 .stack_bottom = callstacks + STACK_SIZE,
16 },
17};
18
19void cpu_module_init(void)
20{
21 size_t i;
22
23 /* Initialize all CPUs. */
24 for (i = 0; i < MAX_CPUS; i++) {
25 struct cpu *c = cpus + i;
26 cpu_init(c);
27 c->id = i; /* TODO: Initialize ID. */
28 c->stack_bottom = callstacks + STACK_SIZE * (i + 1);
29 }
30}
31
32size_t cpu_index(struct cpu *c)
33{
34 return cpus - c;
35}
36
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010037void cpu_init(struct cpu *c)
38{
39 /* TODO: Assumes that c is zeroed out already. */
40 sl_init(&c->lock);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010041 c->irq_disable_count = 1;
42}
43
44void cpu_irq_enable(struct cpu *c)
45{
46 c->irq_disable_count--;
Andrew Scull7364a8e2018-07-19 15:39:29 +010047 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010048 arch_irq_enable();
Andrew Scull7364a8e2018-07-19 15:39:29 +010049 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010050}
51
52void cpu_irq_disable(struct cpu *c)
53{
Andrew Scull7364a8e2018-07-19 15:39:29 +010054 if (!c->irq_disable_count) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010055 arch_irq_disable();
Andrew Scull7364a8e2018-07-19 15:39:29 +010056 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010057 c->irq_disable_count++;
58}
59
Wedson Almeida Filho87009642018-07-02 10:20:07 +010060/**
61 * Turns CPU on and returns the previous state.
62 */
63bool cpu_on(struct cpu *c)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010064{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010065 bool prev;
66
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010067 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010068 prev = c->is_on;
69 c->is_on = true;
70 sl_unlock(&c->lock);
71
72 if (!prev) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010073 /* The CPU is currently off, we need to turn it on. */
74 arch_cpu_on(c->id, c);
75 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010076
77 return prev;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010078}
79
80/*
81 * This must be called only from the same CPU.
82 */
83void cpu_off(struct cpu *c)
84{
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010085 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010086 c->is_on = false;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010087 sl_unlock(&c->lock);
88
Wedson Almeida Filho87009642018-07-02 10:20:07 +010089 arch_cpu_off();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010090}
91
Wedson Almeida Filho87009642018-07-02 10:20:07 +010092void vcpu_init(struct vcpu *vcpu, struct vm *vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010093{
94 memset(vcpu, 0, sizeof(*vcpu));
Wedson Almeida Filho87009642018-07-02 10:20:07 +010095 sl_init(&vcpu->lock);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010096 vcpu->vm = vm;
97 /* TODO: Initialize vmid register. */
98}
Wedson Almeida Filho87009642018-07-02 10:20:07 +010099
100void vcpu_on(struct vcpu *vcpu)
101{
102 sl_lock(&vcpu->lock);
103 vcpu->is_on = true;
104 sl_unlock(&vcpu->lock);
105}
106
107void vcpu_off(struct vcpu *vcpu)
108{
109 sl_lock(&vcpu->lock);
110 vcpu->is_on = false;
111 sl_unlock(&vcpu->lock);
112}