system.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 "pushfq ; pushq %%rbp ; movq %%rsi,%%rbp\n\t"
  18. #define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp ; popfq\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. /*
  125. * Clear and set 'TS' bit respectively
  126. */
  127. #define clts() __asm__ __volatile__ ("clts")
  128. static inline unsigned long read_cr0(void)
  129. {
  130. unsigned long cr0;
  131. asm volatile("movq %%cr0,%0" : "=r" (cr0));
  132. return cr0;
  133. }
  134. static inline void write_cr0(unsigned long val)
  135. {
  136. asm volatile("movq %0,%%cr0" :: "r" (val));
  137. }
  138. static inline unsigned long read_cr3(void)
  139. {
  140. unsigned long cr3;
  141. asm("movq %%cr3,%0" : "=r" (cr3));
  142. return cr3;
  143. }
  144. static inline unsigned long read_cr4(void)
  145. {
  146. unsigned long cr4;
  147. asm("movq %%cr4,%0" : "=r" (cr4));
  148. return cr4;
  149. }
  150. static inline void write_cr4(unsigned long val)
  151. {
  152. asm volatile("movq %0,%%cr4" :: "r" (val));
  153. }
  154. #define stts() write_cr0(8 | read_cr0())
  155. #define wbinvd() \
  156. __asm__ __volatile__ ("wbinvd": : :"memory");
  157. #endif /* __KERNEL__ */
  158. #define nop() __asm__ __volatile__ ("nop")
  159. #define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
  160. #define tas(ptr) (xchg((ptr),1))
  161. #define __xg(x) ((volatile long *)(x))
  162. extern inline void set_64bit(volatile unsigned long *ptr, unsigned long val)
  163. {
  164. *ptr = val;
  165. }
  166. #define _set_64bit set_64bit
  167. /*
  168. * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
  169. * Note 2: xchg has side effect, so that attribute volatile is necessary,
  170. * but generally the primitive is invalid, *ptr is output argument. --ANK
  171. */
  172. static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
  173. {
  174. switch (size) {
  175. case 1:
  176. __asm__ __volatile__("xchgb %b0,%1"
  177. :"=q" (x)
  178. :"m" (*__xg(ptr)), "0" (x)
  179. :"memory");
  180. break;
  181. case 2:
  182. __asm__ __volatile__("xchgw %w0,%1"
  183. :"=r" (x)
  184. :"m" (*__xg(ptr)), "0" (x)
  185. :"memory");
  186. break;
  187. case 4:
  188. __asm__ __volatile__("xchgl %k0,%1"
  189. :"=r" (x)
  190. :"m" (*__xg(ptr)), "0" (x)
  191. :"memory");
  192. break;
  193. case 8:
  194. __asm__ __volatile__("xchgq %0,%1"
  195. :"=r" (x)
  196. :"m" (*__xg(ptr)), "0" (x)
  197. :"memory");
  198. break;
  199. }
  200. return x;
  201. }
  202. /*
  203. * Atomic compare and exchange. Compare OLD with MEM, if identical,
  204. * store NEW in MEM. Return the initial value in MEM. Success is
  205. * indicated by comparing RETURN with OLD.
  206. */
  207. #define __HAVE_ARCH_CMPXCHG 1
  208. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  209. unsigned long new, int size)
  210. {
  211. unsigned long prev;
  212. switch (size) {
  213. case 1:
  214. __asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
  215. : "=a"(prev)
  216. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  217. : "memory");
  218. return prev;
  219. case 2:
  220. __asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
  221. : "=a"(prev)
  222. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  223. : "memory");
  224. return prev;
  225. case 4:
  226. __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %k1,%2"
  227. : "=a"(prev)
  228. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  229. : "memory");
  230. return prev;
  231. case 8:
  232. __asm__ __volatile__(LOCK_PREFIX "cmpxchgq %1,%2"
  233. : "=a"(prev)
  234. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  235. : "memory");
  236. return prev;
  237. }
  238. return old;
  239. }
  240. #define cmpxchg(ptr,o,n)\
  241. ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\
  242. (unsigned long)(n),sizeof(*(ptr))))
  243. #ifdef CONFIG_SMP
  244. #define smp_mb() mb()
  245. #define smp_rmb() rmb()
  246. #define smp_wmb() wmb()
  247. #define smp_read_barrier_depends() do {} while(0)
  248. #else
  249. #define smp_mb() barrier()
  250. #define smp_rmb() barrier()
  251. #define smp_wmb() barrier()
  252. #define smp_read_barrier_depends() do {} while(0)
  253. #endif
  254. /*
  255. * Force strict CPU ordering.
  256. * And yes, this is required on UP too when we're talking
  257. * to devices.
  258. */
  259. #define mb() asm volatile("mfence":::"memory")
  260. #define rmb() asm volatile("lfence":::"memory")
  261. #ifdef CONFIG_UNORDERED_IO
  262. #define wmb() asm volatile("sfence" ::: "memory")
  263. #else
  264. #define wmb() asm volatile("" ::: "memory")
  265. #endif
  266. #define read_barrier_depends() do {} while(0)
  267. #define set_mb(var, value) do { xchg(&var, value); } while (0)
  268. #define set_wmb(var, value) do { var = value; wmb(); } while (0)
  269. #define warn_if_not_ulong(x) do { unsigned long foo; (void) (&(x) == &foo); } while (0)
  270. /* interrupt control.. */
  271. #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)
  272. #define local_irq_restore(x) __asm__ __volatile__("# restore_flags \n\t pushq %0 ; popfq": /* no output */ :"g" (x):"memory", "cc")
  273. #define local_irq_disable() __asm__ __volatile__("cli": : :"memory")
  274. #define local_irq_enable() __asm__ __volatile__("sti": : :"memory")
  275. /* used in the idle loop; sti takes one instruction cycle to complete */
  276. #define safe_halt() __asm__ __volatile__("sti; hlt": : :"memory")
  277. #define irqs_disabled() \
  278. ({ \
  279. unsigned long flags; \
  280. local_save_flags(flags); \
  281. !(flags & (1<<9)); \
  282. })
  283. /* For spinlocks etc */
  284. #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)
  285. void cpu_idle_wait(void);
  286. /*
  287. * disable hlt during certain critical i/o operations
  288. */
  289. #define HAVE_DISABLE_HLT
  290. void disable_hlt(void);
  291. void enable_hlt(void);
  292. extern unsigned long arch_align_stack(unsigned long sp);
  293. #endif