tss.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* TSS + per-CPU flat GDT (see tss.h for the selector layout). */
  2. #include "tss.h"
  3. #include "sched.h"
  4. /* Top of the in-image bootstrap stack (see kernel.ld): used until the first
  5. * context switch replaces TSS.esp0 with the process's own kernel stack. */
  6. extern char __boot_stack_top[];
  7. #define GDT_ENTRIES (6 + MAX_CPUS)
  8. struct gdt_entry {
  9. unsigned short limit_low;
  10. unsigned short base_low;
  11. unsigned char base_mid;
  12. unsigned char access;
  13. unsigned char limit_high_flags;
  14. unsigned char base_high;
  15. } __attribute__((packed));
  16. struct gdt_ptr {
  17. unsigned short limit;
  18. unsigned int base;
  19. } __attribute__((packed));
  20. static struct tss tss[MAX_CPUS];
  21. static struct gdt_entry gdt[MAX_CPUS][GDT_ENTRIES]
  22. __attribute__((aligned(8)));
  23. static void set_entry(struct gdt_entry *g, int i, unsigned int base,
  24. unsigned int limit, unsigned char access,
  25. unsigned char flags)
  26. {
  27. g[i].limit_low = (unsigned short)(limit & 0xFFFF);
  28. g[i].base_low = (unsigned short)(base & 0xFFFF);
  29. g[i].base_mid = (unsigned char)((base >> 16) & 0xFF);
  30. g[i].access = access;
  31. g[i].limit_high_flags =
  32. (unsigned char)(((limit >> 16) & 0x0F) | (flags & 0xF0));
  33. g[i].base_high = (unsigned char)((base >> 24) & 0xFF);
  34. }
  35. void gdt_tss_init(unsigned int cpu)
  36. {
  37. struct cpu *c;
  38. struct gdt_entry *g;
  39. struct gdt_ptr gp;
  40. unsigned short sel;
  41. unsigned int i;
  42. if (cpu >= MAX_CPUS)
  43. return;
  44. c = &cpus[cpu];
  45. g = gdt[cpu];
  46. for (i = 0; i < sizeof(gdt[cpu]); i++)
  47. ((unsigned char *)g)[i] = 0;
  48. for (i = 0; i < sizeof(tss[cpu]); i++)
  49. ((unsigned char *)&tss[cpu])[i] = 0;
  50. c->self = c;
  51. c->tss = &tss[cpu];
  52. c->id = cpu;
  53. /* 0x00 null */
  54. /* 0x08 kernel code, 0x10 kernel data */
  55. set_entry(g, 1, 0, 0xFFFFF, 0x9A, 0xCF);
  56. set_entry(g, 2, 0, 0xFFFFF, 0x92, 0xCF);
  57. /* 0x18 user code, 0x20 user data (DPL=3) */
  58. set_entry(g, 3, 0, 0xFFFFF, 0xFA, 0xCF);
  59. set_entry(g, 4, 0, 0xFFFFF, 0xF2, 0xCF);
  60. /* 0x28 per-CPU data: GS base = this CPU's state */
  61. set_entry(g, 5, (unsigned int)c, 0xFFFFF, 0x92, 0xCF);
  62. /* 0x30 + 8*cpu: one 32-bit TSS per CPU */
  63. for (i = 0; i < MAX_CPUS; i++)
  64. set_entry(g, 6 + i, (unsigned int)&tss[i],
  65. sizeof(tss[i]) - 1, 0x89, 0x00);
  66. tss[cpu].ss0 = 0x10; /* kernel data segment */
  67. tss[cpu].esp0 = (unsigned int)__boot_stack_top;
  68. tss[cpu].iomap_base = 0xFFFF; /* no I/O permission bitmap */
  69. gp.limit = (unsigned short)(sizeof(gdt[cpu]) - 1);
  70. gp.base = (unsigned int)&g[0];
  71. __asm__ volatile ("lgdt %0" : : "m"(gp));
  72. sel = (unsigned short)(0x30 + 8 * cpu);
  73. __asm__ volatile ("ltr %0" : : "r"(sel));
  74. sel = 0x28;
  75. __asm__ volatile ("movw %0, %%gs" : : "r"(sel));
  76. cpu_gs_ready = 1;
  77. }