tss.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* TSS + flat GDT (kernel code/data, user code/data, TSS). */
  2. #include "tss.h"
  3. struct tss tss;
  4. struct gdt_entry {
  5. unsigned short limit_low;
  6. unsigned short base_low;
  7. unsigned char base_mid;
  8. unsigned char access;
  9. unsigned char limit_high_flags;
  10. unsigned char base_high;
  11. } __attribute__((packed));
  12. struct gdt_ptr {
  13. unsigned short limit;
  14. unsigned int base;
  15. } __attribute__((packed));
  16. static struct gdt_entry gdt[6];
  17. static struct gdt_ptr gdtp;
  18. static void set_entry(int i, unsigned int base, unsigned int limit,
  19. unsigned char access, unsigned char flags)
  20. {
  21. gdt[i].limit_low = (unsigned short)(limit & 0xFFFF);
  22. gdt[i].base_low = (unsigned short)(base & 0xFFFF);
  23. gdt[i].base_mid = (unsigned char)((base >> 16) & 0xFF);
  24. gdt[i].access = access;
  25. gdt[i].limit_high_flags =
  26. (unsigned char)(((limit >> 16) & 0x0F) | (flags & 0xF0));
  27. gdt[i].base_high = (unsigned char)((base >> 24) & 0xFF);
  28. }
  29. void gdt_tss_init(void)
  30. {
  31. unsigned int i;
  32. for (i = 0; i < sizeof(gdt); i++)
  33. ((unsigned char *)gdt)[i] = 0;
  34. for (i = 0; i < sizeof(tss); i++)
  35. ((unsigned char *)&tss)[i] = 0;
  36. /* 0x00 null */
  37. /* 0x08 kernel code, 0x10 kernel data */
  38. set_entry(1, 0, 0xFFFFF, 0x9A, 0xCF);
  39. set_entry(2, 0, 0xFFFFF, 0x92, 0xCF);
  40. /* 0x18 user code, 0x20 user data (DPL=3) */
  41. set_entry(3, 0, 0xFFFFF, 0xFA, 0xCF);
  42. set_entry(4, 0, 0xFFFFF, 0xF2, 0xCF);
  43. /* 0x28 TSS */
  44. set_entry(5, (unsigned int)&tss, sizeof(tss) - 1, 0x89, 0x00);
  45. gdtp.limit = (unsigned short)(sizeof(gdt) - 1);
  46. gdtp.base = (unsigned int)gdt;
  47. __asm__ volatile ("lgdt %0" : : "m"(gdtp));
  48. tss.ss0 = 0x10; /* kernel data segment */
  49. tss.esp0 = 0x30800; /* bootstrap stack; updated by switch.S per process */
  50. tss.iomap_base = 0xFFFF; /* no I/O permission bitmap (deny ring-3 I/O) */
  51. __asm__ volatile ("ltr %0" : : "r"((unsigned short)0x28));
  52. }