process.c 24 KB

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