system.h 11 KB

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