blob: bb84bbb59dcb733b9a492b2efd4562f0a37ef0c3 [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--;
47 if (!c->irq_disable_count)
48 arch_irq_enable();
49}
50
51void cpu_irq_disable(struct cpu *c)
52{
53 if (!c->irq_disable_count)
54 arch_irq_disable();
55 c->irq_disable_count++;
56}
57
Wedson Almeida Filho87009642018-07-02 10:20:07 +010058/**
59 * Turns CPU on and returns the previous state.
60 */
61bool cpu_on(struct cpu *c)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010062{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010063 bool prev;
64
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010065 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010066 prev = c->is_on;
67 c->is_on = true;
68 sl_unlock(&c->lock);
69
70 if (!prev) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010071 /* The CPU is currently off, we need to turn it on. */
72 arch_cpu_on(c->id, c);
73 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010074
75 return prev;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010076}
77
78/*
79 * This must be called only from the same CPU.
80 */
81void cpu_off(struct cpu *c)
82{
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010083 sl_lock(&c->lock);
Wedson Almeida Filho87009642018-07-02 10:20:07 +010084 c->is_on = false;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010085 sl_unlock(&c->lock);
86
Wedson Almeida Filho87009642018-07-02 10:20:07 +010087 arch_cpu_off();
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010088}
89
Wedson Almeida Filho87009642018-07-02 10:20:07 +010090void vcpu_init(struct vcpu *vcpu, struct vm *vm)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010091{
92 memset(vcpu, 0, sizeof(*vcpu));
Wedson Almeida Filho87009642018-07-02 10:20:07 +010093 sl_init(&vcpu->lock);
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010094 vcpu->vm = vm;
95 /* TODO: Initialize vmid register. */
96}
Wedson Almeida Filho87009642018-07-02 10:20:07 +010097
98void vcpu_on(struct vcpu *vcpu)
99{
100 sl_lock(&vcpu->lock);
101 vcpu->is_on = true;
102 sl_unlock(&vcpu->lock);
103}
104
105void vcpu_off(struct vcpu *vcpu)
106{
107 sl_lock(&vcpu->lock);
108 vcpu->is_on = false;
109 sl_unlock(&vcpu->lock);
110}