system.h 14.0 KB
Newer Older
H
H. Peter Anvin 已提交
1 2
#ifndef _ASM_X86_SYSTEM_H
#define _ASM_X86_SYSTEM_H
3 4

#include <asm/asm.h>
G
Glauber de Oliveira Costa 已提交
5 6 7
#include <asm/segment.h>
#include <asm/cpufeature.h>
#include <asm/cmpxchg.h>
A
Andi Kleen 已提交
8
#include <asm/nops.h>
9

10
#include <linux/kernel.h>
G
Glauber de Oliveira Costa 已提交
11
#include <linux/irqflags.h>
12

13 14 15 16 17 18 19
/* entries in ARCH_DLINFO: */
#ifdef CONFIG_IA32_EMULATION
# define AT_VECTOR_SIZE_ARCH 2
#else
# define AT_VECTOR_SIZE_ARCH 1
#endif

20
struct task_struct; /* one of the stranger aspects of C forward declarations */
21 22
struct task_struct *__switch_to(struct task_struct *prev,
				struct task_struct *next);
23

24 25
#ifdef CONFIG_X86_32

26 27
#ifdef CONFIG_CC_STACKPROTECTOR
#define __switch_canary							\
T
Tejun Heo 已提交
28 29
	"movl %P[task_canary](%[next]), %%ebx\n\t"			\
	"movl %%ebx, "__percpu_arg([stack_canary])"\n\t"
30 31 32 33 34 35 36 37 38 39
#define __switch_canary_oparam						\
	, [stack_canary] "=m" (per_cpu_var(stack_canary))
#define __switch_canary_iparam						\
	, [task_canary] "i" (offsetof(struct task_struct, stack_canary))
#else	/* CC_STACKPROTECTOR */
#define __switch_canary
#define __switch_canary_oparam
#define __switch_canary_iparam
#endif	/* CC_STACKPROTECTOR */

40 41 42 43
/*
 * Saving eflags is important. It switches not only IOPL between tasks,
 * it also protects other tasks from NT leaking through sysenter etc.
 */
I
Ingo Molnar 已提交
44 45
#define switch_to(prev, next, last)					\
do {									\
I
Ingo Molnar 已提交
46 47 48 49 50 51 52 53
	/*								\
	 * Context-switching clobbers all registers, so we clobber	\
	 * them explicitly, via unused output variables.		\
	 * (EAX and EBP is not listed because EBP is saved/restored	\
	 * explicitly for wchan access and EAX is the return value of	\
	 * __switch_to())						\
	 */								\
	unsigned long ebx, ecx, edx, esi, edi;				\
I
Ingo Molnar 已提交
54
									\
55 56 57 58 59 60
	asm volatile("pushfl\n\t"		/* save    flags */	\
		     "pushl %%ebp\n\t"		/* save    EBP   */	\
		     "movl %%esp,%[prev_sp]\n\t"	/* save    ESP   */ \
		     "movl %[next_sp],%%esp\n\t"	/* restore ESP   */ \
		     "movl $1f,%[prev_ip]\n\t"	/* save    EIP   */	\
		     "pushl %[next_ip]\n\t"	/* restore EIP   */	\
T
Tejun Heo 已提交
61
		     __switch_canary					\
62 63 64 65
		     "jmp __switch_to\n"	/* regparm call  */	\
		     "1:\t"						\
		     "popl %%ebp\n\t"		/* restore EBP   */	\
		     "popfl\n"			/* restore flags */	\
I
Ingo Molnar 已提交
66
									\
67 68 69 70
		     /* output parameters */				\
		     : [prev_sp] "=m" (prev->thread.sp),		\
		       [prev_ip] "=m" (prev->thread.ip),		\
		       "=a" (last),					\
I
Ingo Molnar 已提交
71
									\
72 73 74 75
		       /* clobbered output registers: */		\
		       "=b" (ebx), "=c" (ecx), "=d" (edx),		\
		       "=S" (esi), "=D" (edi)				\
		       							\
76 77
		       __switch_canary_oparam				\
									\
78 79 80 81 82 83
		       /* input parameters: */				\
		     : [next_sp]  "m" (next->thread.sp),		\
		       [next_ip]  "m" (next->thread.ip),		\
		       							\
		       /* regparm parameters for __switch_to(): */	\
		       [prev]     "a" (prev),				\
84 85
		       [next]     "d" (next)				\
									\
86 87
		       __switch_canary_iparam				\
									\
88 89
		     : /* reloaded segment registers */			\
			"memory");					\
90 91
} while (0)

