system.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. /*
  313. * Force strict CPU ordering.
  314. * And yes, this is required on UP too when we're talking
  315. * to devices.
  316. *
  317. * For now, "wmb()" doesn't actually do anything, as all
  318. * Intel CPU's follow what Intel calls a *Processor Order*,
  319. * in which all writes are seen in the program order even
  320. * outside the CPU.
  321. *
  322. * I expect future Intel CPU's to have a weaker ordering,
  323. * but I'd also expect them to finally get their act together
  324. * and add some real memory barriers if so.
  325. *
  326. * Some non intel clones support out of order store. wmb() ceases to be a
  327. * nop for these.
  328. */
  329. /*
  330. * Actually only lfence would be needed for mb() because all stores done
  331. * by the kernel should be already ordered. But keep a full barrier for now.
  332. */
  333. #define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
  334. #define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
  335. /**
  336. * read_barrier_depends - Flush all pending reads that subsequents reads
  337. * depend on.
  338. *
  339. * No data-dependent reads from memory-like regions are ever reordered
  340. * over this barrier. All reads preceding this primitive are guaranteed
  341. * to access memory (but not necessarily other CPUs' caches) before any
  342. * reads following this primitive that depend on the data return by
  343. * any of the preceding reads. This primitive is much lighter weight than
  344. * rmb() on most CPUs, and is never heavier weight than is
  345. * rmb().
  346. *
  347. * These ordering constraints are respected by both the local CPU
  348. * and the compiler.
  349. *
  350. * Ordering is not guaranteed by anything other than these primitives,
  351. * not even by data dependencies. See the documentation for
  352. * memory_barrier() for examples and URLs to more information.
  353. *
  354. * For example, the following code would force ordering (the initial
  355. * value of "a" is zero, "b" is one, and "p" is "&a"):
  356. *
  357. * <programlisting>
  358. * CPU 0 CPU 1
  359. *
  360. * b = 2;
  361. * memory_barrier();
  362. * p = &b; q = p;
  363. * read_barrier_depends();
  364. * d = *q;
  365. * </programlisting>
  366. *
  367. * because the read of "*q" depends on the read of "p" and these
  368. * two reads are separated by a read_barrier_depends(). However,
  369. * the following code, with the same initial values for "a" and "b":
  370. *
  371. * <programlisting>
  372. * CPU 0 CPU 1
  373. *
  374. * a = 2;
  375. * memory_barrier();
  376. * b = 3; y = b;
  377. * read_barrier_depends();
  378. * x = a;
  379. * </programlisting>
  380. *
  381. * does not enforce ordering, since there is no data dependency between
  382. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  383. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  384. * in cases like thiswhere there are no data dependencies.
  385. **/
  386. #define read_barrier_depends() do { } while(0)
  387. #ifdef CONFIG_X86_OOSTORE
  388. /* Actually there are no OOO store capable CPUs for now that do SSE,
  389. but make it already an possibility. */
  390. #define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
  391. #else
  392. #define wmb() __asm__ __volatile__ ("": : :"memory")
  393. #endif
  394. #ifdef CONFIG_SMP
  395. #define smp_mb() mb()
  396. #define smp_rmb() rmb()
  397. #define smp_wmb() wmb()
  398. #define smp_read_barrier_depends() read_barrier_depends()
  399. #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
  400. #else
  401. #define smp_mb() barrier()
  402. #define smp_rmb() barrier()
  403. #define smp_wmb() barrier()
  404. #define smp_read_barrier_depends() do { } while(0)
  405. #define set_mb(var, value) do { var = value; barrier(); } while (0)
  406. #endif
  407. #define set_wmb(var, value) do { var = value; wmb(); } while (0)
  408. /* interrupt control.. */
  409. #define local_save_flags(x) do { typecheck(unsigned long,x); __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */); } while (0)
  410. #define local_irq_restore(x) do { typecheck(unsigned long,x); __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory", "cc"); } while (0)
  411. #define local_irq_disable() __asm__ __volatile__("cli": : :"memory")
  412. #define local_irq_enable() __asm__ __volatile__("sti": : :"memory")
  413. /* used in the idle loop; sti takes one instruction cycle to complete */
  414. #define safe_halt() __asm__ __volatile__("sti; hlt": : :"memory")
  415. /* used when interrupts are already enabled or to shutdown the processor */
  416. #define halt() __asm__ __volatile__("hlt": : :"memory")
  417. #define irqs_disabled() \
  418. ({ \
  419. unsigned long flags; \
  420. local_save_flags(flags); \
  421. !(flags & (1<<9)); \
  422. })
  423. /* For spinlocks etc */
  424. #define local_irq_save(x) __asm__ __volatile__("pushfl ; popl %0 ; cli":"=g" (x): /* no input */ :"memory")
  425. /*
  426. * disable hlt during certain critical i/o operations
  427. */
  428. #define HAVE_DISABLE_HLT
  429. void disable_hlt(void);
  430. void enable_hlt(void);
  431. extern int es7000_plat;
  432. void cpu_idle_wait(void);
  433. /*
  434. * On SMP systems, when the scheduler does migration-cost autodetection,
  435. * it needs a way to flush as much of the CPU's caches as possible:
  436. */
  437. static inline void sched_cacheflush(void)
  438. {
  439. wbinvd();
  440. }
  441. extern unsigned long arch_align_stack(unsigned long sp);
  442. extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
  443. void default_idle(void);
  444. #endif