process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * arch/xtensa/kernel/process.c
  3. *
  4. * Xtensa Processor version.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2001 - 2005 Tensilica Inc.
  11. *
  12. * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
  13. * Chris Zankel <chris@zankel.net>
  14. * Marc Gauthier <marc@tensilica.com, marc@alumni.uwaterloo.ca>
  15. * Kevin Chea
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/sched.h>
  19. #include <linux/sched/debug.h>
  20. #include <linux/sched/task.h>
  21. #include <linux/sched/task_stack.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mm.h>
  24. #include <linux/smp.h>
  25. #include <linux/stddef.h>
  26. #include <linux/unistd.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/elf.h>
  29. #include <linux/hw_breakpoint.h>
  30. #include <linux/init.h>
  31. #include <linux/prctl.h>
  32. #include <linux/init_task.h>
  33. #include <linux/module.h>
  34. #include <linux/mqueue.h>
  35. #include <linux/fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/rcupdate.h>
  38. #include <linux/uaccess.h>
  39. #include <asm/io.h>
  40. #include <asm/processor.h>
  41. #include <asm/platform.h>
  42. #include <asm/mmu.h>
  43. #include <asm/irq.h>
  44. #include <linux/atomic.h>
  45. #include <asm/asm-offsets.h>
  46. #include <asm/regs.h>
  47. #include <asm/hw_breakpoint.h>
  48. #include <asm/sections.h>
  49. #include <asm/traps.h>
  50. extern void ret_from_fork(void);
  51. extern void ret_from_kernel_thread(void);
  52. void (*pm_power_off)(void) = NULL;
  53. EXPORT_SYMBOL(pm_power_off);
  54. #ifdef CONFIG_STACKPROTECTOR
  55. #include <linux/stackprotector.h>
  56. unsigned long __stack_chk_guard __read_mostly;
  57. EXPORT_SYMBOL(__stack_chk_guard);
  58. #endif
  59. #if XTENSA_HAVE_COPROCESSORS
  60. void local_coprocessors_flush_release_all(void)
  61. {
  62. struct thread_info **coprocessor_owner;
  63. struct thread_info *unique_owner[XCHAL_CP_MAX];
  64. int n = 0;
  65. int i, j;
  66. coprocessor_owner = this_cpu_ptr(&exc_table)->coprocessor_owner;
  67. xtensa_set_sr(XCHAL_CP_MASK, cpenable);
  68. for (i = 0; i < XCHAL_CP_MAX; i++) {
  69. struct thread_info *ti = coprocessor_owner[i];
  70. if (ti) {
  71. coprocessor_flush(ti, i);
  72. for (j = 0; j < n; j++)
  73. if (unique_owner[j] == ti)
  74. break;
  75. if (j == n)
  76. unique_owner[n++] = ti;
  77. coprocessor_owner[i] = NULL;
  78. }
  79. }
  80. for (i = 0; i < n; i++) {
  81. /* pairs with memw (1) in fast_coprocessor and memw in switch_to */
  82. smp_wmb();
  83. unique_owner[i]->cpenable = 0;
  84. }
  85. xtensa_set_sr(0, cpenable);
  86. }
  87. static void local_coprocessor_release_all(void *info)
  88. {
  89. struct thread_info *ti = info;
  90. struct thread_info **coprocessor_owner;
  91. int i;
  92. coprocessor_owner = this_cpu_ptr(&exc_table)->coprocessor_owner;
  93. /* Walk through all cp owners and release it for the requested one. */
  94. for (i = 0; i < XCHAL_CP_MAX; i++) {
  95. if (coprocessor_owner[i] == ti)
  96. coprocessor_owner[i] = NULL;
  97. }
  98. /* pairs with memw (1) in fast_coprocessor and memw in switch_to */
  99. smp_wmb();
  100. ti->cpenable = 0;
  101. if (ti == current_thread_info())
  102. xtensa_set_sr(0, cpenable);
  103. }
  104. void coprocessor_release_all(struct thread_info *ti)
  105. {
  106. if (ti->cpenable) {
  107. /* pairs with memw (2) in fast_coprocessor */
  108. smp_rmb();
  109. smp_call_function_single(ti->cp_owner_cpu,
  110. local_coprocessor_release_all,
  111. ti, true);
  112. }
  113. }
  114. static void local_coprocessor_flush_all(void *info)
  115. {
  116. struct thread_info *ti = info;
  117. struct thread_info **coprocessor_owner;
  118. unsigned long old_cpenable;
  119. int i;
  120. coprocessor_owner = this_cpu_ptr(&exc_table)->coprocessor_owner;
  121. old_cpenable = xtensa_xsr(ti->cpenable, cpenable);
  122. for (i = 0; i < XCHAL_CP_MAX; i++) {
  123. if (coprocessor_owner[i] == ti)
  124. coprocessor_flush(ti, i);
  125. }
  126. xtensa_set_sr(old_cpenable, cpenable);
  127. }
  128. void coprocessor_flush_all(struct thread_info *ti)
  129. {
  130. if (ti->cpenable) {
  131. /* pairs with memw (2) in fast_coprocessor */
  132. smp_rmb();
  133. smp_call_function_single(ti->cp_owner_cpu,
  134. local_coprocessor_flush_all,
  135. ti, true);
  136. }
  137. }
  138. static void local_coprocessor_flush_release_all(void *info)
  139. {
  140. local_coprocessor_flush_all(info);
  141. local_coprocessor_release_all(info);
  142. }
  143. void coprocessor_flush_release_all(struct thread_info *ti)
  144. {
  145. if (ti->cpenable) {
  146. /* pairs with memw (2) in fast_coprocessor */
  147. smp_rmb();
  148. smp_call_function_single(ti->cp_owner_cpu,
  149. local_coprocessor_flush_release_all,
  150. ti, true);
  151. }
  152. }
  153. #endif
  154. /*
  155. * Powermanagement idle function, if any is provided by the platform.
  156. */
  157. void arch_cpu_idle(void)
  158. {
  159. platform_idle();
  160. raw_local_irq_disable();
  161. }
  162. /*
  163. * This is called when the thread calls exit().
  164. */
  165. void exit_thread(struct task_struct *tsk)
  166. {
  167. #if XTENSA_HAVE_COPROCESSORS
  168. coprocessor_release_all(task_thread_info(tsk));
  169. #endif
  170. }
  171. /*
  172. * Flush thread state. This is called when a thread does an execve()
  173. * Note that we flush coprocessor registers for the case execve fails.
  174. */
  175. void flush_thread(void)
  176. {
  177. #if XTENSA_HAVE_COPROCESSORS
  178. struct thread_info *ti = current_thread_info();
  179. coprocessor_flush_release_all(ti);
  180. #endif
  181. flush_ptrace_hw_breakpoint(current);
  182. }
  183. /*
  184. * this gets called so that we can store coprocessor state into memory and
  185. * copy the current task into the new thread.
  186. */
  187. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  188. {
  189. #if XTENSA_HAVE_COPROCESSORS
  190. coprocessor_flush_all(task_thread_info(src));
  191. #endif
  192. *dst = *src;
  193. return 0;
  194. }
  195. /*
  196. * Copy thread.
  197. *
  198. * There are two modes in which this function is called:
  199. * 1) Userspace thread creation,
  200. * regs != NULL, usp_thread_fn is userspace stack pointer.
  201. * It is expected to copy parent regs (in case CLONE_VM is not set
  202. * in the clone_flags) and set up passed usp in the childregs.
  203. * 2) Kernel thread creation,
  204. * regs == NULL, usp_thread_fn is the function to run in the new thread
  205. * and thread_fn_arg is its parameter.
  206. * childregs are not used for the kernel threads.
  207. *
  208. * The stack layout for the new thread looks like this:
  209. *
  210. * +------------------------+
  211. * | childregs |
  212. * +------------------------+ <- thread.sp = sp in dummy-frame
  213. * | dummy-frame | (saved in dummy-frame spill-area)
  214. * +------------------------+
  215. *
  216. * We create a dummy frame to return to either ret_from_fork or
  217. * ret_from_kernel_thread:
  218. * a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4)
  219. * sp points to itself (thread.sp)
  220. * a2, a3 are unused for userspace threads,
  221. * a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
  222. *
  223. * Note: This is a pristine frame, so we don't need any spill region on top of
  224. * childregs.
  225. *
  226. * The fun part: if we're keeping the same VM (i.e. cloning a thread,
  227. * not an entire process), we're normally given a new usp, and we CANNOT share
  228. * any live address register windows. If we just copy those live frames over,
  229. * the two threads (parent and child) will overflow the same frames onto the
  230. * parent stack at different times, likely corrupting the parent stack (esp.
  231. * if the parent returns from functions that called clone() and calls new
  232. * ones, before the child overflows its now old copies of its parent windows).
  233. * One solution is to spill windows to the parent stack, but that's fairly
  234. * involved. Much simpler to just not copy those live frames across.
  235. */
  236. int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  237. {
  238. unsigned long clone_flags = args->flags;
  239. unsigned long usp_thread_fn = args->stack;
  240. unsigned long tls = args->tls;
  241. struct pt_regs *childregs = task_pt_regs(p);
  242. #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
  243. struct thread_info *ti;
  244. #endif
  245. #if defined(__XTENSA_WINDOWED_ABI__)
  246. /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
  247. SPILL_SLOT(childregs, 1) = (unsigned long)childregs;
  248. SPILL_SLOT(childregs, 0) = 0;
  249. p->thread.sp = (unsigned long)childregs;
  250. #elif defined(__XTENSA_CALL0_ABI__)
  251. /* Reserve 16 bytes for the _switch_to stack frame. */
  252. p->thread.sp = (unsigned long)childregs - 16;
  253. #else
  254. #error Unsupported Xtensa ABI
  255. #endif
  256. if (!args->fn) {
  257. struct pt_regs *regs = current_pt_regs();
  258. unsigned long usp = usp_thread_fn ?
  259. usp_thread_fn : regs->areg[1];
  260. p->thread.ra = MAKE_RA_FOR_CALL(
  261. (unsigned long)ret_from_fork, 0x1);
  262. *childregs = *regs;
  263. childregs->areg[1] = usp;
  264. childregs->areg[2] = 0;
  265. /* When sharing memory with the parent thread, the child
  266. usually starts on a pristine stack, so we have to reset
  267. windowbase, windowstart and wmask.
  268. (Note that such a new thread is required to always create
  269. an initial call4 frame)
  270. The exception is vfork, where the new thread continues to
  271. run on the parent's stack until it calls execve. This could
  272. be a call8 or call12, which requires a legal stack frame
  273. of the previous caller for the overflow handlers to work.
  274. (Note that it's always legal to overflow live registers).
  275. In this case, ensure to spill at least the stack pointer
  276. of that frame. */
  277. if (clone_flags & CLONE_VM) {
  278. /* check that caller window is live and same stack */
  279. int len = childregs->wmask & ~0xf;
  280. if (regs->areg[1] == usp && len != 0) {
  281. int callinc = (regs->areg[0] >> 30) & 3;
  282. int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
  283. put_user(regs->areg[caller_ars+1],
  284. (unsigned __user*)(usp - 12));
  285. }
  286. childregs->wmask = 1;
  287. childregs->windowstart = 1;
  288. childregs->windowbase = 0;
  289. }
  290. if (clone_flags & CLONE_SETTLS)
  291. childregs->threadptr = tls;
  292. } else {
  293. p->thread.ra = MAKE_RA_FOR_CALL(
  294. (unsigned long)ret_from_kernel_thread, 1);
  295. /* pass parameters to ret_from_kernel_thread: */
  296. #if defined(__XTENSA_WINDOWED_ABI__)
  297. /*
  298. * a2 = thread_fn, a3 = thread_fn arg.
  299. * Window underflow will load registers from the
  300. * spill slots on the stack on return from _switch_to.
  301. */
  302. SPILL_SLOT(childregs, 2) = (unsigned long)args->fn;
  303. SPILL_SLOT(childregs, 3) = (unsigned long)args->fn_arg;
  304. #elif defined(__XTENSA_CALL0_ABI__)
  305. /*
  306. * a12 = thread_fn, a13 = thread_fn arg.
  307. * _switch_to epilogue will load registers from the stack.
  308. */
  309. ((unsigned long *)p->thread.sp)[0] = (unsigned long)args->fn;
  310. ((unsigned long *)p->thread.sp)[1] = (unsigned long)args->fn_arg;
  311. #else
  312. #error Unsupported Xtensa ABI
  313. #endif
  314. /* Childregs are only used when we're going to userspace
  315. * in which case start_thread will set them up.
  316. */
  317. }
  318. #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
  319. ti = task_thread_info(p);
  320. ti->cpenable = 0;
  321. #endif
  322. clear_ptrace_hw_breakpoint(p);
  323. return 0;
  324. }
  325. /*
  326. * These bracket the sleeping functions..
  327. */
  328. unsigned long __get_wchan(struct task_struct *p)
  329. {
  330. unsigned long sp, pc;
  331. unsigned long stack_page = (unsigned long) task_stack_page(p);
  332. int count = 0;
  333. sp = p->thread.sp;
  334. pc = MAKE_PC_FROM_RA(p->thread.ra, _text);
  335. do {
  336. if (sp < stack_page + sizeof(struct task_struct) ||
  337. sp >= (stack_page + THREAD_SIZE) ||
  338. pc == 0)
  339. return 0;
  340. if (!in_sched_functions(pc))
  341. return pc;
  342. /* Stack layout: sp-4: ra, sp-3: sp' */
  343. pc = MAKE_PC_FROM_RA(SPILL_SLOT(sp, 0), _text);
  344. sp = SPILL_SLOT(sp, 1);
  345. } while (count++ < 16);
  346. return 0;
  347. }