process.c 7.8 KB

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