process.c 9.1 KB

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