process.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * arch/ppc/kernel/process.c
  3. *
  4. * Derived from "arch/i386/kernel/process.c"
  5. * Copyright (C) 1995 Linus Torvalds
  6. *
  7. * Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
  8. * Paul Mackerras (paulus@cs.anu.edu.au)
  9. *
  10. * PowerPC version
  11. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. *
  18. */
  19. #include <linux/config.h>
  20. #include <linux/errno.h>
  21. #include <linux/sched.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mm.h>
  24. #include <linux/smp.h>
  25. #include <linux/smp_lock.h>
  26. #include <linux/stddef.h>
  27. #include <linux/unistd.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/slab.h>
  30. #include <linux/user.h>
  31. #include <linux/elf.h>
  32. #include <linux/init.h>
  33. #include <linux/prctl.h>
  34. #include <linux/init_task.h>
  35. #include <linux/module.h>
  36. #include <linux/kallsyms.h>
  37. #include <linux/mqueue.h>
  38. #include <linux/hardirq.h>
  39. #include <asm/pgtable.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/system.h>
  42. #include <asm/io.h>
  43. #include <asm/processor.h>
  44. #include <asm/mmu.h>
  45. #include <asm/prom.h>
  46. extern unsigned long _get_SP(void);
  47. struct task_struct *last_task_used_math = NULL;
  48. struct task_struct *last_task_used_altivec = NULL;
  49. struct task_struct *last_task_used_spe = NULL;
  50. static struct fs_struct init_fs = INIT_FS;
  51. static struct files_struct init_files = INIT_FILES;
  52. static struct signal_struct init_signals = INIT_SIGNALS(init_signals);
  53. static struct sighand_struct init_sighand = INIT_SIGHAND(init_sighand);
  54. struct mm_struct init_mm = INIT_MM(init_mm);
  55. EXPORT_SYMBOL(init_mm);
  56. /* this is 8kB-aligned so we can get to the thread_info struct
  57. at the base of it from the stack pointer with 1 integer instruction. */
  58. union thread_union init_thread_union
  59. __attribute__((__section__(".data.init_task"))) =
  60. { INIT_THREAD_INFO(init_task) };
  61. /* initial task structure */
  62. struct task_struct init_task = INIT_TASK(init_task);
  63. EXPORT_SYMBOL(init_task);
  64. /* only used to get secondary processor up */
  65. struct task_struct *current_set[NR_CPUS] = {&init_task, };
  66. #undef SHOW_TASK_SWITCHES
  67. #undef CHECK_STACK
  68. #if defined(CHECK_STACK)
  69. unsigned long
  70. kernel_stack_top(struct task_struct *tsk)
  71. {
  72. return ((unsigned long)tsk) + sizeof(union task_union);
  73. }
  74. unsigned long
  75. task_top(struct task_struct *tsk)
  76. {
  77. return ((unsigned long)tsk) + sizeof(struct thread_info);
  78. }
  79. /* check to make sure the kernel stack is healthy */
  80. int check_stack(struct task_struct *tsk)
  81. {
  82. unsigned long stack_top = kernel_stack_top(tsk);
  83. unsigned long tsk_top = task_top(tsk);
  84. int ret = 0;
  85. #if 0
  86. /* check thread magic */
  87. if ( tsk->thread.magic != THREAD_MAGIC )
  88. {
  89. ret |= 1;
  90. printk("thread.magic bad: %08x\n", tsk->thread.magic);
  91. }
  92. #endif
  93. if ( !tsk )
  94. printk("check_stack(): tsk bad tsk %p\n",tsk);
  95. /* check if stored ksp is bad */
  96. if ( (tsk->thread.ksp > stack_top) || (tsk->thread.ksp < tsk_top) )
  97. {
  98. printk("stack out of bounds: %s/%d\n"
  99. " tsk_top %08lx ksp %08lx stack_top %08lx\n",
  100. tsk->comm,tsk->pid,
  101. tsk_top, tsk->thread.ksp, stack_top);
  102. ret |= 2;
  103. }
  104. /* check if stack ptr RIGHT NOW is bad */
  105. if ( (tsk == current) && ((_get_SP() > stack_top ) || (_get_SP() < tsk_top)) )
  106. {
  107. printk("current stack ptr out of bounds: %s/%d\n"
  108. " tsk_top %08lx sp %08lx stack_top %08lx\n",
  109. current->comm,current->pid,
  110. tsk_top, _get_SP(), stack_top);
  111. ret |= 4;
  112. }
  113. #if 0
  114. /* check amount of free stack */
  115. for ( i = (unsigned long *)task_top(tsk) ; i < kernel_stack_top(tsk) ; i++ )
  116. {
  117. if ( !i )
  118. printk("check_stack(): i = %p\n", i);
  119. if ( *i != 0 )
  120. {
  121. /* only notify if it's less than 900 bytes */
  122. if ( (i - (unsigned long *)task_top(tsk)) < 900 )
  123. printk("%d bytes free on stack\n",
  124. i - task_top(tsk));
  125. break;
  126. }
  127. }
  128. #endif
  129. if (ret)
  130. {
  131. panic("bad kernel stack");
  132. }
  133. return(ret);
  134. }
  135. #endif /* defined(CHECK_STACK) */
  136. #ifdef CONFIG_ALTIVEC
  137. int
  138. dump_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
  139. {
  140. if (regs->msr & MSR_VEC)
  141. giveup_altivec(current);
  142. memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs));
  143. return 1;
  144. }
  145. void
  146. enable_kernel_altivec(void)
  147. {
  148. WARN_ON(preemptible());
  149. #ifdef CONFIG_SMP
  150. if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
  151. giveup_altivec(current);
  152. else
  153. giveup_altivec(NULL); /* just enable AltiVec for kernel - force */
  154. #else
  155. giveup_altivec(last_task_used_altivec);
  156. #endif /* __SMP __ */
  157. }
  158. EXPORT_SYMBOL(enable_kernel_altivec);
  159. #endif /* CONFIG_ALTIVEC */
  160. #ifdef CONFIG_SPE
  161. int
  162. dump_spe(struct pt_regs *regs, elf_vrregset_t *evrregs)
  163. {
  164. if (regs->msr & MSR_SPE)
  165. giveup_spe(current);
  166. /* We copy u32 evr[32] + u64 acc + u32 spefscr -> 35 */
  167. memcpy(evrregs, &current->thread.evr[0], sizeof(u32) * 35);
  168. return 1;
  169. }
  170. void
  171. enable_kernel_spe(void)
  172. {
  173. WARN_ON(preemptible());
  174. #ifdef CONFIG_SMP
  175. if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
  176. giveup_spe(current);
  177. else
  178. giveup_spe(NULL); /* just enable SPE for kernel - force */
  179. #else
  180. giveup_spe(last_task_used_spe);
  181. #endif /* __SMP __ */
  182. }
  183. EXPORT_SYMBOL(enable_kernel_spe);
  184. #endif /* CONFIG_SPE */
  185. void
  186. enable_kernel_fp(void)
  187. {
  188. WARN_ON(preemptible());
  189. #ifdef CONFIG_SMP
  190. if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
  191. giveup_fpu(current);
  192. else
  193. giveup_fpu(NULL); /* just enables FP for kernel */
  194. #else
  195. giveup_fpu(last_task_used_math);
  196. #endif /* CONFIG_SMP */
  197. }
  198. EXPORT_SYMBOL(enable_kernel_fp);
  199. int
  200. dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
  201. {
  202. preempt_disable();
  203. if (tsk->thread.regs && (tsk->thread.regs->msr & MSR_FP))
  204. giveup_fpu(tsk);
  205. preempt_enable();
  206. memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
  207. return 1;
  208. }
  209. struct task_struct *__switch_to(struct task_struct *prev,
  210. struct task_struct *new)
  211. {
  212. struct thread_struct *new_thread, *old_thread;
  213. unsigned long s;
  214. struct task_struct *last;
  215. local_irq_save(s);
  216. #ifdef CHECK_STACK
  217. check_stack(prev);
  218. check_stack(new);
  219. #endif
  220. #ifdef CONFIG_SMP
  221. /* avoid complexity of lazy save/restore of fpu
  222. * by just saving it every time we switch out if
  223. * this task used the fpu during the last quantum.
  224. *
  225. * If it tries to use the fpu again, it'll trap and
  226. * reload its fp regs. So we don't have to do a restore
  227. * every switch, just a save.
  228. * -- Cort
  229. */
  230. if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
  231. giveup_fpu(prev);
  232. #ifdef CONFIG_ALTIVEC
  233. /*
  234. * If the previous thread used altivec in the last quantum
  235. * (thus changing altivec regs) then save them.
  236. * We used to check the VRSAVE register but not all apps
  237. * set it, so we don't rely on it now (and in fact we need
  238. * to save & restore VSCR even if VRSAVE == 0). -- paulus
  239. *
  240. * On SMP we always save/restore altivec regs just to avoid the
  241. * complexity of changing processors.
  242. * -- Cort
  243. */
  244. if ((prev->thread.regs && (prev->thread.regs->msr & MSR_VEC)))
  245. giveup_altivec(prev);
  246. #endif /* CONFIG_ALTIVEC */
  247. #ifdef CONFIG_SPE
  248. /*
  249. * If the previous thread used spe in the last quantum
  250. * (thus changing spe regs) then save them.
  251. *
  252. * On SMP we always save/restore spe regs just to avoid the
  253. * complexity of changing processors.
  254. */
  255. if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
  256. giveup_spe(prev);
  257. #endif /* CONFIG_SPE */
  258. #endif /* CONFIG_SMP */
  259. /* Avoid the trap. On smp this this never happens since
  260. * we don't set last_task_used_altivec -- Cort
  261. */
  262. if (new->thread.regs && last_task_used_altivec == new)
  263. new->thread.regs->msr |= MSR_VEC;
  264. #ifdef CONFIG_SPE
  265. /* Avoid the trap. On smp this this never happens since
  266. * we don't set last_task_used_spe
  267. */
  268. if (new->thread.regs && last_task_used_spe == new)
  269. new->thread.regs->msr |= MSR_SPE;
  270. #endif /* CONFIG_SPE */
  271. new_thread = &new->thread;
  272. old_thread = &current->thread;
  273. last = _switch(old_thread, new_thread);
  274. local_irq_restore(s);
  275. return last;
  276. }
  277. void show_regs(struct pt_regs * regs)
  278. {
  279. int i, trap;
  280. printk("NIP: %08lX LR: %08lX SP: %08lX REGS: %p TRAP: %04lx %s\n",
  281. regs->nip, regs->link, regs->gpr[1], regs, regs->trap,
  282. print_tainted());
  283. printk("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
  284. regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
  285. regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
  286. regs->msr&MSR_IR ? 1 : 0,
  287. regs->msr&MSR_DR ? 1 : 0);
  288. trap = TRAP(regs);
  289. if (trap == 0x300 || trap == 0x600)
  290. printk("DAR: %08lX, DSISR: %08lX\n", regs->dar, regs->dsisr);
  291. printk("TASK = %p[%d] '%s' THREAD: %p\n",
  292. current, current->pid, current->comm, current->thread_info);
  293. printk("Last syscall: %ld ", current->thread.last_syscall);
  294. #ifdef CONFIG_SMP
  295. printk(" CPU: %d", smp_processor_id());
  296. #endif /* CONFIG_SMP */
  297. for (i = 0; i < 32; i++) {
  298. long r;
  299. if ((i % 8) == 0)
  300. printk("\n" KERN_INFO "GPR%02d: ", i);
  301. if (__get_user(r, &regs->gpr[i]))
  302. break;
  303. printk("%08lX ", r);
  304. if (i == 12 && !FULL_REGS(regs))
  305. break;
  306. }
  307. printk("\n");
  308. #ifdef CONFIG_KALLSYMS
  309. /*
  310. * Lookup NIP late so we have the best change of getting the
  311. * above info out without failing
  312. */
  313. printk("NIP [%08lx] ", regs->nip);
  314. print_symbol("%s\n", regs->nip);
  315. printk("LR [%08lx] ", regs->link);
  316. print_symbol("%s\n", regs->link);
  317. #endif
  318. show_stack(current, (unsigned long *) regs->gpr[1]);
  319. }
  320. void exit_thread(void)
  321. {
  322. if (last_task_used_math == current)
  323. last_task_used_math = NULL;
  324. if (last_task_used_altivec == current)
  325. last_task_used_altivec = NULL;
  326. #ifdef CONFIG_SPE
  327. if (last_task_used_spe == current)
  328. last_task_used_spe = NULL;
  329. #endif
  330. }
  331. void flush_thread(void)
  332. {
  333. if (last_task_used_math == current)
  334. last_task_used_math = NULL;
  335. if (last_task_used_altivec == current)
  336. last_task_used_altivec = NULL;
  337. #ifdef CONFIG_SPE
  338. if (last_task_used_spe == current)
  339. last_task_used_spe = NULL;
  340. #endif
  341. }
  342. void
  343. release_thread(struct task_struct *t)
  344. {
  345. }
  346. /*
  347. * This gets called before we allocate a new thread and copy
  348. * the current task into it.
  349. */
  350. void prepare_to_copy(struct task_struct *tsk)
  351. {
  352. struct pt_regs *regs = tsk->thread.regs;
  353. if (regs == NULL)
  354. return;
  355. preempt_disable();
  356. if (regs->msr & MSR_FP)
  357. giveup_fpu(current);
  358. #ifdef CONFIG_ALTIVEC
  359. if (regs->msr & MSR_VEC)
  360. giveup_altivec(current);
  361. #endif /* CONFIG_ALTIVEC */
  362. #ifdef CONFIG_SPE
  363. if (regs->msr & MSR_SPE)
  364. giveup_spe(current);
  365. #endif /* CONFIG_SPE */
  366. preempt_enable();
  367. }
  368. /*
  369. * Copy a thread..
  370. */
  371. int
  372. copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  373. unsigned long unused,
  374. struct task_struct *p, struct pt_regs *regs)
  375. {
  376. struct pt_regs *childregs, *kregs;
  377. extern void ret_from_fork(void);
  378. unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
  379. unsigned long childframe;
  380. CHECK_FULL_REGS(regs);
  381. /* Copy registers */
  382. sp -= sizeof(struct pt_regs);
  383. childregs = (struct pt_regs *) sp;
  384. *childregs = *regs;
  385. if ((childregs->msr & MSR_PR) == 0) {
  386. /* for kernel thread, set `current' and stackptr in new task */
  387. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  388. childregs->gpr[2] = (unsigned long) p;
  389. p->thread.regs = NULL; /* no user register state */
  390. } else {
  391. childregs->gpr[1] = usp;
  392. p->thread.regs = childregs;
  393. if (clone_flags & CLONE_SETTLS)
  394. childregs->gpr[2] = childregs->gpr[6];
  395. }
  396. childregs->gpr[3] = 0; /* Result from fork() */
  397. sp -= STACK_FRAME_OVERHEAD;
  398. childframe = sp;
  399. /*
  400. * The way this works is that at some point in the future
  401. * some task will call _switch to switch to the new task.
  402. * That will pop off the stack frame created below and start
  403. * the new task running at ret_from_fork. The new task will
  404. * do some house keeping and then return from the fork or clone
  405. * system call, using the stack frame created above.
  406. */
  407. sp -= sizeof(struct pt_regs);
  408. kregs = (struct pt_regs *) sp;
  409. sp -= STACK_FRAME_OVERHEAD;
  410. p->thread.ksp = sp;
  411. kregs->nip = (unsigned long)ret_from_fork;
  412. p->thread.last_syscall = -1;
  413. return 0;
  414. }
  415. /*
  416. * Set up a thread for executing a new program
  417. */
  418. void start_thread(struct pt_regs *regs, unsigned long nip, unsigned long sp)
  419. {
  420. set_fs(USER_DS);
  421. memset(regs->gpr, 0, sizeof(regs->gpr));
  422. regs->ctr = 0;
  423. regs->link = 0;
  424. regs->xer = 0;
  425. regs->ccr = 0;
  426. regs->mq = 0;
  427. regs->nip = nip;
  428. regs->gpr[1] = sp;
  429. regs->msr = MSR_USER;
  430. if (last_task_used_math == current)
  431. last_task_used_math = NULL;
  432. if (last_task_used_altivec == current)
  433. last_task_used_altivec = NULL;
  434. #ifdef CONFIG_SPE
  435. if (last_task_used_spe == current)
  436. last_task_used_spe = NULL;
  437. #endif
  438. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  439. current->thread.fpscr = 0;
  440. #ifdef CONFIG_ALTIVEC
  441. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  442. memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
  443. current->thread.vrsave = 0;
  444. current->thread.used_vr = 0;
  445. #endif /* CONFIG_ALTIVEC */
  446. #ifdef CONFIG_SPE
  447. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  448. current->thread.acc = 0;
  449. current->thread.spefscr = 0;
  450. current->thread.used_spe = 0;
  451. #endif /* CONFIG_SPE */
  452. }
  453. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  454. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  455. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  456. {
  457. struct pt_regs *regs = tsk->thread.regs;
  458. /* This is a bit hairy. If we are an SPE enabled processor
  459. * (have embedded fp) we store the IEEE exception enable flags in
  460. * fpexc_mode. fpexc_mode is also used for setting FP exception
  461. * mode (asyn, precise, disabled) for 'Classic' FP. */
  462. if (val & PR_FP_EXC_SW_ENABLE) {
  463. #ifdef CONFIG_SPE
  464. tsk->thread.fpexc_mode = val &
  465. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  466. #else
  467. return -EINVAL;
  468. #endif
  469. } else {
  470. /* on a CONFIG_SPE this does not hurt us. The bits that
  471. * __pack_fe01 use do not overlap with bits used for
  472. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  473. * on CONFIG_SPE implementations are reserved so writing to
  474. * them does not change anything */
  475. if (val > PR_FP_EXC_PRECISE)
  476. return -EINVAL;
  477. tsk->thread.fpexc_mode = __pack_fe01(val);
  478. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  479. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  480. | tsk->thread.fpexc_mode;
  481. }
  482. return 0;
  483. }
  484. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  485. {
  486. unsigned int val;
  487. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  488. #ifdef CONFIG_SPE
  489. val = tsk->thread.fpexc_mode;
  490. #else
  491. return -EINVAL;
  492. #endif
  493. else
  494. val = __unpack_fe01(tsk->thread.fpexc_mode);
  495. return put_user(val, (unsigned int __user *) adr);
  496. }
  497. int sys_clone(unsigned long clone_flags, unsigned long usp,
  498. int __user *parent_tidp, void __user *child_threadptr,
  499. int __user *child_tidp, int p6,
  500. struct pt_regs *regs)
  501. {
  502. CHECK_FULL_REGS(regs);
  503. if (usp == 0)
  504. usp = regs->gpr[1]; /* stack pointer for child */
  505. return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
  506. }
  507. int sys_fork(int p1, int p2, int p3, int p4, int p5, int p6,
  508. struct pt_regs *regs)
  509. {
  510. CHECK_FULL_REGS(regs);
  511. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  512. }
  513. int sys_vfork(int p1, int p2, int p3, int p4, int p5, int p6,
  514. struct pt_regs *regs)
  515. {
  516. CHECK_FULL_REGS(regs);
  517. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
  518. regs, 0, NULL, NULL);
  519. }
  520. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  521. unsigned long a3, unsigned long a4, unsigned long a5,
  522. struct pt_regs *regs)
  523. {
  524. int error;
  525. char * filename;
  526. filename = getname((char __user *) a0);
  527. error = PTR_ERR(filename);
  528. if (IS_ERR(filename))
  529. goto out;
  530. preempt_disable();
  531. if (regs->msr & MSR_FP)
  532. giveup_fpu(current);
  533. #ifdef CONFIG_ALTIVEC
  534. if (regs->msr & MSR_VEC)
  535. giveup_altivec(current);
  536. #endif /* CONFIG_ALTIVEC */
  537. #ifdef CONFIG_SPE
  538. if (regs->msr & MSR_SPE)
  539. giveup_spe(current);
  540. #endif /* CONFIG_SPE */
  541. preempt_enable();
  542. error = do_execve(filename, (char __user *__user *) a1,
  543. (char __user *__user *) a2, regs);
  544. if (error == 0) {
  545. task_lock(current);
  546. current->ptrace &= ~PT_DTRACE;
  547. task_unlock(current);
  548. }
  549. putname(filename);
  550. out:
  551. return error;
  552. }
  553. void dump_stack(void)
  554. {
  555. show_stack(current, NULL);
  556. }
  557. EXPORT_SYMBOL(dump_stack);
  558. void show_stack(struct task_struct *tsk, unsigned long *stack)
  559. {
  560. unsigned long sp, stack_top, prev_sp, ret;
  561. int count = 0;
  562. unsigned long next_exc = 0;
  563. struct pt_regs *regs;
  564. extern char ret_from_except, ret_from_except_full, ret_from_syscall;
  565. sp = (unsigned long) stack;
  566. if (tsk == NULL)
  567. tsk = current;
  568. if (sp == 0) {
  569. if (tsk == current)
  570. asm("mr %0,1" : "=r" (sp));
  571. else
  572. sp = tsk->thread.ksp;
  573. }
  574. prev_sp = (unsigned long) (tsk->thread_info + 1);
  575. stack_top = (unsigned long) tsk->thread_info + THREAD_SIZE;
  576. while (count < 16 && sp > prev_sp && sp < stack_top && (sp & 3) == 0) {
  577. if (count == 0) {
  578. printk("Call trace:");
  579. #ifdef CONFIG_KALLSYMS
  580. printk("\n");
  581. #endif
  582. } else {
  583. if (next_exc) {
  584. ret = next_exc;
  585. next_exc = 0;
  586. } else
  587. ret = *(unsigned long *)(sp + 4);
  588. printk(" [%08lx] ", ret);
  589. #ifdef CONFIG_KALLSYMS
  590. print_symbol("%s", ret);
  591. printk("\n");
  592. #endif
  593. if (ret == (unsigned long) &ret_from_except
  594. || ret == (unsigned long) &ret_from_except_full
  595. || ret == (unsigned long) &ret_from_syscall) {
  596. /* sp + 16 points to an exception frame */
  597. regs = (struct pt_regs *) (sp + 16);
  598. if (sp + 16 + sizeof(*regs) <= stack_top)
  599. next_exc = regs->nip;
  600. }
  601. }
  602. ++count;
  603. sp = *(unsigned long *)sp;
  604. }
  605. #ifndef CONFIG_KALLSYMS
  606. if (count > 0)
  607. printk("\n");
  608. #endif
  609. }
  610. #if 0
  611. /*
  612. * Low level print for debugging - Cort
  613. */
  614. int __init ll_printk(const char *fmt, ...)
  615. {
  616. va_list args;
  617. char buf[256];
  618. int i;
  619. va_start(args, fmt);
  620. i=vsprintf(buf,fmt,args);
  621. ll_puts(buf);
  622. va_end(args);
  623. return i;
  624. }
  625. int lines = 24, cols = 80;
  626. int orig_x = 0, orig_y = 0;
  627. void puthex(unsigned long val)
  628. {
  629. unsigned char buf[10];
  630. int i;
  631. for (i = 7; i >= 0; i--)
  632. {
  633. buf[i] = "0123456789ABCDEF"[val & 0x0F];
  634. val >>= 4;
  635. }
  636. buf[8] = '\0';
  637. prom_print(buf);
  638. }
  639. void __init ll_puts(const char *s)
  640. {
  641. int x,y;
  642. char *vidmem = (char *)/*(_ISA_MEM_BASE + 0xB8000) */0xD00B8000;
  643. char c;
  644. extern int mem_init_done;
  645. if ( mem_init_done ) /* assume this means we can printk */
  646. {
  647. printk(s);
  648. return;
  649. }
  650. #if 0
  651. if ( have_of )
  652. {
  653. prom_print(s);
  654. return;
  655. }
  656. #endif
  657. /*
  658. * can't ll_puts on chrp without openfirmware yet.
  659. * vidmem just needs to be setup for it.
  660. * -- Cort
  661. */
  662. if ( _machine != _MACH_prep )
  663. return;
  664. x = orig_x;
  665. y = orig_y;
  666. while ( ( c = *s++ ) != '\0' ) {
  667. if ( c == '\n' ) {
  668. x = 0;
  669. if ( ++y >= lines ) {
  670. /*scroll();*/
  671. /*y--;*/
  672. y = 0;
  673. }
  674. } else {
  675. vidmem [ ( x + cols * y ) * 2 ] = c;
  676. if ( ++x >= cols ) {
  677. x = 0;
  678. if ( ++y >= lines ) {
  679. /*scroll();*/
  680. /*y--;*/
  681. y = 0;
  682. }
  683. }
  684. }
  685. }
  686. orig_x = x;
  687. orig_y = y;
  688. }
  689. #endif
  690. unsigned long get_wchan(struct task_struct *p)
  691. {
  692. unsigned long ip, sp;
  693. unsigned long stack_page = (unsigned long) p->thread_info;
  694. int count = 0;
  695. if (!p || p == current || p->state == TASK_RUNNING)
  696. return 0;
  697. sp = p->thread.ksp;
  698. do {
  699. sp = *(unsigned long *)sp;
  700. if (sp < stack_page || sp >= stack_page + 8188)
  701. return 0;
  702. if (count > 0) {
  703. ip = *(unsigned long *)(sp + 4);
  704. if (!in_sched_functions(ip))
  705. return ip;
  706. }
  707. } while (count++ < 16);
  708. return 0;
  709. }