blob: e8173154800d052514249119416ab8af9c0d23b5 [file] [log] [blame]
Suresh Siddha61c46282008-03-10 15:28:04 -07001#include <linux/errno.h>
2#include <linux/kernel.h>
3#include <linux/mm.h>
4#include <linux/smp.h>
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -08005#include <linux/prctl.h>
Suresh Siddha61c46282008-03-10 15:28:04 -07006#include <linux/slab.h>
7#include <linux/sched.h>
Peter Zijlstra7f424a82008-04-25 17:39:01 +02008#include <linux/module.h>
9#include <linux/pm.h>
Thomas Gleixneraa276e12008-06-09 19:15:00 +020010#include <linux/clockchips.h>
Amerigo Wang9d62dcd2009-05-11 22:05:28 -040011#include <linux/random.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030012#include <linux/user-return-notifier.h>
Andy Isaacson814e2c82009-12-08 00:29:42 -080013#include <linux/dmi.h>
14#include <linux/utsname.h>
Richard Weinberger90e24012012-03-25 23:00:04 +020015#include <linux/stackprotector.h>
16#include <linux/tick.h>
17#include <linux/cpuidle.h>
Arjan van de Ven61613522009-09-17 16:11:28 +020018#include <trace/events/power.h>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020019#include <linux/hw_breakpoint.h>
Borislav Petkov93789b32011-01-20 15:42:52 +010020#include <asm/cpu.h>
Ivan Vecerad3ec5ca2008-11-11 14:33:44 +010021#include <asm/apic.h>
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +053022#include <asm/syscalls.h>
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080023#include <asm/idle.h>
24#include <asm/uaccess.h>
25#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080026#include <asm/fpu-internal.h>
K.Prasad66cb5912009-06-01 23:44:55 +053027#include <asm/debugreg.h>
Richard Weinberger90e24012012-03-25 23:00:04 +020028#include <asm/nmi.h>
29
Thomas Gleixner45046892012-05-03 09:03:01 +000030/*
31 * per-CPU TSS segments. Threads are completely 'soft' on Linux,
32 * no more per-task TSS's. The TSS size is kept cacheline-aligned
33 * so they are allowed to end up in the .data..cacheline_aligned
34 * section. Since TSS's are completely CPU-local, we want them
35 * on exact cacheline boundaries, to eliminate cacheline ping-pong.
36 */
37DEFINE_PER_CPU_SHARED_ALIGNED(struct tss_struct, init_tss) = INIT_TSS;
38
Richard Weinberger90e24012012-03-25 23:00:04 +020039#ifdef CONFIG_X86_64
40static DEFINE_PER_CPU(unsigned char, is_idle);
41static ATOMIC_NOTIFIER_HEAD(idle_notifier);
42
43void idle_notifier_register(struct notifier_block *n)
44{
45 atomic_notifier_chain_register(&idle_notifier, n);
46}
47EXPORT_SYMBOL_GPL(idle_notifier_register);
48
49void idle_notifier_unregister(struct notifier_block *n)
50{
51 atomic_notifier_chain_unregister(&idle_notifier, n);
52}
53EXPORT_SYMBOL_GPL(idle_notifier_unregister);
54#endif
Zhao Yakuic1e3b372008-06-24 17:58:53 +080055
Suresh Siddhaaa283f42008-03-10 15:28:05 -070056struct kmem_cache *task_xstate_cachep;
Sheng Yang5ee481d2010-05-17 17:22:23 +080057EXPORT_SYMBOL_GPL(task_xstate_cachep);
Suresh Siddha61c46282008-03-10 15:28:04 -070058
59int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
60{
Avi Kivity86603282010-05-06 11:45:46 +030061 int ret;
62
Suresh Siddha61c46282008-03-10 15:28:04 -070063 *dst = *src;
Avi Kivity86603282010-05-06 11:45:46 +030064 if (fpu_allocated(&src->thread.fpu)) {
65 memset(&dst->thread.fpu, 0, sizeof(dst->thread.fpu));
66 ret = fpu_alloc(&dst->thread.fpu);
67 if (ret)
68 return ret;
69 fpu_copy(&dst->thread.fpu, &src->thread.fpu);
Suresh Siddhaaa283f42008-03-10 15:28:05 -070070 }
Suresh Siddha61c46282008-03-10 15:28:04 -070071 return 0;
72}
73
Suresh Siddhaaa283f42008-03-10 15:28:05 -070074void free_thread_xstate(struct task_struct *tsk)
75{
Avi Kivity86603282010-05-06 11:45:46 +030076 fpu_free(&tsk->thread.fpu);
Suresh Siddhaaa283f42008-03-10 15:28:05 -070077}
78
Thomas Gleixner38e7c572012-05-05 15:05:42 +000079void arch_release_task_struct(struct task_struct *tsk)
Suresh Siddha61c46282008-03-10 15:28:04 -070080{
Thomas Gleixner38e7c572012-05-05 15:05:42 +000081 free_thread_xstate(tsk);
Suresh Siddha61c46282008-03-10 15:28:04 -070082}
83
84void arch_task_cache_init(void)
85{
86 task_xstate_cachep =
87 kmem_cache_create("task_xstate", xstate_size,
88 __alignof__(union thread_xstate),
Vegard Nossum2dff4402008-05-31 15:56:17 +020089 SLAB_PANIC | SLAB_NOTRACK, NULL);
Suresh Siddha61c46282008-03-10 15:28:04 -070090}
Peter Zijlstra7f424a82008-04-25 17:39:01 +020091
Thomas Gleixner00dba562008-06-09 18:35:28 +020092/*
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080093 * Free current thread data structures etc..
94 */
95void exit_thread(void)
96{
97 struct task_struct *me = current;
98 struct thread_struct *t = &me->thread;
Thomas Gleixner250981e2009-03-16 13:07:21 +010099 unsigned long *bp = t->io_bitmap_ptr;
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800100
Thomas Gleixner250981e2009-03-16 13:07:21 +0100101 if (bp) {
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800102 struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
103
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800104 t->io_bitmap_ptr = NULL;
105 clear_thread_flag(TIF_IO_BITMAP);
106 /*
107 * Careful, clear this in the TSS too:
108 */
109 memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
110 t->io_bitmap_max = 0;
111 put_cpu();
Thomas Gleixner250981e2009-03-16 13:07:21 +0100112 kfree(bp);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800113 }
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800114}
115
Brian Gerst3bef4442010-01-13 10:45:55 -0500116void show_regs(struct pt_regs *regs)
117{
118 show_registers(regs);
Namhyung Kime8e999c2011-03-18 11:40:06 +0900119 show_trace(NULL, regs, (unsigned long *)kernel_stack_pointer(regs), 0);
Brian Gerst3bef4442010-01-13 10:45:55 -0500120}
121
Andy Isaacson814e2c82009-12-08 00:29:42 -0800122void show_regs_common(void)
123{
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000124 const char *vendor, *product, *board;
Andy Isaacson814e2c82009-12-08 00:29:42 -0800125
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000126 vendor = dmi_get_system_info(DMI_SYS_VENDOR);
127 if (!vendor)
128 vendor = "";
Andy Isaacsona1884b82009-12-08 00:30:21 -0800129 product = dmi_get_system_info(DMI_PRODUCT_NAME);
130 if (!product)
131 product = "";
Andy Isaacson814e2c82009-12-08 00:29:42 -0800132
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000133 /* Board Name is optional */
134 board = dmi_get_system_info(DMI_BOARD_NAME);
135
Pekka Enbergd015a092009-12-28 10:26:59 +0200136 printk(KERN_CONT "\n");
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000137 printk(KERN_DEFAULT "Pid: %d, comm: %.20s %s %s %.*s",
Andy Isaacson814e2c82009-12-08 00:29:42 -0800138 current->pid, current->comm, print_tainted(),
139 init_utsname()->release,
140 (int)strcspn(init_utsname()->version, " "),
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000141 init_utsname()->version);
Jan Beulichfd8fa4d32011-02-17 15:56:58 +0000142 printk(KERN_CONT " %s %s", vendor, product);
143 if (board)
144 printk(KERN_CONT "/%s", board);
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000145 printk(KERN_CONT "\n");
Andy Isaacson814e2c82009-12-08 00:29:42 -0800146}
147
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800148void flush_thread(void)
149{
150 struct task_struct *tsk = current;
151
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200152 flush_ptrace_hw_breakpoint(tsk);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800153 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
154 /*
155 * Forget coprocessor state..
156 */
157 tsk->fpu_counter = 0;
158 clear_fpu(tsk);
159 clear_used_math();
160}
161
162static void hard_disable_TSC(void)
163{
164 write_cr4(read_cr4() | X86_CR4_TSD);
165}
166
167void disable_TSC(void)
168{
169 preempt_disable();
170 if (!test_and_set_thread_flag(TIF_NOTSC))
171 /*
172 * Must flip the CPU state synchronously with
173 * TIF_NOTSC in the current running context.
174 */
175 hard_disable_TSC();
176 preempt_enable();
177}
178
179static void hard_enable_TSC(void)
180{
181 write_cr4(read_cr4() & ~X86_CR4_TSD);
182}
183
184static void enable_TSC(void)
185{
186 preempt_disable();
187 if (test_and_clear_thread_flag(TIF_NOTSC))
188 /*
189 * Must flip the CPU state synchronously with
190 * TIF_NOTSC in the current running context.
191 */
192 hard_enable_TSC();
193 preempt_enable();
194}
195
196int get_tsc_mode(unsigned long adr)
197{
198 unsigned int val;
199
200 if (test_thread_flag(TIF_NOTSC))
201 val = PR_TSC_SIGSEGV;
202 else
203 val = PR_TSC_ENABLE;
204
205 return put_user(val, (unsigned int __user *)adr);
206}
207
208int set_tsc_mode(unsigned int val)
209{
210 if (val == PR_TSC_SIGSEGV)
211 disable_TSC();
212 else if (val == PR_TSC_ENABLE)
213 enable_TSC();
214 else
215 return -EINVAL;
216
217 return 0;
218}
219
220void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
221 struct tss_struct *tss)
222{
223 struct thread_struct *prev, *next;
224
225 prev = &prev_p->thread;
226 next = &next_p->thread;
227
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100228 if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^
229 test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) {
230 unsigned long debugctl = get_debugctlmsr();
231
232 debugctl &= ~DEBUGCTLMSR_BTF;
233 if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP))
234 debugctl |= DEBUGCTLMSR_BTF;
235
236 update_debugctlmsr(debugctl);
237 }
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800238
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800239 if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
240 test_tsk_thread_flag(next_p, TIF_NOTSC)) {
241 /* prev and next are different */
242 if (test_tsk_thread_flag(next_p, TIF_NOTSC))
243 hard_disable_TSC();
244 else
245 hard_enable_TSC();
246 }
247
248 if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
249 /*
250 * Copy the relevant range of the IO bitmap.
251 * Normally this is 128 bytes or less:
252 */
253 memcpy(tss->io_bitmap, next->io_bitmap_ptr,
254 max(prev->io_bitmap_max, next->io_bitmap_max));
255 } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
256 /*
257 * Clear any possible leftover bits:
258 */
259 memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
260 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300261 propagate_user_return_notify(prev_p, next_p);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800262}
263
264int sys_fork(struct pt_regs *regs)
265{
266 return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
267}
268
269/*
270 * This is trivial, and on the face of it looks like it
271 * could equally well be done in user mode.
272 *
273 * Not so, for quite unobvious reasons - register pressure.
274 * In user mode vfork() cannot have a stack frame, and if
275 * done by calling the "clone()" system call directly, you
276 * do not have enough call-clobbered registers to hold all
277 * the information you need.
278 */
279int sys_vfork(struct pt_regs *regs)
280{
281 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, 0,
282 NULL, NULL);
283}
284
Brian Gerstf839bbc2009-12-09 19:01:56 -0500285long
286sys_clone(unsigned long clone_flags, unsigned long newsp,
287 void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
288{
289 if (!newsp)
290 newsp = regs->sp;
291 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
292}
293
Brian Gerstdf59e7b2009-12-09 12:34:44 -0500294/*
295 * This gets run with %si containing the
296 * function to call, and %di containing
297 * the "args".
298 */
299extern void kernel_thread_helper(void);
300
301/*
302 * Create a kernel thread
303 */
304int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
305{
306 struct pt_regs regs;
307
308 memset(&regs, 0, sizeof(regs));
309
310 regs.si = (unsigned long) fn;
311 regs.di = (unsigned long) arg;
312
313#ifdef CONFIG_X86_32
314 regs.ds = __USER_DS;
315 regs.es = __USER_DS;
316 regs.fs = __KERNEL_PERCPU;
317 regs.gs = __KERNEL_STACK_CANARY;
Cyrill Gorcunov864a0922010-01-13 10:16:07 +0000318#else
319 regs.ss = __KERNEL_DS;
Brian Gerstdf59e7b2009-12-09 12:34:44 -0500320#endif
321
322 regs.orig_ax = -1;
323 regs.ip = (unsigned long) kernel_thread_helper;
324 regs.cs = __KERNEL_CS | get_kernel_rpl();
Seiichi Ikarashi1cf83432011-12-06 17:58:14 +0900325 regs.flags = X86_EFLAGS_IF | X86_EFLAGS_BIT1;
Brian Gerstdf59e7b2009-12-09 12:34:44 -0500326
327 /* Ok, create the new process.. */
328 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
329}
330EXPORT_SYMBOL(kernel_thread);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800331
332/*
Brian Gerst11cf88b2009-12-09 19:01:53 -0500333 * sys_execve() executes a new program.
334 */
David Howellsd7627462010-08-17 23:52:56 +0100335long sys_execve(const char __user *name,
336 const char __user *const __user *argv,
337 const char __user *const __user *envp, struct pt_regs *regs)
Brian Gerst11cf88b2009-12-09 19:01:53 -0500338{
339 long error;
340 char *filename;
341
342 filename = getname(name);
343 error = PTR_ERR(filename);
344 if (IS_ERR(filename))
345 return error;
346 error = do_execve(filename, argv, envp, regs);
347
348#ifdef CONFIG_X86_32
349 if (error == 0) {
350 /* Make sure we don't return using sysenter.. */
351 set_thread_flag(TIF_IRET);
352 }
353#endif
354
355 putname(filename);
356 return error;
357}
Thomas Gleixner09fd4b42008-06-09 18:04:27 +0200358
359/*
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200360 * Idle related variables and functions
361 */
Thomas Renningerd1896042010-11-03 17:06:14 +0100362unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200363EXPORT_SYMBOL(boot_option_idle_override);
364
365/*
366 * Powermanagement idle function, if any..
367 */
368void (*pm_idle)(void);
Andy Whitcroft60b8b1d2011-06-14 12:45:10 -0700369#ifdef CONFIG_APM_MODULE
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200370EXPORT_SYMBOL(pm_idle);
Len Brown06ae40c2011-04-01 15:28:09 -0400371#endif
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200372
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200373static inline int hlt_use_halt(void)
374{
375 return 1;
376}
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200377
Richard Weinberger90e24012012-03-25 23:00:04 +0200378#ifndef CONFIG_SMP
379static inline void play_dead(void)
380{
381 BUG();
382}
383#endif
384
385#ifdef CONFIG_X86_64
386void enter_idle(void)
387{
388 percpu_write(is_idle, 1);
389 atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
390}
391
392static void __exit_idle(void)
393{
394 if (x86_test_and_clear_bit_percpu(0, is_idle) == 0)
395 return;
396 atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
397}
398
399/* Called from interrupts to signify idle end */
400void exit_idle(void)
401{
402 /* idle loop has pid 0 */
403 if (current->pid)
404 return;
405 __exit_idle();
406}
407#endif
408
409/*
410 * The idle thread. There's no useful work to be
411 * done, so just try to conserve power and have a
412 * low exit latency (ie sit in a loop waiting for
413 * somebody to say that they'd like to reschedule)
414 */
415void cpu_idle(void)
416{
417 /*
418 * If we're the non-boot CPU, nothing set the stack canary up
419 * for us. CPU0 already has it initialized but no harm in
420 * doing it again. This is a good place for updating it, as
421 * we wont ever return from this function (so the invalid
422 * canaries already on the stack wont ever trigger).
423 */
424 boot_init_stack_canary();
425 current_thread_info()->status |= TS_POLLING;
426
427 while (1) {
428 tick_nohz_idle_enter();
429
430 while (!need_resched()) {
431 rmb();
432
433 if (cpu_is_offline(smp_processor_id()))
434 play_dead();
435
436 /*
437 * Idle routines should keep interrupts disabled
438 * from here on, until they go to idle.
439 * Otherwise, idle callbacks can misfire.
440 */
441 local_touch_nmi();
442 local_irq_disable();
443
444 enter_idle();
445
446 /* Don't trace irqs off for idle */
447 stop_critical_timings();
448
449 /* enter_idle() needs rcu for notifiers */
450 rcu_idle_enter();
451
452 if (cpuidle_idle_call())
453 pm_idle();
454
455 rcu_idle_exit();
456 start_critical_timings();
457
458 /* In many cases the interrupt that ended idle
459 has already called exit_idle. But some idle
460 loops can be woken up without interrupt. */
461 __exit_idle();
462 }
463
464 tick_nohz_idle_exit();
465 preempt_enable_no_resched();
466 schedule();
467 preempt_disable();
468 }
469}
470
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200471/*
472 * We use this if we don't have any better
473 * idle routine..
474 */
475void default_idle(void)
476{
477 if (hlt_use_halt()) {
Steven Rostedt48454652012-02-07 09:40:30 -0500478 trace_power_start_rcuidle(POWER_CSTATE, 1, smp_processor_id());
479 trace_cpu_idle_rcuidle(1, smp_processor_id());
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200480 current_thread_info()->status &= ~TS_POLLING;
481 /*
482 * TS_POLLING-cleared state must be visible before we
483 * test NEED_RESCHED:
484 */
485 smp_mb();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200486
487 if (!need_resched())
488 safe_halt(); /* enables interrupts racelessly */
489 else
490 local_irq_enable();
491 current_thread_info()->status |= TS_POLLING;
Steven Rostedt48454652012-02-07 09:40:30 -0500492 trace_power_end_rcuidle(smp_processor_id());
493 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200494 } else {
495 local_irq_enable();
496 /* loop is done by the caller */
497 cpu_relax();
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200498 }
499}
Andy Whitcroft60b8b1d2011-06-14 12:45:10 -0700500#ifdef CONFIG_APM_MODULE
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200501EXPORT_SYMBOL(default_idle);
502#endif
503
Konrad Rzeszutek Wilke5fd47b2011-11-21 18:02:02 -0500504bool set_pm_idle_to_default(void)
505{
506 bool ret = !!pm_idle;
507
508 pm_idle = default_idle;
509
510 return ret;
511}
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200512void stop_this_cpu(void *dummy)
513{
514 local_irq_disable();
515 /*
516 * Remove this CPU:
517 */
Rusty Russell4f062892009-03-13 14:49:54 +1030518 set_cpu_online(smp_processor_id(), false);
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200519 disable_local_APIC();
520
521 for (;;) {
522 if (hlt_works(smp_processor_id()))
523 halt();
524 }
525}
526
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200527/* Default MONITOR/MWAIT with no hints, used for default C1 state */
528static void mwait_idle(void)
529{
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200530 if (!need_resched()) {
Steven Rostedt48454652012-02-07 09:40:30 -0500531 trace_power_start_rcuidle(POWER_CSTATE, 1, smp_processor_id());
532 trace_cpu_idle_rcuidle(1, smp_processor_id());
Christoph Lameter349c0042011-03-12 12:50:10 +0100533 if (this_cpu_has(X86_FEATURE_CLFLUSH_MONITOR))
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200534 clflush((void *)&current_thread_info()->flags);
535
536 __monitor((void *)&current_thread_info()->flags, 0, 0);
537 smp_mb();
538 if (!need_resched())
539 __sti_mwait(0, 0);
540 else
541 local_irq_enable();
Steven Rostedt48454652012-02-07 09:40:30 -0500542 trace_power_end_rcuidle(smp_processor_id());
543 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200544 } else
545 local_irq_enable();
546}
547
548/*
549 * On SMP it's slightly faster (but much more power-consuming!)
550 * to poll the ->work.need_resched flag instead of waiting for the
551 * cross-CPU IPI to arrive. Use this option with caution.
552 */
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200553static void poll_idle(void)
554{
Steven Rostedt48454652012-02-07 09:40:30 -0500555 trace_power_start_rcuidle(POWER_CSTATE, 0, smp_processor_id());
556 trace_cpu_idle_rcuidle(0, smp_processor_id());
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200557 local_irq_enable();
558 while (!need_resched())
559 cpu_relax();
Steven Rostedt48454652012-02-07 09:40:30 -0500560 trace_power_end_rcuidle(smp_processor_id());
561 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200562}
563
564/*
565 * mwait selection logic:
566 *
567 * It depends on the CPU. For AMD CPUs that support MWAIT this is
568 * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
569 * then depend on a clock divisor and current Pstate of the core. If
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200570 * all cores of a processor are in halt state (C1) the processor can
571 * enter the C1E (C1 enhanced) state. If mwait is used this will never
572 * happen.
573 *
574 * idle=mwait overrides this decision and forces the usage of mwait.
575 */
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200576
577#define MWAIT_INFO 0x05
578#define MWAIT_ECX_EXTENDED_INFO 0x01
579#define MWAIT_EDX_C1 0xf0
580
Borislav Petkov1c9d16e2011-02-11 18:17:54 +0100581int mwait_usable(const struct cpuinfo_x86 *c)
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200582{
Thomas Gleixner09fd4b42008-06-09 18:04:27 +0200583 u32 eax, ebx, ecx, edx;
584
Thomas Renningerd1896042010-11-03 17:06:14 +0100585 if (boot_option_idle_override == IDLE_FORCE_MWAIT)
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200586 return 1;
587
Thomas Gleixner09fd4b42008-06-09 18:04:27 +0200588 if (c->cpuid_level < MWAIT_INFO)
589 return 0;
590
591 cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
592 /* Check, whether EDX has extended info about MWAIT */
593 if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
594 return 1;
595
596 /*
597 * edx enumeratios MONITOR/MWAIT extensions. Check, whether
598 * C1 supports MWAIT
599 */
600 return (edx & MWAIT_EDX_C1);
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200601}
602
Len Brown02c68a02011-04-01 16:59:53 -0400603bool amd_e400_c1e_detected;
604EXPORT_SYMBOL(amd_e400_c1e_detected);
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200605
Len Brown02c68a02011-04-01 16:59:53 -0400606static cpumask_var_t amd_e400_c1e_mask;
Thomas Gleixner4faac972008-09-22 18:54:29 +0200607
Len Brown02c68a02011-04-01 16:59:53 -0400608void amd_e400_remove_cpu(int cpu)
Thomas Gleixner4faac972008-09-22 18:54:29 +0200609{
Len Brown02c68a02011-04-01 16:59:53 -0400610 if (amd_e400_c1e_mask != NULL)
611 cpumask_clear_cpu(cpu, amd_e400_c1e_mask);
Thomas Gleixner4faac972008-09-22 18:54:29 +0200612}
613
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200614/*
Len Brown02c68a02011-04-01 16:59:53 -0400615 * AMD Erratum 400 aware idle routine. We check for C1E active in the interrupt
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200616 * pending message MSR. If we detect C1E, then we handle it the same
617 * way as C3 power states (local apic timer and TSC stop)
618 */
Len Brown02c68a02011-04-01 16:59:53 -0400619static void amd_e400_idle(void)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200620{
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200621 if (need_resched())
622 return;
623
Len Brown02c68a02011-04-01 16:59:53 -0400624 if (!amd_e400_c1e_detected) {
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200625 u32 lo, hi;
626
627 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
Michal Schmidte8c534e2010-07-27 18:53:35 +0200628
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200629 if (lo & K8_INTP_C1E_ACTIVE_MASK) {
Len Brown02c68a02011-04-01 16:59:53 -0400630 amd_e400_c1e_detected = true;
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800631 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
Andreas Herrmann09bfeea2008-09-18 21:12:10 +0200632 mark_tsc_unstable("TSC halt in AMD C1E");
633 printk(KERN_INFO "System has AMD C1E enabled\n");
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200634 }
635 }
636
Len Brown02c68a02011-04-01 16:59:53 -0400637 if (amd_e400_c1e_detected) {
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200638 int cpu = smp_processor_id();
639
Len Brown02c68a02011-04-01 16:59:53 -0400640 if (!cpumask_test_cpu(cpu, amd_e400_c1e_mask)) {
641 cpumask_set_cpu(cpu, amd_e400_c1e_mask);
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200642 /*
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700643 * Force broadcast so ACPI can not interfere.
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200644 */
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200645 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
646 &cpu);
647 printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
648 cpu);
649 }
650 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200651
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200652 default_idle();
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200653
654 /*
655 * The switch back from broadcast mode needs to be
656 * called with interrupts disabled.
657 */
658 local_irq_disable();
659 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
660 local_irq_enable();
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200661 } else
662 default_idle();
663}
664
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200665void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
666{
Ingo Molnar3e5095d2009-01-27 17:07:08 +0100667#ifdef CONFIG_SMP
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200668 if (pm_idle == poll_idle && smp_num_siblings > 1) {
Mike Travisd6dd6922010-03-05 13:10:38 -0600669 printk_once(KERN_WARNING "WARNING: polling idle and HT enabled,"
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200670 " performance may degrade.\n");
671 }
672#endif
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200673 if (pm_idle)
674 return;
675
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200676 if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200677 /*
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200678 * One CPU supports mwait => All CPUs supports mwait
679 */
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200680 printk(KERN_INFO "using mwait in idle threads.\n");
681 pm_idle = mwait_idle;
Hans Rosenfeld9d8888c2010-07-28 19:09:31 +0200682 } else if (cpu_has_amd_erratum(amd_erratum_400)) {
683 /* E400: APIC timer interrupt does not wake up CPU from C1e */
Len Brown02c68a02011-04-01 16:59:53 -0400684 printk(KERN_INFO "using AMD E400 aware idle routine\n");
685 pm_idle = amd_e400_idle;
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200686 } else
687 pm_idle = default_idle;
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200688}
689
Len Brown02c68a02011-04-01 16:59:53 -0400690void __init init_amd_e400_c1e_mask(void)
Rusty Russell30e1e6d2009-03-17 14:50:34 +1030691{
Len Brown02c68a02011-04-01 16:59:53 -0400692 /* If we're using amd_e400_idle, we need to allocate amd_e400_c1e_mask. */
693 if (pm_idle == amd_e400_idle)
694 zalloc_cpumask_var(&amd_e400_c1e_mask, GFP_KERNEL);
Rusty Russell30e1e6d2009-03-17 14:50:34 +1030695}
696
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200697static int __init idle_setup(char *str)
698{
Cyrill Gorcunovab6bc3e2008-07-05 15:53:36 +0400699 if (!str)
700 return -EINVAL;
701
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200702 if (!strcmp(str, "poll")) {
703 printk("using polling idle threads.\n");
704 pm_idle = poll_idle;
Thomas Renningerd1896042010-11-03 17:06:14 +0100705 boot_option_idle_override = IDLE_POLL;
706 } else if (!strcmp(str, "mwait")) {
707 boot_option_idle_override = IDLE_FORCE_MWAIT;
Linus Torvaldsaf0d6a02011-06-01 02:07:22 +0900708 WARN_ONCE(1, "\"idle=mwait\" will be removed in 2012\n");
Thomas Renningerd1896042010-11-03 17:06:14 +0100709 } else if (!strcmp(str, "halt")) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800710 /*
711 * When the boot option of idle=halt is added, halt is
712 * forced to be used for CPU idle. In such case CPU C2/C3
713 * won't be used again.
714 * To continue to load the CPU idle driver, don't touch
715 * the boot_option_idle_override.
716 */
717 pm_idle = default_idle;
Thomas Renningerd1896042010-11-03 17:06:14 +0100718 boot_option_idle_override = IDLE_HALT;
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800719 } else if (!strcmp(str, "nomwait")) {
720 /*
721 * If the boot option of "idle=nomwait" is added,
722 * it means that mwait will be disabled for CPU C2/C3
723 * states. In such case it won't touch the variable
724 * of boot_option_idle_override.
725 */
Thomas Renningerd1896042010-11-03 17:06:14 +0100726 boot_option_idle_override = IDLE_NOMWAIT;
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800727 } else
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200728 return -1;
729
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200730 return 0;
731}
732early_param("idle", idle_setup);
733
Amerigo Wang9d62dcd2009-05-11 22:05:28 -0400734unsigned long arch_align_stack(unsigned long sp)
735{
736 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
737 sp -= get_random_int() % 8192;
738 return sp & ~0xf;
739}
740
741unsigned long arch_randomize_brk(struct mm_struct *mm)
742{
743 unsigned long range_end = mm->brk + 0x02000000;
744 return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
745}
746