system.h 11 KB

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