Advertisement
My-Bad-2

gdt.cpp

Nov 1st, 2023
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | Source Code | 0 0
  1. namespace system {
  2. namespace gdt {
  3. enum selector {
  4.     Null = 0,
  5.     Kernel_Code = 1,
  6.     Kernel_Data = 2,
  7.     User_Data = 3,
  8.     User_Code = 4,
  9.     Tss = 5,
  10. };
  11.  
  12. enum flags : std::uint32_t {
  13.     Segment = 0b00010000,
  14.     Present = 0b10000000,
  15.     User = 0b01100000,
  16.     Executable = 0b00001000,
  17.     Read_Write = 0b00000010,
  18. };
  19.  
  20. enum granularity {
  21.     Long_Mode = 0b0010,
  22.     Flags = 0b1100,
  23. };
  24.  
  25. struct [[gnu::packed]] tss {
  26.     std::uint32_t reserved;
  27.     std::array<uint64_t, 3> rsp;
  28.     std::uint64_t reserved0;
  29.     std::array<uint64_t, 7> ist;
  30.     std::uint32_t reserved1;
  31.     std::uint32_t reserved2;
  32.     std::uint16_t reserved3;
  33.     std::uint16_t iopb_offset;
  34. };
  35.  
  36. struct [[gnu::packed]] gdt_entry {
  37.     std::uint16_t limit_low;
  38.     std::uint16_t base_low;
  39.     std::uint8_t base_mid;
  40.     std::uint8_t flags;
  41.     std::uint8_t limit_high : 4;
  42.     std::uint8_t granularity : 4;
  43.     std::uint8_t base_high;
  44.  
  45.     constexpr gdt_entry() : gdt_entry(0, 0, 0, 0) {}
  46.  
  47.     constexpr gdt_entry(std::uint8_t flags, std::uint8_t granularity)
  48.         : gdt_entry(0, 0, flags, granularity) {}
  49.  
  50.     constexpr gdt_entry(std::uint32_t base, std::uint32_t limit,
  51.                         std::uint8_t flags, std::uint8_t granularity)
  52.         : limit_low(static_cast<uint16_t>(limit & 0xFFFF)),
  53.           base_low(static_cast<uint16_t>(base & 0xFFFF)),
  54.           base_mid(static_cast<uint8_t>((base >> 16) & 0xFF)),
  55.           flags(flags),
  56.           limit_high((limit >> 16) & 0x0F),
  57.           granularity(granularity),
  58.           base_high(static_cast<uint8_t>((base >> 24) & 0xFF)) {}
  59. };
  60.  
  61. struct [[gnu::packed]] gdt_tss_entry {
  62.     std::uint16_t len;
  63.     std::uint16_t base_low;
  64.     std::uint8_t base_mid;
  65.     std::uint8_t flags1;
  66.     std::uint8_t flags2;
  67.     std::uint8_t base_high;
  68.     std::uint32_t base_upper;
  69.     std::uint32_t reserved;
  70.  
  71.     constexpr gdt_tss_entry() = default;
  72.  
  73.     gdt_tss_entry(const tss& _tss)
  74.         : len(sizeof(tss)),
  75.           base_low(reinterpret_cast<std::uintptr_t>(&_tss) & 0xFFFF),
  76.           base_mid((reinterpret_cast<uintptr_t>(&_tss) >> 16) & 0xFF),
  77.           flags1(0b10001001),
  78.           flags2(0),
  79.           base_high((reinterpret_cast<uintptr_t>(&_tss) >> 24) & 0xFF),
  80.           base_upper(reinterpret_cast<uintptr_t>(&_tss) >> 32),
  81.           reserved() {}
  82. };
  83.  
  84. struct [[gnu::packed]] gdt {
  85.     enum { Length = 6 };
  86.  
  87.     std::array<gdt_entry, Length - 1> entries;
  88.     gdt_tss_entry tss_entry;
  89.  
  90.     gdt(const tss& _tss);
  91. };
  92.  
  93. struct [[gnu::packed]] gdt_descriptor {
  94.     std::uint16_t limit;
  95.     std::uint64_t base;
  96.  
  97.     gdt_descriptor(const gdt& base)
  98.         : limit(sizeof(gdt) - 1),
  99.           base(reinterpret_cast<std::uintptr_t>(&base)) {}
  100.  
  101.     void load() const { gdt_load(this); }
  102. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement