4 #include <linux/kernel.h>
5 #include <asm/segment.h>
6 #include <asm/synch_bitops.h>
7 #include <asm/hypervisor.h>
8 #include <xen/interface/arch-x86_64.h>
13 #define __vcpu_id smp_processor_id()
19 #define STR(x) __STR(x)
21 #define __SAVE(reg,offset) "movq %%" #reg ",(14-" #offset ")*8(%%rsp)\n\t"
22 #define __RESTORE(reg,offset) "movq (14-" #offset ")*8(%%rsp),%%" #reg "\n\t"
24 /* frame pointer must be last for get_wchan */
25 #define SAVE_CONTEXT "pushq %%rbp ; movq %%rsi,%%rbp\n\t"
26 #define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp\n\t"
28 #define __EXTRA_CLOBBER \
29 ,"rcx","rbx","rdx","r8","r9","r10","r11","r12","r13","r14","r15"
31 #define switch_to(prev,next,last) \
32 asm volatile(SAVE_CONTEXT \
33 "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \
34 "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \
35 "call __switch_to\n\t" \
36 ".globl thread_return\n" \
37 "thread_return:\n\t" \
38 "movq %%gs:%P[pda_pcurrent],%%rsi\n\t" \
39 "movq %P[thread_info](%%rsi),%%r8\n\t" \
40 LOCK_PREFIX "btr %[tif_fork],%P[ti_flags](%%r8)\n\t" \
41 "movq %%rax,%%rdi\n\t" \
42 "jc ret_from_fork\n\t" \
45 : [next] "S" (next), [prev] "D" (prev), \
46 [threadrsp] "i" (offsetof(struct task_struct, thread.rsp)), \
47 [ti_flags] "i" (offsetof(struct thread_info, flags)),\
48 [tif_fork] "i" (TIF_FORK), \
49 [thread_info] "i" (offsetof(struct task_struct, thread_info)), \
50 [pda_pcurrent] "i" (offsetof(struct x8664_pda, pcurrent)) \
51 : "memory", "cc" __EXTRA_CLOBBER)
54 extern void load_gs_index(unsigned);
57 * Load a segment. Fall back on loading the zero
58 * segment if something goes wrong..
60 #define loadsegment(seg,value) \
63 "movl %k0,%%" #seg "\n" \
65 ".section .fixup,\"ax\"\n" \
67 "movl %1,%%" #seg "\n\t" \
70 ".section __ex_table,\"a\"\n\t" \
74 : :"r" (value), "r" (0))
77 * Clear and set 'TS' bit respectively
79 #define clts() (HYPERVISOR_fpu_taskswitch(0))
81 static inline unsigned long read_cr0(void)
84 asm volatile("movq %%cr0,%0" : "=r" (cr0));
88 static inline void write_cr0(unsigned long val)
90 asm volatile("movq %0,%%cr0" :: "r" (val));
93 #define read_cr3() ({ \
94 unsigned long __dummy; \
95 asm("movq %%cr3,%0" : "=r" (__dummy)); \
96 machine_to_phys(__dummy); \
99 static inline unsigned long read_cr4(void)
102 asm("movq %%cr4,%0" : "=r" (cr4));
106 static inline void write_cr4(unsigned long val)
108 asm volatile("movq %0,%%cr4" :: "r" (val));
111 #define stts() (HYPERVISOR_fpu_taskswitch(1))
114 __asm__ __volatile__ ("wbinvd": : :"memory");
117 * On SMP systems, when the scheduler does migration-cost autodetection,
118 * it needs a way to flush as much of the CPU's caches as possible.
120 static inline void sched_cacheflush(void)
125 #endif /* __KERNEL__ */
127 #define nop() __asm__ __volatile__ ("nop")
129 #define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
131 #define tas(ptr) (xchg((ptr),1))
133 #define __xg(x) ((volatile long *)(x))
135 static inline void set_64bit(volatile unsigned long *ptr, unsigned long val)
140 #define _set_64bit set_64bit
143 * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
144 * Note 2: xchg has side effect, so that attribute volatile is necessary,
145 * but generally the primitive is invalid, *ptr is output argument. --ANK
147 static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
151 __asm__ __volatile__("xchgb %b0,%1"
153 :"m" (*__xg(ptr)), "0" (x)
157 __asm__ __volatile__("xchgw %w0,%1"
159 :"m" (*__xg(ptr)), "0" (x)
163 __asm__ __volatile__("xchgl %k0,%1"
165 :"m" (*__xg(ptr)), "0" (x)
169 __asm__ __volatile__("xchgq %0,%1"
171 :"m" (*__xg(ptr)), "0" (x)
179 * Atomic compare and exchange. Compare OLD with MEM, if identical,
180 * store NEW in MEM. Return the initial value in MEM. Success is
181 * indicated by comparing RETURN with OLD.
184 #define __HAVE_ARCH_CMPXCHG 1
186 static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
187 unsigned long new, int size)
192 __asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
194 : "q"(new), "m"(*__xg(ptr)), "0"(old)
198 __asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
200 : "r"(new), "m"(*__xg(ptr)), "0"(old)
204 __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %k1,%2"
206 : "r"(new), "m"(*__xg(ptr)), "0"(old)
210 __asm__ __volatile__(LOCK_PREFIX "cmpxchgq %1,%2"
212 : "r"(new), "m"(*__xg(ptr)), "0"(old)
219 #define cmpxchg(ptr,o,n)\
220 ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\
221 (unsigned long)(n),sizeof(*(ptr))))
224 #define smp_mb() mb()
225 #define smp_rmb() rmb()
226 #define smp_wmb() wmb()
227 #define smp_read_barrier_depends() do {} while(0)
229 #define smp_mb() barrier()
230 #define smp_rmb() barrier()
231 #define smp_wmb() barrier()
232 #define smp_read_barrier_depends() do {} while(0)
237 * Force strict CPU ordering.
238 * And yes, this is required on UP too when we're talking
241 #define mb() asm volatile("mfence":::"memory")
242 #define rmb() asm volatile("lfence":::"memory")
244 #ifdef CONFIG_UNORDERED_IO
245 #define wmb() asm volatile("sfence" ::: "memory")
247 #define wmb() asm volatile("" ::: "memory")
249 #define read_barrier_depends() do {} while(0)
250 #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
252 #define warn_if_not_ulong(x) do { unsigned long foo; (void) (&(x) == &foo); } while (0)
254 void safe_halt(void);
257 #include <linux/irqflags.h>
259 void cpu_idle_wait(void);
261 extern unsigned long arch_align_stack(unsigned long sp);
262 extern void free_init_pages(char *what, unsigned long begin, unsigned long end);