G
Glauber de Oliveira Costa 已提交
92 93 94 95
/*
 * disable hlt during certain critical i/o operations
 */
#define HAVE_DISABLE_HLT
96
#else
97 98 99 100 101 102 103 104 105 106 107
#define __SAVE(reg, offset) "movq %%" #reg ",(14-" #offset ")*8(%%rsp)\n\t"
#define __RESTORE(reg, offset) "movq (14-" #offset ")*8(%%rsp),%%" #reg "\n\t"

/* frame pointer must be last for get_wchan */
#define SAVE_CONTEXT    "pushf ; pushq %%rbp ; movq %%rsi,%%rbp\n\t"
#define RESTORE_CONTEXT "movq %%rbp,%%rsi ; popq %%rbp ; popf\t"

#define __EXTRA_CLOBBER  \
	, "rcx", "rbx", "rdx", "r8", "r9", "r10", "r11", \
	  "r12", "r13", "r14", "r15"

108 109 110
#ifdef CONFIG_CC_STACKPROTECTOR
#define __switch_canary							  \
	"movq %P[task_canary](%%rsi),%%r8\n\t"				  \
111 112 113 114 115
	"movq %%r8,"__percpu_arg([gs_canary])"\n\t"
#define __switch_canary_oparam						  \
	, [gs_canary] "=m" (per_cpu_var(irq_stack_union.stack_canary))
#define __switch_canary_iparam						  \
	, [task_canary] "i" (offsetof(struct task_struct, stack_canary))
116 117
#else	/* CC_STACKPROTECTOR */
#define __switch_canary
118 119
#define __switch_canary_oparam
#define __switch_canary_iparam
120 121
#endif	/* CC_STACKPROTECTOR */

122 123
/* Save restore flags to clear handle leaking NT */
#define switch_to(prev, next, last) \
124
	asm volatile(SAVE_CONTEXT					  \
125 126 127 128 129
	     "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */	  \
	     "movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */	  \
	     "call __switch_to\n\t"					  \
	     ".globl thread_return\n"					  \
	     "thread_return:\n\t"					  \
130
	     "movq "__percpu_arg([current_task])",%%rsi\n\t"		  \
131
	     __switch_canary						  \
132 133
	     "movq %P[thread_info](%%rsi),%%r8\n\t"			  \
	     "movq %%rax,%%rdi\n\t" 					  \
134 135
	     "testl  %[_tif_fork],%P[ti_flags](%%r8)\n\t"	  \
	     "jnz   ret_from_fork\n\t"					  \
136 137
	     RESTORE_CONTEXT						  \
	     : "=a" (last)					  	  \
138
	       __switch_canary_oparam					  \
139 140 141
	     : [next] "S" (next), [prev] "D" (prev),			  \
	       [threadrsp] "i" (offsetof(struct task_struct, thread.sp)), \
	       [ti_flags] "i" (offsetof(struct thread_info, flags)),	  \
142
	       [_tif_fork] "i" (_TIF_FORK),			  	  \
143
	       [thread_info] "i" (offsetof(struct task_struct, stack)),   \
144
	       [current_task] "m" (per_cpu_var(current_task))		  \
145
	       __switch_canary_iparam					  \
146
	     : "memory", "cc" __EXTRA_CLOBBER)
147
#endif
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

#ifdef __KERNEL__
#define _set_base(addr, base) do { unsigned long __pr; \
__asm__ __volatile__ ("movw %%dx,%1\n\t" \
	"rorl $16,%%edx\n\t" \
	"movb %%dl,%2\n\t" \
	"movb %%dh,%3" \
	:"=&d" (__pr) \
	:"m" (*((addr)+2)), \
	 "m" (*((addr)+4)), \
	 "m" (*((addr)+7)), \
	 "0" (base) \
	); } while (0)

