From d960909d1bc496d7dd4b4998b0810356c2ea91b3 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 9 Feb 2023 21:38:06 +0000 Subject: [PATCH] x86_64: pmap: enable NX protection during bootstrap --- arch/x86_64/include/arch/paging.h | 1 + arch/x86_64/pmap.c | 2 ++ arch/x86_64/pmap_ctrl.S | 11 +++++++++++ 3 files changed, 14 insertions(+) diff --git a/arch/x86_64/include/arch/paging.h b/arch/x86_64/include/arch/paging.h index 7335e31..d3fd4e0 100644 --- a/arch/x86_64/include/arch/paging.h +++ b/arch/x86_64/include/arch/paging.h @@ -50,5 +50,6 @@ typedef enum page_size { /* returns 1 if gigabyte pages are supported by the CPU, 0 otherwise. defined in pmap_ctrl.S */ extern int gigabyte_pages(void); +extern int enable_nx(void); #endif diff --git a/arch/x86_64/pmap.c b/arch/x86_64/pmap.c index 6f93c48..f1636ea 100644 --- a/arch/x86_64/pmap.c +++ b/arch/x86_64/pmap.c @@ -226,6 +226,8 @@ void pmap_bootstrap(void) { can_use_gbpages = gigabyte_pages(); printk("pmap: gigabyte pages %sabled", can_use_gbpages == 1 ? "en" : "dis"); + enable_nx(); + printk("pmap: NX protection enabled"); page_size_t hugepage = PS_2M; if (can_use_gbpages) { diff --git a/arch/x86_64/pmap_ctrl.S b/arch/x86_64/pmap_ctrl.S index 28c9b67..a8416f2 100644 --- a/arch/x86_64/pmap_ctrl.S +++ b/arch/x86_64/pmap_ctrl.S @@ -29,3 +29,14 @@ gigabyte_pages: 3: pop %rbx pop %rbp ret + + .global enable_nx + .type enable_nx, @function + +enable_nx: + mov $0xC0000080, %ecx + rdmsr + or $0x800, %eax + wrmsr + + ret