system.h 14 KB

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