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