blob: b77bd8bd3cc28438358e7d22486ea55f22695133 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_SYSTEM_H
2#define _ASM_X86_SYSTEM_H
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +01003
4#include <asm/asm.h>
Glauber de Oliveira Costad46d7d72008-01-30 13:31:08 +01005#include <asm/segment.h>
6#include <asm/cpufeature.h>
7#include <asm/cmpxchg.h>
Andi Kleenfde1b3f2008-01-30 13:32:38 +01008#include <asm/nops.h>
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +01009
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +010010#include <linux/kernel.h>
Glauber de Oliveira Costad46d7d72008-01-30 13:31:08 +010011#include <linux/irqflags.h>
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +010012
Jan Beulichded9aa02008-01-30 13:31:24 +010013/* entries in ARCH_DLINFO: */
14#ifdef CONFIG_IA32_EMULATION
15# define AT_VECTOR_SIZE_ARCH 2
16#else
17# define AT_VECTOR_SIZE_ARCH 1
18#endif
19
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +010020struct task_struct; /* one of the stranger aspects of C forward declarations */
Harvey Harrison599db4f2008-02-04 16:48:03 +010021struct task_struct *__switch_to(struct task_struct *prev,
22 struct task_struct *next);
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +010023
Jaswinder Singhaab02f02008-12-15 22:23:54 +053024#ifdef CONFIG_X86_32
25
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +010026/*
27 * Saving eflags is important. It switches not only IOPL between tasks,
28 * it also protects other tasks from NT leaking through sysenter etc.
29 */
Ingo Molnar23b55bd2008-03-05 10:24:37 +010030#define switch_to(prev, next, last) \
31do { \
Ingo Molnar8b6451f2008-03-05 10:46:38 +010032 /* \
33 * Context-switching clobbers all registers, so we clobber \
34 * them explicitly, via unused output variables. \
35 * (EAX and EBP is not listed because EBP is saved/restored \
36 * explicitly for wchan access and EAX is the return value of \
37 * __switch_to()) \
38 */ \
39 unsigned long ebx, ecx, edx, esi, edi; \
Ingo Molnar23b55bd2008-03-05 10:24:37 +010040 \
Joe Perchesc5386c22008-03-23 01:03:39 -070041 asm volatile("pushfl\n\t" /* save flags */ \
42 "pushl %%ebp\n\t" /* save EBP */ \
43 "movl %%esp,%[prev_sp]\n\t" /* save ESP */ \
44 "movl %[next_sp],%%esp\n\t" /* restore ESP */ \
45 "movl $1f,%[prev_ip]\n\t" /* save EIP */ \
46 "pushl %[next_ip]\n\t" /* restore EIP */ \
47 "jmp __switch_to\n" /* regparm call */ \
48 "1:\t" \
49 "popl %%ebp\n\t" /* restore EBP */ \
50 "popfl\n" /* restore flags */ \
Ingo Molnar23b55bd2008-03-05 10:24:37 +010051 \
Joe Perchesc5386c22008-03-23 01:03:39 -070052 /* output parameters */ \
53 : [prev_sp] "=m" (prev->thread.sp), \
54 [prev_ip] "=m" (prev->thread.ip), \
55 "=a" (last), \
Ingo Molnar23b55bd2008-03-05 10:24:37 +010056 \
Joe Perchesc5386c22008-03-23 01:03:39 -070057 /* clobbered output registers: */ \
58 "=b" (ebx), "=c" (ecx), "=d" (edx), \
59 "=S" (esi), "=D" (edi) \
60 \
61 /* input parameters: */ \
62 : [next_sp] "m" (next->thread.sp), \
63 [next_ip] "m" (next->thread.ip), \
64 \
65 /* regparm parameters for __switch_to(): */ \
66 [prev] "a" (prev), \
Vegard Nossum33f8c402008-09-14 19:03:53 +020067 [next] "d" (next) \
68 \
69 : /* reloaded segment registers */ \
70 "memory"); \
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +010071} while (0)
72
Glauber de Oliveira Costad46d7d72008-01-30 13:31:08 +010073/*
74 * disable hlt during certain critical i/o operations
75 */
76#define HAVE_DISABLE_HLT
Thomas Gleixner96a388d2007-10-11 11:20:03 +020077#else
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +010078#define __SAVE(reg, offset) "movq %%" #reg ",(14-" #offset ")*8(%%rsp)\n\t"
79#define __RESTORE(reg, offset) "movq (14-" #offset ")*8(%%rsp),%%" #reg "\n\t"
80
81/* frame pointer must be last for get_wchan */
82#define SAVE_CONTEXT "pushf ; pushq %%rbp ; movq %%rsi,%%rbp\n\t"
83#define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp ; popf\t"
84
85#define __EXTRA_CLOBBER \
86 , "rcx", "rbx", "rdx", "r8", "r9", "r10", "r11", \
87 "r12", "r13", "r14", "r15"
88
Tejun Heob4a8f7a2009-01-20 12:29:19 +090089#ifdef CONFIG_CC_STACKPROTECTOR
90#define __switch_canary \
91 "movq %P[task_canary](%%rsi),%%r8\n\t" \
92 "movq %%r8,%%gs:%P[pda_canary]\n\t"
93#define __switch_canary_param \
94 , [task_canary] "i" (offsetof(struct task_struct, stack_canary)) \
95 , [pda_canary] "i" (offsetof(struct x8664_pda, stack_canary))
96#else /* CC_STACKPROTECTOR */
97#define __switch_canary
98#define __switch_canary_param
99#endif /* CC_STACKPROTECTOR */
100
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +0100101/* Save restore flags to clear handle leaking NT */
102#define switch_to(prev, next, last) \
Tejun Heob4a8f7a2009-01-20 12:29:19 +0900103 asm volatile(SAVE_CONTEXT \
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +0100104 "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \
105 "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \
106 "call __switch_to\n\t" \
107 ".globl thread_return\n" \
108 "thread_return:\n\t" \
Brian Gerst87b26402009-01-19 00:38:59 +0900109 "movq "__percpu_arg([current_task])",%%rsi\n\t" \
Tejun Heob4a8f7a2009-01-20 12:29:19 +0900110 __switch_canary \
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +0100111 "movq %P[thread_info](%%rsi),%%r8\n\t" \
112 LOCK_PREFIX "btr %[tif_fork],%P[ti_flags](%%r8)\n\t" \
113 "movq %%rax,%%rdi\n\t" \
114 "jc ret_from_fork\n\t" \
115 RESTORE_CONTEXT \
116 : "=a" (last) \
117 : [next] "S" (next), [prev] "D" (prev), \
118 [threadrsp] "i" (offsetof(struct task_struct, thread.sp)), \
119 [ti_flags] "i" (offsetof(struct thread_info, flags)), \
120 [tif_fork] "i" (TIF_FORK), \
121 [thread_info] "i" (offsetof(struct task_struct, stack)), \
Tejun Heob4a8f7a2009-01-20 12:29:19 +0900122 [current_task] "m" (per_cpu_var(current_task)) \
123 __switch_canary_param \
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +0100124 : "memory", "cc" __EXTRA_CLOBBER)
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200125#endif
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100126
127#ifdef __KERNEL__
128#define _set_base(addr, base) do { unsigned long __pr; \
129__asm__ __volatile__ ("movw %%dx,%1\n\t" \
130 "rorl $16,%%edx\n\t" \
131 "movb %%dl,%2\n\t" \
132 "movb %%dh,%3" \
133 :"=&d" (__pr) \
134 :"m" (*((addr)+2)), \
135 "m" (*((addr)+4)), \
136 "m" (*((addr)+7)), \
137 "0" (base) \
138 ); } while (0)
139
140#define _set_limit(addr, limit) do { unsigned long __lr; \
141__asm__ __volatile__ ("movw %%dx,%1\n\t" \
142 "rorl $16,%%edx\n\t" \
143 "movb %2,%%dh\n\t" \
144 "andb $0xf0,%%dh\n\t" \
145 "orb %%dh,%%dl\n\t" \
146 "movb %%dl,%2" \
147 :"=&d" (__lr) \
148 :"m" (*(addr)), \
149 "m" (*((addr)+6)), \
150 "0" (limit) \
151 ); } while (0)
152
153#define set_base(ldt, base) _set_base(((char *)&(ldt)) , (base))
154#define set_limit(ldt, limit) _set_limit(((char *)&(ldt)) , ((limit)-1))
155
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -0400156extern void native_load_gs_index(unsigned);
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100157
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100158/*
Glauber de Oliveira Costaa6b46552008-01-30 13:31:08 +0100159 * Load a segment. Fall back on loading the zero
160 * segment if something goes wrong..
161 */
162#define loadsegment(seg, value) \
163 asm volatile("\n" \
Joe Perchesc5386c22008-03-23 01:03:39 -0700164 "1:\t" \
165 "movl %k0,%%" #seg "\n" \
166 "2:\n" \
167 ".section .fixup,\"ax\"\n" \
168 "3:\t" \
169 "movl %k1, %%" #seg "\n\t" \
170 "jmp 2b\n" \
171 ".previous\n" \
172 _ASM_EXTABLE(1b,3b) \
Jeremy Fitzhardinged338c732008-06-25 00:18:58 -0400173 : :"r" (value), "r" (0) : "memory")
Glauber de Oliveira Costaa6b46552008-01-30 13:31:08 +0100174
175
176/*
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100177 * Save a segment register away
178 */
Joe Perchesc5386c22008-03-23 01:03:39 -0700179#define savesegment(seg, value) \
Ingo Molnard9fc3fd2008-07-11 19:41:19 +0200180 asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100181
182static inline unsigned long get_limit(unsigned long segment)
183{
184 unsigned long __limit;
Joe Perchesc5386c22008-03-23 01:03:39 -0700185 asm("lsll %1,%0" : "=r" (__limit) : "r" (segment));
186 return __limit + 1;
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100187}
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100188
189static inline void native_clts(void)
190{
Joe Perchesc5386c22008-03-23 01:03:39 -0700191 asm volatile("clts");
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100192}
193
194/*
195 * Volatile isn't enough to prevent the compiler from reordering the
196 * read/write functions for the control registers and messing everything up.
197 * A memory clobber would solve the problem, but would prevent reordering of
198 * all loads stores around it, which can hurt performance. Solution is to
199 * use a variable and mimic reads and writes to it to enforce serialization
200 */
201static unsigned long __force_order;
202
203static inline unsigned long native_read_cr0(void)
204{
205 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700206 asm volatile("mov %%cr0,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100207 return val;
208}
209
210static inline void native_write_cr0(unsigned long val)
211{
Joe Perchesc5386c22008-03-23 01:03:39 -0700212 asm volatile("mov %0,%%cr0": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100213}
214
215static inline unsigned long native_read_cr2(void)
216{
217 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700218 asm volatile("mov %%cr2,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100219 return val;
220}
221
222static inline void native_write_cr2(unsigned long val)
223{
Joe Perchesc5386c22008-03-23 01:03:39 -0700224 asm volatile("mov %0,%%cr2": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100225}
226
227static inline unsigned long native_read_cr3(void)
228{
229 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700230 asm volatile("mov %%cr3,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100231 return val;
232}
233
234static inline void native_write_cr3(unsigned long val)
235{
Joe Perchesc5386c22008-03-23 01:03:39 -0700236 asm volatile("mov %0,%%cr3": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100237}
238
239static inline unsigned long native_read_cr4(void)
240{
241 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700242 asm volatile("mov %%cr4,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100243 return val;
244}
245
246static inline unsigned long native_read_cr4_safe(void)
247{
248 unsigned long val;
249 /* This could fault if %cr4 does not exist. In x86_64, a cr4 always
250 * exists, so it will never fail. */
251#ifdef CONFIG_X86_32
H. Peter Anvin88976ee2008-02-04 16:47:58 +0100252 asm volatile("1: mov %%cr4, %0\n"
253 "2:\n"
Joe Perchesc5386c22008-03-23 01:03:39 -0700254 _ASM_EXTABLE(1b, 2b)
H. Peter Anvin88976ee2008-02-04 16:47:58 +0100255 : "=r" (val), "=m" (__force_order) : "0" (0));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100256#else
257 val = native_read_cr4();
258#endif
259 return val;
260}
261
262static inline void native_write_cr4(unsigned long val)
263{
Joe Perchesc5386c22008-03-23 01:03:39 -0700264 asm volatile("mov %0,%%cr4": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100265}
266
Glauber de Oliveira Costa94ea03c2008-01-30 13:33:19 +0100267#ifdef CONFIG_X86_64
268static inline unsigned long native_read_cr8(void)
269{
270 unsigned long cr8;
271 asm volatile("movq %%cr8,%0" : "=r" (cr8));
272 return cr8;
273}
274
275static inline void native_write_cr8(unsigned long val)
276{
277 asm volatile("movq %0,%%cr8" :: "r" (val) : "memory");
278}
279#endif
280
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100281static inline void native_wbinvd(void)
282{
283 asm volatile("wbinvd": : :"memory");
284}
Joe Perchesc5386c22008-03-23 01:03:39 -0700285
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100286#ifdef CONFIG_PARAVIRT
287#include <asm/paravirt.h>
288#else
289#define read_cr0() (native_read_cr0())
290#define write_cr0(x) (native_write_cr0(x))
291#define read_cr2() (native_read_cr2())
292#define write_cr2(x) (native_write_cr2(x))
293#define read_cr3() (native_read_cr3())
294#define write_cr3(x) (native_write_cr3(x))
295#define read_cr4() (native_read_cr4())
296#define read_cr4_safe() (native_read_cr4_safe())
297#define write_cr4(x) (native_write_cr4(x))
298#define wbinvd() (native_wbinvd())
Glauber de Oliveira Costad46d7d72008-01-30 13:31:08 +0100299#ifdef CONFIG_X86_64
Glauber de Oliveira Costa94ea03c2008-01-30 13:33:19 +0100300#define read_cr8() (native_read_cr8())
301#define write_cr8(x) (native_write_cr8(x))
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -0400302#define load_gs_index native_load_gs_index
Glauber de Oliveira Costad46d7d72008-01-30 13:31:08 +0100303#endif
304
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100305/* Clear the 'TS' bit */
306#define clts() (native_clts())
307
308#endif/* CONFIG_PARAVIRT */
309
Jeremy Fitzhardinge4e09e212008-05-26 23:31:03 +0100310#define stts() write_cr0(read_cr0() | X86_CR0_TS)
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100311
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100312#endif /* __KERNEL__ */
313
H. Peter Anvin84fb1442008-02-04 16:48:00 +0100314static inline void clflush(volatile void *__p)
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100315{
H. Peter Anvin84fb1442008-02-04 16:48:00 +0100316 asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100317}
318
Joe Perchesc5386c22008-03-23 01:03:39 -0700319#define nop() asm volatile ("nop")
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100320
321void disable_hlt(void);
322void enable_hlt(void);
323
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100324void cpu_idle_wait(void);
325
326extern unsigned long arch_align_stack(unsigned long sp);
327extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
328
329void default_idle(void);
330
Ivan Vecerad3ec5ca2008-11-11 14:33:44 +0100331void stop_this_cpu(void *dummy);
332
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100333/*
334 * Force strict CPU ordering.
335 * And yes, this is required on UP too when we're talking
336 * to devices.
337 */
338#ifdef CONFIG_X86_32
339/*
Pavel Machek0d7a1812008-03-03 12:49:09 +0100340 * Some non-Intel clones support out of order store. wmb() ceases to be a
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100341 * nop for these.
342 */
343#define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
344#define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
345#define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
346#else
347#define mb() asm volatile("mfence":::"memory")
348#define rmb() asm volatile("lfence":::"memory")
349#define wmb() asm volatile("sfence" ::: "memory")
350#endif
351
352/**
353 * read_barrier_depends - Flush all pending reads that subsequents reads
354 * depend on.
355 *
356 * No data-dependent reads from memory-like regions are ever reordered
357 * over this barrier. All reads preceding this primitive are guaranteed
358 * to access memory (but not necessarily other CPUs' caches) before any
359 * reads following this primitive that depend on the data return by
360 * any of the preceding reads. This primitive is much lighter weight than
361 * rmb() on most CPUs, and is never heavier weight than is
362 * rmb().
363 *
364 * These ordering constraints are respected by both the local CPU
365 * and the compiler.
366 *
367 * Ordering is not guaranteed by anything other than these primitives,
368 * not even by data dependencies. See the documentation for
369 * memory_barrier() for examples and URLs to more information.
370 *
371 * For example, the following code would force ordering (the initial
372 * value of "a" is zero, "b" is one, and "p" is "&a"):
373 *
374 * <programlisting>
375 * CPU 0 CPU 1
376 *
377 * b = 2;
378 * memory_barrier();
379 * p = &b; q = p;
380 * read_barrier_depends();
381 * d = *q;
382 * </programlisting>
383 *
384 * because the read of "*q" depends on the read of "p" and these
385 * two reads are separated by a read_barrier_depends(). However,
386 * the following code, with the same initial values for "a" and "b":
387 *
388 * <programlisting>
389 * CPU 0 CPU 1
390 *
391 * a = 2;
392 * memory_barrier();
393 * b = 3; y = b;
394 * read_barrier_depends();
395 * x = a;
396 * </programlisting>
397 *
398 * does not enforce ordering, since there is no data dependency between
399 * the read of "a" and the read of "b". Therefore, on some CPUs, such
400 * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
401 * in cases like this where there are no data dependencies.
402 **/
403
404#define read_barrier_depends() do { } while (0)
405
406#ifdef CONFIG_SMP
407#define smp_mb() mb()
408#ifdef CONFIG_X86_PPRO_FENCE
409# define smp_rmb() rmb()
410#else
411# define smp_rmb() barrier()
412#endif
413#ifdef CONFIG_X86_OOSTORE
414# define smp_wmb() wmb()
415#else
416# define smp_wmb() barrier()
417#endif
418#define smp_read_barrier_depends() read_barrier_depends()
Joe Perchesc5386c22008-03-23 01:03:39 -0700419#define set_mb(var, value) do { (void)xchg(&var, value); } while (0)
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100420#else
421#define smp_mb() barrier()
422#define smp_rmb() barrier()
423#define smp_wmb() barrier()
424#define smp_read_barrier_depends() do { } while (0)
425#define set_mb(var, value) do { var = value; barrier(); } while (0)
426#endif
427
Andi Kleenfde1b3f2008-01-30 13:32:38 +0100428/*
429 * Stop RDTSC speculation. This is needed when you need to use RDTSC
430 * (or get_cycles or vread that possibly accesses the TSC) in a defined
431 * code region.
432 *
433 * (Could use an alternative three way for this if there was one.)
434 */
435static inline void rdtsc_barrier(void)
436{
437 alternative(ASM_NOP3, "mfence", X86_FEATURE_MFENCE_RDTSC);
438 alternative(ASM_NOP3, "lfence", X86_FEATURE_LFENCE_RDTSC);
439}
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100440
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700441#endif /* _ASM_X86_SYSTEM_H */