33b0017156a7fae07db3f80a62b3b14afd4e95ec
[sfrench/cifs-2.6.git] / include / asm-x86 / system.h
1 #ifndef _ASM_X86_SYSTEM_H_
2 #define _ASM_X86_SYSTEM_H_
3
4 #include <asm/asm.h>
5 #include <asm/segment.h>
6 #include <asm/cpufeature.h>
7 #include <asm/cmpxchg.h>
8 #include <asm/nops.h>
9
10 #include <linux/kernel.h>
11 #include <linux/irqflags.h>
12
13 /* 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
20 #ifdef CONFIG_X86_32
21
22 struct task_struct; /* one of the stranger aspects of C forward declarations */
23 struct task_struct *__switch_to(struct task_struct *prev,
24                                 struct task_struct *next);
25
26 /*
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  */
30 #define switch_to(prev, next, last)                                     \
31 do {                                                                    \
32         /*                                                              \
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;                          \
40                                                                         \
41         asm volatile(                                                   \
42                 "pushfl                 \n\t"   /* save    flags */     \
43                 "pushl %%ebp            \n\t"   /* save    EBP   */     \
44                 "movl %%esp,%[prev_sp]  \n\t"   /* save    ESP   */     \
45                 "movl %[next_sp],%%esp  \n\t"   /* restore ESP   */     \
46                 "movl $1f,%[prev_ip]    \n\t"   /* save    EIP   */     \
47                 "pushl %[next_ip]       \n\t"   /* restore EIP   */     \
48                 "jmp __switch_to        \n"     /* regparm call  */     \
49                 "1:                     \t"                             \
50                 "popl %%ebp             \n\t"   /* restore EBP   */     \
51                 "popfl                  \n"     /* restore flags */     \
52                                                                         \
53                   /* output parameters */                               \
54                 : [prev_sp] "=m" (prev->thread.sp),                     \
55                   [prev_ip] "=m" (prev->thread.ip),                     \
56                             "=a" (last),                                \
57                                                                         \
58                   /* clobbered output registers: */                     \
59                   "=b" (ebx), "=c" (ecx), "=d" (edx),                   \
60                   "=S" (esi), "=D" (edi)                                \
61                                                                         \
62                   /* input parameters: */                               \
63                 : [next_sp]  "m" (next->thread.sp),                     \
64                   [next_ip]  "m" (next->thread.ip),                     \
65                                                                         \
66                   /* regparm parameters for __switch_to(): */           \
67                   [prev]     "a" (prev),                                \
68                   [next]     "d" (next)                                 \
69         );                                                              \
70 } while (0)
71
72 /*
73  * disable hlt during certain critical i/o operations
74  */
75 #define HAVE_DISABLE_HLT
76 #else
77 #define __SAVE(reg, offset) "movq %%" #reg ",(14-" #offset ")*8(%%rsp)\n\t"
78 #define __RESTORE(reg, offset) "movq (14-" #offset ")*8(%%rsp),%%" #reg "\n\t"
79
80 /* frame pointer must be last for get_wchan */
81 #define SAVE_CONTEXT    "pushf ; pushq %%rbp ; movq %%rsi,%%rbp\n\t"
82 #define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp ; popf\t"
83
84 #define __EXTRA_CLOBBER  \
85         , "rcx", "rbx", "rdx", "r8", "r9", "r10", "r11", \
86           "r12", "r13", "r14", "r15"
87
88 /* Save restore flags to clear handle leaking NT */
89 #define switch_to(prev, next, last) \
90         asm volatile(SAVE_CONTEXT                                                   \
91              "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */       \
92              "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */    \
93              "call __switch_to\n\t"                                       \
94              ".globl thread_return\n"                                     \
95              "thread_return:\n\t"                                         \
96              "movq %%gs:%P[pda_pcurrent],%%rsi\n\t"                       \
97              "movq %P[thread_info](%%rsi),%%r8\n\t"                       \
98              LOCK_PREFIX "btr  %[tif_fork],%P[ti_flags](%%r8)\n\t"        \
99              "movq %%rax,%%rdi\n\t"                                       \
100              "jc   ret_from_fork\n\t"                                     \
101              RESTORE_CONTEXT                                              \
102              : "=a" (last)                                                \
103              : [next] "S" (next), [prev] "D" (prev),                      \
104                [threadrsp] "i" (offsetof(struct task_struct, thread.sp)), \
105                [ti_flags] "i" (offsetof(struct thread_info, flags)),      \
106                [tif_fork] "i" (TIF_FORK),                                 \
107                [thread_info] "i" (offsetof(struct task_struct, stack)),   \
108                [pda_pcurrent] "i" (offsetof(struct x8664_pda, pcurrent))  \
109              : "memory", "cc" __EXTRA_CLOBBER)
110 #endif
111
112 #ifdef __KERNEL__
113 #define _set_base(addr, base) do { unsigned long __pr; \
114 __asm__ __volatile__ ("movw %%dx,%1\n\t" \
115         "rorl $16,%%edx\n\t" \
116         "movb %%dl,%2\n\t" \
117         "movb %%dh,%3" \
118         :"=&d" (__pr) \
119         :"m" (*((addr)+2)), \
120          "m" (*((addr)+4)), \
121          "m" (*((addr)+7)), \
122          "0" (base) \
123         ); } while (0)
124
125 #define _set_limit(addr, limit) do { unsigned long __lr; \
126 __asm__ __volatile__ ("movw %%dx,%1\n\t" \
127         "rorl $16,%%edx\n\t" \
128         "movb %2,%%dh\n\t" \
129         "andb $0xf0,%%dh\n\t" \
130         "orb %%dh,%%dl\n\t" \
131         "movb %%dl,%2" \
132         :"=&d" (__lr) \
133         :"m" (*(addr)), \
134          "m" (*((addr)+6)), \
135          "0" (limit) \
136         ); } while (0)
137
138 #define set_base(ldt, base) _set_base(((char *)&(ldt)) , (base))
139 #define set_limit(ldt, limit) _set_limit(((char *)&(ldt)) , ((limit)-1))
140
141 extern void load_gs_index(unsigned);
142
143 /*
144  * Load a segment. Fall back on loading the zero
145  * segment if something goes wrong..
146  */
147 #define loadsegment(seg, value)                 \
148         asm volatile("\n"                       \
149                 "1:\t"                          \
150                 "movl %k0,%%" #seg "\n"         \
151                 "2:\n"                          \
152                 ".section .fixup,\"ax\"\n"      \
153                 "3:\t"                          \
154                 "movl %k1, %%" #seg "\n\t"      \
155                 "jmp 2b\n"                      \
156                 ".previous\n"                   \
157                 _ASM_EXTABLE(1b,3b)             \
158                 : :"r" (value), "r" (0))
159
160
161 /*
162  * Save a segment register away
163  */
164 #define savesegment(seg, value) \
165         asm volatile("mov %%" #seg ",%0":"=rm" (value))
166
167 static inline unsigned long get_limit(unsigned long segment)
168 {
169         unsigned long __limit;
170         __asm__("lsll %1,%0"
171                 :"=r" (__limit):"r" (segment));
172         return __limit+1;
173 }
174
175 static inline void native_clts(void)
176 {
177         asm volatile ("clts");
178 }
179
180 /*
181  * Volatile isn't enough to prevent the compiler from reordering the
182  * read/write functions for the control registers and messing everything up.
183  * A memory clobber would solve the problem, but would prevent reordering of
184  * all loads stores around it, which can hurt performance. Solution is to
185  * use a variable and mimic reads and writes to it to enforce serialization
186  */
187 static unsigned long __force_order;
188
189 static inline unsigned long native_read_cr0(void)
190 {
191         unsigned long val;
192         asm volatile("mov %%cr0,%0\n\t" :"=r" (val), "=m" (__force_order));
193         return val;
194 }
195
196 static inline void native_write_cr0(unsigned long val)
197 {
198         asm volatile("mov %0,%%cr0": :"r" (val), "m" (__force_order));
199 }
200
201 static inline unsigned long native_read_cr2(void)
202 {
203         unsigned long val;
204         asm volatile("mov %%cr2,%0\n\t" :"=r" (val), "=m" (__force_order));
205         return val;
206 }
207
208 static inline void native_write_cr2(unsigned long val)
209 {
210         asm volatile("mov %0,%%cr2": :"r" (val), "m" (__force_order));
211 }
212
213 static inline unsigned long native_read_cr3(void)
214 {
215         unsigned long val;
216         asm volatile("mov %%cr3,%0\n\t" :"=r" (val), "=m" (__force_order));
217         return val;
218 }
219
220 static inline void native_write_cr3(unsigned long val)
221 {
222         asm volatile("mov %0,%%cr3": :"r" (val), "m" (__force_order));
223 }
224
225 static inline unsigned long native_read_cr4(void)
226 {
227         unsigned long val;
228         asm volatile("mov %%cr4,%0\n\t" :"=r" (val), "=m" (__force_order));
229         return val;
230 }
231
232 static inline unsigned long native_read_cr4_safe(void)
233 {
234         unsigned long val;
235         /* This could fault if %cr4 does not exist. In x86_64, a cr4 always
236          * exists, so it will never fail. */
237 #ifdef CONFIG_X86_32
238         asm volatile("1: mov %%cr4, %0\n"
239                      "2:\n"
240                      _ASM_EXTABLE(1b,2b)
241                      : "=r" (val), "=m" (__force_order) : "0" (0));
242 #else
243         val = native_read_cr4();
244 #endif
245         return val;
246 }
247
248 static inline void native_write_cr4(unsigned long val)
249 {
250         asm volatile("mov %0,%%cr4": :"r" (val), "m" (__force_order));
251 }
252
253 #ifdef CONFIG_X86_64
254 static inline unsigned long native_read_cr8(void)
255 {
256         unsigned long cr8;
257         asm volatile("movq %%cr8,%0" : "=r" (cr8));
258         return cr8;
259 }
260
261 static inline void native_write_cr8(unsigned long val)
262 {
263         asm volatile("movq %0,%%cr8" :: "r" (val) : "memory");
264 }
265 #endif
266
267 static inline void native_wbinvd(void)
268 {
269         asm volatile("wbinvd": : :"memory");
270 }
271 #ifdef CONFIG_PARAVIRT
272 #include <asm/paravirt.h>
273 #else
274 #define read_cr0()      (native_read_cr0())
275 #define write_cr0(x)    (native_write_cr0(x))
276 #define read_cr2()      (native_read_cr2())
277 #define write_cr2(x)    (native_write_cr2(x))
278 #define read_cr3()      (native_read_cr3())
279 #define write_cr3(x)    (native_write_cr3(x))
280 #define read_cr4()      (native_read_cr4())
281 #define read_cr4_safe() (native_read_cr4_safe())
282 #define write_cr4(x)    (native_write_cr4(x))
283 #define wbinvd()        (native_wbinvd())
284 #ifdef CONFIG_X86_64
285 #define read_cr8()      (native_read_cr8())
286 #define write_cr8(x)    (native_write_cr8(x))
287 #endif
288
289 /* Clear the 'TS' bit */
290 #define clts()          (native_clts())
291
292 #endif/* CONFIG_PARAVIRT */
293
294 #define stts() write_cr0(8 | read_cr0())
295
296 #endif /* __KERNEL__ */
297
298 static inline void clflush(volatile void *__p)
299 {
300         asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
301 }
302
303 #define nop() __asm__ __volatile__ ("nop")
304
305 void disable_hlt(void);
306 void enable_hlt(void);
307
308 extern int es7000_plat;
309 void cpu_idle_wait(void);
310
311 extern unsigned long arch_align_stack(unsigned long sp);
312 extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
313
314 void default_idle(void);
315
316 /*
317  * Force strict CPU ordering.
318  * And yes, this is required on UP too when we're talking
319  * to devices.
320  */
321 #ifdef CONFIG_X86_32
322 /*
323  * Some non-Intel clones support out of order store. wmb() ceases to be a
324  * nop for these.
325  */
326 #define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
327 #define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
328 #define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
329 #else
330 #define mb()    asm volatile("mfence":::"memory")
331 #define rmb()   asm volatile("lfence":::"memory")
332 #define wmb()   asm volatile("sfence" ::: "memory")
333 #endif
334
335 /**
336  * read_barrier_depends - Flush all pending reads that subsequents reads
337  * depend on.
338  *
339  * No data-dependent reads from memory-like regions are ever reordered
340  * over this barrier.  All reads preceding this primitive are guaranteed
341  * to access memory (but not necessarily other CPUs' caches) before any
342  * reads following this primitive that depend on the data return by
343  * any of the preceding reads.  This primitive is much lighter weight than
344  * rmb() on most CPUs, and is never heavier weight than is
345  * rmb().
346  *
347  * These ordering constraints are respected by both the local CPU
348  * and the compiler.
349  *
350  * Ordering is not guaranteed by anything other than these primitives,
351  * not even by data dependencies.  See the documentation for
352  * memory_barrier() for examples and URLs to more information.
353  *
354  * For example, the following code would force ordering (the initial
355  * value of "a" is zero, "b" is one, and "p" is "&a"):
356  *
357  * <programlisting>
358  *      CPU 0                           CPU 1
359  *
360  *      b = 2;
361  *      memory_barrier();
362  *      p = &b;                         q = p;
363  *                                      read_barrier_depends();
364  *                                      d = *q;
365  * </programlisting>
366  *
367  * because the read of "*q" depends on the read of "p" and these
368  * two reads are separated by a read_barrier_depends().  However,
369  * the following code, with the same initial values for "a" and "b":
370  *
371  * <programlisting>
372  *      CPU 0                           CPU 1
373  *
374  *      a = 2;
375  *      memory_barrier();
376  *      b = 3;                          y = b;
377  *                                      read_barrier_depends();
378  *                                      x = a;
379  * </programlisting>
380  *
381  * does not enforce ordering, since there is no data dependency between
382  * the read of "a" and the read of "b".  Therefore, on some CPUs, such
383  * as Alpha, "y" could be set to 3 and "x" to 0.  Use rmb()
384  * in cases like this where there are no data dependencies.
385  **/
386
387 #define read_barrier_depends()  do { } while (0)
388
389 #ifdef CONFIG_SMP
390 #define smp_mb()        mb()
391 #ifdef CONFIG_X86_PPRO_FENCE
392 # define smp_rmb()      rmb()
393 #else
394 # define smp_rmb()      barrier()
395 #endif
396 #ifdef CONFIG_X86_OOSTORE
397 # define smp_wmb()      wmb()
398 #else
399 # define smp_wmb()      barrier()
400 #endif
401 #define smp_read_barrier_depends()      read_barrier_depends()
402 #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
403 #else
404 #define smp_mb()        barrier()
405 #define smp_rmb()       barrier()
406 #define smp_wmb()       barrier()
407 #define smp_read_barrier_depends()      do { } while (0)
408 #define set_mb(var, value) do { var = value; barrier(); } while (0)
409 #endif
410
411 /*
412  * Stop RDTSC speculation. This is needed when you need to use RDTSC
413  * (or get_cycles or vread that possibly accesses the TSC) in a defined
414  * code region.
415  *
416  * (Could use an alternative three way for this if there was one.)
417  */
418 static inline void rdtsc_barrier(void)
419 {
420         alternative(ASM_NOP3, "mfence", X86_FEATURE_MFENCE_RDTSC);
421         alternative(ASM_NOP3, "lfence", X86_FEATURE_LFENCE_RDTSC);
422 }
423
424 #endif