~sircmpwn/helios-devel

init_mmu: remove G bit on PML4E and PDPE v1 APPLIED

Simon Zeni: 2
 init_mmu: remove G bit on PML4E and PDPE
 move call to init_mmu function after call to ExitBootServices

 2 files changed, 8 insertions(+), 8 deletions(-)
Thanks!

To git@git.sr.ht:~sircmpwn/hboot
   ee03f72..1d59a9a  master -> master
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/helios-devel/patches/42699/mbox | git am -3
Learn more about email & git

[PATCH 1/2] init_mmu: remove G bit on PML4E and PDPE Export this patch

The bit is ignored in the PDPE, but must be set to 0 on the PML4E otherwise a
general-protection fault is produced.
---
 src/mmu.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/mmu.c b/src/mmu.c
index 829f6c2..6811143 100644
--- a/src/mmu.c
+++ b/src/mmu.c
@@ -10,9 +10,9 @@
#define IDENT_START	0xFFFFFF8000000000
#define IDENT_SIZE	IDENT_PDS * 1024 * 1024 * 1024

__attribute__((aligned(4096))) static uintptr_t pml4[512];
__attribute__((aligned(4096))) static uintptr_t pdpt[512];
__attribute__((aligned(4096))) static uintptr_t pd_ident[IDENT_PDS][512];
__attribute__((aligned(4096))) static uintptr_t pml4[512] = {0};
__attribute__((aligned(4096))) static uintptr_t pdpt[512] = {0};
__attribute__((aligned(4096))) static uintptr_t pd_ident[IDENT_PDS][512] = {0};

void
init_mmu()
@@ -21,9 +21,9 @@ init_mmu()

	// Identity map first 64GiB of physical memory at -512GiB through
	// -448GiB for general use.
	pml4[511] = (uintptr_t)&pdpt | PDE_P | PDE_W | PDE_G;
	pml4[511] = (uintptr_t)&pdpt | PDE_P | PDE_W;
	for (size_t i = 0; i < IDENT_PDS; i++) {
		pdpt[i] = (uintptr_t)&pd_ident[i] | PDE_P | PDE_W | PDE_G;
		pdpt[i] = (uintptr_t)&pd_ident[i] | PDE_P | PDE_W;
	}
	for (size_t i = 0; i < IDENT_PDS; i++)
	for (size_t j = 0; j < 512; j++) {
@@ -35,7 +35,7 @@ init_mmu()
	pdpt[511] = (uintptr_t)&pd_ident[0] | PDE_P | PDE_W;

	// EFI environment identity maps the lower half, preserve that here.
	pml4[0] = (uintptr_t)&pdpt | PDE_P | PDE_W | PDE_G;
	pml4[0] = (uintptr_t)&pdpt | PDE_P | PDE_W;

	__asm__ volatile("movq %0, %%cr3" : : "r"(&pml4) : "memory");
}
-- 
2.41.0

[PATCH 2/2] move call to init_mmu function after call to ExitBootServices Export this patch

For a reason that is yet to be found, calling the `init_mmu` function
before ExitBootServices causes a GP fault on kernel entry on Intel.

Moving the function call past ExitBootServices fixes the issue on Intel,
and doesn't change anything on AMD.
---
 src/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/main.c b/src/main.c
index 164df5d..3e132bc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -55,11 +55,11 @@ efi_main(efi_handle image, efi_system_table *systab)

	close(kernel);

	init_mmu();

	bprintfln("Exit boot services");
	systab->BootServices->ExitBootServices(image, map_key);

	init_mmu();

	entry();

	return EFI_LOAD_ERROR; // Unreachable
-- 
2.41.0
Thanks!

To git@git.sr.ht:~sircmpwn/hboot
   ee03f72..1d59a9a  master -> master