system.h 13 KB

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