aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/context_switching
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86_64/src/context_switching')
-rw-r--r--arch/x86_64/src/context_switching/descriptor_table/global_descriptor_table.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/arch/x86_64/src/context_switching/descriptor_table/global_descriptor_table.cpp b/arch/x86_64/src/context_switching/descriptor_table/global_descriptor_table.cpp
new file mode 100644
index 0000000..1cba13c
--- /dev/null
+++ b/arch/x86_64/src/context_switching/descriptor_table/global_descriptor_table.cpp
@@ -0,0 +1,57 @@
+#include "global_descriptor_table.hpp"
+
+#include "arch/stl/vector.hpp"
+
+#include "segment_descriptor.hpp"
+
+namespace teachos::arch::context_switching::descriptor_table
+{
+ auto create_global_descriptor_table() -> global_descriptor_table
+ {
+ segment_descriptor null_segment{0};
+
+ // Kernel space code segment
+ access_byte kernel_code_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_KERNEL |
+ access_byte::CODE_OR_DATA_SEGMENT,
+ type_field::CODE_SEGMENT | type_field::READABLE};
+ gdt_flags kernel_code_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::LENGTH};
+ segment_descriptor kernel_code_segment{kernel_code_access_byte, kernel_code_gdt_flags, 0, 0xFFFFF};
+
+ // Kernel space data segment
+ access_byte kernel_data_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_KERNEL |
+ access_byte::CODE_OR_DATA_SEGMENT,
+ type_field::WRITABLE};
+ gdt_flags kernel_data_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::UPPER_BOUND};
+ segment_descriptor kernel_data_segment{kernel_data_access_byte, kernel_data_gdt_flags, 0, 0xFFFFF};
+
+ // User space code segment
+ access_byte user_code_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_USER |
+ access_byte::CODE_OR_DATA_SEGMENT,
+ type_field::CODE_SEGMENT | type_field::READABLE};
+ gdt_flags user_code_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::LENGTH};
+ segment_descriptor user_code_segment{user_code_access_byte, user_code_gdt_flags, 0, 0xFFFFF};
+
+ // User space data segment
+ access_byte user_data_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_USER |
+ access_byte::CODE_OR_DATA_SEGMENT,
+ type_field::WRITABLE};
+ gdt_flags user_data_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::UPPER_BOUND};
+ segment_descriptor user_data_segment{user_data_access_byte, user_data_gdt_flags, 0, 0xFFFFF};
+
+ stl::vector<segment_descriptor> global_descriptor_table{null_segment, kernel_code_segment, kernel_data_segment,
+ user_code_segment, user_data_segment};
+ return global_descriptor_table;
+ }
+
+ auto load_global_descriptor_table(global_descriptor_table_pointer gdt_pointer) -> void
+ {
+ //
+ }
+
+ auto initialize_global_descriptor_table() -> global_descriptor_table
+ {
+ global_descriptor_table gdt{create_global_descriptor_table()};
+ global_descriptor_table_pointer gdt_pointer{gdt.size() - 1, &gdt};
+ load_global_descriptor_table(gdt_pointer);
+ }
+} // namespace teachos::arch::context_switching::descriptor_table \ No newline at end of file