system.h 11 KB

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