desc.h 4.1 KB

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