aboutsummaryrefslogtreecommitdiff
path: root/kernel/main.c
blob: 5a11816ba7d15069a24963b2568668af8f7f5238 (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
62
63
64
#include <fbos/init.h>
#include <fbos/printk.h>
#include <fbos/mm.h>
#include <fbos/sched.h>
#include <fbos/string.h>
#include <fbos/dt.h>

// Stack to be used by our processes. "Blasphemy!" I hear you say. "How dare you
// use the same stack for kernel and user space?" It's not like this is some
// sort of utopian system in which everyone shares everything, but since this
// stupidly simple kernel does not even bother to implement paging nor any other
// memory protection of any kind, it's not like separating stacks for each
// process and kernel space would make much of a difference. Hence, let's keep
// it simple and have the same stack everwhere.
uint64_t stack[STACK_SIZE / sizeof(uint64_t)];

// Initialize the list of structs by providing a fixed stack address and empty
// values everywhere else.
struct task_struct tasks[4] = {
	[TASK_INIT] = { .stack = stack, .name = "init", .entry_addr = nullptr, },
	[TASK_FIZZ] = { .stack = stack, .name = "fizz", .entry_addr = nullptr, },
	[TASK_BUZZ] = { .stack = stack, .name = "buzz", .entry_addr = nullptr, },
	[TASK_FIZZBUZZ] = { .stack = stack, .name = "fizzbuzz", .entry_addr = nullptr, },
};

// Defined in fbos/init.h.
struct dt_info info = {
	.model = { '\0' },
	.cpu_freq = 0,
	.initrd_start = 0,
	.initrd_end = 0,
};

/*
 * 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.
	get_dt_info(dtb, &info);
	extract_initrd((unsigned char *)info.initrd_start, info.initrd_end - info.initrd_start, tasks);

	// If we were able to fetch the model, print it now.
	if (info.model[0] != '\0') {
		printk("Running on: ");
		write(info.model, strlen(info.model));
		write("\n", 1);
	}

	// 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();

	// Loop indefinitely while preserving power.
	for (;;) {
		asm volatile("wfi");
	}
}