From ccb0fcb78c0d22ebaeb9aa37f1941b0d44c98038 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 4 May 2025 11:07:48 +0000 Subject: move user-mode code into own namespace and linker section --- arch/x86_64/src/user/main.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 arch/x86_64/src/user/main.cpp (limited to 'arch/x86_64/src/user/main.cpp') 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(&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 -- cgit v1.2.3 From c1dff44858ebdb3cd5a49e84179796e44e7eb91c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 5 May 2025 06:41:31 +0000 Subject: Fix recursive include using extra file --- arch/x86_64/src/user/main.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/user/main.cpp') diff --git a/arch/x86_64/src/user/main.cpp b/arch/x86_64/src/user/main.cpp index 6d8eea7..8ce21ba 100644 --- a/arch/x86_64/src/user/main.cpp +++ b/arch/x86_64/src/user/main.cpp @@ -7,7 +7,6 @@ namespace teachos::arch::user { - [[gnu::section(".user_text")]] auto main() -> void { @@ -23,9 +22,9 @@ namespace teachos::arch::user // 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(&syscall_message)}); + char constexpr syscall_message[] = "Successfully entered user mode and wrote to VGA buffer via syscall!"; + auto const error = context_switching::syscall::syscall(context_switching::syscall::type::WRITE, + {reinterpret_cast(&syscall_message)}); if (!error) { -- cgit v1.2.3 From 27d4fb90ebbc754e98ff68ce5bc7839a44ed99c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 5 May 2025 09:22:28 +0000 Subject: Add comments to syscall components --- arch/x86_64/src/user/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/user/main.cpp') diff --git a/arch/x86_64/src/user/main.cpp b/arch/x86_64/src/user/main.cpp index 8ce21ba..1d56d2e 100644 --- a/arch/x86_64/src/user/main.cpp +++ b/arch/x86_64/src/user/main.cpp @@ -32,4 +32,4 @@ namespace teachos::arch::user video::vga::text::common_attributes::green_on_black); } } -} // namespace teachos::arch::user \ No newline at end of file +} // namespace teachos::arch::user -- cgit v1.2.3 From 7d2e8bc16e6c1bacb3c676d21ea2245d8132218f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 5 May 2025 11:27:29 +0000 Subject: Remove the addition of USER_ACCESSIBLE flag to every entry created --- arch/x86_64/src/user/main.cpp | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'arch/x86_64/src/user/main.cpp') diff --git a/arch/x86_64/src/user/main.cpp b/arch/x86_64/src/user/main.cpp index 1d56d2e..3ddcb32 100644 --- a/arch/x86_64/src/user/main.cpp +++ b/arch/x86_64/src/user/main.cpp @@ -10,18 +10,6 @@ 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 - char constexpr syscall_message[] = "Successfully entered user mode and wrote to VGA buffer via syscall!"; auto const error = context_switching::syscall::syscall(context_switching::syscall::type::WRITE, {reinterpret_cast(&syscall_message)}); -- cgit v1.2.3 From c9f46f3773e7943ce114af888a44f50061c2ac1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 6 May 2025 13:40:42 +0000 Subject: Remove user Mode Access to VGA / Heap and Kernel Methods. --- arch/x86_64/src/user/main.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src/user/main.cpp') diff --git a/arch/x86_64/src/user/main.cpp b/arch/x86_64/src/user/main.cpp index 3ddcb32..3e7b0ec 100644 --- a/arch/x86_64/src/user/main.cpp +++ b/arch/x86_64/src/user/main.cpp @@ -3,21 +3,31 @@ #include "arch/context_switching/syscall/main.hpp" // TODO: Disallow this import +#include "arch/kernel/cpu/if.hpp" #include "arch/video/vga/text.hpp" namespace teachos::arch::user { - [[gnu::section(".user_text")]] auto main() -> void { char constexpr syscall_message[] = "Successfully entered user mode and wrote to VGA buffer via syscall!"; auto const error = context_switching::syscall::syscall(context_switching::syscall::type::WRITE, {reinterpret_cast(&syscall_message)}); + kernel::cpu::clear_interrupt_flag(); // Causes crash Kernel Code (.text) is not mapped in User mMde + + auto test = new int{20}; // Causes crash Heap is not mapped in User Mode + (void)test; + if (!error) { + // Causes crash vga is not mapped in User Mode video::vga::text::write("Successfully made a SYSCALL and returned with SYSRETQ!", video::vga::text::common_attributes::green_on_black); } + + for (;;) + { + } } } // namespace teachos::arch::user -- cgit v1.2.3 From be32189323ba8c46091d6deaf091cf41147426b4 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Wed, 7 May 2025 14:06:25 +0000 Subject: wip custom heap allocation functions for user mode --- arch/x86_64/src/user/main.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'arch/x86_64/src/user/main.cpp') diff --git a/arch/x86_64/src/user/main.cpp b/arch/x86_64/src/user/main.cpp index 3e7b0ec..4f6e688 100644 --- a/arch/x86_64/src/user/main.cpp +++ b/arch/x86_64/src/user/main.cpp @@ -1,8 +1,9 @@ #include "arch/user/main.hpp" #include "arch/context_switching/syscall/main.hpp" +#include "arch/memory/heap/global_heap_allocator.hpp" -// TODO: Disallow this import +// TODO: Disallow these imports #include "arch/kernel/cpu/if.hpp" #include "arch/video/vga/text.hpp" @@ -11,20 +12,23 @@ namespace teachos::arch::user auto main() -> void { char constexpr syscall_message[] = "Successfully entered user mode and wrote to VGA buffer via syscall!"; - auto const error = context_switching::syscall::syscall(context_switching::syscall::type::WRITE, - {reinterpret_cast(&syscall_message)}); + context_switching::syscall::syscall(context_switching::syscall::type::WRITE, + {reinterpret_cast(&syscall_message)}); - kernel::cpu::clear_interrupt_flag(); // Causes crash Kernel Code (.text) is not mapped in User mMde + // kernel::cpu::clear_interrupt_flag(); // Causes crash Kernel Code (.text) is not mapped in User mMde - auto test = new int{20}; // Causes crash Heap is not mapped in User Mode + auto test = memory::heap::global_heap_allocator::malloc(20U); (void)test; - if (!error) - { - // Causes crash vga is not mapped in User Mode - video::vga::text::write("Successfully made a SYSCALL and returned with SYSRETQ!", - video::vga::text::common_attributes::green_on_black); - } + // auto test = new int{20}; // Causes crash Heap is not mapped in User Mode + // (void)test; + + // if (!error) + // { + // // Causes crash vga is not mapped in User Mode + // video::vga::text::write("Successfully made a SYSCALL and returned with SYSRETQ!", + // video::vga::text::common_attributes::green_on_black); + // } for (;;) { -- cgit v1.2.3 From 0b62fdb3fe657d056a42d7567b67951ba3468738 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 8 May 2025 08:51:52 +0000 Subject: wip allocating heap memory in user mode --- arch/x86_64/src/user/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/user/main.cpp') diff --git a/arch/x86_64/src/user/main.cpp b/arch/x86_64/src/user/main.cpp index 4f6e688..59647f8 100644 --- a/arch/x86_64/src/user/main.cpp +++ b/arch/x86_64/src/user/main.cpp @@ -17,8 +17,8 @@ namespace teachos::arch::user // kernel::cpu::clear_interrupt_flag(); // Causes crash Kernel Code (.text) is not mapped in User mMde - auto test = memory::heap::global_heap_allocator::malloc(20U); - (void)test; + auto address = memory::heap::global_heap_allocator::malloc(8U); + (void)address; // auto test = new int{20}; // Causes crash Heap is not mapped in User Mode // (void)test; -- cgit v1.2.3 From d4cc546df6eba2dd287785f1a63fbcce4a1b9bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Fri, 16 May 2025 09:58:57 +0000 Subject: Attempt to move atomic into stl text as well --- arch/x86_64/src/user/main.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'arch/x86_64/src/user/main.cpp') diff --git a/arch/x86_64/src/user/main.cpp b/arch/x86_64/src/user/main.cpp index 59647f8..a435a41 100644 --- a/arch/x86_64/src/user/main.cpp +++ b/arch/x86_64/src/user/main.cpp @@ -3,6 +3,9 @@ #include "arch/context_switching/syscall/main.hpp" #include "arch/memory/heap/global_heap_allocator.hpp" +#include +#include + // TODO: Disallow these imports #include "arch/kernel/cpu/if.hpp" #include "arch/video/vga/text.hpp" @@ -17,6 +20,12 @@ namespace teachos::arch::user // kernel::cpu::clear_interrupt_flag(); // Causes crash Kernel Code (.text) is not mapped in User mMde + int test = std::max(5, 10); + (void)test; + + std::atomic locked = {false}; + locked.exchange(true); + auto address = memory::heap::global_heap_allocator::malloc(8U); (void)address; -- cgit v1.2.3 From 5c314eef566df2732973e8cb35974ec49748adba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Fri, 16 May 2025 13:27:16 +0000 Subject: Fix bug where level 4 to level 2 entries are not mapped user accesible. --- arch/x86_64/src/user/main.cpp | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'arch/x86_64/src/user/main.cpp') diff --git a/arch/x86_64/src/user/main.cpp b/arch/x86_64/src/user/main.cpp index a435a41..747a8a4 100644 --- a/arch/x86_64/src/user/main.cpp +++ b/arch/x86_64/src/user/main.cpp @@ -6,10 +6,6 @@ #include #include -// TODO: Disallow these imports -#include "arch/kernel/cpu/if.hpp" -#include "arch/video/vga/text.hpp" - namespace teachos::arch::user { auto main() -> void @@ -18,8 +14,6 @@ namespace teachos::arch::user context_switching::syscall::syscall(context_switching::syscall::type::WRITE, {reinterpret_cast(&syscall_message)}); - // kernel::cpu::clear_interrupt_flag(); // Causes crash Kernel Code (.text) is not mapped in User mMde - int test = std::max(5, 10); (void)test; @@ -29,16 +23,6 @@ namespace teachos::arch::user auto address = memory::heap::global_heap_allocator::malloc(8U); (void)address; - // auto test = new int{20}; // Causes crash Heap is not mapped in User Mode - // (void)test; - - // if (!error) - // { - // // Causes crash vga is not mapped in User Mode - // video::vga::text::write("Successfully made a SYSCALL and returned with SYSRETQ!", - // video::vga::text::common_attributes::green_on_black); - // } - for (;;) { } -- cgit v1.2.3 From 1b5a771a34743a2973a82de5ebdfd22da030b841 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 18 May 2025 13:21:42 +0000 Subject: update linker file and improve section parsing --- arch/x86_64/src/user/main.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'arch/x86_64/src/user/main.cpp') diff --git a/arch/x86_64/src/user/main.cpp b/arch/x86_64/src/user/main.cpp index 747a8a4..e621327 100644 --- a/arch/x86_64/src/user/main.cpp +++ b/arch/x86_64/src/user/main.cpp @@ -4,22 +4,29 @@ #include "arch/memory/heap/global_heap_allocator.hpp" #include +#include #include +#include namespace teachos::arch::user { auto main() -> void { + // Test writing to VGA Buffer char constexpr syscall_message[] = "Successfully entered user mode and wrote to VGA buffer via syscall!"; context_switching::syscall::syscall(context_switching::syscall::type::WRITE, {reinterpret_cast(&syscall_message)}); - int test = std::max(5, 10); - (void)test; - - std::atomic locked = {false}; - locked.exchange(true); + // Test C++ standard library + std::array, 4> array_test = {std::atomic{5}, std::atomic{10}, + std::atomic{15}, std::atomic{20}}; + std::ranges::for_each(array_test, [](auto & item) { + auto value = item.load(); + uint8_t max_value = std::max(value, uint8_t{10}); + item.exchange(max_value + 2); + }); + // Test user heap auto address = memory::heap::global_heap_allocator::malloc(8U); (void)address; -- cgit v1.2.3 From 8d39f3f67734bf39cada370c39243e6ef33bf4a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 18 May 2025 14:45:05 +0000 Subject: Make new usable for both kernel and user calls --- arch/x86_64/src/user/main.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/user/main.cpp') diff --git a/arch/x86_64/src/user/main.cpp b/arch/x86_64/src/user/main.cpp index e621327..8b07e4a 100644 --- a/arch/x86_64/src/user/main.cpp +++ b/arch/x86_64/src/user/main.cpp @@ -12,8 +12,7 @@ namespace teachos::arch::user { auto main() -> void { - // Test writing to VGA Buffer - char constexpr syscall_message[] = "Successfully entered user mode and wrote to VGA buffer via syscall!"; + constexpr char syscall_message[] = "Successfully entered user mode and wrote to VGA buffer via syscall!"; context_switching::syscall::syscall(context_switching::syscall::type::WRITE, {reinterpret_cast(&syscall_message)}); @@ -26,8 +25,7 @@ namespace teachos::arch::user item.exchange(max_value + 2); }); - // Test user heap - auto address = memory::heap::global_heap_allocator::malloc(8U); + auto address = new uint64_t{10U}; (void)address; for (;;) -- cgit v1.2.3