segment.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #ifndef _ASM_X86_SEGMENT_H
  2. #define _ASM_X86_SEGMENT_H
  3. #include <linux/const.h>
  4. /* Constructor for a conventional segment GDT (or LDT) entry */
  5. /* This is a macro so it can be used in initializers */
  6. #define GDT_ENTRY(flags, base, limit) \
  7. ((((base) & _AC(0xff000000,ULL)) << (56-24)) | \
  8. (((flags) & _AC(0x0000f0ff,ULL)) << 40) | \
  9. (((limit) & _AC(0x000f0000,ULL)) << (48-16)) | \
  10. (((base) & _AC(0x00ffffff,ULL)) << 16) | \
  11. (((limit) & _AC(0x0000ffff,ULL))))
  12. /* Simple and small GDT entries for booting only */
  13. #define GDT_ENTRY_BOOT_CS 2
  14. #define __BOOT_CS (GDT_ENTRY_BOOT_CS * 8)
  15. #define GDT_ENTRY_BOOT_DS (GDT_ENTRY_BOOT_CS + 1)
  16. #define __BOOT_DS (GDT_ENTRY_BOOT_DS * 8)
  17. #define GDT_ENTRY_BOOT_TSS (GDT_ENTRY_BOOT_CS + 2)
  18. #define __BOOT_TSS (GDT_ENTRY_BOOT_TSS * 8)
  19. #ifdef CONFIG_X86_32
  20. /*
  21. * The layout of the per-CPU GDT under Linux:
  22. *
  23. * 0 - null
  24. * 1 - reserved
  25. * 2 - reserved
  26. * 3 - reserved
  27. *
  28. * 4 - unused <==== new cacheline
  29. * 5 - unused
  30. *
  31. * ------- start of TLS (Thread-Local Storage) segments:
  32. *
  33. * 6 - TLS segment #1 [ glibc's TLS segment ]
  34. * 7 - TLS segment #2 [ Wine's %fs Win32 segment ]
  35. * 8 - TLS segment #3
  36. * 9 - reserved
  37. * 10 - reserved
  38. * 11 - reserved
  39. *
  40. * ------- start of kernel segments:
  41. *
  42. * 12 - kernel code segment <==== new cacheline
  43. * 13 - kernel data segment
  44. * 14 - default user CS
  45. * 15 - default user DS
  46. * 16 - TSS
  47. * 17 - LDT
  48. * 18 - PNPBIOS support (16->32 gate)
  49. * 19 - PNPBIOS support
  50. * 20 - PNPBIOS support
  51. * 21 - PNPBIOS support
  52. * 22 - PNPBIOS support
  53. * 23 - APM BIOS support
  54. * 24 - APM BIOS support
  55. * 25 - APM BIOS support
  56. *
  57. * 26 - ESPFIX small SS
  58. * 27 - per-cpu [ offset to per-cpu data area ]
  59. * 28 - stack_canary-20 [ for stack protector ]
  60. * 29 - unused
  61. * 30 - unused
  62. * 31 - TSS for double fault handler
  63. */
  64. #define GDT_ENTRY_TLS_MIN 6
  65. #define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
  66. #define GDT_ENTRY_DEFAULT_USER_CS 14
  67. #define GDT_ENTRY_DEFAULT_USER_DS 15
  68. #define GDT_ENTRY_KERNEL_BASE (12)
  69. #define GDT_ENTRY_KERNEL_CS (GDT_ENTRY_KERNEL_BASE+0)
  70. #define GDT_ENTRY_KERNEL_DS (GDT_ENTRY_KERNEL_BASE+1)
  71. #define GDT_ENTRY_TSS (GDT_ENTRY_KERNEL_BASE+4)
  72. #define GDT_ENTRY_LDT (GDT_ENTRY_KERNEL_BASE+5)
  73. #define GDT_ENTRY_PNPBIOS_BASE (GDT_ENTRY_KERNEL_BASE+6)
  74. #define GDT_ENTRY_APMBIOS_BASE (GDT_ENTRY_KERNEL_BASE+11)
  75. #define GDT_ENTRY_ESPFIX_SS (GDT_ENTRY_KERNEL_BASE+14)
  76. #define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS*8)
  77. #define GDT_ENTRY_PERCPU (GDT_ENTRY_KERNEL_BASE+15)
  78. #ifdef CONFIG_SMP
  79. #define __KERNEL_PERCPU (GDT_ENTRY_PERCPU * 8)
  80. #else
  81. #define __KERNEL_PERCPU 0
  82. #endif
  83. #define GDT_ENTRY_STACK_CANARY (GDT_ENTRY_KERNEL_BASE+16)
  84. #ifdef CONFIG_CC_STACKPROTECTOR
  85. #define __KERNEL_STACK_CANARY (GDT_ENTRY_STACK_CANARY*8)
  86. #else
  87. #define __KERNEL_STACK_CANARY 0
  88. #endif
  89. #define GDT_ENTRY_DOUBLEFAULT_TSS 31
  90. /*
  91. * The GDT has 32 entries
  92. */
  93. #define GDT_ENTRIES 32
  94. /* The PnP BIOS entries in the GDT */
  95. #define GDT_ENTRY_PNPBIOS_CS32 (GDT_ENTRY_PNPBIOS_BASE + 0)
  96. #define GDT_ENTRY_PNPBIOS_CS16 (GDT_ENTRY_PNPBIOS_BASE + 1)
  97. #define GDT_ENTRY_PNPBIOS_DS (GDT_ENTRY_PNPBIOS_BASE + 2)
  98. #define GDT_ENTRY_PNPBIOS_TS1 (GDT_ENTRY_PNPBIOS_BASE + 3)
  99. #define GDT_ENTRY_PNPBIOS_TS2 (GDT_ENTRY_PNPBIOS_BASE + 4)
  100. /* The PnP BIOS selectors */
  101. #define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32 * 8) /* segment for calling fn */
  102. #define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16 * 8) /* code segment for BIOS */
  103. #define PNP_DS (GDT_ENTRY_PNPBIOS_DS * 8) /* data segment for BIOS */
  104. #define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1 * 8) /* transfer data segment */
  105. #define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2 * 8) /* another data segment */
  106. /* Bottom two bits of selector give the ring privilege level */
  107. #define SEGMENT_RPL_MASK 0x3
  108. /* Bit 2 is table indicator (LDT/GDT) */
  109. #define SEGMENT_TI_MASK 0x4
  110. /* User mode is privilege level 3 */
  111. #define USER_RPL 0x3
  112. /* LDT segment has TI set, GDT has it cleared */
  113. #define SEGMENT_LDT 0x4
  114. #define SEGMENT_GDT 0x0
  115. /*
  116. * Matching rules for certain types of segments.
  117. */
  118. /* Matches PNP_CS32 and PNP_CS16 (they must be consecutive) */
  119. #define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == GDT_ENTRY_PNPBIOS_BASE * 8)
  120. #else
  121. #include <asm/cache.h>
  122. #define GDT_ENTRY_KERNEL32_CS 1
  123. #define GDT_ENTRY_KERNEL_CS 2
  124. #define GDT_ENTRY_KERNEL_DS 3
  125. #define __KERNEL32_CS (GDT_ENTRY_KERNEL32_CS * 8)
  126. /*
  127. * we cannot use the same code segment descriptor for user and kernel
  128. * -- not even in the long flat mode, because of different DPL /kkeil
  129. * The segment offset needs to contain a RPL. Grr. -AK
  130. * GDT layout to get 64bit syscall right (sysret hardcodes gdt offsets)
  131. */
  132. #define GDT_ENTRY_DEFAULT_USER32_CS 4
  133. #define GDT_ENTRY_DEFAULT_USER_DS 5
  134. #define GDT_ENTRY_DEFAULT_USER_CS 6
  135. #define __USER32_CS (GDT_ENTRY_DEFAULT_USER32_CS*8+3)
  136. #define __USER32_DS __USER_DS
  137. #define GDT_ENTRY_TSS 8 /* needs two entries */
  138. #define GDT_ENTRY_LDT 10 /* needs two entries */
  139. #define GDT_ENTRY_TLS_MIN 12
  140. #define GDT_ENTRY_TLS_MAX 14
  141. #define GDT_ENTRY_PER_CPU 15 /* Abused to load per CPU data from limit */
  142. #define __PER_CPU_SEG (GDT_ENTRY_PER_CPU * 8 + 3)
  143. /* TLS indexes for 64bit - hardcoded in arch_prctl */
  144. #define FS_TLS 0
  145. #define GS_TLS 1
  146. #define GS_TLS_SEL ((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3)
  147. #define FS_TLS_SEL ((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3)
  148. #define GDT_ENTRIES 16
  149. #endif
  150. #define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
  151. #define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
  152. #define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8+3)
  153. #define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8+3)
  154. #ifndef CONFIG_PARAVIRT
  155. #define get_kernel_rpl() 0
  156. #endif
  157. /* User mode is privilege level 3 */
  158. #define USER_RPL 0x3
  159. /* LDT segment has TI set, GDT has it cleared */
  160. #define SEGMENT_LDT 0x4
  161. #define SEGMENT_GDT 0x0
  162. /* Bottom two bits of selector give the ring privilege level */
  163. #define SEGMENT_RPL_MASK 0x3
  164. /* Bit 2 is table indicator (LDT/GDT) */
  165. #define SEGMENT_TI_MASK 0x4
  166. #define IDT_ENTRIES 256
  167. #define NUM_EXCEPTION_VECTORS 32
  168. #define GDT_SIZE (GDT_ENTRIES * 8)
  169. #define GDT_ENTRY_TLS_ENTRIES 3
  170. #define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES * 8)
  171. #ifdef __KERNEL__
  172. #ifndef __ASSEMBLY__
  173. extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][10];
  174. /*
  175. * Load a segment. Fall back on loading the zero
  176. * segment if something goes wrong..
  177. */
  178. #define loadsegment(seg, value) \
  179. do { \
  180. unsigned short __val = (value); \
  181. \
  182. asm volatile(" \n" \
  183. "1: movl %k0,%%" #seg " \n" \
  184. \
  185. ".section .fixup,\"ax\" \n" \
  186. "2: xorl %k0,%k0 \n" \
  187. " jmp 1b \n" \
  188. ".previous \n" \
  189. \
  190. _ASM_EXTABLE(1b, 2b) \
  191. \
  192. : "+r" (__val) : : "memory"); \
  193. } while (0)
  194. /*
  195. * Save a segment register away
  196. */
  197. #define savesegment(seg, value) \
  198. asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
  199. /*
  200. * x86_32 user gs accessors.
  201. */
  202. #ifdef CONFIG_X86_32
  203. #ifdef CONFIG_X86_32_LAZY_GS
  204. #define get_user_gs(regs) (u16)({unsigned long v; savesegment(gs, v); v;})
  205. #define set_user_gs(regs, v) loadsegment(gs, (unsigned long)(v))
  206. #define task_user_gs(tsk) ((tsk)->thread.gs)
  207. #define lazy_save_gs(v) savesegment(gs, (v))
  208. #define lazy_load_gs(v) loadsegment(gs, (v))
  209. #else /* X86_32_LAZY_GS */
  210. #define get_user_gs(regs) (u16)((regs)->gs)
  211. #define set_user_gs(regs, v) do { (regs)->gs = (v); } while (0)
  212. #define task_user_gs(tsk) (task_pt_regs(tsk)->gs)
  213. #define lazy_save_gs(v) do { } while (0)
  214. #define lazy_load_gs(v) do { } while (0)
  215. #endif /* X86_32_LAZY_GS */
  216. #endif /* X86_32 */
  217. static inline unsigned long get_limit(unsigned long segment)
  218. {
  219. unsigned long __limit;
  220. asm("lsll %1,%0" : "=r" (__limit) : "r" (segment));
  221. return __limit + 1;
  222. }
  223. #endif /* !__ASSEMBLY__ */
  224. #endif /* __KERNEL__ */
  225. #endif /* _ASM_X86_SEGMENT_H */