os::paging::page<0> bootstrap_pages_for_memory[256]; // 256 pages = 1MiB
+void map_biggest_physical_page(os::paging::PML4T& paging_table, void const* vaddr, amy::ptr& phys_addr, amy::ptr phys_addr_end, os::paging::page_info page_info) {
+ if (phys_addr % (1024 * 1024 * 1024) == 0 && phys_addr + 1024 * 1024 * 1024 < phys_addr_end) {
+ os::paging::map_page<2, 3>(paging_table, (os::paging::page<2>*)vaddr, os::phys_ptr<os::paging::page<2>>(phys_addr), page_info);
+ phys_addr += 1024 * 1024 * 1024;
+ } else if (phys_addr % (1024 * 1024 * 2) == 0 && phys_addr + 1024 * 1024 * 2 < phys_addr_end) {
+ os::paging::map_page<1, 3>(paging_table, (os::paging::page<1>*)vaddr, os::phys_ptr<os::paging::page<1>>(phys_addr), page_info);
+ phys_addr += 1024 * 1024 * 2;
+ } else {
+ os::paging::map_page<0, 3>(paging_table, (os::paging::page<0>*)vaddr, os::phys_ptr<os::paging::page<0>>(phys_addr), page_info);
+ phys_addr += 1024 * 4;
+ }
+}
+
extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_start> info) {
if (!os::init_serial_port()) {
while (true) {
assert(amy::ptr(&_kernel_phys_rw_start) % 4096 == 0, "Kernel isn't page aligned !");
assert(amy::ptr(&_kernel_phys_end) % 4096 == 0, "Kernel isn't page aligned !");
for (amy::size i = amy::ptr(&_kernel_phys_start); i < amy::ptr(&_kernel_phys_rw_start); ) {
- if (i % (1024 * 1024 * 1024) == 0 && i + 1024 * 1024 * 1024 < amy::ptr(&_kernel_phys_rw_start)) {
- os::paging::map_page<2, 3>(os::paging::global_PML4T, (os::paging::page<2>*)(0xFFFF'FFFF'8000'0000 + i), os::phys_ptr<os::paging::page<2>>(i),
- { .RW = false, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = false });
- i += 1024 * 1024 * 1024;
- } else if (i % (1024 * 1024 * 2) == 0 && i + 1024 * 1024 * 2 < amy::ptr(&_kernel_phys_rw_start)) {
- os::paging::map_page<1, 3>(os::paging::global_PML4T, (os::paging::page<1>*)(0xFFFF'FFFF'8000'0000 + i), os::phys_ptr<os::paging::page<1>>(i),
- { .RW = false, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = false });
- i += 1024 * 1024 * 2;
- } else {
- os::paging::map_page<0, 3>(os::paging::global_PML4T, (os::paging::page<0>*)(0xFFFF'FFFF'8000'0000 + i), os::phys_ptr<os::paging::page<0>>(i),
- { .RW = false, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = false });
- i += 1024 * 4;
- }
+ map_biggest_physical_page(os::paging::global_PML4T, (void*)(0xFFFF'FFFF'8000'0000 + i), i, amy::ptr(&_kernel_phys_rw_start),
+ { .RW = false, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = false });
}
for (amy::size i = amy::ptr(&_kernel_phys_rw_start); i < amy::ptr(&_kernel_phys_end); ) {
- if (i % (1024 * 1024 * 1024) == 0 && i + 1024 * 1024 * 1024 < amy::ptr(&_kernel_phys_rw_start)) {
- os::paging::map_page<2, 3>(os::paging::global_PML4T, (os::paging::page<2>*)(0xFFFF'FFFF'8000'0000 + i), os::phys_ptr<os::paging::page<2>>(i),
- { .RW = true, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = true });
- i += 1024 * 1024 * 1024;
- } else if (i % (1024 * 1024 * 2) == 0 && i + 1024 * 1024 * 2 < amy::ptr(&_kernel_phys_rw_start)) {
- os::paging::map_page<1, 3>(os::paging::global_PML4T, (os::paging::page<1>*)(0xFFFF'FFFF'8000'0000 + i), os::phys_ptr<os::paging::page<1>>(i),
- { .RW = true, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = true });
- i += 1024 * 1024 * 2;
- } else {
- os::paging::map_page<0, 3>(os::paging::global_PML4T, (os::paging::page<0>*)(0xFFFF'FFFF'8000'0000 + i), os::phys_ptr<os::paging::page<0>>(i),
- { .RW = true, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = true });
- i += 1024 * 4;
- }
+ map_biggest_physical_page(os::paging::global_PML4T, (void*)(0xFFFF'FFFF'8000'0000 + i), i, amy::ptr(&_kernel_phys_end),
+ { .RW = true, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = true });
}
// Initialise physical memory map.
for (auto it = multiboot2::next(info); it->type != multiboot2::info::type_t::end; it = multiboot2::next(it)) {
assert(s % 0x1000 == 0, "Memory map unaligned on page boundaries.");
assert(e % 0x1000 == 0, "Memory map unaligned on page boundaries.");
for (amy::ptr j = s; j < e; ) {
- if (j % (1024 * 1024 * 1024) == 0 && j + 1024 * 1024 * 1024 < e) {
- os::paging::map_page<2, 3>(
- os::paging::global_PML4T,
- (os::paging::page<2>*)(0xFFFF'8000'0000'0000 + j),
- os::phys_ptr<os::paging::page<2>>(j),
- { .RW = true, .US = false, .PWT = false, .PCD = false, .A = false, .D = false,
- .PAT = false, .G = true, .AVL = 0, .NX = true });
- j += 1024 * 1024 * 1024;
- } else if (j % (1024 * 1024 * 2) == 0 && j + 1024 * 1024 * 2 < e) {
- os::paging::map_page<1, 3>(os::paging::global_PML4T,
- (os::paging::page<1>*)(0xFFFF'8000'0000'0000 + j),
- os::phys_ptr<os::paging::page<1>>(j),
- { .RW = true, .US = false, .PWT = false, .PCD = false, .A = false, .D = false,
- .PAT = false, .G = true, .AVL = 0, .NX = true });
- j += 1024 * 1024 * 2;
- } else {
- os::paging::map_page<0, 3>(os::paging::global_PML4T,
- (os::paging::page<0>*)(0xFFFF'8000'0000'0000 + j),
- os::phys_ptr<os::paging::page<0>>(j),
- { .RW = true, .US = false, .PWT = false, .PCD = false, .A = false, .D = false,
- .PAT = false, .G = true, .AVL = 0, .NX = true });
- j += 1024 * 4;
- }
+ map_biggest_physical_page(os::paging::global_PML4T,
+ (void*)(0xFFFF'8000'0000'0000 + j), j, e,
+ { .RW = true, .US = false, .PWT = false, .PCD = false, .A = false, .D = false,
+ .PAT = false, .G = true, .AVL = 0, .NX = true });
}
}
os::print("RAM END\n");