process.c 12 KB

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