aboutsummaryrefslogtreecommitdiff
path: root/kernel/main.c
blob: 4ed99b5e1ca90d7d0ca17a5ba294593b29841c66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <fbos/init.h>
#include <fbos/printk.h>
#include <fbos/mm.h>
#include <fbos/sched.h>
#include <fbos/dt.h>

unsigned long init_stack[4][THREAD_SIZE / sizeof(unsigned long)];

struct task_struct tasks[4] = {
	[TASK_INIT] = { .stack = init_stack[0], .addr = nullptr, .entry_offset = 0, },
	[TASK_FIZZ] = { .stack = init_stack[1], .addr = nullptr, .entry_offset = 0, },
	[TASK_BUZZ] = { .stack = init_stack[2], .addr = nullptr, .entry_offset = 0, },
	[TASK_FIZZBUZZ] = { .stack = init_stack[3], .addr = nullptr, .entry_offset = 0, },
};

int next_task;

// TODO: this feels really brittle
__kernel void switch_to(int task_id)
{
	const char *ddr = (const char *)tasks[task_id].addr + tasks[task_id].entry_offset;

	asm volatile("csrc sstatus, %[mask]" : : [mask] "r"(1 << 8));
	asm volatile("mv ra, %0" : : "r"(ddr));
	asm volatile("csrw sepc, ra");
	asm volatile("sret");
}

/*
 * This is the main entry point of the kernel after head.S is done. This
 * function can (and will) assume that everything has been reset and that we can
 * start the whole thing.
 */
__noreturn __kernel void start_kernel(void *dtb)
{
	printk("Welcome to FizzBuzz OS!\n");

	// Extract information from the DTB blob.
	struct initrd_addr addr = find_dt_initrd_addr(dtb);
	extract_initrd((unsigned char *)addr.start, addr.end - addr.start);

	next_task = TASK_UNKNOWN;

	// At this point everything has already been handled: setup the interrupt
	// vector and enable the timer to start ticking and scheduling the three
	// tasks at hand.
	seconds_elapsed = 0;
	setup_interrupts();

	idle();
}

__noreturn __kernel void idle(void)
{
	for (;;) {
		if (next_task != TASK_UNKNOWN && next_task != TASK_INIT) {
			switch_to(next_task);
		}
		asm volatile("wfi");
	}
}