system.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. #include <asm/cpufeature.h>
  7. #include <linux/bitops.h> /* for LOCK_PREFIX */
  8. #ifdef __KERNEL__
  9. struct task_struct; /* one of the stranger aspects of C forward declarations.. */
  10. extern struct task_struct * FASTCALL(__switch_to(struct task_struct *prev, struct task_struct *next));
  11. #define switch_to(prev,next,last) do { \
  12. unsigned long esi,edi; \
  13. asm volatile("pushfl\n\t" \
  14. "pushl %%ebp\n\t" \
  15. "movl %%esp,%0\n\t" /* save ESP */ \
  16. "movl %5,%%esp\n\t" /* restore ESP */ \
  17. "movl $1f,%1\n\t" /* save EIP */ \
  18. "pushl %6\n\t" /* restore EIP */ \
  19. "jmp __switch_to\n" \
  20. "1:\t" \
  21. "popl %%ebp\n\t" \
  22. "popfl" \
  23. :"=m" (prev->thread.esp),"=m" (prev->thread.eip), \
  24. "=a" (last),"=S" (esi),"=D" (edi) \
  25. :"m" (next->thread.esp),"m" (next->thread.eip), \
  26. "2" (prev), "d" (next)); \
  27. } while (0)
  28. #define _set_base(addr,base) do { unsigned long __pr; \
  29. __asm__ __volatile__ ("movw %%dx,%1\n\t" \
  30. "rorl $16,%%edx\n\t" \
  31. "movb %%dl,%2\n\t" \
  32. "movb %%dh,%3" \
  33. :"=&d" (__pr) \
  34. :"m" (*((addr)+2)), \
  35. "m" (*((addr)+4)), \
  36. "m" (*((addr)+7)), \
  37. "0" (base) \
  38. ); } while(0)
  39. #define _set_limit(addr,limit) do { unsigned long __lr; \
  40. __asm__ __volatile__ ("movw %%dx,%1\n\t" \
  41. "rorl $16,%%edx\n\t" \
  42. "movb %2,%%dh\n\t" \
  43. "andb $0xf0,%%dh\n\t" \
  44. "orb %%dh,%%dl\n\t" \
  45. "movb %%dl,%2" \
  46. :"=&d" (__lr) \
  47. :"m" (*(addr)), \
  48. "m" (*((addr)+6)), \
  49. "0" (limit) \
  50. ); } while(0)
  51. #define set_base(ldt,base) _set_base( ((char *)&(ldt)) , (base) )
  52. #define set_limit(ldt,limit) _set_limit( ((char *)&(ldt)) , ((limit)-1)>>12 )
  53. static inline unsigned long _get_base(char * addr)
  54. {
  55. unsigned long __base;
  56. __asm__("movb %3,%%dh\n\t"
  57. "movb %2,%%dl\n\t"
  58. "shll $16,%%edx\n\t"
  59. "movw %1,%%dx"
  60. :"=&d" (__base)
  61. :"m" (*((addr)+2)),
  62. "m" (*((addr)+4)),
  63. "m" (*((addr)+7)));
  64. return __base;
  65. }
  66. #define get_base(ldt) _get_base( ((char *)&(ldt)) )
  67. /*
  68. * Load a segment. Fall back on loading the zero
  69. * segment if something goes wrong..
  70. */
  71. #define loadsegment(seg,value) \
  72. asm volatile("\n" \
  73. "1:\t" \
  74. "mov %0,%%" #seg "\n" \
  75. "2:\n" \
  76. ".section .fixup,\"ax\"\n" \
  77. "3:\t" \
  78. "pushl $0\n\t" \
  79. "popl %%" #seg "\n\t" \
  80. "jmp 2b\n" \
  81. ".previous\n" \
  82. ".section __ex_table,\"a\"\n\t" \
  83. ".align 4\n\t" \
  84. ".long 1b,3b\n" \
  85. ".previous" \
  86. : :"m" (value))
  87. /*
  88. * Save a segment register away
  89. */
  90. #define savesegment(seg, value) \
  91. asm volatile("mov %%" #seg ",%0":"=m" (value))
  92. /*
  93. * Clear and set 'TS' bit respectively
  94. */
  95. #define clts() __asm__ __volatile__ ("clts")
  96. #define read_cr0() ({ \
  97. unsigned int __dummy; \
  98. __asm__( \
  99. "movl %%cr0,%0\n\t" \
  100. :"=r" (__dummy)); \
  101. __dummy; \
  102. })
  103. #define write_cr0(x) \
  104. __asm__("movl %0,%%cr0": :"r" (x));
  105. #define read_cr4() ({ \
  106. unsigned int __dummy; \
  107. __asm__( \
  108. "movl %%cr4,%0\n\t" \
  109. :"=r" (__dummy)); \
  110. __dummy; \
  111. })
  112. #define write_cr4(x) \
  113. __asm__("movl %0,%%cr4": :"r" (x));
  114. #define stts() write_cr0(8 | read_cr0())
  115. #endif /* __KERNEL__ */
  116. #define wbinvd() \
  117. __asm__ __volatile__ ("wbinvd": : :"memory");
  118. static inline unsigned long get_limit(unsigned long segment)
  119. {
  120. unsigned long __limit;
  121. __asm__("lsll %1,%0"
  122. :"=r" (__limit):"r" (segment));
  123. return __limit+1;
  124. }
  125. #define nop() __asm__ __volatile__ ("nop")
  126. #define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
  127. #define tas(ptr) (xchg((ptr),1))
  128. struct __xchg_dummy { unsigned long a[100]; };
  129. #define __xg(x) ((struct __xchg_dummy *)(x))
  130. /*
  131. * The semantics of XCHGCMP8B are a bit strange, this is why
  132. * there is a loop and the loading of %%eax and %%edx has to
  133. * be inside. This inlines well in most cases, the cached
  134. * cost is around ~38 cycles. (in the future we might want
  135. * to do an SIMD/3DNOW!/MMX/FPU 64-bit store here, but that
  136. * might have an implicit FPU-save as a cost, so it's not
  137. * clear which path to go.)
  138. *
  139. * cmpxchg8b must be used with the lock prefix here to allow
  140. * the instruction to be executed atomically, see page 3-102
  141. * of the instruction set reference 24319102.pdf. We need
  142. * the reader side to see the coherent 64bit value.
  143. */
  144. static inline void __set_64bit (unsigned long long * ptr,
  145. unsigned int low, unsigned int high)
  146. {
  147. __asm__ __volatile__ (
  148. "\n1:\t"
  149. "movl (%0), %%eax\n\t"
  150. "movl 4(%0), %%edx\n\t"
  151. "lock cmpxchg8b (%0)\n\t"
  152. "jnz 1b"
  153. : /* no outputs */
  154. : "D"(ptr),
  155. "b"(low),
  156. "c"(high)
  157. : "ax","dx","memory");
  158. }
  159. static inline void __set_64bit_constant (unsigned long long *ptr,
  160. unsigned long long value)
  161. {
  162. __set_64bit(ptr,(unsigned int)(value), (unsigned int)((value)>>32ULL));
  163. }
  164. #define ll_low(x) *(((unsigned int*)&(x))+0)
  165. #define ll_high(x) *(((unsigned int*)&(x))+1)
  166. static inline void __set_64bit_var (unsigned long long *ptr,
  167. unsigned long long value)
  168. {
  169. __set_64bit(ptr,ll_low(value), ll_high(value));
  170. }
  171. #define set_64bit(ptr,value) \
  172. (__builtin_constant_p(value) ? \
  173. __set_64bit_constant(ptr, value) : \
  174. __set_64bit_var(ptr, value) )
  175. #define _set_64bit(ptr,value) \
  176. (__builtin_constant_p(value) ? \
  177. __set_64bit(ptr, (unsigned int)(value), (unsigned int)((value)>>32ULL) ) : \
  178. __set_64bit(ptr, ll_low(value), ll_high(value)) )
  179. /*
  180. * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
  181. * Note 2: xchg has side effect, so that attribute volatile is necessary,
  182. * but generally the primitive is invalid, *ptr is output argument. --ANK
  183. */
  184. static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
  185. {
  186. switch (size) {
  187. case 1:
  188. __asm__ __volatile__("xchgb %b0,%1"
  189. :"=q" (x)
  190. :"m" (*__xg(ptr)), "0" (x)
  191. :"memory");
  192. break;
  193. case 2:
  194. __asm__ __volatile__("xchgw %w0,%1"
  195. :"=r" (x)
  196. :"m" (*__xg(ptr)), "0" (x)
  197. :"memory");
  198. break;
  199. case 4:
  200. __asm__ __volatile__("xchgl %0,%1"
  201. :"=r" (x)
  202. :"m" (*__xg(ptr)), "0" (x)
  203. :"memory");
  204. break;
  205. }
  206. return x;
  207. }
  208. /*
  209. * Atomic compare and exchange. Compare OLD with MEM, if identical,
  210. * store NEW in MEM. Return the initial value in MEM. Success is
  211. * indicated by comparing RETURN with OLD.
  212. */
  213. #ifdef CONFIG_X86_CMPXCHG
  214. #define __HAVE_ARCH_CMPXCHG 1
  215. #endif
  216. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  217. unsigned long new, int size)
  218. {
  219. unsigned long prev;
  220. switch (size) {
  221. case 1:
  222. __asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
  223. : "=a"(prev)
  224. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  225. : "memory");
  226. return prev;
  227. case 2:
  228. __asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
  229. : "=a"(prev)
  230. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  231. : "memory");
  232. return prev;
  233. case 4:
  234. __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %1,%2"
  235. : "=a"(prev)
  236. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  237. : "memory");
  238. return prev;
  239. }
  240. return old;
  241. }
  242. #define cmpxchg(ptr,o,n)\
  243. ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\
  244. (unsigned long)(n),sizeof(*(ptr))))
  245. #ifdef __KERNEL__
  246. struct alt_instr {
  247. __u8 *instr; /* original instruction */
  248. __u8 *replacement;
  249. __u8 cpuid; /* cpuid bit set for replacement */
  250. __u8 instrlen; /* length of original instruction */
  251. __u8 replacementlen; /* length of new instruction, <= instrlen */
  252. __u8 pad;
  253. };
  254. #endif
  255. /*
  256. * Alternative instructions for different CPU types or capabilities.
  257. *
  258. * This allows to use optimized instructions even on generic binary
  259. * kernels.
  260. *
  261. * length of oldinstr must be longer or equal the length of newinstr
  262. * It can be padded with nops as needed.
  263. *
  264. * For non barrier like inlines please define new variants
  265. * without volatile and memory clobber.
  266. */
  267. #define alternative(oldinstr, newinstr, feature) \
  268. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  269. ".section .altinstructions,\"a\"\n" \
  270. " .align 4\n" \
  271. " .long 661b\n" /* label */ \
  272. " .long 663f\n" /* new instruction */ \
  273. " .byte %c0\n" /* feature bit */ \
  274. " .byte 662b-661b\n" /* sourcelen */ \
  275. " .byte 664f-663f\n" /* replacementlen */ \
  276. ".previous\n" \
  277. ".section .altinstr_replacement,\"ax\"\n" \
  278. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  279. ".previous" :: "i" (feature) : "memory")
  280. /*
  281. * Alternative inline assembly with input.
  282. *
  283. * Pecularities:
  284. * No memory clobber here.
  285. * Argument numbers start with 1.
  286. * Best is to use constraints that are fixed size (like (%1) ... "r")
  287. * If you use variable sized constraints like "m" or "g" in the
  288. * replacement maake sure to pad to the worst case length.
  289. */
  290. #define alternative_input(oldinstr, newinstr, feature, input...) \
  291. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  292. ".section .altinstructions,\"a\"\n" \
  293. " .align 4\n" \
  294. " .long 661b\n" /* label */ \
  295. " .long 663f\n" /* new instruction */ \
  296. " .byte %c0\n" /* feature bit */ \
  297. " .byte 662b-661b\n" /* sourcelen */ \
  298. " .byte 664f-663f\n" /* replacementlen */ \
  299. ".previous\n" \
  300. ".section .altinstr_replacement,\"ax\"\n" \
  301. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  302. ".previous" :: "i" (feature), ##input)
  303. /*
  304. * Force strict CPU ordering.
  305. * And yes, this is required on UP too when we're talking
  306. * to devices.
  307. *
  308. * For now, "wmb()" doesn't actually do anything, as all
  309. * Intel CPU's follow what Intel calls a *Processor Order*,
  310. * in which all writes are seen in the program order even
  311. * outside the CPU.
  312. *
  313. * I expect future Intel CPU's to have a weaker ordering,
  314. * but I'd also expect them to finally get their act together
  315. * and add some real memory barriers if so.
  316. *
  317. * Some non intel clones support out of order store. wmb() ceases to be a
  318. * nop for these.
  319. */
  320. /*
  321. * Actually only lfence would be needed for mb() because all stores done
  322. * by the kernel should be already ordered. But keep a full barrier for now.
  323. */
  324. #define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
  325. #define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
  326. /**
  327. * read_barrier_depends - Flush all pending reads that subsequents reads
  328. * depend on.
  329. *
  330. * No data-dependent reads from memory-like regions are ever reordered
  331. * over this barrier. All reads preceding this primitive are guaranteed
  332. * to access memory (but not necessarily other CPUs' caches) before any
  333. * reads following this primitive that depend on the data return by
  334. * any of the preceding reads. This primitive is much lighter weight than
  335. * rmb() on most CPUs, and is never heavier weight than is
  336. * rmb().
  337. *
  338. * These ordering constraints are respected by both the local CPU
  339. * and the compiler.
  340. *
  341. * Ordering is not guaranteed by anything other than these primitives,
  342. * not even by data dependencies. See the documentation for
  343. * memory_barrier() for examples and URLs to more information.
  344. *
  345. * For example, the following code would force ordering (the initial
  346. * value of "a" is zero, "b" is one, and "p" is "&a"):
  347. *
  348. * <programlisting>
  349. * CPU 0 CPU 1
  350. *
  351. * b = 2;
  352. * memory_barrier();
  353. * p = &b; q = p;
  354. * read_barrier_depends();
  355. * d = *q;
  356. * </programlisting>
  357. *
  358. * because the read of "*q" depends on the read of "p" and these
  359. * two reads are separated by a read_barrier_depends(). However,
  360. * the following code, with the same initial values for "a" and "b":
  361. *
  362. * <programlisting>
  363. * CPU 0 CPU 1
  364. *
  365. * a = 2;
  366. * memory_barrier();
  367. * b = 3; y = b;
  368. * read_barrier_depends();
  369. * x = a;
  370. * </programlisting>
  371. *
  372. * does not enforce ordering, since there is no data dependency between
  373. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  374. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  375. * in cases like thiswhere there are no data dependencies.
  376. **/
  377. #define read_barrier_depends() do { } while(0)
  378. #ifdef CONFIG_X86_OOSTORE
  379. /* Actually there are no OOO store capable CPUs for now that do SSE,
  380. but make it already an possibility. */
  381. #define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
  382. #else
  383. #define wmb() __asm__ __volatile__ ("": : :"memory")
  384. #endif
  385. #ifdef CONFIG_SMP
  386. #define smp_mb() mb()
  387. #define smp_rmb() rmb()
  388. #define smp_wmb() wmb()
  389. #define smp_read_barrier_depends() read_barrier_depends()
  390. #define set_mb(var, value) do { xchg(&var, value); } while (0)
  391. #else
  392. #define smp_mb() barrier()
  393. #define smp_rmb() barrier()
  394. #define smp_wmb() barrier()
  395. #define smp_read_barrier_depends() do { } while(0)
  396. #define set_mb(var, value) do { var = value; barrier(); } while (0)
  397. #endif
  398. #define set_wmb(var, value) do { var = value; wmb(); } while (0)
  399. /* interrupt control.. */
  400. #define local_save_flags(x) do { typecheck(unsigned long,x); __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */); } while (0)
  401. #define local_irq_restore(x) do { typecheck(unsigned long,x); __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory", "cc"); } while (0)
  402. #define local_irq_disable() __asm__ __volatile__("cli": : :"memory")
  403. #define local_irq_enable() __asm__ __volatile__("sti": : :"memory")
  404. /* used in the idle loop; sti takes one instruction cycle to complete */
  405. #define safe_halt() __asm__ __volatile__("sti; hlt": : :"memory")
  406. #define irqs_disabled() \
  407. ({ \
  408. unsigned long flags; \
  409. local_save_flags(flags); \
  410. !(flags & (1<<9)); \
  411. })
  412. /* For spinlocks etc */
  413. #define local_irq_save(x) __asm__ __volatile__("pushfl ; popl %0 ; cli":"=g" (x): /* no input */ :"memory")
  414. /*
  415. * disable hlt during certain critical i/o operations
  416. */
  417. #define HAVE_DISABLE_HLT
  418. void disable_hlt(void);
  419. void enable_hlt(void);
  420. extern int es7000_plat;
  421. void cpu_idle_wait(void);
  422. extern unsigned long arch_align_stack(unsigned long sp);
  423. #endif