process.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. * linux/arch/ppc64/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. #include <linux/config.h>
  19. #include <linux/module.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/slab.h>
  29. #include <linux/user.h>
  30. #include <linux/elf.h>
  31. #include <linux/init.h>
  32. #include <linux/init_task.h>
  33. #include <linux/prctl.h>
  34. #include <linux/ptrace.h>
  35. #include <linux/kallsyms.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/utsname.h>
  38. #include <linux/kprobes.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/mmu_context.h>
  46. #include <asm/prom.h>
  47. #include <asm/ppcdebug.h>
  48. #include <asm/machdep.h>
  49. #include <asm/iSeries/HvCallHpt.h>
  50. #include <asm/cputable.h>
  51. #include <asm/firmware.h>
  52. #include <asm/sections.h>
  53. #include <asm/tlbflush.h>
  54. #include <asm/time.h>
  55. #include <asm/plpar_wrappers.h>
  56. #ifndef CONFIG_SMP
  57. struct task_struct *last_task_used_math = NULL;
  58. struct task_struct *last_task_used_altivec = NULL;
  59. #endif
  60. /*
  61. * Make sure the floating-point register state in the
  62. * the thread_struct is up to date for task tsk.
  63. */
  64. void flush_fp_to_thread(struct task_struct *tsk)
  65. {
  66. if (tsk->thread.regs) {
  67. /*
  68. * We need to disable preemption here because if we didn't,
  69. * another process could get scheduled after the regs->msr
  70. * test but before we have finished saving the FP registers
  71. * to the thread_struct. That process could take over the
  72. * FPU, and then when we get scheduled again we would store
  73. * bogus values for the remaining FP registers.
  74. */
  75. preempt_disable();
  76. if (tsk->thread.regs->msr & MSR_FP) {
  77. #ifdef CONFIG_SMP
  78. /*
  79. * This should only ever be called for current or
  80. * for a stopped child process. Since we save away
  81. * the FP register state on context switch on SMP,
  82. * there is something wrong if a stopped child appears
  83. * to still have its FP state in the CPU registers.
  84. */
  85. BUG_ON(tsk != current);
  86. #endif
  87. giveup_fpu(current);
  88. }
  89. preempt_enable();
  90. }
  91. }
  92. void enable_kernel_fp(void)
  93. {
  94. WARN_ON(preemptible());
  95. #ifdef CONFIG_SMP
  96. if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
  97. giveup_fpu(current);
  98. else
  99. giveup_fpu(NULL); /* just enables FP for kernel */
  100. #else
  101. giveup_fpu(last_task_used_math);
  102. #endif /* CONFIG_SMP */
  103. }
  104. EXPORT_SYMBOL(enable_kernel_fp);
  105. int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
  106. {
  107. if (!tsk->thread.regs)
  108. return 0;
  109. flush_fp_to_thread(current);
  110. memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
  111. return 1;
  112. }
  113. #ifdef CONFIG_ALTIVEC
  114. void enable_kernel_altivec(void)
  115. {
  116. WARN_ON(preemptible());
  117. #ifdef CONFIG_SMP
  118. if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
  119. giveup_altivec(current);
  120. else
  121. giveup_altivec(NULL); /* just enables FP for kernel */
  122. #else
  123. giveup_altivec(last_task_used_altivec);
  124. #endif /* CONFIG_SMP */
  125. }
  126. EXPORT_SYMBOL(enable_kernel_altivec);
  127. /*
  128. * Make sure the VMX/Altivec register state in the
  129. * the thread_struct is up to date for task tsk.
  130. */
  131. void flush_altivec_to_thread(struct task_struct *tsk)
  132. {
  133. if (tsk->thread.regs) {
  134. preempt_disable();
  135. if (tsk->thread.regs->msr & MSR_VEC) {
  136. #ifdef CONFIG_SMP
  137. BUG_ON(tsk != current);
  138. #endif
  139. giveup_altivec(current);
  140. }
  141. preempt_enable();
  142. }
  143. }
  144. int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
  145. {
  146. flush_altivec_to_thread(current);
  147. memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs));
  148. return 1;
  149. }
  150. #endif /* CONFIG_ALTIVEC */
  151. static void set_dabr_spr(unsigned long val)
  152. {
  153. mtspr(SPRN_DABR, val);
  154. }
  155. int set_dabr(unsigned long dabr)
  156. {
  157. int ret = 0;
  158. if (firmware_has_feature(FW_FEATURE_XDABR)) {
  159. /* We want to catch accesses from kernel and userspace */
  160. unsigned long flags = H_DABRX_KERNEL|H_DABRX_USER;
  161. ret = plpar_set_xdabr(dabr, flags);
  162. } else if (firmware_has_feature(FW_FEATURE_DABR)) {
  163. ret = plpar_set_dabr(dabr);
  164. } else {
  165. set_dabr_spr(dabr);
  166. }
  167. return ret;
  168. }
  169. DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
  170. static DEFINE_PER_CPU(unsigned long, current_dabr);
  171. struct task_struct *__switch_to(struct task_struct *prev,
  172. struct task_struct *new)
  173. {
  174. struct thread_struct *new_thread, *old_thread;
  175. unsigned long flags;
  176. struct task_struct *last;
  177. #ifdef CONFIG_SMP
  178. /* avoid complexity of lazy save/restore of fpu
  179. * by just saving it every time we switch out if
  180. * this task used the fpu during the last quantum.
  181. *
  182. * If it tries to use the fpu again, it'll trap and
  183. * reload its fp regs. So we don't have to do a restore
  184. * every switch, just a save.
  185. * -- Cort
  186. */
  187. if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
  188. giveup_fpu(prev);
  189. #ifdef CONFIG_ALTIVEC
  190. if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
  191. giveup_altivec(prev);
  192. #endif /* CONFIG_ALTIVEC */
  193. #endif /* CONFIG_SMP */
  194. #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
  195. /* Avoid the trap. On smp this this never happens since
  196. * we don't set last_task_used_altivec -- Cort
  197. */
  198. if (new->thread.regs && last_task_used_altivec == new)
  199. new->thread.regs->msr |= MSR_VEC;
  200. #endif /* CONFIG_ALTIVEC */
  201. if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr)) {
  202. set_dabr(new->thread.dabr);
  203. __get_cpu_var(current_dabr) = new->thread.dabr;
  204. }
  205. flush_tlb_pending();
  206. new_thread = &new->thread;
  207. old_thread = &current->thread;
  208. /* Collect purr utilization data per process and per processor
  209. * wise purr is nothing but processor time base
  210. */
  211. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  212. struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
  213. long unsigned start_tb, current_tb;
  214. start_tb = old_thread->start_tb;
  215. cu->current_tb = current_tb = mfspr(SPRN_PURR);
  216. old_thread->accum_tb += (current_tb - start_tb);
  217. new_thread->start_tb = current_tb;
  218. }
  219. local_irq_save(flags);
  220. last = _switch(old_thread, new_thread);
  221. local_irq_restore(flags);
  222. return last;
  223. }
  224. static int instructions_to_print = 16;
  225. static void show_instructions(struct pt_regs *regs)
  226. {
  227. int i;
  228. unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
  229. sizeof(int));
  230. printk("Instruction dump:");
  231. for (i = 0; i < instructions_to_print; i++) {
  232. int instr;
  233. if (!(i % 8))
  234. printk("\n");
  235. if (((REGION_ID(pc) != KERNEL_REGION_ID) &&
  236. (REGION_ID(pc) != VMALLOC_REGION_ID)) ||
  237. __get_user(instr, (unsigned int *)pc)) {
  238. printk("XXXXXXXX ");
  239. } else {
  240. if (regs->nip == pc)
  241. printk("<%08x> ", instr);
  242. else
  243. printk("%08x ", instr);
  244. }
  245. pc += sizeof(int);
  246. }
  247. printk("\n");
  248. }
  249. void show_regs(struct pt_regs * regs)
  250. {
  251. int i;
  252. unsigned long trap;
  253. printk("NIP: %016lX XER: %08X LR: %016lX CTR: %016lX\n",
  254. regs->nip, (unsigned int)regs->xer, regs->link, regs->ctr);
  255. printk("REGS: %p TRAP: %04lx %s (%s)\n",
  256. regs, regs->trap, print_tainted(), system_utsname.release);
  257. printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x "
  258. "IR/DR: %01x%01x CR: %08X\n",
  259. regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
  260. regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
  261. regs->msr&MSR_IR ? 1 : 0,
  262. regs->msr&MSR_DR ? 1 : 0,
  263. (unsigned int)regs->ccr);
  264. trap = TRAP(regs);
  265. printk("DAR: %016lx DSISR: %016lx\n", regs->dar, regs->dsisr);
  266. printk("TASK: %p[%d] '%s' THREAD: %p",
  267. current, current->pid, current->comm, current->thread_info);
  268. #ifdef CONFIG_SMP
  269. printk(" CPU: %d", smp_processor_id());
  270. #endif /* CONFIG_SMP */
  271. for (i = 0; i < 32; i++) {
  272. if ((i % 4) == 0) {
  273. printk("\n" KERN_INFO "GPR%02d: ", i);
  274. }
  275. printk("%016lX ", regs->gpr[i]);
  276. if (i == 13 && !FULL_REGS(regs))
  277. break;
  278. }
  279. printk("\n");
  280. /*
  281. * Lookup NIP late so we have the best change of getting the
  282. * above info out without failing
  283. */
  284. printk("NIP [%016lx] ", regs->nip);
  285. print_symbol("%s\n", regs->nip);
  286. printk("LR [%016lx] ", regs->link);
  287. print_symbol("%s\n", regs->link);
  288. show_stack(current, (unsigned long *)regs->gpr[1]);
  289. if (!user_mode(regs))
  290. show_instructions(regs);
  291. }
  292. void exit_thread(void)
  293. {
  294. kprobe_flush_task(current);
  295. #ifndef CONFIG_SMP
  296. if (last_task_used_math == current)
  297. last_task_used_math = NULL;
  298. #ifdef CONFIG_ALTIVEC
  299. if (last_task_used_altivec == current)
  300. last_task_used_altivec = NULL;
  301. #endif /* CONFIG_ALTIVEC */
  302. #endif /* CONFIG_SMP */
  303. }
  304. void flush_thread(void)
  305. {
  306. struct thread_info *t = current_thread_info();
  307. kprobe_flush_task(current);
  308. if (t->flags & _TIF_ABI_PENDING)
  309. t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
  310. #ifndef CONFIG_SMP
  311. if (last_task_used_math == current)
  312. last_task_used_math = NULL;
  313. #ifdef CONFIG_ALTIVEC
  314. if (last_task_used_altivec == current)
  315. last_task_used_altivec = NULL;
  316. #endif /* CONFIG_ALTIVEC */
  317. #endif /* CONFIG_SMP */
  318. if (current->thread.dabr) {
  319. current->thread.dabr = 0;
  320. set_dabr(0);
  321. }
  322. }
  323. void
  324. release_thread(struct task_struct *t)
  325. {
  326. }
  327. /*
  328. * This gets called before we allocate a new thread and copy
  329. * the current task into it.
  330. */
  331. void prepare_to_copy(struct task_struct *tsk)
  332. {
  333. flush_fp_to_thread(current);
  334. flush_altivec_to_thread(current);
  335. }
  336. /*
  337. * Copy a thread..
  338. */
  339. int
  340. copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  341. unsigned long unused, struct task_struct *p, struct pt_regs *regs)
  342. {
  343. struct pt_regs *childregs, *kregs;
  344. extern void ret_from_fork(void);
  345. unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
  346. /* Copy registers */
  347. sp -= sizeof(struct pt_regs);
  348. childregs = (struct pt_regs *) sp;
  349. *childregs = *regs;
  350. if ((childregs->msr & MSR_PR) == 0) {
  351. /* for kernel thread, set stackptr in new task */
  352. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  353. p->thread.regs = NULL; /* no user register state */
  354. clear_ti_thread_flag(p->thread_info, TIF_32BIT);
  355. } else {
  356. childregs->gpr[1] = usp;
  357. p->thread.regs = childregs;
  358. if (clone_flags & CLONE_SETTLS) {
  359. if (test_thread_flag(TIF_32BIT))
  360. childregs->gpr[2] = childregs->gpr[6];
  361. else
  362. childregs->gpr[13] = childregs->gpr[6];
  363. }
  364. }
  365. childregs->gpr[3] = 0; /* Result from fork() */
  366. sp -= STACK_FRAME_OVERHEAD;
  367. /*
  368. * The way this works is that at some point in the future
  369. * some task will call _switch to switch to the new task.
  370. * That will pop off the stack frame created below and start
  371. * the new task running at ret_from_fork. The new task will
  372. * do some house keeping and then return from the fork or clone
  373. * system call, using the stack frame created above.
  374. */
  375. sp -= sizeof(struct pt_regs);
  376. kregs = (struct pt_regs *) sp;
  377. sp -= STACK_FRAME_OVERHEAD;
  378. p->thread.ksp = sp;
  379. if (cpu_has_feature(CPU_FTR_SLB)) {
  380. unsigned long sp_vsid = get_kernel_vsid(sp);
  381. sp_vsid <<= SLB_VSID_SHIFT;
  382. sp_vsid |= SLB_VSID_KERNEL;
  383. if (cpu_has_feature(CPU_FTR_16M_PAGE))
  384. sp_vsid |= SLB_VSID_L;
  385. p->thread.ksp_vsid = sp_vsid;
  386. }
  387. /*
  388. * The PPC64 ABI makes use of a TOC to contain function
  389. * pointers. The function (ret_from_except) is actually a pointer
  390. * to the TOC entry. The first entry is a pointer to the actual
  391. * function.
  392. */
  393. kregs->nip = *((unsigned long *)ret_from_fork);
  394. return 0;
  395. }
  396. /*
  397. * Set up a thread for executing a new program
  398. */
  399. void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
  400. {
  401. unsigned long entry, toc, load_addr = regs->gpr[2];
  402. /* fdptr is a relocated pointer to the function descriptor for
  403. * the elf _start routine. The first entry in the function
  404. * descriptor is the entry address of _start and the second
  405. * entry is the TOC value we need to use.
  406. */
  407. set_fs(USER_DS);
  408. __get_user(entry, (unsigned long __user *)fdptr);
  409. __get_user(toc, (unsigned long __user *)fdptr+1);
  410. /* Check whether the e_entry function descriptor entries
  411. * need to be relocated before we can use them.
  412. */
  413. if (load_addr != 0) {
  414. entry += load_addr;
  415. toc += load_addr;
  416. }
  417. /*
  418. * If we exec out of a kernel thread then thread.regs will not be
  419. * set. Do it now.
  420. */
  421. if (!current->thread.regs) {
  422. unsigned long childregs = (unsigned long)current->thread_info +
  423. THREAD_SIZE;
  424. childregs -= sizeof(struct pt_regs);
  425. current->thread.regs = (struct pt_regs *)childregs;
  426. }
  427. regs->nip = entry;
  428. regs->gpr[1] = sp;
  429. regs->gpr[2] = toc;
  430. regs->msr = MSR_USER64;
  431. #ifndef CONFIG_SMP
  432. if (last_task_used_math == current)
  433. last_task_used_math = 0;
  434. #endif /* CONFIG_SMP */
  435. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  436. current->thread.fpscr = 0;
  437. #ifdef CONFIG_ALTIVEC
  438. #ifndef CONFIG_SMP
  439. if (last_task_used_altivec == current)
  440. last_task_used_altivec = 0;
  441. #endif /* CONFIG_SMP */
  442. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  443. current->thread.vscr.u[0] = 0;
  444. current->thread.vscr.u[1] = 0;
  445. current->thread.vscr.u[2] = 0;
  446. current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
  447. current->thread.vrsave = 0;
  448. current->thread.used_vr = 0;
  449. #endif /* CONFIG_ALTIVEC */
  450. }
  451. EXPORT_SYMBOL(start_thread);
  452. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  453. {
  454. struct pt_regs *regs = tsk->thread.regs;
  455. if (val > PR_FP_EXC_PRECISE)
  456. return -EINVAL;
  457. tsk->thread.fpexc_mode = __pack_fe01(val);
  458. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  459. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  460. | tsk->thread.fpexc_mode;
  461. return 0;
  462. }
  463. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  464. {
  465. unsigned int val;
  466. val = __unpack_fe01(tsk->thread.fpexc_mode);
  467. return put_user(val, (unsigned int __user *) adr);
  468. }
  469. int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3,
  470. unsigned long p4, unsigned long p5, unsigned long p6,
  471. struct pt_regs *regs)
  472. {
  473. unsigned long parent_tidptr = 0;
  474. unsigned long child_tidptr = 0;
  475. if (p2 == 0)
  476. p2 = regs->gpr[1]; /* stack pointer for child */
  477. if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
  478. CLONE_CHILD_CLEARTID)) {
  479. parent_tidptr = p3;
  480. child_tidptr = p5;
  481. if (test_thread_flag(TIF_32BIT)) {
  482. parent_tidptr &= 0xffffffff;
  483. child_tidptr &= 0xffffffff;
  484. }
  485. }
  486. return do_fork(clone_flags, p2, regs, 0,
  487. (int __user *)parent_tidptr, (int __user *)child_tidptr);
  488. }
  489. int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
  490. unsigned long p4, unsigned long p5, unsigned long p6,
  491. struct pt_regs *regs)
  492. {
  493. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  494. }
  495. int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
  496. unsigned long p4, unsigned long p5, unsigned long p6,
  497. struct pt_regs *regs)
  498. {
  499. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0,
  500. NULL, NULL);
  501. }
  502. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  503. unsigned long a3, unsigned long a4, unsigned long a5,
  504. struct pt_regs *regs)
  505. {
  506. int error;
  507. char * filename;
  508. filename = getname((char __user *) a0);
  509. error = PTR_ERR(filename);
  510. if (IS_ERR(filename))
  511. goto out;
  512. flush_fp_to_thread(current);
  513. flush_altivec_to_thread(current);
  514. error = do_execve(filename, (char __user * __user *) a1,
  515. (char __user * __user *) a2, regs);
  516. if (error == 0) {
  517. task_lock(current);
  518. current->ptrace &= ~PT_DTRACE;
  519. task_unlock(current);
  520. }
  521. putname(filename);
  522. out:
  523. return error;
  524. }
  525. static int kstack_depth_to_print = 64;
  526. static int validate_sp(unsigned long sp, struct task_struct *p,
  527. unsigned long nbytes)
  528. {
  529. unsigned long stack_page = (unsigned long)p->thread_info;
  530. if (sp >= stack_page + sizeof(struct thread_struct)
  531. && sp <= stack_page + THREAD_SIZE - nbytes)
  532. return 1;
  533. #ifdef CONFIG_IRQSTACKS
  534. stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
  535. if (sp >= stack_page + sizeof(struct thread_struct)
  536. && sp <= stack_page + THREAD_SIZE - nbytes)
  537. return 1;
  538. stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
  539. if (sp >= stack_page + sizeof(struct thread_struct)
  540. && sp <= stack_page + THREAD_SIZE - nbytes)
  541. return 1;
  542. #endif
  543. return 0;
  544. }
  545. unsigned long get_wchan(struct task_struct *p)
  546. {
  547. unsigned long ip, sp;
  548. int count = 0;
  549. if (!p || p == current || p->state == TASK_RUNNING)
  550. return 0;
  551. sp = p->thread.ksp;
  552. if (!validate_sp(sp, p, 112))
  553. return 0;
  554. do {
  555. sp = *(unsigned long *)sp;
  556. if (!validate_sp(sp, p, 112))
  557. return 0;
  558. if (count > 0) {
  559. ip = *(unsigned long *)(sp + 16);
  560. if (!in_sched_functions(ip))
  561. return ip;
  562. }
  563. } while (count++ < 16);
  564. return 0;
  565. }
  566. EXPORT_SYMBOL(get_wchan);
  567. void show_stack(struct task_struct *p, unsigned long *_sp)
  568. {
  569. unsigned long ip, newsp, lr;
  570. int count = 0;
  571. unsigned long sp = (unsigned long)_sp;
  572. int firstframe = 1;
  573. if (sp == 0) {
  574. if (p) {
  575. sp = p->thread.ksp;
  576. } else {
  577. sp = __get_SP();
  578. p = current;
  579. }
  580. }
  581. lr = 0;
  582. printk("Call Trace:\n");
  583. do {
  584. if (!validate_sp(sp, p, 112))
  585. return;
  586. _sp = (unsigned long *) sp;
  587. newsp = _sp[0];
  588. ip = _sp[2];
  589. if (!firstframe || ip != lr) {
  590. printk("[%016lx] [%016lx] ", sp, ip);
  591. print_symbol("%s", ip);
  592. if (firstframe)
  593. printk(" (unreliable)");
  594. printk("\n");
  595. }
  596. firstframe = 0;
  597. /*
  598. * See if this is an exception frame.
  599. * We look for the "regshere" marker in the current frame.
  600. */
  601. if (validate_sp(sp, p, sizeof(struct pt_regs) + 400)
  602. && _sp[12] == 0x7265677368657265ul) {
  603. struct pt_regs *regs = (struct pt_regs *)
  604. (sp + STACK_FRAME_OVERHEAD);
  605. printk("--- Exception: %lx", regs->trap);
  606. print_symbol(" at %s\n", regs->nip);
  607. lr = regs->link;
  608. print_symbol(" LR = %s\n", lr);
  609. firstframe = 1;
  610. }
  611. sp = newsp;
  612. } while (count++ < kstack_depth_to_print);
  613. }
  614. void dump_stack(void)
  615. {
  616. show_stack(current, (unsigned long *)__get_SP());
  617. }
  618. EXPORT_SYMBOL(dump_stack);