Kernel: Re-enable protection of the kernel image in memory

This commit is contained in:
Andreas Kling 2020-01-17 22:07:20 +01:00
parent 59b584d983
commit f71fc88393
Notes: sideshowbarker 2024-07-19 10:00:22 +09:00
2 changed files with 23 additions and 19 deletions

View File

@ -45,25 +45,6 @@ void MemoryManager::initialize_paging()
dbgprintf("MM: Kernel page directory @ %p\n", kernel_page_directory().cr3());
#endif
#if 0
// Disable writing to the kernel text and rodata segments.
extern u32 start_of_kernel_text;
extern u32 start_of_kernel_data;
for (size_t i = (u32)&start_of_kernel_text; i < (u32)&start_of_kernel_data; i += PAGE_SIZE) {
auto& pte = ensure_pte(kernel_page_directory(), VirtualAddress(i));
pte.set_writable(false);
}
if (g_cpu_supports_nx) {
// Disable execution of the kernel data and bss segments.
extern u32 end_of_kernel_bss;
for (size_t i = (u32)&start_of_kernel_data; i < (u32)&end_of_kernel_bss; i += PAGE_SIZE) {
auto& pte = ensure_pte(kernel_page_directory(), VirtualAddress(i));
pte.set_execute_disabled(true);
}
}
#endif
m_quickmap_addr = VirtualAddress(0xffe00000);
#ifdef MM_DEBUG
dbgprintf("MM: Quickmap will use %p\n", m_quickmap_addr.get());
@ -136,11 +117,33 @@ void MemoryManager::initialize_paging()
setup_low_1mb();
protect_kernel_image();
#ifdef MM_DEBUG
dbgprintf("MM: Paging initialized.\n");
#endif
}
void MemoryManager::protect_kernel_image()
{
// Disable writing to the kernel text and rodata segments.
extern u32 start_of_kernel_text;
extern u32 start_of_kernel_data;
for (size_t i = (u32)&start_of_kernel_text; i < (u32)&start_of_kernel_data; i += PAGE_SIZE) {
auto& pte = ensure_pte(kernel_page_directory(), VirtualAddress(i));
pte.set_writable(false);
}
if (g_cpu_supports_nx) {
// Disable execution of the kernel data and bss segments.
extern u32 end_of_kernel_bss;
for (size_t i = (u32)&start_of_kernel_data; i < (u32)&end_of_kernel_bss; i += PAGE_SIZE) {
auto& pte = ensure_pte(kernel_page_directory(), VirtualAddress(i));
pte.set_execute_disabled(true);
}
}
}
void MemoryManager::setup_low_1mb()
{
m_low_page_table = allocate_supervisor_physical_page();

View File

@ -123,6 +123,7 @@ private:
void detect_cpu_features();
void initialize_paging();
void setup_low_1mb();
void protect_kernel_image();
void parse_memory_map();
void flush_entire_tlb();
void flush_tlb(VirtualAddress);