process.c 22 KB

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