system.h 14 KB

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