Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2012
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. /* bkerndev - Bran's Kernel Development Tutorial
  2. * By: Brandon F. (friesenb@gmail.com)
  3. * Desc: Global Descriptor Table management
  4. *
  5. * Notes: No warranty expressed or implied. Use at own risk. */
  6.  
  7.  
  8. /* Defines a GDT entry */
  9. struct gdt_entry
  10. {
  11. unsigned short limit_low;
  12. unsigned short base_low;
  13. unsigned char base_middle;
  14. unsigned char access;
  15. unsigned char granularity;
  16. unsigned char base_high;
  17. } __attribute__((packed));
  18.  
  19. struct gdt_ptr
  20. {
  21. unsigned short limit;
  22. unsigned int base;
  23. } __attribute__((packed));
  24.  
  25. /* Our GDT, with 3 entries, and finally our special GDT pointer */
  26. struct gdt_entry gdt[3];
  27. struct gdt_ptr gp;
  28.  
  29. /* This is in start.asm. We use this to properly reload
  30. * the new segment registers */
  31. extern void _gdt_flush();
  32.  
  33. /* Setup a descriptor in the Global Descriptor Table */
  34. void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran)
  35. {
  36. /* Setup the descriptor base address */
  37. gdt[num].base_low = (base & 0xFFFF);
  38. gdt[num].base_middle = (base >> 16) & 0xFF;
  39. gdt[num].base_high = (base >> 24) & 0xFF;
  40.  
  41. /* Setup the descriptor limits */
  42. gdt[num].limit_low = (limit & 0xFFFF);
  43. gdt[num].granularity = ((limit >> 16) & 0x0F);
  44.  
  45. /* Finally, set up the granularity and access flags */
  46. gdt[num].granularity |= (gran & 0xF0);
  47. gdt[num].access = access;
  48. }
  49.  
  50. /* Should be called by main. This will setup the special GDT
  51. * pointer, set up the first 3 entries in our GDT, and then
  52. * finally call gdt_flush() in our assembler file in order
  53. * to tell the processor where the new GDT is and update the
  54. * new segment registers */
  55. void gdt_install()
  56. {
  57. /* Setup the GDT pointer and limit */
  58. gp.limit = (sizeof(struct gdt_entry) * 3) - 1;
  59. gp.base = &gdt;
  60.  
  61. /* Our NULL descriptor */
  62. gdt_set_gate(0, 0, 0, 0, 0);
  63.  
  64. /* The second entry is our Code Segment. The base address
  65. * is 0, the limit is 4GBytes, it uses 4KByte granularity,
  66. * uses 32-bit opcodes, and is a Code Segment descriptor.
  67. * Please check the table above in the tutorial in order
  68. * to see exactly what each value means */
  69. gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
  70.  
  71. /* The third entry is our Data Segment. It's EXACTLY the
  72. * same as our code segment, but the descriptor type in
  73. * this entry's access byte says it's a Data Segment */
  74. gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
  75.  
  76. /* Flush out the old GDT and install the new changes! */
  77. _gdt_flush();
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement