Skip to content

Commit

Permalink
Kernel: Detect support for no-execute (NX) CPU features
Browse files Browse the repository at this point in the history
Previously we assumed all hosts would have support for IA32_EFER.NXE.
This is mostly true for newer hardware, but older hardware will crash
and burn if you try to use this feature.

Now we check for support via CPUID.80000001[20].
  • Loading branch information
deoxxa authored and awesomekling committed Dec 26, 2019
1 parent 5be6a43 commit 17aef7d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
35 changes: 25 additions & 10 deletions Kernel/VM/MemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ MemoryManager& MM

MemoryManager::MemoryManager(u32 physical_address_for_kernel_page_tables)
{
CPUID id(0x80000001);
m_has_nx_support = (id.edx() & (1 << 20)) != 0;

m_kernel_page_directory = PageDirectory::create_at_fixed_address(PhysicalAddress(physical_address_for_kernel_page_tables));
for (size_t i = 0; i < 4; ++i) {
m_low_page_tables[i] = (PageTableEntry*)(physical_address_for_kernel_page_tables + PAGE_SIZE * (5 + i));
Expand Down Expand Up @@ -57,12 +60,18 @@ void MemoryManager::initialize_paging()
create_identity_mapping(kernel_page_directory(), VirtualAddress(PAGE_SIZE), (8 * MB) - PAGE_SIZE);

// Disable execution from 0MB through 1MB (BIOS data, legacy things, ...)
for (size_t i = 0; i < (1 * MB); ++i)
ensure_pte(kernel_page_directory(), VirtualAddress(i)).set_execute_disabled(true);
for (size_t i = 0; i < (1 * MB); ++i) {
auto& pte = ensure_pte(kernel_page_directory(), VirtualAddress(i));
if (m_has_nx_support)
pte.set_execute_disabled(true);
}

// Disable execution from 2MB through 8MB (kmalloc, kmalloc_eternal, slabs, page tables, ...)
for (size_t i = 1; i < 4; ++i)
kernel_page_directory().table().directory(0)[i].set_execute_disabled(true);
for (size_t i = 1; i < 4; ++i) {
auto& pte = kernel_page_directory().table().directory(0)[i];
if (m_has_nx_support)
pte.set_execute_disabled(true);
}

// FIXME: We should move everything kernel-related above the 0xc0000000 virtual mark.

Expand Down Expand Up @@ -175,12 +184,18 @@ void MemoryManager::initialize_paging()
"orl $0x20, %eax\n"
"mov %eax, %cr4\n");

// Turn on IA32_EFER.NXE
asm volatile(
"movl $0xc0000080, %ecx\n"
"rdmsr\n"
"orl $0x800, %eax\n"
"wrmsr\n");
if (m_has_nx_support) {
kprintf("MM: NX support detected; enabling NXE flag\n");

// Turn on IA32_EFER.NXE
asm volatile(
"movl $0xc0000080, %ecx\n"
"rdmsr\n"
"orl $0x800, %eax\n"
"wrmsr\n");
} else {
kprintf("MM: NX support not detected\n");
}

asm volatile("movl %%eax, %%cr3" ::"a"(kernel_page_directory().cr3()));
asm volatile(
Expand Down
3 changes: 3 additions & 0 deletions Kernel/VM/MemoryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class MemoryManager {

PageTableEntry& ensure_pte(PageDirectory&, VirtualAddress);

bool has_nx_support() const { return m_has_nx_support; }

RefPtr<PageDirectory> m_kernel_page_directory;
PageTableEntry* m_low_page_tables[4] { nullptr };

Expand All @@ -130,6 +132,7 @@ class MemoryManager {
InlineLinkedList<VMObject> m_vmobjects;

bool m_quickmap_in_use { false };
bool m_has_nx_support { false };
};

struct ProcessPagingScope {
Expand Down
6 changes: 4 additions & 2 deletions Kernel/VM/Region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ void Region::remap_page(size_t index)
pte.set_writable(false);
else
pte.set_writable(is_writable());
pte.set_execute_disabled(!is_executable());
if (MM.has_nx_support())
pte.set_execute_disabled(!is_executable());
pte.set_user_allowed(is_user_accessible());
m_page_directory->flush(page_vaddr);
#ifdef MM_DEBUG
Expand Down Expand Up @@ -265,7 +266,8 @@ void Region::map(PageDirectory& page_directory)
pte.set_writable(false);
else
pte.set_writable(is_writable());
pte.set_execute_disabled(!is_executable());
if (MM.has_nx_support())
pte.set_execute_disabled(!is_executable());
} else {
pte.set_physical_page_base(0);
pte.set_present(false);
Expand Down

0 comments on commit 17aef7d

Please sign in to comment.