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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include <hilbert/kernel/storage/bd/memory.hpp>
#include <hilbert/kernel/storage/fs/tarfs.hpp>
#include <hilbert/kernel/application.hpp>
#include <hilbert/kernel/framebuffer.hpp>
#include <hilbert/kernel/load-app.hpp>
#include <hilbert/kernel/paging.hpp>
#include <hilbert/kernel/serial.hpp>
#include <hilbert/kernel/input.hpp>
#include <hilbert/kernel/panic.hpp>
#include <hilbert/kernel/vfile.hpp>
#include <limine.h>
using namespace hilbert::kernel;
LIMINE_BASE_REVISION(1)
static volatile limine_memmap_request memmap_request {
.id = LIMINE_MEMMAP_REQUEST,
.revision = 0,
.response = 0
};
static volatile limine_kernel_address_request kernel_address_request {
.id = LIMINE_KERNEL_ADDRESS_REQUEST,
.revision = 0,
.response = 0
};
static volatile limine_framebuffer_request framebuffer_request {
.id = LIMINE_FRAMEBUFFER_REQUEST,
.revision = 0,
.response = 0
};
static volatile limine_hhdm_request hhdm_request {
.id = LIMINE_HHDM_REQUEST,
.revision = 0,
.response = 0
};
static volatile limine_module_request module_request = {
.id = LIMINE_MODULE_REQUEST,
.revision = 2,
.response = 0,
.internal_module_count = 0,
.internal_modules = 0
};
bool try_map_module_by_cmdline(
const char *cmdline, void *&vaddr_out, uint64_t &len_out
) {
auto response = module_request.response;
if (!response)
return false;
for (uint64_t i = 0; i < response->module_count; ++i) {
limine_file *file = response->modules[i];
for (uint64_t j = 0; cmdline[j] == file->cmdline[j]; ++j)
if (!cmdline[j]) {
//module start is guaranteed to be page-aligned, end is not.
uint64_t start_paddr =
(uint64_t)file->address - hhdm_request.response->offset;
uint64_t end_paddr =
((start_paddr + file->size - 1) / 4096 + 1) * 4096;
uint64_t start_vaddr =
paging::find_unmapped_vram_region((end_paddr - start_paddr) / 4096);
for (uint64_t i = 0; i < end_paddr - start_paddr; i += 4096)
paging::map_kernel_page(
start_paddr + i, start_vaddr + i, true, false);
vaddr_out = (void *)start_vaddr;
len_out = file->size;
return true;
}
}
return false;
}
//defined in linker script, page-aligned:
extern uint8_t __kernel_start;
extern uint8_t __kernel_end;
extern uint8_t __kernel_rx_start;
extern uint8_t __kernel_rx_end;
extern uint8_t __kernel_ro_start;
extern uint8_t __kernel_ro_end;
extern uint8_t __kernel_rw_start;
extern uint8_t __kernel_rw_end;
uint8_t *initfs;
uint64_t initfs_len;
[[noreturn]] static void with_kernel_p4();
extern "C" void load_gdt_and_idt();
static bool have_initfs;
extern "C" [[noreturn]] void entry() {
serial_init();
//TODO?: maybe we should check if the limine requests were
// fulfilled and display some error message if not
//set up the physical memory usage bitmap:
paging::mark_all_pram_used();
auto memmap = memmap_request.response;
for (uint64_t i = 0; i < memmap->entry_count; ++i) {
//we don't allocate any physical pages until after we are done using limine
//structures (specifically at the call to paging::map_kernel_stacks), so
//we consider bootloader reclaimable to be free. usable and bootloader
//reclaimable are guaranteed by limine spec to be page-aligned.
auto entry = memmap->entries[i];
if (entry->type == LIMINE_MEMMAP_USABLE ||
entry->type == LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE)
paging::mark_pram_region_free(entry->base, entry->base + entry->length);
}
//set up page mappings:
auto kernel_address = kernel_address_request.response;
uint64_t kernel_offset = kernel_address->virtual_base
- kernel_address->physical_base;
uint64_t hhdm = hhdm_request.response->offset;
//framebuffer might not be page-aligned
auto framebuffer = framebuffer_request.response->framebuffers[0];
uint64_t fb_start = ((uint64_t)framebuffer->address / 4096) * 4096 - hhdm;
uint64_t fb_end = (uint64_t)framebuffer->address
+ framebuffer->pitch * framebuffer->height - hhdm;
fb_end = ((fb_end - 1) / 4096 + 1) * 4096;
paging::init_kernel_page_tables(kernel_offset);
//kernel image rx
for (uint64_t vaddr = (uint64_t)&__kernel_rx_start;
vaddr < (uint64_t)&__kernel_rx_end; vaddr += 4096)
paging::map_kernel_page(vaddr - kernel_offset, vaddr, false, true);
//kernel image ro
for (uint64_t vaddr = (uint64_t)&__kernel_ro_start;
vaddr < (uint64_t)&__kernel_ro_end; vaddr += 4096)
paging::map_kernel_page(vaddr - kernel_offset, vaddr, false, false);
//kernel image rw
for (uint64_t vaddr = (uint64_t)&__kernel_rw_start;
vaddr < (uint64_t)&__kernel_rw_end; vaddr += 4096)
paging::map_kernel_page(vaddr - kernel_offset, vaddr, true, false);
//framebuffer
uint64_t fb_vaddr =
paging::find_unmapped_vram_region((fb_end - fb_start) / 4096);
for (uint64_t i = 0; i < fb_end - fb_start; i += 4096)
paging::map_kernel_page(fb_start + i, fb_vaddr + i, true, false);
have_initfs =
try_map_module_by_cmdline("initfs", (void *&)initfs, initfs_len);
//set up framebuffer and terminal:
//TODO: assumes framebuffer is 32-bpp rgb
framebuffer::init_framebuffer(fb_start, fb_vaddr,
framebuffer->width, framebuffer->height, framebuffer->pitch);
//switch to kernel p4
paging::map_kernel_stacks();
load_gdt_and_idt();
switch_to_kernel_p4(&with_kernel_p4);
}
[[noreturn]] static void with_kernel_p4() {
if (!have_initfs)
panic(0x5f8860);
input::init_input();
application::init_applications();
auto *initfs_bd = new storage::bd::memory(initfs, initfs_len);
auto *initfs_fs = new storage::fs::tarfs_instance(initfs_bd);
initfs_bd->mounted_as = initfs_fs;
vfile::vfile initfs_root;
initfs_root.bd = initfs_bd;
initfs_root.dir_entry.type = storage::file_type::directory;
initfs_root.path.absolute = true;
if (initfs_fs->get_root_node(initfs_root.dir_entry.node) !=
storage::fs_result::success)
panic(0x48a6ed);
vfile::set_root(initfs_root);
utility::string init_path_string("/bin/init", 9);
vfile::canon_path init_path;
vfile::canonize_path(init_path_string, init_path);
vfile::vfile init_file;
if (vfile::look_up_path(init_path, init_file, true) !=
storage::fs_result::success)
panic(0x7e874d);
app_memory *init_memory = new app_memory();
uint64_t init_entry_point;
load_app_result load_init_result =
load_app(init_file, *init_memory, init_entry_point);
if (load_init_result != load_app_result::success)
panic(0xc39db3);
application::process *init_process = new application::process(init_memory);
init_process->add_environment_variable(
utility::string("ARGC", 4), utility::string("1", 1));
init_process->add_environment_variable(
utility::string("ARGV0", 5), utility::string("/bin/init", 9));
application::add_process(init_process);
application::thread *init_thread =
new application::thread(init_process, init_entry_point);
init_process->add_thread(init_thread);
application::paused_threads->insert(init_thread);
application::resume_next_thread();
}
|