system.h 11 KB

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