process.c 9.5 KB

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