system.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #ifndef __ASM_SYSTEM_H
  2. #define __ASM_SYSTEM_H
  3. #include <linux/config.h>
  4. #include <linux/kernel.h>
  5. #include <asm/segment.h>
  6. #ifdef __KERNEL__
  7. #ifdef CONFIG_SMP
  8. #define LOCK_PREFIX "lock ; "
  9. #else
  10. #define LOCK_PREFIX ""
  11. #endif
  12. #define __STR(x) #x
  13. #define STR(x) __STR(x)
  14. #define __SAVE(reg,offset) "movq %%" #reg ",(14-" #offset ")*8(%%rsp)\n\t"
  15. #define __RESTORE(reg,offset) "movq (14-" #offset ")*8(%%rsp),%%" #reg "\n\t"
  16. /* frame pointer must be last for get_wchan */
  17. #define SAVE_CONTEXT "pushq %%rbp ; movq %%rsi,%%rbp\n\t"
  18. #define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp\n\t"
  19. #define __EXTRA_CLOBBER \
  20. ,"rcx","rbx","rdx","r8","r9","r10","r11","r12","r13","r14","r15"
  21. #define switch_to(prev,next,last) \
  22. asm volatile(SAVE_CONTEXT \
  23. "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \
  24. "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \
  25. "call __switch_to\n\t" \
  26. ".globl thread_return\n" \
  27. "thread_return:\n\t" \
  28. "movq %%gs:%P[pda_pcurrent],%%rsi\n\t" \
  29. "movq %P[thread_info](%%rsi),%%r8\n\t" \
  30. LOCK "btr %[tif_fork],%P[ti_flags](%%r8)\n\t" \
  31. "movq %%rax,%%rdi\n\t" \
  32. "jc ret_from_fork\n\t" \
  33. RESTORE_CONTEXT \
  34. : "=a" (last) \
  35. : [next] "S" (next), [prev] "D" (prev), \
  36. [threadrsp] "i" (offsetof(struct task_struct, thread.rsp)), \
  37. [ti_flags] "i" (offsetof(struct thread_info, flags)),\
  38. [tif_fork] "i" (TIF_FORK), \
  39. [thread_info] "i" (offsetof(struct task_struct, thread_info)), \
  40. [pda_pcurrent] "i" (offsetof(struct x8664_pda, pcurrent)) \
  41. : "memory", "cc" __EXTRA_CLOBBER)
  42. extern void load_gs_index(unsigned);
  43. /*
  44. * Load a segment. Fall back on loading the zero
  45. * segment if something goes wrong..
  46. */
  47. #define loadsegment(seg,value) \
  48. asm volatile("\n" \
  49. "1:\t" \
  50. "movl %k0,%%" #seg "\n" \
  51. "2:\n" \
  52. ".section .fixup,\"ax\"\n" \
  53. "3:\t" \
  54. "movl %1,%%" #seg "\n\t" \
  55. "jmp 2b\n" \
  56. ".previous\n" \
  57. ".section __ex_table,\"a\"\n\t" \
  58. ".align 8\n\t" \
  59. ".quad 1b,3b\n" \
  60. ".previous" \
  61. : :"r" (value), "r" (0))
  62. #ifdef __KERNEL__
  63. struct alt_instr {
  64. __u8 *instr; /* original instruction */
  65. __u8 *replacement;
  66. __u8 cpuid; /* cpuid bit set for replacement */
  67. __u8 instrlen; /* length of original instruction */
  68. __u8 replacementlen; /* length of new instruction, <= instrlen */
  69. __u8 pad[5];
  70. };
  71. #endif
  72. /*
  73. * Alternative instructions for different CPU types or capabilities.
  74. *
  75. * This allows to use optimized instructions even on generic binary
  76. * kernels.
  77. *
  78. * length of oldinstr must be longer or equal the length of newinstr
  79. * It can be padded with nops as needed.
  80. *
  81. * For non barrier like inlines please define new variants
  82. * without volatile and memory clobber.
  83. */
  84. #define alternative(oldinstr, newinstr, feature) \
  85. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  86. ".section .altinstructions,\"a\"\n" \
  87. " .align 8\n" \
  88. " .quad 661b\n" /* label */ \
  89. " .quad 663f\n" /* new instruction */ \
  90. " .byte %c0\n" /* feature bit */ \
  91. " .byte 662b-661b\n" /* sourcelen */ \
  92. " .byte 664f-663f\n" /* replacementlen */ \
  93. ".previous\n" \
  94. ".section .altinstr_replacement,\"ax\"\n" \
  95. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  96. ".previous" :: "i" (feature) : "memory")
  97. /*
  98. * Alternative inline assembly with input.
  99. *
  100. * Peculiarities:
  101. * No memory clobber here.
  102. * Argument numbers start with 1.
  103. * Best is to use constraints that are fixed size (like (%1) ... "r")
  104. * If you use variable sized constraints like "m" or "g" in the
  105. * replacement make sure to pad to the worst case length.
  106. */
  107. #define alternative_input(oldinstr, newinstr, feature, input...) \
  108. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  109. ".section .altinstructions,\"a\"\n" \
  110. " .align 8\n" \
  111. " .quad 661b\n" /* label */ \
  112. " .quad 663f\n" /* new instruction */ \
  113. " .byte %c0\n" /* feature bit */ \
  114. " .byte 662b-661b\n" /* sourcelen */ \
  115. " .byte 664f-663f\n" /* replacementlen */ \
  116. ".previous\n" \
  117. ".section .altinstr_replacement,\"ax\"\n" \
  118. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  119. ".previous" :: "i" (feature), ##input)
  120. /* Like alternative_input, but with a single output argument */
  121. #define alternative_io(oldinstr, newinstr, feature, output, input...) \
  122. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  123. ".section .altinstructions,\"a\"\n" \
  124. " .align 8\n" \
  125. " .quad 661b\n" /* label */ \
  126. " .quad 663f\n" /* new instruction */ \
  127. " .byte %c[feat]\n" /* feature bit */ \
  128. " .byte 662b-661b\n" /* sourcelen */ \
  129. " .byte 664f-663f\n" /* replacementlen */ \
  130. ".previous\n" \
  131. ".section .altinstr_replacement,\"ax\"\n" \
  132. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  133. ".previous" : output : [feat] "i" (feature), ##input)
  134. /*
  135. * Clear and set 'TS' bit respectively
  136. */
  137. #define clts() __asm__ __volatile__ ("clts")
  138. static inline unsigned long read_cr0(void)
  139. {
  140. unsigned long cr0;
  141. asm volatile("movq %%cr0,%0" : "=r" (cr0));
  142. return cr0;
  143. }
  144. static inline void write_cr0(unsigned long val)
  145. {
  146. asm volatile("movq %0,%%cr0" :: "r" (val));
  147. }
  148. static inline unsigned long read_cr3(void)
  149. {
  150. unsigned long cr3;
  151. asm("movq %%cr3,%0" : "=r" (cr3));
  152. return cr3;
  153. }
  154. static inline unsigned long read_cr4(void)
  155. {
  156. unsigned long cr4;
  157. asm("movq %%cr4,%0" : "=r" (cr4));
  158. return cr4;
  159. }
  160. static inline void write_cr4(unsigned long val)
  161. {
  162. asm volatile("movq %0,%%cr4" :: "r" (val));
  163. }
  164. #define stts() write_cr0(8 | read_cr0())
  165. #define wbinvd() \
  166. __asm__ __volatile__ ("wbinvd": : :"memory");
  167. /*
  168. * On SMP systems, when the scheduler does migration-cost autodetection,
  169. * it needs a way to flush as much of the CPU's caches as possible.
  170. */
  171. static inline void sched_cacheflush(void)
  172. {
  173. wbinvd();
  174. }
  175. #endif /* __KERNEL__ */
  176. #define nop() __asm__ __volatile__ ("nop")
  177. #define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
  178. #define tas(ptr) (xchg((ptr),1))
  179. #define __xg(x) ((volatile long *)(x))
  180. static inline void set_64bit(volatile unsigned long *ptr, unsigned long val)
  181. {
  182. *ptr = val;
  183. }
  184. #define _set_64bit set_64bit
  185. /*
  186. * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
  187. * Note 2: xchg has side effect, so that attribute volatile is necessary,
  188. * but generally the primitive is invalid, *ptr is output argument. --ANK
  189. */
  190. static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
  191. {
  192. switch (size) {
  193. case 1:
  194. __asm__ __volatile__("xchgb %b0,%1"
  195. :"=q" (x)
  196. :"m" (*__xg(ptr)), "0" (x)
  197. :"memory");
  198. break;
  199. case 2:
  200. __asm__ __volatile__("xchgw %w0,%1"
  201. :"=r" (x)
  202. :"m" (*__xg(ptr)), "0" (x)
  203. :"memory");
  204. break;
  205. case 4:
  206. __asm__ __volatile__("xchgl %k0,%1"
  207. :"=r" (x)
  208. :"m" (*__xg(ptr)), "0" (x)
  209. :"memory");
  210. break;
  211. case 8:
  212. __asm__ __volatile__("xchgq %0,%1"
  213. :"=r" (x)
  214. :"m" (*__xg(ptr)), "0" (x)
  215. :"memory");
  216. break;
  217. }
  218. return x;
  219. }
  220. /*
  221. * Atomic compare and exchange. Compare OLD with MEM, if identical,
  222. * store NEW in MEM. Return the initial value in MEM. Success is
  223. * indicated by comparing RETURN with OLD.
  224. */
  225. #define __HAVE_ARCH_CMPXCHG 1
  226. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  227. unsigned long new, int size)
  228. {
  229. unsigned long prev;
  230. switch (size) {
  231. case 1:
  232. __asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
  233. : "=a"(prev)
  234. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  235. : "memory");
  236. return prev;
  237. case 2:
  238. __asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
  239. : "=a"(prev)
  240. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  241. : "memory");
  242. return prev;
  243. case 4:
  244. __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %k1,%2"
  245. : "=a"(prev)
  246. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  247. : "memory");
  248. return prev;
  249. case 8:
  250. __asm__ __volatile__(LOCK_PREFIX "cmpxchgq %1,%2"
  251. : "=a"(prev)
  252. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  253. : "memory");
  254. return prev;
  255. }
  256. return old;
  257. }
  258. #define cmpxchg(ptr,o,n)\
  259. ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\
  260. (unsigned long)(n),sizeof(*(ptr))))
  261. #ifdef CONFIG_SMP
  262. #define smp_mb() mb()
  263. #define smp_rmb() rmb()
  264. #define smp_wmb() wmb()
  265. #define smp_read_barrier_depends() do {} while(0)
  266. #else
  267. #define smp_mb() barrier()
  268. #define smp_rmb() barrier()
  269. #define smp_wmb() barrier()
  270. #define smp_read_barrier_depends() do {} while(0)
  271. #endif
  272. /*
  273. * Force strict CPU ordering.
  274. * And yes, this is required on UP too when we're talking
  275. * to devices.
  276. */
  277. #define mb() asm volatile("mfence":::"memory")
  278. #define rmb() asm volatile("lfence":::"memory")
  279. #ifdef CONFIG_UNORDERED_IO
  280. #define wmb() asm volatile("sfence" ::: "memory")
  281. #else
  282. #define wmb() asm volatile("" ::: "memory")
  283. #endif
  284. #define read_barrier_depends() do {} while(0)
  285. #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
  286. #define set_wmb(var, value) do { var = value; wmb(); } while (0)
  287. #define warn_if_not_ulong(x) do { unsigned long foo; (void) (&(x) == &foo); } while (0)
  288. /* interrupt control.. */
  289. #define local_save_flags(x) do { warn_if_not_ulong(x); __asm__ __volatile__("# save_flags \n\t pushfq ; popq %q0":"=g" (x): /* no input */ :"memory"); } while (0)
  290. #define local_irq_restore(x) __asm__ __volatile__("# restore_flags \n\t pushq %0 ; popfq": /* no output */ :"g" (x):"memory", "cc")
  291. #ifdef CONFIG_X86_VSMP
  292. /* Interrupt control for VSMP architecture */
  293. #define local_irq_disable() do { unsigned long flags; local_save_flags(flags); local_irq_restore((flags & ~(1 << 9)) | (1 << 18)); } while (0)
  294. #define local_irq_enable() do { unsigned long flags; local_save_flags(flags); local_irq_restore((flags | (1 << 9)) & ~(1 << 18)); } while (0)
  295. #define irqs_disabled() \
  296. ({ \
  297. unsigned long flags; \
  298. local_save_flags(flags); \
  299. (flags & (1<<18)) || !(flags & (1<<9)); \
  300. })
  301. /* For spinlocks etc */
  302. #define local_irq_save(x) do { local_save_flags(x); local_irq_restore((x & ~(1 << 9)) | (1 << 18)); } while (0)
  303. #else /* CONFIG_X86_VSMP */
  304. #define local_irq_disable() __asm__ __volatile__("cli": : :"memory")
  305. #define local_irq_enable() __asm__ __volatile__("sti": : :"memory")
  306. #define irqs_disabled() \
  307. ({ \
  308. unsigned long flags; \
  309. local_save_flags(flags); \
  310. !(flags & (1<<9)); \
  311. })
  312. /* For spinlocks etc */
  313. #define local_irq_save(x) do { warn_if_not_ulong(x); __asm__ __volatile__("# local_irq_save \n\t pushfq ; popq %0 ; cli":"=g" (x): /* no input */ :"memory"); } while (0)
  314. #endif
  315. /* used in the idle loop; sti takes one instruction cycle to complete */
  316. #define safe_halt() __asm__ __volatile__("sti; hlt": : :"memory")
  317. /* used when interrupts are already enabled or to shutdown the processor */
  318. #define halt() __asm__ __volatile__("hlt": : :"memory")
  319. void cpu_idle_wait(void);
  320. extern unsigned long arch_align_stack(unsigned long sp);
  321. #endif