From: Amelia Coutard <eliottulio.coutard@gmail.com>
Date: Mon, 27 Feb 2023 16:55:49 +0000 (+0100)
Subject: Fixed bug in page counting for elf file loading. Added support for writeable segments... 
X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=d292e864388b2e001830f8afd034f330962f3398;p=voyage-au-centre-des-fichiers.git

Fixed bug in page counting for elf file loading. Added support for writeable segments of elf files (RO is still RO)
---

diff --git a/kernel/src/kernel.cpp b/kernel/src/kernel.cpp
index a2cbd58..3933a16 100644
--- a/kernel/src/kernel.cpp
+++ b/kernel/src/kernel.cpp
@@ -194,30 +194,30 @@ extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_s
 		os::assert((std::uintptr_t(program_header.p_vaddr) & 0xFFF) == 0, "Program segment not 4KiB aligned.");
 
 		// Allocate memory for segment:
-		std::size_t nb_pages = (program_header.p_memsz + 1023) / 1024;
+		std::size_t nb_pages = (program_header.p_memsz + 0x1000) / 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);
 			os::assert(indices.pml4e < 256, "Userspace program must be in the lower-half of virtual memory.");
 			if (PML4T.contents[indices.pml4e].P == 0) {
-				PML4T.contents[indices.pml4e] = {.P = 1, .R_W = 0, .U_S = 1, .PWT = 0, .PCD = 0, .base_address = 0, .NX = 0};
+				PML4T.contents[indices.pml4e] = {.P = 1, .R_W = 1, .U_S = 1, .PWT = 0, .PCD = 0, .base_address = 0, .NX = 0};
 				const auto PDPT_alloc = os::paging::page_allocator.allocate(1);
 				set_base_address(PML4T.contents[indices.pml4e], os::phys_ptr<os::paging::PDPT>(PDPT_alloc.ptr.get_phys_addr()));
 			}
 			os::paging::PDPT& PDPT = *get_base_address(PML4T.contents[indices.pml4e]);
 			if (PDPT.contents[indices.pdpe].non_page.P == 0) {
-				PDPT.contents[indices.pdpe] = {.non_page = {.P = 1, .R_W = 0, .U_S = 1, .PWT = 0, .PCD = 0, .base_address = 0, .NX = 0}};
+				PDPT.contents[indices.pdpe] = {.non_page = {.P = 1, .R_W = 1, .U_S = 1, .PWT = 0, .PCD = 0, .base_address = 0, .NX = 0}};
 				const auto PDT_alloc = os::paging::page_allocator.allocate(1);
 				set_base_address(PDPT.contents[indices.pdpe], os::phys_ptr<os::paging::PDT>(PDT_alloc.ptr.get_phys_addr()));
 			}
 			os::paging::PDT& PDT = *get_base_address(PDPT.contents[indices.pdpe]);
 			if (PDT.contents[indices.pde].non_page.P == 0) {
-				PDT.contents[indices.pde] = {.non_page = {.P = 1, .R_W = 0, .U_S = 1, .PWT = 0, .PCD = 0, .base_address = 0, .NX = 0}};
+				PDT.contents[indices.pde] = {.non_page = {.P = 1, .R_W = 1, .U_S = 1, .PWT = 0, .PCD = 0, .base_address = 0, .NX = 0}};
 				const auto PT_alloc = os::paging::page_allocator.allocate(1);
 				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 = 0, .U_S = 1, .PWT = 0, .PCD = 0, .PAT = 0, .G = 0, .base_address = 0, .NX = 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()));
 			}