Guest User

Untitled

a guest
Jun 21st, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. #include "stdint.h"
  2. #include "gdt.h"
  3.  
  4. #define GDT_ENTRIES 3
  5.  
  6. uint64_t gdt[GDT_ENTRIES];
  7.  
  8. #define GDT_FLAG_DATASEG 0x02
  9. #define GDT_FLAG_CODESEG 0x0a
  10. #define GDT_FLAG_TSS     0x09
  11.  
  12. #define GDT_FLAG_SEGMENT 0x10
  13. #define GDT_FLAG_RING0   0x00
  14. #define GDT_FLAG_RING3   0x60
  15. #define GDT_FLAG_PRESENT 0x80
  16.  
  17. #define GDT_FLAG_4K_GRAN 0x800
  18. #define GDT_FLAG_32_BIT  0x400
  19.  
  20. void load_gdt() {
  21.     struct {
  22.           uint16_t limit;
  23.           void* pointer;
  24.     } __attribute__((packed)) gdtp = {
  25.           .limit = GDT_ENTRIES * 8 - 1,
  26.           .pointer = gdt,
  27.     };
  28.     asm volatile("lgdt %0" : : "m" (gdtp));
  29.  
  30.     asm ("mov $0x10, %ax");
  31.     asm ("mov %ax, %ds");
  32.     asm ("mov %ax, %es");
  33.     asm ("mov %ax, %fs");
  34.     asm ("mov %ax, %gs");
  35.     asm ("mov %ax, %ss");
  36.     asm ("ljmp $0x8, $.1");
  37.     asm (".1:");
  38. }
  39.  
  40. void set_entry(int i, unsigned int base, unsigned int limit, int flags)
  41. {
  42.     gdt[i] = limit & 0xffffLL;
  43.     gdt[i] |= (base & 0xffffffLL) << 16;
  44.     gdt[i] |= (flags & 0xffLL) << 40;
  45.     gdt[i] |= ((limit >> 16) & 0xfLL) << 48;
  46.     gdt[i] |= ((flags >> 8 )& 0xffLL) << 52;
  47.     gdt[i] |= ((base >> 24) & 0xffLL) << 56;
  48. }
  49.  
  50. void init_gdt(void)
  51. {
  52.     set_entry(0, 0, 0, 0);
  53.     set_entry(1, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT |
  54.         GDT_FLAG_CODESEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT);
  55.     set_entry(2, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT |
  56.         GDT_FLAG_DATASEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT);
  57.  
  58.     load_gdt();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment