process.c 7.4 KB

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