]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
c767a54b JP |
2 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
3 | ||
61c4628b SS |
4 | #include <linux/errno.h> |
5 | #include <linux/kernel.h> | |
6 | #include <linux/mm.h> | |
7 | #include <linux/smp.h> | |
389d1fb1 | 8 | #include <linux/prctl.h> |
61c4628b SS |
9 | #include <linux/slab.h> |
10 | #include <linux/sched.h> | |
4c822698 | 11 | #include <linux/sched/idle.h> |
b17b0153 | 12 | #include <linux/sched/debug.h> |
29930025 | 13 | #include <linux/sched/task.h> |
68db0cf1 | 14 | #include <linux/sched/task_stack.h> |
186f4360 PG |
15 | #include <linux/init.h> |
16 | #include <linux/export.h> | |
7f424a8b | 17 | #include <linux/pm.h> |
162a688e | 18 | #include <linux/tick.h> |
9d62dcdf | 19 | #include <linux/random.h> |
7c68af6e | 20 | #include <linux/user-return-notifier.h> |
814e2c84 AI |
21 | #include <linux/dmi.h> |
22 | #include <linux/utsname.h> | |
90e24014 RW |
23 | #include <linux/stackprotector.h> |
24 | #include <linux/tick.h> | |
25 | #include <linux/cpuidle.h> | |
61613521 | 26 | #include <trace/events/power.h> |
24f1e32c | 27 | #include <linux/hw_breakpoint.h> |
93789b32 | 28 | #include <asm/cpu.h> |
d3ec5cae | 29 | #include <asm/apic.h> |
2c1b284e | 30 | #include <asm/syscalls.h> |
7c0f6ba6 | 31 | #include <linux/uaccess.h> |
b253149b | 32 | #include <asm/mwait.h> |
78f7f1e5 | 33 | #include <asm/fpu/internal.h> |
66cb5917 | 34 | #include <asm/debugreg.h> |
90e24014 | 35 | #include <asm/nmi.h> |
375074cc | 36 | #include <asm/tlbflush.h> |
8838eb6c | 37 | #include <asm/mce.h> |
9fda6a06 | 38 | #include <asm/vm86.h> |
7b32aead | 39 | #include <asm/switch_to.h> |
b7ffc44d | 40 | #include <asm/desc.h> |
e9ea1e7f | 41 | #include <asm/prctl.h> |
5407b7f8 | 42 | #include <asm/spec-ctrl.h> |
90e24014 | 43 | |
45046892 TG |
44 | /* |
45 | * per-CPU TSS segments. Threads are completely 'soft' on Linux, | |
46 | * no more per-task TSS's. The TSS size is kept cacheline-aligned | |
47 | * so they are allowed to end up in the .data..cacheline_aligned | |
48 | * section. Since TSS's are completely CPU-local, we want them | |
49 | * on exact cacheline boundaries, to eliminate cacheline ping-pong. | |
50 | */ | |
2fd9c41a | 51 | __visible DEFINE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw) = { |
d0a0de21 | 52 | .x86_tss = { |
20bb8344 AL |
53 | /* |
54 | * .sp0 is only used when entering ring 0 from a lower | |
55 | * privilege level. Since the init task never runs anything | |
56 | * but ring 0 code, there is no need for a valid value here. | |
57 | * Poison it. | |
58 | */ | |
59 | .sp0 = (1UL << (BITS_PER_LONG-1)) + 1, | |
9aaefe7b AL |
60 | |
61 | #ifdef CONFIG_X86_64 | |
62 | /* | |
63 | * .sp1 is cpu_current_top_of_stack. The init task never | |
64 | * runs user code, but cpu_current_top_of_stack should still | |
65 | * be well defined before the first context switch. | |
66 | */ | |
67 | .sp1 = TOP_OF_INIT_STACK, | |
68 | #endif | |
69 | ||
d0a0de21 AL |
70 | #ifdef CONFIG_X86_32 |
71 | .ss0 = __KERNEL_DS, | |
72 | .ss1 = __KERNEL_CS, | |
73 | .io_bitmap_base = INVALID_IO_BITMAP_OFFSET, | |
74 | #endif | |
75 | }, | |
76 | #ifdef CONFIG_X86_32 | |
77 | /* | |
78 | * Note that the .io_bitmap member must be extra-big. This is because | |
79 | * the CPU will access an additional byte beyond the end of the IO | |
80 | * permission bitmap. The extra byte must be all 1 bits, and must | |
81 | * be within the limit. | |
82 | */ | |
83 | .io_bitmap = { [0 ... IO_BITMAP_LONGS] = ~0 }, | |
84 | #endif | |
85 | }; | |
c482feef | 86 | EXPORT_PER_CPU_SYMBOL(cpu_tss_rw); |
45046892 | 87 | |
b7ceaec1 AL |
88 | DEFINE_PER_CPU(bool, __tss_limit_invalid); |
89 | EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid); | |
b7ffc44d | 90 | |
55ccf3fe SS |
91 | /* |
92 | * this gets called so that we can store lazy state into memory and copy the | |
93 | * current task into the new thread. | |
94 | */ | |
61c4628b SS |
95 | int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) |
96 | { | |
5aaeb5c0 | 97 | memcpy(dst, src, arch_task_struct_size); |
2459ee86 AL |
98 | #ifdef CONFIG_VM86 |
99 | dst->thread.vm86 = NULL; | |
100 | #endif | |
f1853505 | 101 | |
c69e098b | 102 | return fpu__copy(&dst->thread.fpu, &src->thread.fpu); |
61c4628b | 103 | } |
7f424a8b | 104 | |
389d1fb1 JF |
105 | /* |
106 | * Free current thread data structures etc.. | |
107 | */ | |
e6464694 | 108 | void exit_thread(struct task_struct *tsk) |
389d1fb1 | 109 | { |
e6464694 | 110 | struct thread_struct *t = &tsk->thread; |
250981e6 | 111 | unsigned long *bp = t->io_bitmap_ptr; |
ca6787ba | 112 | struct fpu *fpu = &t->fpu; |
389d1fb1 | 113 | |
250981e6 | 114 | if (bp) { |
c482feef | 115 | struct tss_struct *tss = &per_cpu(cpu_tss_rw, get_cpu()); |
389d1fb1 | 116 | |
389d1fb1 JF |
117 | t->io_bitmap_ptr = NULL; |
118 | clear_thread_flag(TIF_IO_BITMAP); | |
119 | /* | |
120 | * Careful, clear this in the TSS too: | |
121 | */ | |
122 | memset(tss->io_bitmap, 0xff, t->io_bitmap_max); | |
123 | t->io_bitmap_max = 0; | |
124 | put_cpu(); | |
250981e6 | 125 | kfree(bp); |
389d1fb1 | 126 | } |
1dcc8d7b | 127 | |
9fda6a06 BG |
128 | free_vm86(t); |
129 | ||
50338615 | 130 | fpu__drop(fpu); |
389d1fb1 JF |
131 | } |
132 | ||
133 | void flush_thread(void) | |
134 | { | |
135 | struct task_struct *tsk = current; | |
136 | ||
24f1e32c | 137 | flush_ptrace_hw_breakpoint(tsk); |
389d1fb1 | 138 | memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array)); |
110d7f75 | 139 | |
04c8e01d | 140 | fpu__clear(&tsk->thread.fpu); |
389d1fb1 JF |
141 | } |
142 | ||
389d1fb1 JF |
143 | void disable_TSC(void) |
144 | { | |
145 | preempt_disable(); | |
146 | if (!test_and_set_thread_flag(TIF_NOTSC)) | |
147 | /* | |
148 | * Must flip the CPU state synchronously with | |
149 | * TIF_NOTSC in the current running context. | |
150 | */ | |
5a920155 | 151 | cr4_set_bits(X86_CR4_TSD); |
389d1fb1 JF |
152 | preempt_enable(); |
153 | } | |
154 | ||
389d1fb1 JF |
155 | static void enable_TSC(void) |
156 | { | |
157 | preempt_disable(); | |
158 | if (test_and_clear_thread_flag(TIF_NOTSC)) | |
159 | /* | |
160 | * Must flip the CPU state synchronously with | |
161 | * TIF_NOTSC in the current running context. | |
162 | */ | |
5a920155 | 163 | cr4_clear_bits(X86_CR4_TSD); |
389d1fb1 JF |
164 | preempt_enable(); |
165 | } | |
166 | ||
167 | int get_tsc_mode(unsigned long adr) | |
168 | { | |
169 | unsigned int val; | |
170 | ||
171 | if (test_thread_flag(TIF_NOTSC)) | |
172 | val = PR_TSC_SIGSEGV; | |
173 | else | |
174 | val = PR_TSC_ENABLE; | |
175 | ||
176 | return put_user(val, (unsigned int __user *)adr); | |
177 | } | |
178 | ||
179 | int set_tsc_mode(unsigned int val) | |
180 | { | |
181 | if (val == PR_TSC_SIGSEGV) | |
182 | disable_TSC(); | |
183 | else if (val == PR_TSC_ENABLE) | |
184 | enable_TSC(); | |
185 | else | |
186 | return -EINVAL; | |
187 | ||
188 | return 0; | |
189 | } | |
190 | ||
e9ea1e7f KH |
191 | DEFINE_PER_CPU(u64, msr_misc_features_shadow); |
192 | ||
193 | static void set_cpuid_faulting(bool on) | |
194 | { | |
195 | u64 msrval; | |
196 | ||
197 | msrval = this_cpu_read(msr_misc_features_shadow); | |
198 | msrval &= ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT; | |
199 | msrval |= (on << MSR_MISC_FEATURES_ENABLES_CPUID_FAULT_BIT); | |
200 | this_cpu_write(msr_misc_features_shadow, msrval); | |
201 | wrmsrl(MSR_MISC_FEATURES_ENABLES, msrval); | |
202 | } | |
203 | ||
204 | static void disable_cpuid(void) | |
205 | { | |
206 | preempt_disable(); | |
207 | if (!test_and_set_thread_flag(TIF_NOCPUID)) { | |
208 | /* | |
209 | * Must flip the CPU state synchronously with | |
210 | * TIF_NOCPUID in the current running context. | |
211 | */ | |
212 | set_cpuid_faulting(true); | |
213 | } | |
214 | preempt_enable(); | |
215 | } | |
216 | ||
217 | static void enable_cpuid(void) | |
218 | { | |
219 | preempt_disable(); | |
220 | if (test_and_clear_thread_flag(TIF_NOCPUID)) { | |
221 | /* | |
222 | * Must flip the CPU state synchronously with | |
223 | * TIF_NOCPUID in the current running context. | |
224 | */ | |
225 | set_cpuid_faulting(false); | |
226 | } | |
227 | preempt_enable(); | |
228 | } | |
229 | ||
230 | static int get_cpuid_mode(void) | |
231 | { | |
232 | return !test_thread_flag(TIF_NOCPUID); | |
233 | } | |
234 | ||
235 | static int set_cpuid_mode(struct task_struct *task, unsigned long cpuid_enabled) | |
236 | { | |
237 | if (!static_cpu_has(X86_FEATURE_CPUID_FAULT)) | |
238 | return -ENODEV; | |
239 | ||
240 | if (cpuid_enabled) | |
241 | enable_cpuid(); | |
242 | else | |
243 | disable_cpuid(); | |
244 | ||
245 | return 0; | |
246 | } | |
247 | ||
248 | /* | |
249 | * Called immediately after a successful exec. | |
250 | */ | |
251 | void arch_setup_new_exec(void) | |
252 | { | |
253 | /* If cpuid was previously disabled for this task, re-enable it. */ | |
254 | if (test_thread_flag(TIF_NOCPUID)) | |
255 | enable_cpuid(); | |
256 | } | |
257 | ||
af8b3cd3 KH |
258 | static inline void switch_to_bitmap(struct tss_struct *tss, |
259 | struct thread_struct *prev, | |
260 | struct thread_struct *next, | |
261 | unsigned long tifp, unsigned long tifn) | |
262 | { | |
263 | if (tifn & _TIF_IO_BITMAP) { | |
264 | /* | |
265 | * Copy the relevant range of the IO bitmap. | |
266 | * Normally this is 128 bytes or less: | |
267 | */ | |
268 | memcpy(tss->io_bitmap, next->io_bitmap_ptr, | |
269 | max(prev->io_bitmap_max, next->io_bitmap_max)); | |
270 | /* | |
271 | * Make sure that the TSS limit is correct for the CPU | |
272 | * to notice the IO bitmap. | |
273 | */ | |
274 | refresh_tss_limit(); | |
275 | } else if (tifp & _TIF_IO_BITMAP) { | |
276 | /* | |
277 | * Clear any possible leftover bits: | |
278 | */ | |
279 | memset(tss->io_bitmap, 0xff, prev->io_bitmap_max); | |
280 | } | |
281 | } | |
282 | ||
3f6a3b03 TG |
283 | #ifdef CONFIG_SMP |
284 | ||
285 | struct ssb_state { | |
286 | struct ssb_state *shared_state; | |
287 | raw_spinlock_t lock; | |
288 | unsigned int disable_state; | |
289 | unsigned long local_state; | |
290 | }; | |
291 | ||
292 | #define LSTATE_SSB 0 | |
293 | ||
294 | static DEFINE_PER_CPU(struct ssb_state, ssb_state); | |
295 | ||
296 | void speculative_store_bypass_ht_init(void) | |
5407b7f8 | 297 | { |
3f6a3b03 TG |
298 | struct ssb_state *st = this_cpu_ptr(&ssb_state); |
299 | unsigned int this_cpu = smp_processor_id(); | |
300 | unsigned int cpu; | |
301 | ||
302 | st->local_state = 0; | |
303 | ||
304 | /* | |
305 | * Shared state setup happens once on the first bringup | |
306 | * of the CPU. It's not destroyed on CPU hotunplug. | |
307 | */ | |
308 | if (st->shared_state) | |
309 | return; | |
310 | ||
311 | raw_spin_lock_init(&st->lock); | |
312 | ||
313 | /* | |
314 | * Go over HT siblings and check whether one of them has set up the | |
315 | * shared state pointer already. | |
316 | */ | |
317 | for_each_cpu(cpu, topology_sibling_cpumask(this_cpu)) { | |
318 | if (cpu == this_cpu) | |
319 | continue; | |
320 | ||
321 | if (!per_cpu(ssb_state, cpu).shared_state) | |
322 | continue; | |
323 | ||
324 | /* Link it to the state of the sibling: */ | |
325 | st->shared_state = per_cpu(ssb_state, cpu).shared_state; | |
326 | return; | |
327 | } | |
328 | ||
329 | /* | |
330 | * First HT sibling to come up on the core. Link shared state of | |
331 | * the first HT sibling to itself. The siblings on the same core | |
332 | * which come up later will see the shared state pointer and link | |
333 | * themself to the state of this CPU. | |
334 | */ | |
335 | st->shared_state = st; | |
336 | } | |
5407b7f8 | 337 | |
3f6a3b03 TG |
338 | /* |
339 | * Logic is: First HT sibling enables SSBD for both siblings in the core | |
340 | * and last sibling to disable it, disables it for the whole core. This how | |
341 | * MSR_SPEC_CTRL works in "hardware": | |
342 | * | |
343 | * CORE_SPEC_CTRL = THREAD0_SPEC_CTRL | THREAD1_SPEC_CTRL | |
344 | */ | |
345 | static __always_inline void amd_set_core_ssb_state(unsigned long tifn) | |
346 | { | |
347 | struct ssb_state *st = this_cpu_ptr(&ssb_state); | |
348 | u64 msr = x86_amd_ls_cfg_base; | |
349 | ||
350 | if (!static_cpu_has(X86_FEATURE_ZEN)) { | |
351 | msr |= ssbd_tif_to_amd_ls_cfg(tifn); | |
5407b7f8 | 352 | wrmsrl(MSR_AMD64_LS_CFG, msr); |
3f6a3b03 TG |
353 | return; |
354 | } | |
355 | ||
356 | if (tifn & _TIF_SSBD) { | |
357 | /* | |
358 | * Since this can race with prctl(), block reentry on the | |
359 | * same CPU. | |
360 | */ | |
361 | if (__test_and_set_bit(LSTATE_SSB, &st->local_state)) | |
362 | return; | |
363 | ||
364 | msr |= x86_amd_ls_cfg_ssbd_mask; | |
365 | ||
366 | raw_spin_lock(&st->shared_state->lock); | |
367 | /* First sibling enables SSBD: */ | |
368 | if (!st->shared_state->disable_state) | |
369 | wrmsrl(MSR_AMD64_LS_CFG, msr); | |
370 | st->shared_state->disable_state++; | |
371 | raw_spin_unlock(&st->shared_state->lock); | |
5407b7f8 | 372 | } else { |
3f6a3b03 TG |
373 | if (!__test_and_clear_bit(LSTATE_SSB, &st->local_state)) |
374 | return; | |
375 | ||
376 | raw_spin_lock(&st->shared_state->lock); | |
377 | st->shared_state->disable_state--; | |
378 | if (!st->shared_state->disable_state) | |
379 | wrmsrl(MSR_AMD64_LS_CFG, msr); | |
380 | raw_spin_unlock(&st->shared_state->lock); | |
5407b7f8 TG |
381 | } |
382 | } | |
3f6a3b03 TG |
383 | #else |
384 | static __always_inline void amd_set_core_ssb_state(unsigned long tifn) | |
385 | { | |
386 | u64 msr = x86_amd_ls_cfg_base | ssbd_tif_to_amd_ls_cfg(tifn); | |
387 | ||
388 | wrmsrl(MSR_AMD64_LS_CFG, msr); | |
389 | } | |
390 | #endif | |
391 | ||
65e02bbd TL |
392 | static __always_inline void amd_set_ssb_virt_state(unsigned long tifn) |
393 | { | |
394 | /* | |
395 | * SSBD has the same definition in SPEC_CTRL and VIRT_SPEC_CTRL, | |
396 | * so ssbd_tif_to_spec_ctrl() just works. | |
397 | */ | |
398 | wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, ssbd_tif_to_spec_ctrl(tifn)); | |
399 | } | |
400 | ||
8fce7184 | 401 | static __always_inline void spec_ctrl_update_msr(unsigned long tifn) |
3f6a3b03 TG |
402 | { |
403 | u64 msr = x86_spec_ctrl_base | ssbd_tif_to_spec_ctrl(tifn); | |
404 | ||
405 | wrmsrl(MSR_IA32_SPEC_CTRL, msr); | |
406 | } | |
407 | ||
8fce7184 | 408 | static __always_inline void __speculation_ctrl_update(unsigned long tifn) |
3f6a3b03 | 409 | { |
65e02bbd TL |
410 | if (static_cpu_has(X86_FEATURE_VIRT_SSBD)) |
411 | amd_set_ssb_virt_state(tifn); | |
412 | else if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) | |
3f6a3b03 TG |
413 | amd_set_core_ssb_state(tifn); |
414 | else | |
8fce7184 | 415 | spec_ctrl_update_msr(tifn); |
3f6a3b03 | 416 | } |
5407b7f8 | 417 | |
8fce7184 | 418 | void speculation_ctrl_update(unsigned long tif) |
5407b7f8 | 419 | { |
3f6a3b03 | 420 | preempt_disable(); |
8fce7184 | 421 | __speculation_ctrl_update(tif); |
3f6a3b03 | 422 | preempt_enable(); |
5407b7f8 TG |
423 | } |
424 | ||
389d1fb1 JF |
425 | void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p, |
426 | struct tss_struct *tss) | |
427 | { | |
428 | struct thread_struct *prev, *next; | |
af8b3cd3 | 429 | unsigned long tifp, tifn; |
389d1fb1 JF |
430 | |
431 | prev = &prev_p->thread; | |
432 | next = &next_p->thread; | |
433 | ||
af8b3cd3 KH |
434 | tifn = READ_ONCE(task_thread_info(next_p)->flags); |
435 | tifp = READ_ONCE(task_thread_info(prev_p)->flags); | |
436 | switch_to_bitmap(tss, prev, next, tifp, tifn); | |
437 | ||
438 | propagate_user_return_notify(prev_p, next_p); | |
439 | ||
b9894a2f KH |
440 | if ((tifp & _TIF_BLOCKSTEP || tifn & _TIF_BLOCKSTEP) && |
441 | arch_has_block_step()) { | |
442 | unsigned long debugctl, msk; | |
ea8e61b7 | 443 | |
b9894a2f | 444 | rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); |
ea8e61b7 | 445 | debugctl &= ~DEBUGCTLMSR_BTF; |
b9894a2f KH |
446 | msk = tifn & _TIF_BLOCKSTEP; |
447 | debugctl |= (msk >> TIF_BLOCKSTEP) << DEBUGCTLMSR_BTF_SHIFT; | |
448 | wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); | |
ea8e61b7 | 449 | } |
389d1fb1 | 450 | |
5a920155 | 451 | if ((tifp ^ tifn) & _TIF_NOTSC) |
9d0b6232 | 452 | cr4_toggle_bits_irqsoff(X86_CR4_TSD); |
e9ea1e7f KH |
453 | |
454 | if ((tifp ^ tifn) & _TIF_NOCPUID) | |
455 | set_cpuid_faulting(!!(tifn & _TIF_NOCPUID)); | |
5407b7f8 | 456 | |
8fe36c9d | 457 | if ((tifp ^ tifn) & _TIF_SSBD) |
8fce7184 | 458 | __speculation_ctrl_update(tifn); |
389d1fb1 JF |
459 | } |
460 | ||
00dba564 TG |
461 | /* |
462 | * Idle related variables and functions | |
463 | */ | |
d1896049 | 464 | unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE; |
00dba564 TG |
465 | EXPORT_SYMBOL(boot_option_idle_override); |
466 | ||
a476bda3 | 467 | static void (*x86_idle)(void); |
00dba564 | 468 | |
90e24014 RW |
469 | #ifndef CONFIG_SMP |
470 | static inline void play_dead(void) | |
471 | { | |
472 | BUG(); | |
473 | } | |
474 | #endif | |
475 | ||
7d1a9417 TG |
476 | void arch_cpu_idle_enter(void) |
477 | { | |
6a369583 | 478 | tsc_verify_tsc_adjust(false); |
7d1a9417 | 479 | local_touch_nmi(); |
7d1a9417 | 480 | } |
90e24014 | 481 | |
7d1a9417 TG |
482 | void arch_cpu_idle_dead(void) |
483 | { | |
484 | play_dead(); | |
485 | } | |
90e24014 | 486 | |
7d1a9417 TG |
487 | /* |
488 | * Called from the generic idle code. | |
489 | */ | |
490 | void arch_cpu_idle(void) | |
491 | { | |
16f8b05a | 492 | x86_idle(); |
90e24014 RW |
493 | } |
494 | ||
00dba564 | 495 | /* |
7d1a9417 | 496 | * We use this if we don't have any better idle routine.. |
00dba564 | 497 | */ |
6727ad9e | 498 | void __cpuidle default_idle(void) |
00dba564 | 499 | { |
4d0e42cc | 500 | trace_cpu_idle_rcuidle(1, smp_processor_id()); |
7d1a9417 | 501 | safe_halt(); |
4d0e42cc | 502 | trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id()); |
00dba564 | 503 | } |
60b8b1de | 504 | #ifdef CONFIG_APM_MODULE |
00dba564 TG |
505 | EXPORT_SYMBOL(default_idle); |
506 | #endif | |
507 | ||
6a377ddc LB |
508 | #ifdef CONFIG_XEN |
509 | bool xen_set_default_idle(void) | |
e5fd47bf | 510 | { |
a476bda3 | 511 | bool ret = !!x86_idle; |
e5fd47bf | 512 | |
a476bda3 | 513 | x86_idle = default_idle; |
e5fd47bf KRW |
514 | |
515 | return ret; | |
516 | } | |
6a377ddc | 517 | #endif |
bba4ed01 | 518 | |
d3ec5cae IV |
519 | void stop_this_cpu(void *dummy) |
520 | { | |
521 | local_irq_disable(); | |
522 | /* | |
523 | * Remove this CPU: | |
524 | */ | |
4f062896 | 525 | set_cpu_online(smp_processor_id(), false); |
d3ec5cae | 526 | disable_local_APIC(); |
8838eb6c | 527 | mcheck_cpu_clear(this_cpu_ptr(&cpu_info)); |
d3ec5cae | 528 | |
f23d74f6 TL |
529 | /* |
530 | * Use wbinvd on processors that support SME. This provides support | |
531 | * for performing a successful kexec when going from SME inactive | |
532 | * to SME active (or vice-versa). The cache must be cleared so that | |
533 | * if there are entries with the same physical address, both with and | |
534 | * without the encryption bit, they don't race each other when flushed | |
535 | * and potentially end up with the wrong entry being committed to | |
536 | * memory. | |
537 | */ | |
538 | if (boot_cpu_has(X86_FEATURE_SME)) | |
539 | native_wbinvd(); | |
bba4ed01 TL |
540 | for (;;) { |
541 | /* | |
f23d74f6 TL |
542 | * Use native_halt() so that memory contents don't change |
543 | * (stack usage and variables) after possibly issuing the | |
544 | * native_wbinvd() above. | |
bba4ed01 | 545 | */ |
f23d74f6 | 546 | native_halt(); |
bba4ed01 | 547 | } |
7f424a8b PZ |
548 | } |
549 | ||
aa276e1c | 550 | /* |
07c94a38 BP |
551 | * AMD Erratum 400 aware idle routine. We handle it the same way as C3 power |
552 | * states (local apic timer and TSC stop). | |
aa276e1c | 553 | */ |
02c68a02 | 554 | static void amd_e400_idle(void) |
aa276e1c | 555 | { |
07c94a38 BP |
556 | /* |
557 | * We cannot use static_cpu_has_bug() here because X86_BUG_AMD_APIC_C1E | |
558 | * gets set after static_cpu_has() places have been converted via | |
559 | * alternatives. | |
560 | */ | |
561 | if (!boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) { | |
562 | default_idle(); | |
563 | return; | |
aa276e1c TG |
564 | } |
565 | ||
07c94a38 | 566 | tick_broadcast_enter(); |
aa276e1c | 567 | |
07c94a38 | 568 | default_idle(); |
0beefa20 | 569 | |
07c94a38 BP |
570 | /* |
571 | * The switch back from broadcast mode needs to be called with | |
572 | * interrupts disabled. | |
573 | */ | |
574 | local_irq_disable(); | |
575 | tick_broadcast_exit(); | |
576 | local_irq_enable(); | |
aa276e1c TG |
577 | } |
578 | ||
b253149b LB |
579 | /* |
580 | * Intel Core2 and older machines prefer MWAIT over HALT for C1. | |
581 | * We can't rely on cpuidle installing MWAIT, because it will not load | |
582 | * on systems that support only C1 -- so the boot default must be MWAIT. | |
583 | * | |
584 | * Some AMD machines are the opposite, they depend on using HALT. | |
585 | * | |
586 | * So for default C1, which is used during boot until cpuidle loads, | |
587 | * use MWAIT-C1 on Intel HW that has it, else use HALT. | |
588 | */ | |
589 | static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c) | |
590 | { | |
591 | if (c->x86_vendor != X86_VENDOR_INTEL) | |
592 | return 0; | |
593 | ||
08e237fa | 594 | if (!cpu_has(c, X86_FEATURE_MWAIT) || static_cpu_has_bug(X86_BUG_MONITOR)) |
b253149b LB |
595 | return 0; |
596 | ||
597 | return 1; | |
598 | } | |
599 | ||
600 | /* | |
0fb0328d HR |
601 | * MONITOR/MWAIT with no hints, used for default C1 state. This invokes MWAIT |
602 | * with interrupts enabled and no flags, which is backwards compatible with the | |
603 | * original MWAIT implementation. | |
b253149b | 604 | */ |
6727ad9e | 605 | static __cpuidle void mwait_idle(void) |
b253149b | 606 | { |
f8e617f4 | 607 | if (!current_set_polling_and_test()) { |
e43d0189 | 608 | trace_cpu_idle_rcuidle(1, smp_processor_id()); |
f8e617f4 | 609 | if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) { |
ca59809f | 610 | mb(); /* quirk */ |
b253149b | 611 | clflush((void *)¤t_thread_info()->flags); |
ca59809f | 612 | mb(); /* quirk */ |
f8e617f4 | 613 | } |
b253149b LB |
614 | |
615 | __monitor((void *)¤t_thread_info()->flags, 0, 0); | |
b253149b LB |
616 | if (!need_resched()) |
617 | __sti_mwait(0, 0); | |
618 | else | |
619 | local_irq_enable(); | |
e43d0189 | 620 | trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id()); |
f8e617f4 | 621 | } else { |
b253149b | 622 | local_irq_enable(); |
f8e617f4 MG |
623 | } |
624 | __current_clr_polling(); | |
b253149b LB |
625 | } |
626 | ||
148f9bb8 | 627 | void select_idle_routine(const struct cpuinfo_x86 *c) |
7f424a8b | 628 | { |
3e5095d1 | 629 | #ifdef CONFIG_SMP |
7d1a9417 | 630 | if (boot_option_idle_override == IDLE_POLL && smp_num_siblings > 1) |
c767a54b | 631 | pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n"); |
7f424a8b | 632 | #endif |
7d1a9417 | 633 | if (x86_idle || boot_option_idle_override == IDLE_POLL) |
6ddd2a27 TG |
634 | return; |
635 | ||
3344ed30 | 636 | if (boot_cpu_has_bug(X86_BUG_AMD_E400)) { |
c767a54b | 637 | pr_info("using AMD E400 aware idle routine\n"); |
a476bda3 | 638 | x86_idle = amd_e400_idle; |
b253149b LB |
639 | } else if (prefer_mwait_c1_over_halt(c)) { |
640 | pr_info("using mwait in idle threads\n"); | |
641 | x86_idle = mwait_idle; | |
6ddd2a27 | 642 | } else |
a476bda3 | 643 | x86_idle = default_idle; |
7f424a8b PZ |
644 | } |
645 | ||
07c94a38 | 646 | void amd_e400_c1e_apic_setup(void) |
30e1e6d1 | 647 | { |
07c94a38 BP |
648 | if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) { |
649 | pr_info("Switch to broadcast mode on CPU%d\n", smp_processor_id()); | |
650 | local_irq_disable(); | |
651 | tick_broadcast_force(); | |
652 | local_irq_enable(); | |
653 | } | |
30e1e6d1 RR |
654 | } |
655 | ||
e7ff3a47 TG |
656 | void __init arch_post_acpi_subsys_init(void) |
657 | { | |
658 | u32 lo, hi; | |
659 | ||
660 | if (!boot_cpu_has_bug(X86_BUG_AMD_E400)) | |
661 | return; | |
662 | ||
663 | /* | |
664 | * AMD E400 detection needs to happen after ACPI has been enabled. If | |
665 | * the machine is affected K8_INTP_C1E_ACTIVE_MASK bits are set in | |
666 | * MSR_K8_INT_PENDING_MSG. | |
667 | */ | |
668 | rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi); | |
669 | if (!(lo & K8_INTP_C1E_ACTIVE_MASK)) | |
670 | return; | |
671 | ||
672 | boot_cpu_set_bug(X86_BUG_AMD_APIC_C1E); | |
673 | ||
674 | if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC)) | |
675 | mark_tsc_unstable("TSC halt in AMD C1E"); | |
676 | pr_info("System has AMD C1E enabled\n"); | |
677 | } | |
678 | ||
7f424a8b PZ |
679 | static int __init idle_setup(char *str) |
680 | { | |
ab6bc3e3 CG |
681 | if (!str) |
682 | return -EINVAL; | |
683 | ||
7f424a8b | 684 | if (!strcmp(str, "poll")) { |
c767a54b | 685 | pr_info("using polling idle threads\n"); |
d1896049 | 686 | boot_option_idle_override = IDLE_POLL; |
7d1a9417 | 687 | cpu_idle_poll_ctrl(true); |
d1896049 | 688 | } else if (!strcmp(str, "halt")) { |
c1e3b377 ZY |
689 | /* |
690 | * When the boot option of idle=halt is added, halt is | |
691 | * forced to be used for CPU idle. In such case CPU C2/C3 | |
692 | * won't be used again. | |
693 | * To continue to load the CPU idle driver, don't touch | |
694 | * the boot_option_idle_override. | |
695 | */ | |
a476bda3 | 696 | x86_idle = default_idle; |
d1896049 | 697 | boot_option_idle_override = IDLE_HALT; |
da5e09a1 ZY |
698 | } else if (!strcmp(str, "nomwait")) { |
699 | /* | |
700 | * If the boot option of "idle=nomwait" is added, | |
701 | * it means that mwait will be disabled for CPU C2/C3 | |
702 | * states. In such case it won't touch the variable | |
703 | * of boot_option_idle_override. | |
704 | */ | |
d1896049 | 705 | boot_option_idle_override = IDLE_NOMWAIT; |
c1e3b377 | 706 | } else |
7f424a8b PZ |
707 | return -1; |
708 | ||
7f424a8b PZ |
709 | return 0; |
710 | } | |
711 | early_param("idle", idle_setup); | |
712 | ||
9d62dcdf AW |
713 | unsigned long arch_align_stack(unsigned long sp) |
714 | { | |
715 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) | |
716 | sp -= get_random_int() % 8192; | |
717 | return sp & ~0xf; | |
718 | } | |
719 | ||
720 | unsigned long arch_randomize_brk(struct mm_struct *mm) | |
721 | { | |
9c6f0902 | 722 | return randomize_page(mm->brk, 0x02000000); |
9d62dcdf AW |
723 | } |
724 | ||
7ba78053 TG |
725 | /* |
726 | * Called from fs/proc with a reference on @p to find the function | |
727 | * which called into schedule(). This needs to be done carefully | |
728 | * because the task might wake up and we might look at a stack | |
729 | * changing under us. | |
730 | */ | |
731 | unsigned long get_wchan(struct task_struct *p) | |
732 | { | |
74327a3e | 733 | unsigned long start, bottom, top, sp, fp, ip, ret = 0; |
7ba78053 TG |
734 | int count = 0; |
735 | ||
736 | if (!p || p == current || p->state == TASK_RUNNING) | |
737 | return 0; | |
738 | ||
74327a3e AL |
739 | if (!try_get_task_stack(p)) |
740 | return 0; | |
741 | ||
7ba78053 TG |
742 | start = (unsigned long)task_stack_page(p); |
743 | if (!start) | |
74327a3e | 744 | goto out; |
7ba78053 TG |
745 | |
746 | /* | |
747 | * Layout of the stack page: | |
748 | * | |
749 | * ----------- topmax = start + THREAD_SIZE - sizeof(unsigned long) | |
750 | * PADDING | |
751 | * ----------- top = topmax - TOP_OF_KERNEL_STACK_PADDING | |
752 | * stack | |
15f4eae7 | 753 | * ----------- bottom = start |
7ba78053 TG |
754 | * |
755 | * The tasks stack pointer points at the location where the | |
756 | * framepointer is stored. The data on the stack is: | |
757 | * ... IP FP ... IP FP | |
758 | * | |
759 | * We need to read FP and IP, so we need to adjust the upper | |
760 | * bound by another unsigned long. | |
761 | */ | |
762 | top = start + THREAD_SIZE - TOP_OF_KERNEL_STACK_PADDING; | |
763 | top -= 2 * sizeof(unsigned long); | |
15f4eae7 | 764 | bottom = start; |
7ba78053 TG |
765 | |
766 | sp = READ_ONCE(p->thread.sp); | |
767 | if (sp < bottom || sp > top) | |
74327a3e | 768 | goto out; |
7ba78053 | 769 | |
7b32aead | 770 | fp = READ_ONCE_NOCHECK(((struct inactive_task_frame *)sp)->bp); |
7ba78053 TG |
771 | do { |
772 | if (fp < bottom || fp > top) | |
74327a3e | 773 | goto out; |
f7d27c35 | 774 | ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long))); |
74327a3e AL |
775 | if (!in_sched_functions(ip)) { |
776 | ret = ip; | |
777 | goto out; | |
778 | } | |
f7d27c35 | 779 | fp = READ_ONCE_NOCHECK(*(unsigned long *)fp); |
7ba78053 | 780 | } while (count++ < 16 && p->state != TASK_RUNNING); |
74327a3e AL |
781 | |
782 | out: | |
783 | put_task_stack(p); | |
784 | return ret; | |
7ba78053 | 785 | } |
b0b9b014 KH |
786 | |
787 | long do_arch_prctl_common(struct task_struct *task, int option, | |
788 | unsigned long cpuid_enabled) | |
789 | { | |
e9ea1e7f KH |
790 | switch (option) { |
791 | case ARCH_GET_CPUID: | |
792 | return get_cpuid_mode(); | |
793 | case ARCH_SET_CPUID: | |
794 | return set_cpuid_mode(task, cpuid_enabled); | |
795 | } | |
796 | ||
b0b9b014 KH |
797 | return -EINVAL; |
798 | } |