4 #include <linux/config.h>
5 #include <linux/kernel.h>
6 #include <linux/init.h>
7 #include <asm/segment.h>
8 #include <linux/bitops.h> /* for LOCK_PREFIX */
12 struct task_struct; /* one of the stranger aspects of C forward declarations.. */
13 extern void FASTCALL(__switch_to(struct task_struct *prev, struct task_struct *next));
15 #define prepare_to_switch() do { } while(0)
16 #define switch_to(prev,next,last) do { \
17 asm volatile("pushl %%esi\n\t" \
20 "movl %%esp,%0\n\t" /* save ESP */ \
21 "movl %3,%%esp\n\t" /* restore ESP */ \
22 "movl $1f,%1\n\t" /* save EIP */ \
23 "pushl %4\n\t" /* restore EIP */ \
29 :"=m" (prev->thread.esp),"=m" (prev->thread.eip), \
31 :"m" (next->thread.esp),"m" (next->thread.eip), \
32 "a" (prev), "d" (next), \
36 #define _set_base(addr,base) do { unsigned long __pr; \
37 __asm__ __volatile__ ("movw %%dx,%1\n\t" \
38 "rorl $16,%%edx\n\t" \
48 #define _set_limit(addr,limit) do { unsigned long __lr; \
49 __asm__ __volatile__ ("movw %%dx,%1\n\t" \
50 "rorl $16,%%edx\n\t" \
52 "andb $0xf0,%%dh\n\t" \
61 #define set_base(ldt,base) _set_base( ((char *)&(ldt)) , (base) )
62 #define set_limit(ldt,limit) _set_limit( ((char *)&(ldt)) , ((limit)-1)>>12 )
64 static inline unsigned long _get_base(char * addr)
67 __asm__("movb %3,%%dh\n\t"
78 #define get_base(ldt) _get_base( ((char *)&(ldt)) )
81 * Load a segment. Fall back on loading the zero
82 * segment if something goes wrong..
84 #define loadsegment(seg,value) \
87 "mov %0,%%" #seg "\n" \
89 ".section .fixup,\"ax\"\n" \
92 "popl %%" #seg "\n\t" \
95 ".section __ex_table,\"a\"\n\t" \
102 * Clear and set 'TS' bit respectively
104 #define clts() __asm__ __volatile__ ("clts")
105 #define read_cr0() ({ \
106 unsigned int __dummy; \
108 "movl %%cr0,%0\n\t" \
112 #define write_cr0(x) \
113 __asm__("movl %0,%%cr0": :"r" (x));
115 #define read_cr4() ({ \
116 unsigned int __dummy; \
118 "movl %%cr4,%0\n\t" \
122 #define write_cr4(x) \
123 __asm__("movl %0,%%cr4": :"r" (x));
124 #define stts() write_cr0(8 | read_cr0())
126 #endif /* __KERNEL__ */
129 __asm__ __volatile__ ("wbinvd": : :"memory");
131 static inline unsigned long get_limit(unsigned long segment)
133 unsigned long __limit;
135 :"=r" (__limit):"r" (segment));
139 #define nop() __asm__ __volatile__ ("nop")
141 #define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
143 #define tas(ptr) (xchg((ptr),1))
145 struct __xchg_dummy { unsigned long a[100]; };
146 #define __xg(x) ((struct __xchg_dummy *)(x))
150 * The semantics of XCHGCMP8B are a bit strange, this is why
151 * there is a loop and the loading of %%eax and %%edx has to
152 * be inside. This inlines well in most cases, the cached
153 * cost is around ~38 cycles. (in the future we might want
154 * to do an SIMD/3DNOW!/MMX/FPU 64-bit store here, but that
155 * might have an implicit FPU-save as a cost, so it's not
156 * clear which path to go.)
158 * chmxchg8b must be used with the lock prefix here to allow
159 * the instruction to be executed atomically, see page 3-102
160 * of the instruction set reference 24319102.pdf. We need
161 * the reader side to see the coherent 64bit value.
163 static inline void __set_64bit (unsigned long long * ptr,
164 unsigned int low, unsigned int high)
166 __asm__ __volatile__ (
168 "movl (%0), %%eax\n\t"
169 "movl 4(%0), %%edx\n\t"
170 "lock cmpxchg8b (%0)\n\t"
176 : "ax","dx","memory");
179 static inline void __set_64bit_constant (unsigned long long *ptr,
180 unsigned long long value)
182 __set_64bit(ptr,(unsigned int)(value), (unsigned int)((value)>>32ULL));
184 #define ll_low(x) *(((unsigned int*)&(x))+0)
185 #define ll_high(x) *(((unsigned int*)&(x))+1)
187 static inline void __set_64bit_var (unsigned long long *ptr,
188 unsigned long long value)
190 __set_64bit(ptr,ll_low(value), ll_high(value));
193 #define set_64bit(ptr,value) \
194 (__builtin_constant_p(value) ? \
195 __set_64bit_constant(ptr, value) : \
196 __set_64bit_var(ptr, value) )
198 #define _set_64bit(ptr,value) \
199 (__builtin_constant_p(value) ? \
200 __set_64bit(ptr, (unsigned int)(value), (unsigned int)((value)>>32ULL) ) : \
201 __set_64bit(ptr, ll_low(value), ll_high(value)) )
204 * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
205 * Note 2: xchg has side effect, so that attribute volatile is necessary,
206 * but generally the primitive is invalid, *ptr is output argument. --ANK
208 static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
212 __asm__ __volatile__("xchgb %b0,%1"
214 :"m" (*__xg(ptr)), "0" (x)
218 __asm__ __volatile__("xchgw %w0,%1"
220 :"m" (*__xg(ptr)), "0" (x)
224 __asm__ __volatile__("xchgl %0,%1"
226 :"m" (*__xg(ptr)), "0" (x)
234 * Atomic compare and exchange. Compare OLD with MEM, if identical,
235 * store NEW in MEM. Return the initial value in MEM. Success is
236 * indicated by comparing RETURN with OLD.
239 #ifdef CONFIG_X86_CMPXCHG
240 #define __HAVE_ARCH_CMPXCHG 1
243 static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
244 unsigned long new, int size)
249 __asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
251 : "q"(new), "m"(*__xg(ptr)), "0"(old)
255 __asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
257 : "q"(new), "m"(*__xg(ptr)), "0"(old)
261 __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %1,%2"
263 : "q"(new), "m"(*__xg(ptr)), "0"(old)
270 #define cmpxchg(ptr,o,n)\
271 ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\
272 (unsigned long)(n),sizeof(*(ptr))))
275 * Force strict CPU ordering.
276 * And yes, this is required on UP too when we're talking
279 * For now, "wmb()" doesn't actually do anything, as all
280 * Intel CPU's follow what Intel calls a *Processor Order*,
281 * in which all writes are seen in the program order even
284 * I expect future Intel CPU's to have a weaker ordering,
285 * but I'd also expect them to finally get their act together
286 * and add some real memory barriers if so.
288 * Some non intel clones support out of order store. wmb() ceases to be a
292 #define mb() __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory")
295 #ifdef CONFIG_X86_OOSTORE
296 #define wmb() __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory")
298 #define wmb() __asm__ __volatile__ ("": : :"memory")
302 #define smp_mb() mb()
303 #define smp_rmb() rmb()
304 #define smp_wmb() wmb()
305 #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
307 #define smp_mb() barrier()
308 #define smp_rmb() barrier()
309 #define smp_wmb() barrier()
310 #define set_mb(var, value) do { var = value; barrier(); } while (0)
313 #define set_wmb(var, value) do { var = value; wmb(); } while (0)
315 /* interrupt control.. */
316 #define __save_flags(x) __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */)
317 #define __restore_flags(x) __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory", "cc")
318 #define __cli() __asm__ __volatile__("cli": : :"memory")
319 #define __sti() __asm__ __volatile__("sti": : :"memory")
320 /* used in the idle loop; sti takes one instruction cycle to complete */
321 #define safe_halt() __asm__ __volatile__("sti; hlt": : :"memory")
323 #define __save_and_cli(x) do { __save_flags(x); __cli(); } while(0);
324 #define __save_and_sti(x) do { __save_flags(x); __sti(); } while(0);
326 /* For spinlocks etc */
328 #define local_irq_save(x) __asm__ __volatile__("pushfl ; popl %0 ; cli":"=g" (x): /* no input */ :"memory")
329 #define local_irq_set(x) __asm__ __volatile__("pushfl ; popl %0 ; sti":"=g" (x): /* no input */ :"memory")
331 #define local_irq_save(x) __save_and_cli(x)
332 #define local_irq_set(x) __save_and_sti(x)
335 #define local_irq_restore(x) __restore_flags(x)
336 #define local_irq_disable() __cli()
337 #define local_irq_enable() __sti()
341 extern void __global_cli(void);
342 extern void __global_sti(void);
343 extern unsigned long __global_save_flags(void);
344 extern void __global_restore_flags(unsigned long);
345 #define cli() __global_cli()
346 #define sti() __global_sti()
347 #define save_flags(x) ((x)=__global_save_flags())
348 #define restore_flags(x) __global_restore_flags(x)
349 #define save_and_cli(x) do { save_flags(x); cli(); } while(0);
350 #define save_and_sti(x) do { save_flags(x); sti(); } while(0);
354 #define cli() __cli()
355 #define sti() __sti()
356 #define save_flags(x) __save_flags(x)
357 #define restore_flags(x) __restore_flags(x)
358 #define save_and_cli(x) __save_and_cli(x)
359 #define save_and_sti(x) __save_and_sti(x)
364 * disable hlt during certain critical i/o operations
366 #define HAVE_DISABLE_HLT
367 void disable_hlt(void);
368 void enable_hlt(void);
370 extern unsigned long dmi_broken;
371 extern int is_sony_vaio_laptop;
373 #define BROKEN_ACPI_Sx 0x0001
374 #define BROKEN_INIT_AFTER_S1 0x0002
375 #define BROKEN_PNP_BIOS 0x0004