Blame view

arch/x86/kernel/process.c 15.4 KB
61c4628b5   Suresh Siddha   x86, fpu: split F...
1
2
3
4
  #include <linux/errno.h>
  #include <linux/kernel.h>
  #include <linux/mm.h>
  #include <linux/smp.h>
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
5
  #include <linux/prctl.h>
61c4628b5   Suresh Siddha   x86, fpu: split F...
6
7
  #include <linux/slab.h>
  #include <linux/sched.h>
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
8
9
  #include <linux/module.h>
  #include <linux/pm.h>
aa276e1ca   Thomas Gleixner   x86, clockevents:...
10
  #include <linux/clockchips.h>
9d62dcdfa   Amerigo Wang   x86: merge proces...
11
  #include <linux/random.h>
7c68af6e3   Avi Kivity   core, x86: Add us...
12
  #include <linux/user-return-notifier.h>
814e2c84a   Andy Isaacson   x86: Factor dupli...
13
14
  #include <linux/dmi.h>
  #include <linux/utsname.h>
616135214   Arjan van de Ven   tracing, perf: Co...
15
  #include <trace/events/power.h>
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
16
  #include <linux/hw_breakpoint.h>
93789b32d   Borislav Petkov   x86, hotplug: Fix...
17
  #include <asm/cpu.h>
c1e3b377a   Zhao Yakui   ACPI: Create "idl...
18
  #include <asm/system.h>
d3ec5cae0   Ivan Vecera   x86: call machine...
19
  #include <asm/apic.h>
2c1b284e4   Jaswinder Singh Rajput   x86: clean up dec...
20
  #include <asm/syscalls.h>
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
21
22
23
  #include <asm/idle.h>
  #include <asm/uaccess.h>
  #include <asm/i387.h>
66cb59172   K.Prasad   hw-breakpoints: u...
24
  #include <asm/debugreg.h>
c1e3b377a   Zhao Yakui   ACPI: Create "idl...
25

aa283f492   Suresh Siddha   x86, fpu: lazy al...
26
  struct kmem_cache *task_xstate_cachep;
5ee481da7   Sheng Yang   x86: Export FPU A...
27
  EXPORT_SYMBOL_GPL(task_xstate_cachep);
61c4628b5   Suresh Siddha   x86, fpu: split F...
28
29
30
  
  int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  {
866032833   Avi Kivity   x86: Introduce 's...
31
  	int ret;
61c4628b5   Suresh Siddha   x86, fpu: split F...
32
  	*dst = *src;
866032833   Avi Kivity   x86: Introduce 's...
33
34
35
36
37
38
  	if (fpu_allocated(&src->thread.fpu)) {
  		memset(&dst->thread.fpu, 0, sizeof(dst->thread.fpu));
  		ret = fpu_alloc(&dst->thread.fpu);
  		if (ret)
  			return ret;
  		fpu_copy(&dst->thread.fpu, &src->thread.fpu);
aa283f492   Suresh Siddha   x86, fpu: lazy al...
39
  	}
61c4628b5   Suresh Siddha   x86, fpu: split F...
40
41
  	return 0;
  }
aa283f492   Suresh Siddha   x86, fpu: lazy al...
42
  void free_thread_xstate(struct task_struct *tsk)
61c4628b5   Suresh Siddha   x86, fpu: split F...
43
  {
866032833   Avi Kivity   x86: Introduce 's...
44
  	fpu_free(&tsk->thread.fpu);
aa283f492   Suresh Siddha   x86, fpu: lazy al...
45
  }
aa283f492   Suresh Siddha   x86, fpu: lazy al...
46
47
48
  void free_thread_info(struct thread_info *ti)
  {
  	free_thread_xstate(ti->task);
c812d8f7a   Zhao Jin   x86, mm, trivial:...
49
  	free_pages((unsigned long)ti, THREAD_ORDER);
61c4628b5   Suresh Siddha   x86, fpu: split F...
50
51
52
53
54
55
56
  }
  
  void arch_task_cache_init(void)
  {
          task_xstate_cachep =
          	kmem_cache_create("task_xstate", xstate_size,
  				  __alignof__(union thread_xstate),
2dff44052   Vegard Nossum   kmemcheck: add mm...
57
  				  SLAB_PANIC | SLAB_NOTRACK, NULL);
61c4628b5   Suresh Siddha   x86, fpu: split F...
58
  }
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
59

