4 #include <linux/kernel.h>
5 #include <asm/segment.h>
6 #include <asm/cpufeature.h>
7 #include <linux/bitops.h> /* for LOCK_PREFIX */
8 #include <asm/synch_bitops.h>
9 #include <asm/hypervisor.h>
14 #define __vcpu_id smp_processor_id()
19 struct task_struct; /* one of the stranger aspects of C forward declarations.. */
20 extern struct task_struct * FASTCALL(__switch_to(struct task_struct *prev, struct task_struct *next));
22 #define switch_to(prev,next,last) do { \
23 unsigned long esi,edi; \
24 asm volatile("pushfl\n\t" /* Save flags */ \
26 "movl %%esp,%0\n\t" /* save ESP */ \
27 "movl %5,%%esp\n\t" /* restore ESP */ \
28 "movl $1f,%1\n\t" /* save EIP */ \
29 "pushl %6\n\t" /* restore EIP */ \
34 :"=m" (prev->thread.esp),"=m" (prev->thread.eip), \
35 "=a" (last),"=S" (esi),"=D" (edi) \
36 :"m" (next->thread.esp),"m" (next->thread.eip), \
37 "2" (prev), "d" (next)); \
40 #define _set_base(addr,base) do { unsigned long __pr; \
41 __asm__ __volatile__ ("movw %%dx,%1\n\t" \
42 "rorl $16,%%edx\n\t" \
52 #define _set_limit(addr,limit) do { unsigned long __lr; \
53 __asm__ __volatile__ ("movw %%dx,%1\n\t" \
54 "rorl $16,%%edx\n\t" \
56 "andb $0xf0,%%dh\n\t" \
65 #define set_base(ldt,base) _set_base( ((char *)&(ldt)) , (base) )
66 #define set_limit(ldt,limit) _set_limit( ((char *)&(ldt)) , ((limit)-1) )
69 * Load a segment. Fall back on loading the zero
70 * segment if something goes wrong..
72 #define loadsegment(seg,value) \
75 "mov %0,%%" #seg "\n" \
77 ".section .fixup,\"ax\"\n" \
80 "popl %%" #seg "\n\t" \
83 ".section __ex_table,\"a\"\n\t" \
90 * Save a segment register away
92 #define savesegment(seg, value) \
93 asm volatile("mov %%" #seg ",%0":"=rm" (value))
95 #define read_cr0() ({ \
96 unsigned int __dummy; \
97 __asm__ __volatile__( \
102 #define write_cr0(x) \
103 __asm__ __volatile__("movl %0,%%cr0": :"r" (x))
106 (HYPERVISOR_shared_info->vcpu_info[smp_processor_id()].arch.cr2)
107 #define write_cr2(x) \
108 __asm__ __volatile__("movl %0,%%cr2": :"r" (x))
110 #define read_cr3() ({ \
111 unsigned int __dummy; \
113 "movl %%cr3,%0\n\t" \
115 __dummy = xen_cr3_to_pfn(__dummy); \
116 mfn_to_pfn(__dummy) << PAGE_SHIFT; \
118 #define write_cr3(x) ({ \
119 unsigned int __dummy = pfn_to_mfn((x) >> PAGE_SHIFT); \
120 __dummy = xen_pfn_to_cr3(__dummy); \
121 __asm__ __volatile__("movl %0,%%cr3": :"r" (__dummy)); \
124 #define read_cr4() ({ \
125 unsigned int __dummy; \
127 "movl %%cr4,%0\n\t" \
131 #define read_cr4_safe() ({ \
132 unsigned int __dummy; \
133 /* This could fault if %cr4 does not exist */ \
134 __asm__("1: movl %%cr4, %0 \n" \
136 ".section __ex_table,\"a\" \n" \
139 : "=r" (__dummy): "0" (0)); \
143 #define write_cr4(x) \
144 __asm__ __volatile__("movl %0,%%cr4": :"r" (x))
146 * Clear and set 'TS' bit respectively
148 #define clts() (HYPERVISOR_fpu_taskswitch(0))
149 #define stts() (HYPERVISOR_fpu_taskswitch(1))
151 #endif /* __KERNEL__ */
154 __asm__ __volatile__ ("wbinvd": : :"memory")
156 static inline unsigned long get_limit(unsigned long segment)
158 unsigned long __limit;
160 :"=r" (__limit):"r" (segment));
164 #define nop() __asm__ __volatile__ ("nop")
166 #define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
168 #define tas(ptr) (xchg((ptr),1))
170 struct __xchg_dummy { unsigned long a[100]; };
171 #define __xg(x) ((struct __xchg_dummy *)(x))
174 #ifdef CONFIG_X86_CMPXCHG64
177 * The semantics of XCHGCMP8B are a bit strange, this is why
178 * there is a loop and the loading of %%eax and %%edx has to
179 * be inside. This inlines well in most cases, the cached
180 * cost is around ~38 cycles. (in the future we might want
181 * to do an SIMD/3DNOW!/MMX/FPU 64-bit store here, but that
182 * might have an implicit FPU-save as a cost, so it's not
183 * clear which path to go.)
185 * cmpxchg8b must be used with the lock prefix here to allow
186 * the instruction to be executed atomically, see page 3-102
187 * of the instruction set reference 24319102.pdf. We need
188 * the reader side to see the coherent 64bit value.
190 static inline void __set_64bit (unsigned long long * ptr,
191 unsigned int low, unsigned int high)
193 __asm__ __volatile__ (
195 "movl (%0), %%eax\n\t"
196 "movl 4(%0), %%edx\n\t"
197 "lock cmpxchg8b (%0)\n\t"
203 : "ax","dx","memory");
206 static inline void __set_64bit_constant (unsigned long long *ptr,
207 unsigned long long value)
209 __set_64bit(ptr,(unsigned int)(value), (unsigned int)((value)>>32ULL));
211 #define ll_low(x) *(((unsigned int*)&(x))+0)
212 #define ll_high(x) *(((unsigned int*)&(x))+1)
214 static inline void __set_64bit_var (unsigned long long *ptr,
215 unsigned long long value)
217 __set_64bit(ptr,ll_low(value), ll_high(value));
220 #define set_64bit(ptr,value) \
221 (__builtin_constant_p(value) ? \
222 __set_64bit_constant(ptr, value) : \
223 __set_64bit_var(ptr, value) )
225 #define _set_64bit(ptr,value) \
226 (__builtin_constant_p(value) ? \
227 __set_64bit(ptr, (unsigned int)(value), (unsigned int)((value)>>32ULL) ) : \
228 __set_64bit(ptr, ll_low(value), ll_high(value)) )
233 * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
234 * Note 2: xchg has side effect, so that attribute volatile is necessary,
235 * but generally the primitive is invalid, *ptr is output argument. --ANK
237 static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
241 __asm__ __volatile__("xchgb %b0,%1"
243 :"m" (*__xg(ptr)), "0" (x)
247 __asm__ __volatile__("xchgw %w0,%1"
249 :"m" (*__xg(ptr)), "0" (x)
253 __asm__ __volatile__("xchgl %0,%1"
255 :"m" (*__xg(ptr)), "0" (x)
263 * Atomic compare and exchange. Compare OLD with MEM, if identical,
264 * store NEW in MEM. Return the initial value in MEM. Success is
265 * indicated by comparing RETURN with OLD.
268 #ifdef CONFIG_X86_CMPXCHG
269 #define __HAVE_ARCH_CMPXCHG 1
270 #define cmpxchg(ptr,o,n)\
271 ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\
272 (unsigned long)(n),sizeof(*(ptr))))
275 static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
276 unsigned long new, int size)
281 __asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
283 : "q"(new), "m"(*__xg(ptr)), "0"(old)
287 __asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
289 : "r"(new), "m"(*__xg(ptr)), "0"(old)
293 __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %1,%2"
295 : "r"(new), "m"(*__xg(ptr)), "0"(old)
302 #ifndef CONFIG_X86_CMPXCHG
304 * Building a kernel capable running on 80386. It may be necessary to
305 * simulate the cmpxchg on the 80386 CPU. For that purpose we define
306 * a function for each of the sizes we support.
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);
313 static inline unsigned long cmpxchg_386(volatile void *ptr, unsigned long old,
314 unsigned long new, int size)
318 return cmpxchg_386_u8(ptr, old, new);
320 return cmpxchg_386_u16(ptr, old, new);
322 return cmpxchg_386_u32(ptr, old, new);
327 #define cmpxchg(ptr,o,n) \
329 __typeof__(*(ptr)) __ret; \
330 if (likely(boot_cpu_data.x86 > 3)) \
331 __ret = __cmpxchg((ptr), (unsigned long)(o), \
332 (unsigned long)(n), sizeof(*(ptr))); \
334 __ret = cmpxchg_386((ptr), (unsigned long)(o), \
335 (unsigned long)(n), sizeof(*(ptr))); \
340 #ifdef CONFIG_X86_CMPXCHG64
342 static inline unsigned long long __cmpxchg64(volatile void *ptr, unsigned long long old,
343 unsigned long long new)
345 unsigned long long prev;
346 __asm__ __volatile__(LOCK_PREFIX "cmpxchg8b %3"
348 : "b"((unsigned long)new),
349 "c"((unsigned long)(new >> 32)),
356 #define cmpxchg64(ptr,o,n)\
357 ((__typeof__(*(ptr)))__cmpxchg64((ptr),(unsigned long long)(o),\
358 (unsigned long long)(n)))
363 * Force strict CPU ordering.
364 * And yes, this is required on UP too when we're talking
367 * For now, "wmb()" doesn't actually do anything, as all
368 * Intel CPU's follow what Intel calls a *Processor Order*,
369 * in which all writes are seen in the program order even
372 * I expect future Intel CPU's to have a weaker ordering,
373 * but I'd also expect them to finally get their act together
374 * and add some real memory barriers if so.
376 * Some non intel clones support out of order store. wmb() ceases to be a
382 * Actually only lfence would be needed for mb() because all stores done
383 * by the kernel should be already ordered. But keep a full barrier for now.
386 #define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
387 #define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
390 * read_barrier_depends - Flush all pending reads that subsequents reads
393 * No data-dependent reads from memory-like regions are ever reordered
394 * over this barrier. All reads preceding this primitive are guaranteed
395 * to access memory (but not necessarily other CPUs' caches) before any
396 * reads following this primitive that depend on the data return by
397 * any of the preceding reads. This primitive is much lighter weight than
398 * rmb() on most CPUs, and is never heavier weight than is
401 * These ordering constraints are respected by both the local CPU
404 * Ordering is not guaranteed by anything other than these primitives,
405 * not even by data dependencies. See the documentation for
406 * memory_barrier() for examples and URLs to more information.
408 * For example, the following code would force ordering (the initial
409 * value of "a" is zero, "b" is one, and "p" is "&a"):
417 * read_barrier_depends();
421 * because the read of "*q" depends on the read of "p" and these
422 * two reads are separated by a read_barrier_depends(). However,
423 * the following code, with the same initial values for "a" and "b":
431 * read_barrier_depends();
435 * does not enforce ordering, since there is no data dependency between
436 * the read of "a" and the read of "b". Therefore, on some CPUs, such
437 * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
438 * in cases like this where there are no data dependencies.
441 #define read_barrier_depends() do { } while(0)
443 #ifdef CONFIG_X86_OOSTORE
444 /* Actually there are no OOO store capable CPUs for now that do SSE,
445 but make it already an possibility. */
446 #define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
448 #define wmb() __asm__ __volatile__ ("": : :"memory")
452 #define smp_mb() mb()
453 #define smp_rmb() rmb()
454 #define smp_wmb() wmb()
455 #define smp_read_barrier_depends() read_barrier_depends()
456 #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
458 #define smp_mb() barrier()
459 #define smp_rmb() barrier()
460 #define smp_wmb() barrier()
461 #define smp_read_barrier_depends() do { } while(0)
462 #define set_mb(var, value) do { var = value; barrier(); } while (0)
465 #include <linux/irqflags.h>
468 * disable hlt during certain critical i/o operations
470 #define HAVE_DISABLE_HLT
471 void disable_hlt(void);
472 void enable_hlt(void);
474 extern int es7000_plat;
475 void cpu_idle_wait(void);
478 * On SMP systems, when the scheduler does migration-cost autodetection,
479 * it needs a way to flush as much of the CPU's caches as possible:
481 static inline void sched_cacheflush(void)
486 extern unsigned long arch_align_stack(unsigned long sp);
487 extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
489 void default_idle(void);