#define _set_limit(addr, limit) do { unsigned long __lr; \
__asm__ __volatile__ ("movw %%dx,%1\n\t" \
	"rorl $16,%%edx\n\t" \
	"movb %2,%%dh\n\t" \
	"andb $0xf0,%%dh\n\t" \
	"orb %%dh,%%dl\n\t" \
	"movb %%dl,%2" \
	:"=&d" (__lr) \
	:"m" (*(addr)), \
	 "m" (*((addr)+6)), \
	 "0" (limit) \
	); } while (0)

#define set_base(ldt, base) _set_base(((char *)&(ldt)) , (base))
#define set_limit(ldt, limit) _set_limit(((char *)&(ldt)) , ((limit)-1))

178
extern void native_load_gs_index(unsigned);
179

180 181 182 183 184 185
/*
 * Load a segment. Fall back on loading the zero
 * segment if something goes wrong..
 */
#define loadsegment(seg, value)			\
	asm volatile("\n"			\
186 187 188 189 190 191 192 193 194
		     "1:\t"			\
		     "movl %k0,%%" #seg "\n"	\
		     "2:\n"			\
		     ".section .fixup,\"ax\"\n"	\
		     "3:\t"			\
		     "movl %k1, %%" #seg "\n\t"	\
		     "jmp 2b\n"			\
		     ".previous\n"		\
		     _ASM_EXTABLE(1b,3b)	\
195
		     : :"r" (value), "r" (0) : "memory")
196 197


198 199 200
/*
 * Save a segment register away
 */
201
#define savesegment(seg, value)				\
202
	asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
203

T
Tejun Heo 已提交
204 205 206 207
/*
 * x86_32 user gs accessors.
 */
#ifdef CONFIG_X86_32
208
#ifdef CONFIG_X86_32_LAZY_GS
T
Tejun Heo 已提交
209 210 211
#define get_user_gs(regs)	(u16)({unsigned long v; savesegment(gs, v); v;})
#define set_user_gs(regs, v)	loadsegment(gs, (unsigned long)(v))
#define task_user_gs(tsk)	((tsk)->thread.gs)
212 213 214 215 216 217 218 219 220 221
#define lazy_save_gs(v)		savesegment(gs, (v))
#define lazy_load_gs(v)		loadsegment(gs, (v))
#else	/* X86_32_LAZY_GS */
#define get_user_gs(regs)	(u16)((regs)->gs)
#define set_user_gs(regs, v)	do { (regs)->gs = (v); } while (0)
#define task_user_gs(tsk)	(task_pt_regs(tsk)->gs)
#define lazy_save_gs(v)		do { } while (0)
#define lazy_load_gs(v)		do { } while (0)
#endif	/* X86_32_LAZY_GS */
#endif	/* X86_32 */
T
Tejun Heo 已提交
222

223 224 225
static inline unsigned long get_limit(unsigned long segment)
{
	unsigned long __limit;
226 227
	asm("lsll %1,%0" : "=r" (__limit) : "r" (segment));
	return __limit + 1;
228
}
229 230 231

static inline void native_clts(void)
{
232
	asm volatile("clts");
233 234 235 236 237 238 239 240 241 242 243 244 245 246
}

/*
 * Volatile isn't enough to prevent the compiler from reordering the
 * read/write functions for the control registers and messing everything up.
 * A memory clobber would solve the problem, but would prevent reordering of
 * all loads stores around it, which can hurt performance. Solution is to
 * use a variable and mimic reads and writes to it to enforce serialization
 */
static unsigned long __force_order;

static inline unsigned long native_read_cr0(void)
{
	unsigned long val;
247
	asm volatile("mov %%cr0,%0\n\t" : "=r" (val), "=m" (__force_order));
248 249 250 251 252
	return val;
}

static inline void native_write_cr0(unsigned long val)
{
253
	asm volatile("mov %0,%%cr0": : "r" (val), "m" (__force_order));
254 255 256 257 258
}

