Advertisement
Guest User

gdt.c

a guest
Sep 1st, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include "kstdio.h"
  2. #include "include/gdt.h"
  3.  
  4.  
  5. GDTEntry g_GDT[GTD_DESC_COUNT];
  6. GDT_PTR g_GDT_PTR;
  7. //reserved for future use
  8. int g_GdtEntriesCount = 0;
  9. void SetGDTEntry(int index, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran) {
  10.  
  11.    
  12.  
  13.     GDTEntry *this = &g_GDT[index];
  14.     g_GdtEntriesCount++;
  15.     this->LimitLow = limit & 0xFFFF;
  16.     this->BaseLow = base & 0xFFFF;
  17.     this->BaseMiddle = (base >> 16) & 0xFF;
  18.     this->Access = access;
  19.  
  20.     this->FlagsLimitHi = (limit >> 16) & 0x0F;
  21.     this->FlagsLimitHi = this->FlagsLimitHi | (gran & 0xF0);
  22.  
  23.     this->BaseHigh = (base >> 24 & 0xFF);
  24.  
  25.      
  26.     debugf("\n\nGDT[%d]: BASE: 0x%x LIMIT: 0x%x ACCESS: 0x%x GRAN: 0x%x\n", index,
  27.             base, limit, access, gran);
  28.    
  29. }
  30.  
  31.  
  32. //todo implement TSS next and interrupts
  33.  
  34. void gdt_init(void) {
  35.    
  36.     debugf("\n=====GDT DUMP BEGIN=====\n");
  37.    
  38.     g_GDT_PTR.limit = sizeof(g_GDT) - 1;
  39.     g_GDT_PTR.base_address = (uint64_t)g_GDT;
  40. //index, base, limit, access, grann
  41.     SetGDTEntry(0, 0, 0, 0, 0);
  42.     SetGDTEntry(1, 0, 0xffff, 0x9a, 0x80);
  43.     SetGDTEntry(2, 0, 0xffff, 0x92, 0x80);
  44.     SetGDTEntry(3, 0, 0xffff, 0x9a, 0xcf);
  45.     SetGDTEntry(4, 0, 0xffff, 0x92, 0xcf);
  46.     SetGDTEntry(5, 0, 0, 0x9a, 0xa2);
  47.     SetGDTEntry(6, 0, 0, 0x92, 0xa0);
  48.     SetGDTEntry(7, 0, 0, 0xf2, 0);
  49.     SetGDTEntry(8, 0, 0, 0xfa, 0x20);
  50.  
  51.    
  52.  
  53.     gdt_flush(&g_GDT_PTR);
  54.     debugf("\n======GDT DUMP END======\n");
  55.     printf("\nIt works");
  56.  
  57.     // 16-bit code descriptor (Entry 1)
  58.  
  59.  
  60. }
  61.  
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement