]> git.ameliathe1st.gay Git - voyage-au-centre-des-fichiers.git/commitdiff
Déduplication du code pour mettre les plus grandes pages physiques possibles dans...
authorAmelia Coutard <eliottulio.coutard@gmail.com>
Sat, 30 Dec 2023 02:10:31 +0000 (03:10 +0100)
committerAmelia Coutard <eliottulio.coutard@gmail.com>
Sat, 30 Dec 2023 02:10:31 +0000 (03:10 +0100)
kernel/src/kernel.cpp

index 76dcd4a3b2a54bf28b93db36fea0f66334f22929..10547cd5e4ed021afe971cd5f11e926a3ed41137 100644 (file)
@@ -32,6 +32,19 @@ char _kernel_phys_end;
 
 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) {
@@ -51,34 +64,12 @@ extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_s
        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)) {
@@ -98,29 +89,10 @@ extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_s
                                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");