process.c 9.8 KB

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