system.h 12 KB

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