process.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <asm/pgtable.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/system.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 <asm/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. while (!need_resched())
  90. platform_idle();
  91. preempt_enable_no_resched();
  92. schedule();
  93. preempt_disable();
  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 is called before the thread is copied.
  119. */
  120. void prepare_to_copy(struct task_struct *tsk)
  121. {
  122. #if XTENSA_HAVE_COPROCESSORS
  123. coprocessor_flush_all(task_thread_info(tsk));
  124. #endif
  125. }
  126. /*
  127. * Copy thread.
  128. *
  129. * The stack layout for the new thread looks like this:
  130. *
  131. * +------------------------+ <- sp in childregs (= tos)
  132. * | childregs |
  133. * +------------------------+ <- thread.sp = sp in dummy-frame
  134. * | dummy-frame | (saved in dummy-frame spill-area)
  135. * +------------------------+
  136. *
  137. * We create a dummy frame to return to ret_from_fork:
  138. * a0 points to ret_from_fork (simulating a call4)
  139. * sp points to itself (thread.sp)
  140. * a2, a3 are unused.
  141. *
  142. * Note: This is a pristine frame, so we don't need any spill region on top of
  143. * childregs.
  144. */
  145. int copy_thread(unsigned long clone_flags, unsigned long usp,
  146. unsigned long unused,
  147. struct task_struct * p, struct pt_regs * regs)
  148. {
  149. struct pt_regs *childregs;
  150. struct thread_info *ti;
  151. unsigned long tos;
  152. int user_mode = user_mode(regs);
  153. /* Set up new TSS. */
  154. tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  155. if (user_mode)
  156. childregs = (struct pt_regs*)(tos - PT_USER_SIZE);
  157. else
  158. childregs = (struct pt_regs*)tos - 1;
  159. *childregs = *regs;
  160. /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
  161. *((int*)childregs - 3) = (unsigned long)childregs;
  162. *((int*)childregs - 4) = 0;
  163. childregs->areg[1] = tos;
  164. childregs->areg[2] = 0;
  165. p->set_child_tid = p->clear_child_tid = NULL;
  166. p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1);
  167. p->thread.sp = (unsigned long)childregs;
  168. if (user_mode(regs)) {
  169. int len = childregs->wmask & ~0xf;
  170. childregs->areg[1] = usp;
  171. memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
  172. &regs->areg[XCHAL_NUM_AREGS - len/4], len);
  173. // FIXME: we need to set THREADPTR in thread_info...
  174. if (clone_flags & CLONE_SETTLS)
  175. childregs->areg[2] = childregs->areg[6];
  176. } else {
  177. /* In kernel space, we start a new thread with a new stack. */
  178. childregs->wmask = 1;
  179. }
  180. #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
  181. ti = task_thread_info(p);
  182. ti->cpenable = 0;
  183. #endif
  184. return 0;
  185. }
  186. /*
  187. * These bracket the sleeping functions..
  188. */
  189. unsigned long get_wchan(struct task_struct *p)
  190. {
  191. unsigned long sp, pc;
  192. unsigned long stack_page = (unsigned long) task_stack_page(p);
  193. int count = 0;
  194. if (!p || p == current || p->state == TASK_RUNNING)
  195. return 0;
  196. sp = p->thread.sp;
  197. pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
  198. do {
  199. if (sp < stack_page + sizeof(struct task_struct) ||
  200. sp >= (stack_page + THREAD_SIZE) ||
  201. pc == 0)
  202. return 0;
  203. if (!in_sched_functions(pc))
  204. return pc;
  205. /* Stack layout: sp-4: ra, sp-3: sp' */
  206. pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
  207. sp = *(unsigned long *)sp - 3;
  208. } while (count++ < 16);
  209. return 0;
  210. }
  211. /*
  212. * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
  213. * of processor registers. Besides different ordering,
  214. * xtensa_gregset_t contains non-live register information that
  215. * 'struct pt_regs' does not. Exception handling (primarily) uses
  216. * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
  217. *
  218. */
  219. void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
  220. {
  221. unsigned long wb, ws, wm;
  222. int live, last;
  223. wb = regs->windowbase;
  224. ws = regs->windowstart;
  225. wm = regs->wmask;
  226. ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
  227. /* Don't leak any random bits. */
  228. memset(elfregs, 0, sizeof (elfregs));
  229. /* Note: PS.EXCM is not set while user task is running; its
  230. * being set in regs->ps is for exception handling convenience.
  231. */
  232. elfregs->pc = regs->pc;
  233. elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
  234. elfregs->lbeg = regs->lbeg;
  235. elfregs->lend = regs->lend;
  236. elfregs->lcount = regs->lcount;
  237. elfregs->sar = regs->sar;
  238. elfregs->windowstart = ws;
  239. live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
  240. last = XCHAL_NUM_AREGS - (wm >> 4) * 4;
  241. memcpy(elfregs->a, regs->areg, live * 4);
  242. memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16);
  243. }
  244. int dump_fpu(void)
  245. {
  246. return 0;
  247. }
  248. asmlinkage
  249. long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
  250. void __user *parent_tid, void *child_tls,
  251. void __user *child_tid, long a5,
  252. struct pt_regs *regs)
  253. {
  254. if (!newsp)
  255. newsp = regs->areg[1];
  256. return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
  257. }
  258. /*
  259. * xtensa_execve() executes a new program.
  260. */
  261. asmlinkage
  262. long xtensa_execve(const char __user *name,
  263. const char __user *const __user *argv,
  264. const char __user *const __user *envp,
  265. long a3, long a4, long a5,
  266. struct pt_regs *regs)
  267. {
  268. long error;
  269. char * filename;
  270. filename = getname(name);
  271. error = PTR_ERR(filename);
  272. if (IS_ERR(filename))
  273. goto out;
  274. error = do_execve(filename, argv, envp, regs);
  275. putname(filename);
  276. out:
  277. return error;
  278. }