aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86_64/src')
-rw-r--r--arch/x86_64/src/kernel/main.cpp4
-rw-r--r--arch/x86_64/src/memory/cpu/control_register.cpp30
-rw-r--r--arch/x86_64/src/memory/cpu/msr.cpp8
3 files changed, 26 insertions, 16 deletions
diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp
index c111f22..ea1a157 100644
--- a/arch/x86_64/src/kernel/main.cpp
+++ b/arch/x86_64/src/kernel/main.cpp
@@ -2,6 +2,7 @@
#include "arch/exception_handling/assert.hpp"
#include "arch/memory/allocator/area_frame_allocator.hpp"
+#include "arch/memory/cpu/msr.hpp"
#include "arch/memory/multiboot/reader.hpp"
#include "arch/memory/paging/kernel_mapper.hpp"
#include "arch/memory/paging/temporary_page.hpp"
@@ -20,6 +21,9 @@ namespace teachos::arch::kernel
auto const memory_information = memory::multiboot::read_multiboot2();
memory::allocator::area_frame_allocator allocator(memory_information);
+ memory::cpu::set_cr2_bit(memory::cpu::cr2_flags::WRITE_PROTECT);
+ memory::cpu::set_efer_bit(memory::cpu::efer_flags::NXE);
+
memory::paging::kernel_mapper kernel(allocator, memory_information);
kernel.remap_kernel();
video::vga::text::write("Kernel remapping successfull", video::vga::text::common_attributes::green_on_black);
diff --git a/arch/x86_64/src/memory/cpu/control_register.cpp b/arch/x86_64/src/memory/cpu/control_register.cpp
index 38a887c..7624244 100644
--- a/arch/x86_64/src/memory/cpu/control_register.cpp
+++ b/arch/x86_64/src/memory/cpu/control_register.cpp
@@ -2,24 +2,26 @@
#include "arch/exception_handling/assert.hpp"
+#include <type_traits>
+
namespace teachos::arch::memory::cpu
{
- auto read_control_register(control_register cr) -> std::size_t
+ auto read_control_register(control_register cr) -> uint64_t
{
- std::size_t current_value;
+ uint64_t current_value;
switch (cr)
{
case control_register::CR0:
- asm volatile("movq %%cr0, %[output]" : [output] "=r"(current_value));
+ asm volatile("mov %%cr0, %[output]" : [output] "=r"(current_value));
break;
case control_register::CR2:
- asm volatile("movq %%cr2, %[output]" : [output] "=r"(current_value));
+ asm volatile("mov %%cr2, %[output]" : [output] "=r"(current_value));
break;
case control_register::CR3:
- asm volatile("movq %%cr3, %[output]" : [output] "=r"(current_value));
+ asm volatile("mov %%cr3, %[output]" : [output] "=r"(current_value));
break;
case control_register::CR4:
- asm volatile("movq %%cr4, %[output]" : [output] "=r"(current_value));
+ asm volatile("mov %%cr4, %[output]" : [output] "=r"(current_value));
break;
default:
exception_handling::assert(false,
@@ -29,30 +31,30 @@ namespace teachos::arch::memory::cpu
return current_value;
}
- auto write_control_register(control_register cr, std::size_t new_value) -> void
+ auto write_control_register(control_register cr, uint64_t new_value) -> void
{
switch (cr)
{
case control_register::CR0:
- asm volatile("movq %[input], %%cr0"
+ asm volatile("mov %[input], %%cr0"
: /* no output from call */
: [input] "r"(new_value)
: "memory");
break;
case control_register::CR2:
- asm volatile("movq %[input], %%cr2"
+ asm volatile("mov %[input], %%cr2"
: /* no output from call */
: [input] "r"(new_value)
: "memory");
break;
case control_register::CR3:
- asm volatile("movq %[input], %%cr3"
+ asm volatile("mov %[input], %%cr3"
: /* no output from call */
: [input] "r"(new_value)
: "memory");
break;
case control_register::CR4:
- asm volatile("movq %[input], %%cr4"
+ asm volatile("mov %[input], %%cr4"
: /* no output from call */
: [input] "r"(new_value)
: "memory");
@@ -63,4 +65,10 @@ namespace teachos::arch::memory::cpu
break;
}
}
+
+ auto set_cr2_bit(cr2_flags flag) -> void
+ {
+ auto const cr2 = read_control_register(control_register::CR2);
+ write_control_register(control_register::CR2, static_cast<std::underlying_type<cr2_flags>::type>(flag) | cr2);
+ }
} // namespace teachos::arch::memory::cpu
diff --git a/arch/x86_64/src/memory/cpu/msr.cpp b/arch/x86_64/src/memory/cpu/msr.cpp
index 6e3d1d3..b83f902 100644
--- a/arch/x86_64/src/memory/cpu/msr.cpp
+++ b/arch/x86_64/src/memory/cpu/msr.cpp
@@ -4,7 +4,7 @@ namespace teachos::arch::memory::cpu
{
namespace
{
- constexpr uint32_t IA32_EFER_ADDRESS = 0xC0000080;
+ auto constexpr IA32_EFER_ADDRESS = 0xC0000080;
}
auto read_msr(uint32_t msr) -> uint64_t
@@ -25,9 +25,7 @@ namespace teachos::arch::memory::cpu
auto set_efer_bit(efer_flags flag) -> void
{
- uint64_t const efer = read_msr(IA32_EFER_ADDRESS);
- write_msr(IA32_EFER_ADDRESS, static_cast<uint64_t>(flag) | efer);
+ auto const efer = read_msr(IA32_EFER_ADDRESS);
+ write_msr(IA32_EFER_ADDRESS, static_cast<std::underlying_type<efer_flags>::type>(flag) | efer);
}
-
- auto enable_nxe_bit() -> void { set_efer_bit(efer_flags::NXE); }
} // namespace teachos::arch::memory::cpu