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