system.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. #ifdef CONFIG_X86_CMPXCHG64
  147. /*
  148. * The semantics of XCHGCMP8B are a bit strange, this is why
  149. * there is a loop and the loading of %%eax and %%edx has to
  150. * be inside. This inlines well in most cases, the cached
  151. * cost is around ~38 cycles. (in the future we might want
  152. * to do an SIMD/3DNOW!/MMX/FPU 64-bit store here, but that
  153. * might have an implicit FPU-save as a cost, so it's not
  154. * clear which path to go.)
  155. *
  156. * cmpxchg8b must be used with the lock prefix here to allow
  157. * the instruction to be executed atomically, see page 3-102
  158. * of the instruction set reference 24319102.pdf. We need
  159. * the reader side to see the coherent 64bit value.
  160. */
  161. static inline void __set_64bit (unsigned long long * ptr,
  162. unsigned int low, unsigned int high)
  163. {
  164. __asm__ __volatile__ (
  165. "\n1:\t"
  166. "movl (%0), %%eax\n\t"
  167. "movl 4(%0), %%edx\n\t"
  168. "lock cmpxchg8b (%0)\n\t"
  169. "jnz 1b"
  170. : /* no outputs */
  171. : "D"(ptr),
  172. "b"(low),
  173. "c"(high)
  174. : "ax","dx","memory");
  175. }
  176. static inline void __set_64bit_constant (unsigned long long *ptr,
  177. unsigned long long value)
  178. {
  179. __set_64bit(ptr,(unsigned int)(value), (unsigned int)((value)>>32ULL));
  180. }
  181. #define ll_low(x) *(((unsigned int*)&(x))+0)
  182. #define ll_high(x) *(((unsigned int*)&(x))+1)
  183. static inline void __set_64bit_var (unsigned long long *ptr,
  184. unsigned long long value)
  185. {
  186. __set_64bit(ptr,ll_low(value), ll_high(value));
  187. }
  188. #define set_64bit(ptr,value) \
  189. (__builtin_constant_p(value) ? \
  190. __set_64bit_constant(ptr, value) : \
  191. __set_64bit_var(ptr, value) )
  192. #define _set_64bit(ptr,value) \
  193. (__builtin_constant_p(value) ? \
  194. __set_64bit(ptr, (unsigned int)(value), (unsigned int)((value)>>32ULL) ) : \
  195. __set_64bit(ptr, ll_low(value), ll_high(value)) )
  196. #endif
  197. /*
  198. * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
  199. * Note 2: xchg has side effect, so that attribute volatile is necessary,
  200. * but generally the primitive is invalid, *ptr is output argument. --ANK
  201. */
  202. static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
  203. {
  204. switch (size) {
  205. case 1:
  206. __asm__ __volatile__("xchgb %b0,%1"
  207. :"=q" (x)
  208. :"m" (*__xg(ptr)), "0" (x)
  209. :"memory");
  210. break;
  211. case 2:
  212. __asm__ __volatile__("xchgw %w0,%1"
  213. :"=r" (x)
  214. :"m" (*__xg(ptr)), "0" (x)
  215. :"memory");
  216. break;
  217. case 4:
  218. __asm__ __volatile__("xchgl %0,%1"
  219. :"=r" (x)
  220. :"m" (*__xg(ptr)), "0" (x)
  221. :"memory");
  222. break;
  223. }
  224. return x;
  225. }
  226. /*
  227. * Atomic compare and exchange. Compare OLD with MEM, if identical,
  228. * store NEW in MEM. Return the initial value in MEM. Success is
  229. * indicated by comparing RETURN with OLD.
  230. */
  231. #ifdef CONFIG_X86_CMPXCHG
  232. #define __HAVE_ARCH_CMPXCHG 1
  233. #define cmpxchg(ptr,o,n)\
  234. ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\
  235. (unsigned long)(n),sizeof(*(ptr))))
  236. #endif
  237. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  238. unsigned long new, int size)
  239. {
  240. unsigned long prev;
  241. switch (size) {
  242. case 1:
  243. __asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
  244. : "=a"(prev)
  245. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  246. : "memory");
  247. return prev;
  248. case 2:
  249. __asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
  250. : "=a"(prev)
  251. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  252. : "memory");
  253. return prev;
  254. case 4:
  255. __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %1,%2"
  256. : "=a"(prev)
  257. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  258. : "memory");
  259. return prev;
  260. }
  261. return old;
  262. }
  263. #ifndef CONFIG_X86_CMPXCHG
  264. /*
  265. * Building a kernel capable running on 80386. It may be necessary to
  266. * simulate the cmpxchg on the 80386 CPU. For that purpose we define
  267. * a function for each of the sizes we support.
  268. */
  269. extern unsigned long cmpxchg_386_u8(volatile void *, u8, u8);
  270. extern unsigned long cmpxchg_386_u16(volatile void *, u16, u16);
  271. extern unsigned long cmpxchg_386_u32(volatile void *, u32, u32);
  272. static inline unsigned long cmpxchg_386(volatile void *ptr, unsigned long old,
  273. unsigned long new, int size)
  274. {
  275. switch (size) {
  276. case 1:
  277. return cmpxchg_386_u8(ptr, old, new);
  278. case 2:
  279. return cmpxchg_386_u16(ptr, old, new);
  280. case 4:
  281. return cmpxchg_386_u32(ptr, old, new);
  282. }
  283. return old;
  284. }
  285. #define cmpxchg(ptr,o,n) \
  286. ({ \
  287. __typeof__(*(ptr)) __ret; \
  288. if (likely(boot_cpu_data.x86 > 3)) \
  289. __ret = __cmpxchg((ptr), (unsigned long)(o), \
  290. (unsigned long)(n), sizeof(*(ptr))); \
  291. else \
  292. __ret = cmpxchg_386((ptr), (unsigned long)(o), \
  293. (unsigned long)(n), sizeof(*(ptr))); \
  294. __ret; \
  295. })
  296. #endif
  297. #ifdef CONFIG_X86_CMPXCHG64
  298. static inline unsigned long long __cmpxchg64(volatile void *ptr, unsigned long long old,
  299. unsigned long long new)
  300. {
  301. unsigned long long prev;
  302. __asm__ __volatile__(LOCK_PREFIX "cmpxchg8b %3"
  303. : "=A"(prev)
  304. : "b"((unsigned long)new),
  305. "c"((unsigned long)(new >> 32)),
  306. "m"(*__xg(ptr)),
  307. "0"(old)
  308. : "memory");
  309. return prev;
  310. }
  311. #define cmpxchg64(ptr,o,n)\
  312. ((__typeof__(*(ptr)))__cmpxchg64((ptr),(unsigned long long)(o),\
  313. (unsigned long long)(n)))
  314. #endif
  315. #ifdef __KERNEL__
  316. struct alt_instr {
  317. __u8 *instr; /* original instruction */
  318. __u8 *replacement;
  319. __u8 cpuid; /* cpuid bit set for replacement */
  320. __u8 instrlen; /* length of original instruction */
  321. __u8 replacementlen; /* length of new instruction, <= instrlen */
  322. __u8 pad;
  323. };
  324. #endif
  325. /*
  326. * Alternative instructions for different CPU types or capabilities.
  327. *
  328. * This allows to use optimized instructions even on generic binary
  329. * kernels.
  330. *
  331. * length of oldinstr must be longer or equal the length of newinstr
  332. * It can be padded with nops as needed.
  333. *
  334. * For non barrier like inlines please define new variants
  335. * without volatile and memory clobber.
  336. */
  337. #define alternative(oldinstr, newinstr, feature) \
  338. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  339. ".section .altinstructions,\"a\"\n" \
  340. " .align 4\n" \
  341. " .long 661b\n" /* label */ \
  342. " .long 663f\n" /* new instruction */ \
  343. " .byte %c0\n" /* feature bit */ \
  344. " .byte 662b-661b\n" /* sourcelen */ \
  345. " .byte 664f-663f\n" /* replacementlen */ \
  346. ".previous\n" \
  347. ".section .altinstr_replacement,\"ax\"\n" \
  348. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  349. ".previous" :: "i" (feature) : "memory")
  350. /*
  351. * Alternative inline assembly with input.
  352. *
  353. * Pecularities:
  354. * No memory clobber here.
  355. * Argument numbers start with 1.
  356. * Best is to use constraints that are fixed size (like (%1) ... "r")
  357. * If you use variable sized constraints like "m" or "g" in the
  358. * replacement maake sure to pad to the worst case length.
  359. */
  360. #define alternative_input(oldinstr, newinstr, feature, input...) \
  361. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  362. ".section .altinstructions,\"a\"\n" \
  363. " .align 4\n" \
  364. " .long 661b\n" /* label */ \
  365. " .long 663f\n" /* new instruction */ \
  366. " .byte %c0\n" /* feature bit */ \
  367. " .byte 662b-661b\n" /* sourcelen */ \
  368. " .byte 664f-663f\n" /* replacementlen */ \
  369. ".previous\n" \
  370. ".section .altinstr_replacement,\"ax\"\n" \
  371. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  372. ".previous" :: "i" (feature), ##input)
  373. /*
  374. * Force strict CPU ordering.
  375. * And yes, this is required on UP too when we're talking
  376. * to devices.
  377. *
  378. * For now, "wmb()" doesn't actually do anything, as all
  379. * Intel CPU's follow what Intel calls a *Processor Order*,
  380. * in which all writes are seen in the program order even
  381. * outside the CPU.
  382. *
  383. * I expect future Intel CPU's to have a weaker ordering,
  384. * but I'd also expect them to finally get their act together
  385. * and add some real memory barriers if so.
  386. *
  387. * Some non intel clones support out of order store. wmb() ceases to be a
  388. * nop for these.
  389. */
  390. /*
  391. * Actually only lfence would be needed for mb() because all stores done
  392. * by the kernel should be already ordered. But keep a full barrier for now.
  393. */
  394. #define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
  395. #define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
  396. /**
  397. * read_barrier_depends - Flush all pending reads that subsequents reads
  398. * depend on.
  399. *
  400. * No data-dependent reads from memory-like regions are ever reordered
  401. * over this barrier. All reads preceding this primitive are guaranteed
  402. * to access memory (but not necessarily other CPUs' caches) before any
  403. * reads following this primitive that depend on the data return by
  404. * any of the preceding reads. This primitive is much lighter weight than
  405. * rmb() on most CPUs, and is never heavier weight than is
  406. * rmb().
  407. *
  408. * These ordering constraints are respected by both the local CPU
  409. * and the compiler.
  410. *
  411. * Ordering is not guaranteed by anything other than these primitives,
  412. * not even by data dependencies. See the documentation for
  413. * memory_barrier() for examples and URLs to more information.
  414. *
  415. * For example, the following code would force ordering (the initial
  416. * value of "a" is zero, "b" is one, and "p" is "&a"):
  417. *
  418. * <programlisting>
  419. * CPU 0 CPU 1
  420. *
  421. * b = 2;
  422. * memory_barrier();
  423. * p = &b; q = p;
  424. * read_barrier_depends();
  425. * d = *q;
  426. * </programlisting>
  427. *
  428. * because the read of "*q" depends on the read of "p" and these
  429. * two reads are separated by a read_barrier_depends(). However,
  430. * the following code, with the same initial values for "a" and "b":
  431. *
  432. * <programlisting>
  433. * CPU 0 CPU 1
  434. *
  435. * a = 2;
  436. * memory_barrier();
  437. * b = 3; y = b;
  438. * read_barrier_depends();
  439. * x = a;
  440. * </programlisting>
  441. *
  442. * does not enforce ordering, since there is no data dependency between
  443. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  444. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  445. * in cases like thiswhere there are no data dependencies.
  446. **/
  447. #define read_barrier_depends() do { } while(0)
  448. #ifdef CONFIG_X86_OOSTORE
  449. /* Actually there are no OOO store capable CPUs for now that do SSE,
  450. but make it already an possibility. */
  451. #define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
  452. #else
  453. #define wmb() __asm__ __volatile__ ("": : :"memory")
  454. #endif
  455. #ifdef CONFIG_SMP
  456. #define smp_mb() mb()
  457. #define smp_rmb() rmb()
  458. #define smp_wmb() wmb()
  459. #define smp_read_barrier_depends() read_barrier_depends()
  460. #define set_mb(var, value) do { xchg(&var, value); } while (0)
  461. #else
  462. #define smp_mb() barrier()
  463. #define smp_rmb() barrier()
  464. #define smp_wmb() barrier()
  465. #define smp_read_barrier_depends() do { } while(0)
  466. #define set_mb(var, value) do { var = value; barrier(); } while (0)
  467. #endif
  468. #define set_wmb(var, value) do { var = value; wmb(); } while (0)
  469. /* interrupt control.. */
  470. #define local_save_flags(x) do { typecheck(unsigned long,x); __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */); } while (0)
  471. #define local_irq_restore(x) do { typecheck(unsigned long,x); __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory", "cc"); } while (0)
  472. #define local_irq_disable() __asm__ __volatile__("cli": : :"memory")
  473. #define local_irq_enable() __asm__ __volatile__("sti": : :"memory")
  474. /* used in the idle loop; sti takes one instruction cycle to complete */
  475. #define safe_halt() __asm__ __volatile__("sti; hlt": : :"memory")
  476. /* used when interrupts are already enabled or to shutdown the processor */
  477. #define halt() __asm__ __volatile__("hlt": : :"memory")
  478. #define irqs_disabled() \
  479. ({ \
  480. unsigned long flags; \
  481. local_save_flags(flags); \
  482. !(flags & (1<<9)); \
  483. })
  484. /* For spinlocks etc */
  485. #define local_irq_save(x) __asm__ __volatile__("pushfl ; popl %0 ; cli":"=g" (x): /* no input */ :"memory")
  486. /*
  487. * disable hlt during certain critical i/o operations
  488. */
  489. #define HAVE_DISABLE_HLT
  490. void disable_hlt(void);
  491. void enable_hlt(void);
  492. extern int es7000_plat;
  493. void cpu_idle_wait(void);
  494. extern unsigned long arch_align_stack(unsigned long sp);
  495. #endif