system.h 11 KB

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