process.c 7.8 KB

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