process.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /*
  2. * Derived from "arch/i386/kernel/process.c"
  3. * Copyright (C) 1995 Linus Torvalds
  4. *
  5. * Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
  6. * Paul Mackerras (paulus@cs.anu.edu.au)
  7. *
  8. * PowerPC version
  9. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/errno.h>
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/smp.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/stddef.h>
  23. #include <linux/unistd.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/slab.h>
  26. #include <linux/user.h>
  27. #include <linux/elf.h>
  28. #include <linux/init.h>
  29. #include <linux/prctl.h>
  30. #include <linux/init_task.h>
  31. #include <linux/module.h>
  32. #include <linux/kallsyms.h>
  33. #include <linux/mqueue.h>
  34. #include <linux/hardirq.h>
  35. #include <linux/utsname.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/system.h>
  39. #include <asm/io.h>
  40. #include <asm/processor.h>
  41. #include <asm/mmu.h>
  42. #include <asm/prom.h>
  43. #include <asm/machdep.h>
  44. #include <asm/time.h>
  45. #include <asm/syscalls.h>
  46. #ifdef CONFIG_PPC64
  47. #include <asm/firmware.h>
  48. #endif
  49. extern unsigned long _get_SP(void);
  50. #ifndef CONFIG_SMP
  51. struct task_struct *last_task_used_math = NULL;
  52. struct task_struct *last_task_used_altivec = NULL;
  53. struct task_struct *last_task_used_spe = NULL;
  54. #endif
  55. /*
  56. * Make sure the floating-point register state in the
  57. * the thread_struct is up to date for task tsk.
  58. */
  59. void flush_fp_to_thread(struct task_struct *tsk)
  60. {
  61. if (tsk->thread.regs) {
  62. /*
  63. * We need to disable preemption here because if we didn't,
  64. * another process could get scheduled after the regs->msr
  65. * test but before we have finished saving the FP registers
  66. * to the thread_struct. That process could take over the
  67. * FPU, and then when we get scheduled again we would store
  68. * bogus values for the remaining FP registers.
  69. */
  70. preempt_disable();
  71. if (tsk->thread.regs->msr & MSR_FP) {
  72. #ifdef CONFIG_SMP
  73. /*
  74. * This should only ever be called for current or
  75. * for a stopped child process. Since we save away
  76. * the FP register state on context switch on SMP,
  77. * there is something wrong if a stopped child appears
  78. * to still have its FP state in the CPU registers.
  79. */
  80. BUG_ON(tsk != current);
  81. #endif
  82. giveup_fpu(current);
  83. }
  84. preempt_enable();
  85. }
  86. }
  87. void enable_kernel_fp(void)
  88. {
  89. WARN_ON(preemptible());
  90. #ifdef CONFIG_SMP
  91. if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
  92. giveup_fpu(current);
  93. else
  94. giveup_fpu(NULL); /* just enables FP for kernel */
  95. #else
  96. giveup_fpu(last_task_used_math);
  97. #endif /* CONFIG_SMP */
  98. }
  99. EXPORT_SYMBOL(enable_kernel_fp);
  100. int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
  101. {
  102. if (!tsk->thread.regs)
  103. return 0;
  104. flush_fp_to_thread(current);
  105. memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
  106. return 1;
  107. }
  108. #ifdef CONFIG_ALTIVEC
  109. void enable_kernel_altivec(void)
  110. {
  111. WARN_ON(preemptible());
  112. #ifdef CONFIG_SMP
  113. if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
  114. giveup_altivec(current);
  115. else
  116. giveup_altivec(NULL); /* just enable AltiVec for kernel - force */
  117. #else
  118. giveup_altivec(last_task_used_altivec);
  119. #endif /* CONFIG_SMP */
  120. }
  121. EXPORT_SYMBOL(enable_kernel_altivec);
  122. /*
  123. * Make sure the VMX/Altivec register state in the
  124. * the thread_struct is up to date for task tsk.
  125. */
  126. void flush_altivec_to_thread(struct task_struct *tsk)
  127. {
  128. if (tsk->thread.regs) {
  129. preempt_disable();
  130. if (tsk->thread.regs->msr & MSR_VEC) {
  131. #ifdef CONFIG_SMP
  132. BUG_ON(tsk != current);
  133. #endif
  134. giveup_altivec(current);
  135. }
  136. preempt_enable();
  137. }
  138. }
  139. int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
  140. {
  141. flush_altivec_to_thread(current);
  142. memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs));
  143. return 1;
  144. }
  145. #endif /* CONFIG_ALTIVEC */
  146. #ifdef CONFIG_SPE
  147. void enable_kernel_spe(void)
  148. {
  149. WARN_ON(preemptible());
  150. #ifdef CONFIG_SMP
  151. if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
  152. giveup_spe(current);
  153. else
  154. giveup_spe(NULL); /* just enable SPE for kernel - force */
  155. #else
  156. giveup_spe(last_task_used_spe);
  157. #endif /* __SMP __ */
  158. }
  159. EXPORT_SYMBOL(enable_kernel_spe);
  160. void flush_spe_to_thread(struct task_struct *tsk)
  161. {
  162. if (tsk->thread.regs) {
  163. preempt_disable();
  164. if (tsk->thread.regs->msr & MSR_SPE) {
  165. #ifdef CONFIG_SMP
  166. BUG_ON(tsk != current);
  167. #endif
  168. giveup_spe(current);
  169. }
  170. preempt_enable();
  171. }
  172. }
  173. int dump_spe(struct pt_regs *regs, elf_vrregset_t *evrregs)
  174. {
  175. flush_spe_to_thread(current);
  176. /* We copy u32 evr[32] + u64 acc + u32 spefscr -> 35 */
  177. memcpy(evrregs, &current->thread.evr[0], sizeof(u32) * 35);
  178. return 1;
  179. }
  180. #endif /* CONFIG_SPE */
  181. #ifndef CONFIG_SMP
  182. /*
  183. * If we are doing lazy switching of CPU state (FP, altivec or SPE),
  184. * and the current task has some state, discard it.
  185. */
  186. void discard_lazy_cpu_state(void)
  187. {
  188. preempt_disable();
  189. if (last_task_used_math == current)
  190. last_task_used_math = NULL;
  191. #ifdef CONFIG_ALTIVEC
  192. if (last_task_used_altivec == current)
  193. last_task_used_altivec = NULL;
  194. #endif /* CONFIG_ALTIVEC */
  195. #ifdef CONFIG_SPE
  196. if (last_task_used_spe == current)
  197. last_task_used_spe = NULL;
  198. #endif
  199. preempt_enable();
  200. }
  201. #endif /* CONFIG_SMP */
  202. #ifdef CONFIG_PPC_MERGE /* XXX for now */
  203. int set_dabr(unsigned long dabr)
  204. {
  205. if (ppc_md.set_dabr)
  206. return ppc_md.set_dabr(dabr);
  207. mtspr(SPRN_DABR, dabr);
  208. return 0;
  209. }
  210. #endif
  211. #ifdef CONFIG_PPC64
  212. DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
  213. static DEFINE_PER_CPU(unsigned long, current_dabr);
  214. #endif
  215. struct task_struct *__switch_to(struct task_struct *prev,
  216. struct task_struct *new)
  217. {
  218. struct thread_struct *new_thread, *old_thread;
  219. unsigned long flags;
  220. struct task_struct *last;
  221. #ifdef CONFIG_SMP
  222. /* avoid complexity of lazy save/restore of fpu
  223. * by just saving it every time we switch out if
  224. * this task used the fpu during the last quantum.
  225. *
  226. * If it tries to use the fpu again, it'll trap and
  227. * reload its fp regs. So we don't have to do a restore
  228. * every switch, just a save.
  229. * -- Cort
  230. */
  231. if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
  232. giveup_fpu(prev);
  233. #ifdef CONFIG_ALTIVEC
  234. /*
  235. * If the previous thread used altivec in the last quantum
  236. * (thus changing altivec regs) then save them.
  237. * We used to check the VRSAVE register but not all apps
  238. * set it, so we don't rely on it now (and in fact we need
  239. * to save & restore VSCR even if VRSAVE == 0). -- paulus
  240. *
  241. * On SMP we always save/restore altivec regs just to avoid the
  242. * complexity of changing processors.
  243. * -- Cort
  244. */
  245. if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
  246. giveup_altivec(prev);
  247. #endif /* CONFIG_ALTIVEC */
  248. #ifdef CONFIG_SPE
  249. /*
  250. * If the previous thread used spe in the last quantum
  251. * (thus changing spe regs) then save them.
  252. *
  253. * On SMP we always save/restore spe regs just to avoid the
  254. * complexity of changing processors.
  255. */
  256. if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
  257. giveup_spe(prev);
  258. #endif /* CONFIG_SPE */
  259. #else /* CONFIG_SMP */
  260. #ifdef CONFIG_ALTIVEC
  261. /* Avoid the trap. On smp this this never happens since
  262. * we don't set last_task_used_altivec -- Cort
  263. */
  264. if (new->thread.regs && last_task_used_altivec == new)
  265. new->thread.regs->msr |= MSR_VEC;
  266. #endif /* CONFIG_ALTIVEC */
  267. #ifdef CONFIG_SPE
  268. /* Avoid the trap. On smp this this never happens since
  269. * we don't set last_task_used_spe
  270. */
  271. if (new->thread.regs && last_task_used_spe == new)
  272. new->thread.regs->msr |= MSR_SPE;
  273. #endif /* CONFIG_SPE */
  274. #endif /* CONFIG_SMP */
  275. #ifdef CONFIG_PPC64 /* for now */
  276. if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr)) {
  277. set_dabr(new->thread.dabr);
  278. __get_cpu_var(current_dabr) = new->thread.dabr;
  279. }
  280. flush_tlb_pending();
  281. #endif
  282. new_thread = &new->thread;
  283. old_thread = &current->thread;
  284. #ifdef CONFIG_PPC64
  285. /*
  286. * Collect processor utilization data per process
  287. */
  288. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  289. struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
  290. long unsigned start_tb, current_tb;
  291. start_tb = old_thread->start_tb;
  292. cu->current_tb = current_tb = mfspr(SPRN_PURR);
  293. old_thread->accum_tb += (current_tb - start_tb);
  294. new_thread->start_tb = current_tb;
  295. }
  296. #endif
  297. local_irq_save(flags);
  298. account_system_vtime(current);
  299. account_process_vtime(current);
  300. calculate_steal_time();
  301. last = _switch(old_thread, new_thread);
  302. local_irq_restore(flags);
  303. return last;
  304. }
  305. static int instructions_to_print = 16;
  306. static void show_instructions(struct pt_regs *regs)
  307. {
  308. int i;
  309. unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
  310. sizeof(int));
  311. printk("Instruction dump:");
  312. for (i = 0; i < instructions_to_print; i++) {
  313. int instr;
  314. if (!(i % 8))
  315. printk("\n");
  316. /* We use __get_user here *only* to avoid an OOPS on a
  317. * bad address because the pc *should* only be a
  318. * kernel address.
  319. */
  320. if (!__kernel_text_address(pc) ||
  321. __get_user(instr, (unsigned int __user *)pc)) {
  322. printk("XXXXXXXX ");
  323. } else {
  324. if (regs->nip == pc)
  325. printk("<%08x> ", instr);
  326. else
  327. printk("%08x ", instr);
  328. }
  329. pc += sizeof(int);
  330. }
  331. printk("\n");
  332. }
  333. static struct regbit {
  334. unsigned long bit;
  335. const char *name;
  336. } msr_bits[] = {
  337. {MSR_EE, "EE"},
  338. {MSR_PR, "PR"},
  339. {MSR_FP, "FP"},
  340. {MSR_ME, "ME"},
  341. {MSR_IR, "IR"},
  342. {MSR_DR, "DR"},
  343. {0, NULL}
  344. };
  345. static void printbits(unsigned long val, struct regbit *bits)
  346. {
  347. const char *sep = "";
  348. printk("<");
  349. for (; bits->bit; ++bits)
  350. if (val & bits->bit) {
  351. printk("%s%s", sep, bits->name);
  352. sep = ",";
  353. }
  354. printk(">");
  355. }
  356. #ifdef CONFIG_PPC64
  357. #define REG "%016lX"
  358. #define REGS_PER_LINE 4
  359. #define LAST_VOLATILE 13
  360. #else
  361. #define REG "%08lX"
  362. #define REGS_PER_LINE 8
  363. #define LAST_VOLATILE 12
  364. #endif
  365. void show_regs(struct pt_regs * regs)
  366. {
  367. int i, trap;
  368. printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
  369. regs->nip, regs->link, regs->ctr);
  370. printk("REGS: %p TRAP: %04lx %s (%s)\n",
  371. regs, regs->trap, print_tainted(), init_utsname()->release);
  372. printk("MSR: "REG" ", regs->msr);
  373. printbits(regs->msr, msr_bits);
  374. printk(" CR: %08lX XER: %08lX\n", regs->ccr, regs->xer);
  375. trap = TRAP(regs);
  376. if (trap == 0x300 || trap == 0x600)
  377. printk("DAR: "REG", DSISR: "REG"\n", regs->dar, regs->dsisr);
  378. printk("TASK = %p[%d] '%s' THREAD: %p",
  379. current, current->pid, current->comm, task_thread_info(current));
  380. #ifdef CONFIG_SMP
  381. printk(" CPU: %d", smp_processor_id());
  382. #endif /* CONFIG_SMP */
  383. for (i = 0; i < 32; i++) {
  384. if ((i % REGS_PER_LINE) == 0)
  385. printk("\n" KERN_INFO "GPR%02d: ", i);
  386. printk(REG " ", regs->gpr[i]);
  387. if (i == LAST_VOLATILE && !FULL_REGS(regs))
  388. break;
  389. }
  390. printk("\n");
  391. #ifdef CONFIG_KALLSYMS
  392. /*
  393. * Lookup NIP late so we have the best change of getting the
  394. * above info out without failing
  395. */
  396. printk("NIP ["REG"] ", regs->nip);
  397. print_symbol("%s\n", regs->nip);
  398. printk("LR ["REG"] ", regs->link);
  399. print_symbol("%s\n", regs->link);
  400. #endif
  401. show_stack(current, (unsigned long *) regs->gpr[1]);
  402. if (!user_mode(regs))
  403. show_instructions(regs);
  404. }
  405. void exit_thread(void)
  406. {
  407. discard_lazy_cpu_state();
  408. }
  409. void flush_thread(void)
  410. {
  411. #ifdef CONFIG_PPC64
  412. struct thread_info *t = current_thread_info();
  413. if (t->flags & _TIF_ABI_PENDING)
  414. t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
  415. #endif
  416. discard_lazy_cpu_state();
  417. #ifdef CONFIG_PPC64 /* for now */
  418. if (current->thread.dabr) {
  419. current->thread.dabr = 0;
  420. set_dabr(0);
  421. }
  422. #endif
  423. }
  424. void
  425. release_thread(struct task_struct *t)
  426. {
  427. }
  428. /*
  429. * This gets called before we allocate a new thread and copy
  430. * the current task into it.
  431. */
  432. void prepare_to_copy(struct task_struct *tsk)
  433. {
  434. flush_fp_to_thread(current);
  435. flush_altivec_to_thread(current);
  436. flush_spe_to_thread(current);
  437. }
  438. /*
  439. * Copy a thread..
  440. */
  441. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  442. unsigned long unused, struct task_struct *p,
  443. struct pt_regs *regs)
  444. {
  445. struct pt_regs *childregs, *kregs;
  446. extern void ret_from_fork(void);
  447. unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  448. CHECK_FULL_REGS(regs);
  449. /* Copy registers */
  450. sp -= sizeof(struct pt_regs);
  451. childregs = (struct pt_regs *) sp;
  452. *childregs = *regs;
  453. if ((childregs->msr & MSR_PR) == 0) {
  454. /* for kernel thread, set `current' and stackptr in new task */
  455. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  456. #ifdef CONFIG_PPC32
  457. childregs->gpr[2] = (unsigned long) p;
  458. #else
  459. clear_tsk_thread_flag(p, TIF_32BIT);
  460. #endif
  461. p->thread.regs = NULL; /* no user register state */
  462. } else {
  463. childregs->gpr[1] = usp;
  464. p->thread.regs = childregs;
  465. if (clone_flags & CLONE_SETTLS) {
  466. #ifdef CONFIG_PPC64
  467. if (!test_thread_flag(TIF_32BIT))
  468. childregs->gpr[13] = childregs->gpr[6];
  469. else
  470. #endif
  471. childregs->gpr[2] = childregs->gpr[6];
  472. }
  473. }
  474. childregs->gpr[3] = 0; /* Result from fork() */
  475. sp -= STACK_FRAME_OVERHEAD;
  476. /*
  477. * The way this works is that at some point in the future
  478. * some task will call _switch to switch to the new task.
  479. * That will pop off the stack frame created below and start
  480. * the new task running at ret_from_fork. The new task will
  481. * do some house keeping and then return from the fork or clone
  482. * system call, using the stack frame created above.
  483. */
  484. sp -= sizeof(struct pt_regs);
  485. kregs = (struct pt_regs *) sp;
  486. sp -= STACK_FRAME_OVERHEAD;
  487. p->thread.ksp = sp;
  488. #ifdef CONFIG_PPC64
  489. if (cpu_has_feature(CPU_FTR_SLB)) {
  490. unsigned long sp_vsid = get_kernel_vsid(sp);
  491. unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
  492. sp_vsid <<= SLB_VSID_SHIFT;
  493. sp_vsid |= SLB_VSID_KERNEL | llp;
  494. p->thread.ksp_vsid = sp_vsid;
  495. }
  496. /*
  497. * The PPC64 ABI makes use of a TOC to contain function
  498. * pointers. The function (ret_from_except) is actually a pointer
  499. * to the TOC entry. The first entry is a pointer to the actual
  500. * function.
  501. */
  502. kregs->nip = *((unsigned long *)ret_from_fork);
  503. #else
  504. kregs->nip = (unsigned long)ret_from_fork;
  505. #endif
  506. return 0;
  507. }
  508. /*
  509. * Set up a thread for executing a new program
  510. */
  511. void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
  512. {
  513. #ifdef CONFIG_PPC64
  514. unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
  515. #endif
  516. set_fs(USER_DS);
  517. /*
  518. * If we exec out of a kernel thread then thread.regs will not be
  519. * set. Do it now.
  520. */
  521. if (!current->thread.regs) {
  522. struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
  523. current->thread.regs = regs - 1;
  524. }
  525. memset(regs->gpr, 0, sizeof(regs->gpr));
  526. regs->ctr = 0;
  527. regs->link = 0;
  528. regs->xer = 0;
  529. regs->ccr = 0;
  530. regs->gpr[1] = sp;
  531. #ifdef CONFIG_PPC32
  532. regs->mq = 0;
  533. regs->nip = start;
  534. regs->msr = MSR_USER;
  535. #else
  536. if (!test_thread_flag(TIF_32BIT)) {
  537. unsigned long entry, toc;
  538. /* start is a relocated pointer to the function descriptor for
  539. * the elf _start routine. The first entry in the function
  540. * descriptor is the entry address of _start and the second
  541. * entry is the TOC value we need to use.
  542. */
  543. __get_user(entry, (unsigned long __user *)start);
  544. __get_user(toc, (unsigned long __user *)start+1);
  545. /* Check whether the e_entry function descriptor entries
  546. * need to be relocated before we can use them.
  547. */
  548. if (load_addr != 0) {
  549. entry += load_addr;
  550. toc += load_addr;
  551. }
  552. regs->nip = entry;
  553. regs->gpr[2] = toc;
  554. regs->msr = MSR_USER64;
  555. } else {
  556. regs->nip = start;
  557. regs->gpr[2] = 0;
  558. regs->msr = MSR_USER32;
  559. }
  560. #endif
  561. discard_lazy_cpu_state();
  562. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  563. current->thread.fpscr.val = 0;
  564. #ifdef CONFIG_ALTIVEC
  565. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  566. memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
  567. current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
  568. current->thread.vrsave = 0;
  569. current->thread.used_vr = 0;
  570. #endif /* CONFIG_ALTIVEC */
  571. #ifdef CONFIG_SPE
  572. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  573. current->thread.acc = 0;
  574. current->thread.spefscr = 0;
  575. current->thread.used_spe = 0;
  576. #endif /* CONFIG_SPE */
  577. }
  578. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  579. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  580. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  581. {
  582. struct pt_regs *regs = tsk->thread.regs;
  583. /* This is a bit hairy. If we are an SPE enabled processor
  584. * (have embedded fp) we store the IEEE exception enable flags in
  585. * fpexc_mode. fpexc_mode is also used for setting FP exception
  586. * mode (asyn, precise, disabled) for 'Classic' FP. */
  587. if (val & PR_FP_EXC_SW_ENABLE) {
  588. #ifdef CONFIG_SPE
  589. tsk->thread.fpexc_mode = val &
  590. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  591. return 0;
  592. #else
  593. return -EINVAL;
  594. #endif
  595. }
  596. /* on a CONFIG_SPE this does not hurt us. The bits that
  597. * __pack_fe01 use do not overlap with bits used for
  598. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  599. * on CONFIG_SPE implementations are reserved so writing to
  600. * them does not change anything */
  601. if (val > PR_FP_EXC_PRECISE)
  602. return -EINVAL;
  603. tsk->thread.fpexc_mode = __pack_fe01(val);
  604. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  605. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  606. | tsk->thread.fpexc_mode;
  607. return 0;
  608. }
  609. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  610. {
  611. unsigned int val;
  612. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  613. #ifdef CONFIG_SPE
  614. val = tsk->thread.fpexc_mode;
  615. #else
  616. return -EINVAL;
  617. #endif
  618. else
  619. val = __unpack_fe01(tsk->thread.fpexc_mode);
  620. return put_user(val, (unsigned int __user *) adr);
  621. }
  622. int set_endian(struct task_struct *tsk, unsigned int val)
  623. {
  624. struct pt_regs *regs = tsk->thread.regs;
  625. if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
  626. (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
  627. return -EINVAL;
  628. if (regs == NULL)
  629. return -EINVAL;
  630. if (val == PR_ENDIAN_BIG)
  631. regs->msr &= ~MSR_LE;
  632. else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
  633. regs->msr |= MSR_LE;
  634. else
  635. return -EINVAL;
  636. return 0;
  637. }
  638. int get_endian(struct task_struct *tsk, unsigned long adr)
  639. {
  640. struct pt_regs *regs = tsk->thread.regs;
  641. unsigned int val;
  642. if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
  643. !cpu_has_feature(CPU_FTR_REAL_LE))
  644. return -EINVAL;
  645. if (regs == NULL)
  646. return -EINVAL;
  647. if (regs->msr & MSR_LE) {
  648. if (cpu_has_feature(CPU_FTR_REAL_LE))
  649. val = PR_ENDIAN_LITTLE;
  650. else
  651. val = PR_ENDIAN_PPC_LITTLE;
  652. } else
  653. val = PR_ENDIAN_BIG;
  654. return put_user(val, (unsigned int __user *)adr);
  655. }
  656. int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
  657. {
  658. tsk->thread.align_ctl = val;
  659. return 0;
  660. }
  661. int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  662. {
  663. return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
  664. }
  665. #define TRUNC_PTR(x) ((typeof(x))(((unsigned long)(x)) & 0xffffffff))
  666. int sys_clone(unsigned long clone_flags, unsigned long usp,
  667. int __user *parent_tidp, void __user *child_threadptr,
  668. int __user *child_tidp, int p6,
  669. struct pt_regs *regs)
  670. {
  671. CHECK_FULL_REGS(regs);
  672. if (usp == 0)
  673. usp = regs->gpr[1]; /* stack pointer for child */
  674. #ifdef CONFIG_PPC64
  675. if (test_thread_flag(TIF_32BIT)) {
  676. parent_tidp = TRUNC_PTR(parent_tidp);
  677. child_tidp = TRUNC_PTR(child_tidp);
  678. }
  679. #endif
  680. return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
  681. }
  682. int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
  683. unsigned long p4, unsigned long p5, unsigned long p6,
  684. struct pt_regs *regs)
  685. {
  686. CHECK_FULL_REGS(regs);
  687. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  688. }
  689. int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
  690. unsigned long p4, unsigned long p5, unsigned long p6,
  691. struct pt_regs *regs)
  692. {
  693. CHECK_FULL_REGS(regs);
  694. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
  695. regs, 0, NULL, NULL);
  696. }
  697. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  698. unsigned long a3, unsigned long a4, unsigned long a5,
  699. struct pt_regs *regs)
  700. {
  701. int error;
  702. char *filename;
  703. filename = getname((char __user *) a0);
  704. error = PTR_ERR(filename);
  705. if (IS_ERR(filename))
  706. goto out;
  707. flush_fp_to_thread(current);
  708. flush_altivec_to_thread(current);
  709. flush_spe_to_thread(current);
  710. error = do_execve(filename, (char __user * __user *) a1,
  711. (char __user * __user *) a2, regs);
  712. if (error == 0) {
  713. task_lock(current);
  714. current->ptrace &= ~PT_DTRACE;
  715. task_unlock(current);
  716. }
  717. putname(filename);
  718. out:
  719. return error;
  720. }
  721. #ifdef CONFIG_IRQSTACKS
  722. static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
  723. unsigned long nbytes)
  724. {
  725. unsigned long stack_page;
  726. unsigned long cpu = task_cpu(p);
  727. /*
  728. * Avoid crashing if the stack has overflowed and corrupted
  729. * task_cpu(p), which is in the thread_info struct.
  730. */
  731. if (cpu < NR_CPUS && cpu_possible(cpu)) {
  732. stack_page = (unsigned long) hardirq_ctx[cpu];
  733. if (sp >= stack_page + sizeof(struct thread_struct)
  734. && sp <= stack_page + THREAD_SIZE - nbytes)
  735. return 1;
  736. stack_page = (unsigned long) softirq_ctx[cpu];
  737. if (sp >= stack_page + sizeof(struct thread_struct)
  738. && sp <= stack_page + THREAD_SIZE - nbytes)
  739. return 1;
  740. }
  741. return 0;
  742. }
  743. #else
  744. #define valid_irq_stack(sp, p, nb) 0
  745. #endif /* CONFIG_IRQSTACKS */
  746. int validate_sp(unsigned long sp, struct task_struct *p,
  747. unsigned long nbytes)
  748. {
  749. unsigned long stack_page = (unsigned long)task_stack_page(p);
  750. if (sp >= stack_page + sizeof(struct thread_struct)
  751. && sp <= stack_page + THREAD_SIZE - nbytes)
  752. return 1;
  753. return valid_irq_stack(sp, p, nbytes);
  754. }
  755. #ifdef CONFIG_PPC64
  756. #define MIN_STACK_FRAME 112 /* same as STACK_FRAME_OVERHEAD, in fact */
  757. #define FRAME_LR_SAVE 2
  758. #define INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD + 288)
  759. #define REGS_MARKER 0x7265677368657265ul
  760. #define FRAME_MARKER 12
  761. #else
  762. #define MIN_STACK_FRAME 16
  763. #define FRAME_LR_SAVE 1
  764. #define INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD)
  765. #define REGS_MARKER 0x72656773ul
  766. #define FRAME_MARKER 2
  767. #endif
  768. EXPORT_SYMBOL(validate_sp);
  769. unsigned long get_wchan(struct task_struct *p)
  770. {
  771. unsigned long ip, sp;
  772. int count = 0;
  773. if (!p || p == current || p->state == TASK_RUNNING)
  774. return 0;
  775. sp = p->thread.ksp;
  776. if (!validate_sp(sp, p, MIN_STACK_FRAME))
  777. return 0;
  778. do {
  779. sp = *(unsigned long *)sp;
  780. if (!validate_sp(sp, p, MIN_STACK_FRAME))
  781. return 0;
  782. if (count > 0) {
  783. ip = ((unsigned long *)sp)[FRAME_LR_SAVE];
  784. if (!in_sched_functions(ip))
  785. return ip;
  786. }
  787. } while (count++ < 16);
  788. return 0;
  789. }
  790. static int kstack_depth_to_print = 64;
  791. void show_stack(struct task_struct *tsk, unsigned long *stack)
  792. {
  793. unsigned long sp, ip, lr, newsp;
  794. int count = 0;
  795. int firstframe = 1;
  796. sp = (unsigned long) stack;
  797. if (tsk == NULL)
  798. tsk = current;
  799. if (sp == 0) {
  800. if (tsk == current)
  801. asm("mr %0,1" : "=r" (sp));
  802. else
  803. sp = tsk->thread.ksp;
  804. }
  805. lr = 0;
  806. printk("Call Trace:\n");
  807. do {
  808. if (!validate_sp(sp, tsk, MIN_STACK_FRAME))
  809. return;
  810. stack = (unsigned long *) sp;
  811. newsp = stack[0];
  812. ip = stack[FRAME_LR_SAVE];
  813. if (!firstframe || ip != lr) {
  814. printk("["REG"] ["REG"] ", sp, ip);
  815. print_symbol("%s", ip);
  816. if (firstframe)
  817. printk(" (unreliable)");
  818. printk("\n");
  819. }
  820. firstframe = 0;
  821. /*
  822. * See if this is an exception frame.
  823. * We look for the "regshere" marker in the current frame.
  824. */
  825. if (validate_sp(sp, tsk, INT_FRAME_SIZE)
  826. && stack[FRAME_MARKER] == REGS_MARKER) {
  827. struct pt_regs *regs = (struct pt_regs *)
  828. (sp + STACK_FRAME_OVERHEAD);
  829. printk("--- Exception: %lx", regs->trap);
  830. print_symbol(" at %s\n", regs->nip);
  831. lr = regs->link;
  832. print_symbol(" LR = %s\n", lr);
  833. firstframe = 1;
  834. }
  835. sp = newsp;
  836. } while (count++ < kstack_depth_to_print);
  837. }
  838. void dump_stack(void)
  839. {
  840. show_stack(current, NULL);
  841. }
  842. EXPORT_SYMBOL(dump_stack);
  843. #ifdef CONFIG_PPC64
  844. void ppc64_runlatch_on(void)
  845. {
  846. unsigned long ctrl;
  847. if (cpu_has_feature(CPU_FTR_CTRL) && !test_thread_flag(TIF_RUNLATCH)) {
  848. HMT_medium();
  849. ctrl = mfspr(SPRN_CTRLF);
  850. ctrl |= CTRL_RUNLATCH;
  851. mtspr(SPRN_CTRLT, ctrl);
  852. set_thread_flag(TIF_RUNLATCH);
  853. }
  854. }
  855. void ppc64_runlatch_off(void)
  856. {
  857. unsigned long ctrl;
  858. if (cpu_has_feature(CPU_FTR_CTRL) && test_thread_flag(TIF_RUNLATCH)) {
  859. HMT_medium();
  860. clear_thread_flag(TIF_RUNLATCH);
  861. ctrl = mfspr(SPRN_CTRLF);
  862. ctrl &= ~CTRL_RUNLATCH;
  863. mtspr(SPRN_CTRLT, ctrl);
  864. }
  865. }
  866. #endif