system.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #ifndef _ASM_X86_SYSTEM_H
  2. #define _ASM_X86_SYSTEM_H
  3. #include <asm/asm.h>
  4. #include <asm/segment.h>
  5. #include <asm/cpufeature.h>
  6. #include <asm/cmpxchg.h>
  7. #include <asm/nops.h>
  8. #include <linux/kernel.h>
  9. #include <linux/irqflags.h>
  10. /* entries in ARCH_DLINFO: */
  11. #ifdef CONFIG_IA32_EMULATION
  12. # define AT_VECTOR_SIZE_ARCH 2
  13. #else
  14. # define AT_VECTOR_SIZE_ARCH 1
  15. #endif
  16. struct task_struct; /* one of the stranger aspects of C forward declarations */
  17. struct task_struct *__switch_to(struct task_struct *prev,
  18. struct task_struct *next);
  19. #ifdef CONFIG_X86_32
  20. /*
  21. * Saving eflags is important. It switches not only IOPL between tasks,
  22. * it also protects other tasks from NT leaking through sysenter etc.
  23. */
  24. #define switch_to(prev, next, last) \
  25. do { \
  26. /* \
  27. * Context-switching clobbers all registers, so we clobber \
  28. * them explicitly, via unused output variables. \
  29. * (EAX and EBP is not listed because EBP is saved/restored \
  30. * explicitly for wchan access and EAX is the return value of \
  31. * __switch_to()) \
  32. */ \
  33. unsigned long ebx, ecx, edx, esi, edi; \
  34. \
  35. asm volatile("pushfl\n\t" /* save flags */ \
  36. "pushl %%ebp\n\t" /* save EBP */ \
  37. "movl %%esp,%[prev_sp]\n\t" /* save ESP */ \
  38. "movl %[next_sp],%%esp\n\t" /* restore ESP */ \
  39. "movl $1f,%[prev_ip]\n\t" /* save EIP */ \
  40. "pushl %[next_ip]\n\t" /* restore EIP */ \
  41. "jmp __switch_to\n" /* regparm call */ \
  42. "1:\t" \
  43. "popl %%ebp\n\t" /* restore EBP */ \
  44. "popfl\n" /* restore flags */ \
  45. \
  46. /* output parameters */ \
  47. : [prev_sp] "=m" (prev->thread.sp), \
  48. [prev_ip] "=m" (prev->thread.ip), \
  49. "=a" (last), \
  50. \
  51. /* clobbered output registers: */ \
  52. "=b" (ebx), "=c" (ecx), "=d" (edx), \
  53. "=S" (esi), "=D" (edi) \
  54. \
  55. /* input parameters: */ \
  56. : [next_sp] "m" (next->thread.sp), \
  57. [next_ip] "m" (next->thread.ip), \
  58. \
  59. /* regparm parameters for __switch_to(): */ \
  60. [prev] "a" (prev), \
  61. [next] "d" (next) \
  62. \
  63. : /* reloaded segment registers */ \
  64. "memory"); \
  65. } while (0)
  66. /*
  67. * disable hlt during certain critical i/o operations
  68. */
  69. #define HAVE_DISABLE_HLT
  70. #else
  71. #define __SAVE(reg, offset) "movq %%" #reg ",(14-" #offset ")*8(%%rsp)\n\t"
  72. #define __RESTORE(reg, offset) "movq (14-" #offset ")*8(%%rsp),%%" #reg "\n\t"
  73. /* frame pointer must be last for get_wchan */
  74. #define SAVE_CONTEXT "pushf ; pushq %%rbp ; movq %%rsi,%%rbp\n\t"
  75. #define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp ; popf\t"
  76. #define __EXTRA_CLOBBER \
  77. , "rcx", "rbx", "rdx", "r8", "r9", "r10", "r11", \
  78. "r12", "r13", "r14", "r15"
  79. #ifdef CONFIG_CC_STACKPROTECTOR
  80. #define __switch_canary \
  81. "movq %P[task_canary](%%rsi),%%r8\n\t" \
  82. "movq %%r8,%%gs:%P[gs_canary]\n\t"
  83. #define __switch_canary_param \
  84. , [task_canary] "i" (offsetof(struct task_struct, stack_canary)) \
  85. , [gs_canary] "i" (offsetof(union irq_stack_union, stack_canary))
  86. #else /* CC_STACKPROTECTOR */
  87. #define __switch_canary
  88. #define __switch_canary_param
  89. #endif /* CC_STACKPROTECTOR */
  90. /* Save restore flags to clear handle leaking NT */
  91. #define switch_to(prev, next, last) \
  92. asm volatile(SAVE_CONTEXT \
  93. "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \
  94. "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \
  95. "call __switch_to\n\t" \
  96. ".globl thread_return\n" \
  97. "thread_return:\n\t" \
  98. "movq "__percpu_arg([current_task])",%%rsi\n\t" \
  99. __switch_canary \
  100. "movq %P[thread_info](%%rsi),%%r8\n\t" \
  101. LOCK_PREFIX "btr %[tif_fork],%P[ti_flags](%%r8)\n\t" \
  102. "movq %%rax,%%rdi\n\t" \
  103. "jc ret_from_fork\n\t" \
  104. RESTORE_CONTEXT \
  105. : "=a" (last) \
  106. : [next] "S" (next), [prev] "D" (prev), \
  107. [threadrsp] "i" (offsetof(struct task_struct, thread.sp)), \
  108. [ti_flags] "i" (offsetof(struct thread_info, flags)), \
  109. [tif_fork] "i" (TIF_FORK), \
  110. [thread_info] "i" (offsetof(struct task_struct, stack)), \
  111. [current_task] "m" (per_cpu_var(current_task)) \
  112. __switch_canary_param \
  113. : "memory", "cc" __EXTRA_CLOBBER)
  114. #endif
  115. #ifdef __KERNEL__
  116. #define _set_base(addr, base) do { unsigned long __pr; \
  117. __asm__ __volatile__ ("movw %%dx,%1\n\t" \
  118. "rorl $16,%%edx\n\t" \
  119. "movb %%dl,%2\n\t" \
  120. "movb %%dh,%3" \
  121. :"=&d" (__pr) \
  122. :"m" (*((addr)+2)), \
  123. "m" (*((addr)+4)), \
  124. "m" (*((addr)+7)), \
  125. "0" (base) \
  126. ); } while (0)
  127. #define _set_limit(addr, limit) do { unsigned long __lr; \
  128. __asm__ __volatile__ ("movw %%dx,%1\n\t" \
  129. "rorl $16,%%edx\n\t" \
  130. "movb %2,%%dh\n\t" \
  131. "andb $0xf0,%%dh\n\t" \
  132. "orb %%dh,%%dl\n\t" \
  133. "movb %%dl,%2" \
  134. :"=&d" (__lr) \
  135. :"m" (*(addr)), \
  136. "m" (*((addr)+6)), \
  137. "0" (limit) \
  138. ); } while (0)
  139. #define set_base(ldt, base) _set_base(((char *)&(ldt)) , (base))
  140. #define set_limit(ldt, limit) _set_limit(((char *)&(ldt)) , ((limit)-1))
  141. extern void native_load_gs_index(unsigned);
  142. /*
  143. * Load a segment. Fall back on loading the zero
  144. * segment if something goes wrong..
  145. */
  146. #define loadsegment(seg, value) \
  147. asm volatile("\n" \
  148. "1:\t" \
  149. "movl %k0,%%" #seg "\n" \
  150. "2:\n" \
  151. ".section .fixup,\"ax\"\n" \
  152. "3:\t" \
  153. "movl %k1, %%" #seg "\n\t" \
  154. "jmp 2b\n" \
  155. ".previous\n" \
  156. _ASM_EXTABLE(1b,3b) \
  157. : :"r" (value), "r" (0) : "memory")
  158. /*
  159. * Save a segment register away
  160. */
  161. #define savesegment(seg, value) \
  162. asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
  163. static inline unsigned long get_limit(unsigned long segment)
  164. {
  165. unsigned long __limit;
  166. asm("lsll %1,%0" : "=r" (__limit) : "r" (segment));
  167. return __limit + 1;
  168. }
  169. static inline void native_clts(void)
  170. {
  171. asm volatile("clts");
  172. }
  173. /*
  174. * Volatile isn't enough to prevent the compiler from reordering the
  175. * read/write functions for the control registers and messing everything up.
  176. * A memory clobber would solve the problem, but would prevent reordering of
  177. * all loads stores around it, which can hurt performance. Solution is to
  178. * use a variable and mimic reads and writes to it to enforce serialization
  179. */
  180. static unsigned long __force_order;
  181. static inline unsigned long native_read_cr0(void)
  182. {
  183. unsigned long val;
  184. asm volatile("mov %%cr0,%0\n\t" : "=r" (val), "=m" (__force_order));
  185. return val;
  186. }
  187. static inline void native_write_cr0(unsigned long val)
  188. {
  189. asm volatile("mov %0,%%cr0": : "r" (val), "m" (__force_order));
  190. }
  191. static inline unsigned long native_read_cr2(void)
  192. {
  193. unsigned long val;
  194. asm volatile("mov %%cr2,%0\n\t" : "=r" (val), "=m" (__force_order));
  195. return val;
  196. }
  197. static inline void native_write_cr2(unsigned long val)
  198. {
  199. asm volatile("mov %0,%%cr2": : "r" (val), "m" (__force_order));
  200. }
  201. static inline unsigned long native_read_cr3(void)
  202. {
  203. unsigned long val;
  204. asm volatile("mov %%cr3,%0\n\t" : "=r" (val), "=m" (__force_order));
  205. return val;
  206. }
  207. static inline void native_write_cr3(unsigned long val)
  208. {
  209. asm volatile("mov %0,%%cr3": : "r" (val), "m" (__force_order));
  210. }
  211. static inline unsigned long native_read_cr4(void)
  212. {
  213. unsigned long val;
  214. asm volatile("mov %%cr4,%0\n\t" : "=r" (val), "=m" (__force_order));
  215. return val;
  216. }
  217. static inline unsigned long native_read_cr4_safe(void)
  218. {
  219. unsigned long val;
  220. /* This could fault if %cr4 does not exist. In x86_64, a cr4 always
  221. * exists, so it will never fail. */
  222. #ifdef CONFIG_X86_32
  223. asm volatile("1: mov %%cr4, %0\n"
  224. "2:\n"
  225. _ASM_EXTABLE(1b, 2b)
  226. : "=r" (val), "=m" (__force_order) : "0" (0));
  227. #else
  228. val = native_read_cr4();
  229. #endif
  230. return val;
  231. }
  232. static inline void native_write_cr4(unsigned long val)
  233. {
  234. asm volatile("mov %0,%%cr4": : "r" (val), "m" (__force_order));
  235. }
  236. #ifdef CONFIG_X86_64
  237. static inline unsigned long native_read_cr8(void)
  238. {
  239. unsigned long cr8;
  240. asm volatile("movq %%cr8,%0" : "=r" (cr8));
  241. return cr8;
  242. }
  243. static inline void native_write_cr8(unsigned long val)
  244. {
  245. asm volatile("movq %0,%%cr8" :: "r" (val) : "memory");
  246. }
  247. #endif
  248. static inline void native_wbinvd(void)
  249. {
  250. asm volatile("wbinvd": : :"memory");
  251. }
  252. #ifdef CONFIG_PARAVIRT
  253. #include <asm/paravirt.h>
  254. #else
  255. #define read_cr0() (native_read_cr0())
  256. #define write_cr0(x) (native_write_cr0(x))
  257. #define read_cr2() (native_read_cr2())
  258. #define write_cr2(x) (native_write_cr2(x))
  259. #define read_cr3() (native_read_cr3())
  260. #define write_cr3(x) (native_write_cr3(x))
  261. #define read_cr4() (native_read_cr4())
  262. #define read_cr4_safe() (native_read_cr4_safe())
  263. #define write_cr4(x) (native_write_cr4(x))
  264. #define wbinvd() (native_wbinvd())
  265. #ifdef CONFIG_X86_64
  266. #define read_cr8() (native_read_cr8())
  267. #define write_cr8(x) (native_write_cr8(x))
  268. #define load_gs_index native_load_gs_index
  269. #endif
  270. /* Clear the 'TS' bit */
  271. #define clts() (native_clts())
  272. #endif/* CONFIG_PARAVIRT */
  273. #define stts() write_cr0(read_cr0() | X86_CR0_TS)
  274. #endif /* __KERNEL__ */
  275. static inline void clflush(volatile void *__p)
  276. {
  277. asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
  278. }
  279. #define nop() asm volatile ("nop")
  280. void disable_hlt(void);
  281. void enable_hlt(void);
  282. void cpu_idle_wait(void);
  283. extern unsigned long arch_align_stack(unsigned long sp);
  284. extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
  285. void default_idle(void);
  286. void stop_this_cpu(void *dummy);
  287. /*
  288. * Force strict CPU ordering.
  289. * And yes, this is required on UP too when we're talking
  290. * to devices.
  291. */
  292. #ifdef CONFIG_X86_32
  293. /*
  294. * Some non-Intel clones support out of order store. wmb() ceases to be a
  295. * nop for these.
  296. */
  297. #define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
  298. #define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
  299. #define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
  300. #else
  301. #define mb() asm volatile("mfence":::"memory")
  302. #define rmb() asm volatile("lfence":::"memory")
  303. #define wmb() asm volatile("sfence" ::: "memory")
  304. #endif
  305. /**
  306. * read_barrier_depends - Flush all pending reads that subsequents reads
  307. * depend on.
  308. *
  309. * No data-dependent reads from memory-like regions are ever reordered
  310. * over this barrier. All reads preceding this primitive are guaranteed
  311. * to access memory (but not necessarily other CPUs' caches) before any
  312. * reads following this primitive that depend on the data return by
  313. * any of the preceding reads. This primitive is much lighter weight than
  314. * rmb() on most CPUs, and is never heavier weight than is
  315. * rmb().
  316. *
  317. * These ordering constraints are respected by both the local CPU
  318. * and the compiler.
  319. *
  320. * Ordering is not guaranteed by anything other than these primitives,
  321. * not even by data dependencies. See the documentation for
  322. * memory_barrier() for examples and URLs to more information.
  323. *
  324. * For example, the following code would force ordering (the initial
  325. * value of "a" is zero, "b" is one, and "p" is "&a"):
  326. *
  327. * <programlisting>
  328. * CPU 0 CPU 1
  329. *
  330. * b = 2;
  331. * memory_barrier();
  332. * p = &b; q = p;
  333. * read_barrier_depends();
  334. * d = *q;
  335. * </programlisting>
  336. *
  337. * because the read of "*q" depends on the read of "p" and these
  338. * two reads are separated by a read_barrier_depends(). However,
  339. * the following code, with the same initial values for "a" and "b":
  340. *
  341. * <programlisting>
  342. * CPU 0 CPU 1
  343. *
  344. * a = 2;
  345. * memory_barrier();
  346. * b = 3; y = b;
  347. * read_barrier_depends();
  348. * x = a;
  349. * </programlisting>
  350. *
  351. * does not enforce ordering, since there is no data dependency between
  352. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  353. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  354. * in cases like this where there are no data dependencies.
  355. **/
  356. #define read_barrier_depends() do { } while (0)
  357. #ifdef CONFIG_SMP
  358. #define smp_mb() mb()
  359. #ifdef CONFIG_X86_PPRO_FENCE
  360. # define smp_rmb() rmb()
  361. #else
  362. # define smp_rmb() barrier()
  363. #endif
  364. #ifdef CONFIG_X86_OOSTORE
  365. # define smp_wmb() wmb()
  366. #else
  367. # define smp_wmb() barrier()
  368. #endif
  369. #define smp_read_barrier_depends() read_barrier_depends()
  370. #define set_mb(var, value) do { (void)xchg(&var, value); } while (0)
  371. #else
  372. #define smp_mb() barrier()
  373. #define smp_rmb() barrier()
  374. #define smp_wmb() barrier()
  375. #define smp_read_barrier_depends() do { } while (0)
  376. #define set_mb(var, value) do { var = value; barrier(); } while (0)
  377. #endif
  378. /*
  379. * Stop RDTSC speculation. This is needed when you need to use RDTSC
  380. * (or get_cycles or vread that possibly accesses the TSC) in a defined
  381. * code region.
  382. *
  383. * (Could use an alternative three way for this if there was one.)
  384. */
  385. static inline void rdtsc_barrier(void)
  386. {
  387. alternative(ASM_NOP3, "mfence", X86_FEATURE_MFENCE_RDTSC);
  388. alternative(ASM_NOP3, "lfence", X86_FEATURE_LFENCE_RDTSC);
  389. }
  390. #endif /* _ASM_X86_SYSTEM_H */