static inline unsigned long native_read_cr2(void)
{
	unsigned long val;
259
	asm volatile("mov %%cr2,%0\n\t" : "=r" (val), "=m" (__force_order));
260 261 262 263 264
	return val;
}

static inline void native_write_cr2(unsigned long val)
{
265
	asm volatile("mov %0,%%cr2": : "r" (val), "m" (__force_order));
266 267 268 269 270
}

static inline unsigned long native_read_cr3(void)
{
	unsigned long val;
271
	asm volatile("mov %%cr3,%0\n\t" : "=r" (val), "=m" (__force_order));
272 273 274 275 276
	return val;
}

static inline void native_write_cr3(unsigned long val)
{
277
	asm volatile("mov %0,%%cr3": : "r" (val), "m" (__force_order));
278 279 280 281 282
}

static inline unsigned long native_read_cr4(void)
{
	unsigned long val;
283
	asm volatile("mov %%cr4,%0\n\t" : "=r" (val), "=m" (__force_order));
284 285 286 287 288 289 290 291 292
	return val;
}

static inline unsigned long native_read_cr4_safe(void)
{
	unsigned long val;
	/* This could fault if %cr4 does not exist. In x86_64, a cr4 always
	 * exists, so it will never fail. */
#ifdef CONFIG_X86_32
293 294
	asm volatile("1: mov %%cr4, %0\n"
		     "2:\n"
295
		     _ASM_EXTABLE(1b, 2b)
296
		     : "=r" (val), "=m" (__force_order) : "0" (0));
297 298 299 300 301 302 303 304
#else
	val = native_read_cr4();
#endif
	return val;
}

static inline void native_write_cr4(unsigned long val)
{
305
	asm volatile("mov %0,%%cr4": : "r" (val), "m" (__force_order));
306 307
}

308 309 310 311 312 313 314 315 316 317 318 319 320 321
#ifdef CONFIG_X86_64
static inline unsigned long native_read_cr8(void)
{
	unsigned long cr8;
	asm volatile("movq %%cr8,%0" : "=r" (cr8));
	return cr8;
}

static inline void native_write_cr8(unsigned long val)
{
	asm volatile("movq %0,%%cr8" :: "r" (val) : "memory");
}
#endif

322 323 324 325
static inline void native_wbinvd(void)
{
	asm volatile("wbinvd": : :"memory");
}
326

327 328 329 330 331 332 333 334 335 336 337 338 339
#ifdef CONFIG_PARAVIRT
#include <asm/paravirt.h>
#else
#define read_cr0()	(native_read_cr0())
#define write_cr0(x)	(native_write_cr0(x))
#define read_cr2()	(native_read_cr2())
#define write_cr2(x)	(native_write_cr2(x))
#define read_cr3()	(native_read_cr3())
#define write_cr3(x)	(native_write_cr3(x))
#define read_cr4()	(native_read_cr4())
#define read_cr4_safe()	(native_read_cr4_safe())
#define write_cr4(x)	(native_write_cr4(x))
#define wbinvd()	(native_wbinvd())
G
Glauber de Oliveira Costa 已提交
340
#ifdef CONFIG_X86_64
341 342
#define read_cr8()	(native_read_cr8())
#define write_cr8(x)	(native_write_cr8(x))
343
#define load_gs_index   native_load_gs_index
G
Glauber de Oliveira Costa 已提交
344 345
#endif

346 347 348 349 350
/* Clear the 'TS' bit */
#define clts()		(native_clts())

#endif/* CONFIG_PARAVIRT */

351
#define stts() write_cr0(read_cr0() | X86_CR0_TS)
352

353 354
#endif /* __KERNEL__ */

355
static inline void clflush(volatile void *__p)
356
{
357
	asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
358 359
}

360
#define nop() asm volatile ("nop")
361 362 363 364 365 366 367 368 369 370 371

void disable_hlt(void);
void enable_hlt(void);

void cpu_idle_wait(void);

extern unsigned long arch_align_stack(unsigned long sp);
extern void free_init_pages(char *what, unsigned long begin, unsigned long end);

