]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/process.c
x86/cpufeatures: Disentangle SSBD enumeration
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / process.c
CommitLineData
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 86EXPORT_PER_CPU_SYMBOL(cpu_tss_rw);
45046892 87
b7ceaec1
AL
88DEFINE_PER_CPU(bool, __tss_limit_invalid);
89EXPORT_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
95int 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 108void 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
133void 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
143void 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
155static 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
167int 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
179int 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
191DEFINE_PER_CPU(u64, msr_misc_features_shadow);
192
193static 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
204static 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
217static 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
230static int get_cpuid_mode(void)
231{
232 return !test_thread_flag(TIF_NOCPUID);
233}
234
235static 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 */
251void 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
258static 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
5407b7f8
TG
283static __always_inline void __speculative_store_bypass_update(unsigned long tifn)
284{
285 u64 msr;
286
e48f404c 287 if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) {
8fe36c9d 288 msr = x86_amd_ls_cfg_base | ssbd_tif_to_amd_ls_cfg(tifn);
5407b7f8
TG
289 wrmsrl(MSR_AMD64_LS_CFG, msr);
290 } else {
8fe36c9d 291 msr = x86_spec_ctrl_base | ssbd_tif_to_spec_ctrl(tifn);
5407b7f8
TG
292 wrmsrl(MSR_IA32_SPEC_CTRL, msr);
293 }
294}
295
296void speculative_store_bypass_update(void)
297{
298 __speculative_store_bypass_update(current_thread_info()->flags);
299}
300
389d1fb1
JF
301void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
302 struct tss_struct *tss)
303{
304 struct thread_struct *prev, *next;
af8b3cd3 305 unsigned long tifp, tifn;
389d1fb1
JF
306
307 prev = &prev_p->thread;
308 next = &next_p->thread;
309
af8b3cd3
KH
310 tifn = READ_ONCE(task_thread_info(next_p)->flags);
311 tifp = READ_ONCE(task_thread_info(prev_p)->flags);
312 switch_to_bitmap(tss, prev, next, tifp, tifn);
313
314 propagate_user_return_notify(prev_p, next_p);
315
b9894a2f
KH
316 if ((tifp & _TIF_BLOCKSTEP || tifn & _TIF_BLOCKSTEP) &&
317 arch_has_block_step()) {
318 unsigned long debugctl, msk;
ea8e61b7 319
b9894a2f 320 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
ea8e61b7 321 debugctl &= ~DEBUGCTLMSR_BTF;
b9894a2f
KH
322 msk = tifn & _TIF_BLOCKSTEP;
323 debugctl |= (msk >> TIF_BLOCKSTEP) << DEBUGCTLMSR_BTF_SHIFT;
324 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
ea8e61b7 325 }
389d1fb1 326
5a920155 327 if ((tifp ^ tifn) & _TIF_NOTSC)
9d0b6232 328 cr4_toggle_bits_irqsoff(X86_CR4_TSD);
e9ea1e7f
KH
329
330 if ((tifp ^ tifn) & _TIF_NOCPUID)
331 set_cpuid_faulting(!!(tifn & _TIF_NOCPUID));
5407b7f8 332
8fe36c9d 333 if ((tifp ^ tifn) & _TIF_SSBD)
5407b7f8 334 __speculative_store_bypass_update(tifn);
389d1fb1
JF
335}
336
00dba564
TG
337/*
338 * Idle related variables and functions
339 */
d1896049 340unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
00dba564
TG
341EXPORT_SYMBOL(boot_option_idle_override);
342
a476bda3 343static void (*x86_idle)(void);
00dba564 344
90e24014
RW
345#ifndef CONFIG_SMP
346static inline void play_dead(void)
347{
348 BUG();
349}
350#endif
351
7d1a9417
TG
352void arch_cpu_idle_enter(void)
353{
6a369583 354 tsc_verify_tsc_adjust(false);
7d1a9417 355 local_touch_nmi();
7d1a9417 356}
90e24014 357
7d1a9417
TG
358void arch_cpu_idle_dead(void)
359{
360 play_dead();
361}
90e24014 362
7d1a9417
TG
363/*
364 * Called from the generic idle code.
365 */
366void arch_cpu_idle(void)
367{
16f8b05a 368 x86_idle();
90e24014
RW
369}
370
00dba564 371/*
7d1a9417 372 * We use this if we don't have any better idle routine..
00dba564 373 */
6727ad9e 374void __cpuidle default_idle(void)
00dba564 375{
4d0e42cc 376 trace_cpu_idle_rcuidle(1, smp_processor_id());
7d1a9417 377 safe_halt();
4d0e42cc 378 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
00dba564 379}
60b8b1de 380#ifdef CONFIG_APM_MODULE
00dba564
TG
381EXPORT_SYMBOL(default_idle);
382#endif
383
6a377ddc
LB
384#ifdef CONFIG_XEN
385bool xen_set_default_idle(void)
e5fd47bf 386{
a476bda3 387 bool ret = !!x86_idle;
e5fd47bf 388
a476bda3 389 x86_idle = default_idle;
e5fd47bf
KRW
390
391 return ret;
392}
6a377ddc 393#endif
bba4ed01 394
d3ec5cae
IV
395void stop_this_cpu(void *dummy)
396{
397 local_irq_disable();
398 /*
399 * Remove this CPU:
400 */
4f062896 401 set_cpu_online(smp_processor_id(), false);
d3ec5cae 402 disable_local_APIC();
8838eb6c 403 mcheck_cpu_clear(this_cpu_ptr(&cpu_info));
d3ec5cae 404
f23d74f6
TL
405 /*
406 * Use wbinvd on processors that support SME. This provides support
407 * for performing a successful kexec when going from SME inactive
408 * to SME active (or vice-versa). The cache must be cleared so that
409 * if there are entries with the same physical address, both with and
410 * without the encryption bit, they don't race each other when flushed
411 * and potentially end up with the wrong entry being committed to
412 * memory.
413 */
414 if (boot_cpu_has(X86_FEATURE_SME))
415 native_wbinvd();
bba4ed01
TL
416 for (;;) {
417 /*
f23d74f6
TL
418 * Use native_halt() so that memory contents don't change
419 * (stack usage and variables) after possibly issuing the
420 * native_wbinvd() above.
bba4ed01 421 */
f23d74f6 422 native_halt();
bba4ed01 423 }
7f424a8b
PZ
424}
425
aa276e1c 426/*
07c94a38
BP
427 * AMD Erratum 400 aware idle routine. We handle it the same way as C3 power
428 * states (local apic timer and TSC stop).
aa276e1c 429 */
02c68a02 430static void amd_e400_idle(void)
aa276e1c 431{
07c94a38
BP
432 /*
433 * We cannot use static_cpu_has_bug() here because X86_BUG_AMD_APIC_C1E
434 * gets set after static_cpu_has() places have been converted via
435 * alternatives.
436 */
437 if (!boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) {
438 default_idle();
439 return;
aa276e1c
TG
440 }
441
07c94a38 442 tick_broadcast_enter();
aa276e1c 443
07c94a38 444 default_idle();
0beefa20 445
07c94a38
BP
446 /*
447 * The switch back from broadcast mode needs to be called with
448 * interrupts disabled.
449 */
450 local_irq_disable();
451 tick_broadcast_exit();
452 local_irq_enable();
aa276e1c
TG
453}
454
b253149b
LB
455/*
456 * Intel Core2 and older machines prefer MWAIT over HALT for C1.
457 * We can't rely on cpuidle installing MWAIT, because it will not load
458 * on systems that support only C1 -- so the boot default must be MWAIT.
459 *
460 * Some AMD machines are the opposite, they depend on using HALT.
461 *
462 * So for default C1, which is used during boot until cpuidle loads,
463 * use MWAIT-C1 on Intel HW that has it, else use HALT.
464 */
465static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c)
466{
467 if (c->x86_vendor != X86_VENDOR_INTEL)
468 return 0;
469
08e237fa 470 if (!cpu_has(c, X86_FEATURE_MWAIT) || static_cpu_has_bug(X86_BUG_MONITOR))
b253149b
LB
471 return 0;
472
473 return 1;
474}
475
476/*
0fb0328d
HR
477 * MONITOR/MWAIT with no hints, used for default C1 state. This invokes MWAIT
478 * with interrupts enabled and no flags, which is backwards compatible with the
479 * original MWAIT implementation.
b253149b 480 */
6727ad9e 481static __cpuidle void mwait_idle(void)
b253149b 482{
f8e617f4 483 if (!current_set_polling_and_test()) {
e43d0189 484 trace_cpu_idle_rcuidle(1, smp_processor_id());
f8e617f4 485 if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) {
ca59809f 486 mb(); /* quirk */
b253149b 487 clflush((void *)&current_thread_info()->flags);
ca59809f 488 mb(); /* quirk */
f8e617f4 489 }
b253149b
LB
490
491 __monitor((void *)&current_thread_info()->flags, 0, 0);
b253149b
LB
492 if (!need_resched())
493 __sti_mwait(0, 0);
494 else
495 local_irq_enable();
e43d0189 496 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
f8e617f4 497 } else {
b253149b 498 local_irq_enable();
f8e617f4
MG
499 }
500 __current_clr_polling();
b253149b
LB
501}
502
148f9bb8 503void select_idle_routine(const struct cpuinfo_x86 *c)
7f424a8b 504{
3e5095d1 505#ifdef CONFIG_SMP
7d1a9417 506 if (boot_option_idle_override == IDLE_POLL && smp_num_siblings > 1)
c767a54b 507 pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n");
7f424a8b 508#endif
7d1a9417 509 if (x86_idle || boot_option_idle_override == IDLE_POLL)
6ddd2a27
TG
510 return;
511
3344ed30 512 if (boot_cpu_has_bug(X86_BUG_AMD_E400)) {
c767a54b 513 pr_info("using AMD E400 aware idle routine\n");
a476bda3 514 x86_idle = amd_e400_idle;
b253149b
LB
515 } else if (prefer_mwait_c1_over_halt(c)) {
516 pr_info("using mwait in idle threads\n");
517 x86_idle = mwait_idle;
6ddd2a27 518 } else
a476bda3 519 x86_idle = default_idle;
7f424a8b
PZ
520}
521
07c94a38 522void amd_e400_c1e_apic_setup(void)
30e1e6d1 523{
07c94a38
BP
524 if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) {
525 pr_info("Switch to broadcast mode on CPU%d\n", smp_processor_id());
526 local_irq_disable();
527 tick_broadcast_force();
528 local_irq_enable();
529 }
30e1e6d1
RR
530}
531
e7ff3a47
TG
532void __init arch_post_acpi_subsys_init(void)
533{
534 u32 lo, hi;
535
536 if (!boot_cpu_has_bug(X86_BUG_AMD_E400))
537 return;
538
539 /*
540 * AMD E400 detection needs to happen after ACPI has been enabled. If
541 * the machine is affected K8_INTP_C1E_ACTIVE_MASK bits are set in
542 * MSR_K8_INT_PENDING_MSG.
543 */
544 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
545 if (!(lo & K8_INTP_C1E_ACTIVE_MASK))
546 return;
547
548 boot_cpu_set_bug(X86_BUG_AMD_APIC_C1E);
549
550 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
551 mark_tsc_unstable("TSC halt in AMD C1E");
552 pr_info("System has AMD C1E enabled\n");
553}
554
7f424a8b
PZ
555static int __init idle_setup(char *str)
556{
ab6bc3e3
CG
557 if (!str)
558 return -EINVAL;
559
7f424a8b 560 if (!strcmp(str, "poll")) {
c767a54b 561 pr_info("using polling idle threads\n");
d1896049 562 boot_option_idle_override = IDLE_POLL;
7d1a9417 563 cpu_idle_poll_ctrl(true);
d1896049 564 } else if (!strcmp(str, "halt")) {
c1e3b377
ZY
565 /*
566 * When the boot option of idle=halt is added, halt is
567 * forced to be used for CPU idle. In such case CPU C2/C3
568 * won't be used again.
569 * To continue to load the CPU idle driver, don't touch
570 * the boot_option_idle_override.
571 */
a476bda3 572 x86_idle = default_idle;
d1896049 573 boot_option_idle_override = IDLE_HALT;
da5e09a1
ZY
574 } else if (!strcmp(str, "nomwait")) {
575 /*
576 * If the boot option of "idle=nomwait" is added,
577 * it means that mwait will be disabled for CPU C2/C3
578 * states. In such case it won't touch the variable
579 * of boot_option_idle_override.
580 */
d1896049 581 boot_option_idle_override = IDLE_NOMWAIT;
c1e3b377 582 } else
7f424a8b
PZ
583 return -1;
584
7f424a8b
PZ
585 return 0;
586}
587early_param("idle", idle_setup);
588
9d62dcdf
AW
589unsigned long arch_align_stack(unsigned long sp)
590{
591 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
592 sp -= get_random_int() % 8192;
593 return sp & ~0xf;
594}
595
596unsigned long arch_randomize_brk(struct mm_struct *mm)
597{
9c6f0902 598 return randomize_page(mm->brk, 0x02000000);
9d62dcdf
AW
599}
600
7ba78053
TG
601/*
602 * Called from fs/proc with a reference on @p to find the function
603 * which called into schedule(). This needs to be done carefully
604 * because the task might wake up and we might look at a stack
605 * changing under us.
606 */
607unsigned long get_wchan(struct task_struct *p)
608{
74327a3e 609 unsigned long start, bottom, top, sp, fp, ip, ret = 0;
7ba78053
TG
610 int count = 0;
611
612 if (!p || p == current || p->state == TASK_RUNNING)
613 return 0;
614
74327a3e
AL
615 if (!try_get_task_stack(p))
616 return 0;
617
7ba78053
TG
618 start = (unsigned long)task_stack_page(p);
619 if (!start)
74327a3e 620 goto out;
7ba78053
TG
621
622 /*
623 * Layout of the stack page:
624 *
625 * ----------- topmax = start + THREAD_SIZE - sizeof(unsigned long)
626 * PADDING
627 * ----------- top = topmax - TOP_OF_KERNEL_STACK_PADDING
628 * stack
15f4eae7 629 * ----------- bottom = start
7ba78053
TG
630 *
631 * The tasks stack pointer points at the location where the
632 * framepointer is stored. The data on the stack is:
633 * ... IP FP ... IP FP
634 *
635 * We need to read FP and IP, so we need to adjust the upper
636 * bound by another unsigned long.
637 */
638 top = start + THREAD_SIZE - TOP_OF_KERNEL_STACK_PADDING;
639 top -= 2 * sizeof(unsigned long);
15f4eae7 640 bottom = start;
7ba78053
TG
641
642 sp = READ_ONCE(p->thread.sp);
643 if (sp < bottom || sp > top)
74327a3e 644 goto out;
7ba78053 645
7b32aead 646 fp = READ_ONCE_NOCHECK(((struct inactive_task_frame *)sp)->bp);
7ba78053
TG
647 do {
648 if (fp < bottom || fp > top)
74327a3e 649 goto out;
f7d27c35 650 ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
74327a3e
AL
651 if (!in_sched_functions(ip)) {
652 ret = ip;
653 goto out;
654 }
f7d27c35 655 fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
7ba78053 656 } while (count++ < 16 && p->state != TASK_RUNNING);
74327a3e
AL
657
658out:
659 put_task_stack(p);
660 return ret;
7ba78053 661}
b0b9b014
KH
662
663long do_arch_prctl_common(struct task_struct *task, int option,
664 unsigned long cpuid_enabled)
665{
e9ea1e7f
KH
666 switch (option) {
667 case ARCH_GET_CPUID:
668 return get_cpuid_mode();
669 case ARCH_SET_CPUID:
670 return set_cpuid_mode(task, cpuid_enabled);
671 }
672
b0b9b014
KH
673 return -EINVAL;
674}