Guest User

Untitled

a guest
Apr 28th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.86 KB | None | 0 0
  1. #include <system/System.hpp>
  2. #include <system/gdt/Gdt.hpp>
  3. #include <Kernel.hpp>
  4.  
  5. // Values used in the GDT
  6. const uint8_t gdtLength = 4;
  7.  
  8. /**
  9.  * The GdtEntry array containing 4 entries.
  10.  * - the null entry
  11.  * - the Kernel code segment descriptor
  12.  * - the Kernel data segment descriptor
  13.  * - the TSS entry
  14.  */
  15. GdtEntry gdt[gdtLength];
  16.  
  17. /**
  18.  * A GdtPointer pointing to the GDT.
  19.  */
  20. GdtPointer gdtPointer;
  21.  
  22. /**
  23.  * The TSS
  24.  */
  25. Tss tss;
  26.  
  27. /**
  28.  * External method for loading the GDT and the TSS
  29.  */
  30. extern "C" void _loadGdt();
  31. extern "C" void _loadTss();
  32.  
  33. /**
  34.  * Creates a GDT gate
  35.  *
  36.  * @param index the gate index
  37.  * @param base the base address
  38.  * @param limit the limit
  39.  * @param access the access byte
  40.  */
  41. void Gdt::createGdtGate(uint32_t index, uint32_t base, uint32_t limit,
  42.         uint8_t access, uint8_t granularity) {
  43.     gdt[index].baseLow = (base & 0xFFFF);
  44.     gdt[index].baseMiddle = (base >> 16) & 0xFF;
  45.     gdt[index].baseHigh = (base >> 24) & 0xFF;
  46.  
  47.     gdt[index].limitLow = (limit & 0xFFFF);
  48.     gdt[index].granularity = ((limit >> 16) & 0x0F);
  49.  
  50.     gdt[index].granularity |= (granularity & 0xF0);
  51.     gdt[index].access = access;
  52. }
  53.  
  54. /**
  55.  * Installs the global descriptor table.
  56.  *
  57.  * - Sets the limit of the GDT pointer to the size of the GDT entries
  58.  * - Sets the base address to the gdt's address
  59.  * - Installs the GDT gates
  60.  * - Mounts the GDT
  61.  */
  62. void Gdt::install() {
  63.  
  64.     LOGLN("Installing GDT");
  65.     gdtPointer.limit = (sizeof(GdtEntry) * gdtLength) - 1;
  66.     gdtPointer.base = (uint32_t) &gdt;
  67.  
  68.     // Null descriptor, position 0x00
  69.     createGdtGate(0, 0, 0, 0, 0);
  70.  
  71.     // Kernel code segment descriptor, position 0x08
  72.     createGdtGate(1, 0, 0xFFFFFFFF, ACCESS_BYTE__KERNEL_CODE_SEGMENT, 0xCF);
  73.  
  74.     // Kernel data segment descriptor, position 0x10
  75.     createGdtGate(2, 0, 0xFFFFFFFF, ACCESS_BYTE__KERNEL_DATA_SEGMENT, 0xCF);
  76.  
  77.     // TSS descriptor, position 0x18
  78.     createGdtGate(3, (uint32_t) &tss, sizeof(Tss),
  79.     ACCESS_BYTE__TSS_386_SEGMENT, 0x40);
  80.     tss.ss0 = 0x10; // Kernel data
  81.     tss.esp0 = 0; // This will be initially set once the Scheduler creates the first task
  82.  
  83.     // Load the GDT and TSS
  84.     _loadGdt();
  85.     _loadTss();
  86. }
  87.  
  88. /**
  89.  * Sets the ESP0 of the TSS to the value.
  90.  */
  91. void Gdt::setTssEsp0(uint32_t esp) {
  92.     tss.esp0 = esp;
  93. }
  94.  
  95.  
  96.  
  97. // ###########################################################################
  98.  
  99.  
  100.  
  101. #ifndef SYSTEM__GDT
  102. #define SYSTEM__GDT
  103.  
  104. #include <stdint.h>
  105. #include <system/gdt/Tss.hpp>
  106.  
  107. /**
  108.  * Macro to create the ACCESS byte for a GDT entry to a code or data segment
  109.  *
  110.  * Parameters:
  111.  *   readWrite       if code read allowed, if data write allowed
  112.  *   codeOrData      determines if its a code (1) or a data (0) segment
  113.  *   privilege       Privilege level (Ring 0 - Ring 3)
  114.  *   present         1 if present
  115.  *
  116.  * Bits:           Values:                    Info:
  117.  *   0               0                          Accessed bit, free for developers use
  118.  *   1               parameter readWrite        read if code segment, or write if data segment
  119.  *   2               0                          Direction bit or conforming bit)
  120.  *   3               parameter codeOrData       Entry is a code (1) or data (0) segment
  121.  *   4               0                          Entry is for Code/Data (1) or Gate/TSS (0)
  122.  *   5 - 6           parameter privilege        Ring 0 - Ring 3
  123.  *   7               parameter present          Must be 1 for active entry
  124.  */
  125. #define ACCESS_BYTE__FOR__CODE_OR_DATA(readWrite, codeOrData, privilege, present) \
  126.               (0 | (readWrite << 1) | (0 << 2) | (codeOrData << 3) | (1 << 4) | (privilege << 5) | (present << 7))
  127.  
  128. /**
  129.  * Macro to create the ACCESS byte for a GDT entry to a Gate/TSS
  130.  *
  131.  * Parameters:
  132.  *   segmentType     e.g. 0x9 for 386-TSS
  133.  *   privilege       Privilege level (Ring 0 - Ring 3)
  134.  *   present         1 if present
  135.  *
  136.  * Bits:           Values:                    Info:
  137.  *   0 - 3           parameter segmentType      Segment type
  138.  *   4               0                          Entry is for Code/Data (1) or Gate/TSS (0)
  139.  *   5 - 6           parameter privilege        Ring 0 - Ring 3
  140.  *   7               parameter present          Must be 1 for active entry
  141.  */
  142. #define ACCESS_BYTE__FOR__GATE_OR_TSS(segmentType, privilege, present) \
  143.               (segmentType | (0 << 4) | (privilege << 5) | (present << 7))
  144.  
  145. /**
  146.  * Common descriptor access flags
  147.  */
  148. #define ACCESS_BYTE__KERNEL_CODE_SEGMENT        ACCESS_BYTE__FOR__CODE_OR_DATA(1, 1, 0, 1)
  149. #define ACCESS_BYTE__KERNEL_DATA_SEGMENT        ACCESS_BYTE__FOR__CODE_OR_DATA(1, 0, 0, 1)
  150. #define ACCESS_BYTE__USER_CODE_SEGMENT          ACCESS_BYTE__FOR__CODE_OR_DATA(1, 1, 3, 1)
  151. #define ACCESS_BYTE__USER_DATA_SEGMENT          ACCESS_BYTE__FOR__CODE_OR_DATA(1, 0, 3, 1)
  152. #define ACCESS_BYTE__TSS_386_SEGMENT            ACCESS_BYTE__FOR__GATE_OR_TSS(0x9, 0, 1)
  153.  
  154. /**
  155.  * Structure of a GDT entry
  156.  */
  157. struct GdtEntry {
  158.     uint16_t limitLow :16;
  159.     uint16_t baseLow :16;
  160.     uint8_t baseMiddle :8;
  161.     uint8_t access :8;
  162.     uint16_t limitHigh :4;
  163.     uint8_t granularity :4;
  164.     uint8_t baseHigh :8;
  165. }__attribute__((packed));
  166.  
  167. /**
  168.  * Structure of the GDT pointer
  169.  */
  170. struct GdtPointer {
  171.     uint16_t limit;
  172.     uint32_t base;
  173. }__attribute__((packed));
  174.  
  175. /**
  176.  * The Gdt class provides methods for setting up the
  177.  * global descriptor table.
  178.  */
  179. class Gdt {
  180. public:
  181.     static void install();
  182.     static void createGdtGate(uint32_t index, uint32_t base, uint32_t limit,
  183.             uint8_t access, uint8_t granularity);
  184.     static void setTssEsp0(uint32_t esp);
  185. };
  186.  
  187. #endif
  188.  
  189.  
  190.  
  191.  
  192. // ###########################################################################
  193.  
  194.  
  195.  
  196.  
  197.  
  198. global _loadGdt
  199. global _loadTss
  200.  
  201. extern gdtPointer
  202.  
  203. ; Mounts the global descriptor table
  204. ; and does a far jump back to the code
  205. _loadGdt:
  206.     lgdt [gdtPointer]
  207.     mov ax, 0x10
  208.     mov ds, ax
  209.     mov es, ax
  210.     mov fs, ax
  211.     mov gs, ax
  212.     mov ss, ax
  213.     jmp 0x08:flush
  214. flush:
  215.     ret
  216.  
  217. _loadTss:
  218.     mov ax, 0x18 ; 4th entry of GDT
  219.     ltr ax
  220.     ret
Advertisement
Add Comment
Please, Sign In to add comment