]> git.ameliathe1st.gay Git - voyage-au-centre-des-fichiers.git/commitdiff
Fixed elf page-alignment necessity. Now only requires no page overlap
authorAmelia Coutard <eliottulio.coutard@gmail.com>
Wed, 1 Mar 2023 13:57:54 +0000 (14:57 +0100)
committerAmelia Coutard <eliottulio.coutard@gmail.com>
Wed, 1 Mar 2023 13:57:54 +0000 (14:57 +0100)
kernel/src/kernel.cpp

index c5d8591c3a653c67626342b93bf6eec4891f4961..8c1569cbe9baaf9127619dcd8711f021a2dc693c 100644 (file)
@@ -200,13 +200,12 @@ extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_s
                        continue;
                }
                os::print("Segment: loadable\n");
-               os::assert((std::uintptr_t(program_header.p_vaddr) & 0xFFF) == 0, "Program segment not 4KiB aligned.");
                os::assert(0x1000 <= std::uint64_t(program_header.p_vaddr)
                                && std::uint64_t(program_header.p_vaddr + program_header.p_memsz) < 0x10'0000'0000,
                        "Program segments must be contained between 0x1000 and 0x10'0000'0000 (i.e. 64GiB).");
 
                // Allocate memory for segment:
-               std::size_t nb_pages = (program_header.p_memsz + 0x1000) / 0x1000;
+               std::size_t nb_pages = (std::uint64_t(program_header.p_vaddr) % 0x1000 + program_header.p_memsz + 0x1000 - 1) / 0x1000;
                for (std::size_t i = 0; i < nb_pages; i++) {
                        const auto indices = os::paging::calc_page_table_indices(program_header.p_vaddr + i * 0x1000);
                        if (PML4T.contents[indices.pml4e].P == 0) {
@@ -227,11 +226,10 @@ extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_s
                                set_base_address(PDT.contents[indices.pde], os::phys_ptr<os::paging::PT>(PT_alloc.ptr.get_phys_addr()));
                        }
                        os::paging::PT& PT = *get_base_address(PDT.contents[indices.pde]);
-                       if (PT.contents[indices.pe].P == 0) {
-                               PT.contents[indices.pe] = {.P = 1, .R_W = (program_header.flags & 2) >> 1, .U_S = 1, .PWT = 0, .PCD = 0, .PAT = 0, .G = 0, .base_address = 0, .NX = 0};
-                               const auto page_alloc = os::paging::page_allocator.allocate(1);
-                               set_page_base_address(PT.contents[indices.pe], os::phys_ptr<os::paging::page>(page_alloc.ptr.get_phys_addr()));
-                       }
+                       os::assert(PT.contents[indices.pe].P == 0, "Process segments' memory overlaps the same pages.");
+                       PT.contents[indices.pe] = {.P = 1, .R_W = (program_header.flags & 2) >> 1, .U_S = 1, .PWT = 0, .PCD = 0, .PAT = 0, .G = 0, .base_address = 0, .NX = 0};
+                       const auto page_alloc = os::paging::page_allocator.allocate(1);
+                       set_page_base_address(PT.contents[indices.pe], os::phys_ptr<os::paging::page>(page_alloc.ptr.get_phys_addr()));
                }
 
                // Initialise memory for segment:
@@ -267,11 +265,10 @@ extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_s
                        set_base_address(PDT.contents[indices.pde], os::phys_ptr<os::paging::PT>(PT_alloc.ptr.get_phys_addr()));
                }
                os::paging::PT& PT = *get_base_address(PDT.contents[indices.pde]);
-               if (PT.contents[indices.pe].P == 0) {
-                       PT.contents[indices.pe] = {.P = 1, .R_W = 1, .U_S = 1, .PWT = 0, .PCD = 0, .PAT = 0, .G = 0, .base_address = 0, .NX = 0};
-                       const auto page_alloc = os::paging::page_allocator.allocate(1);
-                       set_page_base_address(PT.contents[indices.pe], os::phys_ptr<os::paging::page>(page_alloc.ptr.get_phys_addr()));
-               }
+               os::assert(PT.contents[indices.pe].P == 0, "Process segments' memory overlaps the same pages.");
+               PT.contents[indices.pe] = {.P = 1, .R_W = 1, .U_S = 1, .PWT = 0, .PCD = 0, .PAT = 0, .G = 0, .base_address = 0, .NX = 0};
+               const auto page_alloc = os::paging::page_allocator.allocate(1);
+               set_page_base_address(PT.contents[indices.pe], os::phys_ptr<os::paging::page>(page_alloc.ptr.get_phys_addr()));
        }
 
        // Initialise stack to 0.