process.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/stddef.h>
  28. #include <linux/unistd.h>
  29. #include <linux/user.h>
  30. #include <linux/delay.h>
  31. #include <linux/reboot.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/kallsyms.h>
  34. #include <linux/init.h>
  35. #include <linux/cpu.h>
  36. #include <linux/elfcore.h>
  37. #include <linux/pm.h>
  38. #include <linux/tick.h>
  39. #include <linux/utsname.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/random.h>
  42. #include <linux/hw_breakpoint.h>
  43. #include <linux/personality.h>
  44. #include <linux/notifier.h>
  45. #include <trace/events/power.h>
  46. #include <linux/percpu.h>
  47. #include <asm/alternative.h>
  48. #include <asm/compat.h>
  49. #include <asm/cacheflush.h>
  50. #include <asm/exec.h>
  51. #include <asm/fpsimd.h>
  52. #include <asm/mmu_context.h>
  53. #include <asm/processor.h>
  54. #include <asm/stacktrace.h>
  55. #ifdef CONFIG_CC_STACKPROTECTOR
  56. #include <linux/stackprotector.h>
  57. unsigned long __stack_chk_guard __read_mostly;
  58. EXPORT_SYMBOL(__stack_chk_guard);
  59. #endif
  60. /*
  61. * Function pointers to optional machine specific functions
  62. */
  63. void (*pm_power_off)(void);
  64. EXPORT_SYMBOL_GPL(pm_power_off);
  65. void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
  66. /*
  67. * This is our default idle handler.
  68. */
  69. void arch_cpu_idle(void)
  70. {
  71. /*
  72. * This should do all the clock switching and wait for interrupt
  73. * tricks
  74. */
  75. trace_cpu_idle_rcuidle(1, smp_processor_id());
  76. cpu_do_idle();
  77. local_irq_enable();
  78. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  79. }
  80. #ifdef CONFIG_HOTPLUG_CPU
  81. void arch_cpu_idle_dead(void)
  82. {
  83. cpu_die();
  84. }
  85. #endif
  86. /*
  87. * Called by kexec, immediately prior to machine_kexec().
  88. *
  89. * This must completely disable all secondary CPUs; simply causing those CPUs
  90. * to execute e.g. a RAM-based pin loop is not sufficient. This allows the
  91. * kexec'd kernel to use any and all RAM as it sees fit, without having to
  92. * avoid any code or data used by any SW CPU pin loop. The CPU hotplug
  93. * functionality embodied in disable_nonboot_cpus() to achieve this.
  94. */
  95. void machine_shutdown(void)
  96. {
  97. disable_nonboot_cpus();
  98. }
  99. /*
  100. * Halting simply requires that the secondary CPUs stop performing any
  101. * activity (executing tasks, handling interrupts). smp_send_stop()
  102. * achieves this.
  103. */
  104. void machine_halt(void)
  105. {
  106. local_irq_disable();
  107. smp_send_stop();
  108. while (1);
  109. }
  110. /*
  111. * Power-off simply requires that the secondary CPUs stop performing any
  112. * activity (executing tasks, handling interrupts). smp_send_stop()
  113. * achieves this. When the system power is turned off, it will take all CPUs
  114. * with it.
  115. */
  116. void machine_power_off(void)
  117. {
  118. local_irq_disable();
  119. smp_send_stop();
  120. if (pm_power_off)
  121. pm_power_off();
  122. }
  123. /*
  124. * Restart requires that the secondary CPUs stop performing any activity
  125. * while the primary CPU resets the system. Systems with multiple CPUs must
  126. * provide a HW restart implementation, to ensure that all CPUs reset at once.
  127. * This is required so that any code running after reset on the primary CPU
  128. * doesn't have to co-ordinate with other CPUs to ensure they aren't still
  129. * executing pre-reset code, and using RAM that the primary CPU's code wishes
  130. * to use. Implementing such co-ordination would be essentially impossible.
  131. */
  132. void machine_restart(char *cmd)
  133. {
  134. /* Disable interrupts first */
  135. local_irq_disable();
  136. smp_send_stop();
  137. /*
  138. * UpdateCapsule() depends on the system being reset via
  139. * ResetSystem().
  140. */
  141. if (efi_enabled(EFI_RUNTIME_SERVICES))
  142. efi_reboot(reboot_mode, NULL);
  143. /* Now call the architecture specific reboot code. */
  144. if (arm_pm_restart)
  145. arm_pm_restart(reboot_mode, cmd);
  146. else
  147. do_kernel_restart(cmd);
  148. /*
  149. * Whoops - the architecture was unable to reboot.
  150. */
  151. printk("Reboot failed -- System halted\n");
  152. while (1);
  153. }
  154. void __show_regs(struct pt_regs *regs)
  155. {
  156. int i, top_reg;
  157. u64 lr, sp;
  158. if (compat_user_mode(regs)) {
  159. lr = regs->compat_lr;
  160. sp = regs->compat_sp;
  161. top_reg = 12;
  162. } else {
  163. lr = regs->regs[30];
  164. sp = regs->sp;
  165. top_reg = 29;
  166. }
  167. show_regs_print_info(KERN_DEFAULT);
  168. print_symbol("PC is at %s\n", instruction_pointer(regs));
  169. print_symbol("LR is at %s\n", lr);
  170. printk("pc : [<%016llx>] lr : [<%016llx>] pstate: %08llx\n",
  171. regs->pc, lr, regs->pstate);
  172. printk("sp : %016llx\n", sp);
  173. i = top_reg;
  174. while (i >= 0) {
  175. printk("x%-2d: %016llx ", i, regs->regs[i]);
  176. i--;
  177. if (i % 2 == 0) {
  178. pr_cont("x%-2d: %016llx ", i, regs->regs[i]);
  179. i--;
  180. }
  181. pr_cont("\n");
  182. }
  183. printk("\n");
  184. }
  185. void show_regs(struct pt_regs * regs)
  186. {
  187. printk("\n");
  188. __show_regs(regs);
  189. }
  190. static void tls_thread_flush(void)
  191. {
  192. write_sysreg(0, tpidr_el0);
  193. if (is_compat_task()) {
  194. current->thread.tp_value = 0;
  195. /*
  196. * We need to ensure ordering between the shadow state and the
  197. * hardware state, so that we don't corrupt the hardware state
  198. * with a stale shadow state during context switch.
  199. */
  200. barrier();
  201. write_sysreg(0, tpidrro_el0);
  202. }
  203. }
  204. void flush_thread(void)
  205. {
  206. fpsimd_flush_thread();
  207. tls_thread_flush();
  208. flush_ptrace_hw_breakpoint(current);
  209. }
  210. void release_thread(struct task_struct *dead_task)
  211. {
  212. }
  213. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  214. {
  215. if (current->mm)
  216. fpsimd_preserve_current_state();
  217. *dst = *src;
  218. return 0;
  219. }
  220. asmlinkage void ret_from_fork(void) asm("ret_from_fork");
  221. int copy_thread(unsigned long clone_flags, unsigned long stack_start,
  222. unsigned long stk_sz, struct task_struct *p)
  223. {
  224. struct pt_regs *childregs = task_pt_regs(p);
  225. memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
  226. if (likely(!(p->flags & PF_KTHREAD))) {
  227. *childregs = *current_pt_regs();
  228. childregs->regs[0] = 0;
  229. /*
  230. * Read the current TLS pointer from tpidr_el0 as it may be
  231. * out-of-sync with the saved value.
  232. */
  233. *task_user_tls(p) = read_sysreg(tpidr_el0);
  234. if (stack_start) {
  235. if (is_compat_thread(task_thread_info(p)))
  236. childregs->compat_sp = stack_start;
  237. else
  238. childregs->sp = stack_start;
  239. }
  240. /*
  241. * If a TLS pointer was passed to clone (4th argument), use it
  242. * for the new thread.
  243. */
  244. if (clone_flags & CLONE_SETTLS)
  245. p->thread.tp_value = childregs->regs[3];
  246. } else {
  247. memset(childregs, 0, sizeof(struct pt_regs));
  248. childregs->pstate = PSR_MODE_EL1h;
  249. if (IS_ENABLED(CONFIG_ARM64_UAO) &&
  250. cpus_have_cap(ARM64_HAS_UAO))
  251. childregs->pstate |= PSR_UAO_BIT;
  252. p->thread.cpu_context.x19 = stack_start;
  253. p->thread.cpu_context.x20 = stk_sz;
  254. }
  255. p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
  256. p->thread.cpu_context.sp = (unsigned long)childregs;
  257. ptrace_hw_copy_thread(p);
  258. return 0;
  259. }
  260. static void tls_thread_switch(struct task_struct *next)
  261. {
  262. unsigned long tpidr, tpidrro;
  263. tpidr = read_sysreg(tpidr_el0);
  264. *task_user_tls(current) = tpidr;
  265. tpidr = *task_user_tls(next);
  266. tpidrro = is_compat_thread(task_thread_info(next)) ?
  267. next->thread.tp_value : 0;
  268. write_sysreg(tpidr, tpidr_el0);
  269. write_sysreg(tpidrro, tpidrro_el0);
  270. }
  271. /* Restore the UAO state depending on next's addr_limit */
  272. void uao_thread_switch(struct task_struct *next)
  273. {
  274. if (IS_ENABLED(CONFIG_ARM64_UAO)) {
  275. if (task_thread_info(next)->addr_limit == KERNEL_DS)
  276. asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
  277. else
  278. asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO));
  279. }
  280. }
  281. /*
  282. * We store our current task in sp_el0, which is clobbered by userspace. Keep a
  283. * shadow copy so that we can restore this upon entry from userspace.
  284. *
  285. * This is *only* for exception entry from EL0, and is not valid until we
  286. * __switch_to() a user task.
  287. */
  288. DEFINE_PER_CPU(struct task_struct *, __entry_task);
  289. static void entry_task_switch(struct task_struct *next)
  290. {
  291. __this_cpu_write(__entry_task, next);
  292. }
  293. /*
  294. * Thread switching.
  295. */
  296. struct task_struct *__switch_to(struct task_struct *prev,
  297. struct task_struct *next)
  298. {
  299. struct task_struct *last;
  300. fpsimd_thread_switch(next);
  301. tls_thread_switch(next);
  302. hw_breakpoint_thread_switch(next);
  303. contextidr_thread_switch(next);
  304. entry_task_switch(next);
  305. uao_thread_switch(next);
  306. /*
  307. * Complete any pending TLB or cache maintenance on this CPU in case
  308. * the thread migrates to a different CPU.
  309. */
  310. dsb(ish);
  311. /* the actual thread switch */
  312. last = cpu_switch_to(prev, next);
  313. return last;
  314. }
  315. unsigned long get_wchan(struct task_struct *p)
  316. {
  317. struct stackframe frame;
  318. unsigned long stack_page, ret = 0;
  319. int count = 0;
  320. if (!p || p == current || p->state == TASK_RUNNING)
  321. return 0;
  322. stack_page = (unsigned long)try_get_task_stack(p);
  323. if (!stack_page)
  324. return 0;
  325. frame.fp = thread_saved_fp(p);
  326. frame.sp = thread_saved_sp(p);
  327. frame.pc = thread_saved_pc(p);
  328. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  329. frame.graph = p->curr_ret_stack;
  330. #endif
  331. do {
  332. if (frame.sp < stack_page ||
  333. frame.sp >= stack_page + THREAD_SIZE ||
  334. unwind_frame(p, &frame))
  335. goto out;
  336. if (!in_sched_functions(frame.pc)) {
  337. ret = frame.pc;
  338. goto out;
  339. }
  340. } while (count ++ < 16);
  341. out:
  342. put_task_stack(p);
  343. return ret;
  344. }
  345. unsigned long arch_align_stack(unsigned long sp)
  346. {
  347. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  348. sp -= get_random_int() & ~PAGE_MASK;
  349. return sp & ~0xf;
  350. }
  351. unsigned long arch_randomize_brk(struct mm_struct *mm)
  352. {
  353. if (is_compat_task())
  354. return randomize_page(mm->brk, 0x02000000);
  355. else
  356. return randomize_page(mm->brk, 0x40000000);
  357. }