process.c 7.7 KB

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