Advertisement
My-Bad-2

idt.hpp

Nov 1st, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | Source Code | 0 0
  1. enum class type {
  2.     User = 0x60,
  3.     Gate = 0x8E,
  4.     Trap = 0xEF,
  5. };
  6.  
  7. struct [[gnu::packed]] idt_entry {
  8.     std::uint16_t offset_low = 0;
  9.     std::uint16_t code_segment = 0;
  10.     std::uint8_t ist = 0;
  11.     std::uint8_t attributes = 0;
  12.     std::uint16_t offset_mid = 0;
  13.     std::uint32_t offset_high = 0;
  14.     std::uint32_t zero = 0;
  15.  
  16.     idt_entry() = default;
  17.  
  18.     idt_entry(std::uintptr_t handler, uint8_t ist, type attributes)
  19.         : offset_low(static_cast<uint16_t>(handler)),
  20.           code_segment(gdt::Kernel_Code * 8),
  21.           ist(ist),
  22.           attributes(static_cast<uint8_t>(attributes)),
  23.           offset_mid(static_cast<uint16_t>(handler >> 16)),
  24.           offset_high(static_cast<uint32_t>(handler >> 32)),
  25.           zero(0) {}
  26. };
  27.  
  28. struct [[gnu::packed, gnu::aligned(0x10)]] idt {
  29.     enum { Length = 256 };
  30.     // standard STl array
  31.     std::array<idt_entry, Length> entries{};
  32.  
  33.     idt() {}
  34. };
  35.  
  36. struct [[gnu::packed]] idt_descriptor {
  37.     std::uint16_t limit;
  38.     std::uint64_t base;
  39.  
  40.     idt_descriptor(std::uint16_t limit, std::uint64_t base)
  41.         : limit(limit), base(base) {}
  42.  
  43.     idt_descriptor(idt& _idt)
  44.         : limit(sizeof(idt) - 1),
  45.           base(reinterpret_cast<std::uint64_t>(&_idt)) {}
  46.  
  47.     void load() { asm volatile("lidt %0" ::"memory"(*this)); }
  48. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement