process.c 9.7 KB

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