system.h 13 KB

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