4 #include <linux/config.h>
5 #include <linux/kernel.h>
6 #include <asm/segment.h>
7 #include <linux/bitops.h> /* for LOCK_PREFIX */
11 struct task_struct
; /* one of the stranger aspects of C forward declarations.. */
12 extern void FASTCALL(__switch_to(struct task_struct
*prev
, struct task_struct
*next
));
14 #define prepare_to_switch() do { } while(0)
15 #define switch_to(prev,next,last) do { \
16 asm volatile("pushl %%esi\n\t" \
19 "movl %%esp,%0\n\t" /* save ESP */ \
20 "movl %3,%%esp\n\t" /* restore ESP */ \
21 "movl $1f,%1\n\t" /* save EIP */ \
22 "pushl %4\n\t" /* restore EIP */ \
28 :"=m" (prev->thread.esp),"=m" (prev->thread.eip), \
30 :"m" (next->thread.esp),"m" (next->thread.eip), \
31 "a" (prev), "d" (next), \
35 #define _set_base(addr,base) do { unsigned long __pr; \
36 __asm__ __volatile__ ("movw %%dx,%1\n\t" \
37 "rorl $16,%%edx\n\t" \
47 #define _set_limit(addr,limit) do { unsigned long __lr; \
48 __asm__ __volatile__ ("movw %%dx,%1\n\t" \
49 "rorl $16,%%edx\n\t" \
51 "andb $0xf0,%%dh\n\t" \
60 #define set_base(ldt,base) _set_base( ((char *)&(ldt)) , (base) )
61 #define set_limit(ldt,limit) _set_limit( ((char *)&(ldt)) , ((limit)-1)>>12 )
63 static inline unsigned long _get_base(char * addr
)
66 __asm__("movb %3,%%dh\n\t"
77 #define get_base(ldt) _get_base( ((char *)&(ldt)) )
80 * Load a segment. Fall back on loading the zero
81 * segment if something goes wrong..
83 #define loadsegment(seg,value) \
86 "movl %0,%%" #seg "\n" \
88 ".section .fixup,\"ax\"\n" \
91 "popl %%" #seg "\n\t" \
94 ".section __ex_table,\"a\"\n\t" \
98 : :"m" (*(unsigned int *)&(value)))
101 * Clear and set 'TS' bit respectively
103 #define clts() __asm__ __volatile__ ("clts")
104 #define read_cr0() ({ \
105 unsigned int __dummy; \
107 "movl %%cr0,%0\n\t" \
111 #define write_cr0(x) \
112 __asm__("movl %0,%%cr0": :"r" (x));
113 #define stts() write_cr0(8 | read_cr0())
115 #endif /* __KERNEL__ */
117 static inline unsigned long get_limit(unsigned long segment
)
119 unsigned long __limit
;
121 :"=r" (__limit
):"r" (segment
));
125 #define nop() __asm__ __volatile__ ("nop")
127 #define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
129 #define tas(ptr) (xchg((ptr),1))
131 struct __xchg_dummy
{ unsigned long a
[100]; };
132 #define __xg(x) ((struct __xchg_dummy *)(x))
136 * The semantics of XCHGCMP8B are a bit strange, this is why
137 * there is a loop and the loading of %%eax and %%edx has to
138 * be inside. This inlines well in most cases, the cached
139 * cost is around ~38 cycles. (in the future we might want
140 * to do an SIMD/3DNOW!/MMX/FPU 64-bit store here, but that
141 * might have an implicit FPU-save as a cost, so it's not
142 * clear which path to go.)
144 extern inline void __set_64bit (unsigned long long * ptr
,
145 unsigned int low
, unsigned int high
)
147 __asm__
__volatile__ (
148 "1: movl (%0), %%eax;
159 extern void inline __set_64bit_constant (unsigned long long *ptr
,
160 unsigned long long value
)
162 __set_64bit(ptr
,(unsigned int)(value
), (unsigned int)((value
)>>32ULL));
164 #define ll_low(x) *(((unsigned int*)&(x))+0)
165 #define ll_high(x) *(((unsigned int*)&(x))+1)
167 extern void inline __set_64bit_var (unsigned long long *ptr
,
168 unsigned long long value
)
170 __set_64bit(ptr
,ll_low(value
), ll_high(value
));
173 #define set_64bit(ptr,value) \
174 (__builtin_constant_p(value) ? \
175 __set_64bit_constant(ptr, value) : \
176 __set_64bit_var(ptr, value) )
178 #define _set_64bit(ptr,value) \
179 (__builtin_constant_p(value) ? \
180 __set_64bit(ptr, (unsigned int)(value), (unsigned int)((value)>>32ULL) ) : \
181 __set_64bit(ptr, ll_low(value), ll_high(value)) )
184 * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
185 * Note 2: xchg has side effect, so that attribute volatile is necessary,
186 * but generally the primitive is invalid, *ptr is output argument. --ANK
188 static inline unsigned long __xchg(unsigned long x
, volatile void * ptr
, int size
)
192 __asm__
__volatile__("xchgb %b0,%1"
194 :"m" (*__xg(ptr
)), "0" (x
)
198 __asm__
__volatile__("xchgw %w0,%1"
200 :"m" (*__xg(ptr
)), "0" (x
)
204 __asm__
__volatile__("xchgl %0,%1"
206 :"m" (*__xg(ptr
)), "0" (x
)
214 * Atomic compare and exchange. Compare OLD with MEM, if identical,
215 * store NEW in MEM. Return the initial value in MEM. Success is
216 * indicated by comparing RETURN with OLD.
219 #ifdef CONFIG_X86_CMPXCHG
220 #define __HAVE_ARCH_CMPXCHG 1
222 static inline unsigned long __cmpxchg(volatile void *ptr
, unsigned long old
,
223 unsigned long new, int size
)
228 __asm__
__volatile__(LOCK_PREFIX
"cmpxchgb %b1,%2"
230 : "q"(new), "m"(*__xg(ptr
)), "0"(old
)
234 __asm__
__volatile__(LOCK_PREFIX
"cmpxchgw %w1,%2"
236 : "q"(new), "m"(*__xg(ptr
)), "0"(old
)
240 __asm__
__volatile__(LOCK_PREFIX
"cmpxchgl %1,%2"
242 : "q"(new), "m"(*__xg(ptr
)), "0"(old
)
249 #define cmpxchg(ptr,o,n)\
250 ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\
251 (unsigned long)(n),sizeof(*(ptr))))
254 /* Compiling for a 386 proper. Is it worth implementing via cli/sti? */
258 * Force strict CPU ordering.
259 * And yes, this is required on UP too when we're talking
262 * For now, "wmb()" doesn't actually do anything, as all
263 * Intel CPU's follow what Intel calls a *Processor Order*,
264 * in which all writes are seen in the program order even
267 * I expect future Intel CPU's to have a weaker ordering,
268 * but I'd also expect them to finally get their act together
269 * and add some real memory barriers if so.
271 * The Pentium III does add a real memory barrier with the
272 * sfence instruction, so we use that where appropriate.
274 #ifndef CONFIG_X86_XMM
275 #define mb() __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory")
277 #define mb() __asm__ __volatile__ ("sfence": : :"memory")
280 #define wmb() __asm__ __volatile__ ("": : :"memory")
281 #define set_mb(var, value) do { xchg(&var, value); } while (0)
282 #define set_wmb(var, value) do { var = value; wmb(); } while (0)
284 /* interrupt control.. */
285 #define __save_flags(x) __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */ :"memory")
286 #define __restore_flags(x) __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
287 #define __cli() __asm__ __volatile__("cli": : :"memory")
288 #define __sti() __asm__ __volatile__("sti": : :"memory")
289 /* used in the idle loop; sti takes one instruction cycle to complete */
290 #define safe_halt() __asm__ __volatile__("sti; hlt": : :"memory")
292 /* For spinlocks etc */
293 #define local_irq_save(x) __asm__ __volatile__("pushfl ; popl %0 ; cli":"=g" (x): /* no input */ :"memory")
294 #define local_irq_restore(x) __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
295 #define local_irq_disable() __asm__ __volatile__("cli": : :"memory")
296 #define local_irq_enable() __asm__ __volatile__("sti": : :"memory")
300 extern void __global_cli(void);
301 extern void __global_sti(void);
302 extern unsigned long __global_save_flags(void);
303 extern void __global_restore_flags(unsigned long);
304 #define cli() __global_cli()
305 #define sti() __global_sti()
306 #define save_flags(x) ((x)=__global_save_flags())
307 #define restore_flags(x) __global_restore_flags(x)
311 #define cli() __cli()
312 #define sti() __sti()
313 #define save_flags(x) __save_flags(x)
314 #define restore_flags(x) __restore_flags(x)
319 * disable hlt during certain critical i/o operations
321 #define HAVE_DISABLE_HLT
322 void disable_hlt(void);
323 void enable_hlt(void);