process.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. /*
  137. * Make sure the floating-point register state in the
  138. * the thread_struct is up to date for task tsk.
  139. */
  140. void flush_fp_to_thread(struct task_struct *tsk)
  141. {
  142. if (tsk->thread.regs) {
  143. /*
  144. * We need to disable preemption here because if we didn't,
  145. * another process could get scheduled after the regs->msr
  146. * test but before we have finished saving the FP registers
  147. * to the thread_struct. That process could take over the
  148. * FPU, and then when we get scheduled again we would store
  149. * bogus values for the remaining FP registers.
  150. */
  151. preempt_disable();
  152. if (tsk->thread.regs->msr & MSR_FP) {
  153. #ifdef CONFIG_SMP
  154. /*
  155. * This should only ever be called for current or
  156. * for a stopped child process. Since we save away
  157. * the FP register state on context switch on SMP,
  158. * there is something wrong if a stopped child appears
  159. * to still have its FP state in the CPU registers.
  160. */
  161. BUG_ON(tsk != current);
  162. #endif
  163. giveup_fpu(current);
  164. }
  165. preempt_enable();
  166. }
  167. }
  168. void enable_kernel_fp(void)
  169. {
  170. WARN_ON(preemptible());
  171. #ifdef CONFIG_SMP
  172. if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
  173. giveup_fpu(current);
  174. else
  175. giveup_fpu(NULL); /* just enables FP for kernel */
  176. #else
  177. giveup_fpu(last_task_used_math);
  178. #endif /* CONFIG_SMP */
  179. }
  180. EXPORT_SYMBOL(enable_kernel_fp);
  181. int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
  182. {
  183. preempt_disable();
  184. if (tsk->thread.regs && (tsk->thread.regs->msr & MSR_FP))
  185. giveup_fpu(tsk);
  186. preempt_enable();
  187. memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
  188. return 1;
  189. }
  190. #ifdef CONFIG_ALTIVEC
  191. void enable_kernel_altivec(void)
  192. {
  193. WARN_ON(preemptible());
  194. #ifdef CONFIG_SMP
  195. if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
  196. giveup_altivec(current);
  197. else
  198. giveup_altivec(NULL); /* just enable AltiVec for kernel - force */
  199. #else
  200. giveup_altivec(last_task_used_altivec);
  201. #endif /* __SMP __ */
  202. }
  203. EXPORT_SYMBOL(enable_kernel_altivec);
  204. /*
  205. * Make sure the VMX/Altivec register state in the
  206. * the thread_struct is up to date for task tsk.
  207. */
  208. void flush_altivec_to_thread(struct task_struct *tsk)
  209. {
  210. if (tsk->thread.regs) {
  211. preempt_disable();
  212. if (tsk->thread.regs->msr & MSR_VEC) {
  213. #ifdef CONFIG_SMP
  214. BUG_ON(tsk != current);
  215. #endif
  216. giveup_altivec(current);
  217. }
  218. preempt_enable();
  219. }
  220. }
  221. int dump_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
  222. {
  223. if (regs->msr & MSR_VEC)
  224. giveup_altivec(current);
  225. memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs));
  226. return 1;
  227. }
  228. #endif /* CONFIG_ALTIVEC */
  229. #ifdef CONFIG_SPE
  230. void
  231. enable_kernel_spe(void)
  232. {
  233. WARN_ON(preemptible());
  234. #ifdef CONFIG_SMP
  235. if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
  236. giveup_spe(current);
  237. else
  238. giveup_spe(NULL); /* just enable SPE for kernel - force */
  239. #else
  240. giveup_spe(last_task_used_spe);
  241. #endif /* __SMP __ */
  242. }
  243. EXPORT_SYMBOL(enable_kernel_spe);
  244. void flush_spe_to_thread(struct task_struct *tsk)
  245. {
  246. if (tsk->thread.regs) {
  247. preempt_disable();
  248. if (tsk->thread.regs->msr & MSR_SPE) {
  249. #ifdef CONFIG_SMP
  250. BUG_ON(tsk != current);
  251. #endif
  252. giveup_spe(current);
  253. }
  254. preempt_enable();
  255. }
  256. }
  257. int dump_spe(struct pt_regs *regs, elf_vrregset_t *evrregs)
  258. {
  259. if (regs->msr & MSR_SPE)
  260. giveup_spe(current);
  261. /* We copy u32 evr[32] + u64 acc + u32 spefscr -> 35 */
  262. memcpy(evrregs, &current->thread.evr[0], sizeof(u32) * 35);
  263. return 1;
  264. }
  265. #endif /* CONFIG_SPE */
  266. struct task_struct *__switch_to(struct task_struct *prev,
  267. struct task_struct *new)
  268. {
  269. struct thread_struct *new_thread, *old_thread;
  270. unsigned long s;
  271. struct task_struct *last;
  272. local_irq_save(s);
  273. #ifdef CHECK_STACK
  274. check_stack(prev);
  275. check_stack(new);
  276. #endif
  277. #ifdef CONFIG_SMP
  278. /* avoid complexity of lazy save/restore of fpu
  279. * by just saving it every time we switch out if
  280. * this task used the fpu during the last quantum.
  281. *
  282. * If it tries to use the fpu again, it'll trap and
  283. * reload its fp regs. So we don't have to do a restore
  284. * every switch, just a save.
  285. * -- Cort
  286. */
  287. if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
  288. giveup_fpu(prev);
  289. #ifdef CONFIG_ALTIVEC
  290. /*
  291. * If the previous thread used altivec in the last quantum
  292. * (thus changing altivec regs) then save them.
  293. * We used to check the VRSAVE register but not all apps
  294. * set it, so we don't rely on it now (and in fact we need
  295. * to save & restore VSCR even if VRSAVE == 0). -- paulus
  296. *
  297. * On SMP we always save/restore altivec regs just to avoid the
  298. * complexity of changing processors.
  299. * -- Cort
  300. */
  301. if ((prev->thread.regs && (prev->thread.regs->msr & MSR_VEC)))
  302. giveup_altivec(prev);
  303. #endif /* CONFIG_ALTIVEC */
  304. #ifdef CONFIG_SPE
  305. /*
  306. * If the previous thread used spe in the last quantum
  307. * (thus changing spe regs) then save them.
  308. *
  309. * On SMP we always save/restore spe regs just to avoid the
  310. * complexity of changing processors.
  311. */
  312. if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
  313. giveup_spe(prev);
  314. #endif /* CONFIG_SPE */
  315. #endif /* CONFIG_SMP */
  316. #ifdef CONFIG_ALTIVEC
  317. /* Avoid the trap. On smp this this never happens since
  318. * we don't set last_task_used_altivec -- Cort
  319. */
  320. if (new->thread.regs && last_task_used_altivec == new)
  321. new->thread.regs->msr |= MSR_VEC;
  322. #endif
  323. #ifdef CONFIG_SPE
  324. /* Avoid the trap. On smp this this never happens since
  325. * we don't set last_task_used_spe
  326. */
  327. if (new->thread.regs && last_task_used_spe == new)
  328. new->thread.regs->msr |= MSR_SPE;
  329. #endif /* CONFIG_SPE */
  330. new_thread = &new->thread;
  331. old_thread = &current->thread;
  332. last = _switch(old_thread, new_thread);
  333. local_irq_restore(s);
  334. return last;
  335. }
  336. void show_regs(struct pt_regs * regs)
  337. {
  338. int i, trap;
  339. printk("NIP: %08lX LR: %08lX SP: %08lX REGS: %p TRAP: %04lx %s\n",
  340. regs->nip, regs->link, regs->gpr[1], regs, regs->trap,
  341. print_tainted());
  342. printk("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
  343. regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
  344. regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
  345. regs->msr&MSR_IR ? 1 : 0,
  346. regs->msr&MSR_DR ? 1 : 0);
  347. trap = TRAP(regs);
  348. if (trap == 0x300 || trap == 0x600)
  349. printk("DAR: %08lX, DSISR: %08lX\n", regs->dar, regs->dsisr);
  350. printk("TASK = %p[%d] '%s' THREAD: %p\n",
  351. current, current->pid, current->comm, current->thread_info);
  352. printk("Last syscall: %ld ", current->thread.last_syscall);
  353. #ifdef CONFIG_SMP
  354. printk(" CPU: %d", smp_processor_id());
  355. #endif /* CONFIG_SMP */
  356. for (i = 0; i < 32; i++) {
  357. long r;
  358. if ((i % 8) == 0)
  359. printk("\n" KERN_INFO "GPR%02d: ", i);
  360. if (__get_user(r, &regs->gpr[i]))
  361. break;
  362. printk("%08lX ", r);
  363. if (i == 12 && !FULL_REGS(regs))
  364. break;
  365. }
  366. printk("\n");
  367. #ifdef CONFIG_KALLSYMS
  368. /*
  369. * Lookup NIP late so we have the best change of getting the
  370. * above info out without failing
  371. */
  372. printk("NIP [%08lx] ", regs->nip);
  373. print_symbol("%s\n", regs->nip);
  374. printk("LR [%08lx] ", regs->link);
  375. print_symbol("%s\n", regs->link);
  376. #endif
  377. show_stack(current, (unsigned long *) regs->gpr[1]);
  378. }
  379. void exit_thread(void)
  380. {
  381. if (last_task_used_math == current)
  382. last_task_used_math = NULL;
  383. if (last_task_used_altivec == current)
  384. last_task_used_altivec = NULL;
  385. #ifdef CONFIG_SPE
  386. if (last_task_used_spe == current)
  387. last_task_used_spe = NULL;
  388. #endif
  389. }
  390. void flush_thread(void)
  391. {
  392. if (last_task_used_math == current)
  393. last_task_used_math = NULL;
  394. if (last_task_used_altivec == current)
  395. last_task_used_altivec = NULL;
  396. #ifdef CONFIG_SPE
  397. if (last_task_used_spe == current)
  398. last_task_used_spe = NULL;
  399. #endif
  400. }
  401. void
  402. release_thread(struct task_struct *t)
  403. {
  404. }
  405. /*
  406. * This gets called before we allocate a new thread and copy
  407. * the current task into it.
  408. */
  409. void prepare_to_copy(struct task_struct *tsk)
  410. {
  411. struct pt_regs *regs = tsk->thread.regs;
  412. if (regs == NULL)
  413. return;
  414. preempt_disable();
  415. if (regs->msr & MSR_FP)
  416. giveup_fpu(current);
  417. #ifdef CONFIG_ALTIVEC
  418. if (regs->msr & MSR_VEC)
  419. giveup_altivec(current);
  420. #endif /* CONFIG_ALTIVEC */
  421. #ifdef CONFIG_SPE
  422. if (regs->msr & MSR_SPE)
  423. giveup_spe(current);
  424. #endif /* CONFIG_SPE */
  425. preempt_enable();
  426. }
  427. /*
  428. * Copy a thread..
  429. */
  430. int
  431. copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  432. unsigned long unused,
  433. struct task_struct *p, struct pt_regs *regs)
  434. {
  435. struct pt_regs *childregs, *kregs;
  436. extern void ret_from_fork(void);
  437. unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
  438. unsigned long childframe;
  439. CHECK_FULL_REGS(regs);
  440. /* Copy registers */
  441. sp -= sizeof(struct pt_regs);
  442. childregs = (struct pt_regs *) sp;
  443. *childregs = *regs;
  444. if ((childregs->msr & MSR_PR) == 0) {
  445. /* for kernel thread, set `current' and stackptr in new task */
  446. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  447. childregs->gpr[2] = (unsigned long) p;
  448. p->thread.regs = NULL; /* no user register state */
  449. } else {
  450. childregs->gpr[1] = usp;
  451. p->thread.regs = childregs;
  452. if (clone_flags & CLONE_SETTLS)
  453. childregs->gpr[2] = childregs->gpr[6];
  454. }
  455. childregs->gpr[3] = 0; /* Result from fork() */
  456. sp -= STACK_FRAME_OVERHEAD;
  457. childframe = sp;
  458. /*
  459. * The way this works is that at some point in the future
  460. * some task will call _switch to switch to the new task.
  461. * That will pop off the stack frame created below and start
  462. * the new task running at ret_from_fork. The new task will
  463. * do some house keeping and then return from the fork or clone
  464. * system call, using the stack frame created above.
  465. */
  466. sp -= sizeof(struct pt_regs);
  467. kregs = (struct pt_regs *) sp;
  468. sp -= STACK_FRAME_OVERHEAD;
  469. p->thread.ksp = sp;
  470. kregs->nip = (unsigned long)ret_from_fork;
  471. p->thread.last_syscall = -1;
  472. return 0;
  473. }
  474. /*
  475. * Set up a thread for executing a new program
  476. */
  477. void start_thread(struct pt_regs *regs, unsigned long nip, unsigned long sp)
  478. {
  479. set_fs(USER_DS);
  480. memset(regs->gpr, 0, sizeof(regs->gpr));
  481. regs->ctr = 0;
  482. regs->link = 0;
  483. regs->xer = 0;
  484. regs->ccr = 0;
  485. regs->mq = 0;
  486. regs->nip = nip;
  487. regs->gpr[1] = sp;
  488. regs->msr = MSR_USER;
  489. if (last_task_used_math == current)
  490. last_task_used_math = NULL;
  491. if (last_task_used_altivec == current)
  492. last_task_used_altivec = NULL;
  493. #ifdef CONFIG_SPE
  494. if (last_task_used_spe == current)
  495. last_task_used_spe = NULL;
  496. #endif
  497. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  498. current->thread.fpscr.val = 0;
  499. #ifdef CONFIG_ALTIVEC
  500. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  501. memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
  502. current->thread.vrsave = 0;
  503. current->thread.used_vr = 0;
  504. #endif /* CONFIG_ALTIVEC */
  505. #ifdef CONFIG_SPE
  506. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  507. current->thread.acc = 0;
  508. current->thread.spefscr = 0;
  509. current->thread.used_spe = 0;
  510. #endif /* CONFIG_SPE */
  511. }
  512. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  513. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  514. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  515. {
  516. struct pt_regs *regs = tsk->thread.regs;
  517. /* This is a bit hairy. If we are an SPE enabled processor
  518. * (have embedded fp) we store the IEEE exception enable flags in
  519. * fpexc_mode. fpexc_mode is also used for setting FP exception
  520. * mode (asyn, precise, disabled) for 'Classic' FP. */
  521. if (val & PR_FP_EXC_SW_ENABLE) {
  522. #ifdef CONFIG_SPE
  523. tsk->thread.fpexc_mode = val &
  524. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  525. #else
  526. return -EINVAL;
  527. #endif
  528. } else {
  529. /* on a CONFIG_SPE this does not hurt us. The bits that
  530. * __pack_fe01 use do not overlap with bits used for
  531. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  532. * on CONFIG_SPE implementations are reserved so writing to
  533. * them does not change anything */
  534. if (val > PR_FP_EXC_PRECISE)
  535. return -EINVAL;
  536. tsk->thread.fpexc_mode = __pack_fe01(val);
  537. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  538. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  539. | tsk->thread.fpexc_mode;
  540. }
  541. return 0;
  542. }
  543. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  544. {
  545. unsigned int val;
  546. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  547. #ifdef CONFIG_SPE
  548. val = tsk->thread.fpexc_mode;
  549. #else
  550. return -EINVAL;
  551. #endif
  552. else
  553. val = __unpack_fe01(tsk->thread.fpexc_mode);
  554. return put_user(val, (unsigned int __user *) adr);
  555. }
  556. int sys_clone(unsigned long clone_flags, unsigned long usp,
  557. int __user *parent_tidp, void __user *child_threadptr,
  558. int __user *child_tidp, int p6,
  559. struct pt_regs *regs)
  560. {
  561. CHECK_FULL_REGS(regs);
  562. if (usp == 0)
  563. usp = regs->gpr[1]; /* stack pointer for child */
  564. return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
  565. }
  566. int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
  567. unsigned long p4, unsigned long p5, unsigned long p6,
  568. struct pt_regs *regs)
  569. {
  570. CHECK_FULL_REGS(regs);
  571. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  572. }
  573. int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
  574. unsigned long p4, unsigned long p5, unsigned long p6,
  575. struct pt_regs *regs)
  576. {
  577. CHECK_FULL_REGS(regs);
  578. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
  579. regs, 0, NULL, NULL);
  580. }
  581. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  582. unsigned long a3, unsigned long a4, unsigned long a5,
  583. struct pt_regs *regs)
  584. {
  585. int error;
  586. char * filename;
  587. filename = getname((char __user *) a0);
  588. error = PTR_ERR(filename);
  589. if (IS_ERR(filename))
  590. goto out;
  591. preempt_disable();
  592. if (regs->msr & MSR_FP)
  593. giveup_fpu(current);
  594. #ifdef CONFIG_ALTIVEC
  595. if (regs->msr & MSR_VEC)
  596. giveup_altivec(current);
  597. #endif /* CONFIG_ALTIVEC */
  598. #ifdef CONFIG_SPE
  599. if (regs->msr & MSR_SPE)
  600. giveup_spe(current);
  601. #endif /* CONFIG_SPE */
  602. preempt_enable();
  603. error = do_execve(filename, (char __user *__user *) a1,
  604. (char __user *__user *) a2, regs);
  605. if (error == 0) {
  606. task_lock(current);
  607. current->ptrace &= ~PT_DTRACE;
  608. task_unlock(current);
  609. }
  610. putname(filename);
  611. out:
  612. return error;
  613. }
  614. void dump_stack(void)
  615. {
  616. show_stack(current, NULL);
  617. }
  618. EXPORT_SYMBOL(dump_stack);
  619. void show_stack(struct task_struct *tsk, unsigned long *stack)
  620. {
  621. unsigned long sp, stack_top, prev_sp, ret;
  622. int count = 0;
  623. unsigned long next_exc = 0;
  624. struct pt_regs *regs;
  625. extern char ret_from_except, ret_from_except_full, ret_from_syscall;
  626. sp = (unsigned long) stack;
  627. if (tsk == NULL)
  628. tsk = current;
  629. if (sp == 0) {
  630. if (tsk == current)
  631. asm("mr %0,1" : "=r" (sp));
  632. else
  633. sp = tsk->thread.ksp;
  634. }
  635. prev_sp = (unsigned long) (tsk->thread_info + 1);
  636. stack_top = (unsigned long) tsk->thread_info + THREAD_SIZE;
  637. while (count < 16 && sp > prev_sp && sp < stack_top && (sp & 3) == 0) {
  638. if (count == 0) {
  639. printk("Call trace:");
  640. #ifdef CONFIG_KALLSYMS
  641. printk("\n");
  642. #endif
  643. } else {
  644. if (next_exc) {
  645. ret = next_exc;
  646. next_exc = 0;
  647. } else
  648. ret = *(unsigned long *)(sp + 4);
  649. printk(" [%08lx] ", ret);
  650. #ifdef CONFIG_KALLSYMS
  651. print_symbol("%s", ret);
  652. printk("\n");
  653. #endif
  654. if (ret == (unsigned long) &ret_from_except
  655. || ret == (unsigned long) &ret_from_except_full
  656. || ret == (unsigned long) &ret_from_syscall) {
  657. /* sp + 16 points to an exception frame */
  658. regs = (struct pt_regs *) (sp + 16);
  659. if (sp + 16 + sizeof(*regs) <= stack_top)
  660. next_exc = regs->nip;
  661. }
  662. }
  663. ++count;
  664. sp = *(unsigned long *)sp;
  665. }
  666. #ifndef CONFIG_KALLSYMS
  667. if (count > 0)
  668. printk("\n");
  669. #endif
  670. }
  671. #if 0
  672. /*
  673. * Low level print for debugging - Cort
  674. */
  675. int __init ll_printk(const char *fmt, ...)
  676. {
  677. va_list args;
  678. char buf[256];
  679. int i;
  680. va_start(args, fmt);
  681. i=vsprintf(buf,fmt,args);
  682. ll_puts(buf);
  683. va_end(args);
  684. return i;
  685. }
  686. int lines = 24, cols = 80;
  687. int orig_x = 0, orig_y = 0;
  688. void puthex(unsigned long val)
  689. {
  690. unsigned char buf[10];
  691. int i;
  692. for (i = 7; i >= 0; i--)
  693. {
  694. buf[i] = "0123456789ABCDEF"[val & 0x0F];
  695. val >>= 4;
  696. }
  697. buf[8] = '\0';
  698. prom_print(buf);
  699. }
  700. void __init ll_puts(const char *s)
  701. {
  702. int x,y;
  703. char *vidmem = (char *)/*(_ISA_MEM_BASE + 0xB8000) */0xD00B8000;
  704. char c;
  705. extern int mem_init_done;
  706. if ( mem_init_done ) /* assume this means we can printk */
  707. {
  708. printk(s);
  709. return;
  710. }
  711. #if 0
  712. if ( have_of )
  713. {
  714. prom_print(s);
  715. return;
  716. }
  717. #endif
  718. /*
  719. * can't ll_puts on chrp without openfirmware yet.
  720. * vidmem just needs to be setup for it.
  721. * -- Cort
  722. */
  723. if ( _machine != _MACH_prep )
  724. return;
  725. x = orig_x;
  726. y = orig_y;
  727. while ( ( c = *s++ ) != '\0' ) {
  728. if ( c == '\n' ) {
  729. x = 0;
  730. if ( ++y >= lines ) {
  731. /*scroll();*/
  732. /*y--;*/
  733. y = 0;
  734. }
  735. } else {
  736. vidmem [ ( x + cols * y ) * 2 ] = c;
  737. if ( ++x >= cols ) {
  738. x = 0;
  739. if ( ++y >= lines ) {
  740. /*scroll();*/
  741. /*y--;*/
  742. y = 0;
  743. }
  744. }
  745. }
  746. }
  747. orig_x = x;
  748. orig_y = y;
  749. }
  750. #endif
  751. unsigned long get_wchan(struct task_struct *p)
  752. {
  753. unsigned long ip, sp;
  754. unsigned long stack_page = (unsigned long) p->thread_info;
  755. int count = 0;
  756. if (!p || p == current || p->state == TASK_RUNNING)
  757. return 0;
  758. sp = p->thread.ksp;
  759. do {
  760. sp = *(unsigned long *)sp;
  761. if (sp < stack_page || sp >= stack_page + 8188)
  762. return 0;
  763. if (count > 0) {
  764. ip = *(unsigned long *)(sp + 4);
  765. if (!in_sched_functions(ip))
  766. return ip;
  767. }
  768. } while (count++ < 16);
  769. return 0;
  770. }