summaryrefslogtreecommitdiff
path: root/stage3/main.c
blob: b4ce692648c186646969f59832f2cdd7825a12c3 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "def.h"
#include "halt.h"
#include "heap.h"
#include "font.h"
#include "interrupts.h"
#include "pic.h"
#include "io.h"
#include "ata.h"
#include "pci.h"
#include "ps2.h"
#include "thread.h"
#include "shell.h"
#include "fs.h"
#include "clock.h"
#include "rng.h"
#include "debug.h"
#include "bootinfo.h"

#define T mem_region
#include "vec.h"

char keymap[256] = { '\0' };

void keyboard_handler()
{
	str buffer = NILS;
	usize cap = 0;
	print(S("$ "));
	bool shift = false;

	for (;;) {
		event *e = yield(nil);
		u8 code = e->data.scancode;
		kfree(e);

		bool stop = (code & (1 << 7)) != 0;
		code &= ~(1 << 7);

		char c = keymap[code*2+shift];
		if (c == '\xe') {
			shift = !stop;
		} else if (stop) {
		} else if (c == '\b') {
			if (buffer.len > 0) {
				print_char(c);
				buffer.len--;
			}
		} else if (c == '\n') {
			print_char(c);
			shell_run_cmd(buffer);
			buffer.len = 0;
			print(S("$ "));
		} else if (c != '\0') {
			print_char(c);
			if (buffer.len == cap)
				buffer.data = krealloc(buffer.data, cap = cap ? cap*2 : 1);
			buffer.data[buffer.len++] = c;
		}
	}
}

struct bootinfo *bootinfo;

void kmain(struct bootinfo *info)
{
	bootinfo = info;

	// PML3
	for (u64 page = 0; page < 512*512; page++)
		((u64 *) 0x200000)[page] = (page << 30) | 0b10000011; // bit 7 is for huge pages

	// PML4
	for (u64 tbl = 0; tbl < 512; tbl++)
		((u64 *) 0x1000)[tbl] = (0x200000 + tbl * 0x1000) | 0b11;

	// heap init and fill
	heap_init();
	ITER(bootinfo->mmap) {
		mem_region *r = &bootinfo->mmap.data[i];
		if (r->type != MEM_USABLE || r->size == 0)
			continue;

		// remove anything between 0x100000 and 0x400000. it is used for kernel and page tables
		usize start = (usize) r->start;
		usize size = r->size;

		if (start < 0x100000)
			continue;

		if (start >= 0x100000 && start < 0x400000) {
			if (start + size <= 0x400000)
				continue; // skip

			size = start + size - 0x400000;
			start = 0x400000;
		}

		heap_add((void *) start, size);
	}

	// font init
	font_init();
	font_set_size(1);
	font_load_builtin();
	font_clear_screen();

	print(S("welcome to cuddles\n"));

	// memory map
	print(S("memory map:\n"));
	ITER(bootinfo->mmap) {
		mem_region *r = &bootinfo->mmap.data[i];
		print_num_pad((u64) r->start, 16, 16, ' ');
		print(S(" | "));
		print_num_pad((u64) r->start + r->size, 16, 16, ' ');
		print(S(" | "));
		switch (r->type) {
			case MEM_USABLE: print(S("usable")); break;
			case MEM_RESERVED: print(S("reserved")); break;
			case MEM_ACPI_RECLAIMABLE: print(S("acpi reclaimable")); break;
			case MEM_ACPI_NVS: print(S("acpi nvs")); break;
			case MEM_BAD: print(S("bad")); break;
			default: print_dec(r->type); break;
		}
		print(S("\n"));
	}

	print(S("gfx framebuffer at "));
	print_hex((u64) bootinfo->gfx_framebuffer);
	print(S("-"));
	print_hex((u64) bootinfo->gfx_framebuffer + bootinfo->gfx_pitch * bootinfo->gfx_height);
	print(S("\n"));

	u32 vendor[4];
	asm volatile("cpuid":"=a"(vendor[0]),"=b"(vendor[1]),
		"=c"(vendor[3]),"=d"(vendor[2]):"a"(0));

	print(S("cpu vendor: "));
	print((str) { 12, (void *) &vendor[1] });
	print(S("\n"));

	u32 features;
	asm volatile("cpuid":"=d"(features):"a"(1):"ebx","ecx");

	print(S("cpu features: "));
	print_num_pad(features, 2, 32, '0');
	print(S("\n"));

	srand(clock_cycles());

	interrupts_init();
	pic_init();
	thread_init();
	ata_init();
	ps2_init();
	debug_init();
	pci_init();

	shell_run_cmd(S("run init"));

	thread *keyboard_thread = thread_create(S("keyboard"), &keyboard_handler);
	irq_services[1] = keyboard_thread;

	unmask_irq(1);
	unmask_irq(2);
	clock_init();
	enable_irqs();

	thread_sched(nil, nil);
	freeze();
}