Advertisement
Guest User

Untitled

a guest
Sep 4th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. gdt.h:
  2. #include <types.h>
  3.  
  4. struct gdt_seg_entry {
  5. uint16_t limit;
  6. uint16_t offset1;
  7. uint8_t offset2;
  8. uint8_t access;
  9. uint8_t limitflags;
  10. uint8_t offset3;
  11. } __attribute__((packed));
  12. typedef struct gdt_seg_entry gdt_seg_entry;
  13.  
  14. struct gdt_tss_entry {
  15. uint16_t limit;
  16. uint16_t offset1;
  17. uint8_t offset2;
  18. uint8_t access;
  19. uint8_t limitflags;
  20. uint8_t offset3;
  21. uint32_t offset4;
  22. uint32_t reserved;
  23. } __attribute__((packed));
  24. typedef struct gdt_tss_entry gdt_tss_entry;
  25.  
  26. struct gdt_p {
  27. uint16_t size;
  28. uint64_t offset;
  29. } __attribute__((packed));
  30. typedef struct gdt_p gdt_p;
  31.  
  32. void add_gdt_entry(uint32_t num, uint32_t offset, uint32_t limit, uint8_t access, uint8_t flags);
  33. void add_gdt_tss(uint32_t num, uint64_t offset, uint32_t limit, uint8_t access, uint8_t flags);
  34. void load_gdt(gdt_p* pointer);
  35. void init_gdt(void);
  36.  
  37. gdt.c:
  38. #include <gdt.h>
  39. #include <tss.h>
  40.  
  41. gdt_seg_entry gdt[6];
  42. gdt_p gdt_pointer;
  43.  
  44. void add_gdt_entry(uint32_t num, uint32_t offset, uint32_t limit, uint8_t access, uint8_t flags)
  45. {
  46. gdt[num].offset1 = offset & 0xffff;
  47. gdt[num].offset2 = (offset >> 16) & 0xff;
  48. gdt[num].offset3 = (uint8_t)(offset >> 24) & 0xff;
  49. gdt[num].limit = limit & 0xffff;
  50. gdt[num].limitflags = (limit >> 16) & 0xf;
  51. gdt[num].access = access;
  52. gdt[num].limitflags = flags << 4;
  53. }
  54.  
  55. void add_gdt_tss(uint32_t num, uint64_t offset, uint32_t limit, uint8_t access, uint8_t flags)
  56. {
  57. add_gdt_entry(num, offset & 0xffffffff, limit, access, flags);
  58. }
  59.  
  60. void load_gdt(gdt_p* pointer)
  61. {
  62. __asm__ volatile ("lgdt (%0);" : : "r"(pointer) : );
  63. }
  64.  
  65. void init_gdt()
  66. {
  67. gdt_pointer.offset = (uint64_t)&gdt;
  68. gdt_pointer.size = sizeof(gdt) - 1;
  69.  
  70. add_gdt_entry(0, 0, 0, 0, 0);
  71. add_gdt_entry(1, 0, 0xfffff, 0x9a, 0xa);
  72. add_gdt_entry(2, 0, 0xfffff, 0x92, 0xc);
  73. add_gdt_entry(3, 0, 0xfffff, 0xfa, 0xa);
  74. add_gdt_entry(4, 0, 0xfffff, 0xf2, 0xc);
  75. add_gdt_tss(5, (uint64_t)&tss, sizeof(tss_type), 0x89, 0);
  76.  
  77. load_gdt(&gdt_pointer);
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement