process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. int i, n, wb_offset;
  167. elfregs->xchal_config_id0 = XCHAL_HW_CONFIGID0;
  168. elfregs->xchal_config_id1 = XCHAL_HW_CONFIGID1;
  169. __asm__ __volatile__ ("rsr %0, 176\n" : "=a" (i));
  170. elfregs->cpux = i;
  171. __asm__ __volatile__ ("rsr %0, 208\n" : "=a" (i));
  172. elfregs->cpuy = i;
  173. /* Note: PS.EXCM is not set while user task is running; its
  174. * being set in regs->ps is for exception handling convenience.
  175. */
  176. elfregs->pc = regs->pc;
  177. elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
  178. elfregs->exccause = regs->exccause;
  179. elfregs->excvaddr = regs->excvaddr;
  180. elfregs->windowbase = regs->windowbase;
  181. elfregs->windowstart = regs->windowstart;
  182. elfregs->lbeg = regs->lbeg;
  183. elfregs->lend = regs->lend;
  184. elfregs->lcount = regs->lcount;
  185. elfregs->sar = regs->sar;
  186. elfregs->syscall = regs->syscall;
  187. /* Copy register file.
  188. * The layout looks like this:
  189. *
  190. * | a0 ... a15 | Z ... Z | arX ... arY |
  191. * current window unused saved frames
  192. */
  193. memset (elfregs->ar, 0, sizeof(elfregs->ar));
  194. wb_offset = regs->windowbase * 4;
  195. n = (regs->wmask&1)? 4 : (regs->wmask&2)? 8 : (regs->wmask&4)? 12 : 16;
  196. for (i = 0; i < n; i++)
  197. elfregs->ar[(wb_offset + i) % XCHAL_NUM_AREGS] = regs->areg[i];
  198. n = (regs->wmask >> 4) * 4;
  199. for (i = XCHAL_NUM_AREGS - n; n > 0; i++, n--)
  200. elfregs->ar[(wb_offset + i) % XCHAL_NUM_AREGS] = regs->areg[i];
  201. }
  202. void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
  203. {
  204. do_copy_regs ((xtensa_gregset_t *)elfregs, regs, current);
  205. }
  206. /* The inverse of do_copy_regs(). No error or sanity checking. */
  207. void do_restore_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
  208. struct task_struct *tsk)
  209. {
  210. int i, n, wb_offset;
  211. /* Note: PS.EXCM is not set while user task is running; it
  212. * needs to be set in regs->ps is for exception handling convenience.
  213. */
  214. regs->pc = elfregs->pc;
  215. regs->ps = (elfregs->ps | (1 << PS_EXCM_BIT));
  216. regs->exccause = elfregs->exccause;
  217. regs->excvaddr = elfregs->excvaddr;
  218. regs->windowbase = elfregs->windowbase;
  219. regs->windowstart = elfregs->windowstart;
  220. regs->lbeg = elfregs->lbeg;
  221. regs->lend = elfregs->lend;
  222. regs->lcount = elfregs->lcount;
  223. regs->sar = elfregs->sar;
  224. regs->syscall = elfregs->syscall;
  225. /* Clear everything. */
  226. memset (regs->areg, 0, sizeof(regs->areg));
  227. /* Copy regs from live window frame. */
  228. wb_offset = regs->windowbase * 4;
  229. n = (regs->wmask&1)? 4 : (regs->wmask&2)? 8 : (regs->wmask&4)? 12 : 16;
  230. for (i = 0; i < n; i++)
  231. regs->areg[(wb_offset+i) % XCHAL_NUM_AREGS] = elfregs->ar[i];
  232. n = (regs->wmask >> 4) * 4;
  233. for (i = XCHAL_NUM_AREGS - n; n > 0; i++, n--)
  234. regs->areg[(wb_offset+i) % XCHAL_NUM_AREGS] = elfregs->ar[i];
  235. }
  236. /*
  237. * do_save_fpregs() gathers information from 'struct pt_regs' and
  238. * 'current->thread' to fill in the elf_fpregset_t structure.
  239. *
  240. * Core files and ptrace use elf_fpregset_t.
  241. */
  242. void do_save_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
  243. struct task_struct *tsk)
  244. {
  245. #if XCHAL_HAVE_CP
  246. extern unsigned char _xtensa_reginfo_tables[];
  247. extern unsigned _xtensa_reginfo_table_size;
  248. int i;
  249. unsigned long flags;
  250. /* Before dumping coprocessor state from memory,
  251. * ensure any live coprocessor contents for this
  252. * task are first saved to memory:
  253. */
  254. local_irq_save(flags);
  255. for (i = 0; i < XCHAL_CP_MAX; i++) {
  256. if (tsk == coprocessor_info[i].owner) {
  257. enable_coprocessor(i);
  258. save_coprocessor_registers(
  259. tsk->thread.cp_save+coprocessor_info[i].offset,i);
  260. disable_coprocessor(i);
  261. }
  262. }
  263. local_irq_restore(flags);
  264. /* Now dump coprocessor & extra state: */
  265. memcpy((unsigned char*)fpregs,
  266. _xtensa_reginfo_tables, _xtensa_reginfo_table_size);
  267. memcpy((unsigned char*)fpregs + _xtensa_reginfo_table_size,
  268. tsk->thread.cp_save, XTENSA_CP_EXTRA_SIZE);
  269. #endif
  270. }
  271. /*
  272. * The inverse of do_save_fpregs().
  273. * Copies coprocessor and extra state from fpregs into regs and tsk->thread.
  274. * Returns 0 on success, non-zero if layout doesn't match.
  275. */
  276. int do_restore_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
  277. struct task_struct *tsk)
  278. {
  279. #if XCHAL_HAVE_CP
  280. extern unsigned char _xtensa_reginfo_tables[];
  281. extern unsigned _xtensa_reginfo_table_size;
  282. int i;
  283. unsigned long flags;
  284. /* Make sure save area layouts match.
  285. * FIXME: in the future we could allow restoring from
  286. * a different layout of the same registers, by comparing
  287. * fpregs' table with _xtensa_reginfo_tables and matching
  288. * entries and copying registers one at a time.
  289. * Not too sure yet whether that's very useful.
  290. */
  291. if( memcmp((unsigned char*)fpregs,
  292. _xtensa_reginfo_tables, _xtensa_reginfo_table_size) ) {
  293. return -1;
  294. }
  295. /* Before restoring coprocessor state from memory,
  296. * ensure any live coprocessor contents for this
  297. * task are first invalidated.
  298. */
  299. local_irq_save(flags);
  300. for (i = 0; i < XCHAL_CP_MAX; i++) {
  301. if (tsk == coprocessor_info[i].owner) {
  302. enable_coprocessor(i);
  303. save_coprocessor_registers(
  304. tsk->thread.cp_save+coprocessor_info[i].offset,i);
  305. coprocessor_info[i].owner = 0;
  306. disable_coprocessor(i);
  307. }
  308. }
  309. local_irq_restore(flags);
  310. /* Now restore coprocessor & extra state: */
  311. memcpy(tsk->thread.cp_save,
  312. (unsigned char*)fpregs + _xtensa_reginfo_table_size,
  313. XTENSA_CP_EXTRA_SIZE);
  314. #endif
  315. return 0;
  316. }
  317. /*
  318. * Fill in the CP structure for a core dump for a particular task.
  319. */
  320. int
  321. dump_task_fpu(struct pt_regs *regs, struct task_struct *task, elf_fpregset_t *r)
  322. {
  323. return 0; /* no coprocessors active on this processor */
  324. }
  325. /*
  326. * Fill in the CP structure for a core dump.
  327. * This includes any FPU coprocessor.
  328. * Here, we dump all coprocessors, and other ("extra") custom state.
  329. *
  330. * This function is called by elf_core_dump() in fs/binfmt_elf.c
  331. * (in which case 'regs' comes from calls to do_coredump, see signals.c).
  332. */
  333. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
  334. {
  335. return dump_task_fpu(regs, current, r);
  336. }
  337. asmlinkage
  338. long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
  339. void __user *parent_tid, void *child_tls,
  340. void __user *child_tid, long a5,
  341. struct pt_regs *regs)
  342. {
  343. if (!newsp)
  344. newsp = regs->areg[1];
  345. return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
  346. }
  347. /*
  348. * * xtensa_execve() executes a new program.
  349. * */
  350. asmlinkage
  351. long xtensa_execve(char __user *name, char __user * __user *argv,
  352. char __user * __user *envp,
  353. long a3, long a4, long a5,
  354. struct pt_regs *regs)
  355. {
  356. long error;
  357. char * filename;
  358. filename = getname(name);
  359. error = PTR_ERR(filename);
  360. if (IS_ERR(filename))
  361. goto out;
  362. // FIXME: release coprocessor??
  363. error = do_execve(filename, argv, envp, regs);
  364. if (error == 0) {
  365. task_lock(current);
  366. current->ptrace &= ~PT_DTRACE;
  367. task_unlock(current);
  368. }
  369. putname(filename);
  370. out:
  371. return error;
  372. }