process.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. preempt_disable();
  382. if (last_task_used_math == current)
  383. last_task_used_math = NULL;
  384. if (last_task_used_altivec == current)
  385. last_task_used_altivec = NULL;
  386. #ifdef CONFIG_SPE
  387. if (last_task_used_spe == current)
  388. last_task_used_spe = NULL;
  389. #endif
  390. preempt_enable();
  391. }
  392. void flush_thread(void)
  393. {
  394. preempt_disable();
  395. if (last_task_used_math == current)
  396. last_task_used_math = NULL;
  397. if (last_task_used_altivec == current)
  398. last_task_used_altivec = NULL;
  399. #ifdef CONFIG_SPE
  400. if (last_task_used_spe == current)
  401. last_task_used_spe = NULL;
  402. #endif
  403. preempt_enable();
  404. }
  405. void
  406. release_thread(struct task_struct *t)
  407. {
  408. }
  409. /*
  410. * This gets called before we allocate a new thread and copy
  411. * the current task into it.
  412. */
  413. void prepare_to_copy(struct task_struct *tsk)
  414. {
  415. struct pt_regs *regs = tsk->thread.regs;
  416. if (regs == NULL)
  417. return;
  418. preempt_disable();
  419. if (regs->msr & MSR_FP)
  420. giveup_fpu(current);
  421. #ifdef CONFIG_ALTIVEC
  422. if (regs->msr & MSR_VEC)
  423. giveup_altivec(current);
  424. #endif /* CONFIG_ALTIVEC */
  425. #ifdef CONFIG_SPE
  426. if (regs->msr & MSR_SPE)
  427. giveup_spe(current);
  428. #endif /* CONFIG_SPE */
  429. preempt_enable();
  430. }
  431. /*
  432. * Copy a thread..
  433. */
  434. int
  435. copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  436. unsigned long unused,
  437. struct task_struct *p, struct pt_regs *regs)
  438. {
  439. struct pt_regs *childregs, *kregs;
  440. extern void ret_from_fork(void);
  441. unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
  442. unsigned long childframe;
  443. CHECK_FULL_REGS(regs);
  444. /* Copy registers */
  445. sp -= sizeof(struct pt_regs);
  446. childregs = (struct pt_regs *) sp;
  447. *childregs = *regs;
  448. if ((childregs->msr & MSR_PR) == 0) {
  449. /* for kernel thread, set `current' and stackptr in new task */
  450. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  451. childregs->gpr[2] = (unsigned long) p;
  452. p->thread.regs = NULL; /* no user register state */
  453. } else {
  454. childregs->gpr[1] = usp;
  455. p->thread.regs = childregs;
  456. if (clone_flags & CLONE_SETTLS)
  457. childregs->gpr[2] = childregs->gpr[6];
  458. }
  459. childregs->gpr[3] = 0; /* Result from fork() */
  460. sp -= STACK_FRAME_OVERHEAD;
  461. childframe = sp;
  462. /*
  463. * The way this works is that at some point in the future
  464. * some task will call _switch to switch to the new task.
  465. * That will pop off the stack frame created below and start
  466. * the new task running at ret_from_fork. The new task will
  467. * do some house keeping and then return from the fork or clone
  468. * system call, using the stack frame created above.
  469. */
  470. sp -= sizeof(struct pt_regs);
  471. kregs = (struct pt_regs *) sp;
  472. sp -= STACK_FRAME_OVERHEAD;
  473. p->thread.ksp = sp;
  474. kregs->nip = (unsigned long)ret_from_fork;
  475. p->thread.last_syscall = -1;
  476. return 0;
  477. }
  478. /*
  479. * Set up a thread for executing a new program
  480. */
  481. void start_thread(struct pt_regs *regs, unsigned long nip, unsigned long sp)
  482. {
  483. set_fs(USER_DS);
  484. memset(regs->gpr, 0, sizeof(regs->gpr));
  485. regs->ctr = 0;
  486. regs->link = 0;
  487. regs->xer = 0;
  488. regs->ccr = 0;
  489. regs->mq = 0;
  490. regs->nip = nip;
  491. regs->gpr[1] = sp;
  492. regs->msr = MSR_USER;
  493. preempt_disable();
  494. if (last_task_used_math == current)
  495. last_task_used_math = NULL;
  496. if (last_task_used_altivec == current)
  497. last_task_used_altivec = NULL;
  498. #ifdef CONFIG_SPE
  499. if (last_task_used_spe == current)
  500. last_task_used_spe = NULL;
  501. #endif
  502. preempt_enable();
  503. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  504. current->thread.fpscr.val = 0;
  505. #ifdef CONFIG_ALTIVEC
  506. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  507. memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
  508. current->thread.vrsave = 0;
  509. current->thread.used_vr = 0;
  510. #endif /* CONFIG_ALTIVEC */
  511. #ifdef CONFIG_SPE
  512. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  513. current->thread.acc = 0;
  514. current->thread.spefscr = 0;
  515. current->thread.used_spe = 0;
  516. #endif /* CONFIG_SPE */
  517. }
  518. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  519. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  520. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  521. {
  522. struct pt_regs *regs = tsk->thread.regs;
  523. /* This is a bit hairy. If we are an SPE enabled processor
  524. * (have embedded fp) we store the IEEE exception enable flags in
  525. * fpexc_mode. fpexc_mode is also used for setting FP exception
  526. * mode (asyn, precise, disabled) for 'Classic' FP. */
  527. if (val & PR_FP_EXC_SW_ENABLE) {
  528. #ifdef CONFIG_SPE
  529. tsk->thread.fpexc_mode = val &
  530. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  531. #else
  532. return -EINVAL;
  533. #endif
  534. } else {
  535. /* on a CONFIG_SPE this does not hurt us. The bits that
  536. * __pack_fe01 use do not overlap with bits used for
  537. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  538. * on CONFIG_SPE implementations are reserved so writing to
  539. * them does not change anything */
  540. if (val > PR_FP_EXC_PRECISE)
  541. return -EINVAL;
  542. tsk->thread.fpexc_mode = __pack_fe01(val);
  543. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  544. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  545. | tsk->thread.fpexc_mode;
  546. }
  547. return 0;
  548. }
  549. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  550. {
  551. unsigned int val;
  552. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  553. #ifdef CONFIG_SPE
  554. val = tsk->thread.fpexc_mode;
  555. #else
  556. return -EINVAL;
  557. #endif
  558. else
  559. val = __unpack_fe01(tsk->thread.fpexc_mode);
  560. return put_user(val, (unsigned int __user *) adr);
  561. }
  562. int sys_clone(unsigned long clone_flags, unsigned long usp,
  563. int __user *parent_tidp, void __user *child_threadptr,
  564. int __user *child_tidp, int p6,
  565. struct pt_regs *regs)
  566. {
  567. CHECK_FULL_REGS(regs);
  568. if (usp == 0)
  569. usp = regs->gpr[1]; /* stack pointer for child */
  570. return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
  571. }
  572. int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
  573. unsigned long p4, unsigned long p5, unsigned long p6,
  574. struct pt_regs *regs)
  575. {
  576. CHECK_FULL_REGS(regs);
  577. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  578. }
  579. int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
  580. unsigned long p4, unsigned long p5, unsigned long p6,
  581. struct pt_regs *regs)
  582. {
  583. CHECK_FULL_REGS(regs);
  584. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
  585. regs, 0, NULL, NULL);
  586. }
  587. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  588. unsigned long a3, unsigned long a4, unsigned long a5,
  589. struct pt_regs *regs)
  590. {
  591. int error;
  592. char * filename;
  593. filename = getname((char __user *) a0);
  594. error = PTR_ERR(filename);
  595. if (IS_ERR(filename))
  596. goto out;
  597. preempt_disable();
  598. if (regs->msr & MSR_FP)
  599. giveup_fpu(current);
  600. #ifdef CONFIG_ALTIVEC
  601. if (regs->msr & MSR_VEC)
  602. giveup_altivec(current);
  603. #endif /* CONFIG_ALTIVEC */
  604. #ifdef CONFIG_SPE
  605. if (regs->msr & MSR_SPE)
  606. giveup_spe(current);
  607. #endif /* CONFIG_SPE */
  608. preempt_enable();
  609. error = do_execve(filename, (char __user *__user *) a1,
  610. (char __user *__user *) a2, regs);
  611. if (error == 0) {
  612. task_lock(current);
  613. current->ptrace &= ~PT_DTRACE;
  614. task_unlock(current);
  615. }
  616. putname(filename);
  617. out:
  618. return error;
  619. }
  620. void dump_stack(void)
  621. {
  622. show_stack(current, NULL);
  623. }
  624. EXPORT_SYMBOL(dump_stack);
  625. void show_stack(struct task_struct *tsk, unsigned long *stack)
  626. {
  627. unsigned long sp, stack_top, prev_sp, ret;
  628. int count = 0;
  629. unsigned long next_exc = 0;
  630. struct pt_regs *regs;
  631. extern char ret_from_except, ret_from_except_full, ret_from_syscall;
  632. sp = (unsigned long) stack;
  633. if (tsk == NULL)
  634. tsk = current;
  635. if (sp == 0) {
  636. if (tsk == current)
  637. asm("mr %0,1" : "=r" (sp));
  638. else
  639. sp = tsk->thread.ksp;
  640. }
  641. prev_sp = (unsigned long) (tsk->thread_info + 1);
  642. stack_top = (unsigned long) tsk->thread_info + THREAD_SIZE;
  643. while (count < 16 && sp > prev_sp && sp < stack_top && (sp & 3) == 0) {
  644. if (count == 0) {
  645. printk("Call trace:");
  646. #ifdef CONFIG_KALLSYMS
  647. printk("\n");
  648. #endif
  649. } else {
  650. if (next_exc) {
  651. ret = next_exc;
  652. next_exc = 0;
  653. } else
  654. ret = *(unsigned long *)(sp + 4);
  655. printk(" [%08lx] ", ret);
  656. #ifdef CONFIG_KALLSYMS
  657. print_symbol("%s", ret);
  658. printk("\n");
  659. #endif
  660. if (ret == (unsigned long) &ret_from_except
  661. || ret == (unsigned long) &ret_from_except_full
  662. || ret == (unsigned long) &ret_from_syscall) {
  663. /* sp + 16 points to an exception frame */
  664. regs = (struct pt_regs *) (sp + 16);
  665. if (sp + 16 + sizeof(*regs) <= stack_top)
  666. next_exc = regs->nip;
  667. }
  668. }
  669. ++count;
  670. sp = *(unsigned long *)sp;
  671. }
  672. #ifndef CONFIG_KALLSYMS
  673. if (count > 0)
  674. printk("\n");
  675. #endif
  676. }
  677. #if 0
  678. /*
  679. * Low level print for debugging - Cort
  680. */
  681. int __init ll_printk(const char *fmt, ...)
  682. {
  683. va_list args;
  684. char buf[256];
  685. int i;
  686. va_start(args, fmt);
  687. i=vsprintf(buf,fmt,args);
  688. ll_puts(buf);
  689. va_end(args);
  690. return i;
  691. }
  692. int lines = 24, cols = 80;
  693. int orig_x = 0, orig_y = 0;
  694. void puthex(unsigned long val)
  695. {
  696. unsigned char buf[10];
  697. int i;
  698. for (i = 7; i >= 0; i--)
  699. {
  700. buf[i] = "0123456789ABCDEF"[val & 0x0F];
  701. val >>= 4;
  702. }
  703. buf[8] = '\0';
  704. prom_print(buf);
  705. }
  706. void __init ll_puts(const char *s)
  707. {
  708. int x,y;
  709. char *vidmem = (char *)/*(_ISA_MEM_BASE + 0xB8000) */0xD00B8000;
  710. char c;
  711. extern int mem_init_done;
  712. if ( mem_init_done ) /* assume this means we can printk */
  713. {
  714. printk(s);
  715. return;
  716. }
  717. #if 0
  718. if ( have_of )
  719. {
  720. prom_print(s);
  721. return;
  722. }
  723. #endif
  724. /*
  725. * can't ll_puts on chrp without openfirmware yet.
  726. * vidmem just needs to be setup for it.
  727. * -- Cort
  728. */
  729. if ( _machine != _MACH_prep )
  730. return;
  731. x = orig_x;
  732. y = orig_y;
  733. while ( ( c = *s++ ) != '\0' ) {
  734. if ( c == '\n' ) {
  735. x = 0;
  736. if ( ++y >= lines ) {
  737. /*scroll();*/
  738. /*y--;*/
  739. y = 0;
  740. }
  741. } else {
  742. vidmem [ ( x + cols * y ) * 2 ] = c;
  743. if ( ++x >= cols ) {
  744. x = 0;
  745. if ( ++y >= lines ) {
  746. /*scroll();*/
  747. /*y--;*/
  748. y = 0;
  749. }
  750. }
  751. }
  752. }
  753. orig_x = x;
  754. orig_y = y;
  755. }
  756. #endif
  757. unsigned long get_wchan(struct task_struct *p)
  758. {
  759. unsigned long ip, sp;
  760. unsigned long stack_page = (unsigned long) p->thread_info;
  761. int count = 0;
  762. if (!p || p == current || p->state == TASK_RUNNING)
  763. return 0;
  764. sp = p->thread.ksp;
  765. do {
  766. sp = *(unsigned long *)sp;
  767. if (sp < stack_page || sp >= stack_page + 8188)
  768. return 0;
  769. if (count > 0) {
  770. ip = *(unsigned long *)(sp + 4);
  771. if (!in_sched_functions(ip))
  772. return ip;
  773. }
  774. } while (count++ < 16);
  775. return 0;
  776. }