00dba5646   Thomas Gleixner   x86: move more co...
60
  /*
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
61
62
63
64
65
66
   * Free current thread data structures etc..
   */
  void exit_thread(void)
  {
  	struct task_struct *me = current;
  	struct thread_struct *t = &me->thread;
250981e6e   Thomas Gleixner   x86: reduce preem...
67
  	unsigned long *bp = t->io_bitmap_ptr;
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
68

250981e6e   Thomas Gleixner   x86: reduce preem...
69
  	if (bp) {
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
70
  		struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
71
72
73
74
75
76
77
78
  		t->io_bitmap_ptr = NULL;
  		clear_thread_flag(TIF_IO_BITMAP);
  		/*
  		 * Careful, clear this in the TSS too:
  		 */
  		memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
  		t->io_bitmap_max = 0;
  		put_cpu();
250981e6e   Thomas Gleixner   x86: reduce preem...
79
  		kfree(bp);
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
80
  	}
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
81
  }
3bef44479   Brian Gerst   x86: Merge show_r...
82
83
84
  void show_regs(struct pt_regs *regs)
  {
  	show_registers(regs);
e8e999cf3   Namhyung Kim   x86, dumpstack: C...
85
  	show_trace(NULL, regs, (unsigned long *)kernel_stack_pointer(regs), 0);
3bef44479   Brian Gerst   x86: Merge show_r...
86
  }
814e2c84a   Andy Isaacson   x86: Factor dupli...
87
88
  void show_regs_common(void)
  {
84e383b32   Naga Chumbalkar   x86, dmi, debug: ...
89
  	const char *vendor, *product, *board;
814e2c84a   Andy Isaacson   x86: Factor dupli...
90

84e383b32   Naga Chumbalkar   x86, dmi, debug: ...
91
92
93
  	vendor = dmi_get_system_info(DMI_SYS_VENDOR);
  	if (!vendor)
  		vendor = "";
a1884b8e5   Andy Isaacson   x86: Print DMI_BO...
94
95
96
  	product = dmi_get_system_info(DMI_PRODUCT_NAME);
  	if (!product)
  		product = "";
814e2c84a   Andy Isaacson   x86: Factor dupli...
97

84e383b32   Naga Chumbalkar   x86, dmi, debug: ...
98
99
  	/* Board Name is optional */
  	board = dmi_get_system_info(DMI_BOARD_NAME);
d015a0929   Pekka Enberg   x86: Use KERN_DEF...
100
101
  	printk(KERN_CONT "
  ");
84e383b32   Naga Chumbalkar   x86, dmi, debug: ...
102
  	printk(KERN_DEFAULT "Pid: %d, comm: %.20s %s %s %.*s",
814e2c84a   Andy Isaacson   x86: Factor dupli...
103
104
105
  		current->pid, current->comm, print_tainted(),
  		init_utsname()->release,
  		(int)strcspn(init_utsname()->version, " "),
84e383b32   Naga Chumbalkar   x86, dmi, debug: ...
106
  		init_utsname()->version);
fd8fa4d3d   Jan Beulich   x86: Combine prin...
107
108
109
  	printk(KERN_CONT " %s %s", vendor, product);
  	if (board)
  		printk(KERN_CONT "/%s", board);
84e383b32   Naga Chumbalkar   x86, dmi, debug: ...
110
111
  	printk(KERN_CONT "
  ");
814e2c84a   Andy Isaacson   x86: Factor dupli...
112
  }
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
113
114
115
  void flush_thread(void)
  {
  	struct task_struct *tsk = current;
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
116
  	flush_ptrace_hw_breakpoint(tsk);
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
178
179
180
181
182
183
184
185
186
187
188
189
190
  	memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  	/*
  	 * Forget coprocessor state..
  	 */
  	tsk->fpu_counter = 0;
  	clear_fpu(tsk);
  	clear_used_math();
  }
  
  static void hard_disable_TSC(void)
  {
  	write_cr4(read_cr4() | X86_CR4_TSD);
  }
  
  void disable_TSC(void)
  {
  	preempt_disable();
  	if (!test_and_set_thread_flag(TIF_NOTSC))
  		/*
  		 * Must flip the CPU state synchronously with
  		 * TIF_NOTSC in the current running context.
  		 */
  		hard_disable_TSC();
  	preempt_enable();
  }
  
  static void hard_enable_TSC(void)
  {
  	write_cr4(read_cr4() & ~X86_CR4_TSD);
  }
  
  static void enable_TSC(void)
  {
  	preempt_disable();
  	if (test_and_clear_thread_flag(TIF_NOTSC))
  		/*
  		 * Must flip the CPU state synchronously with
  		 * TIF_NOTSC in the current running context.
  		 */
  		hard_enable_TSC();
  	preempt_enable();
  }
  
  int get_tsc_mode(unsigned long adr)
  {
  	unsigned int val;
  
  	if (test_thread_flag(TIF_NOTSC))
  		val = PR_TSC_SIGSEGV;
  	else
  		val = PR_TSC_ENABLE;
  
  	return put_user(val, (unsigned int __user *)adr);
  }
  
  int set_tsc_mode(unsigned int val)
  {
  	if (val == PR_TSC_SIGSEGV)
  		disable_TSC();
  	else if (val == PR_TSC_ENABLE)
  		enable_TSC();
  	else
  		return -EINVAL;
  
  	return 0;
  }
  
  void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
  		      struct tss_struct *tss)
  {
  	struct thread_struct *prev, *next;
  
  	prev = &prev_p->thread;
  	next = &next_p->thread;
ea8e61b7b   Peter Zijlstra   x86, ptrace: Fix ...
191
192
193
194
195
196
197
198
199
200
  	if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^
  	    test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) {
  		unsigned long debugctl = get_debugctlmsr();
  
  		debugctl &= ~DEBUGCTLMSR_BTF;
  		if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP))
  			debugctl |= DEBUGCTLMSR_BTF;
  
  		update_debugctlmsr(debugctl);
  	}
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
201

