1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
3 #include <linux/errno.h>
4 #include <linux/kernel.h>
5 #include <linux/mm.h>
6 #include <linux/smp.h>
7 #include <linux/prctl.h>
8 #include <linux/slab.h>
9 #include <linux/sched.h>
10 #include <linux/module.h>
11 #include <linux/pm.h>
12 #include <linux/clockchips.h>
13 #include <linux/random.h>
14 #include <linux/user-return-notifier.h>
15 #include <linux/dmi.h>
16 #include <linux/utsname.h>
17 #include <linux/stackprotector.h>
18 #include <linux/tick.h>
19 #include <linux/cpuidle.h>
20 #include <trace/events/power.h>
21 #include <linux/hw_breakpoint.h>
22 #include <asm/cpu.h>
23 #include <asm/apic.h>
24 #include <asm/syscalls.h>
25 #include <asm/idle.h>
26 #include <asm/uaccess.h>
27 #include <asm/mwait.h>
28 #include <asm/i387.h>
29 #include <asm/fpu-internal.h>
30 #include <asm/debugreg.h>
31 #include <asm/nmi.h>
32 #include <asm/tlbflush.h>
33
34 /*
35 * per-CPU TSS segments. Threads are completely 'soft' on Linux,
36 * no more per-task TSS's. The TSS size is kept cacheline-aligned
37 * so they are allowed to end up in the .data..cacheline_aligned
38 * section. Since TSS's are completely CPU-local, we want them
39 * on exact cacheline boundaries, to eliminate cacheline ping-pong.
40 */
41 __visible DEFINE_PER_CPU_SHARED_ALIGNED(struct tss_struct, init_tss) = INIT_TSS;
42
43 #ifdef CONFIG_X86_64
44 static DEFINE_PER_CPU(unsigned char, is_idle);
45 #endif
46
47 struct kmem_cache *task_xstate_cachep;
48 EXPORT_SYMBOL_GPL(task_xstate_cachep);
49
50 /*
51 * this gets called so that we can store lazy state into memory and copy the
52 * current task into the new thread.
53 */
arch_dup_task_struct(struct task_struct * dst,struct task_struct * src)54 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
55 {
56 *dst = *src;
57
58 dst->thread.fpu_counter = 0;
59 dst->thread.fpu.has_fpu = 0;
60 dst->thread.fpu.last_cpu = ~0;
61 dst->thread.fpu.state = NULL;
62 if (tsk_used_math(src)) {
63 int err = fpu_alloc(&dst->thread.fpu);
64 if (err)
65 return err;
66 fpu_copy(dst, src);
67 }
68 return 0;
69 }
70
free_thread_xstate(struct task_struct * tsk)71 void free_thread_xstate(struct task_struct *tsk)
72 {
73 fpu_free(&tsk->thread.fpu);
74 }
75
arch_release_task_struct(struct task_struct * tsk)76 void arch_release_task_struct(struct task_struct *tsk)
77 {
78 free_thread_xstate(tsk);
79 }
80
arch_task_cache_init(void)81 void arch_task_cache_init(void)
82 {
83 task_xstate_cachep =
84 kmem_cache_create("task_xstate", xstate_size,
85 __alignof__(union thread_xstate),
86 SLAB_PANIC | SLAB_NOTRACK, NULL);
87 setup_xstate_comp();
88 }
89
90 /*
91 * Free current thread data structures etc..
92 */
exit_thread(struct task_struct * tsk)93 void exit_thread(struct task_struct *tsk)
94 {
95 struct thread_struct *t = &tsk->thread;
96 unsigned long *bp = t->io_bitmap_ptr;
97
98 if (bp) {
99 struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
100
101 t->io_bitmap_ptr = NULL;
102 clear_thread_flag(TIF_IO_BITMAP);
103 /*
104 * Careful, clear this in the TSS too:
105 */
106 memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
107 t->io_bitmap_max = 0;
108 put_cpu();
109 kfree(bp);
110 }
111
112 drop_fpu(tsk);
113 }
114
flush_thread(void)115 void flush_thread(void)
116 {
117 struct task_struct *tsk = current;
118
119 flush_ptrace_hw_breakpoint(tsk);
120 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
121 drop_init_fpu(tsk);
122 /*
123 * Free the FPU state for non xsave platforms. They get reallocated
124 * lazily at the first use.
125 */
126 if (!use_eager_fpu())
127 free_thread_xstate(tsk);
128 }
129
hard_disable_TSC(void)130 static void hard_disable_TSC(void)
131 {
132 cr4_set_bits(X86_CR4_TSD);
133 }
134
disable_TSC(void)135 void disable_TSC(void)
136 {
137 preempt_disable();
138 if (!test_and_set_thread_flag(TIF_NOTSC))
139 /*
140 * Must flip the CPU state synchronously with
141 * TIF_NOTSC in the current running context.
142 */
143 hard_disable_TSC();
144 preempt_enable();
145 }
146
hard_enable_TSC(void)147 static void hard_enable_TSC(void)
148 {
149 cr4_clear_bits(X86_CR4_TSD);
150 }
151
enable_TSC(void)152 static void enable_TSC(void)
153 {
154 preempt_disable();
155 if (test_and_clear_thread_flag(TIF_NOTSC))
156 /*
157 * Must flip the CPU state synchronously with
158 * TIF_NOTSC in the current running context.
159 */
160 hard_enable_TSC();
161 preempt_enable();
162 }
163
get_tsc_mode(unsigned long adr)164 int get_tsc_mode(unsigned long adr)
165 {
166 unsigned int val;
167
168 if (test_thread_flag(TIF_NOTSC))
169 val = PR_TSC_SIGSEGV;
170 else
171 val = PR_TSC_ENABLE;
172
173 return put_user(val, (unsigned int __user *)adr);
174 }
175
set_tsc_mode(unsigned int val)176 int set_tsc_mode(unsigned int val)
177 {
178 if (val == PR_TSC_SIGSEGV)
179 disable_TSC();
180 else if (val == PR_TSC_ENABLE)
181 enable_TSC();
182 else
183 return -EINVAL;
184
185 return 0;
186 }
187
__switch_to_xtra(struct task_struct * prev_p,struct task_struct * next_p,struct tss_struct * tss)188 void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
189 struct tss_struct *tss)
190 {
191 struct thread_struct *prev, *next;
192
193 prev = &prev_p->thread;
194 next = &next_p->thread;
195
196 if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^
197 test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) {
198 unsigned long debugctl = get_debugctlmsr();
199
200 debugctl &= ~DEBUGCTLMSR_BTF;
201 if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP))
202 debugctl |= DEBUGCTLMSR_BTF;
203
204 update_debugctlmsr(debugctl);
205 }
206
207 if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
208 test_tsk_thread_flag(next_p, TIF_NOTSC)) {
209 /* prev and next are different */
210 if (test_tsk_thread_flag(next_p, TIF_NOTSC))
211 hard_disable_TSC();
212 else
213 hard_enable_TSC();
214 }
215
216 if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
217 /*
218 * Copy the relevant range of the IO bitmap.
219 * Normally this is 128 bytes or less:
220 */
221 memcpy(tss->io_bitmap, next->io_bitmap_ptr,
222 max(prev->io_bitmap_max, next->io_bitmap_max));
223 } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
224 /*
225 * Clear any possible leftover bits:
226 */
227 memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
228 }
229 propagate_user_return_notify(prev_p, next_p);
230 }
231
232 /*
233 * Idle related variables and functions
234 */
235 unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
236 EXPORT_SYMBOL(boot_option_idle_override);
237
238 static void (*x86_idle)(void);
239
240 #ifndef CONFIG_SMP
play_dead(void)241 static inline void play_dead(void)
242 {
243 BUG();
244 }
245 #endif
246
247 #ifdef CONFIG_X86_64
enter_idle(void)248 void enter_idle(void)
249 {
250 this_cpu_write(is_idle, 1);
251 idle_notifier_call_chain(IDLE_START);
252 }
253
__exit_idle(void)254 static void __exit_idle(void)
255 {
256 if (x86_test_and_clear_bit_percpu(0, is_idle) == 0)
257 return;
258 idle_notifier_call_chain(IDLE_END);
259 }
260
261 /* Called from interrupts to signify idle end */
exit_idle(void)262 void exit_idle(void)
263 {
264 /* idle loop has pid 0 */
265 if (current->pid)
266 return;
267 __exit_idle();
268 }
269 #endif
270
arch_cpu_idle_enter(void)271 void arch_cpu_idle_enter(void)
272 {
273 local_touch_nmi();
274 enter_idle();
275 }
276
arch_cpu_idle_exit(void)277 void arch_cpu_idle_exit(void)
278 {
279 __exit_idle();
280 }
281
arch_cpu_idle_dead(void)282 void arch_cpu_idle_dead(void)
283 {
284 play_dead();
285 }
286
287 /*
288 * Called from the generic idle code.
289 */
arch_cpu_idle(void)290 void arch_cpu_idle(void)
291 {
292 x86_idle();
293 }
294
295 /*
296 * We use this if we don't have any better idle routine..
297 */
default_idle(void)298 void default_idle(void)
299 {
300 trace_cpu_idle_rcuidle(1, smp_processor_id());
301 safe_halt();
302 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
303 }
304 #ifdef CONFIG_APM_MODULE
305 EXPORT_SYMBOL(default_idle);
306 #endif
307
308 #ifdef CONFIG_XEN
xen_set_default_idle(void)309 bool xen_set_default_idle(void)
310 {
311 bool ret = !!x86_idle;
312
313 x86_idle = default_idle;
314
315 return ret;
316 }
317 #endif
stop_this_cpu(void * dummy)318 void stop_this_cpu(void *dummy)
319 {
320 local_irq_disable();
321 /*
322 * Remove this CPU:
323 */
324 set_cpu_online(smp_processor_id(), false);
325 disable_local_APIC();
326
327 for (;;)
328 halt();
329 }
330
331 bool amd_e400_c1e_detected;
332 EXPORT_SYMBOL(amd_e400_c1e_detected);
333
334 static cpumask_var_t amd_e400_c1e_mask;
335
amd_e400_remove_cpu(int cpu)336 void amd_e400_remove_cpu(int cpu)
337 {
338 if (amd_e400_c1e_mask != NULL)
339 cpumask_clear_cpu(cpu, amd_e400_c1e_mask);
340 }
341
342 /*
343 * AMD Erratum 400 aware idle routine. We check for C1E active in the interrupt
344 * pending message MSR. If we detect C1E, then we handle it the same
345 * way as C3 power states (local apic timer and TSC stop)
346 */
amd_e400_idle(void)347 static void amd_e400_idle(void)
348 {
349 if (!amd_e400_c1e_detected) {
350 u32 lo, hi;
351
352 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
353
354 if (lo & K8_INTP_C1E_ACTIVE_MASK) {
355 amd_e400_c1e_detected = true;
356 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
357 mark_tsc_unstable("TSC halt in AMD C1E");
358 pr_info("System has AMD C1E enabled\n");
359 }
360 }
361
362 if (amd_e400_c1e_detected) {
363 int cpu = smp_processor_id();
364
365 if (!cpumask_test_cpu(cpu, amd_e400_c1e_mask)) {
366 cpumask_set_cpu(cpu, amd_e400_c1e_mask);
367 /*
368 * Force broadcast so ACPI can not interfere.
369 */
370 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
371 &cpu);
372 pr_info("Switch to broadcast mode on CPU%d\n", cpu);
373 }
374 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
375
376 default_idle();
377
378 /*
379 * The switch back from broadcast mode needs to be
380 * called with interrupts disabled.
381 */
382 local_irq_disable();
383 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
384 local_irq_enable();
385 } else
386 default_idle();
387 }
388
389 /*
390 * Intel Core2 and older machines prefer MWAIT over HALT for C1.
391 * We can't rely on cpuidle installing MWAIT, because it will not load
392 * on systems that support only C1 -- so the boot default must be MWAIT.
393 *
394 * Some AMD machines are the opposite, they depend on using HALT.
395 *
396 * So for default C1, which is used during boot until cpuidle loads,
397 * use MWAIT-C1 on Intel HW that has it, else use HALT.
398 */
prefer_mwait_c1_over_halt(const struct cpuinfo_x86 * c)399 static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c)
400 {
401 if (c->x86_vendor != X86_VENDOR_INTEL)
402 return 0;
403
404 if (!cpu_has(c, X86_FEATURE_MWAIT))
405 return 0;
406
407 return 1;
408 }
409
410 /*
411 * MONITOR/MWAIT with no hints, used for default default C1 state.
412 * This invokes MWAIT with interrutps enabled and no flags,
413 * which is backwards compatible with the original MWAIT implementation.
414 */
415
mwait_idle(void)416 static void mwait_idle(void)
417 {
418 if (!current_set_polling_and_test()) {
419 if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) {
420 smp_mb(); /* quirk */
421 clflush((void *)¤t_thread_info()->flags);
422 smp_mb(); /* quirk */
423 }
424
425 __monitor((void *)¤t_thread_info()->flags, 0, 0);
426 if (!need_resched())
427 __sti_mwait(0, 0);
428 else
429 local_irq_enable();
430 } else {
431 local_irq_enable();
432 }
433 __current_clr_polling();
434 }
435
select_idle_routine(const struct cpuinfo_x86 * c)436 void select_idle_routine(const struct cpuinfo_x86 *c)
437 {
438 #ifdef CONFIG_SMP
439 if (boot_option_idle_override == IDLE_POLL && smp_num_siblings > 1)
440 pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n");
441 #endif
442 if (x86_idle || boot_option_idle_override == IDLE_POLL)
443 return;
444
445 if (cpu_has_bug(c, X86_BUG_AMD_APIC_C1E)) {
446 /* E400: APIC timer interrupt does not wake up CPU from C1e */
447 pr_info("using AMD E400 aware idle routine\n");
448 x86_idle = amd_e400_idle;
449 } else if (prefer_mwait_c1_over_halt(c)) {
450 pr_info("using mwait in idle threads\n");
451 x86_idle = mwait_idle;
452 } else
453 x86_idle = default_idle;
454 }
455
init_amd_e400_c1e_mask(void)456 void __init init_amd_e400_c1e_mask(void)
457 {
458 /* If we're using amd_e400_idle, we need to allocate amd_e400_c1e_mask. */
459 if (x86_idle == amd_e400_idle)
460 zalloc_cpumask_var(&amd_e400_c1e_mask, GFP_KERNEL);
461 }
462
idle_setup(char * str)463 static int __init idle_setup(char *str)
464 {
465 if (!str)
466 return -EINVAL;
467
468 if (!strcmp(str, "poll")) {
469 pr_info("using polling idle threads\n");
470 boot_option_idle_override = IDLE_POLL;
471 cpu_idle_poll_ctrl(true);
472 } else if (!strcmp(str, "halt")) {
473 /*
474 * When the boot option of idle=halt is added, halt is
475 * forced to be used for CPU idle. In such case CPU C2/C3
476 * won't be used again.
477 * To continue to load the CPU idle driver, don't touch
478 * the boot_option_idle_override.
479 */
480 x86_idle = default_idle;
481 boot_option_idle_override = IDLE_HALT;
482 } else if (!strcmp(str, "nomwait")) {
483 /*
484 * If the boot option of "idle=nomwait" is added,
485 * it means that mwait will be disabled for CPU C2/C3
486 * states. In such case it won't touch the variable
487 * of boot_option_idle_override.
488 */
489 boot_option_idle_override = IDLE_NOMWAIT;
490 } else
491 return -1;
492
493 return 0;
494 }
495 early_param("idle", idle_setup);
496
arch_align_stack(unsigned long sp)497 unsigned long arch_align_stack(unsigned long sp)
498 {
499 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
500 sp -= get_random_int() % 8192;
501 return sp & ~0xf;
502 }
503
arch_randomize_brk(struct mm_struct * mm)504 unsigned long arch_randomize_brk(struct mm_struct *mm)
505 {
506 unsigned long range_end = mm->brk + 0x02000000;
507 return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
508 }
509
510