aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64
diff options
context:
space:
mode:
authorFabian Imhof <fabian.imhof@ost.ch>2025-05-04 11:07:48 +0000
committerFabian Imhof <fabian.imhof@ost.ch>2025-05-04 11:07:48 +0000
commitccb0fcb78c0d22ebaeb9aa37f1941b0d44c98038 (patch)
tree266a971084657b0e0eefe3b54a5555efbe50fbed /arch/x86_64
parent4d4e23116284f41329ea809e2bda86feea1b325c (diff)
downloadteachos-ccb0fcb78c0d22ebaeb9aa37f1941b0d44c98038.tar.xz
teachos-ccb0fcb78c0d22ebaeb9aa37f1941b0d44c98038.zip
move user-mode code into own namespace and linker section
Diffstat (limited to 'arch/x86_64')
-rw-r--r--arch/x86_64/CMakeLists.txt8
-rw-r--r--arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp2
-rw-r--r--arch/x86_64/include/arch/user/main.hpp10
-rw-r--r--arch/x86_64/src/context_switching/main.cpp27
-rw-r--r--arch/x86_64/src/user/main.cpp36
5 files changed, 57 insertions, 26 deletions
diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt
index 0b4eafe..1ce2731 100644
--- a/arch/x86_64/CMakeLists.txt
+++ b/arch/x86_64/CMakeLists.txt
@@ -119,6 +119,14 @@ target_sources("_interrupt_handling" PRIVATE
)
#[============================================================================[
+# The User code
+#]============================================================================]
+
+target_sources("_context" PRIVATE
+ "src/user/main.cpp"
+)
+
+#[============================================================================[
# The Bootable ISO Image
#]============================================================================]
diff --git a/arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp b/arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp
index 8091a58..2d26668 100644
--- a/arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp
+++ b/arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp
@@ -26,7 +26,7 @@ namespace teachos::arch::interrupt_handling
*
* @param frame Pointer to the interrupt frame containing CPU state.
*/
- [[gnu::interrupt]] [[gnu::section(".interrupt_text")]]
+ [[gnu::interrupt]]
auto generic_interrupt_handler(interrupt_frame * frame) -> void;
} // namespace teachos::arch::interrupt_handling
diff --git a/arch/x86_64/include/arch/user/main.hpp b/arch/x86_64/include/arch/user/main.hpp
new file mode 100644
index 0000000..7127d07
--- /dev/null
+++ b/arch/x86_64/include/arch/user/main.hpp
@@ -0,0 +1,10 @@
+#ifndef TEACHOS_ARCH_X86_64_USER_MAIN_HPP
+#define TEACHOS_ARCH_X86_64_USER_MAIN_HPP
+
+namespace teachos::arch::user
+{
+ auto main() -> void;
+
+} // namespace teachos::arch::user
+
+#endif // TEACHOS_ARCH_X86_64_USER_MAIN_HPP \ No newline at end of file
diff --git a/arch/x86_64/src/context_switching/main.cpp b/arch/x86_64/src/context_switching/main.cpp
index 6c6ec1a..a112924 100644
--- a/arch/x86_64/src/context_switching/main.cpp
+++ b/arch/x86_64/src/context_switching/main.cpp
@@ -8,6 +8,7 @@
#include "arch/kernel/cpu/if.hpp"
#include "arch/kernel/cpu/segment_register.hpp"
#include "arch/kernel/cpu/tr.hpp"
+#include "arch/user/main.hpp"
#include "arch/video/vga/text.hpp"
namespace teachos::arch::context_switching
@@ -24,30 +25,6 @@ namespace teachos::arch::context_switching
4U, context_switching::interrupt_descriptor_table::segment_selector::REQUEST_LEVEL_USER};
auto reload_gdtr() -> void { kernel::cpu::call(KERNEL_CODE_POINTER); }
-
- auto user_mode_main() -> void
- {
- // TODO: Remove or replace this wall of text
- // RFLAGS is saved into R11, RIP of the next instruction into RCX
- // Required for SYSRETURN to know where to return too.
- // Additional state needs to be saved by calling convention:
-
- // Syscall Number: RAX, Return Value: RAX (0 indicating no error, and -1 indicating an error, use as a boolean)
- // Argument in this order (max 6. no argument on stack): RDI, RSI, RDX, R10, R8, R9
- // Not used registers: RBX, RSP, R12, R13, R14
-
- // Actual Source: https://man7.org/linux/man-pages/man2/syscall.2.html More cleare documentation:
- // https://sys.readthedocs.io/en/latest/doc/05_calling_system_calls.html
-
- const char syscall_message[68] = "Successfully entered user mode and wrote to VGA buffer via syscall!";
- auto error = syscall::syscall(syscall::WRITE, {reinterpret_cast<uint64_t>(&syscall_message)});
-
- if (!error)
- {
- video::vga::text::write("Successfully made a SYSCALL and returned with SYSRETQ!",
- video::vga::text::common_attributes::green_on_black);
- }
- }
} // namespace
auto initialize_descriptor_tables() -> descriptor_tables
@@ -76,7 +53,7 @@ namespace teachos::arch::context_switching
auto switch_to_user_mode() -> void
{
syscall::enable_syscall();
- switch_context(USER_DATA_SEGMENT_SELECTOR, USER_CODE_SEGMENT_SELECTOR, user_mode_main);
+ switch_context(USER_DATA_SEGMENT_SELECTOR, USER_CODE_SEGMENT_SELECTOR, user::main);
}
auto switch_context(interrupt_descriptor_table::segment_selector data_segment,
diff --git a/arch/x86_64/src/user/main.cpp b/arch/x86_64/src/user/main.cpp
new file mode 100644
index 0000000..6d8eea7
--- /dev/null
+++ b/arch/x86_64/src/user/main.cpp
@@ -0,0 +1,36 @@
+#include "arch/user/main.hpp"
+
+#include "arch/context_switching/syscall/main.hpp"
+
+// TODO: Disallow this import
+#include "arch/video/vga/text.hpp"
+
+namespace teachos::arch::user
+{
+
+ [[gnu::section(".user_text")]]
+ auto main() -> void
+ {
+ // TODO: Remove or replace this wall of text
+ // RFLAGS is saved into R11, RIP of the next instruction into RCX
+ // Required for SYSRETURN to know where to return too.
+ // Additional state needs to be saved by calling convention:
+
+ // Syscall Number: RAX, Return Value: RAX (0 indicating no error, and -1 indicating an error, use as a boolean)
+ // Argument in this order (max 6. no argument on stack): RDI, RSI, RDX, R10, R8, R9
+ // Not used registers: RBX, RSP, R12, R13, R14
+
+ // Actual Source: https://man7.org/linux/man-pages/man2/syscall.2.html More cleare documentation:
+ // https://sys.readthedocs.io/en/latest/doc/05_calling_system_calls.html
+
+ const char syscall_message[68] = "Successfully entered user mode and wrote to VGA buffer via syscall!";
+ auto error = context_switching::syscall::syscall(context_switching::syscall::WRITE,
+ {reinterpret_cast<uint64_t>(&syscall_message)});
+
+ if (!error)
+ {
+ video::vga::text::write("Successfully made a SYSCALL and returned with SYSRETQ!",
+ video::vga::text::common_attributes::green_on_black);
+ }
+ }
+} // namespace teachos::arch::user \ No newline at end of file