process.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. struct task_struct *current_set[NR_CPUS] = {&init_task, };
  46. void (*pm_power_off)(void) = NULL;
  47. EXPORT_SYMBOL(pm_power_off);
  48. #if XTENSA_HAVE_COPROCESSORS
  49. void coprocessor_release_all(struct thread_info *ti)
  50. {
  51. unsigned long cpenable;
  52. int i;
  53. /* Make sure we don't switch tasks during this operation. */
  54. preempt_disable();
  55. /* Walk through all cp owners and release it for the requested one. */
  56. cpenable = ti->cpenable;
  57. for (i = 0; i < XCHAL_CP_MAX; i++) {
  58. if (coprocessor_owner[i] == ti) {
  59. coprocessor_owner[i] = 0;
  60. cpenable &= ~(1 << i);
  61. }
  62. }
  63. ti->cpenable = cpenable;
  64. coprocessor_clear_cpenable();
  65. preempt_enable();
  66. }
  67. void coprocessor_flush_all(struct thread_info *ti)
  68. {
  69. unsigned long cpenable;
  70. int i;
  71. preempt_disable();
  72. cpenable = ti->cpenable;
  73. for (i = 0; i < XCHAL_CP_MAX; i++) {
  74. if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
  75. coprocessor_flush(ti, i);
  76. cpenable >>= 1;
  77. }
  78. preempt_enable();
  79. }
  80. #endif
  81. /*
  82. * Powermanagement idle function, if any is provided by the platform.
  83. */
  84. void cpu_idle(void)
  85. {
  86. local_irq_enable();
  87. /* endless idle loop with no priority at all */
  88. while (1) {
  89. rcu_idle_enter();
  90. while (!need_resched())
  91. platform_idle();
  92. rcu_idle_exit();
  93. schedule_preempt_disabled();
  94. }
  95. }
  96. /*
  97. * This is called when the thread calls exit().
  98. */
  99. void exit_thread(void)
  100. {
  101. #if XTENSA_HAVE_COPROCESSORS
  102. coprocessor_release_all(current_thread_info());
  103. #endif
  104. }
  105. /*
  106. * Flush thread state. This is called when a thread does an execve()
  107. * Note that we flush coprocessor registers for the case execve fails.
  108. */
  109. void flush_thread(void)
  110. {
  111. #if XTENSA_HAVE_COPROCESSORS
  112. struct thread_info *ti = current_thread_info();
  113. coprocessor_flush_all(ti);
  114. coprocessor_release_all(ti);
  115. #endif
  116. }
  117. /*
  118. * this gets called so that we can store coprocessor state into memory and
  119. * copy the current task into the new thread.
  120. */
  121. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  122. {
  123. #if XTENSA_HAVE_COPROCESSORS
  124. coprocessor_flush_all(task_thread_info(src));
  125. #endif
  126. *dst = *src;
  127. return 0;
  128. }
  129. /*
  130. * Copy thread.
  131. *
  132. * The stack layout for the new thread looks like this:
  133. *
  134. * +------------------------+ <- sp in childregs (= tos)
  135. * | childregs |
  136. * +------------------------+ <- thread.sp = sp in dummy-frame
  137. * | dummy-frame | (saved in dummy-frame spill-area)
  138. * +------------------------+
  139. *
  140. * We create a dummy frame to return to ret_from_fork:
  141. * a0 points to ret_from_fork (simulating a call4)
  142. * sp points to itself (thread.sp)
  143. * a2, a3 are unused.
  144. *
  145. * Note: This is a pristine frame, so we don't need any spill region on top of
  146. * childregs.
  147. */
  148. int copy_thread(unsigned long clone_flags, unsigned long usp,
  149. unsigned long unused,
  150. struct task_struct * p, struct pt_regs * regs)
  151. {
  152. struct pt_regs *childregs;
  153. struct thread_info *ti;
  154. unsigned long tos;
  155. int user_mode = user_mode(regs);
  156. /* Set up new TSS. */
  157. tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  158. if (user_mode)
  159. childregs = (struct pt_regs*)(tos - PT_USER_SIZE);
  160. else
  161. childregs = (struct pt_regs*)tos - 1;
  162. *childregs = *regs;
  163. /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
  164. *((int*)childregs - 3) = (unsigned long)childregs;
  165. *((int*)childregs - 4) = 0;
  166. childregs->areg[1] = tos;
  167. childregs->areg[2] = 0;
  168. p->set_child_tid = p->clear_child_tid = NULL;
  169. p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1);
  170. p->thread.sp = (unsigned long)childregs;
  171. if (user_mode(regs)) {
  172. int len = childregs->wmask & ~0xf;
  173. childregs->areg[1] = usp;
  174. memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
  175. &regs->areg[XCHAL_NUM_AREGS - len/4], len);
  176. // FIXME: we need to set THREADPTR in thread_info...
  177. if (clone_flags & CLONE_SETTLS)
  178. childregs->areg[2] = childregs->areg[6];
  179. } else {
  180. /* In kernel space, we start a new thread with a new stack. */
  181. childregs->wmask = 1;
  182. }
  183. #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
  184. ti = task_thread_info(p);
  185. ti->cpenable = 0;
  186. #endif
  187. return 0;
  188. }
  189. /*
  190. * These bracket the sleeping functions..
  191. */
  192. unsigned long get_wchan(struct task_struct *p)
  193. {
  194. unsigned long sp, pc;
  195. unsigned long stack_page = (unsigned long) task_stack_page(p);
  196. int count = 0;
  197. if (!p || p == current || p->state == TASK_RUNNING)
  198. return 0;
  199. sp = p->thread.sp;
  200. pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
  201. do {
  202. if (sp < stack_page + sizeof(struct task_struct) ||
  203. sp >= (stack_page + THREAD_SIZE) ||
  204. pc == 0)
  205. return 0;
  206. if (!in_sched_functions(pc))
  207. return pc;
  208. /* Stack layout: sp-4: ra, sp-3: sp' */
  209. pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
  210. sp = *(unsigned long *)sp - 3;
  211. } while (count++ < 16);
  212. return 0;
  213. }
  214. /*
  215. * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
  216. * of processor registers. Besides different ordering,
  217. * xtensa_gregset_t contains non-live register information that
  218. * 'struct pt_regs' does not. Exception handling (primarily) uses
  219. * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
  220. *
  221. */
  222. void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
  223. {
  224. unsigned long wb, ws, wm;
  225. int live, last;
  226. wb = regs->windowbase;
  227. ws = regs->windowstart;
  228. wm = regs->wmask;
  229. ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
  230. /* Don't leak any random bits. */
  231. memset(elfregs, 0, sizeof(*elfregs));
  232. /* Note: PS.EXCM is not set while user task is running; its
  233. * being set in regs->ps is for exception handling convenience.
  234. */
  235. elfregs->pc = regs->pc;
  236. elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
  237. elfregs->lbeg = regs->lbeg;
  238. elfregs->lend = regs->lend;
  239. elfregs->lcount = regs->lcount;
  240. elfregs->sar = regs->sar;
  241. elfregs->windowstart = ws;
  242. live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
  243. last = XCHAL_NUM_AREGS - (wm >> 4) * 4;
  244. memcpy(elfregs->a, regs->areg, live * 4);
  245. memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16);
  246. }
  247. int dump_fpu(void)
  248. {
  249. return 0;
  250. }
  251. asmlinkage
  252. long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
  253. void __user *parent_tid, void *child_tls,
  254. void __user *child_tid, long a5,
  255. struct pt_regs *regs)
  256. {
  257. if (!newsp)
  258. newsp = regs->areg[1];
  259. return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
  260. }
  261. /*
  262. * xtensa_execve() executes a new program.
  263. */
  264. asmlinkage
  265. long xtensa_execve(const char __user *name,
  266. const char __user *const __user *argv,
  267. const char __user *const __user *envp,
  268. long a3, long a4, long a5,
  269. struct pt_regs *regs)
  270. {
  271. long error;
  272. struct filename *filename;
  273. filename = getname(name);
  274. error = PTR_ERR(filename);
  275. if (IS_ERR(filename))
  276. goto out;
  277. error = do_execve(filename->name, argv, envp, regs);
  278. putname(filename);
  279. out:
  280. return error;
  281. }