summaryrefslogtreecommitdiff
path: root/Kernel
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2020-01-17 23:56:13 +0100
committerAndreas Kling <awesomekling@gmail.com>2020-01-18 00:28:16 +0100
commit3e8b60c6181757d28c619622dbdcc863dda89f30 (patch)
tree9c5b6c6c9f84e70facd9d862bb5537cc0fb5d356 /Kernel
parent6b52f6c61db3b81f44c83be55aeddaccbd580ee8 (diff)
downloadserenity-3e8b60c6181757d28c619622dbdcc863dda89f30.zip
Kernel: Clean up MemoryManager initialization a bit more
Move the CPU feature enabling to functions in Arch/i386/CPU.cpp.
Diffstat (limited to 'Kernel')
-rw-r--r--Kernel/Arch/i386/CPU.cpp77
-rw-r--r--Kernel/Arch/i386/CPU.h7
-rw-r--r--Kernel/VM/MemoryManager.cpp93
-rw-r--r--Kernel/VM/MemoryManager.h1
4 files changed, 91 insertions, 87 deletions
diff --git a/Kernel/Arch/i386/CPU.cpp b/Kernel/Arch/i386/CPU.cpp
index e08a7e6acf..8a7b18223e 100644
--- a/Kernel/Arch/i386/CPU.cpp
+++ b/Kernel/Arch/i386/CPU.cpp
@@ -570,7 +570,6 @@ void detect_cpu_features()
g_cpu_supports_umip = (extended_features.ecx() & (1 << 2));
}
-
void stac()
{
if (!g_cpu_supports_smap)
@@ -586,3 +585,79 @@ void clac()
asm volatile("clac" ::
: "cc");
}
+
+void x86_enable_pae()
+{
+ // Turn on CR4.PAE
+ asm volatile(
+ "mov %cr4, %eax\n"
+ "orl $0x20, %eax\n"
+ "mov %eax, %cr4\n");
+}
+
+void x86_enable_pge()
+{
+ if (g_cpu_supports_pge) {
+ // Turn on CR4.PGE so the CPU will respect the G bit in page tables.
+ asm volatile(
+ "mov %cr4, %eax\n"
+ "orl $0x80, %eax\n"
+ "mov %eax, %cr4\n");
+ kprintf("x86: PGE support enabled\n");
+ } else {
+ kprintf("x86: PGE support not detected\n");
+ }
+}
+
+void x86_enable_smep()
+{
+ if (g_cpu_supports_smep) {
+ // Turn on CR4.SMEP
+ asm volatile(
+ "mov %cr4, %eax\n"
+ "orl $0x100000, %eax\n"
+ "mov %eax, %cr4\n");
+ kprintf("x86: SMEP support enabled\n");
+ } else {
+ kprintf("x86: SMEP support not detected\n");
+ }
+}
+
+void x86_enable_smap()
+{
+ if (g_cpu_supports_smap) {
+ // Turn on CR4.SMAP
+ kprintf("x86: Enabling SMAP\n");
+ asm volatile(
+ "mov %cr4, %eax\n"
+ "orl $0x200000, %eax\n"
+ "mov %eax, %cr4\n");
+ kprintf("x86: SMAP support enabled\n");
+ } else {
+ kprintf("x86: SMAP support not detected\n");
+ }
+}
+
+void x86_enable_nx()
+{
+ if (g_cpu_supports_nx) {
+ // Turn on IA32_EFER.NXE
+ asm volatile(
+ "movl $0xc0000080, %ecx\n"
+ "rdmsr\n"
+ "orl $0x800, %eax\n"
+ "wrmsr\n");
+ kprintf("x86: NX support enabled\n");
+ } else {
+ kprintf("x86: NX support not detected\n");
+ }
+}
+
+void x86_enable_wp()
+{
+ asm volatile(
+ "movl %%cr0, %%eax\n"
+ "orl $0x00010000, %%eax\n"
+ "movl %%eax, %%cr0\n" ::
+ : "%eax", "memory");
+}
diff --git a/Kernel/Arch/i386/CPU.h b/Kernel/Arch/i386/CPU.h
index 583a66d7f9..7a73f2b458 100644
--- a/Kernel/Arch/i386/CPU.h
+++ b/Kernel/Arch/i386/CPU.h
@@ -539,8 +539,13 @@ extern bool g_cpu_supports_tsc;
extern bool g_cpu_supports_umip;
void stac();
-
void clac();
+void x86_enable_pae();
+void x86_enable_pge();
+void x86_enable_smep();
+void x86_enable_smap();
+void x86_enable_nx();
+void x86_enable_wp();
class SmapDisabler {
public:
diff --git a/Kernel/VM/MemoryManager.cpp b/Kernel/VM/MemoryManager.cpp
index 71d0c4a23a..1c64ee6053 100644
--- a/Kernel/VM/MemoryManager.cpp
+++ b/Kernel/VM/MemoryManager.cpp
@@ -25,98 +25,23 @@ MemoryManager::MemoryManager()
{
m_kernel_page_directory = PageDirectory::create_kernel_page_directory();
- initialize_paging();
-
- kprintf("MM initialized.\n");
-}
-
-MemoryManager::~MemoryManager()
-{
-}
-
-void MemoryManager::initialize_paging()
-{
- if (!g_cpu_supports_pae) {
- kprintf("x86: Cannot boot on machines without PAE support.\n");
- hang();
- }
-
-#ifdef MM_DEBUG
- dbgprintf("MM: Kernel page directory @ %p\n", kernel_page_directory().cr3());
-#endif
-
parse_memory_map();
-#ifdef MM_DEBUG
- dbgprintf("MM: Installing page directory\n");
-#endif
-
- // Turn on CR4.PAE
- asm volatile(
- "mov %cr4, %eax\n"
- "orl $0x20, %eax\n"
- "mov %eax, %cr4\n");
-
- if (g_cpu_supports_pge) {
- // Turn on CR4.PGE so the CPU will respect the G bit in page tables.
- asm volatile(
- "mov %cr4, %eax\n"
- "orl $0x80, %eax\n"
- "mov %eax, %cr4\n");
- kprintf("x86: PGE support enabled\n");
- } else {
- kprintf("x86: PGE support not detected\n");
- }
-
- if (g_cpu_supports_smep) {
- // Turn on CR4.SMEP
- asm volatile(
- "mov %cr4, %eax\n"
- "orl $0x100000, %eax\n"
- "mov %eax, %cr4\n");
- kprintf("x86: SMEP support enabled\n");
- } else {
- kprintf("x86: SMEP support not detected\n");
- }
-
- if (g_cpu_supports_smap) {
- // Turn on CR4.SMAP
- kprintf("x86: Enabling SMAP\n");
- asm volatile(
- "mov %cr4, %eax\n"
- "orl $0x200000, %eax\n"
- "mov %eax, %cr4\n");
- kprintf("x86: SMAP support enabled\n");
- } else {
- kprintf("x86: SMAP support not detected\n");
- }
-
- if (g_cpu_supports_nx) {
- // Turn on IA32_EFER.NXE
- asm volatile(
- "movl $0xc0000080, %ecx\n"
- "rdmsr\n"
- "orl $0x800, %eax\n"
- "wrmsr\n");
- kprintf("x86: NX support enabled\n");
- } else {
- kprintf("x86: NX support not detected\n");
- }
+ x86_enable_pae();
+ x86_enable_pge();
+ x86_enable_smep();
+ x86_enable_smap();
+ x86_enable_nx();
+ x86_enable_wp();
asm volatile("movl %%eax, %%cr3" ::"a"(kernel_page_directory().cr3()));
- asm volatile(
- "movl %%cr0, %%eax\n"
- "orl $0x80010001, %%eax\n"
- "movl %%eax, %%cr0\n" ::
- : "%eax", "memory");
setup_low_1mb();
-
protect_kernel_image();
+}
-#ifdef MM_DEBUG
- dbgprintf("MM: Paging initialized.\n");
-#endif
+MemoryManager::~MemoryManager()
+{
}
void MemoryManager::protect_kernel_image()
diff --git a/Kernel/VM/MemoryManager.h b/Kernel/VM/MemoryManager.h
index 823d68f937..f9f292fc64 100644
--- a/Kernel/VM/MemoryManager.h
+++ b/Kernel/VM/MemoryManager.h
@@ -121,7 +121,6 @@ private:
void unregister_region(Region&);
void detect_cpu_features();
- void initialize_paging();
void setup_low_1mb();
void protect_kernel_image();
void parse_memory_map();