system.h 14 KB

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