blob: 7a80f72bec470a5f1929bc5f43c5bd53d20c438c [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
Tejun Heo60a53172009-02-09 22:17:40 +090026#ifdef CONFIG_CC_STACKPROTECTOR
27#define __switch_canary \
Tejun Heo5c79d2a2009-02-11 16:31:00 +090028 "movl %P[task_canary](%[next]), %%ebx\n\t" \
29 "movl %%ebx, "__percpu_arg([stack_canary])"\n\t"
Tejun Heo60a53172009-02-09 22:17:40 +090030#define __switch_canary_oparam \
31 , [stack_canary] "=m" (per_cpu_var(stack_canary))
32#define __switch_canary_iparam \
Tejun Heo60a53172009-02-09 22:17:40 +090033 , [task_canary] "i" (offsetof(struct task_struct, stack_canary))
34#else /* CC_STACKPROTECTOR */
35#define __switch_canary
36#define __switch_canary_oparam
37#define __switch_canary_iparam
38#endif /* CC_STACKPROTECTOR */
39
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +010040/*
41 * Saving eflags is important. It switches not only IOPL between tasks,
42 * it also protects other tasks from NT leaking through sysenter etc.
43 */
Ingo Molnar23b55bd2008-03-05 10:24:37 +010044#define switch_to(prev, next, last) \
45do { \
Ingo Molnar8b6451f2008-03-05 10:46:38 +010046 /* \
47 * Context-switching clobbers all registers, so we clobber \
48 * them explicitly, via unused output variables. \
49 * (EAX and EBP is not listed because EBP is saved/restored \
50 * explicitly for wchan access and EAX is the return value of \
51 * __switch_to()) \
52 */ \
53 unsigned long ebx, ecx, edx, esi, edi; \
Ingo Molnar23b55bd2008-03-05 10:24:37 +010054 \
Joe Perchesc5386c22008-03-23 01:03:39 -070055 asm volatile("pushfl\n\t" /* save flags */ \
56 "pushl %%ebp\n\t" /* save EBP */ \
57 "movl %%esp,%[prev_sp]\n\t" /* save ESP */ \
58 "movl %[next_sp],%%esp\n\t" /* restore ESP */ \
59 "movl $1f,%[prev_ip]\n\t" /* save EIP */ \
60 "pushl %[next_ip]\n\t" /* restore EIP */ \
Tejun Heo5c79d2a2009-02-11 16:31:00 +090061 __switch_canary \
Joe Perchesc5386c22008-03-23 01:03:39 -070062 "jmp __switch_to\n" /* regparm call */ \
63 "1:\t" \
64 "popl %%ebp\n\t" /* restore EBP */ \
65 "popfl\n" /* restore flags */ \
Ingo Molnar23b55bd2008-03-05 10:24:37 +010066 \
Joe Perchesc5386c22008-03-23 01:03:39 -070067 /* output parameters */ \
68 : [prev_sp] "=m" (prev->thread.sp), \
69 [prev_ip] "=m" (prev->thread.ip), \
70 "=a" (last), \
Ingo Molnar23b55bd2008-03-05 10:24:37 +010071 \
Joe Perchesc5386c22008-03-23 01:03:39 -070072 /* clobbered output registers: */ \
73 "=b" (ebx), "=c" (ecx), "=d" (edx), \
74 "=S" (esi), "=D" (edi) \
75 \
Tejun Heo60a53172009-02-09 22:17:40 +090076 __switch_canary_oparam \
77 \
Joe Perchesc5386c22008-03-23 01:03:39 -070078 /* input parameters: */ \
79 : [next_sp] "m" (next->thread.sp), \
80 [next_ip] "m" (next->thread.ip), \
81 \
82 /* regparm parameters for __switch_to(): */ \
83 [prev] "a" (prev), \
Vegard Nossum33f8c402008-09-14 19:03:53 +020084 [next] "d" (next) \
85 \
Tejun Heo60a53172009-02-09 22:17:40 +090086 __switch_canary_iparam \
87 \
Vegard Nossum33f8c402008-09-14 19:03:53 +020088 : /* reloaded segment registers */ \
89 "memory"); \
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +010090} while (0)
91
Glauber de Oliveira Costad46d7d72008-01-30 13:31:08 +010092/*
93 * disable hlt during certain critical i/o operations
94 */
95#define HAVE_DISABLE_HLT
Thomas Gleixner96a388d2007-10-11 11:20:03 +020096#else
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +010097#define __SAVE(reg, offset) "movq %%" #reg ",(14-" #offset ")*8(%%rsp)\n\t"
98#define __RESTORE(reg, offset) "movq (14-" #offset ")*8(%%rsp),%%" #reg "\n\t"
99
100/* frame pointer must be last for get_wchan */
101#define SAVE_CONTEXT "pushf ; pushq %%rbp ; movq %%rsi,%%rbp\n\t"
102#define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp ; popf\t"
103
104#define __EXTRA_CLOBBER \
105 , "rcx", "rbx", "rdx", "r8", "r9", "r10", "r11", \
106 "r12", "r13", "r14", "r15"
107
Tejun Heob4a8f7a2009-01-20 12:29:19 +0900108#ifdef CONFIG_CC_STACKPROTECTOR
109#define __switch_canary \
110 "movq %P[task_canary](%%rsi),%%r8\n\t" \
Tejun Heo67e68bd2009-01-21 17:26:05 +0900111 "movq %%r8,"__percpu_arg([gs_canary])"\n\t"
112#define __switch_canary_oparam \
113 , [gs_canary] "=m" (per_cpu_var(irq_stack_union.stack_canary))
114#define __switch_canary_iparam \
115 , [task_canary] "i" (offsetof(struct task_struct, stack_canary))
Tejun Heob4a8f7a2009-01-20 12:29:19 +0900116#else /* CC_STACKPROTECTOR */
117#define __switch_canary
Tejun Heo67e68bd2009-01-21 17:26:05 +0900118#define __switch_canary_oparam
119#define __switch_canary_iparam
Tejun Heob4a8f7a2009-01-20 12:29:19 +0900120#endif /* CC_STACKPROTECTOR */
121
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +0100122/* Save restore flags to clear handle leaking NT */
123#define switch_to(prev, next, last) \
Tejun Heob4a8f7a2009-01-20 12:29:19 +0900124 asm volatile(SAVE_CONTEXT \
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +0100125 "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \
126 "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \
127 "call __switch_to\n\t" \
128 ".globl thread_return\n" \
129 "thread_return:\n\t" \
Brian Gerst87b26402009-01-19 00:38:59 +0900130 "movq "__percpu_arg([current_task])",%%rsi\n\t" \
Tejun Heob4a8f7a2009-01-20 12:29:19 +0900131 __switch_canary \
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +0100132 "movq %P[thread_info](%%rsi),%%r8\n\t" \
133 LOCK_PREFIX "btr %[tif_fork],%P[ti_flags](%%r8)\n\t" \
134 "movq %%rax,%%rdi\n\t" \
135 "jc ret_from_fork\n\t" \
136 RESTORE_CONTEXT \
137 : "=a" (last) \
Tejun Heo67e68bd2009-01-21 17:26:05 +0900138 __switch_canary_oparam \
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +0100139 : [next] "S" (next), [prev] "D" (prev), \
140 [threadrsp] "i" (offsetof(struct task_struct, thread.sp)), \
141 [ti_flags] "i" (offsetof(struct thread_info, flags)), \
142 [tif_fork] "i" (TIF_FORK), \
143 [thread_info] "i" (offsetof(struct task_struct, stack)), \
Tejun Heob4a8f7a2009-01-20 12:29:19 +0900144 [current_task] "m" (per_cpu_var(current_task)) \
Tejun Heo67e68bd2009-01-21 17:26:05 +0900145 __switch_canary_iparam \
Glauber de Oliveira Costa0a3b4d12008-01-30 13:31:08 +0100146 : "memory", "cc" __EXTRA_CLOBBER)
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200147#endif
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100148
149#ifdef __KERNEL__
150#define _set_base(addr, base) do { unsigned long __pr; \
151__asm__ __volatile__ ("movw %%dx,%1\n\t" \
152 "rorl $16,%%edx\n\t" \
153 "movb %%dl,%2\n\t" \
154 "movb %%dh,%3" \
155 :"=&d" (__pr) \
156 :"m" (*((addr)+2)), \
157 "m" (*((addr)+4)), \
158 "m" (*((addr)+7)), \
159 "0" (base) \
160 ); } while (0)
161
162#define _set_limit(addr, limit) do { unsigned long __lr; \
163__asm__ __volatile__ ("movw %%dx,%1\n\t" \
164 "rorl $16,%%edx\n\t" \
165 "movb %2,%%dh\n\t" \
166 "andb $0xf0,%%dh\n\t" \
167 "orb %%dh,%%dl\n\t" \
168 "movb %%dl,%2" \
169 :"=&d" (__lr) \
170 :"m" (*(addr)), \
171 "m" (*((addr)+6)), \
172 "0" (limit) \
173 ); } while (0)
174
175#define set_base(ldt, base) _set_base(((char *)&(ldt)) , (base))
176#define set_limit(ldt, limit) _set_limit(((char *)&(ldt)) , ((limit)-1))
177
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -0400178extern void native_load_gs_index(unsigned);
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100179
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100180/*
Glauber de Oliveira Costaa6b46552008-01-30 13:31:08 +0100181 * Load a segment. Fall back on loading the zero
182 * segment if something goes wrong..
183 */
184#define loadsegment(seg, value) \
185 asm volatile("\n" \
Joe Perchesc5386c22008-03-23 01:03:39 -0700186 "1:\t" \
187 "movl %k0,%%" #seg "\n" \
188 "2:\n" \
189 ".section .fixup,\"ax\"\n" \
190 "3:\t" \
191 "movl %k1, %%" #seg "\n\t" \
192 "jmp 2b\n" \
193 ".previous\n" \
194 _ASM_EXTABLE(1b,3b) \
Jeremy Fitzhardinged338c732008-06-25 00:18:58 -0400195 : :"r" (value), "r" (0) : "memory")
Glauber de Oliveira Costaa6b46552008-01-30 13:31:08 +0100196
197
198/*
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100199 * Save a segment register away
200 */
Joe Perchesc5386c22008-03-23 01:03:39 -0700201#define savesegment(seg, value) \
Ingo Molnard9fc3fd2008-07-11 19:41:19 +0200202 asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100203
Tejun Heod9a89a22009-02-09 22:17:40 +0900204/*
205 * x86_32 user gs accessors.
206 */
207#ifdef CONFIG_X86_32
Tejun Heoccbeed32009-02-09 22:17:40 +0900208#ifdef CONFIG_X86_32_LAZY_GS
Tejun Heod9a89a22009-02-09 22:17:40 +0900209#define get_user_gs(regs) (u16)({unsigned long v; savesegment(gs, v); v;})
210#define set_user_gs(regs, v) loadsegment(gs, (unsigned long)(v))
211#define task_user_gs(tsk) ((tsk)->thread.gs)
Tejun Heoccbeed32009-02-09 22:17:40 +0900212#define lazy_save_gs(v) savesegment(gs, (v))
213#define lazy_load_gs(v) loadsegment(gs, (v))
214#else /* X86_32_LAZY_GS */
215#define get_user_gs(regs) (u16)((regs)->gs)
216#define set_user_gs(regs, v) do { (regs)->gs = (v); } while (0)
217#define task_user_gs(tsk) (task_pt_regs(tsk)->gs)
218#define lazy_save_gs(v) do { } while (0)
219#define lazy_load_gs(v) do { } while (0)
220#endif /* X86_32_LAZY_GS */
221#endif /* X86_32 */
Tejun Heod9a89a22009-02-09 22:17:40 +0900222
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100223static inline unsigned long get_limit(unsigned long segment)
224{
225 unsigned long __limit;
Joe Perchesc5386c22008-03-23 01:03:39 -0700226 asm("lsll %1,%0" : "=r" (__limit) : "r" (segment));
227 return __limit + 1;
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100228}
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100229
230static inline void native_clts(void)
231{
Joe Perchesc5386c22008-03-23 01:03:39 -0700232 asm volatile("clts");
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100233}
234
235/*
236 * Volatile isn't enough to prevent the compiler from reordering the
237 * read/write functions for the control registers and messing everything up.
238 * A memory clobber would solve the problem, but would prevent reordering of
239 * all loads stores around it, which can hurt performance. Solution is to
240 * use a variable and mimic reads and writes to it to enforce serialization
241 */
242static unsigned long __force_order;
243
244static inline unsigned long native_read_cr0(void)
245{
246 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700247 asm volatile("mov %%cr0,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100248 return val;
249}
250
251static inline void native_write_cr0(unsigned long val)
252{
Joe Perchesc5386c22008-03-23 01:03:39 -0700253 asm volatile("mov %0,%%cr0": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100254}
255
256static inline unsigned long native_read_cr2(void)
257{
258 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700259 asm volatile("mov %%cr2,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100260 return val;
261}
262
263static inline void native_write_cr2(unsigned long val)
264{
Joe Perchesc5386c22008-03-23 01:03:39 -0700265 asm volatile("mov %0,%%cr2": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100266}
267
268static inline unsigned long native_read_cr3(void)
269{
270 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700271 asm volatile("mov %%cr3,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100272 return val;
273}
274
275static inline void native_write_cr3(unsigned long val)
276{
Joe Perchesc5386c22008-03-23 01:03:39 -0700277 asm volatile("mov %0,%%cr3": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100278}
279
280static inline unsigned long native_read_cr4(void)
281{
282 unsigned long val;
Joe Perchesc5386c22008-03-23 01:03:39 -0700283 asm volatile("mov %%cr4,%0\n\t" : "=r" (val), "=m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100284 return val;
285}
286
287static inline unsigned long native_read_cr4_safe(void)
288{
289 unsigned long val;
290 /* This could fault if %cr4 does not exist. In x86_64, a cr4 always
291 * exists, so it will never fail. */
292#ifdef CONFIG_X86_32
H. Peter Anvin88976ee2008-02-04 16:47:58 +0100293 asm volatile("1: mov %%cr4, %0\n"
294 "2:\n"
Joe Perchesc5386c22008-03-23 01:03:39 -0700295 _ASM_EXTABLE(1b, 2b)
H. Peter Anvin88976ee2008-02-04 16:47:58 +0100296 : "=r" (val), "=m" (__force_order) : "0" (0));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100297#else
298 val = native_read_cr4();
299#endif
300 return val;
301}
302
303static inline void native_write_cr4(unsigned long val)
304{
Joe Perchesc5386c22008-03-23 01:03:39 -0700305 asm volatile("mov %0,%%cr4": : "r" (val), "m" (__force_order));
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100306}
307
Glauber de Oliveira Costa94ea03c2008-01-30 13:33:19 +0100308#ifdef CONFIG_X86_64
309static inline unsigned long native_read_cr8(void)
310{
311 unsigned long cr8;
312 asm volatile("movq %%cr8,%0" : "=r" (cr8));
313 return cr8;
314}
315
316static inline void native_write_cr8(unsigned long val)
317{
318 asm volatile("movq %0,%%cr8" :: "r" (val) : "memory");
319}
320#endif
321
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100322static inline void native_wbinvd(void)
323{
324 asm volatile("wbinvd": : :"memory");
325}
Joe Perchesc5386c22008-03-23 01:03:39 -0700326
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100327#ifdef CONFIG_PARAVIRT
328#include <asm/paravirt.h>
329#else
330#define read_cr0() (native_read_cr0())
331#define write_cr0(x) (native_write_cr0(x))
332#define read_cr2() (native_read_cr2())
333#define write_cr2(x) (native_write_cr2(x))
334#define read_cr3() (native_read_cr3())
335#define write_cr3(x) (native_write_cr3(x))
336#define read_cr4() (native_read_cr4())
337#define read_cr4_safe() (native_read_cr4_safe())
338#define write_cr4(x) (native_write_cr4(x))
339#define wbinvd() (native_wbinvd())
Glauber de Oliveira Costad46d7d72008-01-30 13:31:08 +0100340#ifdef CONFIG_X86_64
Glauber de Oliveira Costa94ea03c2008-01-30 13:33:19 +0100341#define read_cr8() (native_read_cr8())
342#define write_cr8(x) (native_write_cr8(x))
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -0400343#define load_gs_index native_load_gs_index
Glauber de Oliveira Costad46d7d72008-01-30 13:31:08 +0100344#endif
345
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100346/* Clear the 'TS' bit */
347#define clts() (native_clts())
348
349#endif/* CONFIG_PARAVIRT */
350
Jeremy Fitzhardinge4e09e212008-05-26 23:31:03 +0100351#define stts() write_cr0(read_cr0() | X86_CR0_TS)
Glauber de Oliveira Costad3ca9012008-01-30 13:31:08 +0100352
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100353#endif /* __KERNEL__ */
354
H. Peter Anvin84fb1442008-02-04 16:48:00 +0100355static inline void clflush(volatile void *__p)
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100356{
H. Peter Anvin84fb1442008-02-04 16:48:00 +0100357 asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100358}
359
Joe Perchesc5386c22008-03-23 01:03:39 -0700360#define nop() asm volatile ("nop")
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100361
362void disable_hlt(void);
363void enable_hlt(void);
364
Glauber de Oliveira Costad8954222008-01-30 13:31:08 +0100365void cpu_idle_wait(void);
366
367extern unsigned long arch_align_stack(unsigned long sp);
368extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
369
370void default_idle(void);
371
Ivan Vecerad3ec5ca2008-11-11 14:33:44 +0100372void stop_this_cpu(void *dummy);
373
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100374/*
375 * Force strict CPU ordering.
376 * And yes, this is required on UP too when we're talking
377 * to devices.
378 */
379#ifdef CONFIG_X86_32
380/*
Pavel Machek0d7a1812008-03-03 12:49:09 +0100381 * Some non-Intel clones support out of order store. wmb() ceases to be a
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100382 * nop for these.
383 */
384#define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
385#define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
386#define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
387#else
388#define mb() asm volatile("mfence":::"memory")
389#define rmb() asm volatile("lfence":::"memory")
390#define wmb() asm volatile("sfence" ::: "memory")
391#endif
392
393/**
394 * read_barrier_depends - Flush all pending reads that subsequents reads
395 * depend on.
396 *
397 * No data-dependent reads from memory-like regions are ever reordered
398 * over this barrier. All reads preceding this primitive are guaranteed
399 * to access memory (but not necessarily other CPUs' caches) before any
400 * reads following this primitive that depend on the data return by
401 * any of the preceding reads. This primitive is much lighter weight than
402 * rmb() on most CPUs, and is never heavier weight than is
403 * rmb().
404 *
405 * These ordering constraints are respected by both the local CPU
406 * and the compiler.
407 *
408 * Ordering is not guaranteed by anything other than these primitives,
409 * not even by data dependencies. See the documentation for
410 * memory_barrier() for examples and URLs to more information.
411 *
412 * For example, the following code would force ordering (the initial
413 * value of "a" is zero, "b" is one, and "p" is "&a"):
414 *
415 * <programlisting>
416 * CPU 0 CPU 1
417 *
418 * b = 2;
419 * memory_barrier();
420 * p = &b; q = p;
421 * read_barrier_depends();
422 * d = *q;
423 * </programlisting>
424 *
425 * because the read of "*q" depends on the read of "p" and these
426 * two reads are separated by a read_barrier_depends(). However,
427 * the following code, with the same initial values for "a" and "b":
428 *
429 * <programlisting>
430 * CPU 0 CPU 1
431 *
432 * a = 2;
433 * memory_barrier();
434 * b = 3; y = b;
435 * read_barrier_depends();
436 * x = a;
437 * </programlisting>
438 *
439 * does not enforce ordering, since there is no data dependency between
440 * the read of "a" and the read of "b". Therefore, on some CPUs, such
441 * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
442 * in cases like this where there are no data dependencies.
443 **/
444
445#define read_barrier_depends() do { } while (0)
446
447#ifdef CONFIG_SMP
448#define smp_mb() mb()
449#ifdef CONFIG_X86_PPRO_FENCE
450# define smp_rmb() rmb()
451#else
452# define smp_rmb() barrier()
453#endif
454#ifdef CONFIG_X86_OOSTORE
455# define smp_wmb() wmb()
456#else
457# define smp_wmb() barrier()
458#endif
459#define smp_read_barrier_depends() read_barrier_depends()
Joe Perchesc5386c22008-03-23 01:03:39 -0700460#define set_mb(var, value) do { (void)xchg(&var, value); } while (0)
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100461#else
462#define smp_mb() barrier()
463#define smp_rmb() barrier()
464#define smp_wmb() barrier()
465#define smp_read_barrier_depends() do { } while (0)
466#define set_mb(var, value) do { var = value; barrier(); } while (0)
467#endif
468
Andi Kleenfde1b3f2008-01-30 13:32:38 +0100469/*
470 * Stop RDTSC speculation. This is needed when you need to use RDTSC
471 * (or get_cycles or vread that possibly accesses the TSC) in a defined
472 * code region.
473 *
474 * (Could use an alternative three way for this if there was one.)
475 */
476static inline void rdtsc_barrier(void)
477{
478 alternative(ASM_NOP3, "mfence", X86_FEATURE_MFENCE_RDTSC);
479 alternative(ASM_NOP3, "lfence", X86_FEATURE_LFENCE_RDTSC);
480}
Glauber de Oliveira Costa833d8462008-01-30 13:31:08 +0100481
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700482#endif /* _ASM_X86_SYSTEM_H */