aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/pre/src/context_switching/main.cpp
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2025-12-15 17:13:12 +0100
committerFelix Morgner <felix.morgner@ost.ch>2025-12-15 17:13:12 +0100
commit7b9482ae637126ac9337876e60f519b493437711 (patch)
tree6fc71a253c8b0325d303bd34c95b564ba536ed14 /arch/x86_64/pre/src/context_switching/main.cpp
parent116f9332a206767c45095950f09f7c7447b561cf (diff)
parenta9eeec745e29d89afd48ee43d09432eb6fc35be7 (diff)
downloadkernel-7b9482ae637126ac9337876e60f519b493437711.tar.xz
kernel-7b9482ae637126ac9337876e60f519b493437711.zip
os: rework kernel architecture
Rework the code structure and architecture of the kernel by separating platform-dependent and platform-independent code more cleanly. As of this patchset, full feature parity has not been achieved. Nonetheless, a sufficient subset of functionality has been ported to the new architecture to demonstrate the feasibility of the new structure.
Diffstat (limited to 'arch/x86_64/pre/src/context_switching/main.cpp')
-rw-r--r--arch/x86_64/pre/src/context_switching/main.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/arch/x86_64/pre/src/context_switching/main.cpp b/arch/x86_64/pre/src/context_switching/main.cpp
new file mode 100644
index 0000000..3eb6dae
--- /dev/null
+++ b/arch/x86_64/pre/src/context_switching/main.cpp
@@ -0,0 +1,66 @@
+#include "arch/context_switching/main.hpp"
+
+#include "arch/boot/pointers.hpp"
+#include "arch/context_switching/syscall/syscall_enable.hpp"
+#include "arch/kernel/cpu/call.hpp"
+#include "arch/kernel/cpu/if.hpp"
+#include "arch/kernel/cpu/segment_register.hpp"
+#include "arch/kernel/cpu/tr.hpp"
+#include "arch/user/main.hpp"
+
+namespace teachos::arch::context_switching
+{
+ namespace
+ {
+ constexpr interrupt_descriptor_table::segment_selector KERNEL_CODE_SEGMENT_SELECTOR{
+ 1U, interrupt_descriptor_table::segment_selector::REQUEST_LEVEL_KERNEL};
+ constexpr kernel::cpu::far_pointer KERNEL_CODE_POINTER{&kernel::cpu::reload_data_segment_registers,
+ KERNEL_CODE_SEGMENT_SELECTOR};
+ constexpr context_switching::interrupt_descriptor_table::segment_selector USER_CODE_SEGMENT_SELECTOR{
+ 3U, context_switching::interrupt_descriptor_table::segment_selector::REQUEST_LEVEL_USER};
+ constexpr context_switching::interrupt_descriptor_table::segment_selector USER_DATA_SEGMENT_SELECTOR{
+ 4U, context_switching::interrupt_descriptor_table::segment_selector::REQUEST_LEVEL_USER};
+
+ auto reload_gdtr() -> void
+ {
+ kernel::cpu::call(KERNEL_CODE_POINTER);
+ }
+ } // namespace
+
+ auto initialize_descriptor_tables() -> descriptor_tables
+ {
+ bool static initalized = false;
+
+ if (!initalized)
+ {
+ kernel::cpu::clear_interrupt_flag();
+
+ segment_descriptor_table::update_gdtr();
+ interrupt_descriptor_table::update_interrupt_descriptor_table_register();
+
+ reload_gdtr();
+ segment_descriptor_table::update_tss_register();
+
+ kernel::cpu::set_interrupt_flag();
+ initalized = true;
+ }
+
+ descriptor_tables tables = {segment_descriptor_table::get_or_create_gdt(),
+ interrupt_descriptor_table::get_or_create_interrupt_descriptor_table()};
+ return tables;
+ }
+
+ auto switch_to_user_mode() -> void
+ {
+ syscall::enable_syscall();
+ switch_context(USER_DATA_SEGMENT_SELECTOR, USER_CODE_SEGMENT_SELECTOR, user::main);
+ }
+
+ auto switch_context(interrupt_descriptor_table::segment_selector data_segment,
+ interrupt_descriptor_table::segment_selector code_segment, void (*return_function)()) -> void
+ {
+ (void)initialize_descriptor_tables();
+ kernel::cpu::set_data_segment_registers(data_segment);
+ kernel::cpu::set_code_segment_register(data_segment, code_segment, reinterpret_cast<uint64_t>(return_function));
+ }
+} // namespace teachos::arch::context_switching