H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_SYSTEM_H |
| 2 | #define _ASM_X86_SYSTEM_H |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 3 | |
| 4 | #include <asm/asm.h> |
Glauber de Oliveira Costa | d46d7d7 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 5 | #include <asm/segment.h> |
| 6 | #include <asm/cpufeature.h> |
| 7 | #include <asm/cmpxchg.h> |
Andi Kleen | fde1b3f | 2008-01-30 13:32:38 +0100 | [diff] [blame] | 8 | #include <asm/nops.h> |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 9 | |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Glauber de Oliveira Costa | d46d7d7 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 11 | #include <linux/irqflags.h> |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 12 | |
Jan Beulich | ded9aa0 | 2008-01-30 13:31:24 +0100 | [diff] [blame] | 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 | |
Glauber de Oliveira Costa | 0a3b4d1 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 20 | struct task_struct; /* one of the stranger aspects of C forward declarations */ |
Harvey Harrison | 599db4f | 2008-02-04 16:48:03 +0100 | [diff] [blame] | 21 | struct task_struct *__switch_to(struct task_struct *prev, |
| 22 | struct task_struct *next); |
Glauber de Oliveira Costa | 0a3b4d1 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 23 | |
Jaswinder Singh | aab02f0 | 2008-12-15 22:23:54 +0530 | [diff] [blame] | 24 | #ifdef CONFIG_X86_32 |
| 25 | |
Glauber de Oliveira Costa | 0a3b4d1 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 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 | */ |
Ingo Molnar | 23b55bd | 2008-03-05 10:24:37 +0100 | [diff] [blame] | 30 | #define switch_to(prev, next, last) \ |
| 31 | do { \ |
Ingo Molnar | 8b6451f | 2008-03-05 10:46:38 +0100 | [diff] [blame] | 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; \ |
Ingo Molnar | 23b55bd | 2008-03-05 10:24:37 +0100 | [diff] [blame] | 40 | \ |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 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 */ \ |
Ingo Molnar | 23b55bd | 2008-03-05 10:24:37 +0100 | [diff] [blame] | 51 | \ |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 52 | /* output parameters */ \ |
| 53 | : [prev_sp] "=m" (prev->thread.sp), \ |
| 54 | [prev_ip] "=m" (prev->thread.ip), \ |
| 55 | "=a" (last), \ |
Ingo Molnar | 23b55bd | 2008-03-05 10:24:37 +0100 | [diff] [blame] | 56 | \ |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 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), \ |
Vegard Nossum | 33f8c40 | 2008-09-14 19:03:53 +0200 | [diff] [blame] | 67 | [next] "d" (next) \ |
| 68 | \ |
| 69 | : /* reloaded segment registers */ \ |
| 70 | "memory"); \ |
Glauber de Oliveira Costa | 0a3b4d1 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 71 | } while (0) |
| 72 | |
Glauber de Oliveira Costa | d46d7d7 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 73 | /* |
| 74 | * disable hlt during certain critical i/o operations |
| 75 | */ |
| 76 | #define HAVE_DISABLE_HLT |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 77 | #else |
Glauber de Oliveira Costa | 0a3b4d1 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 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 | |
| 89 | /* Save restore flags to clear handle leaking NT */ |
| 90 | #define switch_to(prev, next, last) \ |
Jan Beulich | ded9aa0 | 2008-01-30 13:31:24 +0100 | [diff] [blame] | 91 | asm volatile(SAVE_CONTEXT \ |
Glauber de Oliveira Costa | 0a3b4d1 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 92 | "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 Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 97 | "movq "__percpu_arg([current_task])",%%rsi\n\t" \ |
Ingo Molnar | a9de18e | 2008-12-31 08:31:57 +0100 | [diff] [blame] | 98 | "movq %P[task_canary](%%rsi),%%r8\n\t" \ |
| 99 | "movq %%r8,%%gs:%P[pda_canary]\n\t" \ |
Glauber de Oliveira Costa | 0a3b4d1 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 100 | "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 Molnar | a9de18e | 2008-12-31 08:31:57 +0100 | [diff] [blame] | 111 | [task_canary] "i" (offsetof(struct task_struct, stack_canary)),\ |
Ingo Molnar | b2b062b | 2009-01-18 18:37:14 +0100 | [diff] [blame] | 112 | [current_task] "m" (per_cpu_var(current_task)), \ |
Ingo Molnar | a9de18e | 2008-12-31 08:31:57 +0100 | [diff] [blame] | 113 | [pda_canary] "i" (offsetof(struct x8664_pda, stack_canary))\ |
Glauber de Oliveira Costa | 0a3b4d1 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 114 | : "memory", "cc" __EXTRA_CLOBBER) |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 115 | #endif |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 116 | |
| 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 Fitzhardinge | 9f9d489 | 2008-06-25 00:19:32 -0400 | [diff] [blame] | 146 | extern void native_load_gs_index(unsigned); |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 147 | |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 148 | /* |
Glauber de Oliveira Costa | a6b4655 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 149 | * 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 Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 154 | "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 Fitzhardinge | d338c73 | 2008-06-25 00:18:58 -0400 | [diff] [blame] | 163 | : :"r" (value), "r" (0) : "memory") |
Glauber de Oliveira Costa | a6b4655 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 164 | |
| 165 | |
| 166 | /* |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 167 | * Save a segment register away |
| 168 | */ |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 169 | #define savesegment(seg, value) \ |
Ingo Molnar | d9fc3fd | 2008-07-11 19:41:19 +0200 | [diff] [blame] | 170 | asm("mov %%" #seg ",%0":"=r" (value) : : "memory") |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 171 | |
| 172 | static inline unsigned long get_limit(unsigned long segment) |
| 173 | { |
| 174 | unsigned long __limit; |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 175 | asm("lsll %1,%0" : "=r" (__limit) : "r" (segment)); |
| 176 | return __limit + 1; |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 177 | } |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 178 | |
| 179 | static inline void native_clts(void) |
| 180 | { |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 181 | asm volatile("clts"); |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 182 | } |
| 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 | */ |
| 191 | static unsigned long __force_order; |
| 192 | |
| 193 | static inline unsigned long native_read_cr0(void) |
| 194 | { |
| 195 | unsigned long val; |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 196 | asm volatile("mov %%cr0,%0\n\t" : "=r" (val), "=m" (__force_order)); |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 197 | return val; |
| 198 | } |
| 199 | |
| 200 | static inline void native_write_cr0(unsigned long val) |
| 201 | { |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 202 | asm volatile("mov %0,%%cr0": : "r" (val), "m" (__force_order)); |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static inline unsigned long native_read_cr2(void) |
| 206 | { |
| 207 | unsigned long val; |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 208 | asm volatile("mov %%cr2,%0\n\t" : "=r" (val), "=m" (__force_order)); |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 209 | return val; |
| 210 | } |
| 211 | |
| 212 | static inline void native_write_cr2(unsigned long val) |
| 213 | { |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 214 | asm volatile("mov %0,%%cr2": : "r" (val), "m" (__force_order)); |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static inline unsigned long native_read_cr3(void) |
| 218 | { |
| 219 | unsigned long val; |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 220 | asm volatile("mov %%cr3,%0\n\t" : "=r" (val), "=m" (__force_order)); |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 221 | return val; |
| 222 | } |
| 223 | |
| 224 | static inline void native_write_cr3(unsigned long val) |
| 225 | { |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 226 | asm volatile("mov %0,%%cr3": : "r" (val), "m" (__force_order)); |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static inline unsigned long native_read_cr4(void) |
| 230 | { |
| 231 | unsigned long val; |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 232 | asm volatile("mov %%cr4,%0\n\t" : "=r" (val), "=m" (__force_order)); |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 233 | return val; |
| 234 | } |
| 235 | |
| 236 | static 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 Anvin | 88976ee | 2008-02-04 16:47:58 +0100 | [diff] [blame] | 242 | asm volatile("1: mov %%cr4, %0\n" |
| 243 | "2:\n" |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 244 | _ASM_EXTABLE(1b, 2b) |
H. Peter Anvin | 88976ee | 2008-02-04 16:47:58 +0100 | [diff] [blame] | 245 | : "=r" (val), "=m" (__force_order) : "0" (0)); |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 246 | #else |
| 247 | val = native_read_cr4(); |
| 248 | #endif |
| 249 | return val; |
| 250 | } |
| 251 | |
| 252 | static inline void native_write_cr4(unsigned long val) |
| 253 | { |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 254 | asm volatile("mov %0,%%cr4": : "r" (val), "m" (__force_order)); |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Glauber de Oliveira Costa | 94ea03c | 2008-01-30 13:33:19 +0100 | [diff] [blame] | 257 | #ifdef CONFIG_X86_64 |
| 258 | static 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 | |
| 265 | static 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 Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 271 | static inline void native_wbinvd(void) |
| 272 | { |
| 273 | asm volatile("wbinvd": : :"memory"); |
| 274 | } |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 275 | |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 276 | #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 Costa | d46d7d7 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 289 | #ifdef CONFIG_X86_64 |
Glauber de Oliveira Costa | 94ea03c | 2008-01-30 13:33:19 +0100 | [diff] [blame] | 290 | #define read_cr8() (native_read_cr8()) |
| 291 | #define write_cr8(x) (native_write_cr8(x)) |
Jeremy Fitzhardinge | 9f9d489 | 2008-06-25 00:19:32 -0400 | [diff] [blame] | 292 | #define load_gs_index native_load_gs_index |
Glauber de Oliveira Costa | d46d7d7 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 293 | #endif |
| 294 | |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 295 | /* Clear the 'TS' bit */ |
| 296 | #define clts() (native_clts()) |
| 297 | |
| 298 | #endif/* CONFIG_PARAVIRT */ |
| 299 | |
Jeremy Fitzhardinge | 4e09e21 | 2008-05-26 23:31:03 +0100 | [diff] [blame] | 300 | #define stts() write_cr0(read_cr0() | X86_CR0_TS) |
Glauber de Oliveira Costa | d3ca901 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 301 | |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 302 | #endif /* __KERNEL__ */ |
| 303 | |
H. Peter Anvin | 84fb144 | 2008-02-04 16:48:00 +0100 | [diff] [blame] | 304 | static inline void clflush(volatile void *__p) |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 305 | { |
H. Peter Anvin | 84fb144 | 2008-02-04 16:48:00 +0100 | [diff] [blame] | 306 | asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p)); |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Joe Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 309 | #define nop() asm volatile ("nop") |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 310 | |
| 311 | void disable_hlt(void); |
| 312 | void enable_hlt(void); |
| 313 | |
Glauber de Oliveira Costa | d895422 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 314 | void cpu_idle_wait(void); |
| 315 | |
| 316 | extern unsigned long arch_align_stack(unsigned long sp); |
| 317 | extern void free_init_pages(char *what, unsigned long begin, unsigned long end); |
| 318 | |
| 319 | void default_idle(void); |
| 320 | |
Ivan Vecera | d3ec5ca | 2008-11-11 14:33:44 +0100 | [diff] [blame] | 321 | void stop_this_cpu(void *dummy); |
| 322 | |
Glauber de Oliveira Costa | 833d846 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 323 | /* |
| 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 Machek | 0d7a181 | 2008-03-03 12:49:09 +0100 | [diff] [blame] | 330 | * Some non-Intel clones support out of order store. wmb() ceases to be a |
Glauber de Oliveira Costa | 833d846 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 331 | * 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 Perches | c5386c2 | 2008-03-23 01:03:39 -0700 | [diff] [blame] | 409 | #define set_mb(var, value) do { (void)xchg(&var, value); } while (0) |
Glauber de Oliveira Costa | 833d846 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 410 | #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 Kleen | fde1b3f | 2008-01-30 13:32:38 +0100 | [diff] [blame] | 418 | /* |
| 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 | */ |
| 425 | static 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 Costa | 833d846 | 2008-01-30 13:31:08 +0100 | [diff] [blame] | 430 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 431 | #endif /* _ASM_X86_SYSTEM_H */ |