process.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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/slab.h>
  26. #include <linux/elf.h>
  27. #include <linux/init.h>
  28. #include <linux/prctl.h>
  29. #include <linux/init_task.h>
  30. #include <linux/module.h>
  31. #include <linux/mqueue.h>
  32. #include <linux/fs.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. /*
  49. * Powermanagement idle function, if any is provided by the platform.
  50. */
  51. void cpu_idle(void)
  52. {
  53. local_irq_enable();
  54. /* endless idle loop with no priority at all */
  55. while (1) {
  56. while (!need_resched())
  57. platform_idle();
  58. preempt_enable_no_resched();
  59. schedule();
  60. preempt_disable();
  61. }
  62. }
  63. /*
  64. * Free current thread data structures etc..
  65. */
  66. void exit_thread(void)
  67. {
  68. }
  69. void flush_thread(void)
  70. {
  71. }
  72. /*
  73. * Copy thread.
  74. *
  75. * The stack layout for the new thread looks like this:
  76. *
  77. * +------------------------+ <- sp in childregs (= tos)
  78. * | childregs |
  79. * +------------------------+ <- thread.sp = sp in dummy-frame
  80. * | dummy-frame | (saved in dummy-frame spill-area)
  81. * +------------------------+
  82. *
  83. * We create a dummy frame to return to ret_from_fork:
  84. * a0 points to ret_from_fork (simulating a call4)
  85. * sp points to itself (thread.sp)
  86. * a2, a3 are unused.
  87. *
  88. * Note: This is a pristine frame, so we don't need any spill region on top of
  89. * childregs.
  90. */
  91. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  92. unsigned long unused,
  93. struct task_struct * p, struct pt_regs * regs)
  94. {
  95. struct pt_regs *childregs;
  96. unsigned long tos;
  97. int user_mode = user_mode(regs);
  98. /* Set up new TSS. */
  99. tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  100. if (user_mode)
  101. childregs = (struct pt_regs*)(tos - PT_USER_SIZE);
  102. else
  103. childregs = (struct pt_regs*)tos - 1;
  104. *childregs = *regs;
  105. /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
  106. *((int*)childregs - 3) = (unsigned long)childregs;
  107. *((int*)childregs - 4) = 0;
  108. childregs->areg[1] = tos;
  109. childregs->areg[2] = 0;
  110. p->set_child_tid = p->clear_child_tid = NULL;
  111. p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1);
  112. p->thread.sp = (unsigned long)childregs;
  113. if (user_mode(regs)) {
  114. int len = childregs->wmask & ~0xf;
  115. childregs->areg[1] = usp;
  116. memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
  117. &regs->areg[XCHAL_NUM_AREGS - len/4], len);
  118. if (clone_flags & CLONE_SETTLS)
  119. childregs->areg[2] = childregs->areg[6];
  120. } else {
  121. /* In kernel space, we start a new thread with a new stack. */
  122. childregs->wmask = 1;
  123. }
  124. return 0;
  125. }
  126. /*
  127. * These bracket the sleeping functions..
  128. */
  129. unsigned long get_wchan(struct task_struct *p)
  130. {
  131. unsigned long sp, pc;
  132. unsigned long stack_page = (unsigned long) task_stack_page(p);
  133. int count = 0;
  134. if (!p || p == current || p->state == TASK_RUNNING)
  135. return 0;
  136. sp = p->thread.sp;
  137. pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
  138. do {
  139. if (sp < stack_page + sizeof(struct task_struct) ||
  140. sp >= (stack_page + THREAD_SIZE) ||
  141. pc == 0)
  142. return 0;
  143. if (!in_sched_functions(pc))
  144. return pc;
  145. /* Stack layout: sp-4: ra, sp-3: sp' */
  146. pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
  147. sp = *(unsigned long *)sp - 3;
  148. } while (count++ < 16);
  149. return 0;
  150. }
  151. /*
  152. * do_copy_regs() gathers information from 'struct pt_regs' and
  153. * 'current->thread.areg[]' to fill in the xtensa_gregset_t
  154. * structure.
  155. *
  156. * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
  157. * of processor registers. Besides different ordering,
  158. * xtensa_gregset_t contains non-live register information that
  159. * 'struct pt_regs' does not. Exception handling (primarily) uses
  160. * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
  161. *
  162. */
  163. void do_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
  164. struct task_struct *tsk)
  165. {
  166. /* Note: PS.EXCM is not set while user task is running; its
  167. * being set in regs->ps is for exception handling convenience.
  168. */
  169. elfregs->pc = regs->pc;
  170. elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
  171. elfregs->lbeg = regs->lbeg;
  172. elfregs->lend = regs->lend;
  173. elfregs->lcount = regs->lcount;
  174. elfregs->sar = regs->sar;
  175. memcpy (elfregs->a, regs->areg, sizeof(elfregs->a));
  176. }
  177. void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
  178. {
  179. do_copy_regs ((xtensa_gregset_t *)elfregs, regs, current);
  180. }
  181. /* The inverse of do_copy_regs(). No error or sanity checking. */
  182. void do_restore_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
  183. struct task_struct *tsk)
  184. {
  185. const unsigned long ps_mask = PS_CALLINC_MASK | PS_OWB_MASK;
  186. unsigned long ps;
  187. /* Note: PS.EXCM is not set while user task is running; it
  188. * needs to be set in regs->ps is for exception handling convenience.
  189. */
  190. ps = (regs->ps & ~ps_mask) | (elfregs->ps & ps_mask) | (1<<PS_EXCM_BIT);
  191. regs->ps = ps;
  192. regs->pc = elfregs->pc;
  193. regs->lbeg = elfregs->lbeg;
  194. regs->lend = elfregs->lend;
  195. regs->lcount = elfregs->lcount;
  196. regs->sar = elfregs->sar;
  197. memcpy (regs->areg, elfregs->a, sizeof(regs->areg));
  198. }
  199. /*
  200. * do_save_fpregs() gathers information from 'struct pt_regs' and
  201. * 'current->thread' to fill in the elf_fpregset_t structure.
  202. *
  203. * Core files and ptrace use elf_fpregset_t.
  204. */
  205. void do_save_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
  206. struct task_struct *tsk)
  207. {
  208. #if XCHAL_HAVE_CP
  209. extern unsigned char _xtensa_reginfo_tables[];
  210. extern unsigned _xtensa_reginfo_table_size;
  211. int i;
  212. unsigned long flags;
  213. /* Before dumping coprocessor state from memory,
  214. * ensure any live coprocessor contents for this
  215. * task are first saved to memory:
  216. */
  217. local_irq_save(flags);
  218. for (i = 0; i < XCHAL_CP_MAX; i++) {
  219. if (tsk == coprocessor_info[i].owner) {
  220. enable_coprocessor(i);
  221. save_coprocessor_registers(
  222. tsk->thread.cp_save+coprocessor_info[i].offset,i);
  223. disable_coprocessor(i);
  224. }
  225. }
  226. local_irq_restore(flags);
  227. /* Now dump coprocessor & extra state: */
  228. memcpy((unsigned char*)fpregs,
  229. _xtensa_reginfo_tables, _xtensa_reginfo_table_size);
  230. memcpy((unsigned char*)fpregs + _xtensa_reginfo_table_size,
  231. tsk->thread.cp_save, XTENSA_CP_EXTRA_SIZE);
  232. #endif
  233. }
  234. /*
  235. * The inverse of do_save_fpregs().
  236. * Copies coprocessor and extra state from fpregs into regs and tsk->thread.
  237. * Returns 0 on success, non-zero if layout doesn't match.
  238. */
  239. int do_restore_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
  240. struct task_struct *tsk)
  241. {
  242. #if XCHAL_HAVE_CP
  243. extern unsigned char _xtensa_reginfo_tables[];
  244. extern unsigned _xtensa_reginfo_table_size;
  245. int i;
  246. unsigned long flags;
  247. /* Make sure save area layouts match.
  248. * FIXME: in the future we could allow restoring from
  249. * a different layout of the same registers, by comparing
  250. * fpregs' table with _xtensa_reginfo_tables and matching
  251. * entries and copying registers one at a time.
  252. * Not too sure yet whether that's very useful.
  253. */
  254. if( memcmp((unsigned char*)fpregs,
  255. _xtensa_reginfo_tables, _xtensa_reginfo_table_size) ) {
  256. return -1;
  257. }
  258. /* Before restoring coprocessor state from memory,
  259. * ensure any live coprocessor contents for this
  260. * task are first invalidated.
  261. */
  262. local_irq_save(flags);
  263. for (i = 0; i < XCHAL_CP_MAX; i++) {
  264. if (tsk == coprocessor_info[i].owner) {
  265. enable_coprocessor(i);
  266. save_coprocessor_registers(
  267. tsk->thread.cp_save+coprocessor_info[i].offset,i);
  268. coprocessor_info[i].owner = 0;
  269. disable_coprocessor(i);
  270. }
  271. }
  272. local_irq_restore(flags);
  273. /* Now restore coprocessor & extra state: */
  274. memcpy(tsk->thread.cp_save,
  275. (unsigned char*)fpregs + _xtensa_reginfo_table_size,
  276. XTENSA_CP_EXTRA_SIZE);
  277. #endif
  278. return 0;
  279. }
  280. /*
  281. * Fill in the CP structure for a core dump for a particular task.
  282. */
  283. int
  284. dump_task_fpu(struct pt_regs *regs, struct task_struct *task, elf_fpregset_t *r)
  285. {
  286. return 0; /* no coprocessors active on this processor */
  287. }
  288. /*
  289. * Fill in the CP structure for a core dump.
  290. * This includes any FPU coprocessor.
  291. * Here, we dump all coprocessors, and other ("extra") custom state.
  292. *
  293. * This function is called by elf_core_dump() in fs/binfmt_elf.c
  294. * (in which case 'regs' comes from calls to do_coredump, see signals.c).
  295. */
  296. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
  297. {
  298. return dump_task_fpu(regs, current, r);
  299. }
  300. asmlinkage
  301. long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
  302. void __user *parent_tid, void *child_tls,
  303. void __user *child_tid, long a5,
  304. struct pt_regs *regs)
  305. {
  306. if (!newsp)
  307. newsp = regs->areg[1];
  308. return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
  309. }
  310. /*
  311. * * xtensa_execve() executes a new program.
  312. * */
  313. asmlinkage
  314. long xtensa_execve(char __user *name, char __user * __user *argv,
  315. char __user * __user *envp,
  316. long a3, long a4, long a5,
  317. struct pt_regs *regs)
  318. {
  319. long error;
  320. char * filename;
  321. filename = getname(name);
  322. error = PTR_ERR(filename);
  323. if (IS_ERR(filename))
  324. goto out;
  325. // FIXME: release coprocessor??
  326. error = do_execve(filename, argv, envp, regs);
  327. if (error == 0) {
  328. task_lock(current);
  329. current->ptrace &= ~PT_DTRACE;
  330. task_unlock(current);
  331. }
  332. putname(filename);
  333. out:
  334. return error;
  335. }