process.c 7.1 KB

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