389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  	if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
  	    test_tsk_thread_flag(next_p, TIF_NOTSC)) {
  		/* prev and next are different */
  		if (test_tsk_thread_flag(next_p, TIF_NOTSC))
  			hard_disable_TSC();
  		else
  			hard_enable_TSC();
  	}
  
  	if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
  		/*
  		 * Copy the relevant range of the IO bitmap.
  		 * Normally this is 128 bytes or less:
  		 */
  		memcpy(tss->io_bitmap, next->io_bitmap_ptr,
  		       max(prev->io_bitmap_max, next->io_bitmap_max));
  	} else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
  		/*
  		 * Clear any possible leftover bits:
  		 */
  		memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
  	}
7c68af6e3   Avi Kivity   core, x86: Add us...
224
  	propagate_user_return_notify(prev_p, next_p);
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  }
  
  int sys_fork(struct pt_regs *regs)
  {
  	return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
  }
  
  /*
   * This is trivial, and on the face of it looks like it
   * could equally well be done in user mode.
   *
   * Not so, for quite unobvious reasons - register pressure.
   * In user mode vfork() cannot have a stack frame, and if
   * done by calling the "clone()" system call directly, you
   * do not have enough call-clobbered registers to hold all
   * the information you need.
   */
  int sys_vfork(struct pt_regs *regs)
  {
  	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, 0,
  		       NULL, NULL);
  }
