system.h 16 KB

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