1 #ifndef _ASM_X86_SYSTEM_H_
2 #define _ASM_X86_SYSTEM_H_
5 #include <asm/segment.h>
6 #include <asm/cpufeature.h>
7 #include <asm/cmpxchg.h>
10 #include <linux/kernel.h>
11 #include <linux/irqflags.h>
13 /* entries in ARCH_DLINFO: */
14 #ifdef CONFIG_IA32_EMULATION
15 # define AT_VECTOR_SIZE_ARCH 2
17 # define AT_VECTOR_SIZE_ARCH 1
22 struct task_struct
; /* one of the stranger aspects of C forward declarations */
23 extern struct task_struct
*FASTCALL(__switch_to(struct task_struct
*prev
,
24 struct task_struct
*next
));
27 * Saving eflags is important. It switches not only IOPL between tasks,
28 * it also protects other tasks from NT leaking through sysenter etc.
30 #define switch_to(prev, next, last) do { \
31 unsigned long esi, edi; \
32 asm volatile("pushfl\n\t" /* Save flags */ \
34 "movl %%esp,%0\n\t" /* save ESP */ \
35 "movl %5,%%esp\n\t" /* restore ESP */ \
36 "movl $1f,%1\n\t" /* save EIP */ \
37 "pushl %6\n\t" /* restore EIP */ \
42 :"=m" (prev->thread.sp), "=m" (prev->thread.ip), \
43 "=a" (last), "=S" (esi), "=D" (edi) \
44 :"m" (next->thread.sp), "m" (next->thread.ip), \
45 "2" (prev), "d" (next)); \
49 * disable hlt during certain critical i/o operations
51 #define HAVE_DISABLE_HLT
53 #define __SAVE(reg, offset) "movq %%" #reg ",(14-" #offset ")*8(%%rsp)\n\t"
54 #define __RESTORE(reg, offset) "movq (14-" #offset ")*8(%%rsp),%%" #reg "\n\t"
56 /* frame pointer must be last for get_wchan */
57 #define SAVE_CONTEXT "pushf ; pushq %%rbp ; movq %%rsi,%%rbp\n\t"
58 #define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp ; popf\t"
60 #define __EXTRA_CLOBBER \
61 , "rcx", "rbx", "rdx", "r8", "r9", "r10", "r11", \
62 "r12", "r13", "r14", "r15"
64 /* Save restore flags to clear handle leaking NT */
65 #define switch_to(prev, next, last) \
66 asm volatile(SAVE_CONTEXT \
67 "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \
68 "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \
69 "call __switch_to\n\t" \
70 ".globl thread_return\n" \
71 "thread_return:\n\t" \
72 "movq %%gs:%P[pda_pcurrent],%%rsi\n\t" \
73 "movq %P[thread_info](%%rsi),%%r8\n\t" \
74 LOCK_PREFIX "btr %[tif_fork],%P[ti_flags](%%r8)\n\t" \
75 "movq %%rax,%%rdi\n\t" \
76 "jc ret_from_fork\n\t" \
79 : [next] "S" (next), [prev] "D" (prev), \
80 [threadrsp] "i" (offsetof(struct task_struct, thread.sp)), \
81 [ti_flags] "i" (offsetof(struct thread_info, flags)), \
82 [tif_fork] "i" (TIF_FORK), \
83 [thread_info] "i" (offsetof(struct task_struct, stack)), \
84 [pda_pcurrent] "i" (offsetof(struct x8664_pda, pcurrent)) \
85 : "memory", "cc" __EXTRA_CLOBBER)
89 #define _set_base(addr, base) do { unsigned long __pr; \
90 __asm__ __volatile__ ("movw %%dx,%1\n\t" \
91 "rorl $16,%%edx\n\t" \
101 #define _set_limit(addr, limit) do { unsigned long __lr; \
102 __asm__ __volatile__ ("movw %%dx,%1\n\t" \
103 "rorl $16,%%edx\n\t" \
105 "andb $0xf0,%%dh\n\t" \
106 "orb %%dh,%%dl\n\t" \
114 #define set_base(ldt, base) _set_base(((char *)&(ldt)) , (base))
115 #define set_limit(ldt, limit) _set_limit(((char *)&(ldt)) , ((limit)-1))
117 extern void load_gs_index(unsigned);
120 * Load a segment. Fall back on loading the zero
121 * segment if something goes wrong..
123 #define loadsegment(seg, value) \
126 "movl %k0,%%" #seg "\n" \
128 ".section .fixup,\"ax\"\n" \
130 "movl %k1, %%" #seg "\n\t" \
133 ".section __ex_table,\"a\"\n\t" \
135 _ASM_PTR " 1b,3b\n" \
137 : :"r" (value), "r" (0))
141 * Save a segment register away
143 #define savesegment(seg, value) \
144 asm volatile("mov %%" #seg ",%0":"=rm" (value))
146 static inline unsigned long get_limit(unsigned long segment
)
148 unsigned long __limit
;
150 :"=r" (__limit
):"r" (segment
));
154 static inline void native_clts(void)
156 asm volatile ("clts");
160 * Volatile isn't enough to prevent the compiler from reordering the
161 * read/write functions for the control registers and messing everything up.
162 * A memory clobber would solve the problem, but would prevent reordering of
163 * all loads stores around it, which can hurt performance. Solution is to
164 * use a variable and mimic reads and writes to it to enforce serialization
166 static unsigned long __force_order
;
168 static inline unsigned long native_read_cr0(void)
171 asm volatile("mov %%cr0,%0\n\t" :"=r" (val
), "=m" (__force_order
));
175 static inline void native_write_cr0(unsigned long val
)
177 asm volatile("mov %0,%%cr0": :"r" (val
), "m" (__force_order
));
180 static inline unsigned long native_read_cr2(void)
183 asm volatile("mov %%cr2,%0\n\t" :"=r" (val
), "=m" (__force_order
));
187 static inline void native_write_cr2(unsigned long val
)
189 asm volatile("mov %0,%%cr2": :"r" (val
), "m" (__force_order
));
192 static inline unsigned long native_read_cr3(void)
195 asm volatile("mov %%cr3,%0\n\t" :"=r" (val
), "=m" (__force_order
));
199 static inline void native_write_cr3(unsigned long val
)
201 asm volatile("mov %0,%%cr3": :"r" (val
), "m" (__force_order
));
204 static inline unsigned long native_read_cr4(void)
207 asm volatile("mov %%cr4,%0\n\t" :"=r" (val
), "=m" (__force_order
));
211 static inline unsigned long native_read_cr4_safe(void)
214 /* This could fault if %cr4 does not exist. In x86_64, a cr4 always
215 * exists, so it will never fail. */
217 asm volatile("1: mov %%cr4, %0 \n"
219 ".section __ex_table,\"a\" \n"
222 : "=r" (val
), "=m" (__force_order
) : "0" (0));
224 val
= native_read_cr4();
229 static inline void native_write_cr4(unsigned long val
)
231 asm volatile("mov %0,%%cr4": :"r" (val
), "m" (__force_order
));
235 static inline unsigned long native_read_cr8(void)
238 asm volatile("movq %%cr8,%0" : "=r" (cr8
));
242 static inline void native_write_cr8(unsigned long val
)
244 asm volatile("movq %0,%%cr8" :: "r" (val
) : "memory");
248 static inline void native_wbinvd(void)
250 asm volatile("wbinvd": : :"memory");
252 #ifdef CONFIG_PARAVIRT
253 #include <asm/paravirt.h>
255 #define read_cr0() (native_read_cr0())
256 #define write_cr0(x) (native_write_cr0(x))
257 #define read_cr2() (native_read_cr2())
258 #define write_cr2(x) (native_write_cr2(x))
259 #define read_cr3() (native_read_cr3())
260 #define write_cr3(x) (native_write_cr3(x))
261 #define read_cr4() (native_read_cr4())
262 #define read_cr4_safe() (native_read_cr4_safe())
263 #define write_cr4(x) (native_write_cr4(x))
264 #define wbinvd() (native_wbinvd())
266 #define read_cr8() (native_read_cr8())
267 #define write_cr8(x) (native_write_cr8(x))
270 /* Clear the 'TS' bit */
271 #define clts() (native_clts())
273 #endif/* CONFIG_PARAVIRT */
275 #define stts() write_cr0(8 | read_cr0())
277 #endif /* __KERNEL__ */
279 static inline void clflush(void *__p
)
281 asm volatile("clflush %0" : "+m" (*(char __force
*)__p
));
284 #define nop() __asm__ __volatile__ ("nop")
286 void disable_hlt(void);
287 void enable_hlt(void);
289 extern int es7000_plat
;
290 void cpu_idle_wait(void);
292 extern unsigned long arch_align_stack(unsigned long sp
);
293 extern void free_init_pages(char *what
, unsigned long begin
, unsigned long end
);
295 void default_idle(void);
298 * Force strict CPU ordering.
299 * And yes, this is required on UP too when we're talking
304 * For now, "wmb()" doesn't actually do anything, as all
305 * Intel CPU's follow what Intel calls a *Processor Order*,
306 * in which all writes are seen in the program order even
309 * I expect future Intel CPU's to have a weaker ordering,
310 * but I'd also expect them to finally get their act together
311 * and add some real memory barriers if so.
313 * Some non intel clones support out of order store. wmb() ceases to be a
316 #define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
317 #define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
318 #define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
320 #define mb() asm volatile("mfence":::"memory")
321 #define rmb() asm volatile("lfence":::"memory")
322 #define wmb() asm volatile("sfence" ::: "memory")
326 * read_barrier_depends - Flush all pending reads that subsequents reads
329 * No data-dependent reads from memory-like regions are ever reordered
330 * over this barrier. All reads preceding this primitive are guaranteed
331 * to access memory (but not necessarily other CPUs' caches) before any
332 * reads following this primitive that depend on the data return by
333 * any of the preceding reads. This primitive is much lighter weight than
334 * rmb() on most CPUs, and is never heavier weight than is
337 * These ordering constraints are respected by both the local CPU
340 * Ordering is not guaranteed by anything other than these primitives,
341 * not even by data dependencies. See the documentation for
342 * memory_barrier() for examples and URLs to more information.
344 * For example, the following code would force ordering (the initial
345 * value of "a" is zero, "b" is one, and "p" is "&a"):
353 * read_barrier_depends();
357 * because the read of "*q" depends on the read of "p" and these
358 * two reads are separated by a read_barrier_depends(). However,
359 * the following code, with the same initial values for "a" and "b":
367 * read_barrier_depends();
371 * does not enforce ordering, since there is no data dependency between
372 * the read of "a" and the read of "b". Therefore, on some CPUs, such
373 * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
374 * in cases like this where there are no data dependencies.
377 #define read_barrier_depends() do { } while (0)
380 #define smp_mb() mb()
381 #ifdef CONFIG_X86_PPRO_FENCE
382 # define smp_rmb() rmb()
384 # define smp_rmb() barrier()
386 #ifdef CONFIG_X86_OOSTORE
387 # define smp_wmb() wmb()
389 # define smp_wmb() barrier()
391 #define smp_read_barrier_depends() read_barrier_depends()
392 #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
394 #define smp_mb() barrier()
395 #define smp_rmb() barrier()
396 #define smp_wmb() barrier()
397 #define smp_read_barrier_depends() do { } while (0)
398 #define set_mb(var, value) do { var = value; barrier(); } while (0)
402 * Stop RDTSC speculation. This is needed when you need to use RDTSC
403 * (or get_cycles or vread that possibly accesses the TSC) in a defined
406 * (Could use an alternative three way for this if there was one.)
408 static inline void rdtsc_barrier(void)
410 alternative(ASM_NOP3
, "mfence", X86_FEATURE_MFENCE_RDTSC
);
411 alternative(ASM_NOP3
, "lfence", X86_FEATURE_LFENCE_RDTSC
);