process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. void soft_restart(unsigned long addr)
  56. {
  57. setup_mm_for_reboot();
  58. cpu_soft_restart(virt_to_phys(cpu_reset), addr);
  59. /* Should never get here */
  60. BUG();
  61. }
  62. /*
  63. * Function pointers to optional machine specific functions
  64. */
  65. void (*pm_power_off)(void);
  66. EXPORT_SYMBOL_GPL(pm_power_off);
  67. void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
  68. /*
  69. * This is our default idle handler.
  70. */
  71. void arch_cpu_idle(void)
  72. {
  73. /*
  74. * This should do all the clock switching and wait for interrupt
  75. * tricks
  76. */
  77. cpu_do_idle();
  78. local_irq_enable();
  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. pr_emerg("machine_power_off, pm_power_off(%p)\n", pm_power_off);
  121. dump_stack();
  122. if (pm_power_off)
  123. pm_power_off();
  124. }
  125. /*
  126. * Restart requires that the secondary CPUs stop performing any activity
  127. * while the primary CPU resets the system. Systems with a single CPU can
  128. * use soft_restart() as their machine descriptor's .restart hook, since that
  129. * will cause the only available CPU to reset. Systems with multiple CPUs must
  130. * provide a HW restart implementation, to ensure that all CPUs reset at once.
  131. * This is required so that any code running after reset on the primary CPU
  132. * doesn't have to co-ordinate with other CPUs to ensure they aren't still
  133. * executing pre-reset code, and using RAM that the primary CPU's code wishes
  134. * to use. Implementing such co-ordination would be essentially impossible.
  135. */
  136. void machine_restart(char *cmd)
  137. {
  138. /* Disable interrupts first */
  139. local_irq_disable();
  140. smp_send_stop();
  141. /* Now call the architecture specific reboot code. */
  142. pr_emerg("machine_restart, arm_pm_restart(%p)\n", arm_pm_restart);
  143. if (arm_pm_restart)
  144. arm_pm_restart(reboot_mode, cmd);
  145. else
  146. do_kernel_restart(cmd);
  147. /*
  148. * Whoops - the architecture was unable to reboot.
  149. */
  150. printk("Reboot failed -- System halted\n");
  151. while (1);
  152. }
  153. /*
  154. * dump a block of kernel memory from around the given address
  155. */
  156. static void show_data(unsigned long addr, int nbytes, const char *name)
  157. {
  158. int i, j;
  159. int nlines;
  160. u32 *p;
  161. /*
  162. * don't attempt to dump non-kernel addresses or
  163. * values that are probably just small negative numbers
  164. */
  165. if (addr < PAGE_OFFSET || addr > -256UL)
  166. return;
  167. printk("\n%s: %#lx:\n", name, addr);
  168. /*
  169. * round address down to a 32 bit boundary
  170. * and always dump a multiple of 32 bytes
  171. */
  172. p = (u32 *)(addr & ~(sizeof(u32) - 1));
  173. nbytes += (addr & (sizeof(u32) - 1));
  174. nlines = (nbytes + 31) / 32;
  175. for (i = 0; i < nlines; i++) {
  176. /*
  177. * just display low 16 bits of address to keep
  178. * each line of the dump < 80 characters
  179. */
  180. printk("%04lx ", (unsigned long)p & 0xffff);
  181. for (j = 0; j < 8; j++) {
  182. u32 data;
  183. if (probe_kernel_address(p, data)) {
  184. printk(" ********");
  185. } else {
  186. printk(" %08x", data);
  187. }
  188. ++p;
  189. }
  190. printk("\n");
  191. }
  192. }
  193. static void show_extra_register_data(struct pt_regs *regs, int nbytes)
  194. {
  195. mm_segment_t fs;
  196. unsigned int i;
  197. fs = get_fs();
  198. set_fs(KERNEL_DS);
  199. show_data(regs->pc - nbytes, nbytes * 2, "PC");
  200. show_data(regs->regs[30] - nbytes, nbytes * 2, "LR");
  201. show_data(regs->sp - nbytes, nbytes * 2, "SP");
  202. for (i = 0; i < 30; i++) {
  203. char name[4];
  204. snprintf(name, sizeof(name), "X%u", i);
  205. show_data(regs->regs[i] - nbytes, nbytes * 2, name);
  206. }
  207. set_fs(fs);
  208. }
  209. void __show_regs(struct pt_regs *regs)
  210. {
  211. int i, top_reg;
  212. u64 lr, sp;
  213. if (compat_user_mode(regs)) {
  214. lr = regs->compat_lr;
  215. sp = regs->compat_sp;
  216. top_reg = 12;
  217. } else {
  218. lr = regs->regs[30];
  219. sp = regs->sp;
  220. top_reg = 29;
  221. }
  222. show_regs_print_info(KERN_DEFAULT);
  223. print_symbol("PC is at %s\n", instruction_pointer(regs));
  224. print_symbol("LR is at %s\n", lr);
  225. printk("pc : [<%016llx>] lr : [<%016llx>] pstate: %08llx\n",
  226. regs->pc, lr, regs->pstate);
  227. printk("sp : %016llx\n", sp);
  228. for (i = top_reg; i >= 0; i--) {
  229. printk("x%-2d: %016llx ", i, regs->regs[i]);
  230. if (i % 2 == 0)
  231. printk("\n");
  232. }
  233. if (!user_mode(regs))
  234. show_extra_register_data(regs, 128);
  235. printk("\n");
  236. }
  237. void show_regs(struct pt_regs * regs)
  238. {
  239. printk("\n");
  240. __show_regs(regs);
  241. }
  242. /*
  243. * Free current thread data structures etc..
  244. */
  245. void exit_thread(void)
  246. {
  247. }
  248. static void tls_thread_flush(void)
  249. {
  250. asm ("msr tpidr_el0, xzr");
  251. if (is_compat_task()) {
  252. current->thread.tp_value = 0;
  253. /*
  254. * We need to ensure ordering between the shadow state and the
  255. * hardware state, so that we don't corrupt the hardware state
  256. * with a stale shadow state during context switch.
  257. */
  258. barrier();
  259. asm ("msr tpidrro_el0, xzr");
  260. }
  261. }
  262. void flush_thread(void)
  263. {
  264. fpsimd_flush_thread();
  265. tls_thread_flush();
  266. flush_ptrace_hw_breakpoint(current);
  267. }
  268. void release_thread(struct task_struct *dead_task)
  269. {
  270. }
  271. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  272. {
  273. fpsimd_preserve_current_state();
  274. *dst = *src;
  275. return 0;
  276. }
  277. asmlinkage void ret_from_fork(void) asm("ret_from_fork");
  278. int copy_thread(unsigned long clone_flags, unsigned long stack_start,
  279. unsigned long stk_sz, struct task_struct *p)
  280. {
  281. struct pt_regs *childregs = task_pt_regs(p);
  282. unsigned long tls = p->thread.tp_value;
  283. memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
  284. if (likely(!(p->flags & PF_KTHREAD))) {
  285. *childregs = *current_pt_regs();
  286. childregs->regs[0] = 0;
  287. if (is_compat_thread(task_thread_info(p))) {
  288. if (stack_start)
  289. childregs->compat_sp = stack_start;
  290. } else {
  291. /*
  292. * Read the current TLS pointer from tpidr_el0 as it may be
  293. * out-of-sync with the saved value.
  294. */
  295. asm("mrs %0, tpidr_el0" : "=r" (tls));
  296. if (stack_start) {
  297. /* 16-byte aligned stack mandatory on AArch64 */
  298. if (stack_start & 15)
  299. return -EINVAL;
  300. childregs->sp = stack_start;
  301. }
  302. }
  303. /*
  304. * If a TLS pointer was passed to clone (4th argument), use it
  305. * for the new thread.
  306. */
  307. if (clone_flags & CLONE_SETTLS)
  308. tls = childregs->regs[3];
  309. } else {
  310. memset(childregs, 0, sizeof(struct pt_regs));
  311. childregs->pstate = PSR_MODE_EL1h;
  312. p->thread.cpu_context.x19 = stack_start;
  313. p->thread.cpu_context.x20 = stk_sz;
  314. }
  315. p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
  316. p->thread.cpu_context.sp = (unsigned long)childregs;
  317. p->thread.tp_value = tls;
  318. ptrace_hw_copy_thread(p);
  319. return 0;
  320. }
  321. static void tls_thread_switch(struct task_struct *next)
  322. {
  323. unsigned long tpidr, tpidrro;
  324. if (!is_compat_task()) {
  325. asm("mrs %0, tpidr_el0" : "=r" (tpidr));
  326. current->thread.tp_value = tpidr;
  327. }
  328. if (is_compat_thread(task_thread_info(next))) {
  329. tpidr = 0;
  330. tpidrro = next->thread.tp_value;
  331. } else {
  332. tpidr = next->thread.tp_value;
  333. tpidrro = 0;
  334. }
  335. asm(
  336. " msr tpidr_el0, %0\n"
  337. " msr tpidrro_el0, %1"
  338. : : "r" (tpidr), "r" (tpidrro));
  339. }
  340. /*
  341. * Thread switching.
  342. */
  343. struct task_struct *__switch_to(struct task_struct *prev,
  344. struct task_struct *next)
  345. {
  346. struct task_struct *last;
  347. fpsimd_thread_switch(next);
  348. tls_thread_switch(next);
  349. hw_breakpoint_thread_switch(next);
  350. contextidr_thread_switch(next);
  351. /*
  352. * Complete any pending TLB or cache maintenance on this CPU in case
  353. * the thread migrates to a different CPU.
  354. */
  355. dsb(ish);
  356. /* the actual thread switch */
  357. last = cpu_switch_to(prev, next);
  358. return last;
  359. }
  360. unsigned long get_wchan(struct task_struct *p)
  361. {
  362. struct stackframe frame;
  363. unsigned long stack_page;
  364. int count = 0;
  365. if (!p || p == current || p->state == TASK_RUNNING)
  366. return 0;
  367. frame.fp = thread_saved_fp(p);
  368. frame.sp = thread_saved_sp(p);
  369. frame.pc = thread_saved_pc(p);
  370. stack_page = (unsigned long)task_stack_page(p);
  371. do {
  372. if (frame.sp < stack_page ||
  373. frame.sp >= stack_page + THREAD_SIZE ||
  374. unwind_frame(&frame))
  375. return 0;
  376. if (!in_sched_functions(frame.pc))
  377. return frame.pc;
  378. } while (count ++ < 16);
  379. return 0;
  380. }
  381. unsigned long arch_align_stack(unsigned long sp)
  382. {
  383. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  384. sp -= get_random_int() & ~PAGE_MASK;
  385. return sp & ~0xf;
  386. }
  387. static unsigned long randomize_base(unsigned long base)
  388. {
  389. unsigned long range_end = base + (STACK_RND_MASK << PAGE_SHIFT) + 1;
  390. return randomize_range(base, range_end, 0) ? : base;
  391. }
  392. unsigned long arch_randomize_brk(struct mm_struct *mm)
  393. {
  394. return randomize_base(mm->brk);
  395. }