system.h 14 KB

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