From d8a8efe3e8d90ec83069d1c934ff319626e87a2d Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 27 Feb 2025 10:13:35 +0000 Subject: add descriptor_table access_byte --- .../descriptor_table/access_byte.hpp | 87 ++++++++++++++++++++++ .../descriptor_table/access_bytes.hpp | 0 2 files changed, 87 insertions(+) create mode 100644 arch/x86_64/include/arch/context_switching/descriptor_table/access_byte.hpp delete mode 100644 arch/x86_64/include/arch/context_switching/descriptor_table/access_bytes.hpp (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/context_switching/descriptor_table/access_byte.hpp b/arch/x86_64/include/arch/context_switching/descriptor_table/access_byte.hpp new file mode 100644 index 0000000..b3bce11 --- /dev/null +++ b/arch/x86_64/include/arch/context_switching/descriptor_table/access_byte.hpp @@ -0,0 +1,87 @@ + +#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_DESCRIPTOR_TABLE_POINTERS_HPP +#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_DESCRIPTOR_TABLE_POINTERS_HPP + +#include +#include +#include + +namespace teachos::arch::context_switching::descriptor_table +{ + enum class privilege_level + { + KERNEL = 0, + ADMIN = 1, + PRIVILEGED_USER = 2, + USER = 3 + }; + + /// @brief Defines the access byte of a Descriptor Table + struct access_byte + { + /** + * @brief Possible set bits in our underlying std::bitset and the meaning when they are set. + */ + enum bitset : uint8_t + { + PRESENT = 1U << 0U, ///< Present bit; Allows an entry to refer to a valid segment. + ///< Must be set (1) for any valid segment. + PRIVILEGE_LEVEL = 1U << 1U | 1 << 2U, ///< Descriptor privilege level field. + ///< Contains the CPU Privilege level of the segment. + DESCRIPTOR_TYPE = 1U << 3U, ///< Defines a system segment (if 0) or a code/data segment (if 1). + EXECUTABLE = 1U << 4U, ///< Defines a data segment (if 0) or a code segment which can be executed from (if 1). + CONFORMING = 1U << 5U, ///< For data selectors: Direction bit + ///< Grows up (if 0), grows down (if 1) + ///< For code selectors: Conforming bit + ///< Code can only be executed from the DPL ring (if 0) + ///< Code can be executed from equal or lower DPL ring (if 1) + READ_WRITE = 1U << 6U, ///< For code segments: Readable bit + ///< For data segments: Writeable bit (read is always allowed) + ACCESSED = 1U << 7U ///< Set, when the segment is accessed. If GDT descriptor is stored in read only pages and + ///< this bit is set to 0, the CPU trying to set this bit will trigger a page fault. + }; + + /** + * @brief Constructor. + * + * @param flags Actual value read from the elf section header, which should be converted into a std::bitset, to + * allow reading the state of single bits more easily. + */ + explicit access_byte(uint8_t flags) + : flags(flags) + { + // Nothing to do + } + + /** + * @brief Checks if the given std::bitset is a subset or equivalent to the underlying std::bitset. + * + * @note Meaning that all bits that are set in the given std::bitset also have to be set in the underlyng + * std::bitset. Any additional bits that are set are not relevant. + * + * @param other Flags that we want to compare against and check if the underlying std::bitset has the same bits set. + * @return Whether the given flags are a subset or equivalent with the underlying std::bitset. + */ + auto contains_flags(std::bitset<8U> other) const -> bool; + + /** + * @brief Returns the privilege level of the current access_byte + * + * @return privilege_level + */ + auto get_privilege_level() const -> privilege_level; + + /** + * @brief Allows to compare the underlying std::bitset of two instances. + * + * @param other Other instance that we want to compare with. + * @return Whether the underlying std::bitset of both types is the same. + */ + auto operator==(access_byte const & other) const -> bool = default; + + private: + std::bitset<8U> flags; ///< Underlying bitset used to read the flags from + }; +} // namespace teachos::arch::context_switching::descriptor_table + +#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_DESCRIPTOR_TABLE_POINTERS_HPP \ No newline at end of file diff --git a/arch/x86_64/include/arch/context_switching/descriptor_table/access_bytes.hpp b/arch/x86_64/include/arch/context_switching/descriptor_table/access_bytes.hpp deleted file mode 100644 index e69de29..0000000 -- cgit v1.2.3