f839bbc5c   Brian Gerst   x86: Merge sys_clone
247
248
249
250
251
252
253
254
  long
  sys_clone(unsigned long clone_flags, unsigned long newsp,
  	  void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
  {
  	if (!newsp)
  		newsp = regs->sp;
  	return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
  }
df59e7bf4   Brian Gerst   x86: Merge kernel...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
  /*
   * This gets run with %si containing the
   * function to call, and %di containing
   * the "args".
   */
  extern void kernel_thread_helper(void);
  
  /*
   * Create a kernel thread
   */
  int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  {
  	struct pt_regs regs;
  
  	memset(&regs, 0, sizeof(regs));
  
  	regs.si = (unsigned long) fn;
  	regs.di = (unsigned long) arg;
  
  #ifdef CONFIG_X86_32
  	regs.ds = __USER_DS;
  	regs.es = __USER_DS;
  	regs.fs = __KERNEL_PERCPU;
  	regs.gs = __KERNEL_STACK_CANARY;
864a0922d   Cyrill Gorcunov   x86: kernel_threa...
279
280
  #else
  	regs.ss = __KERNEL_DS;
df59e7bf4   Brian Gerst   x86: Merge kernel...
281
282
283
284
285
  #endif
  
  	regs.orig_ax = -1;
  	regs.ip = (unsigned long) kernel_thread_helper;
  	regs.cs = __KERNEL_CS | get_kernel_rpl();
1cf8343f5   Seiichi Ikarashi   x86: Fix rflags i...
286
  	regs.flags = X86_EFLAGS_IF | X86_EFLAGS_BIT1;
df59e7bf4   Brian Gerst   x86: Merge kernel...
287
288
289
290
291
  
  	/* Ok, create the new process.. */
  	return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  }
  EXPORT_SYMBOL(kernel_thread);
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
292
293
  
  /*
11cf88bd0   Brian Gerst   x86: Merge sys_ex...
294
295
   * sys_execve() executes a new program.
   */
d7627467b   David Howells   Make do_execve() ...
296
297
298
  long sys_execve(const char __user *name,
  		const char __user *const __user *argv,
  		const char __user *const __user *envp, struct pt_regs *regs)
11cf88bd0   Brian Gerst   x86: Merge sys_ex...
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
  {
  	long error;
  	char *filename;
  
  	filename = getname(name);
  	error = PTR_ERR(filename);
  	if (IS_ERR(filename))
  		return error;
  	error = do_execve(filename, argv, envp, regs);
  
  #ifdef CONFIG_X86_32
  	if (error == 0) {
  		/* Make sure we don't return using sysenter.. */
                  set_thread_flag(TIF_IRET);
          }
  #endif
  
  	putname(filename);
  	return error;
  }
389d1fb11   Jeremy Fitzhardinge   x86: unify chunks...
319
320
  
  /*
00dba5646   Thomas Gleixner   x86: move more co...
321
322
   * Idle related variables and functions
   */
d18960494   Thomas Renninger   ACPI, intel_idle:...
323
  unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
00dba5646   Thomas Gleixner   x86: move more co...
324
325
326
327
328
329
  EXPORT_SYMBOL(boot_option_idle_override);
  
  /*
   * Powermanagement idle function, if any..
   */
  void (*pm_idle)(void);
60b8b1de0   Andy Whitcroft   x86 idle: APM req...
330
  #ifdef CONFIG_APM_MODULE
00dba5646   Thomas Gleixner   x86: move more co...
331
  EXPORT_SYMBOL(pm_idle);
06ae40ce0   Len Brown   x86 idle: EXPORT_...
332
  #endif
00dba5646   Thomas Gleixner   x86: move more co...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  
  #ifdef CONFIG_X86_32
  /*
   * This halt magic was a workaround for ancient floppy DMA
   * wreckage. It should be safe to remove.
   */
  static int hlt_counter;
  void disable_hlt(void)
  {
  	hlt_counter++;
  }
  EXPORT_SYMBOL(disable_hlt);
  
  void enable_hlt(void)
  {
  	hlt_counter--;
  }
  EXPORT_SYMBOL(enable_hlt);
  
  static inline int hlt_use_halt(void)
  {
  	return (!hlt_counter && boot_cpu_data.hlt_works_ok);
  }
  #else
  static inline int hlt_use_halt(void)
  {
  	return 1;
  }
  #endif
  
  /*
   * We use this if we don't have any better
   * idle routine..
   */
  void default_idle(void)
  {
  	if (hlt_use_halt()) {
6f4f2723d   Thomas Renninger   [CPUFREQ] x86 cpu...
370
  		trace_power_start(POWER_CSTATE, 1, smp_processor_id());
25e41933b   Thomas Renninger   perf: Clean up po...
371
  		trace_cpu_idle(1, smp_processor_id());
00dba5646   Thomas Gleixner   x86: move more co...
372
373
374
375
376
377
378
379
380
381
382
383
  		current_thread_info()->status &= ~TS_POLLING;
  		/*
  		 * TS_POLLING-cleared state must be visible before we
  		 * test NEED_RESCHED:
  		 */
  		smp_mb();
  
  		if (!need_resched())
  			safe_halt();	/* enables interrupts racelessly */
  		else
  			local_irq_enable();
  		current_thread_info()->status |= TS_POLLING;
f77cfe4ea   Thomas Renninger   cpuidle/x86/perf:...
384
385
  		trace_power_end(smp_processor_id());
  		trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
00dba5646   Thomas Gleixner   x86: move more co...
386
387
388
389
390
391
  	} else {
  		local_irq_enable();
  		/* loop is done by the caller */
  		cpu_relax();
  	}
  }
60b8b1de0   Andy Whitcroft   x86 idle: APM req...
392
  #ifdef CONFIG_APM_MODULE
00dba5646   Thomas Gleixner   x86: move more co...
393
394
  EXPORT_SYMBOL(default_idle);
  #endif
e5fd47bfa   Konrad Rzeszutek Wilk   xen/pm_idle: Make...
395
396
397
398
399
400
401
402
  bool set_pm_idle_to_default(void)
  {
  	bool ret = !!pm_idle;
  
  	pm_idle = default_idle;
  
  	return ret;
  }
d3ec5cae0   Ivan Vecera   x86: call machine...
403
404
405
406
407
408
  void stop_this_cpu(void *dummy)
  {
  	local_irq_disable();
  	/*
  	 * Remove this CPU:
  	 */
4f0628963   Rusty Russell   cpumask: use new ...
409
  	set_cpu_online(smp_processor_id(), false);
d3ec5cae0   Ivan Vecera   x86: call machine...
410
411
412
413
414
415
416
  	disable_local_APIC();
  
  	for (;;) {
  		if (hlt_works(smp_processor_id()))
  			halt();
  	}
  }
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
  static void do_nothing(void *unused)
  {
  }
  
  /*
   * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
   * pm_idle and update to new pm_idle value. Required while changing pm_idle
   * handler on SMP systems.
   *
   * Caller must have changed pm_idle to the new value before the call. Old
   * pm_idle value will not be used by any CPU after the return of this function.
   */
  void cpu_idle_wait(void)
  {
  	smp_mb();
  	/* kick all the CPUs so that they exit out of pm_idle */
127a237a1   Ingo Molnar   fix "smp_call_fun...
433
  	smp_call_function(do_nothing, NULL, 1);
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
434
435
  }
  EXPORT_SYMBOL_GPL(cpu_idle_wait);
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
436
437
438
439
  /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  static void mwait_idle(void)
  {
  	if (!need_resched()) {
6f4f2723d   Thomas Renninger   [CPUFREQ] x86 cpu...
440
  		trace_power_start(POWER_CSTATE, 1, smp_processor_id());
25e41933b   Thomas Renninger   perf: Clean up po...
441
  		trace_cpu_idle(1, smp_processor_id());
349c004e3   Christoph Lameter   x86: A fast way t...
442
  		if (this_cpu_has(X86_FEATURE_CLFLUSH_MONITOR))
e736ad548   Pallipadi, Venkatesh   x86: add clflush ...
443
  			clflush((void *)&current_thread_info()->flags);
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
444
445
446
447
448
449
  		__monitor((void *)&current_thread_info()->flags, 0, 0);
  		smp_mb();
  		if (!need_resched())
  			__sti_mwait(0, 0);
  		else
  			local_irq_enable();
f77cfe4ea   Thomas Renninger   cpuidle/x86/perf:...
450
451
  		trace_power_end(smp_processor_id());
  		trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
452
453
454
  	} else
  		local_irq_enable();
  }
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
455
456
457
458
459
460
461
  /*
   * On SMP it's slightly faster (but much more power-consuming!)
   * to poll the ->work.need_resched flag instead of waiting for the
   * cross-CPU IPI to arrive. Use this option with caution.
   */
  static void poll_idle(void)
  {
6f4f2723d   Thomas Renninger   [CPUFREQ] x86 cpu...
462
  	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
25e41933b   Thomas Renninger   perf: Clean up po...
463
  	trace_cpu_idle(0, smp_processor_id());
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
464
  	local_irq_enable();
2c7e9fd4c   Joe Korty   x86: make poll_id...
465
466
  	while (!need_resched())
  		cpu_relax();
25e41933b   Thomas Renninger   perf: Clean up po...
467
468
  	trace_power_end(smp_processor_id());
  	trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
469
  }
e9623b355   Thomas Gleixner   x86: disable mwai...
470
471
472
473
474
475
476
477
478
479
480
481
  /*
   * mwait selection logic:
   *
   * It depends on the CPU. For AMD CPUs that support MWAIT this is
   * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
   * then depend on a clock divisor and current Pstate of the core. If
   * all cores of a processor are in halt state (C1) the processor can
   * enter the C1E (C1 enhanced) state. If mwait is used this will never
   * happen.
   *
   * idle=mwait overrides this decision and forces the usage of mwait.
   */
09fd4b4ef   Thomas Gleixner   x86: use cpuid to...
482
483
484
485
  
  #define MWAIT_INFO			0x05
  #define MWAIT_ECX_EXTENDED_INFO		0x01
  #define MWAIT_EDX_C1			0xf0
1c9d16e35   Borislav Petkov   x86: Fix mwait_us...
486
  int mwait_usable(const struct cpuinfo_x86 *c)
e9623b355   Thomas Gleixner   x86: disable mwai...
487
  {
09fd4b4ef   Thomas Gleixner   x86: use cpuid to...
488
  	u32 eax, ebx, ecx, edx;
d18960494   Thomas Renninger   ACPI, intel_idle:...
489
  	if (boot_option_idle_override == IDLE_FORCE_MWAIT)
e9623b355   Thomas Gleixner   x86: disable mwai...
490
  		return 1;
09fd4b4ef   Thomas Gleixner   x86: use cpuid to...
491
492
493
494
495
496
497
498
499
500
501
502
503
  	if (c->cpuid_level < MWAIT_INFO)
  		return 0;
  
  	cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
  	/* Check, whether EDX has extended info about MWAIT */
  	if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
  		return 1;
  
  	/*
  	 * edx enumeratios MONITOR/MWAIT extensions. Check, whether
  	 * C1  supports MWAIT
  	 */
  	return (edx & MWAIT_EDX_C1);
e9623b355   Thomas Gleixner   x86: disable mwai...
504
  }
02c68a020   Len Brown   x86 idle: clarify...
505
506
  bool amd_e400_c1e_detected;
  EXPORT_SYMBOL(amd_e400_c1e_detected);
aa276e1ca   Thomas Gleixner   x86, clockevents:...
507

02c68a020   Len Brown   x86 idle: clarify...
508
  static cpumask_var_t amd_e400_c1e_mask;
4faac97d4   Thomas Gleixner   x86: prevent stal...
509

02c68a020   Len Brown   x86 idle: clarify...
510
  void amd_e400_remove_cpu(int cpu)
4faac97d4   Thomas Gleixner   x86: prevent stal...
511
  {
02c68a020   Len Brown   x86 idle: clarify...
512
513
  	if (amd_e400_c1e_mask != NULL)
  		cpumask_clear_cpu(cpu, amd_e400_c1e_mask);
4faac97d4   Thomas Gleixner   x86: prevent stal...
514
  }
aa276e1ca   Thomas Gleixner   x86, clockevents:...
515
  /*
02c68a020   Len Brown   x86 idle: clarify...
516
   * AMD Erratum 400 aware idle routine. We check for C1E active in the interrupt
aa276e1ca   Thomas Gleixner   x86, clockevents:...
517
518
519
   * pending message MSR. If we detect C1E, then we handle it the same
   * way as C3 power states (local apic timer and TSC stop)
   */
02c68a020   Len Brown   x86 idle: clarify...
520
  static void amd_e400_idle(void)
aa276e1ca   Thomas Gleixner   x86, clockevents:...
521
  {
aa276e1ca   Thomas Gleixner   x86, clockevents:...
522
523
  	if (need_resched())
  		return;
02c68a020   Len Brown   x86 idle: clarify...
524
  	if (!amd_e400_c1e_detected) {
aa276e1ca   Thomas Gleixner   x86, clockevents:...
525
526
527
  		u32 lo, hi;
  
  		rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
e8c534ec0   Michal Schmidt   x86: Fix keeping ...
528

aa276e1ca   Thomas Gleixner   x86, clockevents:...
529
  		if (lo & K8_INTP_C1E_ACTIVE_MASK) {
02c68a020   Len Brown   x86 idle: clarify...
530
  			amd_e400_c1e_detected = true;
40fb17152   Venki Pallipadi   x86: support alwa...
531
  			if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
09bfeea13   Andreas Herrmann   x86: c1e_idle: do...
532
533
534
  				mark_tsc_unstable("TSC halt in AMD C1E");
  			printk(KERN_INFO "System has AMD C1E enabled
  ");
aa276e1ca   Thomas Gleixner   x86, clockevents:...
535
536
  		}
  	}
02c68a020   Len Brown   x86 idle: clarify...
537
  	if (amd_e400_c1e_detected) {
aa276e1ca   Thomas Gleixner   x86, clockevents:...
538
  		int cpu = smp_processor_id();
02c68a020   Len Brown   x86 idle: clarify...
539
540
  		if (!cpumask_test_cpu(cpu, amd_e400_c1e_mask)) {
  			cpumask_set_cpu(cpu, amd_e400_c1e_mask);
0beefa208   Thomas Gleixner   x86: add C1E awar...
541
  			/*
f833bab87   Suresh Siddha   clockevent: Preve...
542
  			 * Force broadcast so ACPI can not interfere.
0beefa208   Thomas Gleixner   x86: add C1E awar...
543
  			 */
aa276e1ca   Thomas Gleixner   x86, clockevents:...
544
545
546
547
548
549
550
  			clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  					   &cpu);
  			printk(KERN_INFO "Switch to broadcast mode on CPU%d
  ",
  			       cpu);
  		}
  		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
0beefa208   Thomas Gleixner   x86: add C1E awar...
551

aa276e1ca   Thomas Gleixner   x86, clockevents:...
552
  		default_idle();
0beefa208   Thomas Gleixner   x86: add C1E awar...
553
554
555
556
557
558
559
560
  
  		/*
  		 * The switch back from broadcast mode needs to be
  		 * called with interrupts disabled.
  		 */
  		 local_irq_disable();
  		 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  		 local_irq_enable();
aa276e1ca   Thomas Gleixner   x86, clockevents:...
561
562
563
  	} else
  		default_idle();
  }
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
564
565
  void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  {
3e5095d15   Ingo Molnar   x86: replace CONF...
566
  #ifdef CONFIG_SMP
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
567
  	if (pm_idle == poll_idle && smp_num_siblings > 1) {
d6dd69216   Mike Travis   x86: Reduce per c...
568
  		printk_once(KERN_WARNING "WARNING: polling idle and HT enabled,"
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
569
570
571
572
  			" performance may degrade.
  ");
  	}
  #endif
6ddd2a279   Thomas Gleixner   x86: simplify idl...
573
574
  	if (pm_idle)
  		return;
e9623b355   Thomas Gleixner   x86: disable mwai...
575
  	if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
576
  		/*
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
577
578
  		 * One CPU supports mwait => All CPUs supports mwait
  		 */
6ddd2a279   Thomas Gleixner   x86: simplify idl...
579
580
581
  		printk(KERN_INFO "using mwait in idle threads.
  ");
  		pm_idle = mwait_idle;
9d8888c2a   Hans Rosenfeld   x86, cpu: Clean u...
582
583
  	} else if (cpu_has_amd_erratum(amd_erratum_400)) {
  		/* E400: APIC timer interrupt does not wake up CPU from C1e */
02c68a020   Len Brown   x86 idle: clarify...
584
585
586
  		printk(KERN_INFO "using AMD E400 aware idle routine
  ");
  		pm_idle = amd_e400_idle;
6ddd2a279   Thomas Gleixner   x86: simplify idl...
587
588
  	} else
  		pm_idle = default_idle;
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
589
  }
02c68a020   Len Brown   x86 idle: clarify...
590
  void __init init_amd_e400_c1e_mask(void)
30e1e6d1a   Rusty Russell   cpumask: fix CONF...
591
  {
02c68a020   Len Brown   x86 idle: clarify...
592
593
594
  	/* If we're using amd_e400_idle, we need to allocate amd_e400_c1e_mask. */
  	if (pm_idle == amd_e400_idle)
  		zalloc_cpumask_var(&amd_e400_c1e_mask, GFP_KERNEL);
30e1e6d1a   Rusty Russell   cpumask: fix CONF...
595
  }
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
596
597
  static int __init idle_setup(char *str)
  {
ab6bc3e34   Cyrill Gorcunov   x86: idle process...
598
599
  	if (!str)
  		return -EINVAL;
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
600
601
602
603
  	if (!strcmp(str, "poll")) {
  		printk("using polling idle threads.
  ");
  		pm_idle = poll_idle;
d18960494   Thomas Renninger   ACPI, intel_idle:...
604
605
606
  		boot_option_idle_override = IDLE_POLL;
  	} else if (!strcmp(str, "mwait")) {
  		boot_option_idle_override = IDLE_FORCE_MWAIT;
af0d6a0a3   Linus Torvalds   Merge branch 'x86...
607
608
  		WARN_ONCE(1, "\"idle=mwait\" will be removed in 2012
  ");
d18960494   Thomas Renninger   ACPI, intel_idle:...
609
  	} else if (!strcmp(str, "halt")) {
c1e3b377a   Zhao Yakui   ACPI: Create "idl...
610
611
612
613
614
615
616
617
  		/*
  		 * When the boot option of idle=halt is added, halt is
  		 * forced to be used for CPU idle. In such case CPU C2/C3
  		 * won't be used again.
  		 * To continue to load the CPU idle driver, don't touch
  		 * the boot_option_idle_override.
  		 */
  		pm_idle = default_idle;
d18960494   Thomas Renninger   ACPI, intel_idle:...
618
  		boot_option_idle_override = IDLE_HALT;
da5e09a1b   Zhao Yakui   ACPI : Create "id...
619
620
621
622
623
624
625
  	} else if (!strcmp(str, "nomwait")) {
  		/*
  		 * If the boot option of "idle=nomwait" is added,
  		 * it means that mwait will be disabled for CPU C2/C3
  		 * states. In such case it won't touch the variable
  		 * of boot_option_idle_override.
  		 */
d18960494   Thomas Renninger   ACPI, intel_idle:...
626
  		boot_option_idle_override = IDLE_NOMWAIT;
c1e3b377a   Zhao Yakui   ACPI: Create "idl...
627
  	} else
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
628
  		return -1;
7f424a8b0   Peter Zijlstra   fix idle (arch, a...
629
630
631
  	return 0;
  }
  early_param("idle", idle_setup);
9d62dcdfa   Amerigo Wang   x86: merge proces...
632
633
634
635
636
637
638
639
640
641
642
643
  unsigned long arch_align_stack(unsigned long sp)
  {
  	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  		sp -= get_random_int() % 8192;
  	return sp & ~0xf;
  }
  
  unsigned long arch_randomize_brk(struct mm_struct *mm)
  {
  	unsigned long range_end = mm->brk + 0x02000000;
  	return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  }