void default_idle(void);

372 373
void stop_this_cpu(void *dummy);

374 375 376 377 378 379 380
/*
 * Force strict CPU ordering.
 * And yes, this is required on UP too when we're talking
 * to devices.
 */
#ifdef CONFIG_X86_32
/*
P
Pavel Machek 已提交
381
 * Some non-Intel clones support out of order store. wmb() ceases to be a
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
 * nop for these.
 */
#define mb() alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2)
#define rmb() alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2)
#define wmb() alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM)
#else
#define mb() 	asm volatile("mfence":::"memory")
#define rmb()	asm volatile("lfence":::"memory")
#define wmb()	asm volatile("sfence" ::: "memory")
#endif

/**
 * read_barrier_depends - Flush all pending reads that subsequents reads
 * depend on.
 *
 * No data-dependent reads from memory-like regions are ever reordered
 * over this barrier.  All reads preceding this primitive are guaranteed
 * to access memory (but not necessarily other CPUs' caches) before any
 * reads following this primitive that depend on the data return by
 * any of the preceding reads.  This primitive is much lighter weight than
 * rmb() on most CPUs, and is never heavier weight than is
 * rmb().
 *
 * These ordering constraints are respected by both the local CPU
 * and the compiler.
 *
 * Ordering is not guaranteed by anything other than these primitives,
 * not even by data dependencies.  See the documentation for
 * memory_barrier() for examples and URLs to more information.
 *
 * For example, the following code would force ordering (the initial
 * value of "a" is zero, "b" is one, and "p" is "&a"):
 *
 * <programlisting>
 *	CPU 0				CPU 1
 *
 *	b = 2;
 *	memory_barrier();
 *	p = &b;				q = p;
 *					read_barrier_depends();
 *					d = *q;
 * </programlisting>
 *
 * because the read of "*q" depends on the read of "p" and these
 * two reads are separated by a read_barrier_depends().  However,
 * the following code, with the same initial values for "a" and "b":
 *
 * <programlisting>
 *	CPU 0				CPU 1
 *
 *	a = 2;
 *	memory_barrier();
 *	b = 3;				y = b;
 *					read_barrier_depends();
 *					x = a;
 * </programlisting>
 *
 * does not enforce ordering, since there is no data dependency between
 * the read of "a" and the read of "b".  Therefore, on some CPUs, such
 * as Alpha, "y" could be set to 3 and "x" to 0.  Use rmb()
 * in cases like this where there are no data dependencies.
 **/

#define read_barrier_depends()	do { } while (0)

#ifdef CONFIG_SMP
#define smp_mb()	mb()
#ifdef CONFIG_X86_PPRO_FENCE
# define smp_rmb()	rmb()
#else
# define smp_rmb()	barrier()
#endif
#ifdef CONFIG_X86_OOSTORE
# define smp_wmb() 	wmb()
#else
# define smp_wmb()	barrier()
#endif
#define smp_read_barrier_depends()	read_barrier_depends()
460
#define set_mb(var, value) do { (void)xchg(&var, value); } while (0)
461 462 463 464 465 466 467 468
#else
#define smp_mb()	barrier()
#define smp_rmb()	barrier()
#define smp_wmb()	barrier()
#define smp_read_barrier_depends()	do { } while (0)
#define set_mb(var, value) do { var = value; barrier(); } while (0)
#endif

A
Andi Kleen 已提交
469 470 471 472 473 474 475 476 477 478 479 480
/*
 * Stop RDTSC speculation. This is needed when you need to use RDTSC
 * (or get_cycles or vread that possibly accesses the TSC) in a defined
 * code region.
 *
 * (Could use an alternative three way for this if there was one.)
 */
static inline void rdtsc_barrier(void)
{
	alternative(ASM_NOP3, "mfence", X86_FEATURE_MFENCE_RDTSC);
	alternative(ASM_NOP3, "lfence", X86_FEATURE_LFENCE_RDTSC);
}
481

H
H. Peter Anvin 已提交
482
#endif /* _ASM_X86_SYSTEM_H */