process.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. extern void ret_from_fork(void);
  49. extern void ret_from_kernel_thread(void);
  50. void (*pm_power_off)(void) = NULL;
  51. EXPORT_SYMBOL(pm_power_off);
  52. #ifdef CONFIG_STACKPROTECTOR
  53. #include <linux/stackprotector.h>
  54. unsigned long __stack_chk_guard __read_mostly;
  55. EXPORT_SYMBOL(__stack_chk_guard);
  56. #endif
  57. #if XTENSA_HAVE_COPROCESSORS
  58. void coprocessor_release_all(struct thread_info *ti)
  59. {
  60. unsigned long cpenable;
  61. int i;
  62. /* Make sure we don't switch tasks during this operation. */
  63. preempt_disable();
  64. /* Walk through all cp owners and release it for the requested one. */
  65. cpenable = ti->cpenable;
  66. for (i = 0; i < XCHAL_CP_MAX; i++) {
  67. if (coprocessor_owner[i] == ti) {
  68. coprocessor_owner[i] = 0;
  69. cpenable &= ~(1 << i);
  70. }
  71. }
  72. ti->cpenable = cpenable;
  73. if (ti == current_thread_info())
  74. xtensa_set_sr(0, cpenable);
  75. preempt_enable();
  76. }
  77. void coprocessor_flush_all(struct thread_info *ti)
  78. {
  79. unsigned long cpenable, old_cpenable;
  80. int i;
  81. preempt_disable();
  82. old_cpenable = xtensa_get_sr(cpenable);
  83. cpenable = ti->cpenable;
  84. xtensa_set_sr(cpenable, cpenable);
  85. for (i = 0; i < XCHAL_CP_MAX; i++) {
  86. if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
  87. coprocessor_flush(ti, i);
  88. cpenable >>= 1;
  89. }
  90. xtensa_set_sr(old_cpenable, cpenable);
  91. preempt_enable();
  92. }
  93. #endif
  94. /*
  95. * Powermanagement idle function, if any is provided by the platform.
  96. */
  97. void arch_cpu_idle(void)
  98. {
  99. platform_idle();
  100. }
  101. /*
  102. * This is called when the thread calls exit().
  103. */
  104. void exit_thread(struct task_struct *tsk)
  105. {
  106. #if XTENSA_HAVE_COPROCESSORS
  107. coprocessor_release_all(task_thread_info(tsk));
  108. #endif
  109. }
  110. /*
  111. * Flush thread state. This is called when a thread does an execve()
  112. * Note that we flush coprocessor registers for the case execve fails.
  113. */
  114. void flush_thread(void)
  115. {
  116. #if XTENSA_HAVE_COPROCESSORS
  117. struct thread_info *ti = current_thread_info();
  118. coprocessor_flush_all(ti);
  119. coprocessor_release_all(ti);
  120. #endif
  121. flush_ptrace_hw_breakpoint(current);
  122. }
  123. /*
  124. * this gets called so that we can store coprocessor state into memory and
  125. * copy the current task into the new thread.
  126. */
  127. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  128. {
  129. #if XTENSA_HAVE_COPROCESSORS
  130. coprocessor_flush_all(task_thread_info(src));
  131. #endif
  132. *dst = *src;
  133. return 0;
  134. }
  135. /*
  136. * Copy thread.
  137. *
  138. * There are two modes in which this function is called:
  139. * 1) Userspace thread creation,
  140. * regs != NULL, usp_thread_fn is userspace stack pointer.
  141. * It is expected to copy parent regs (in case CLONE_VM is not set
  142. * in the clone_flags) and set up passed usp in the childregs.
  143. * 2) Kernel thread creation,
  144. * regs == NULL, usp_thread_fn is the function to run in the new thread
  145. * and thread_fn_arg is its parameter.
  146. * childregs are not used for the kernel threads.
  147. *
  148. * The stack layout for the new thread looks like this:
  149. *
  150. * +------------------------+
  151. * | childregs |
  152. * +------------------------+ <- thread.sp = sp in dummy-frame
  153. * | dummy-frame | (saved in dummy-frame spill-area)
  154. * +------------------------+
  155. *
  156. * We create a dummy frame to return to either ret_from_fork or
  157. * ret_from_kernel_thread:
  158. * a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4)
  159. * sp points to itself (thread.sp)
  160. * a2, a3 are unused for userspace threads,
  161. * a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
  162. *
  163. * Note: This is a pristine frame, so we don't need any spill region on top of
  164. * childregs.
  165. *
  166. * The fun part: if we're keeping the same VM (i.e. cloning a thread,
  167. * not an entire process), we're normally given a new usp, and we CANNOT share
  168. * any live address register windows. If we just copy those live frames over,
  169. * the two threads (parent and child) will overflow the same frames onto the
  170. * parent stack at different times, likely corrupting the parent stack (esp.
  171. * if the parent returns from functions that called clone() and calls new
  172. * ones, before the child overflows its now old copies of its parent windows).
  173. * One solution is to spill windows to the parent stack, but that's fairly
  174. * involved. Much simpler to just not copy those live frames across.
  175. */
  176. int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
  177. unsigned long thread_fn_arg, struct task_struct *p,
  178. unsigned long tls)
  179. {
  180. struct pt_regs *childregs = task_pt_regs(p);
  181. #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
  182. struct thread_info *ti;
  183. #endif
  184. /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
  185. SPILL_SLOT(childregs, 1) = (unsigned long)childregs;
  186. SPILL_SLOT(childregs, 0) = 0;
  187. p->thread.sp = (unsigned long)childregs;
  188. if (!(p->flags & PF_KTHREAD)) {
  189. struct pt_regs *regs = current_pt_regs();
  190. unsigned long usp = usp_thread_fn ?
  191. usp_thread_fn : regs->areg[1];
  192. p->thread.ra = MAKE_RA_FOR_CALL(
  193. (unsigned long)ret_from_fork, 0x1);
  194. /* This does not copy all the regs.
  195. * In a bout of brilliance or madness,
  196. * ARs beyond a0-a15 exist past the end of the struct.
  197. */
  198. *childregs = *regs;
  199. childregs->areg[1] = usp;
  200. childregs->areg[2] = 0;
  201. /* When sharing memory with the parent thread, the child
  202. usually starts on a pristine stack, so we have to reset
  203. windowbase, windowstart and wmask.
  204. (Note that such a new thread is required to always create
  205. an initial call4 frame)
  206. The exception is vfork, where the new thread continues to
  207. run on the parent's stack until it calls execve. This could
  208. be a call8 or call12, which requires a legal stack frame
  209. of the previous caller for the overflow handlers to work.
  210. (Note that it's always legal to overflow live registers).
  211. In this case, ensure to spill at least the stack pointer
  212. of that frame. */
  213. if (clone_flags & CLONE_VM) {
  214. /* check that caller window is live and same stack */
  215. int len = childregs->wmask & ~0xf;
  216. if (regs->areg[1] == usp && len != 0) {
  217. int callinc = (regs->areg[0] >> 30) & 3;
  218. int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
  219. put_user(regs->areg[caller_ars+1],
  220. (unsigned __user*)(usp - 12));
  221. }
  222. childregs->wmask = 1;
  223. childregs->windowstart = 1;
  224. childregs->windowbase = 0;
  225. } else {
  226. int len = childregs->wmask & ~0xf;
  227. memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
  228. &regs->areg[XCHAL_NUM_AREGS - len/4], len);
  229. }
  230. childregs->syscall = regs->syscall;
  231. if (clone_flags & CLONE_SETTLS)
  232. childregs->threadptr = tls;
  233. } else {
  234. p->thread.ra = MAKE_RA_FOR_CALL(
  235. (unsigned long)ret_from_kernel_thread, 1);
  236. /* pass parameters to ret_from_kernel_thread:
  237. * a2 = thread_fn, a3 = thread_fn arg
  238. */
  239. SPILL_SLOT(childregs, 3) = thread_fn_arg;
  240. SPILL_SLOT(childregs, 2) = usp_thread_fn;
  241. /* Childregs are only used when we're going to userspace
  242. * in which case start_thread will set them up.
  243. */
  244. }
  245. #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
  246. ti = task_thread_info(p);
  247. ti->cpenable = 0;
  248. #endif
  249. clear_ptrace_hw_breakpoint(p);
  250. return 0;
  251. }
  252. /*
  253. * These bracket the sleeping functions..
  254. */
  255. unsigned long get_wchan(struct task_struct *p)
  256. {
  257. unsigned long sp, pc;
  258. unsigned long stack_page = (unsigned long) task_stack_page(p);
  259. int count = 0;
  260. if (!p || p == current || p->state == TASK_RUNNING)
  261. return 0;
  262. sp = p->thread.sp;
  263. pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
  264. do {
  265. if (sp < stack_page + sizeof(struct task_struct) ||
  266. sp >= (stack_page + THREAD_SIZE) ||
  267. pc == 0)
  268. return 0;
  269. if (!in_sched_functions(pc))
  270. return pc;
  271. /* Stack layout: sp-4: ra, sp-3: sp' */
  272. pc = MAKE_PC_FROM_RA(SPILL_SLOT(sp, 0), sp);
  273. sp = SPILL_SLOT(sp, 1);
  274. } while (count++ < 16);
  275. return 0;
  276. }