blob: 54b2711f2dbc28041e3bb9e48ca6576f9521a7d7 [file] [log] [blame]
Joe Perchesc767a542012-05-21 19:50:07 -07001#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
Suresh Siddha61c46282008-03-10 15:28:04 -07003#include <linux/errno.h>
4#include <linux/kernel.h>
5#include <linux/mm.h>
6#include <linux/smp.h>
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -08007#include <linux/prctl.h>
Suresh Siddha61c46282008-03-10 15:28:04 -07008#include <linux/slab.h>
9#include <linux/sched.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -040010#include <linux/init.h>
11#include <linux/export.h>
Peter Zijlstra7f424a82008-04-25 17:39:01 +020012#include <linux/pm.h>
Thomas Gleixner162a6882015-04-03 02:01:28 +020013#include <linux/tick.h>
Amerigo Wang9d62dcd2009-05-11 22:05:28 -040014#include <linux/random.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030015#include <linux/user-return-notifier.h>
Andy Isaacson814e2c82009-12-08 00:29:42 -080016#include <linux/dmi.h>
17#include <linux/utsname.h>
Richard Weinberger90e24012012-03-25 23:00:04 +020018#include <linux/stackprotector.h>
19#include <linux/tick.h>
20#include <linux/cpuidle.h>
Arjan van de Ven61613522009-09-17 16:11:28 +020021#include <trace/events/power.h>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020022#include <linux/hw_breakpoint.h>
Borislav Petkov93789b32011-01-20 15:42:52 +010023#include <asm/cpu.h>
Ivan Vecerad3ec5ca2008-11-11 14:33:44 +010024#include <asm/apic.h>
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +053025#include <asm/syscalls.h>
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080026#include <asm/idle.h>
27#include <asm/uaccess.h>
Len Brownb2531492014-01-15 00:37:34 -050028#include <asm/mwait.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020029#include <asm/fpu/internal.h>
K.Prasad66cb5912009-06-01 23:44:55 +053030#include <asm/debugreg.h>
Richard Weinberger90e24012012-03-25 23:00:04 +020031#include <asm/nmi.h>
Andy Lutomirski375074c2014-10-24 15:58:07 -070032#include <asm/tlbflush.h>
Ashok Raj8838eb62015-08-12 18:29:40 +020033#include <asm/mce.h>
Brian Gerst9fda6a02015-07-29 01:41:16 -040034#include <asm/vm86.h>
Brian Gerst7b32aea2016-08-13 12:38:18 -040035#include <asm/switch_to.h>
Richard Weinberger90e24012012-03-25 23:00:04 +020036
Thomas Gleixner45046892012-05-03 09:03:01 +000037/*
38 * per-CPU TSS segments. Threads are completely 'soft' on Linux,
39 * no more per-task TSS's. The TSS size is kept cacheline-aligned
40 * so they are allowed to end up in the .data..cacheline_aligned
41 * section. Since TSS's are completely CPU-local, we want them
42 * on exact cacheline boundaries, to eliminate cacheline ping-pong.
43 */
Richard Fellner13be4482017-05-04 14:26:50 +020044__visible DEFINE_PER_CPU_SHARED_ALIGNED_USER_MAPPED(struct tss_struct, cpu_tss) = {
Andy Lutomirskid0a0de22015-03-05 19:19:06 -080045 .x86_tss = {
Andy Lutomirskid9e05cc2015-03-10 11:05:59 -070046 .sp0 = TOP_OF_INIT_STACK,
Andy Lutomirskid0a0de22015-03-05 19:19:06 -080047#ifdef CONFIG_X86_32
48 .ss0 = __KERNEL_DS,
49 .ss1 = __KERNEL_CS,
50 .io_bitmap_base = INVALID_IO_BITMAP_OFFSET,
51#endif
52 },
53#ifdef CONFIG_X86_32
54 /*
55 * Note that the .io_bitmap member must be extra-big. This is because
56 * the CPU will access an additional byte beyond the end of the IO
57 * permission bitmap. The extra byte must be all 1 bits, and must
58 * be within the limit.
59 */
60 .io_bitmap = { [0 ... IO_BITMAP_LONGS] = ~0 },
61#endif
Andy Lutomirski2a41aa42016-03-09 19:00:33 -080062#ifdef CONFIG_X86_32
63 .SYSENTER_stack_canary = STACK_END_MAGIC,
64#endif
Andy Lutomirskid0a0de22015-03-05 19:19:06 -080065};
Marc Dionnede71ad22015-05-04 15:16:44 -030066EXPORT_PER_CPU_SYMBOL(cpu_tss);
Thomas Gleixner45046892012-05-03 09:03:01 +000067
Richard Weinberger90e24012012-03-25 23:00:04 +020068#ifdef CONFIG_X86_64
69static DEFINE_PER_CPU(unsigned char, is_idle);
Richard Weinberger90e24012012-03-25 23:00:04 +020070#endif
Zhao Yakuic1e3b372008-06-24 17:58:53 +080071
Suresh Siddha55ccf3f2012-05-16 15:03:51 -070072/*
73 * this gets called so that we can store lazy state into memory and copy the
74 * current task into the new thread.
75 */
Suresh Siddha61c46282008-03-10 15:28:04 -070076int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
77{
Ingo Molnar5aaeb5c2015-07-17 12:28:12 +020078 memcpy(dst, src, arch_task_struct_size);
Andy Lutomirski2459ee82015-10-30 22:42:46 -070079#ifdef CONFIG_VM86
80 dst->thread.vm86 = NULL;
81#endif
Oleg Nesterovf1853502014-09-02 19:57:23 +020082
Ingo Molnarc69e0982015-04-24 02:07:15 +020083 return fpu__copy(&dst->thread.fpu, &src->thread.fpu);
Suresh Siddha61c46282008-03-10 15:28:04 -070084}
Peter Zijlstra7f424a82008-04-25 17:39:01 +020085
Thomas Gleixner00dba562008-06-09 18:35:28 +020086/*
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080087 * Free current thread data structures etc..
88 */
Jiri Slabye6464692016-05-20 17:00:20 -070089void exit_thread(struct task_struct *tsk)
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080090{
Jiri Slabye6464692016-05-20 17:00:20 -070091 struct thread_struct *t = &tsk->thread;
Thomas Gleixner250981e2009-03-16 13:07:21 +010092 unsigned long *bp = t->io_bitmap_ptr;
Ingo Molnarca6787b2015-04-23 12:33:50 +020093 struct fpu *fpu = &t->fpu;
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080094
Thomas Gleixner250981e2009-03-16 13:07:21 +010095 if (bp) {
Andy Lutomirski24933b82015-03-05 19:19:05 -080096 struct tss_struct *tss = &per_cpu(cpu_tss, get_cpu());
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080097
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080098 t->io_bitmap_ptr = NULL;
99 clear_thread_flag(TIF_IO_BITMAP);
100 /*
101 * Careful, clear this in the TSS too:
102 */
103 memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
104 t->io_bitmap_max = 0;
105 put_cpu();
Thomas Gleixner250981e2009-03-16 13:07:21 +0100106 kfree(bp);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800107 }
Suresh Siddha1dcc8d72012-05-16 15:03:54 -0700108
Brian Gerst9fda6a02015-07-29 01:41:16 -0400109 free_vm86(t);
110
Ingo Molnar50338612015-04-29 19:04:31 +0200111 fpu__drop(fpu);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800112}
113
114void flush_thread(void)
115{
116 struct task_struct *tsk = current;
117
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200118 flush_ptrace_hw_breakpoint(tsk);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800119 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
Oleg Nesterov110d7f72015-01-19 19:52:12 +0100120
Ingo Molnar04c8e012015-04-29 20:35:33 +0200121 fpu__clear(&tsk->thread.fpu);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800122}
123
124static void hard_disable_TSC(void)
125{
Andy Lutomirski375074c2014-10-24 15:58:07 -0700126 cr4_set_bits(X86_CR4_TSD);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800127}
128
129void disable_TSC(void)
130{
131 preempt_disable();
132 if (!test_and_set_thread_flag(TIF_NOTSC))
133 /*
134 * Must flip the CPU state synchronously with
135 * TIF_NOTSC in the current running context.
136 */
137 hard_disable_TSC();
138 preempt_enable();
139}
140
141static void hard_enable_TSC(void)
142{
Andy Lutomirski375074c2014-10-24 15:58:07 -0700143 cr4_clear_bits(X86_CR4_TSD);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800144}
145
146static void enable_TSC(void)
147{
148 preempt_disable();
149 if (test_and_clear_thread_flag(TIF_NOTSC))
150 /*
151 * Must flip the CPU state synchronously with
152 * TIF_NOTSC in the current running context.
153 */
154 hard_enable_TSC();
155 preempt_enable();
156}
157
158int get_tsc_mode(unsigned long adr)
159{
160 unsigned int val;
161
162 if (test_thread_flag(TIF_NOTSC))
163 val = PR_TSC_SIGSEGV;
164 else
165 val = PR_TSC_ENABLE;
166
167 return put_user(val, (unsigned int __user *)adr);
168}
169
170int set_tsc_mode(unsigned int val)
171{
172 if (val == PR_TSC_SIGSEGV)
173 disable_TSC();
174 else if (val == PR_TSC_ENABLE)
175 enable_TSC();
176 else
177 return -EINVAL;
178
179 return 0;
180}
181
182void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
183 struct tss_struct *tss)
184{
185 struct thread_struct *prev, *next;
186
187 prev = &prev_p->thread;
188 next = &next_p->thread;
189
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100190 if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^
191 test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) {
192 unsigned long debugctl = get_debugctlmsr();
193
194 debugctl &= ~DEBUGCTLMSR_BTF;
195 if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP))
196 debugctl |= DEBUGCTLMSR_BTF;
197
198 update_debugctlmsr(debugctl);
199 }
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800200
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800201 if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
202 test_tsk_thread_flag(next_p, TIF_NOTSC)) {
203 /* prev and next are different */
204 if (test_tsk_thread_flag(next_p, TIF_NOTSC))
205 hard_disable_TSC();
206 else
207 hard_enable_TSC();
208 }
209
210 if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
211 /*
212 * Copy the relevant range of the IO bitmap.
213 * Normally this is 128 bytes or less:
214 */
215 memcpy(tss->io_bitmap, next->io_bitmap_ptr,
216 max(prev->io_bitmap_max, next->io_bitmap_max));
217 } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
218 /*
219 * Clear any possible leftover bits:
220 */
221 memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
222 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300223 propagate_user_return_notify(prev_p, next_p);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800224}
225
Brian Gerstdf59e7b2009-12-09 12:34:44 -0500226/*
Thomas Gleixner00dba562008-06-09 18:35:28 +0200227 * Idle related variables and functions
228 */
Thomas Renningerd1896042010-11-03 17:06:14 +0100229unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
Thomas Gleixner00dba562008-06-09 18:35:28 +0200230EXPORT_SYMBOL(boot_option_idle_override);
231
Len Browna476bda2013-02-09 21:45:03 -0500232static void (*x86_idle)(void);
Thomas Gleixner00dba562008-06-09 18:35:28 +0200233
Richard Weinberger90e24012012-03-25 23:00:04 +0200234#ifndef CONFIG_SMP
235static inline void play_dead(void)
236{
237 BUG();
238}
239#endif
240
241#ifdef CONFIG_X86_64
242void enter_idle(void)
243{
Alex Shic6ae41e2012-05-11 15:35:27 +0800244 this_cpu_write(is_idle, 1);
Todd Poynor5ee34122011-06-15 17:21:57 -0700245 idle_notifier_call_chain(IDLE_START);
Richard Weinberger90e24012012-03-25 23:00:04 +0200246}
247
248static void __exit_idle(void)
249{
250 if (x86_test_and_clear_bit_percpu(0, is_idle) == 0)
251 return;
Todd Poynor5ee34122011-06-15 17:21:57 -0700252 idle_notifier_call_chain(IDLE_END);
Richard Weinberger90e24012012-03-25 23:00:04 +0200253}
254
255/* Called from interrupts to signify idle end */
256void exit_idle(void)
257{
258 /* idle loop has pid 0 */
259 if (current->pid)
260 return;
261 __exit_idle();
262}
263#endif
264
Thomas Gleixner7d1a9412013-03-21 22:50:03 +0100265void arch_cpu_idle_enter(void)
266{
267 local_touch_nmi();
268 enter_idle();
269}
Richard Weinberger90e24012012-03-25 23:00:04 +0200270
Thomas Gleixner7d1a9412013-03-21 22:50:03 +0100271void arch_cpu_idle_exit(void)
272{
273 __exit_idle();
274}
Richard Weinberger90e24012012-03-25 23:00:04 +0200275
Thomas Gleixner7d1a9412013-03-21 22:50:03 +0100276void arch_cpu_idle_dead(void)
277{
278 play_dead();
Richard Weinberger90e24012012-03-25 23:00:04 +0200279}
280
Thomas Gleixner00dba562008-06-09 18:35:28 +0200281/*
Thomas Gleixner7d1a9412013-03-21 22:50:03 +0100282 * Called from the generic idle code.
283 */
284void arch_cpu_idle(void)
285{
Nicolas Pitre16f8b052014-01-29 12:45:12 -0500286 x86_idle();
Thomas Gleixner7d1a9412013-03-21 22:50:03 +0100287}
288
289/*
290 * We use this if we don't have any better idle routine..
Thomas Gleixner00dba562008-06-09 18:35:28 +0200291 */
Chris Metcalf6727ad92016-10-07 17:02:55 -0700292void __cpuidle default_idle(void)
Thomas Gleixner00dba562008-06-09 18:35:28 +0200293{
Daniel Lezcano4d0e42c2012-10-25 18:13:11 +0200294 trace_cpu_idle_rcuidle(1, smp_processor_id());
Thomas Gleixner7d1a9412013-03-21 22:50:03 +0100295 safe_halt();
Daniel Lezcano4d0e42c2012-10-25 18:13:11 +0200296 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
Thomas Gleixner00dba562008-06-09 18:35:28 +0200297}
Andy Whitcroft60b8b1d2011-06-14 12:45:10 -0700298#ifdef CONFIG_APM_MODULE
Thomas Gleixner00dba562008-06-09 18:35:28 +0200299EXPORT_SYMBOL(default_idle);
300#endif
301
Len Brown6a377dd2013-02-09 23:08:07 -0500302#ifdef CONFIG_XEN
303bool xen_set_default_idle(void)
Konrad Rzeszutek Wilke5fd47b2011-11-21 18:02:02 -0500304{
Len Browna476bda2013-02-09 21:45:03 -0500305 bool ret = !!x86_idle;
Konrad Rzeszutek Wilke5fd47b2011-11-21 18:02:02 -0500306
Len Browna476bda2013-02-09 21:45:03 -0500307 x86_idle = default_idle;
Konrad Rzeszutek Wilke5fd47b2011-11-21 18:02:02 -0500308
309 return ret;
310}
Len Brown6a377dd2013-02-09 23:08:07 -0500311#endif
Ivan Vecerad3ec5ca2008-11-11 14:33:44 +0100312void stop_this_cpu(void *dummy)
313{
314 local_irq_disable();
315 /*
316 * Remove this CPU:
317 */
Rusty Russell4f062892009-03-13 14:49:54 +1030318 set_cpu_online(smp_processor_id(), false);
Ivan Vecerad3ec5ca2008-11-11 14:33:44 +0100319 disable_local_APIC();
Ashok Raj8838eb62015-08-12 18:29:40 +0200320 mcheck_cpu_clear(this_cpu_ptr(&cpu_info));
Ivan Vecerad3ec5ca2008-11-11 14:33:44 +0100321
Len Brown27be4572013-02-10 02:28:46 -0500322 for (;;)
323 halt();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200324}
325
Len Brown02c68a02011-04-01 16:59:53 -0400326bool amd_e400_c1e_detected;
327EXPORT_SYMBOL(amd_e400_c1e_detected);
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200328
Len Brown02c68a02011-04-01 16:59:53 -0400329static cpumask_var_t amd_e400_c1e_mask;
Thomas Gleixner4faac972008-09-22 18:54:29 +0200330
Len Brown02c68a02011-04-01 16:59:53 -0400331void amd_e400_remove_cpu(int cpu)
Thomas Gleixner4faac972008-09-22 18:54:29 +0200332{
Len Brown02c68a02011-04-01 16:59:53 -0400333 if (amd_e400_c1e_mask != NULL)
334 cpumask_clear_cpu(cpu, amd_e400_c1e_mask);
Thomas Gleixner4faac972008-09-22 18:54:29 +0200335}
336
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200337/*
Len Brown02c68a02011-04-01 16:59:53 -0400338 * AMD Erratum 400 aware idle routine. We check for C1E active in the interrupt
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200339 * pending message MSR. If we detect C1E, then we handle it the same
340 * way as C3 power states (local apic timer and TSC stop)
341 */
Len Brown02c68a02011-04-01 16:59:53 -0400342static void amd_e400_idle(void)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200343{
Len Brown02c68a02011-04-01 16:59:53 -0400344 if (!amd_e400_c1e_detected) {
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200345 u32 lo, hi;
346
347 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
Michal Schmidte8c534e2010-07-27 18:53:35 +0200348
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200349 if (lo & K8_INTP_C1E_ACTIVE_MASK) {
Len Brown02c68a02011-04-01 16:59:53 -0400350 amd_e400_c1e_detected = true;
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800351 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
Andreas Herrmann09bfeea2008-09-18 21:12:10 +0200352 mark_tsc_unstable("TSC halt in AMD C1E");
Joe Perchesc767a542012-05-21 19:50:07 -0700353 pr_info("System has AMD C1E enabled\n");
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200354 }
355 }
356
Len Brown02c68a02011-04-01 16:59:53 -0400357 if (amd_e400_c1e_detected) {
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200358 int cpu = smp_processor_id();
359
Len Brown02c68a02011-04-01 16:59:53 -0400360 if (!cpumask_test_cpu(cpu, amd_e400_c1e_mask)) {
361 cpumask_set_cpu(cpu, amd_e400_c1e_mask);
Thomas Gleixner162a6882015-04-03 02:01:28 +0200362 /* Force broadcast so ACPI can not interfere. */
363 tick_broadcast_force();
Joe Perchesc767a542012-05-21 19:50:07 -0700364 pr_info("Switch to broadcast mode on CPU%d\n", cpu);
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200365 }
Thomas Gleixner435c3502015-04-03 02:05:53 +0200366 tick_broadcast_enter();
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200367
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200368 default_idle();
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200369
370 /*
371 * The switch back from broadcast mode needs to be
372 * called with interrupts disabled.
373 */
Peter Zijlstraea811742013-09-11 12:43:13 +0200374 local_irq_disable();
Thomas Gleixner435c3502015-04-03 02:05:53 +0200375 tick_broadcast_exit();
Peter Zijlstraea811742013-09-11 12:43:13 +0200376 local_irq_enable();
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200377 } else
378 default_idle();
379}
380
Len Brownb2531492014-01-15 00:37:34 -0500381/*
382 * Intel Core2 and older machines prefer MWAIT over HALT for C1.
383 * We can't rely on cpuidle installing MWAIT, because it will not load
384 * on systems that support only C1 -- so the boot default must be MWAIT.
385 *
386 * Some AMD machines are the opposite, they depend on using HALT.
387 *
388 * So for default C1, which is used during boot until cpuidle loads,
389 * use MWAIT-C1 on Intel HW that has it, else use HALT.
390 */
391static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c)
392{
393 if (c->x86_vendor != X86_VENDOR_INTEL)
394 return 0;
395
Peter Zijlstra08e237f2016-07-18 11:41:10 -0700396 if (!cpu_has(c, X86_FEATURE_MWAIT) || static_cpu_has_bug(X86_BUG_MONITOR))
Len Brownb2531492014-01-15 00:37:34 -0500397 return 0;
398
399 return 1;
400}
401
402/*
Huang Rui0fb03282015-05-26 10:28:09 +0200403 * MONITOR/MWAIT with no hints, used for default C1 state. This invokes MWAIT
404 * with interrupts enabled and no flags, which is backwards compatible with the
405 * original MWAIT implementation.
Len Brownb2531492014-01-15 00:37:34 -0500406 */
Chris Metcalf6727ad92016-10-07 17:02:55 -0700407static __cpuidle void mwait_idle(void)
Len Brownb2531492014-01-15 00:37:34 -0500408{
Mike Galbraithf8e617f2014-01-18 17:14:44 +0100409 if (!current_set_polling_and_test()) {
Jisheng Zhange43d0182015-08-20 12:54:39 +0800410 trace_cpu_idle_rcuidle(1, smp_processor_id());
Mike Galbraithf8e617f2014-01-18 17:14:44 +0100411 if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) {
Michael S. Tsirkinca598092016-01-28 19:02:51 +0200412 mb(); /* quirk */
Len Brownb2531492014-01-15 00:37:34 -0500413 clflush((void *)&current_thread_info()->flags);
Michael S. Tsirkinca598092016-01-28 19:02:51 +0200414 mb(); /* quirk */
Mike Galbraithf8e617f2014-01-18 17:14:44 +0100415 }
Len Brownb2531492014-01-15 00:37:34 -0500416
417 __monitor((void *)&current_thread_info()->flags, 0, 0);
Len Brownb2531492014-01-15 00:37:34 -0500418 if (!need_resched())
419 __sti_mwait(0, 0);
420 else
421 local_irq_enable();
Jisheng Zhange43d0182015-08-20 12:54:39 +0800422 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
Mike Galbraithf8e617f2014-01-18 17:14:44 +0100423 } else {
Len Brownb2531492014-01-15 00:37:34 -0500424 local_irq_enable();
Mike Galbraithf8e617f2014-01-18 17:14:44 +0100425 }
426 __current_clr_polling();
Len Brownb2531492014-01-15 00:37:34 -0500427}
428
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400429void select_idle_routine(const struct cpuinfo_x86 *c)
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200430{
Ingo Molnar3e5095d2009-01-27 17:07:08 +0100431#ifdef CONFIG_SMP
Thomas Gleixner7d1a9412013-03-21 22:50:03 +0100432 if (boot_option_idle_override == IDLE_POLL && smp_num_siblings > 1)
Joe Perchesc767a542012-05-21 19:50:07 -0700433 pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n");
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200434#endif
Thomas Gleixner7d1a9412013-03-21 22:50:03 +0100435 if (x86_idle || boot_option_idle_override == IDLE_POLL)
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200436 return;
437
Thomas Gleixnerbd7e7692016-12-09 19:29:09 +0100438 if (boot_cpu_has_bug(X86_BUG_AMD_E400)) {
Joe Perchesc767a542012-05-21 19:50:07 -0700439 pr_info("using AMD E400 aware idle routine\n");
Len Browna476bda2013-02-09 21:45:03 -0500440 x86_idle = amd_e400_idle;
Len Brownb2531492014-01-15 00:37:34 -0500441 } else if (prefer_mwait_c1_over_halt(c)) {
442 pr_info("using mwait in idle threads\n");
443 x86_idle = mwait_idle;
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200444 } else
Len Browna476bda2013-02-09 21:45:03 -0500445 x86_idle = default_idle;
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200446}
447
Len Brown02c68a02011-04-01 16:59:53 -0400448void __init init_amd_e400_c1e_mask(void)
Rusty Russell30e1e6d2009-03-17 14:50:34 +1030449{
Len Brown02c68a02011-04-01 16:59:53 -0400450 /* If we're using amd_e400_idle, we need to allocate amd_e400_c1e_mask. */
Len Browna476bda2013-02-09 21:45:03 -0500451 if (x86_idle == amd_e400_idle)
Len Brown02c68a02011-04-01 16:59:53 -0400452 zalloc_cpumask_var(&amd_e400_c1e_mask, GFP_KERNEL);
Rusty Russell30e1e6d2009-03-17 14:50:34 +1030453}
454
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200455static int __init idle_setup(char *str)
456{
Cyrill Gorcunovab6bc3e2008-07-05 15:53:36 +0400457 if (!str)
458 return -EINVAL;
459
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200460 if (!strcmp(str, "poll")) {
Joe Perchesc767a542012-05-21 19:50:07 -0700461 pr_info("using polling idle threads\n");
Thomas Renningerd1896042010-11-03 17:06:14 +0100462 boot_option_idle_override = IDLE_POLL;
Thomas Gleixner7d1a9412013-03-21 22:50:03 +0100463 cpu_idle_poll_ctrl(true);
Thomas Renningerd1896042010-11-03 17:06:14 +0100464 } else if (!strcmp(str, "halt")) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800465 /*
466 * When the boot option of idle=halt is added, halt is
467 * forced to be used for CPU idle. In such case CPU C2/C3
468 * won't be used again.
469 * To continue to load the CPU idle driver, don't touch
470 * the boot_option_idle_override.
471 */
Len Browna476bda2013-02-09 21:45:03 -0500472 x86_idle = default_idle;
Thomas Renningerd1896042010-11-03 17:06:14 +0100473 boot_option_idle_override = IDLE_HALT;
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800474 } else if (!strcmp(str, "nomwait")) {
475 /*
476 * If the boot option of "idle=nomwait" is added,
477 * it means that mwait will be disabled for CPU C2/C3
478 * states. In such case it won't touch the variable
479 * of boot_option_idle_override.
480 */
Thomas Renningerd1896042010-11-03 17:06:14 +0100481 boot_option_idle_override = IDLE_NOMWAIT;
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800482 } else
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200483 return -1;
484
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200485 return 0;
486}
487early_param("idle", idle_setup);
488
Amerigo Wang9d62dcd2009-05-11 22:05:28 -0400489unsigned long arch_align_stack(unsigned long sp)
490{
491 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
492 sp -= get_random_int() % 8192;
493 return sp & ~0xf;
494}
495
496unsigned long arch_randomize_brk(struct mm_struct *mm)
497{
Jason Cooper9c6f0902016-10-11 13:53:56 -0700498 return randomize_page(mm->brk, 0x02000000);
Amerigo Wang9d62dcd2009-05-11 22:05:28 -0400499}
500
Thomas Gleixner7ba78052015-09-30 08:38:23 +0000501/*
Brian Gerstffcb0432016-08-13 12:38:21 -0400502 * Return saved PC of a blocked thread.
503 * What is this good for? it will be always the scheduler or ret_from_fork.
504 */
505unsigned long thread_saved_pc(struct task_struct *tsk)
506{
507 struct inactive_task_frame *frame =
508 (struct inactive_task_frame *) READ_ONCE(tsk->thread.sp);
509 return READ_ONCE_NOCHECK(frame->ret_addr);
510}
511
512/*
Thomas Gleixner7ba78052015-09-30 08:38:23 +0000513 * Called from fs/proc with a reference on @p to find the function
514 * which called into schedule(). This needs to be done carefully
515 * because the task might wake up and we might look at a stack
516 * changing under us.
517 */
518unsigned long get_wchan(struct task_struct *p)
519{
Andy Lutomirski74327a32016-09-15 22:45:46 -0700520 unsigned long start, bottom, top, sp, fp, ip, ret = 0;
Thomas Gleixner7ba78052015-09-30 08:38:23 +0000521 int count = 0;
522
523 if (!p || p == current || p->state == TASK_RUNNING)
524 return 0;
525
Andy Lutomirski74327a32016-09-15 22:45:46 -0700526 if (!try_get_task_stack(p))
527 return 0;
528
Thomas Gleixner7ba78052015-09-30 08:38:23 +0000529 start = (unsigned long)task_stack_page(p);
530 if (!start)
Andy Lutomirski74327a32016-09-15 22:45:46 -0700531 goto out;
Thomas Gleixner7ba78052015-09-30 08:38:23 +0000532
533 /*
534 * Layout of the stack page:
535 *
536 * ----------- topmax = start + THREAD_SIZE - sizeof(unsigned long)
537 * PADDING
538 * ----------- top = topmax - TOP_OF_KERNEL_STACK_PADDING
539 * stack
Andy Lutomirski15f4eae2016-09-13 14:29:25 -0700540 * ----------- bottom = start
Thomas Gleixner7ba78052015-09-30 08:38:23 +0000541 *
542 * The tasks stack pointer points at the location where the
543 * framepointer is stored. The data on the stack is:
544 * ... IP FP ... IP FP
545 *
546 * We need to read FP and IP, so we need to adjust the upper
547 * bound by another unsigned long.
548 */
549 top = start + THREAD_SIZE - TOP_OF_KERNEL_STACK_PADDING;
550 top -= 2 * sizeof(unsigned long);
Andy Lutomirski15f4eae2016-09-13 14:29:25 -0700551 bottom = start;
Thomas Gleixner7ba78052015-09-30 08:38:23 +0000552
553 sp = READ_ONCE(p->thread.sp);
554 if (sp < bottom || sp > top)
Andy Lutomirski74327a32016-09-15 22:45:46 -0700555 goto out;
Thomas Gleixner7ba78052015-09-30 08:38:23 +0000556
Brian Gerst7b32aea2016-08-13 12:38:18 -0400557 fp = READ_ONCE_NOCHECK(((struct inactive_task_frame *)sp)->bp);
Thomas Gleixner7ba78052015-09-30 08:38:23 +0000558 do {
559 if (fp < bottom || fp > top)
Andy Lutomirski74327a32016-09-15 22:45:46 -0700560 goto out;
Andrey Ryabininf7d27c32015-10-19 11:37:18 +0300561 ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
Andy Lutomirski74327a32016-09-15 22:45:46 -0700562 if (!in_sched_functions(ip)) {
563 ret = ip;
564 goto out;
565 }
Andrey Ryabininf7d27c32015-10-19 11:37:18 +0300566 fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
Thomas Gleixner7ba78052015-09-30 08:38:23 +0000567 } while (count++ < 16 && p->state != TASK_RUNNING);
Andy Lutomirski74327a32016-09-15 22:45:46 -0700568
569out:
570 put_task_stack(p);
571 return ret;
Thomas Gleixner7ba78052015-09-30 08:38:23 +0000572}