desc.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef __ARCH_DESC_H
  2. #define __ARCH_DESC_H
  3. #include <asm/ldt.h>
  4. #include <asm/segment.h>
  5. #define CPU_16BIT_STACK_SIZE 1024
  6. #ifndef __ASSEMBLY__
  7. #include <linux/preempt.h>
  8. #include <linux/smp.h>
  9. #include <linux/percpu.h>
  10. #include <asm/mmu.h>
  11. extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
  12. DECLARE_PER_CPU(struct desc_struct, cpu_gdt_table[GDT_ENTRIES]);
  13. #define get_cpu_gdt_table(_cpu) (per_cpu(cpu_gdt_table,_cpu))
  14. DECLARE_PER_CPU(unsigned char, cpu_16bit_stack[CPU_16BIT_STACK_SIZE]);
  15. struct Xgt_desc_struct {
  16. unsigned short size;
  17. unsigned long address __attribute__((packed));
  18. unsigned short pad;
  19. } __attribute__ ((packed));
  20. extern struct Xgt_desc_struct idt_descr, cpu_gdt_descr[NR_CPUS];
  21. #define load_TR_desc() __asm__ __volatile__("ltr %w0"::"q" (GDT_ENTRY_TSS*8))
  22. #define load_LDT_desc() __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8))
  23. #define load_gdt(dtr) __asm__ __volatile("lgdt %0"::"m" (*dtr))
  24. #define load_idt(dtr) __asm__ __volatile("lidt %0"::"m" (*dtr))
  25. #define load_tr(tr) __asm__ __volatile("ltr %0"::"mr" (tr))
  26. #define load_ldt(ldt) __asm__ __volatile("lldt %0"::"mr" (ldt))
  27. #define store_gdt(dtr) __asm__ ("sgdt %0":"=m" (*dtr))
  28. #define store_idt(dtr) __asm__ ("sidt %0":"=m" (*dtr))
  29. #define store_tr(tr) __asm__ ("str %0":"=mr" (tr))
  30. #define store_ldt(ldt) __asm__ ("sldt %0":"=mr" (ldt))
  31. /*
  32. * This is the ldt that every process will get unless we need
  33. * something other than this.
  34. */
  35. extern struct desc_struct default_ldt[];
  36. extern void set_intr_gate(unsigned int irq, void * addr);
  37. #define _set_tssldt_desc(n,addr,limit,type) \
  38. __asm__ __volatile__ ("movw %w3,0(%2)\n\t" \
  39. "movw %w1,2(%2)\n\t" \
  40. "rorl $16,%1\n\t" \
  41. "movb %b1,4(%2)\n\t" \
  42. "movb %4,5(%2)\n\t" \
  43. "movb $0,6(%2)\n\t" \
  44. "movb %h1,7(%2)\n\t" \
  45. "rorl $16,%1" \
  46. : "=m"(*(n)) : "q" (addr), "r"(n), "ir"(limit), "i"(type))
  47. static inline void __set_tss_desc(unsigned int cpu, unsigned int entry, void *addr)
  48. {
  49. _set_tssldt_desc(&get_cpu_gdt_table(cpu)[entry], (int)addr,
  50. offsetof(struct tss_struct, __cacheline_filler) - 1, 0x89);
  51. }
  52. #define set_tss_desc(cpu,addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
  53. static inline void set_ldt_desc(unsigned int cpu, void *addr, unsigned int size)
  54. {
  55. _set_tssldt_desc(&get_cpu_gdt_table(cpu)[GDT_ENTRY_LDT], (int)addr, ((size << 3)-1), 0x82);
  56. }
  57. #define LDT_entry_a(info) \
  58. ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
  59. #define LDT_entry_b(info) \
  60. (((info)->base_addr & 0xff000000) | \
  61. (((info)->base_addr & 0x00ff0000) >> 16) | \
  62. ((info)->limit & 0xf0000) | \
  63. (((info)->read_exec_only ^ 1) << 9) | \
  64. ((info)->contents << 10) | \
  65. (((info)->seg_not_present ^ 1) << 15) | \
  66. ((info)->seg_32bit << 22) | \
  67. ((info)->limit_in_pages << 23) | \
  68. ((info)->useable << 20) | \
  69. 0x7000)
  70. #define LDT_empty(info) (\
  71. (info)->base_addr == 0 && \
  72. (info)->limit == 0 && \
  73. (info)->contents == 0 && \
  74. (info)->read_exec_only == 1 && \
  75. (info)->seg_32bit == 0 && \
  76. (info)->limit_in_pages == 0 && \
  77. (info)->seg_not_present == 1 && \
  78. (info)->useable == 0 )
  79. static inline void write_ldt_entry(void *ldt, int entry, __u32 entry_a, __u32 entry_b)
  80. {
  81. __u32 *lp = (__u32 *)((char *)ldt + entry*8);
  82. *lp = entry_a;
  83. *(lp+1) = entry_b;
  84. }
  85. #if TLS_SIZE != 24
  86. # error update this code.
  87. #endif
  88. static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
  89. {
  90. #define C(i) get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]
  91. C(0); C(1); C(2);
  92. #undef C
  93. }
  94. static inline void clear_LDT(void)
  95. {
  96. int cpu = get_cpu();
  97. set_ldt_desc(cpu, &default_ldt[0], 5);
  98. load_LDT_desc();
  99. put_cpu();
  100. }
  101. /*
  102. * load one particular LDT into the current CPU
  103. */
  104. static inline void load_LDT_nolock(mm_context_t *pc, int cpu)
  105. {
  106. void *segments = pc->ldt;
  107. int count = pc->size;
  108. if (likely(!count)) {
  109. segments = &default_ldt[0];
  110. count = 5;
  111. }
  112. set_ldt_desc(cpu, segments, count);
  113. load_LDT_desc();
  114. }
  115. static inline void load_LDT(mm_context_t *pc)
  116. {
  117. int cpu = get_cpu();
  118. load_LDT_nolock(pc, cpu);
  119. put_cpu();
  120. }
  121. static inline unsigned long get_desc_base(unsigned long *desc)
  122. {
  123. unsigned long base;
  124. base = ((desc[0] >> 16) & 0x0000ffff) |
  125. ((desc[1] << 16) & 0x00ff0000) |
  126. (desc[1] & 0xff000000);
  127. return base;
  128. }
  129. #endif /* !__ASSEMBLY__ */
  130. #endif