gdt.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2011-2013 Kevin Lange
  5. * Copyright (C) 2015 Dale Weiler
  6. *
  7. * Global Descriptor Tables module
  8. *
  9. */
  10. #include <kernel/system.h>
  11. #include <kernel/logging.h>
  12. #include <kernel/tss.h>
  13. typedef struct {
  14. /* Limits */
  15. uint16_t limit_low;
  16. /* Segment address */
  17. uint16_t base_low;
  18. uint8_t base_middle;
  19. /* Access modes */
  20. uint8_t access;
  21. uint8_t granularity;
  22. uint8_t base_high;
  23. } __attribute__((packed)) gdt_entry_t;
  24. typedef struct {
  25. uint16_t limit;
  26. uintptr_t base;
  27. } __attribute__((packed)) gdt_pointer_t;
  28. /* In the future we may need to put a lock on the access of this */
  29. static struct {
  30. gdt_entry_t entries[6];
  31. gdt_pointer_t pointer;
  32. tss_entry_t tss;
  33. } gdt __attribute__((used));
  34. extern void gdt_flush(uintptr_t);
  35. #define ENTRY(X) (gdt.entries[(X)])
  36. void gdt_set_gate(uint8_t num, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran) {
  37. /* Base Address */
  38. ENTRY(num).base_low = (base & 0xFFFF);
  39. ENTRY(num).base_middle = (base >> 16) & 0xFF;
  40. ENTRY(num).base_high = (base >> 24) & 0xFF;
  41. /* Limits */
  42. ENTRY(num).limit_low = (limit & 0xFFFF);
  43. ENTRY(num).granularity = (limit >> 16) & 0X0F;
  44. /* Granularity */
  45. ENTRY(num).granularity |= (gran & 0xF0);
  46. /* Access flags */
  47. ENTRY(num).access = access;
  48. }
  49. static void write_tss(int32_t num, uint16_t ss0, uint32_t esp0);
  50. void gdt_install(void) {
  51. gdt_pointer_t *gdtp = &gdt.pointer;
  52. gdtp->limit = sizeof gdt.entries - 1;
  53. gdtp->base = (uintptr_t)&ENTRY(0);
  54. gdt_set_gate(0, 0, 0, 0, 0); /* NULL segment */
  55. gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); /* Code segment */
  56. gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); /* Data segment */
  57. gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); /* User code */
  58. gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); /* User data */
  59. write_tss(5, 0x10, 0x0);
  60. /* Go go go */
  61. gdt_flush((uintptr_t)gdtp);
  62. tss_flush();
  63. }
  64. static void write_tss(int32_t num, uint16_t ss0, uint32_t esp0) {
  65. tss_entry_t * tss = &gdt.tss;
  66. uintptr_t base = (uintptr_t)tss;
  67. uintptr_t limit = base + sizeof *tss;
  68. /* Add the TSS descriptor to the GDT */
  69. gdt_set_gate(num, base, limit, 0xE9, 0x00);
  70. memset(tss, 0x0, sizeof *tss);
  71. tss->ss0 = ss0;
  72. tss->esp0 = esp0;
  73. tss->cs = 0x0b;
  74. tss->ss = 0x13;
  75. tss->ds = 0x13;
  76. tss->es = 0x13;
  77. tss->fs = 0x13;
  78. tss->gs = 0x13;
  79. tss->iomap_base = sizeof *tss;
  80. }
  81. void set_kernel_stack(uintptr_t stack) {
  82. /* Set the kernel stack */
  83. gdt.tss.esp0 = stack;
  84. }