From 15c2207ab911d85483b357694a776c17b7487cba Mon Sep 17 00:00:00 2001 From: Max Wash Date: Wed, 1 Apr 2026 18:06:50 +0100 Subject: [PATCH] x86_64: pmap: enable kernel-mode write-protection --- arch/x86_64/include/arch/paging.h | 3 ++- arch/x86_64/pmap.c | 2 ++ arch/x86_64/pmap_ctrl.S | 14 +++++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/arch/x86_64/include/arch/paging.h b/arch/x86_64/include/arch/paging.h index 9c6a770..8732fb7 100644 --- a/arch/x86_64/include/arch/paging.h +++ b/arch/x86_64/include/arch/paging.h @@ -1,8 +1,8 @@ #ifndef ARCH_PAGING_H_ #define ARCH_PAGING_H_ -#include #include +#include #ifdef __cplusplus extern "C" { @@ -55,6 +55,7 @@ enum page_size { defined in pmap_ctrl.S */ extern int gigabyte_pages(void); extern int enable_nx(void); +extern int enable_wp(void); #ifdef __cplusplus } diff --git a/arch/x86_64/pmap.c b/arch/x86_64/pmap.c index 15184e5..d1fbe02 100644 --- a/arch/x86_64/pmap.c +++ b/arch/x86_64/pmap.c @@ -351,6 +351,8 @@ void pmap_bootstrap(void) can_use_gbpages == 1 ? "en" : "dis"); enable_nx(); printk("pmap: NX protection enabled"); + enable_wp(); + printk("pmap: kernel-mode write protection enabled"); enum page_size hugepage = PS_2M; if (can_use_gbpages) { diff --git a/arch/x86_64/pmap_ctrl.S b/arch/x86_64/pmap_ctrl.S index a8416f2..77f1b0e 100644 --- a/arch/x86_64/pmap_ctrl.S +++ b/arch/x86_64/pmap_ctrl.S @@ -24,12 +24,13 @@ gigabyte_pages: 1: mov $0x1, %rax jmp 3f -2: mov $0x0, %rax +2: mov $0x0, %rax 3: pop %rbx pop %rbp ret + .global enable_nx .type enable_nx, @function @@ -40,3 +41,14 @@ enable_nx: wrmsr ret + + + .global enable_wp + .type enable_wp, @function + +enable_wp: + mov %cr0, %rax + or $0x10000, %rax + mov %rax, %cr0 + + ret