process.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * Based on arch/arm/kernel/process.c
  3. *
  4. * Original Copyright (C) 1995 Linus Torvalds
  5. * Copyright (C) 1996-2000 Russell King - Converted to ARM.
  6. * Copyright (C) 2012 ARM Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <stdarg.h>
  21. #include <linux/compat.h>
  22. #include <linux/efi.h>
  23. #include <linux/export.h>
  24. #include <linux/sched.h>
  25. #include <linux/sched/debug.h>
  26. #include <linux/sched/task.h>
  27. #include <linux/sched/task_stack.h>
  28. #include <linux/kernel.h>
  29. #include <linux/mm.h>
  30. #include <linux/stddef.h>
  31. #include <linux/unistd.h>
  32. #include <linux/user.h>
  33. #include <linux/delay.h>
  34. #include <linux/reboot.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/init.h>
  37. #include <linux/cpu.h>
  38. #include <linux/elfcore.h>
  39. #include <linux/pm.h>
  40. #include <linux/tick.h>
  41. #include <linux/utsname.h>
  42. #include <linux/uaccess.h>
  43. #include <linux/random.h>
  44. #include <linux/hw_breakpoint.h>
  45. #include <linux/personality.h>
  46. #include <linux/notifier.h>
  47. #include <trace/events/power.h>
  48. #include <linux/percpu.h>
  49. #include <linux/thread_info.h>
  50. #include <asm/alternative.h>
  51. #include <asm/compat.h>
  52. #include <asm/cacheflush.h>
  53. #include <asm/exec.h>
  54. #include <asm/fpsimd.h>
  55. #include <asm/mmu_context.h>
  56. #include <asm/processor.h>
  57. #include <asm/stacktrace.h>
  58. #ifdef CONFIG_STACKPROTECTOR
  59. #include <linux/stackprotector.h>
  60. unsigned long __stack_chk_guard __read_mostly;
  61. EXPORT_SYMBOL(__stack_chk_guard);
  62. #endif
  63. /*
  64. * Function pointers to optional machine specific functions
  65. */
  66. void (*pm_power_off)(void);
  67. EXPORT_SYMBOL_GPL(pm_power_off);
  68. void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
  69. /*
  70. * This is our default idle handler.
  71. */
  72. void arch_cpu_idle(void)
  73. {
  74. /*
  75. * This should do all the clock switching and wait for interrupt
  76. * tricks
  77. */
  78. trace_cpu_idle_rcuidle(1, smp_processor_id());
  79. cpu_do_idle();
  80. local_irq_enable();
  81. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  82. }
  83. #ifdef CONFIG_HOTPLUG_CPU
  84. void arch_cpu_idle_dead(void)
  85. {
  86. cpu_die();
  87. }
  88. #endif
  89. /*
  90. * Called by kexec, immediately prior to machine_kexec().
  91. *
  92. * This must completely disable all secondary CPUs; simply causing those CPUs
  93. * to execute e.g. a RAM-based pin loop is not sufficient. This allows the
  94. * kexec'd kernel to use any and all RAM as it sees fit, without having to
  95. * avoid any code or data used by any SW CPU pin loop. The CPU hotplug
  96. * functionality embodied in disable_nonboot_cpus() to achieve this.
  97. */
  98. void machine_shutdown(void)
  99. {
  100. disable_nonboot_cpus();
  101. }
  102. /*
  103. * Halting simply requires that the secondary CPUs stop performing any
  104. * activity (executing tasks, handling interrupts). smp_send_stop()
  105. * achieves this.
  106. */
  107. void machine_halt(void)
  108. {
  109. local_irq_disable();
  110. smp_send_stop();
  111. while (1);
  112. }
  113. /*
  114. * Power-off simply requires that the secondary CPUs stop performing any
  115. * activity (executing tasks, handling interrupts). smp_send_stop()
  116. * achieves this. When the system power is turned off, it will take all CPUs
  117. * with it.
  118. */
  119. void machine_power_off(void)
  120. {
  121. local_irq_disable();
  122. smp_send_stop();
  123. if (pm_power_off)
  124. pm_power_off();
  125. }
  126. /*
  127. * Restart requires that the secondary CPUs stop performing any activity
  128. * while the primary CPU resets the system. Systems with multiple CPUs must
  129. * provide a HW restart implementation, to ensure that all CPUs reset at once.
  130. * This is required so that any code running after reset on the primary CPU
  131. * doesn't have to co-ordinate with other CPUs to ensure they aren't still
  132. * executing pre-reset code, and using RAM that the primary CPU's code wishes
  133. * to use. Implementing such co-ordination would be essentially impossible.
  134. */
  135. void machine_restart(char *cmd)
  136. {
  137. /* Disable interrupts first */
  138. local_irq_disable();
  139. smp_send_stop();
  140. /*
  141. * UpdateCapsule() depends on the system being reset via
  142. * ResetSystem().
  143. */
  144. if (efi_enabled(EFI_RUNTIME_SERVICES))
  145. efi_reboot(reboot_mode, NULL);
  146. /* Now call the architecture specific reboot code. */
  147. if (arm_pm_restart)
  148. arm_pm_restart(reboot_mode, cmd);
  149. else
  150. do_kernel_restart(cmd);
  151. /*
  152. * Whoops - the architecture was unable to reboot.
  153. */
  154. printk("Reboot failed -- System halted\n");
  155. while (1);
  156. }
  157. static void print_pstate(struct pt_regs *regs)
  158. {
  159. u64 pstate = regs->pstate;
  160. if (compat_user_mode(regs)) {
  161. printk("pstate: %08llx (%c%c%c%c %c %s %s %c%c%c)\n",
  162. pstate,
  163. pstate & PSR_AA32_N_BIT ? 'N' : 'n',
  164. pstate & PSR_AA32_Z_BIT ? 'Z' : 'z',
  165. pstate & PSR_AA32_C_BIT ? 'C' : 'c',
  166. pstate & PSR_AA32_V_BIT ? 'V' : 'v',
  167. pstate & PSR_AA32_Q_BIT ? 'Q' : 'q',
  168. pstate & PSR_AA32_T_BIT ? "T32" : "A32",
  169. pstate & PSR_AA32_E_BIT ? "BE" : "LE",
  170. pstate & PSR_AA32_A_BIT ? 'A' : 'a',
  171. pstate & PSR_AA32_I_BIT ? 'I' : 'i',
  172. pstate & PSR_AA32_F_BIT ? 'F' : 'f');
  173. } else {
  174. printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO)\n",
  175. pstate,
  176. pstate & PSR_N_BIT ? 'N' : 'n',
  177. pstate & PSR_Z_BIT ? 'Z' : 'z',
  178. pstate & PSR_C_BIT ? 'C' : 'c',
  179. pstate & PSR_V_BIT ? 'V' : 'v',
  180. pstate & PSR_D_BIT ? 'D' : 'd',
  181. pstate & PSR_A_BIT ? 'A' : 'a',
  182. pstate & PSR_I_BIT ? 'I' : 'i',
  183. pstate & PSR_F_BIT ? 'F' : 'f',
  184. pstate & PSR_PAN_BIT ? '+' : '-',
  185. pstate & PSR_UAO_BIT ? '+' : '-');
  186. }
  187. }
  188. void __show_regs(struct pt_regs *regs)
  189. {
  190. int i, top_reg;
  191. u64 lr, sp;
  192. if (compat_user_mode(regs)) {
  193. lr = regs->compat_lr;
  194. sp = regs->compat_sp;
  195. top_reg = 12;
  196. } else {
  197. lr = regs->regs[30];
  198. sp = regs->sp;
  199. top_reg = 29;
  200. }
  201. show_regs_print_info(KERN_DEFAULT);
  202. print_pstate(regs);
  203. if (!user_mode(regs)) {
  204. printk("pc : %pS\n", (void *)regs->pc);
  205. printk("lr : %pS\n", (void *)lr);
  206. } else {
  207. printk("pc : %016llx\n", regs->pc);
  208. printk("lr : %016llx\n", lr);
  209. }
  210. printk("sp : %016llx\n", sp);
  211. i = top_reg;
  212. while (i >= 0) {
  213. printk("x%-2d: %016llx ", i, regs->regs[i]);
  214. i--;
  215. if (i % 2 == 0) {
  216. pr_cont("x%-2d: %016llx ", i, regs->regs[i]);
  217. i--;
  218. }
  219. pr_cont("\n");
  220. }
  221. }
  222. void show_regs(struct pt_regs * regs)
  223. {
  224. __show_regs(regs);
  225. dump_backtrace(regs, NULL);
  226. }
  227. static void tls_thread_flush(void)
  228. {
  229. write_sysreg(0, tpidr_el0);
  230. if (is_compat_task()) {
  231. current->thread.uw.tp_value = 0;
  232. /*
  233. * We need to ensure ordering between the shadow state and the
  234. * hardware state, so that we don't corrupt the hardware state
  235. * with a stale shadow state during context switch.
  236. */
  237. barrier();
  238. write_sysreg(0, tpidrro_el0);
  239. }
  240. }
  241. void flush_thread(void)
  242. {
  243. fpsimd_flush_thread();
  244. tls_thread_flush();
  245. flush_ptrace_hw_breakpoint(current);
  246. }
  247. void release_thread(struct task_struct *dead_task)
  248. {
  249. }
  250. void arch_release_task_struct(struct task_struct *tsk)
  251. {
  252. fpsimd_release_task(tsk);
  253. }
  254. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  255. {
  256. if (current->mm)
  257. fpsimd_preserve_current_state();
  258. *dst = *src;
  259. /* We rely on the above assignment to initialize dst's thread_flags: */
  260. BUILD_BUG_ON(!IS_ENABLED(CONFIG_THREAD_INFO_IN_TASK));
  261. /*
  262. * Detach src's sve_state (if any) from dst so that it does not
  263. * get erroneously used or freed prematurely. dst's sve_state
  264. * will be allocated on demand later on if dst uses SVE.
  265. * For consistency, also clear TIF_SVE here: this could be done
  266. * later in copy_process(), but to avoid tripping up future
  267. * maintainers it is best not to leave TIF_SVE and sve_state in
  268. * an inconsistent state, even temporarily.
  269. */
  270. dst->thread.sve_state = NULL;
  271. clear_tsk_thread_flag(dst, TIF_SVE);
  272. return 0;
  273. }
  274. asmlinkage void ret_from_fork(void) asm("ret_from_fork");
  275. int copy_thread(unsigned long clone_flags, unsigned long stack_start,
  276. unsigned long stk_sz, struct task_struct *p)
  277. {
  278. struct pt_regs *childregs = task_pt_regs(p);
  279. memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
  280. /*
  281. * In case p was allocated the same task_struct pointer as some
  282. * other recently-exited task, make sure p is disassociated from
  283. * any cpu that may have run that now-exited task recently.
  284. * Otherwise we could erroneously skip reloading the FPSIMD
  285. * registers for p.
  286. */
  287. fpsimd_flush_task_state(p);
  288. if (likely(!(p->flags & PF_KTHREAD))) {
  289. *childregs = *current_pt_regs();
  290. childregs->regs[0] = 0;
  291. /*
  292. * Read the current TLS pointer from tpidr_el0 as it may be
  293. * out-of-sync with the saved value.
  294. */
  295. *task_user_tls(p) = read_sysreg(tpidr_el0);
  296. if (stack_start) {
  297. if (is_compat_thread(task_thread_info(p)))
  298. childregs->compat_sp = stack_start;
  299. else
  300. childregs->sp = stack_start;
  301. }
  302. /*
  303. * If a TLS pointer was passed to clone (4th argument), use it
  304. * for the new thread.
  305. */
  306. if (clone_flags & CLONE_SETTLS)
  307. p->thread.uw.tp_value = childregs->regs[3];
  308. } else {
  309. memset(childregs, 0, sizeof(struct pt_regs));
  310. childregs->pstate = PSR_MODE_EL1h;
  311. if (IS_ENABLED(CONFIG_ARM64_UAO) &&
  312. cpus_have_const_cap(ARM64_HAS_UAO))
  313. childregs->pstate |= PSR_UAO_BIT;
  314. if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE)
  315. set_ssbs_bit(childregs);
  316. p->thread.cpu_context.x19 = stack_start;
  317. p->thread.cpu_context.x20 = stk_sz;
  318. }
  319. p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
  320. p->thread.cpu_context.sp = (unsigned long)childregs;
  321. ptrace_hw_copy_thread(p);
  322. return 0;
  323. }
  324. void tls_preserve_current_state(void)
  325. {
  326. *task_user_tls(current) = read_sysreg(tpidr_el0);
  327. }
  328. static void tls_thread_switch(struct task_struct *next)
  329. {
  330. tls_preserve_current_state();
  331. if (is_compat_thread(task_thread_info(next)))
  332. write_sysreg(next->thread.uw.tp_value, tpidrro_el0);
  333. else if (!arm64_kernel_unmapped_at_el0())
  334. write_sysreg(0, tpidrro_el0);
  335. write_sysreg(*task_user_tls(next), tpidr_el0);
  336. }
  337. /* Restore the UAO state depending on next's addr_limit */
  338. void uao_thread_switch(struct task_struct *next)
  339. {
  340. if (IS_ENABLED(CONFIG_ARM64_UAO)) {
  341. if (task_thread_info(next)->addr_limit == KERNEL_DS)
  342. asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
  343. else
  344. asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO));
  345. }
  346. }
  347. /*
  348. * Force SSBS state on context-switch, since it may be lost after migrating
  349. * from a CPU which treats the bit as RES0 in a heterogeneous system.
  350. */
  351. static void ssbs_thread_switch(struct task_struct *next)
  352. {
  353. struct pt_regs *regs = task_pt_regs(next);
  354. /*
  355. * Nothing to do for kernel threads, but 'regs' may be junk
  356. * (e.g. idle task) so check the flags and bail early.
  357. */
  358. if (unlikely(next->flags & PF_KTHREAD))
  359. return;
  360. /*
  361. * If all CPUs implement the SSBS extension, then we just need to
  362. * context-switch the PSTATE field.
  363. */
  364. if (cpu_have_feature(cpu_feature(SSBS)))
  365. return;
  366. /* If the mitigation is enabled, then we leave SSBS clear. */
  367. if ((arm64_get_ssbd_state() == ARM64_SSBD_FORCE_ENABLE) ||
  368. test_tsk_thread_flag(next, TIF_SSBD))
  369. return;
  370. if (compat_user_mode(regs))
  371. set_compat_ssbs_bit(regs);
  372. else if (user_mode(regs))
  373. set_ssbs_bit(regs);
  374. }
  375. /*
  376. * We store our current task in sp_el0, which is clobbered by userspace. Keep a
  377. * shadow copy so that we can restore this upon entry from userspace.
  378. *
  379. * This is *only* for exception entry from EL0, and is not valid until we
  380. * __switch_to() a user task.
  381. */
  382. DEFINE_PER_CPU(struct task_struct *, __entry_task);
  383. static void entry_task_switch(struct task_struct *next)
  384. {
  385. __this_cpu_write(__entry_task, next);
  386. }
  387. /*
  388. * Thread switching.
  389. */
  390. __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
  391. struct task_struct *next)
  392. {
  393. struct task_struct *last;
  394. fpsimd_thread_switch(next);
  395. tls_thread_switch(next);
  396. hw_breakpoint_thread_switch(next);
  397. contextidr_thread_switch(next);
  398. entry_task_switch(next);
  399. uao_thread_switch(next);
  400. ssbs_thread_switch(next);
  401. /*
  402. * Complete any pending TLB or cache maintenance on this CPU in case
  403. * the thread migrates to a different CPU.
  404. * This full barrier is also required by the membarrier system
  405. * call.
  406. */
  407. dsb(ish);
  408. /* the actual thread switch */
  409. last = cpu_switch_to(prev, next);
  410. return last;
  411. }
  412. unsigned long get_wchan(struct task_struct *p)
  413. {
  414. struct stackframe frame;
  415. unsigned long stack_page, ret = 0;
  416. int count = 0;
  417. if (!p || p == current || p->state == TASK_RUNNING)
  418. return 0;
  419. stack_page = (unsigned long)try_get_task_stack(p);
  420. if (!stack_page)
  421. return 0;
  422. frame.fp = thread_saved_fp(p);
  423. frame.pc = thread_saved_pc(p);
  424. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  425. frame.graph = p->curr_ret_stack;
  426. #endif
  427. do {
  428. if (unwind_frame(p, &frame))
  429. goto out;
  430. if (!in_sched_functions(frame.pc)) {
  431. ret = frame.pc;
  432. goto out;
  433. }
  434. } while (count ++ < 16);
  435. out:
  436. put_task_stack(p);
  437. return ret;
  438. }
  439. unsigned long arch_align_stack(unsigned long sp)
  440. {
  441. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  442. sp -= get_random_int() & ~PAGE_MASK;
  443. return sp & ~0xf;
  444. }
  445. unsigned long arch_randomize_brk(struct mm_struct *mm)
  446. {
  447. if (is_compat_task())
  448. return randomize_page(mm->brk, SZ_32M);
  449. else
  450. return randomize_page(mm->brk, SZ_1G);
  451. }
  452. /*
  453. * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY.
  454. */
  455. void arch_setup_new_exec(void)
  456. {
  457. current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0;
  458. }
  459. #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
  460. void __used stackleak_check_alloca(unsigned long size)
  461. {
  462. unsigned long stack_left;
  463. unsigned long current_sp = current_stack_pointer;
  464. struct stack_info info;
  465. BUG_ON(!on_accessible_stack(current, current_sp, &info));
  466. stack_left = current_sp - info.low;
  467. /*
  468. * There's a good chance we're almost out of stack space if this
  469. * is true. Using panic() over BUG() is more likely to give
  470. * reliable debugging output.
  471. */
  472. if (size >= stack_left)
  473. panic("alloca() over the kernel stack boundary\n");
  474. }
  475. EXPORT_SYMBOL(stackleak_check_alloca);
  476. #endif