process.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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/export.h>
  22. #include <linux/sched.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/stddef.h>
  26. #include <linux/unistd.h>
  27. #include <linux/user.h>
  28. #include <linux/delay.h>
  29. #include <linux/reboot.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/kallsyms.h>
  32. #include <linux/init.h>
  33. #include <linux/cpu.h>
  34. #include <linux/elfcore.h>
  35. #include <linux/pm.h>
  36. #include <linux/tick.h>
  37. #include <linux/utsname.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/random.h>
  40. #include <linux/hw_breakpoint.h>
  41. #include <linux/personality.h>
  42. #include <linux/notifier.h>
  43. #include <asm/compat.h>
  44. #include <asm/cacheflush.h>
  45. #include <asm/fpsimd.h>
  46. #include <asm/mmu_context.h>
  47. #include <asm/processor.h>
  48. #include <asm/stacktrace.h>
  49. static void setup_restart(void)
  50. {
  51. /*
  52. * Tell the mm system that we are going to reboot -
  53. * we may need it to insert some 1:1 mappings so that
  54. * soft boot works.
  55. */
  56. setup_mm_for_reboot();
  57. /* Clean and invalidate caches */
  58. flush_cache_all();
  59. /* Turn D-cache off */
  60. cpu_cache_off();
  61. /* Push out any further dirty data, and ensure cache is empty */
  62. flush_cache_all();
  63. }
  64. void soft_restart(unsigned long addr)
  65. {
  66. typedef void (*phys_reset_t)(unsigned long);
  67. phys_reset_t phys_reset;
  68. setup_restart();
  69. /* Switch to the identity mapping */
  70. phys_reset = (phys_reset_t)virt_to_phys(cpu_reset);
  71. phys_reset(addr);
  72. /* Should never get here */
  73. BUG();
  74. }
  75. /*
  76. * Function pointers to optional machine specific functions
  77. */
  78. void (*pm_power_off)(void);
  79. EXPORT_SYMBOL_GPL(pm_power_off);
  80. void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
  81. EXPORT_SYMBOL_GPL(arm_pm_restart);
  82. /*
  83. * This is our default idle handler.
  84. */
  85. void arch_cpu_idle(void)
  86. {
  87. /*
  88. * This should do all the clock switching and wait for interrupt
  89. * tricks
  90. */
  91. cpu_do_idle();
  92. local_irq_enable();
  93. }
  94. #ifdef CONFIG_HOTPLUG_CPU
  95. void arch_cpu_idle_dead(void)
  96. {
  97. cpu_die();
  98. }
  99. #endif
  100. void machine_shutdown(void)
  101. {
  102. #ifdef CONFIG_SMP
  103. smp_send_stop();
  104. #endif
  105. }
  106. void machine_halt(void)
  107. {
  108. machine_shutdown();
  109. while (1);
  110. }
  111. void machine_power_off(void)
  112. {
  113. machine_shutdown();
  114. if (pm_power_off)
  115. pm_power_off();
  116. }
  117. void machine_restart(char *cmd)
  118. {
  119. machine_shutdown();
  120. /* Disable interrupts first */
  121. local_irq_disable();
  122. /* Now call the architecture specific reboot code. */
  123. if (arm_pm_restart)
  124. arm_pm_restart(reboot_mode, cmd);
  125. /*
  126. * Whoops - the architecture was unable to reboot.
  127. */
  128. printk("Reboot failed -- System halted\n");
  129. while (1);
  130. }
  131. void __show_regs(struct pt_regs *regs)
  132. {
  133. int i, top_reg;
  134. u64 lr, sp;
  135. if (compat_user_mode(regs)) {
  136. lr = regs->compat_lr;
  137. sp = regs->compat_sp;
  138. top_reg = 12;
  139. } else {
  140. lr = regs->regs[30];
  141. sp = regs->sp;
  142. top_reg = 29;
  143. }
  144. show_regs_print_info(KERN_DEFAULT);
  145. print_symbol("PC is at %s\n", instruction_pointer(regs));
  146. print_symbol("LR is at %s\n", lr);
  147. printk("pc : [<%016llx>] lr : [<%016llx>] pstate: %08llx\n",
  148. regs->pc, lr, regs->pstate);
  149. printk("sp : %016llx\n", sp);
  150. for (i = top_reg; i >= 0; i--) {
  151. printk("x%-2d: %016llx ", i, regs->regs[i]);
  152. if (i % 2 == 0)
  153. printk("\n");
  154. }
  155. printk("\n");
  156. }
  157. void show_regs(struct pt_regs * regs)
  158. {
  159. printk("\n");
  160. __show_regs(regs);
  161. }
  162. /*
  163. * Free current thread data structures etc..
  164. */
  165. void exit_thread(void)
  166. {
  167. }
  168. void flush_thread(void)
  169. {
  170. fpsimd_flush_thread();
  171. flush_ptrace_hw_breakpoint(current);
  172. }
  173. void release_thread(struct task_struct *dead_task)
  174. {
  175. }
  176. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  177. {
  178. fpsimd_save_state(&current->thread.fpsimd_state);
  179. *dst = *src;
  180. return 0;
  181. }
  182. asmlinkage void ret_from_fork(void) asm("ret_from_fork");
  183. int copy_thread(unsigned long clone_flags, unsigned long stack_start,
  184. unsigned long stk_sz, struct task_struct *p)
  185. {
  186. struct pt_regs *childregs = task_pt_regs(p);
  187. unsigned long tls = p->thread.tp_value;
  188. memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
  189. if (likely(!(p->flags & PF_KTHREAD))) {
  190. *childregs = *current_pt_regs();
  191. childregs->regs[0] = 0;
  192. if (is_compat_thread(task_thread_info(p))) {
  193. if (stack_start)
  194. childregs->compat_sp = stack_start;
  195. } else {
  196. /*
  197. * Read the current TLS pointer from tpidr_el0 as it may be
  198. * out-of-sync with the saved value.
  199. */
  200. asm("mrs %0, tpidr_el0" : "=r" (tls));
  201. if (stack_start) {
  202. /* 16-byte aligned stack mandatory on AArch64 */
  203. if (stack_start & 15)
  204. return -EINVAL;
  205. childregs->sp = stack_start;
  206. }
  207. }
  208. /*
  209. * If a TLS pointer was passed to clone (4th argument), use it
  210. * for the new thread.
  211. */
  212. if (clone_flags & CLONE_SETTLS)
  213. tls = childregs->regs[3];
  214. } else {
  215. memset(childregs, 0, sizeof(struct pt_regs));
  216. childregs->pstate = PSR_MODE_EL1h;
  217. p->thread.cpu_context.x19 = stack_start;
  218. p->thread.cpu_context.x20 = stk_sz;
  219. }
  220. p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
  221. p->thread.cpu_context.sp = (unsigned long)childregs;
  222. p->thread.tp_value = tls;
  223. ptrace_hw_copy_thread(p);
  224. return 0;
  225. }
  226. static void tls_thread_switch(struct task_struct *next)
  227. {
  228. unsigned long tpidr, tpidrro;
  229. if (!is_compat_task()) {
  230. asm("mrs %0, tpidr_el0" : "=r" (tpidr));
  231. current->thread.tp_value = tpidr;
  232. }
  233. if (is_compat_thread(task_thread_info(next))) {
  234. tpidr = 0;
  235. tpidrro = next->thread.tp_value;
  236. } else {
  237. tpidr = next->thread.tp_value;
  238. tpidrro = 0;
  239. }
  240. asm(
  241. " msr tpidr_el0, %0\n"
  242. " msr tpidrro_el0, %1"
  243. : : "r" (tpidr), "r" (tpidrro));
  244. }
  245. /*
  246. * Thread switching.
  247. */
  248. struct task_struct *__switch_to(struct task_struct *prev,
  249. struct task_struct *next)
  250. {
  251. struct task_struct *last;
  252. fpsimd_thread_switch(next);
  253. tls_thread_switch(next);
  254. hw_breakpoint_thread_switch(next);
  255. contextidr_thread_switch(next);
  256. /*
  257. * Complete any pending TLB or cache maintenance on this CPU in case
  258. * the thread migrates to a different CPU.
  259. */
  260. dsb();
  261. /* the actual thread switch */
  262. last = cpu_switch_to(prev, next);
  263. return last;
  264. }
  265. unsigned long get_wchan(struct task_struct *p)
  266. {
  267. struct stackframe frame;
  268. unsigned long stack_page;
  269. int count = 0;
  270. if (!p || p == current || p->state == TASK_RUNNING)
  271. return 0;
  272. frame.fp = thread_saved_fp(p);
  273. frame.sp = thread_saved_sp(p);
  274. frame.pc = thread_saved_pc(p);
  275. stack_page = (unsigned long)task_stack_page(p);
  276. do {
  277. if (frame.sp < stack_page ||
  278. frame.sp >= stack_page + THREAD_SIZE ||
  279. unwind_frame(&frame))
  280. return 0;
  281. if (!in_sched_functions(frame.pc))
  282. return frame.pc;
  283. } while (count ++ < 16);
  284. return 0;
  285. }
  286. unsigned long arch_align_stack(unsigned long sp)
  287. {
  288. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  289. sp -= get_random_int() & ~PAGE_MASK;
  290. return sp & ~0xf;
  291. }
  292. static unsigned long randomize_base(unsigned long base)
  293. {
  294. unsigned long range_end = base + (STACK_RND_MASK << PAGE_SHIFT) + 1;
  295. return randomize_range(base, range_end, 0) ? : base;
  296. }
  297. unsigned long arch_randomize_brk(struct mm_struct *mm)
  298. {
  299. return randomize_base(mm->brk);
  300. }
  301. unsigned long randomize_et_dyn(unsigned long base)
  302. {
  303. return randomize_base(base);
  304. }