system.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <asm/cmpxchg.h>
  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. static inline void native_clts(void)
  82. {
  83. asm volatile ("clts");
  84. }
  85. static inline unsigned long native_read_cr0(void)
  86. {
  87. unsigned long val;
  88. asm volatile("movl %%cr0,%0\n\t" :"=r" (val));
  89. return val;
  90. }
  91. static inline void native_write_cr0(unsigned long val)
  92. {
  93. asm volatile("movl %0,%%cr0": :"r" (val));
  94. }
  95. static inline unsigned long native_read_cr2(void)
  96. {
  97. unsigned long val;
  98. asm volatile("movl %%cr2,%0\n\t" :"=r" (val));
  99. return val;
  100. }
  101. static inline void native_write_cr2(unsigned long val)
  102. {
  103. asm volatile("movl %0,%%cr2": :"r" (val));
  104. }
  105. static inline unsigned long native_read_cr3(void)
  106. {
  107. unsigned long val;
  108. asm volatile("movl %%cr3,%0\n\t" :"=r" (val));
  109. return val;
  110. }
  111. static inline void native_write_cr3(unsigned long val)
  112. {
  113. asm volatile("movl %0,%%cr3": :"r" (val));
  114. }
  115. static inline unsigned long native_read_cr4(void)
  116. {
  117. unsigned long val;
  118. asm volatile("movl %%cr4,%0\n\t" :"=r" (val));
  119. return val;
  120. }
  121. static inline unsigned long native_read_cr4_safe(void)
  122. {
  123. unsigned long val;
  124. /* This could fault if %cr4 does not exist */
  125. asm("1: movl %%cr4, %0 \n"
  126. "2: \n"
  127. ".section __ex_table,\"a\" \n"
  128. ".long 1b,2b \n"
  129. ".previous \n"
  130. : "=r" (val): "0" (0));
  131. return val;
  132. }
  133. static inline void native_write_cr4(unsigned long val)
  134. {
  135. asm volatile("movl %0,%%cr4": :"r" (val));
  136. }
  137. static inline void native_wbinvd(void)
  138. {
  139. asm volatile("wbinvd": : :"memory");
  140. }
  141. #ifdef CONFIG_PARAVIRT
  142. #include <asm/paravirt.h>
  143. #else
  144. #define read_cr0() (native_read_cr0())
  145. #define write_cr0(x) (native_write_cr0(x))
  146. #define read_cr2() (native_read_cr2())
  147. #define write_cr2(x) (native_write_cr2(x))
  148. #define read_cr3() (native_read_cr3())
  149. #define write_cr3(x) (native_write_cr3(x))
  150. #define read_cr4() (native_read_cr4())
  151. #define read_cr4_safe() (native_read_cr4_safe())
  152. #define write_cr4(x) (native_write_cr4(x))
  153. #define wbinvd() (native_wbinvd())
  154. /* Clear the 'TS' bit */
  155. #define clts() (native_clts())
  156. #endif/* CONFIG_PARAVIRT */
  157. /* Set the 'TS' bit */
  158. #define stts() write_cr0(8 | read_cr0())
  159. #endif /* __KERNEL__ */
  160. static inline unsigned long get_limit(unsigned long segment)
  161. {
  162. unsigned long __limit;
  163. __asm__("lsll %1,%0"
  164. :"=r" (__limit):"r" (segment));
  165. return __limit+1;
  166. }
  167. #define nop() __asm__ __volatile__ ("nop")
  168. /*
  169. * Force strict CPU ordering.
  170. * And yes, this is required on UP too when we're talking
  171. * to devices.
  172. *
  173. * For now, "wmb()" doesn't actually do anything, as all
  174. * Intel CPU's follow what Intel calls a *Processor Order*,
  175. * in which all writes are seen in the program order even
  176. * outside the CPU.
  177. *
  178. * I expect future Intel CPU's to have a weaker ordering,
  179. * but I'd also expect them to finally get their act together
  180. * and add some real memory barriers if so.
  181. *
  182. * Some non intel clones support out of order store. wmb() ceases to be a
  183. * nop for these.
  184. */
  185. /*
  186. * Actually only lfence would be needed for mb() because all stores done
  187. * by the kernel should be already ordered. But keep a full barrier for now.
  188. */
  189. #define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
  190. #define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
  191. /**
  192. * read_barrier_depends - Flush all pending reads that subsequents reads
  193. * depend on.
  194. *
  195. * No data-dependent reads from memory-like regions are ever reordered
  196. * over this barrier. All reads preceding this primitive are guaranteed
  197. * to access memory (but not necessarily other CPUs' caches) before any
  198. * reads following this primitive that depend on the data return by
  199. * any of the preceding reads. This primitive is much lighter weight than
  200. * rmb() on most CPUs, and is never heavier weight than is
  201. * rmb().
  202. *
  203. * These ordering constraints are respected by both the local CPU
  204. * and the compiler.
  205. *
  206. * Ordering is not guaranteed by anything other than these primitives,
  207. * not even by data dependencies. See the documentation for
  208. * memory_barrier() for examples and URLs to more information.
  209. *
  210. * For example, the following code would force ordering (the initial
  211. * value of "a" is zero, "b" is one, and "p" is "&a"):
  212. *
  213. * <programlisting>
  214. * CPU 0 CPU 1
  215. *
  216. * b = 2;
  217. * memory_barrier();
  218. * p = &b; q = p;
  219. * read_barrier_depends();
  220. * d = *q;
  221. * </programlisting>
  222. *
  223. * because the read of "*q" depends on the read of "p" and these
  224. * two reads are separated by a read_barrier_depends(). However,
  225. * the following code, with the same initial values for "a" and "b":
  226. *
  227. * <programlisting>
  228. * CPU 0 CPU 1
  229. *
  230. * a = 2;
  231. * memory_barrier();
  232. * b = 3; y = b;
  233. * read_barrier_depends();
  234. * x = a;
  235. * </programlisting>
  236. *
  237. * does not enforce ordering, since there is no data dependency between
  238. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  239. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  240. * in cases like this where there are no data dependencies.
  241. **/
  242. #define read_barrier_depends() do { } while(0)
  243. #ifdef CONFIG_X86_OOSTORE
  244. /* Actually there are no OOO store capable CPUs for now that do SSE,
  245. but make it already an possibility. */
  246. #define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
  247. #else
  248. #define wmb() __asm__ __volatile__ ("": : :"memory")
  249. #endif
  250. #ifdef CONFIG_SMP
  251. #define smp_mb() mb()
  252. #define smp_rmb() rmb()
  253. #define smp_wmb() wmb()
  254. #define smp_read_barrier_depends() read_barrier_depends()
  255. #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
  256. #else
  257. #define smp_mb() barrier()
  258. #define smp_rmb() barrier()
  259. #define smp_wmb() barrier()
  260. #define smp_read_barrier_depends() do { } while(0)
  261. #define set_mb(var, value) do { var = value; barrier(); } while (0)
  262. #endif
  263. #include <linux/irqflags.h>
  264. /*
  265. * disable hlt during certain critical i/o operations
  266. */
  267. #define HAVE_DISABLE_HLT
  268. void disable_hlt(void);
  269. void enable_hlt(void);
  270. extern int es7000_plat;
  271. void cpu_idle_wait(void);
  272. /*
  273. * On SMP systems, when the scheduler does migration-cost autodetection,
  274. * it needs a way to flush as much of the CPU's caches as possible:
  275. */
  276. static inline void sched_cacheflush(void)
  277. {
  278. wbinvd();
  279. }
  280. extern unsigned long arch_align_stack(unsigned long sp);
  281. extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
  282. void default_idle(void);
  283. #endif