blob: 8cadfe9b1194ebfe78a4fb4bf2bce6c0e3a8dbba [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
89/* Save restore flags to clear handle leaking NT */
90#define switch_to(prev, next, last) \
Jan Beulichded9aa02008-01-30 13:31:24 +010091 asm volatile(SAVE_CONTEXT \
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +010092 "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \
93 "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \
94 "call __switch_to\n\t" \
95 ".globl thread_return\n" \
96 "thread_return:\n\t" \
Brian Gerst87b26402009-01-19 00:38:59 +090097 "movq "__percpu_arg([current_task])",%%rsi\n\t" \
Ingo Molnara9de18e2008-12-31 08:31:57 +010098 "movq %P[task_canary](%%rsi),%%r8\n\t" \
99 "movq %%r8,%%gs:%P[pda_canary]\n\t" \
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +0100100 "movq %P[thread_info](%%rsi),%%r8\n\t" \
101 LOCK_PREFIX "btr %[tif_fork],%P[ti_flags](%%r8)\n\t" \
102 "movq %%rax,%%rdi\n\t" \
103 "jc ret_from_fork\n\t" \
104 RESTORE_CONTEXT \
105 : "=a" (last) \
106 : [next] "S" (next), [prev] "D" (prev), \
107 [threadrsp] "i" (offsetof(struct task_struct, thread.sp)), \
108 [ti_flags] "i" (offsetof(struct thread_info, flags)), \
109 [tif_fork] "i" (TIF_FORK), \
110 [thread_info] "i" (offsetof(struct task_struct, stack)), \
Ingo Molnara9de18e2008-12-31 08:31:57 +0100111 [task_canary] "i" (offsetof(struct task_struct, stack_canary)),\
Ingo Molnarb2b062b2009-01-18 18:37:14 +0100112 [current_task] "m" (per_cpu_var(current_task)), \
Ingo Molnara9de18e2008-12-31 08:31:57 +0100113 [pda_canary] "i" (offsetof(struct x8664_pda, stack_canary))\
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +0100114 : "memory", "cc" __EXTRA_CLOBBER)
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200115#endif
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100116
117#ifdef __KERNEL__
118#define _set_base(addr, base) do { unsigned long __pr; \
119__asm__ __volatile__ ("movw %%dx,%1\n\t" \
120 "rorl $16,%%edx\n\t" \
121 "movb %%dl,%2\n\t" \
122 "movb %%dh,%3" \
123 :"=&d" (__pr) \
124 :"m" (*((addr)+2)), \
125 "m" (*((addr)+4)), \
126 "m" (*((addr)+7)), \
127 "0" (base) \
128 ); } while (0)
129
130#define _set_limit(addr, limit) do { unsigned long __lr; \
131__asm__ __volatile__ ("movw %%dx,%1\n\t" \
132 "rorl $16,%%edx\n\t" \
133 "movb %2,%%dh\n\t" \
134 "andb $0xf0,%%dh\n\t" \
135 "orb %%dh,%%dl\n\t" \
136 "movb %%dl,%2" \
137 :"=&d" (__lr) \
138 :"m" (*(addr)), \
139 "m" (*((addr)+6)), \
140 "0" (limit) \
141 ); } while (0)
142
143#define set_base(ldt, base) _set_base(((char *)&(ldt)) , (base))
144#define set_limit(ldt, limit) _set_limit(((char *)&(ldt)) , ((limit)-1))
145
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -0400146extern void native_load_gs_index(unsigned);
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100147
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100148/*
Glauber de Oliveira Costaa6b46552008-01-30 13:31:08 +0100149 * Load a segment. Fall back on loading the zero
150 * segment if something goes wrong..
151 */
152#define loadsegment(seg, value) \
153 asm volatile("\n" \
Joe Perchesc5386c22008-03-23 01:03:39 -0700154 "1:\t" \
155 "movl %k0,%%" #seg "\n" \
156 "2:\n" \
157 ".section .fixup,\"ax\"\n" \
158 "3:\t" \
159 "movl %k1, %%" #seg "\n\t" \
160 "jmp 2b\n" \
161 ".previous\n" \
162 _ASM_EXTABLE(1b,3b) \
Jeremy Fitzhardinged338c732008-06-25 00:18:58 -0400163 : :"r" (value), "r" (0) : "memory")
Glauber de Oliveira Costaa6b46552008-01-30 13:31:08 +0100164
165
166/*
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100167 * Save a segment register away
168 */
Joe Perchesc5386c22008-03-23 01:03:39 -0700169#define savesegment(seg, value) \
Ingo Molnard9fc3fd2008-07-11 19:41:19 +0200170 asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100171
172static inline unsigned long get_limit(unsigned long segment)
173{
174 unsigned long __limit;
Joe Perchesc5386c22008-03-23 01:03:39 -0700175 asm("lsll %1,%0" : "=r" (__limit) : "r" (segment));
176 return __limit + 1;
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100177}
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100178
179static inline void native_clts(void)
180{
Joe Perchesc5386c22008-03-23 01:03:39 -0700181 asm volatile("clts");
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100182}
183
184/*
185 * Volatile isn't enough to prevent the compiler from reordering the
186 * read/write functions for the control registers and messing everything up.
187 * A memory clobber would solve the problem, but would prevent reordering of
188 * all loads stores around it, which can hurt performance. Solution is to
189 * use a variable and mimic reads and writes to it to enforce serialization
190 */
191static unsigned long __force_order;
192
193static inline unsigned long native_read_cr0(void)
194{
195 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700196 asm volatile("mov %%cr0,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100197 return val;
198}
199
200static inline void native_write_cr0(unsigned long val)
201{
Joe Perchesc5386c22008-03-23 01:03:39 -0700202 asm volatile("mov %0,%%cr0": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100203}
204
205static inline unsigned long native_read_cr2(void)
206{
207 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700208 asm volatile("mov %%cr2,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100209 return val;
210}
211
212static inline void native_write_cr2(unsigned long val)
213{
Joe Perchesc5386c22008-03-23 01:03:39 -0700214 asm volatile("mov %0,%%cr2": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100215}
216
217static inline unsigned long native_read_cr3(void)
218{
219 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700220 asm volatile("mov %%cr3,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100221 return val;
222}
223
224static inline void native_write_cr3(unsigned long val)
225{
Joe Perchesc5386c22008-03-23 01:03:39 -0700226 asm volatile("mov %0,%%cr3": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100227}
228
229static inline unsigned long native_read_cr4(void)
230{
231 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700232 asm volatile("mov %%cr4,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100233 return val;
234}
235
236static inline unsigned long native_read_cr4_safe(void)
237{
238 unsigned long val;
239 /* This could fault if %cr4 does not exist. In x86_64, a cr4 always
240 * exists, so it will never fail. */
241#ifdef CONFIG_X86_32
H. Peter Anvin88976ee2008-02-04 16:47:58 +0100242 asm volatile("1: mov %%cr4, %0\n"
243 "2:\n"
Joe Perchesc5386c22008-03-23 01:03:39 -0700244 _ASM_EXTABLE(1b, 2b)
H. Peter Anvin88976ee2008-02-04 16:47:58 +0100245 : "=r" (val), "=m" (__force_order) : "0" (0));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100246#else
247 val = native_read_cr4();
248#endif
249 return val;
250}
251
252static inline void native_write_cr4(unsigned long val)
253{
Joe Perchesc5386c22008-03-23 01:03:39 -0700254 asm volatile("mov %0,%%cr4": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100255}
256
Glauber de Oliveira Costa94ea03c2008-01-30 13:33:19 +0100257#ifdef CONFIG_X86_64
258static inline unsigned long native_read_cr8(void)
259{
260 unsigned long cr8;
261 asm volatile("movq %%cr8,%0" : "=r" (cr8));
262 return cr8;
263}
264
265static inline void native_write_cr8(unsigned long val)
266{
267 asm volatile("movq %0,%%cr8" :: "r" (val) : "memory");
268}
269#endif
270
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100271static inline void native_wbinvd(void)
272{
273 asm volatile("wbinvd": : :"memory");
274}
Joe Perchesc5386c22008-03-23 01:03:39 -0700275
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100276#ifdef CONFIG_PARAVIRT
277#include <asm/paravirt.h>
278#else
279#define read_cr0() (native_read_cr0())
280#define write_cr0(x) (native_write_cr0(x))
281#define read_cr2() (native_read_cr2())
282#define write_cr2(x) (native_write_cr2(x))
283#define read_cr3() (native_read_cr3())
284#define write_cr3(x) (native_write_cr3(x))
285#define read_cr4() (native_read_cr4())
286#define read_cr4_safe() (native_read_cr4_safe())
287#define write_cr4(x) (native_write_cr4(x))
288#define wbinvd() (native_wbinvd())
Glauber de Oliveira Costad46d7d72008-01-30 13:31:08 +0100289#ifdef CONFIG_X86_64
Glauber de Oliveira Costa94ea03c2008-01-30 13:33:19 +0100290#define read_cr8() (native_read_cr8())
291#define write_cr8(x) (native_write_cr8(x))
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -0400292#define load_gs_index native_load_gs_index
Glauber de Oliveira Costad46d7d72008-01-30 13:31:08 +0100293#endif
294
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100295/* Clear the 'TS' bit */
296#define clts() (native_clts())
297
298#endif/* CONFIG_PARAVIRT */
299
Jeremy Fitzhardinge4e09e212008-05-26 23:31:03 +0100300#define stts() write_cr0(read_cr0() | X86_CR0_TS)
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100301
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100302#endif /* __KERNEL__ */
303
H. Peter Anvin84fb1442008-02-04 16:48:00 +0100304static inline void clflush(volatile void *__p)
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100305{
H. Peter Anvin84fb1442008-02-04 16:48:00 +0100306 asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100307}
308
Joe Perchesc5386c22008-03-23 01:03:39 -0700309#define nop() asm volatile ("nop")
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100310
311void disable_hlt(void);
312void enable_hlt(void);
313
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100314void cpu_idle_wait(void);
315
316extern unsigned long arch_align_stack(unsigned long sp);
317extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
318
319void default_idle(void);
320
Ivan Vecerad3ec5ca2008-11-11 14:33:44 +0100321void stop_this_cpu(void *dummy);
322
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100323/*
324 * Force strict CPU ordering.
325 * And yes, this is required on UP too when we're talking
326 * to devices.
327 */
328#ifdef CONFIG_X86_32
329/*
Pavel Machek0d7a1812008-03-03 12:49:09 +0100330 * Some non-Intel clones support out of order store. wmb() ceases to be a
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100331 * nop for these.
332 */
333#define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
334#define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
335#define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
336#else
337#define mb() asm volatile("mfence":::"memory")
338#define rmb() asm volatile("lfence":::"memory")
339#define wmb() asm volatile("sfence" ::: "memory")
340#endif
341
342/**
343 * read_barrier_depends - Flush all pending reads that subsequents reads
344 * depend on.
345 *
346 * No data-dependent reads from memory-like regions are ever reordered
347 * over this barrier. All reads preceding this primitive are guaranteed
348 * to access memory (but not necessarily other CPUs' caches) before any
349 * reads following this primitive that depend on the data return by
350 * any of the preceding reads. This primitive is much lighter weight than
351 * rmb() on most CPUs, and is never heavier weight than is
352 * rmb().
353 *
354 * These ordering constraints are respected by both the local CPU
355 * and the compiler.
356 *
357 * Ordering is not guaranteed by anything other than these primitives,
358 * not even by data dependencies. See the documentation for
359 * memory_barrier() for examples and URLs to more information.
360 *
361 * For example, the following code would force ordering (the initial
362 * value of "a" is zero, "b" is one, and "p" is "&a"):
363 *
364 * <programlisting>
365 * CPU 0 CPU 1
366 *
367 * b = 2;
368 * memory_barrier();
369 * p = &b; q = p;
370 * read_barrier_depends();
371 * d = *q;
372 * </programlisting>
373 *
374 * because the read of "*q" depends on the read of "p" and these
375 * two reads are separated by a read_barrier_depends(). However,
376 * the following code, with the same initial values for "a" and "b":
377 *
378 * <programlisting>
379 * CPU 0 CPU 1
380 *
381 * a = 2;
382 * memory_barrier();
383 * b = 3; y = b;
384 * read_barrier_depends();
385 * x = a;
386 * </programlisting>
387 *
388 * does not enforce ordering, since there is no data dependency between
389 * the read of "a" and the read of "b". Therefore, on some CPUs, such
390 * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
391 * in cases like this where there are no data dependencies.
392 **/
393
394#define read_barrier_depends() do { } while (0)
395
396#ifdef CONFIG_SMP
397#define smp_mb() mb()
398#ifdef CONFIG_X86_PPRO_FENCE
399# define smp_rmb() rmb()
400#else
401# define smp_rmb() barrier()
402#endif
403#ifdef CONFIG_X86_OOSTORE
404# define smp_wmb() wmb()
405#else
406# define smp_wmb() barrier()
407#endif
408#define smp_read_barrier_depends() read_barrier_depends()
Joe Perchesc5386c22008-03-23 01:03:39 -0700409#define set_mb(var, value) do { (void)xchg(&var, value); } while (0)
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100410#else
411#define smp_mb() barrier()
412#define smp_rmb() barrier()
413#define smp_wmb() barrier()
414#define smp_read_barrier_depends() do { } while (0)
415#define set_mb(var, value) do { var = value; barrier(); } while (0)
416#endif
417
Andi Kleenfde1b3f2008-01-30 13:32:38 +0100418/*
419 * Stop RDTSC speculation. This is needed when you need to use RDTSC
420 * (or get_cycles or vread that possibly accesses the TSC) in a defined
421 * code region.
422 *
423 * (Could use an alternative three way for this if there was one.)
424 */
425static inline void rdtsc_barrier(void)
426{
427 alternative(ASM_NOP3, "mfence", X86_FEATURE_MFENCE_RDTSC);
428 alternative(ASM_NOP3, "lfence", X86_FEATURE_LFENCE_RDTSC);
429}
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100430
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700431#endif /* _ASM_X86_SYSTEM_H */