aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/pre/include
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/include
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/include')
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/gate_descriptor.hpp69
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/idt_flags.hpp81
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.hpp24
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.hpp40
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/ist_offset.hpp45
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/segment_selector.hpp105
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/main.hpp51
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/access_byte.hpp104
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/gdt_flags.hpp93
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/global_descriptor_table.hpp37
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/global_descriptor_table_pointer.hpp41
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_base.hpp73
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_extension.hpp74
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_type.hpp27
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/task_state_segment.hpp32
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/syscall/main.hpp91
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/syscall/syscall_enable.hpp18
-rw-r--r--arch/x86_64/pre/include/arch/context_switching/syscall/syscall_handler.hpp18
-rw-r--r--arch/x86_64/pre/include/arch/exception_handling/assert.hpp17
-rw-r--r--arch/x86_64/pre/include/arch/exception_handling/panic.hpp23
-rw-r--r--arch/x86_64/pre/include/arch/interrupt_handling/generic_interrupt_handler.hpp34
-rw-r--r--arch/x86_64/pre/include/arch/kernel/cpu/call.hpp30
-rw-r--r--arch/x86_64/pre/include/arch/kernel/cpu/gdtr.hpp27
-rw-r--r--arch/x86_64/pre/include/arch/kernel/cpu/idtr.hpp27
-rw-r--r--arch/x86_64/pre/include/arch/kernel/cpu/if.hpp21
-rw-r--r--arch/x86_64/pre/include/arch/kernel/cpu/msr.hpp64
-rw-r--r--arch/x86_64/pre/include/arch/kernel/cpu/segment_register.hpp97
-rw-r--r--arch/x86_64/pre/include/arch/kernel/cpu/tr.hpp24
-rw-r--r--arch/x86_64/pre/include/arch/kernel/halt.hpp13
-rw-r--r--arch/x86_64/pre/include/arch/kernel/main.hpp13
-rw-r--r--arch/x86_64/pre/include/arch/memory/heap/bump_allocator.hpp48
-rw-r--r--arch/x86_64/pre/include/arch/memory/heap/global_heap_allocator.hpp117
-rw-r--r--arch/x86_64/pre/include/arch/memory/heap/heap_allocator.hpp45
-rw-r--r--arch/x86_64/pre/include/arch/memory/heap/linked_list_allocator.hpp123
-rw-r--r--arch/x86_64/pre/include/arch/memory/heap/memory_block.hpp39
-rw-r--r--arch/x86_64/pre/include/arch/memory/heap/user_heap_allocator.hpp153
-rw-r--r--arch/x86_64/pre/include/arch/user/main.hpp16
37 files changed, 1954 insertions, 0 deletions
diff --git a/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/gate_descriptor.hpp b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/gate_descriptor.hpp
new file mode 100644
index 0000000..07110c8
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/gate_descriptor.hpp
@@ -0,0 +1,69 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_GATE_DESCRIPTOR_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_GATE_DESCRIPTOR_HPP
+
+#include "arch/context_switching/interrupt_descriptor_table/idt_flags.hpp"
+#include "arch/context_switching/interrupt_descriptor_table/ist_offset.hpp"
+#include "arch/context_switching/interrupt_descriptor_table/segment_selector.hpp"
+
+#include <bitset>
+#include <cstdint>
+
+namespace teachos::arch::context_switching::interrupt_descriptor_table
+{
+ __extension__ typedef __int128 int128_t;
+ __extension__ typedef unsigned __int128 uint128_t;
+
+ /**
+ * @brief Defines helper function for all states and the actual data the gate descriptor can have.
+ */
+ struct [[gnu::packed]] gate_descriptor
+ {
+ /**
+ * @brief Default Constructor.
+ */
+ gate_descriptor() = default;
+
+ /**
+ * @brief Constructor.
+ *
+ * @note Created gate descriptor copies the given bytes into these components ending with a 32 bit reserved
+ * field that has to be used, because the 64-bit gate descriptor needs to be big enough for two 32-bit gate
+ * descriptor.
+ * - 16 bit Segment Selector
+ * - 3 bit Interrupt Stack Table Offset
+ * - 8 bit Type and Flags
+ * - 64 bit Offset
+ *
+ * @param flags Copies the bits set from the given data into the individual components of a gate
+ * descriptor.
+ */
+ explicit gate_descriptor(uint128_t flags);
+
+ /**
+ * @brief Constructor.
+ *
+ * @param selector, ist, flags, offset Copies the bits set from the given data into the individual components of
+ * a gate descriptor.
+ */
+ gate_descriptor(segment_selector selector, ist_offset ist, idt_flags flags, uint64_t offset);
+
+ /**
+ * @brief Allows to compare the underlying bits of two instances.
+ *
+ * @param other Other instance that we want to compare with.
+ * @return Whether the underlying set bits of both types are the same.
+ */
+ auto operator==(gate_descriptor const & other) const -> bool = default;
+
+ private:
+ // The order in private variables starts for the first variable being the rightmost bit.
+ uint16_t _offset_1 = {}; ///< Lower 16 bits of handler function address (0 - 15)
+ segment_selector _selector = {}; ///< Segment selector (16 - 31)
+ ist_offset _ist = {}; ///< Interrupt Stack Table offset (32 - 39)
+ idt_flags _flags = {}; ///< Gate Type and Flags (40 - 47)
+ uint64_t _offset_2 : 48 = {}; ///< Upper 48 bits of handler function address (48 - 95)
+ uint32_t : 32; ///< Reserved field used to ensure this struct is 128 bits big (96 - 127)
+ };
+} // namespace teachos::arch::context_switching::interrupt_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_GATE_DESCRIPTOR_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/idt_flags.hpp b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/idt_flags.hpp
new file mode 100644
index 0000000..5104c36
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/idt_flags.hpp
@@ -0,0 +1,81 @@
+
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_IDT_FLAGS_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_IDT_FLAGS_HPP
+
+#include <bitset>
+#include <cstdint>
+
+namespace teachos::arch::context_switching::interrupt_descriptor_table
+{
+ /**
+ * @brief Defines helper function for all states that the access byte field of a segment descriptor can
+ * have.
+ */
+ struct [[gnu::packed]] idt_flags
+ {
+ /**
+ * @brief Possible set bits in our underlying bits and the meaning when they are set.
+ */
+ enum bitset : uint8_t
+ {
+ INTERRUPT_GATE = 0b01110, ///< The actual type of gate segment is a interrupt gate.
+ TRAP_GATE = 0b01111, ///< The actual type of gate segment is a trap gate.
+ DESCRIPTOR_LEVEL_KERNEL =
+ 0U << 5U, ///< Highest privileged level used by the kernel to allow for full access of resources.
+ DESCRIPTOR_LEVEL_ADMIN =
+ 1U << 5U, ///< Restricts access to own application and thoose of lower privilege. Should only be used if more
+ ///< than two privilege levels are required, otherwise using Level 3 and Level 0 is recommended.
+ DESCRIPTOR_LEVEL_PRIVILEGED_USER =
+ 2U << 5U, ///< Restricts access to own application and thoose of lower privilege. Should only be used if more
+ ///< than two privilege levels are required, otherwise using Level 3 and Level 0 is recommended.
+ DESCRIPTOR_LEVEL_USER = 3U << 5U, ///< Restricts access to only application and their specific memory.
+ PRESENT = 1U << 7U, ///< Present bit; Allows an entry to refer to a valid segment.
+ ///< Must be set (1) for any valid segment.
+ };
+
+ /**
+ * @brief Default Constructor.
+ */
+ idt_flags() = default;
+
+ /**
+ * @brief Constructor.
+ *
+ * @param flags Allows to set flags for the access byte field using the unscoped enum contained in this class, used
+ * to allow for direct integer conversion. This value is saved and can later be used to check whether certain flags
+ * are enabled or not using contains_flags method.
+ */
+ idt_flags(uint8_t flags);
+
+ /**
+ * @brief Checks if the given std::bitset is a subset or equivalent to the underlying data.
+ *
+ * @note Meaning that all bits that are set in the given std::bitset also have to be set in the underlyng
+ * data. Any additional bits that are set are not relevant.
+ *
+ * @param other Flags that we want to compare against and check if the underlying data has the same bits set.
+ * @return Whether the given flags are a subset or equivalent with the underlying data.
+ */
+ auto contains_flags(std::bitset<8U> other) const -> bool;
+
+ /**
+ * @brief Allows to compare the underlying bits of two instances.
+ *
+ * @param other Other instance that we want to compare with.
+ * @return Whether the underlying set bits of both types are the same.
+ */
+ auto operator==(idt_flags const & other) const -> bool = default;
+
+ /**
+ * @brief Combines all bits that are set in the std::bitset flags with the bits already set in the underlying data.
+ *
+ * @param other Additional bits that should be set.
+ */
+ auto operator|=(std::bitset<8U> other) -> void;
+
+ private:
+ uint8_t _flags = {}; ///< Underlying bits used to read the flags from.
+ };
+} // namespace teachos::arch::context_switching::interrupt_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_IDT_FLAGS_HPP \ No newline at end of file
diff --git a/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.hpp b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.hpp
new file mode 100644
index 0000000..b388e0e
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.hpp
@@ -0,0 +1,24 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_HPP
+
+#include "arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.hpp"
+
+namespace teachos::arch::context_switching::interrupt_descriptor_table
+{
+ /**
+ * @brief Updates the IDTR with the created interrupt descriptor table. If it has not been created yet this
+ * method will create it.
+ */
+ auto update_interrupt_descriptor_table_register() -> void;
+
+ /**
+ * @brief Creates the interrupt descriptor table, with the minimum required configuration. If this method is called
+ * more than once, the previously created instance is returned instead.
+ *
+ * @return Reference to the created interrupt_descriptor_table.
+ */
+ auto get_or_create_interrupt_descriptor_table() -> interrupt_descriptor_table &;
+
+} // namespace teachos::arch::context_switching::interrupt_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.hpp b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.hpp
new file mode 100644
index 0000000..7fe933b
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.hpp
@@ -0,0 +1,40 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_POINTER_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_POINTER_HPP
+
+#include "arch/context_switching/interrupt_descriptor_table/gate_descriptor.hpp"
+#include "arch/stl/vector.hpp"
+
+namespace teachos::arch::context_switching::interrupt_descriptor_table
+{
+ using interrupt_descriptor_table = stl::vector<gate_descriptor>;
+
+ /**
+ * @brief Represents a pointer to the Interrupt Descriptor Table (IDT).
+ *
+ * This structure is used to store the base address and length of the IDT.
+ */
+ struct [[gnu::packed]] interrupt_descriptor_table_pointer
+ {
+ /**
+ * @brief Default constructor.
+ */
+ interrupt_descriptor_table_pointer() = default;
+
+ /**
+ * @brief Constructor.
+ */
+ interrupt_descriptor_table_pointer(uint16_t table_length, gate_descriptor * address);
+
+ /**
+ * @brief Defaulted three-way comparsion operator.
+ */
+ auto operator<=>(interrupt_descriptor_table_pointer const & other) const -> std::strong_ordering = default;
+
+ private:
+ uint16_t table_length = {}; ///< The amount of segment descriptor entries in the global descriptor table - 1.
+ gate_descriptor * address = {}; ///< Non-owning pointer to the IDT base address.
+ };
+
+} // namespace teachos::arch::context_switching::interrupt_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_POINTER_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/ist_offset.hpp b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/ist_offset.hpp
new file mode 100644
index 0000000..e45bcf4
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/ist_offset.hpp
@@ -0,0 +1,45 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_IST_OFFSET_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_IST_OFFSET_HPP
+
+#include <bitset>
+#include <cstdint>
+
+namespace teachos::arch::context_switching::interrupt_descriptor_table
+{
+ /**
+ * @brief Defines helper function for all states that the interrupt stack table offset field of a gate descriptor can
+ * have. Is automatically increased to one byte in size, to include the following 5 reserved bits in the gate
+ * descriptor.
+ */
+ struct [[gnu::packed]] ist_offset
+ {
+ /**
+ * @brief Default Constructor.
+ */
+ ist_offset() = default;
+
+ /**
+ * @brief Constructor.
+ *
+ * @param offset Offset into the interrupt stack table. A value of of 0 means we do not switch stacks, whereas 1 - 7
+ * mean we switch to the n-th stack in the Interrupt Stack Table, contained in the TSS if the gate descriptor that
+ * contains this field is called.
+ */
+ ist_offset(uint8_t offset);
+
+ /**
+ * @brief Allows to compare the underlying set bits of two instances.
+ *
+ * @param other Other instance that we want to compare with.
+ * @return Whether the underlying set bits of both types are the same.
+ */
+ auto operator==(ist_offset const & other) const -> bool = default;
+
+ private:
+ uint8_t _ist : 3 = {}; ///< Offset into the interrupt stack table. A value of of 0 means we do not switch stacks,
+ ///< whereas 1 - 7 mean we switch to the n-th stack in the Interrupt Stack Table, contained
+ ///< in the TSS if the gate descriptor that contains this field is called.
+ };
+} // namespace teachos::arch::context_switching::interrupt_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_IST_OFFSET_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/segment_selector.hpp b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/segment_selector.hpp
new file mode 100644
index 0000000..ea8c145
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/interrupt_descriptor_table/segment_selector.hpp
@@ -0,0 +1,105 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_SEGMENT_SELECTOR_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_SEGMENT_SELECTOR_HPP
+
+#include <bitset>
+#include <cstdint>
+
+namespace teachos::arch::context_switching::interrupt_descriptor_table
+{
+ /**
+ * @brief Represents a segment selector in the x86_64 architecture, which points to a valid code segment in the global
+ * descriptor table.
+ *
+ * A segment selector is a 16-bit identifier used to select a segment descriptor
+ * from the Global Descriptor Table (GDT) or the Local Descriptor Table (LDT).
+ * It contains an index, a table indicator (TI), and a requested privilege level (RPL).
+ */
+ struct [[gnu::packed]] segment_selector
+ {
+ /**
+ * @brief Possible set bits in our underlying bits and the meaning when they are set.
+ */
+ enum bitset : uint8_t
+ {
+ REQUEST_LEVEL_KERNEL =
+ 0U << 0U, ///< Highest privileged level used by the kernel to allow for full access of resources.
+ REQUEST_LEVEL_ADMIN =
+ 1U << 0U, ///< Restricts access to own application and thoose of lower privilege. Should only be used if more
+ ///< than two privilege levels are required, otherwise using Level 3 and Level 0 is recommended.
+ REQUEST_LEVEL_PRIVILEGED_USER =
+ 2U << 0U, ///< Restricts access to own application and thoose of lower privilege. Should only be used if more
+ ///< than two privilege levels are required, otherwise using Level 3 and Level 0 is recommended.
+ REQUEST_LEVEL_USER = 3U << 0U, ///< Restricts access to only application and their specific memory.
+ LOCAL_DESCRIPTOR_TABLE = 1U << 2U, ///< Wheter the index referes to an entry in the local or global descriptor
+ ///< table. If enabled the index points to a local descriptor table, if it is
+ ///< cleared it referes to a global descriptor table instead.
+ };
+
+ /**
+ * @brief Default constructor.
+ */
+ segment_selector() = default;
+
+ /**
+ * @brief Constructor.
+ *
+ * @param index Index into the local or global descriptor table. Processor multiplies the index value by 8 (number
+ * of bytes in 32-bit segment descriptor) and adds the result to the base GDT or LDT address.
+ * @param flags Allows to set flags for the flags field using the unscoped enum contained in this class, used to
+ * allow for direct integer conversion.
+ */
+ constexpr segment_selector(uint16_t index, uint8_t flags)
+ : _flags(flags)
+ , _index(index)
+ {
+ // Nothing to do.
+ }
+
+ /**
+ * @brief Checks if the given std::bitset is a subset or equivalent to the underlying data.
+ *
+ * @note Meaning that all bits that are set in the given std::bitset also have to be set in the underlyng
+ * data. Any additional bits that are set are not relevant.
+ *
+ * @param other Flags that we want to compare against and check if the underlying data has the same bits set.
+ * @return Whether the given flags are a subset or equivalent with the underlying data.
+ */
+ auto contains_flags(std::bitset<3U> other) const -> bool;
+
+ /**
+ * @brief Gets the index into the global descriptor table or the local descriptor table this segment selector is
+ * pointing too.
+ *
+ * @return Underlying value of the index field, bit 3 - 16.
+ */
+ [[gnu::section(".user_text")]]
+ auto get_index() const -> uint16_t;
+
+ /**
+ * @brief Defaulted three-way comparsion operator.
+ */
+ auto operator<=>(segment_selector const & other) const -> std::strong_ordering = default;
+
+ /**
+ * @brief Combines all bits that are set in the std::bitset flags with the bits already set in the underlying data.
+ *
+ * @param other Additional bits that should be set.
+ */
+ auto operator|=(std::bitset<3U> other) -> void;
+
+ /**
+ * @brief Cast the underlying data into a combined 16-bit form, that contains all data.
+ *
+ * @return Underlying value combined into it's full size.
+ */
+ operator uint16_t() const;
+
+ private:
+ uint8_t _flags : 3 = {}; ///< Underlying bits used to read the flags from.
+ uint16_t _index
+ : 13 = {}; ///< Index into the local or global descriptor table. Processor multiplies the index value by 16
+ ///< (number of bytes in segment descriptor) and adds the result to the base address.
+ };
+} // namespace teachos::arch::context_switching::interrupt_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_SEGMENT_SELECTOR_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/main.hpp b/arch/x86_64/pre/include/arch/context_switching/main.hpp
new file mode 100644
index 0000000..f8477ea
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/main.hpp
@@ -0,0 +1,51 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_MAIN_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_MAIN_HPP
+
+#include "arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.hpp"
+#include "arch/context_switching/segment_descriptor_table/global_descriptor_table.hpp"
+
+namespace teachos::arch::context_switching
+{
+ /**
+ * @brief Contains the references to the tables required for context switching
+ */
+ struct descriptor_tables
+ {
+ segment_descriptor_table::global_descriptor_table & gdt; ///< Reference to the global descriptor table.
+ interrupt_descriptor_table::interrupt_descriptor_table & idt; ///< Reference to the interrupt descriptor table.
+ };
+
+ /**
+ * @brief Creates the Interrupt Descriptor Table and Global Descriptor Table as a static variable the first time this
+ * method is called and update IDTR and GDTR registers values.
+ *
+ * @note Subsequent calls after the first one, will simply return the previously created tables, but not update the
+ * registers again.
+ *
+ * @return References to the statically created Interrupt Descriptor and Global Descriptor Table.
+ */
+ auto initialize_descriptor_tables() -> descriptor_tables;
+
+ /**
+ * @brief Switches from the current Kernel Mode (Level 0) to User Mode (Level 3). Will simply use predefined Segment
+ * Selectors for the User Data and User Code Segment, which are Index 3 and 4 in the GDT respectively.
+ */
+ auto switch_to_user_mode() -> void;
+
+ /**
+ * @brief Switches from the current Code and Data Segment to the given Code and Data Segment.
+ *
+ * @note This method will additionally call initialize_descriptor_tables, to ensure the GDTR and IDTR have been setup
+ * correctly before attempting to switch the context. This switch is achieved using a far return, which will once
+ * executed call the given void function.
+ *
+ * @param data_segment Data Segment that the SS, DS; ES, FS and GS register will be set too.
+ * @param code_segment Code Segment that the CS register will be set too.
+ * @param return_function Function that will be called once the switch has been achieved.
+ */
+ auto switch_context(interrupt_descriptor_table::segment_selector data_segment,
+ interrupt_descriptor_table::segment_selector code_segment, void (*return_function)()) -> void;
+
+} // namespace teachos::arch::context_switching
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_MAIN_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/access_byte.hpp b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/access_byte.hpp
new file mode 100644
index 0000000..7450330
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/access_byte.hpp
@@ -0,0 +1,104 @@
+
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_ACCESS_BYTE_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_ACCESS_BYTE_HPP
+
+#include <bitset>
+#include <cstdint>
+
+namespace teachos::arch::context_switching::segment_descriptor_table
+{
+ /**
+ * @brief Defines helper function for all states that the access byte field of a segment descriptor can
+ * have.
+ */
+ struct [[gnu::packed]] access_byte
+ {
+ /**
+ * @brief Possible set bits in our underlying bits and the meaning when they are set.
+ */
+ enum bitset : uint8_t
+ {
+ ACCESSED =
+ 1U
+ << 0U, ///< Whether the segment has been accessed since the last time the operating system has cleared the
+ ///< flag. If enabled it has been accessed, otherwise it has not been accessed since the last clear.
+ WRITABLE = 1U << 1U, ///< Indicates if the data segment is writable or not. If enabled the code segment allows
+ ///< read and write access, otherwise only read access is possible.
+ READABLE = 1U << 1U, ///< Indicates if the code segment is readable or not. If enabled the code segment allows
+ ///< read and execute access, otherwise only executable access is possible.
+ CONFORMING =
+ 1U << 2U, ///< Indicates if the code is allowed to be executed by different access levels
+ ///< (higher or lower) in code segments. If enabled the code segment allows access, otherwise
+ ///< access from different privilege levels with throw a General-Protectione exception.
+ EXPAND_DOWN = 1U << 2U, ///< Indicates if the expansion direction is up or down in data segments. If enabled the
+ ///< data segment expands downwards, otherwise it expands upwards.
+ CODE_SEGMENT = 1U << 3U, ///< Further defines the actual type of the segment. If enabled this segment is a code
+ ///< segment, otherwise its a data segment.
+ LOCAL_DESCRIPTOR_TABLE = 2, ///< The actual type of sytem segment is a local descriptor table.
+ TASK_STATE_SEGMENT_AVAILABLE =
+ 9, ///< The actual type of sytem segment is a task state segment that is still available.
+ TASK_STATE_SEGMENT_BUSY = 11, ///< The actual type of sytem segment is a task state segment that is currently in
+ ///< use and therefore busy.
+ CALL_GATE = 11, ///< The actual type of sytem segment is a call gate.
+ INTERRUPT_GATE = 14, ///< The actual type of sytem segment is a interrupt gate.
+ TRAP_GATE = 15, ///< The actual type of sytem segment is a trap gate.
+ CODE_OR_DATA_SEGMENT = 1U << 4U, ///< Defines a system segment (if 0) or a code/data segment (if 1).
+ DESCRIPTOR_LEVEL_KERNEL =
+ 0U << 5U, ///< Highest privileged level used by the kernel to allow for full access of resources.
+ DESCRIPTOR_LEVEL_ADMIN =
+ 1U << 5U, ///< Restricts access to own application and thoose of lower privilege. Should only be used if more
+ ///< than two privilege levels are required, otherwise using Level 3 and Level 0 is recommended.
+ DESCRIPTOR_LEVEL_PRIVILEGED_USER =
+ 2U << 5U, ///< Restricts access to own application and thoose of lower privilege. Should only be used if more
+ ///< than two privilege levels are required, otherwise using Level 3 and Level 0 is recommended.
+ DESCRIPTOR_LEVEL_USER = 3U << 5U, ///< Restricts access to only application and their specific memory.
+ PRESENT = 1U << 7U, ///< Present bit; Allows an entry to refer to a valid segment.
+ ///< Must be set (1) for any valid segment.
+ };
+
+ /**
+ * @brief Default Constructor.
+ */
+ access_byte() = default;
+
+ /**
+ * @brief Constructor.
+ *
+ * @param flags Allows to set flags for the access byte field using the unscoped enum contained in this class, used
+ * to allow for direct integer conversion. This value is saved and can later be used to check whether certain flags
+ * are enabled or not using contains_flags method.
+ */
+ access_byte(uint8_t flags);
+
+ /**
+ * @brief Checks if the given std::bitset is a subset or equivalent to the underlying data.
+ *
+ * @note Meaning that all bits that are set in the given std::bitset also have to be set in the underlyng
+ * data. Any additional bits that are set are not relevant.
+ *
+ * @param other Flags that we want to compare against and check if the underlying data has the same bits set.
+ * @return Whether the given flags are a subset or equivalent with the underlying data.
+ */
+ auto contains_flags(std::bitset<8U> other) const -> bool;
+
+ /**
+ * @brief Allows to compare the underlying data of two instances.
+ *
+ * @param other Other instance that we want to compare with.
+ * @return Whether the underlying data of both types is the same.
+ */
+ auto operator==(access_byte const & other) const -> bool = default;
+
+ /**
+ * @brief Combines all bits that are set in the std::bitset flags with the bits already set in the underlying data.
+ *
+ * @param other Additional bits that should be set.
+ */
+ auto operator|=(std::bitset<8U> other) -> void;
+
+ private:
+ uint8_t _flags = {}; ///< Underlying bits used to read the flags from.
+ };
+} // namespace teachos::arch::context_switching::segment_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_ACCESS_BYTE_HPP \ No newline at end of file
diff --git a/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/gdt_flags.hpp b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/gdt_flags.hpp
new file mode 100644
index 0000000..e24b988
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/gdt_flags.hpp
@@ -0,0 +1,93 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_GDT_FLAGS_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_GDT_FLAGS_HPP
+
+#include "arch/context_switching/segment_descriptor_table/segment_descriptor_type.hpp"
+
+#include <bitset>
+
+namespace teachos::arch::context_switching::segment_descriptor_table
+{
+ /**
+ * @brief Defines helper function for all states that the flags field of a segment descriptor can
+ * have.
+ */
+ struct [[gnu::packed]] gdt_flags
+ {
+ /**
+ * @brief Possible set bits in our underlying bits and the meaning when they are set.
+ */
+ enum bitset : uint8_t
+ {
+ LONG_MODE = 1U << 1U, ///< Defines in IA-32e mode (64-bit code and 32-bit compatability mode) if the segment
+ ///< contains 64-bit code. Otherwise this bit should always be 0. Enable if instructions
+ ///< are executed in 64-bit code, otherwise they are executed in compatability 32-bit mode.
+ ///< If this bit is set the 3rd bit needs to be clear (0).
+ UPPER_BOUND = 1U << 2U, ///< Specifies the upper bound of the segment for expand down data segment. Enable for 4
+ ///< GiB, 4 KiB otherwise.
+ STACK_POINTER_SIZE = 1U << 2U, ///< Specifies the size of the Stack Pointer (SP) for stack segments used for
+ ///< implicit stack operations. Enable for 32 bit, 16 bit otherwise.
+ DEFAULT_LENGTH = 1U << 2U, ///< Indicates the default length for code segments with effective addresses and
+ ///< operands. Enable for 32 bit, 16 bit otherwise.
+ GRANULARITY = 1U << 3U, ///< Indicates the size the Limit value in the segment descriptor is scaled by 1 Byte
+ ///< blocks if the bit is not set or by 4 KiB blocks if the bit is set.
+ };
+
+ /**
+ * @brief Default Constructor.
+ */
+ gdt_flags() = default;
+
+ /**
+ * @brief Constructor.
+ *
+ * @param flags Allows to set flags for the flags field using the unscoped enum contained in this class, used to
+ * allow for direct integer conversion. This value is saved and can later be used to check whether certain flags are
+ * enabled or not using contains_flags method.
+ * @param limit Does not necessarily make sense in the gdt flags type, but because the flags alone are only 4 bit
+ * the type would still require the space for a complete bit. Therefore the 4 bit segment limit field before the
+ * flags field is included in this type to ensure we actually contain 8 bit of data.
+ */
+ gdt_flags(uint8_t flags, std::bitset<20U> limit);
+
+ /**
+ * @brief Checks if the given std::bitset is a subset or equivalent to the underlying data.
+ *
+ * @note Meaning that all bits that are set in the given std::bitset also have to be set in the underlyng
+ * data. Any additional bits that are set are not relevant.
+ *
+ * @param other Flags that we want to compare against and check if the underlying data has the same bits set.
+ * @return Whether the given flags are a subset or equivalent with the underlying data.
+ */
+ auto contains_flags(std::bitset<4U> other) const -> bool;
+
+ /**
+ * @brief Get part of the segment limit that is saved in the gdt flags. This does not necessarily make sense in this
+ * object, but it has to be included here because a struct can not be smaller than a full byte. Therefore we include
+ * the 4 bit segment limit field so that it results in a compelte byte with the addtional 4 bit of gdt flags.
+ *
+ * @return 4-bit limit segment
+ */
+ auto get_limit() const -> std::bitset<4U>;
+
+ /**
+ * @brief Allows to compare the underlying set bits of two instances.
+ *
+ * @param other Other instance that we want to compare with.
+ * @return Whether the underlying set bits of both types are the same.
+ */
+ auto operator==(gdt_flags const & other) const -> bool = default;
+
+ /**
+ * @brief Combines all bits that are set in the std::bitset flags with the bits already set in the underlying data.
+ *
+ * @param other Additional bits that should be set.
+ */
+ auto operator|=(std::bitset<4U> other) -> void;
+
+ private:
+ uint8_t _limit_2 : 4 = {}; ///< Second part of the limit field.
+ uint8_t _flags : 4 = {}; ///< Underlying bits used to read the flags from.
+ };
+} // namespace teachos::arch::context_switching::segment_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_GDT_FLAGS_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/global_descriptor_table.hpp b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/global_descriptor_table.hpp
new file mode 100644
index 0000000..44f2692
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/global_descriptor_table.hpp
@@ -0,0 +1,37 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_HPP
+
+#include "arch/context_switching/segment_descriptor_table/global_descriptor_table_pointer.hpp"
+#include "arch/context_switching/segment_descriptor_table/task_state_segment.hpp"
+
+namespace teachos::arch::context_switching::segment_descriptor_table
+{
+ /**
+ * @brief Creates the global descriptor table, with the minimum required configuration. If this method is called more
+ * than once, the previously created instance is returned instead.
+ *
+ * @return Reference to the created global_descriptor_table.
+ */
+ auto get_or_create_gdt() -> global_descriptor_table &;
+
+ /**
+ * @brief Updates the GDTR with the created global descriptor table. If it has not been created yet this
+ * method will create it.
+ *
+ * @note This method will only set the GDTR, but for the processor to actually register the change a far jump
+ * has to be executed. This also has to be done before updating the TR.
+ */
+ auto update_gdtr() -> void;
+
+ /**
+ * @brief Updates the TR with the created task state segment. If it has not been created yet this
+ * method will create it.
+ *
+ * @note This method should only be called after update_gdtr() and a far jump has been
+ * executed. Because before that trying to access the segment will cause an exception.
+ */
+ auto update_tss_register() -> void;
+
+} // namespace teachos::arch::context_switching::segment_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/global_descriptor_table_pointer.hpp b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/global_descriptor_table_pointer.hpp
new file mode 100644
index 0000000..292ff70
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/global_descriptor_table_pointer.hpp
@@ -0,0 +1,41 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_POINTER_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_POINTER_HPP
+
+#include "arch/stl/vector.hpp"
+
+#include <cstdint>
+
+namespace teachos::arch::context_switching::segment_descriptor_table
+{
+ using global_descriptor_table = stl::vector<uint64_t>;
+
+ /**
+ * @brief Represents a pointer to the Global Descriptor Table (GDT).
+ *
+ * This structure is used to store the base address and length of the GDT.
+ * It is used when loading or modifying the GDT during context switching.
+ */
+ struct [[gnu::packed]] global_descriptor_table_pointer
+ {
+ /**
+ * @brief Default constructor.
+ */
+ global_descriptor_table_pointer() = default;
+
+ /**
+ * @brief Constructor.
+ */
+ global_descriptor_table_pointer(uint16_t table_length, uint64_t * address);
+
+ /**
+ * @brief Defaulted three-way comparsion operator.
+ */
+ auto operator<=>(global_descriptor_table_pointer const & other) const -> std::strong_ordering = default;
+
+ private:
+ uint16_t table_length = {}; ///< The amount of segment descriptor entries in the global descriptor table - 1.
+ uint64_t * address = {}; ///< Non-owning pointer to the GDT base address.
+ };
+} // namespace teachos::arch::context_switching::segment_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_POINTER_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_base.hpp b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_base.hpp
new file mode 100644
index 0000000..933fb4d
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_base.hpp
@@ -0,0 +1,73 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_SEGMENT_DESCRIPTOR_BASE_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_SEGMENT_DESCRIPTOR_BASE_HPP
+
+#include "arch/context_switching/segment_descriptor_table/access_byte.hpp"
+#include "arch/context_switching/segment_descriptor_table/gdt_flags.hpp"
+#include "arch/context_switching/segment_descriptor_table/segment_descriptor_type.hpp"
+
+namespace teachos::arch::context_switching::segment_descriptor_table
+{
+ /**
+ * @brief Defines helper function for all states and the actual data the segment descriptor can have.
+ */
+ struct [[gnu::packed]] segment_descriptor_base
+ {
+ /**
+ * @brief Default Constructor.
+ */
+ segment_descriptor_base() = default;
+
+ /**
+ * @brief Constructor.
+ *
+ * @note Created segment descriptor copies the given bytes into these components requiring the space of one
+ * segment descriptor entry in the global descriptor table being 64-bit.
+ * - 8 bit Access Type
+ * - 4 bit Flags
+ * - 32 bit Base Address
+ * - 20 bit Limit
+ *
+ * @param flags Copies the bits set from the given data into the individual components of a segment
+ * descriptor.
+ */
+ explicit segment_descriptor_base(uint64_t flags);
+
+ /**
+ * @brief Constructor.
+ *
+ * @param access_byte, flags, base, limit Copies the bits set from the given data into the individual components of
+ * a segment descriptor.
+ */
+ segment_descriptor_base(access_byte access_byte, gdt_flags flags, uint32_t base, std::bitset<20U> limit);
+
+ /**
+ * @brief Calculates the underlying segment type that this segement descriptor is describing.
+ */
+ auto get_segment_type() const -> segment_descriptor_type;
+
+ /**
+ * @brief Cast the underlying data into a combined 64-bit form, that contains all data.
+ *
+ * @return Underlying value combined into it's full size.
+ */
+ operator uint64_t() const;
+
+ /**
+ * @brief Allows to compare the underlying bits of two instances.
+ *
+ * @param other Other instance that we want to compare with.
+ * @return Whether the underlying set bits of both types are the same.
+ */
+ auto operator==(segment_descriptor_base const & other) const -> bool = default;
+
+ private:
+ // The order in private variables starts for the first variable being the rightmost bit.
+ uint16_t _limit_1 = {}; ///< First part of the limit field (0 - 15)
+ uint32_t _base_1 : 24 = {}; ///< First part of the base field (16 - 39)
+ access_byte _access = {}; ///< Access byte field (40 - 47)
+ gdt_flags _flag = {}; ///< Second part of the limit field + Flags field (48 - 55)
+ uint8_t _base_2 = {}; ///< Second part of the base field (56 - 63)
+ };
+} // namespace teachos::arch::context_switching::segment_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_SEGMENT_DESCRIPTOR_BASE_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_extension.hpp b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_extension.hpp
new file mode 100644
index 0000000..40bcc8a
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_extension.hpp
@@ -0,0 +1,74 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_SEGMENT_DESCRIPTOR_EXTENSION_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_SEGMENT_DESCRIPTOR_EXTENSION_HPP
+
+#include "arch/context_switching/segment_descriptor_table/segment_descriptor_base.hpp"
+
+namespace teachos::arch::context_switching::segment_descriptor_table
+{
+ __extension__ typedef __int128 int128_t;
+ __extension__ typedef unsigned __int128 uint128_t;
+
+ /**
+ * @brief Defines helper function for all states and the actual data the segment descriptor can have.
+ */
+ struct [[gnu::packed]] segment_descriptor_extension
+ {
+ /**
+ * @brief Default Constructor.
+ */
+ segment_descriptor_extension() = default;
+
+ /**
+ * @brief Constructor.
+ *
+ * @note Created segment descriptor copies the given bytes into these components requiring the space of two
+ * segment descriptor entry in the global descriptor table being 128-bit. Ending with a 32 bit reserved
+ * field that has to be used, because the segment descriptor needs to be big enough for two segment
+ * descriptor entries.
+ * - 8 bit Access Type
+ * - 4 bit Flags
+ * - 64 bit Base Address
+ * - 20 bit Limit
+ *
+ * @param flags Copies the bits set from the given data into the individual components of a segment
+ * descriptor.
+ */
+ explicit segment_descriptor_extension(uint128_t flags);
+
+ /**
+ * @brief Constructor.
+ *
+ * @param access_byte, flags, base, limit Copies the bits set from the given data into the individual components of
+ * a segment descriptor.
+ */
+ segment_descriptor_extension(access_byte access_byte, gdt_flags flags, uint64_t base, std::bitset<20U> limit);
+
+ /**
+ * @brief Returns the underlying base segment descriptor, being the first part of the segment descriptor consisting
+ * of two entries in the global descriptor table.
+ */
+ auto get_first_gdt_entry() const -> segment_descriptor_base;
+
+ /**
+ * @brief Returns the underlying extension to the segment descriptor, being the second part of the segment
+ * descriptor consiting of two entries in the global descriptor table.
+ */
+ auto get_second_gdt_entry() const -> uint64_t;
+
+ /**
+ * @brief Allows to compare the underlying bits of two instances.
+ *
+ * @param other Other instance that we want to compare with.
+ * @return Whether the underlying set bits of both types are the same.
+ */
+ auto operator==(segment_descriptor_extension const & other) const -> bool = default;
+
+ private:
+ // The order in private variables starts for the first variable being the rightmost bit.
+ segment_descriptor_base _base = {}; ///< Base Segment Descriptor representing single entry in GDT (0 - 63)
+ uint32_t _base_3 = {}; ///< Third part of the base field (63 - 95)
+ uint32_t : 32; ///< Reserved field used to ensure this struct is 128 bits big (96 - 127)
+ };
+} // namespace teachos::arch::context_switching::segment_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_SEGMENT_DESCRIPTOR_EXTENSION_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_type.hpp b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_type.hpp
new file mode 100644
index 0000000..8770b81
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/segment_descriptor_type.hpp
@@ -0,0 +1,27 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_SEGMENT_DESCRIPTOR_TYPES_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_SEGMENT_DESCRIPTOR_TYPES_HPP
+
+#include <cstdint>
+
+namespace teachos::arch::context_switching::segment_descriptor_table
+{
+ /**
+ * @brief Possible overlying types of the segment descriptor. Allowing to discern between the major types, which
+ * result in different handling of the actual data contained in the descriptor.
+ */
+ enum class segment_descriptor_type : uint8_t
+ {
+ SYSTEM_SEGMENT, ///< The segment is of type system, is distinguished by the Descriptor Type field in the Access
+ ///< Byte. Can be further distinguised to specific system segment types using the Type Field in the
+ ///< Access Byte.
+ DATA_SEGMENT, ///< The segment is of type data, is is distinguished by the Descriptor Type field in the Access
+ ///< Byte and the first bit of the Type Field in the Access Byte. Can be further distinguised to
+ ///< specific data segment types using the the remaining bits in the Type Field in the Access Byte.
+ CODE_SEGMENT, ///< The segment is of type code, is is distinguished by the Descriptor Type field in
+ ///< the Access Byte and the first bit of the Type Field in the Access Byte. Can be
+ ///< further distinguised to specific data segment types using the the remaining bits in
+ ///< the Type Field in the Access Byte.
+ };
+} // namespace teachos::arch::context_switching::segment_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_SEGMENT_DESCRIPTOR_TYPES_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/task_state_segment.hpp b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/task_state_segment.hpp
new file mode 100644
index 0000000..d4aa5e8
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/segment_descriptor_table/task_state_segment.hpp
@@ -0,0 +1,32 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_TASK_STATE_SEGMENT_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_TASK_STATE_SEGMENT_HPP
+
+#include <cstdint>
+
+namespace teachos::arch::context_switching::segment_descriptor_table
+{
+ /**
+ * @brief 64-bit task state segment
+ */
+ struct [[gnu::packed]] task_state_segment
+ {
+ private:
+ uint32_t : 32;
+ uint64_t rsp0 = {};
+ uint64_t rsp1 = {};
+ uint64_t rsp2 = {};
+ uint64_t : 64;
+ uint64_t ist1 = {};
+ uint64_t ist2 = {};
+ uint64_t ist3 = {};
+ uint64_t ist4 = {};
+ uint64_t ist5 = {};
+ uint64_t ist6 = {};
+ uint64_t ist7 = {};
+ uint64_t : 64;
+ uint16_t : 16;
+ uint16_t io_map_base_address = {};
+ };
+} // namespace teachos::arch::context_switching::segment_descriptor_table
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SEGMENT_DESCRIPTOR_TABLE_TASK_STATE_SEGMENT_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/syscall/main.hpp b/arch/x86_64/pre/include/arch/context_switching/syscall/main.hpp
new file mode 100644
index 0000000..f507c61
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/syscall/main.hpp
@@ -0,0 +1,91 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_MAIN_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_MAIN_HPP
+
+#include <cstdint>
+
+namespace teachos::arch::context_switching::syscall
+{
+ /**
+ * @brief Possible syscall implementation that should actually be called.
+ *
+ * @note Attempts to reflect the Linux interface partially. See https://filippo.io/linux-syscall-table/ for more
+ * information.
+ */
+ enum class type : uint64_t
+ {
+ WRITE = 1U, ///< Loads the arg_0 parameter as an address pointing to a const char array, which will then be printed
+ ///< onto the VGA buffer screen.
+ EXPAND_HEAP = 2U, /// Expands the User Heap by additonally mapping 100 KiB of virtual page memory. Ignores the
+ /// parameters and uses them as out parameters instead, where arg_0 is the start of the newly
+ /// mapped heap area and arg_1 is the size of the entire area. Can be less than 100 KiB if less
+ /// space remains.
+ ASSERT = 3U, /// Loads the arg_0 parameter as a boolean which needs to be true or it will print the message in
+ /// arg_1 parameter onto the VGA buffer screen, keep it as a nullptr if it shouldn't print anything
+ /// and then it halts the further execution of the application.
+ };
+
+ /**
+ * @brief Possible error codes that can be returned by the different syscall methods called depending on the type
+ * enum.
+ */
+ enum class error : uint8_t
+ {
+ OK = 0U, ///< No error occured in syscall.
+ OUT_OF_MEMORY = 1U, ///< Expanding heap failed because we have run out of mappable virtual address space.
+ };
+
+ /**
+ * @brief Allows to convert the error enum type into a boolean directly. Where any code besides 0 being no error, will
+ * return true. The remaining errors return true.
+ *
+ * @param e Error code that was returned by the syscall.
+ * @return Return true if there was no error and false otherwise.
+ */
+ constexpr bool operator!(error e)
+ {
+ return e == error::OK;
+ }
+
+ /**
+ * @brief Maximum amount of arguments that can be passed to a syscall. Default value is 0 and arguments are only ever
+ * used depending on the actual enum type and if the method requires thoose parameters.
+ */
+ struct arguments
+ {
+ uint64_t arg_0{}; ///< First optional paramter to the syscall representing the RDI register.
+ uint64_t arg_1{}; ///< Second optional paramter to the syscall representing the RSI register.
+ uint64_t arg_2{}; ///< Third optional paramter to the syscall representing the RDX register.
+ uint64_t arg_3{}; ///< Fourth optional paramter to the syscall representing the R10 register.
+ uint64_t arg_4{}; ///< Fifth optional paramter to the syscall representing the R8 register.
+ uint64_t arg_5{}; ///< Sixth optional paramter to the syscall representing the R9 register.
+ };
+
+ /**
+ * @brief Response of a systemcall always containin an error code, signaling if the syscall even succeeded or not.
+ * Additionally it may contain up to 6 return values in the values struct.
+ */
+ struct response
+ {
+ error error_code; ///< Error code returned by the syscall. If it failed all the values will be 0.
+ arguments values = {}; ///< Optional return values of the syscall implementation.
+ };
+
+ /**
+ * @brief Calls the method associated with the given syscall number and passes the given optional arguments to it,
+ * over the RDI, RSI, RDX, R10, R8 and R9 register.
+ *
+ * @param syscall_number Syscall method that should be called. See enum values in type for possible implemented
+ * methods.
+ * @param args Optional arguments passable to the different syscall methods, called depending on the syscall_number.
+ * Not passing the required parameters to the method, will result in passing 0 instead, which might make the fail or
+ * not function correctly.
+ * @return The syscall implementation always returns a bool-convertable error code converting to true if the syscall
+ * failed or false if it didn't. Additionally it might pase additional values besides the error code, they will be set
+ * in the arguments struct. So the value can be read and used for further processing.
+ */
+ [[gnu::section(".user_text")]]
+ auto syscall(type syscall_number, arguments args = {}) -> response;
+
+} // namespace teachos::arch::context_switching::syscall
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_MAIN_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/syscall/syscall_enable.hpp b/arch/x86_64/pre/include/arch/context_switching/syscall/syscall_enable.hpp
new file mode 100644
index 0000000..8cb468a
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/syscall/syscall_enable.hpp
@@ -0,0 +1,18 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_SYSCALL_ENABLE_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_SYSCALL_ENABLE_HPP
+
+namespace teachos::arch::context_switching::syscall
+{
+ /**
+ * @brief Enables and sets up internal CPU registers to allow for syscall to function correctly and switch context
+ * temporarily back to the kernel level.
+ *
+ * @note Configures the Model Specific Register required by syscall (LSTAR, FMASK, STAR) with the correct values so
+ * that the syscall_handler is called and sets the System Call Extension bit on the EFER flags to allow for syscall
+ * to be used.
+ */
+ auto enable_syscall() -> void;
+
+} // namespace teachos::arch::context_switching::syscall
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_SYSCALL_ENABLE_HPP
diff --git a/arch/x86_64/pre/include/arch/context_switching/syscall/syscall_handler.hpp b/arch/x86_64/pre/include/arch/context_switching/syscall/syscall_handler.hpp
new file mode 100644
index 0000000..2e7bcd1
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/context_switching/syscall/syscall_handler.hpp
@@ -0,0 +1,18 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_SYSCALL_HANDLER_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_SYSCALL_HANDLER_HPP
+
+#include <cstdint>
+
+namespace teachos::arch::context_switching::syscall
+{
+ /**
+ * @brief Handler for SYSCALL instruction. Calls a specific implementation based
+ * on the register RAX.
+ *
+ * @return Returns with LEAVE, SYSRETQ
+ */
+ auto syscall_handler() -> void;
+
+} // namespace teachos::arch::context_switching::syscall
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_SYSCALL_HANDLER_HPP
diff --git a/arch/x86_64/pre/include/arch/exception_handling/assert.hpp b/arch/x86_64/pre/include/arch/exception_handling/assert.hpp
new file mode 100644
index 0000000..1286768
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/exception_handling/assert.hpp
@@ -0,0 +1,17 @@
+#ifndef TEACHOS_ARCH_X86_64_EXCEPTION_HANDLING_ASSERT_HPP
+#define TEACHOS_ARCH_X86_64_EXCEPTION_HANDLING_ASSERT_HPP
+
+namespace teachos::arch::exception_handling
+{
+ /**
+ * @brief Assert a condition to be true, if not do not continue
+ * execution of the code and print the given message to screen.
+ *
+ * @param condition Condition we want to be true or else halt execution.
+ * @param message Message that should be printed before halting the execution if the condition is not met.
+ */
+ auto assert(bool condition, char const * message) -> void;
+
+} // namespace teachos::arch::exception_handling
+
+#endif // TEACHOS_ARCH_X86_64_EXCEPTION_HANDLING_ASSERT_HPP
diff --git a/arch/x86_64/pre/include/arch/exception_handling/panic.hpp b/arch/x86_64/pre/include/arch/exception_handling/panic.hpp
new file mode 100644
index 0000000..6a2404c
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/exception_handling/panic.hpp
@@ -0,0 +1,23 @@
+#ifndef TEACHOS_ARCH_X86_64_EXCEPTION_HANDLING_PANIC_HPP
+#define TEACHOS_ARCH_X86_64_EXCEPTION_HANDLING_PANIC_HPP
+
+namespace teachos::arch::exception_handling
+{
+ /**
+ * @brief Print the given kernel panic message and then halt the system.
+ *
+ * @param reason Reason to print before halting the system.
+ */
+ [[noreturn]] auto panic(char const * reason) -> void;
+
+ /**
+ * @brief Print the given kernel panic message started by a given prefix and then halt the system.
+ *
+ * @param prefix Prefix to print before printing the reason.
+ * @param reason Reason to print before halting the system.
+ */
+ [[noreturn]] auto panic(char const * prefix, char const * reason) -> void;
+
+} // namespace teachos::arch::exception_handling
+
+#endif // TEACHOS_ARCH_X86_64_EXCEPTION_HANDLING_PANIC_HPP
diff --git a/arch/x86_64/pre/include/arch/interrupt_handling/generic_interrupt_handler.hpp b/arch/x86_64/pre/include/arch/interrupt_handling/generic_interrupt_handler.hpp
new file mode 100644
index 0000000..15b35c1
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/interrupt_handling/generic_interrupt_handler.hpp
@@ -0,0 +1,34 @@
+#ifndef TEACHOS_ARCH_X86_64_INTERRUPT_HANDLING_GENERIC_INTERRUPT_HANDLER_HPP
+#define TEACHOS_ARCH_X86_64_INTERRUPT_HANDLING_GENERIC_INTERRUPT_HANDLER_HPP
+
+#include <cstdint>
+
+namespace teachos::arch::interrupt_handling
+{
+ /**
+ * @brief Represents the CPU state during an interrupt.
+ *
+ * Some interrupts push an error code, while others do not. The full list
+ * of which vector number contains the error code can be found here: https://wiki.osdev.org/Exceptions
+ */
+ struct [[gnu::packed]] interrupt_frame
+ {
+ // uint64_t error_code; ///< Error code only pushed by some exceptions, therefore it is commented out.
+ uint64_t ip; ///< Instruction pointer at the time of the interrupt.
+ uint64_t cs; ///< Code segment selector indicating privilege level.
+ uint64_t flags; ///< CPU flags (RFLAGS) storing processor state.
+ uint64_t sp; ///< Stack pointer at the time of the interrupt.
+ uint64_t ss; ///< Stack segment selector, usually unused in 64-bit mode.
+ };
+
+ /**
+ * @brief Generic interrupt handler function.
+ *
+ * @param frame Pointer to the interrupt frame containing CPU state.
+ */
+ [[gnu::interrupt]]
+ auto generic_interrupt_handler(interrupt_frame * frame) -> void;
+
+} // namespace teachos::arch::interrupt_handling
+
+#endif // TEACHOS_ARCH_X86_64_INTERRUPT_HANDLING_GENERIC_INTERRUPT_HANDLER_HPP
diff --git a/arch/x86_64/pre/include/arch/kernel/cpu/call.hpp b/arch/x86_64/pre/include/arch/kernel/cpu/call.hpp
new file mode 100644
index 0000000..3c43304
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/kernel/cpu/call.hpp
@@ -0,0 +1,30 @@
+#ifndef TEACHOS_ARCH_X86_64_KERNEL_CPU_JMP_HPP
+#define TEACHOS_ARCH_X86_64_KERNEL_CPU_JMP_HPP
+
+#include "arch/context_switching/interrupt_descriptor_table/segment_selector.hpp"
+
+#include <cstdint>
+
+namespace teachos::arch::kernel::cpu
+{
+ /**
+ * @brief Far Pointer. Address to function located in another code segment.
+ */
+ struct far_pointer
+ {
+ void (*function)(); ///< Address of the function we want to call. (0-63)
+ context_switching::interrupt_descriptor_table::segment_selector
+ selector; ///< Segment selector pointing to the GDT entry we want to load into register CS. (64-79)
+ };
+
+ /**
+ * @brief Far call - A call to an instruction located in a different segment than the current code segment but at the
+ * same privilege level.
+ *
+ * @param pointer 64-bit operand size far pointer that we want to call.
+ */
+ auto call(far_pointer pointer) -> void;
+
+} // namespace teachos::arch::kernel::cpu
+
+#endif // TEACHOS_ARCH_X86_64_KERNEL_CPU_JMP_HPP
diff --git a/arch/x86_64/pre/include/arch/kernel/cpu/gdtr.hpp b/arch/x86_64/pre/include/arch/kernel/cpu/gdtr.hpp
new file mode 100644
index 0000000..68b950d
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/kernel/cpu/gdtr.hpp
@@ -0,0 +1,27 @@
+#ifndef TEACHOS_ARCH_X86_64_KERNEL_CPU_GDTR_HPP
+#define TEACHOS_ARCH_X86_64_KERNEL_CPU_GDTR_HPP
+
+#include "arch/context_switching/segment_descriptor_table/global_descriptor_table_pointer.hpp"
+
+#include <bitset>
+#include <cstdint>
+
+namespace teachos::arch::kernel::cpu
+{
+
+ /**
+ * @brief Returns the value in the GDTR register.
+ *
+ * @return Value of GDTR register.
+ */
+ auto store_global_descriptor_table() -> context_switching::segment_descriptor_table::global_descriptor_table_pointer;
+
+ /**
+ * @brief Loads the global_descriptor_table_pointer into the global descriptor table register (GDTR).
+ */
+ auto load_global_descriptor_table(
+ context_switching::segment_descriptor_table::global_descriptor_table_pointer const & gdt_pointer) -> void;
+
+} // namespace teachos::arch::kernel::cpu
+
+#endif // TEACHOS_ARCH_X86_64_KERNEL_CPU_GDTR_HPP
diff --git a/arch/x86_64/pre/include/arch/kernel/cpu/idtr.hpp b/arch/x86_64/pre/include/arch/kernel/cpu/idtr.hpp
new file mode 100644
index 0000000..cb800d0
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/kernel/cpu/idtr.hpp
@@ -0,0 +1,27 @@
+#ifndef TEACHOS_ARCH_X86_64_KERNEL_CPU_IDTR_HPP
+#define TEACHOS_ARCH_X86_64_KERNEL_CPU_IDTR_HPP
+
+#include "arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.hpp"
+
+#include <bitset>
+#include <cstdint>
+
+namespace teachos::arch::kernel::cpu
+{
+ /**
+ * @brief Returns the value in the IDTR register.
+ *
+ * @return Value of IDTR register.
+ */
+ auto store_interrupt_descriptor_table()
+ -> context_switching::interrupt_descriptor_table::interrupt_descriptor_table_pointer;
+
+ /**
+ * @brief Loads the interrupt_descriptor_table_pointer into the interrupt descriptor table register (IDTR).
+ */
+ auto load_interrupt_descriptor_table(
+ context_switching::interrupt_descriptor_table::interrupt_descriptor_table_pointer const & idt_pointer) -> void;
+
+} // namespace teachos::arch::kernel::cpu
+
+#endif // TEACHOS_ARCH_X86_64_KERNEL_CPU_IDTR_HPP
diff --git a/arch/x86_64/pre/include/arch/kernel/cpu/if.hpp b/arch/x86_64/pre/include/arch/kernel/cpu/if.hpp
new file mode 100644
index 0000000..48707dc
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/kernel/cpu/if.hpp
@@ -0,0 +1,21 @@
+#ifndef TEACHOS_ARCH_X86_64_KERNEL_CPU_IF_HPP
+#define TEACHOS_ARCH_X86_64_KERNEL_CPU_IF_HPP
+
+namespace teachos::arch::kernel::cpu
+{
+ /**
+ * @brief Sets the interrupt flag (IF) in the EFLAGS register.
+ * This allows the processor to respond to maskable hardware interrupts.
+ */
+ auto set_interrupt_flag() -> void;
+
+ /**
+ * @brief Clears the interrupt flag (IF) in the EFLAGS register.
+ * This will stop the processor to respond to maskable hardware interrupts and needs to be done before changing the
+ * Interrupt Descriptor Table with lidt.
+ */
+ auto clear_interrupt_flag() -> void;
+
+} // namespace teachos::arch::kernel::cpu
+
+#endif // TEACHOS_ARCH_X86_64_KERNEL_CPU_IF_HPP
diff --git a/arch/x86_64/pre/include/arch/kernel/cpu/msr.hpp b/arch/x86_64/pre/include/arch/kernel/cpu/msr.hpp
new file mode 100644
index 0000000..99d6378
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/kernel/cpu/msr.hpp
@@ -0,0 +1,64 @@
+#ifndef TEACHOS_ARCH_X86_64_KERNEL_CPU_NXE_HPP
+#define TEACHOS_ARCH_X86_64_KERNEL_CPU_NXE_HPP
+
+#include <bitset>
+#include <cstdint>
+
+namespace teachos::arch::kernel::cpu
+{
+ /**
+ * @brief Important flags that can be writen into the Extended Feature Enable Register (EFER).
+ *
+ * @note EFER is a model-specific register allowing to configure CPU extensions. Only the most important extensions
+ * are listed below, the rest are excluded for brevity. See https://en.wikipedia.org/wiki/Control_register#EFER for
+ * more information.
+ */
+ enum class efer_flags : uint64_t
+ {
+ SCE = 1UL << 0UL, ///< System Call Extensions.
+ LME = 1UL << 8UL, ///< Long Mode Enabled.
+ LMA = 1UL << 10UL, ///< Long Mode Active.
+ NXE = 1UL << 11UL, ///< No-Execute Enable.
+ SVME = 1UL << 12UL, ///< Secure Virtual Machine Enable.
+ LMSLE = 1UL << 13UL, ///< Long Mode Segment Limit Enable.
+ FFXSR = 1UL << 14UL, ///< Fast FXSAVE/FXSTOR.
+ TCE = 1UL << 15UL, ///< Translation Cache Extension.
+ };
+
+ /**
+ * @brief Reads a 64-bit from the Model-Specific Register (MSR).
+ *
+ * @note This function reads the value of an MSR specified by the given address. It combines the lower and upper
+ * 32-bits of the MSR value read using the 'rdmsr' instruction and returns it as a 64-bit unsigned integer.
+ *
+ * @param msr The address of the MSR to read.
+ * @return The 64-bit value read from the MSR.
+ */
+ auto read_msr(uint32_t msr) -> uint64_t;
+
+ /**
+ * @brief Writes a 64-bit value to a Model-Specific Register (MSR).
+ *
+ * @note This function writes a 64-bit value to the MSR specified by the given address.
+ * It splits the 64-bit value into two 32-bit parts and writes them using the
+ * `wrmsr` instruction.
+ *
+ * @param msr The address of the MSR to write to.
+ * @param new_value The 64-bit value to write to the MSR.
+ */
+ auto write_msr(uint32_t msr, uint64_t new_value) -> void;
+
+ /**
+ * @brief Sets a specific bit in the Extended Feature Enable Register (EFER), which is a Model-Specific Register
+ * (MSR).
+ *
+ * @note This function reads the current value of the EFER register, ORs the specified
+ * bit with the current value, and writes the updated value back to the EFER register.
+ *
+ * @param flag The flag to set in the EFER register.
+ */
+ auto set_efer_bit(efer_flags flag) -> void;
+
+} // namespace teachos::arch::kernel::cpu
+
+#endif // TEACHOS_ARCH_X86_64_KERNEL_CPU_NXE_HPP \ No newline at end of file
diff --git a/arch/x86_64/pre/include/arch/kernel/cpu/segment_register.hpp b/arch/x86_64/pre/include/arch/kernel/cpu/segment_register.hpp
new file mode 100644
index 0000000..a236452
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/kernel/cpu/segment_register.hpp
@@ -0,0 +1,97 @@
+#ifndef TEACHOS_ARCH_X86_64_KERNEL_CPU_SEGMENT_REGISTER_HPP
+#define TEACHOS_ARCH_X86_64_KERNEL_CPU_SEGMENT_REGISTER_HPP
+
+#include "arch/context_switching/interrupt_descriptor_table/segment_selector.hpp"
+
+namespace teachos::arch::kernel::cpu
+{
+ /**
+ * @brief Clear all Data Segment registers (DS / ES / FS / GS).
+ */
+ auto reload_data_segment_registers() -> void;
+
+ /**
+ * @brief Updates the value of the Data Segment Register (DS), Extra Segment Register (ES), Thread-Local Storage
+ * Registers (FS / GS).
+ *
+ * @note The Stack Segment Register (SS) value should also be updated, but the value can not be directly set in
+ * comparsion to the other registers. This is the case because the register is used for stack management and can not
+ * be directly changed, instead this has to be done by a special instruction. Therefore
+ * validate_data_segment_registers should only be called after set_code_segment_register has been called as well.
+ *
+ * @param data_segment Value that should be loaded into the registers.
+ */
+ auto set_data_segment_registers(context_switching::interrupt_descriptor_table::segment_selector data_segment) -> void;
+
+ /**
+ * @brief Returns the Segment Selector pointing to the Code Segment that has been loaded into the Code Segment
+ * Register (CS).
+ *
+ * @note The CS register can not be directly changed, instead a Far Return has to be executed to change it
+ *
+ * @return Segment Selector pointing to the currently loaded Code Segment.
+ */
+ [[gnu::section(".user_text")]]
+ auto read_code_segment_register() -> context_switching::interrupt_descriptor_table::segment_selector;
+
+ /**
+ * @brief Validates that all Data Segment Registers (DS / ES / FS / GS / SS) are the same as the given Data Segment
+ * and asserts and stops the application if they are not.
+ *
+ * @note This is only the case after set_code_segment_register has been executed as well, because it makes a far
+ * return that updates the SS register.
+ *
+ * @param data_segment Value that should be loaded into all Data Segment Registers.
+ */
+ auto validate_data_segment_registers(context_switching::interrupt_descriptor_table::segment_selector data_segment)
+ -> void;
+
+ /**
+ * @brief Validates that the Code Segment Register (CS) is the same as the given Code Segment
+ * and asserts and stops the application if they are not.
+ *
+ * @param code_segment Value that should be loaded into the Code Segment Register.
+ */
+ auto validate_code_segment_register(context_switching::interrupt_descriptor_table::segment_selector code_segment)
+ -> void;
+
+ /**
+ * @brief Simply forwards the call to validate_data_segment_registers and validate_code_segment_register and ensures
+ * that all Segment Registers, have been configured correctly.
+ *
+ * @note If all Segment Register have been set correctly the Context Switch using the set_code_segment_register method
+ * was successfull and the Privilege Level has been changed.
+ *
+ * @param data_segment Value that should be loaded into all Data Segment Registers.
+ * @param code_segment Value that should be loaded into the Code Segment Register.
+ */
+ auto validate_segment_registers(context_switching::interrupt_descriptor_table::segment_selector data_segment,
+ context_switching::interrupt_descriptor_table::segment_selector code_segment) -> void;
+
+ /**
+ * @brief Sets the value of the Code Segment Register (CS), this is achieved using a Far Return.
+ *
+ * @note The Far Return used by this method, will cause the context to switch, because we are changing from the
+ * current Code Segment and it's associated Privilege Level to another Code Segment. The given method will then be
+ * called in the new context and it should be possible to call validate_segment_registers, with the same values
+ * without assertions if the switch was successful.
+ *
+ * To achieve this Far Return we call IRETQ, which expects the stack to be defined a certain way to achieve that we:
+ * 1. Push the Data Segment Selector
+ * 2. Push the current Stack Pointer
+ * 3. Push Eflags
+ * 4. Push Code Segment Selector
+ * 5. Push Return Address
+ *
+ * @param data_segment Data Segment that should be loaded into the SS register.
+ * @param code_segment Code Segment that should be loaded into the CS register.
+ * @param address Function that we want to call in the new context created by the given Code Segment.
+ */
+ [[gnu::naked]]
+ auto set_code_segment_register(context_switching::interrupt_descriptor_table::segment_selector data_segment,
+ context_switching::interrupt_descriptor_table::segment_selector code_segment,
+ uint64_t address) -> void;
+
+} // namespace teachos::arch::kernel::cpu
+
+#endif // TEACHOS_ARCH_X86_64_KERNEL_CPU_SEGMENT_REGISTER_HPP
diff --git a/arch/x86_64/pre/include/arch/kernel/cpu/tr.hpp b/arch/x86_64/pre/include/arch/kernel/cpu/tr.hpp
new file mode 100644
index 0000000..7c856f1
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/kernel/cpu/tr.hpp
@@ -0,0 +1,24 @@
+#ifndef TEACHOS_ARCH_X86_64_KERNEL_CPU_TR_HPP
+#define TEACHOS_ARCH_X86_64_KERNEL_CPU_TR_HPP
+
+#include <bitset>
+#include <cstdint>
+
+namespace teachos::arch::kernel::cpu
+{
+
+ /**
+ * @brief Returns the value in the LTR register.
+ *
+ * @return Value of LTR register.
+ */
+ auto store_task_register() -> uint16_t;
+
+ /**
+ * @brief Loads the gdt offset to the tss segment descriptor into the task register (TR).
+ */
+ auto load_task_register(uint16_t gdt_offset) -> void;
+
+} // namespace teachos::arch::kernel::cpu
+
+#endif // TEACHOS_ARCH_X86_64_KERNEL_CPU_TR_HPP
diff --git a/arch/x86_64/pre/include/arch/kernel/halt.hpp b/arch/x86_64/pre/include/arch/kernel/halt.hpp
new file mode 100644
index 0000000..377acc0
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/kernel/halt.hpp
@@ -0,0 +1,13 @@
+#ifndef TEACHOS_ARCH_X86_64_KERNEL_HALT_HPP
+#define TEACHOS_ARCH_X86_64_KERNEL_HALT_HPP
+
+namespace teachos::arch::kernel
+{
+ /**
+ * @brief Halts the kernel execution, meaning any code after a call to this will not run anymore.
+ */
+ extern "C" [[noreturn]] auto halt() -> void;
+
+} // namespace teachos::arch::kernel
+
+#endif // TEACHOS_ARCH_X86_64_KERNEL_HALT_HPP
diff --git a/arch/x86_64/pre/include/arch/kernel/main.hpp b/arch/x86_64/pre/include/arch/kernel/main.hpp
new file mode 100644
index 0000000..a13e5f4
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/kernel/main.hpp
@@ -0,0 +1,13 @@
+#ifndef TEACHOS_ARCH_X86_64_KERNEL_MAIN_HPP
+#define TEACHOS_ARCH_X86_64_KERNEL_MAIN_HPP
+
+namespace teachos::arch::kernel
+{
+ /**
+ * @brief Initalizes the kernel system.
+ */
+ auto main() -> void;
+
+} // namespace teachos::arch::kernel
+
+#endif // TEACHOS_ARCH_X86_64_KERNEL_MAIN_HPP
diff --git a/arch/x86_64/pre/include/arch/memory/heap/bump_allocator.hpp b/arch/x86_64/pre/include/arch/memory/heap/bump_allocator.hpp
new file mode 100644
index 0000000..011f45c
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/memory/heap/bump_allocator.hpp
@@ -0,0 +1,48 @@
+#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_BUMP_ALLOCATOR_HPP
+#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_BUMP_ALLOCATOR_HPP
+
+#include "arch/memory/heap/heap_allocator.hpp"
+
+#include <atomic>
+#include <cstdint>
+
+namespace teachos::arch::memory::heap
+{
+ /**
+ * @brief Simple heap allocator, which allocates linearly and leaks all allocated memory, because it does not really
+ * deallocate anything.
+ */
+ struct bump_allocator : heap_allocator
+ {
+ /**
+ * @brief Constructor.
+ *
+ * @param heap_start Start of the allocatable heap area
+ * @param heap_end End of the allocatable heap area (Start + Size)
+ */
+ bump_allocator(std::size_t heap_start, std::size_t heap_end)
+ : heap_start{heap_start}
+ , heap_end{heap_end}
+ , next{heap_start}
+ {
+ // Nothing to do
+ }
+
+ auto allocate(std::size_t size) -> void * override;
+
+ /**
+ * @copybrief heap_allocator::deallocate
+ *
+ * @note Simply does nothing, because this allocator leaks all memory
+ */
+ auto deallocate(void * pointer) noexcept -> void override;
+
+ private:
+ std::size_t heap_start; ///< Start of the allocatable heap area
+ std::size_t heap_end; ///< End of the allocatable heap area
+ std::atomic_uint64_t next; ///< Current address, which is the start of still unused allocatable heap area
+ };
+
+} // namespace teachos::arch::memory::heap
+
+#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_BUMP_ALLOCATOR_HPP
diff --git a/arch/x86_64/pre/include/arch/memory/heap/global_heap_allocator.hpp b/arch/x86_64/pre/include/arch/memory/heap/global_heap_allocator.hpp
new file mode 100644
index 0000000..480b1d0
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/memory/heap/global_heap_allocator.hpp
@@ -0,0 +1,117 @@
+#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_GLOBAL_HEAP_ALLOCATOR_HPP
+#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_GLOBAL_HEAP_ALLOCATOR_HPP
+
+#include "arch/memory/heap/heap_allocator.hpp"
+#include "arch/memory/heap/user_heap_allocator.hpp"
+
+namespace teachos::arch::memory::heap
+{
+ /**
+ * @brief Possible types that should be constructed by the register_heap_allocator factory method.
+ * Creates the underlying heap allocator instance that is then used by all global allocations using new and delete
+ */
+ enum class heap_allocator_type : uint8_t
+ {
+ NONE, ///< Don't use any heap allocation implementation, this will result in all calls of new and delte halting
+ ///< further execution of the kernel
+ BUMP, ///< Use the bump allocator as the heap allocation implementation, be aware that using this allocator leaks
+ ///< memory, because there is no delete implementation
+ LINKED_LIST ///< Use the linked list allocator as the heap implementation, recommended because it does not leak
+ ///< memory
+ };
+
+ /**
+ * @brief Global instance of a heap allocator implementation created by the factory method pattern @see
+ * https://refactoring.guru/design-patterns/factory-method for more information.
+ *
+ * @note Can only be registered once and only once the kernel and the heap part of the kernel has been remapped
+ * successfully. If the instance is created before than the device will abort, because it acceses unmapped memory
+ * areas.
+ */
+ struct global_heap_allocator
+ {
+ /**
+ * @brief Registers the heap allocation implementation that should be used by the global heap allocator.
+ * Meaning all future calls to the global new or delete will be forwarded to the allocate and deallocate calls of
+ * the underlying heap allocation implementation
+ *
+ * @param new_type Type of the heap allocation implementation we want to instantiate
+ */
+ auto static register_heap_allocator(heap_allocator_type new_type) -> void;
+
+ /**
+ * @brief Allocates the given amount of memory and returns the pointer to the start of the allocatable memory area.
+ * Simply forwards the call to the allocate method of the registered heap_allocation implementation
+ *
+ * @param size Amount of bytes that should be allocated
+ * @return void* Pointer to the start of the allocatable memory area
+ */
+ auto static kmalloc(std::size_t size) -> void *;
+
+ /**
+ * @brief Deallocated all memory associated with the memory area starting from the given pointer address.
+ * Simply forwards the call to the deallocate method of the registered heap_allocation implementation
+ *
+ * @param pointer Previously allocated memory area, that should now be freed
+ */
+ auto static kfree(void * pointer) noexcept -> void;
+
+ /**
+ * @brief Allocates the given amount of memory and returns the pointer to the start of the allocatable memory area.
+ * Simply forwards the call to the allocate method of the registered heap_allocation implementation
+ *
+ * @param size Amount of bytes that should be allocated
+ * @return void* Pointer to the start of the allocatable memory area
+ */
+ [[gnu::section(".user_text")]]
+ auto static malloc(std::size_t size) -> void *;
+
+ /**
+ * @brief Deallocated all memory associated with the memory area starting from the given pointer address.
+ * Simply forwards the call to the deallocate method of the registered heap_allocation implementation
+ *
+ * @param pointer Previously allocated memory area, that should now be freed
+ */
+ [[gnu::section(".user_text")]]
+ auto static free(void * pointer) noexcept -> void;
+
+ private:
+ heap_allocator static * kernel_allocator_instance; ///< Instance used to allocate and deallocate kernel heap memory
+ [[gnu::section(".user_data")]] user_heap_allocator static *
+ user_allocator_instance; ///< Instance used to allocate and deallocate user heap memory
+
+ /**
+ * @brief Either returns the previously registered heap allocated or halts further execution
+ *
+ * @return Reference to the registered kernel heap allocation
+ */
+ auto static kernel() -> heap_allocator &;
+
+ /**
+ * @brief Either returns the previously registered heap allocated or halts further execution
+ *
+ * @return Reference to the registered user heap allocation
+ */
+ [[gnu::section(".user_text")]] auto static user() -> user_heap_allocator &;
+ };
+} // namespace teachos::arch::memory::heap
+
+[[gnu::section(".user_text")]]
+auto operator new(std::size_t size) -> void *;
+
+[[gnu::section(".user_text")]]
+auto operator delete(void * pointer) noexcept -> void;
+
+[[gnu::section(".user_text")]]
+auto operator delete(void * pointer, std::size_t size) noexcept -> void;
+
+[[gnu::section(".user_text")]]
+auto operator new[](std::size_t size) -> void *;
+
+[[gnu::section(".user_text")]]
+auto operator delete[](void * pointer) noexcept -> void;
+
+[[gnu::section(".user_text")]]
+auto operator delete[](void * pointer, std::size_t size) noexcept -> void;
+
+#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_GLOBAL_HEAP_ALLOCATOR_HPP
diff --git a/arch/x86_64/pre/include/arch/memory/heap/heap_allocator.hpp b/arch/x86_64/pre/include/arch/memory/heap/heap_allocator.hpp
new file mode 100644
index 0000000..6c25532
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/memory/heap/heap_allocator.hpp
@@ -0,0 +1,45 @@
+#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_HEAP_ALLOCATOR_HPP
+#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_HEAP_ALLOCATOR_HPP
+
+#include <cstdint>
+
+namespace teachos::arch::memory::heap
+{
+ constexpr std::size_t KERNEL_HEAP_START = 0x1'0000'0000;
+ constexpr std::size_t KERNEL_HEAP_SIZE = 100 * 1024;
+ constexpr std::size_t USER_HEAP_START = 0x1'0001'9000; // Starts directly after kernel heap
+ constexpr std::size_t USER_HEAP_SIZE = 100 * 1024;
+
+ /**
+ * @brief Heap allocator interface containing methods required to allocate and deallocate heap memory areas
+ */
+ struct heap_allocator
+ {
+ /**
+ * @brief Virtual default destructor, created to ensure that if a pointer to this class is used and deleted, we will
+ * also call the derived base class destructor. Deleting a base class destructor that does not have a virtual
+ * destructor is undefined behaviour, because the derived class destructor originally instantiated with new is never
+ * called. This can cause potential memory leaks, because derived classes can not clean up their internal members as
+ * expected and instead simply leak them
+ */
+ virtual ~heap_allocator() {}
+
+ /**
+ * @brief Allocates the given amount of memory and returns the pointer to the start of the allocatable memory area
+ *
+ * @param size Amount of bytes that should be allocated
+ * @return void* Pointer to the start of the allocatable memory area
+ */
+ virtual auto allocate(std::size_t size) -> void * = 0;
+
+ /**
+ * @brief Deallocates all memory associated with the given pointer address.
+ * Simply deallocates the amount of memory created with the corresponding call to allocate
+ *
+ * @param pointer Previously allocated memory area, that should now be freed
+ */
+ virtual auto deallocate(void * pointer) noexcept -> void = 0;
+ };
+} // namespace teachos::arch::memory::heap
+
+#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_HEAP_ALLOCATOR_HPP
diff --git a/arch/x86_64/pre/include/arch/memory/heap/linked_list_allocator.hpp b/arch/x86_64/pre/include/arch/memory/heap/linked_list_allocator.hpp
new file mode 100644
index 0000000..bb8b526
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/memory/heap/linked_list_allocator.hpp
@@ -0,0 +1,123 @@
+#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_LINKED_LIST_ALLOCATOR_HPP
+#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_LINKED_LIST_ALLOCATOR_HPP
+
+#include "arch/memory/heap/heap_allocator.hpp"
+#include "arch/memory/heap/memory_block.hpp"
+
+#include <kstd/mutex.hpp>
+
+namespace teachos::arch::memory::heap
+{
+ /**
+ * @brief Sorted by address list of memory holes (free memory). Uses free holes itself to save the information,
+ * containing the size and pointer to the next hole. Resulting in a singly linked list.
+ */
+ struct linked_list_allocator : heap_allocator
+ {
+ /**
+ * @brief Constructor.
+ *
+ * @param heap_start Start of the allocatable heap area
+ * @param heap_end End of the allocatable heap area (Start + Size)
+ */
+ linked_list_allocator(std::size_t heap_start, std::size_t heap_end);
+
+ /**
+ * @copybrief heap_allocator::allocate
+ *
+ * @note The specified size is used to find a free memory block with the exact same size, meaning we can remove that
+ * free memory block from the free list and simply return its address. Or it has to be big enough to hold the size
+ * and alteast enough memory for another free memory block entry (16 bytes). If the amount of memory of that free
+ * memory block is in between we cannot use it for our allocation, because we could only return it to the user, but
+ * the additional bytes, could not be used to create a free memory block. Additionaly the user couldn't know
+ * they received more memory than wanted. Therefore the memory would simply be unused and because it is neither
+ * allocated nor deallocated would never be indexed by the free memory list. We would therefore permanently loose
+ * that memory, to prevent that allocation into free memory blocks like that are impossible.
+ */
+ auto allocate(std::size_t size) -> void * override;
+
+ auto deallocate(void * pointer) noexcept -> void override;
+
+ private:
+ /**
+ * @brief Returns the smallest allocatable block of heap memory.
+ *
+ * @return Smallest allocatable block of heap memory.
+ */
+ constexpr auto min_allocatable_size() -> std::size_t
+ {
+ return sizeof(memory_block);
+ }
+
+ /**
+ * @brief Removes a free memory block from the free list and returns its address so the caller can allocate into it.
+ *
+ * @param previous_block Free memory block before the block to allocate in our heap memory. Was to small to
+ * allocate the required size into.
+ * @param current_block Free memory block we want to remove from the free list and return for the allocation.
+ *
+ * @return Previous start address of the memory block we removed, because it can now be used for the allocation.
+ */
+ auto remove_free_memory_block(memory_block * previous_block, memory_block * current_block) -> void *;
+
+ /**
+ * @brief Splits the given free memory block into two, where the latter block keeps being free and the first
+ * part will be used for the allocation.
+ *
+ * @param previous_block Free memory block before the block to allocate in our heap memory. Was to small to
+ * allocate the required size into.
+ * @param current_block Free memory block we want to split into a size part for the allocation and the rest for
+ * future allocations.
+ * @param size Size we want to allocate at the start of the free memory block.
+ *
+ * @return Previous start address of the memory block we just split, because it can now be used for the allocation.
+ */
+ auto split_free_memory_block(memory_block * previous_block, memory_block * current_block, std::size_t size)
+ -> void *;
+
+ /**
+ * @brief Removes a free memory block from the free list and returns its address so the caller can allocate into it.
+ *
+ * @param previous_block Free memory block before the block to allocate in our heap memory. Was to small to
+ * allocate the required size into.
+ * @param current_block Free memory block we want to remove from the free list and return for the allocation.
+ * @param new_block Replaces the current block with the given new block can be nullptr, meaning the free list will
+ * end here.
+ *
+ * @return Previous start address of the memory block we removed, because it can now be used for the allocation.
+ */
+ auto replace_free_memory_block(memory_block * previous_block, memory_block * current_block,
+ memory_block * new_block) -> void *;
+
+ /**
+ * @brief Combines multiple free memory blocks into one if they are adjacent.
+ *
+ * @note The internal algorithm for recombination functions like this:
+ * 1. Check if there is even any memory left, if not the first entry of our linked list should be a nullptr and
+ * we can therefore set the first entry to our newly created entry. This entry is created in the now deallocated
+ * memory area.
+ * 2. If there are more blocks but neither the previous nor the current block are adjacent, we simply create a
+ * new free memory block of the given size and set the previous next to our block and the next of our block to
+ * the current block.
+ * 3. If the current block is adjacent the start address of the newly created block stays the same, but the size
+ * increases by the amount in the current memory block header. After reading it we also clear the header.
+ * 4. If the previous block is adjacent the size of the previous block simply increases to include the given
+ * size as well.
+ * 5. If the previous block is directly in our start address, so they overlap then it has to mean some or all of
+ * the region we are trying to deallocate has been freed before. Which would result in a double free therefore
+ * we halt the execution of the program.
+ *
+ * @param previous_block Free memory block before the block to deallocate in our heap memory.
+ * @param current_block Free memory block after the block to deallocate in our heap memory.
+ * @param pointer Block to deallocate.
+ * @param size Size of the block we want to deallocate.
+ */
+ auto coalesce_free_memory_block(memory_block * previous_block, memory_block * current_block, void * pointer,
+ std::size_t size) -> void;
+
+ memory_block * first; ///< First free entry in our memory.
+ kstd::mutex mutex; ///< Mutex to ensure only one thread calls allocate or deallocate at once.
+ };
+} // namespace teachos::arch::memory::heap
+
+#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_LINKED_LIST_ALLOCATOR_HPP
diff --git a/arch/x86_64/pre/include/arch/memory/heap/memory_block.hpp b/arch/x86_64/pre/include/arch/memory/heap/memory_block.hpp
new file mode 100644
index 0000000..9d1fb02
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/memory/heap/memory_block.hpp
@@ -0,0 +1,39 @@
+#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_MEMORY_BLOCK_HPP
+#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_MEMORY_BLOCK_HPP
+
+#include <cstdint>
+
+namespace teachos::arch::memory::heap
+{
+ /**
+ * @brief Block containing free memory, pointing to the next free hole (nullptr) if there is none.
+ * Forms a singly linked list of free memory blocks that we can callocate memory into.
+ */
+ struct memory_block
+ {
+ /**
+ * @brief Constructor. Clears all memory from the place it was allocated until the end (address +
+ * size).
+ *
+ * @param size Amount of free memory of this specific hole.
+ * @param next Optional pointer to the next free memory.
+ */
+ [[gnu::section(".user_text")]]
+ memory_block(std::size_t size, memory_block * next);
+
+ /**
+ * @brief Destructor. Clears all internal memory.
+ *
+ * @note Used so the memory can be reused to construct other classes into, without having the old values.
+ * Required because we cannot call delete, because it causes "undefined reference to `sbrk`".
+ */
+ [[gnu::section(".user_text")]]
+ ~memory_block();
+
+ std::size_t size; ///< Amount of free memory this hole contains, has to always be atleast 16 bytes to hold the
+ ///< size variable and the pointer to the next hole.
+ memory_block * next; ///< Optional pointer to the next free memory, holds nullptr if there is none.
+ };
+} // namespace teachos::arch::memory::heap
+
+#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_MEMORY_BLOCK_HPP
diff --git a/arch/x86_64/pre/include/arch/memory/heap/user_heap_allocator.hpp b/arch/x86_64/pre/include/arch/memory/heap/user_heap_allocator.hpp
new file mode 100644
index 0000000..15d8574
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/memory/heap/user_heap_allocator.hpp
@@ -0,0 +1,153 @@
+#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_USER_HEAP_ALLOCATOR_HPP
+#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_USER_HEAP_ALLOCATOR_HPP
+
+#include "arch/memory/heap/memory_block.hpp"
+
+// #include <kstd/mutex.hpp>
+#include <kstd/mutex.hpp>
+
+#include <optional>
+
+namespace teachos::arch::memory::heap
+{
+ /**
+ * @brief Sorted by address list of memory holes (free memory). Uses free holes itself to save the information,
+ * containing the size and pointer to the next hole. Resulting in a singly linked list.
+ */
+ struct user_heap_allocator
+ {
+ /**
+ * @brief Constructor.
+ */
+ user_heap_allocator() = default;
+
+ /**
+ * @copybrief heap_allocator::allocate
+ *
+ * @note The specified size is used to find a free memory block with the exact same size, meaning we can remove that
+ * free memory block from the free list and simply return its address. Or it has to be big enough to hold the size
+ * and alteast enough memory for another free memory block entry (16 bytes). If the amount of memory of that free
+ * memory block is in between we cannot use it for our allocation, because we could only return it to the user, but
+ * the additional bytes, could not be used to create a free memory block. Additionaly the user couldn't know
+ * they received more memory than wanted. Therefore the memory would simply be unused and because it is neither
+ * allocated nor deallocated would never be indexed by the free memory list. We would therefore permanently loose
+ * that memory, to prevent that allocation into free memory blocks like that are impossible.
+ */
+ [[gnu::section(".user_text")]]
+ auto allocate(std::size_t size) -> void *;
+
+ /**
+ * @copybrief heap_allocator::deallocate
+ */
+ [[gnu::section(".user_text")]]
+ auto deallocate(void * pointer) noexcept -> void;
+
+ private:
+ /**
+ * @brief Returns the smallest allocatable block of heap memory.
+ *
+ * @return Smallest allocatable block of heap memory.
+ */
+ [[gnu::section(".user_text")]] constexpr auto min_allocatable_size() -> std::size_t
+ {
+ return sizeof(memory_block);
+ }
+
+ /**
+ * @brief Checks if the given memory block is big enough and if it is allocates into the current block.
+ *
+ * @note Adjusts the link of the previous memory block to the new smaller remaining block. If the allocation used
+ * the complete block instead the previous block will point to the next block of the current memroy block that was
+ * used for the allocation.
+ *
+ * @return Allocated usable memory area.
+ */
+ [[gnu::section(".user_text")]] auto
+ allocate_into_memory_block_if_big_enough(memory_block * current, memory_block * previous, std::size_t total_size)
+ -> std::optional<void *>;
+
+ /**
+ * @brief Special functionality fo the user heap allocator. Which will result in it being expanded by a syscall with
+ * addtionally 100 KiB, which are mapped into the page table. Will always work until there is no physical memory
+ * left.
+ *
+ * @return Start of the newly with syscall allocated free memory block. Nullptr if the syscall failed.
+ */
+ [[gnu::section(".user_text")]] auto expand_heap_if_full() -> memory_block *;
+
+ /**
+ * @brief Removes a free memory block from the free list and returns its address so the caller can allocate into it.
+ *
+ * @param previous_block Free memory block before the block to allocate in our heap memory. Was to small to
+ * allocate the required size into.
+ * @param current_block Free memory block we want to remove from the free list and return for the allocation.
+ *
+ * @return Previous start address of the memory block we removed, because it can now be used for the allocation.
+ */
+ [[gnu::section(".user_text")]]
+ auto remove_free_memory_block(memory_block * previous_block, memory_block * current_block) -> void *;
+
+ /**
+ * @brief Splits the given free memory block into two, where the latter block keeps being free and the first
+ * part will be used for the allocation.
+ *
+ * @param previous_block Free memory block before the block to allocate in our heap memory. Was to small to
+ * allocate the required size into.
+ * @param current_block Free memory block we want to split into a size part for the allocation and the rest for
+ * future allocations.
+ * @param size Size we want to allocate at the start of the free memory block.
+ *
+ * @return Previous start address of the memory block we just split, because it can now be used for the allocation.
+ */
+ [[gnu::section(".user_text")]]
+ auto split_free_memory_block(memory_block * previous_block, memory_block * current_block, std::size_t size)
+ -> void *;
+
+ /**
+ * @brief Removes a free memory block from the free list and returns its address so the caller can allocate into it.
+ *
+ * @param previous_block Free memory block before the block to allocate in our heap memory. Was to small to
+ * allocate the required size into.
+ * @param current_block Free memory block we want to remove from the free list and return for the allocation.
+ * @param new_block Replaces the current block with the given new block can be nullptr, meaning the free list will
+ * end here.
+ *
+ * @return Previous start address of the memory block we removed, because it can now be used for the allocation.
+ */
+ [[gnu::section(".user_text")]]
+ auto replace_free_memory_block(memory_block * previous_block, memory_block * current_block,
+ memory_block * new_block) -> void *;
+
+ /**
+ * @brief Combines multiple free memory blocks into one if they are adjacent.
+ *
+ * @note The internal algorithm for recombination functions like this:
+ * 1. Check if there is even any memory left, if not the first entry of our linked list should be a nullptr and
+ * we can therefore set the first entry to our newly created entry. This entry is created in the now deallocated
+ * memory area.
+ * 2. If there are more blocks but neither the previous nor the current block are adjacent, we simply create a
+ * new free memory block of the given size and set the previous next to our block and the next of our block to
+ * the current block.
+ * 3. If the current block is adjacent the start address of the newly created block stays the same, but the size
+ * increases by the amount in the current memory block header. After reading it we also clear the header.
+ * 4. If the previous block is adjacent the size of the previous block simply increases to include the given
+ * size as well.
+ * 5. If the previous block is directly in our start address, so they overlap then it has to mean some or all of
+ * the region we are trying to deallocate has been freed before. Which would result in a double free therefore
+ * we halt the execution of the program.
+ *
+ * @param previous_block Free memory block before the block to deallocate in our heap memory.
+ * @param current_block Free memory block after the block to deallocate in our heap memory.
+ * @param pointer Block to deallocate.
+ * @param size Size of the block we want to deallocate.
+ */
+ [[gnu::section(".user_text")]]
+ auto coalesce_free_memory_block(memory_block * previous_block, memory_block * current_block, void * pointer,
+ std::size_t size) -> void;
+
+ memory_block * first = {}; ///< First free entry in our memory.
+ kstd::mutex mutex = {}; ///< Mutex to ensure only one thread calls allocate or deallocate at once.
+ };
+} // namespace teachos::arch::memory::heap
+
+#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_USER_HEAP_ALLOCATOR_HPP
diff --git a/arch/x86_64/pre/include/arch/user/main.hpp b/arch/x86_64/pre/include/arch/user/main.hpp
new file mode 100644
index 0000000..c168a1f
--- /dev/null
+++ b/arch/x86_64/pre/include/arch/user/main.hpp
@@ -0,0 +1,16 @@
+#ifndef TEACHOS_ARCH_X86_64_USER_MAIN_HPP
+#define TEACHOS_ARCH_X86_64_USER_MAIN_HPP
+
+namespace teachos::arch::user
+{
+ /**
+ * @brief User Main method. If this method finishes there is no code left to run and the whole OS will shut down.
+ * Additionally this main method is executed at Ring 3 and accessing CPU Registers or Kernel level functionality can
+ * only be done over syscalls and not directly anymore.
+ */
+ [[gnu::section(".user_text")]]
+ auto main() -> void;
+
+} // namespace teachos::arch::user
+
+#endif // TEACHOS_ARCH_X86_64_USER_MAIN_HPP