xref: /openbmc/linux/arch/powerpc/kernel/process.c (revision 5bd8e16d)
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 
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/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/export.h>
32 #include <linux/kallsyms.h>
33 #include <linux/mqueue.h>
34 #include <linux/hardirq.h>
35 #include <linux/utsname.h>
36 #include <linux/ftrace.h>
37 #include <linux/kernel_stat.h>
38 #include <linux/personality.h>
39 #include <linux/random.h>
40 #include <linux/hw_breakpoint.h>
41 
42 #include <asm/pgtable.h>
43 #include <asm/uaccess.h>
44 #include <asm/io.h>
45 #include <asm/processor.h>
46 #include <asm/mmu.h>
47 #include <asm/prom.h>
48 #include <asm/machdep.h>
49 #include <asm/time.h>
50 #include <asm/runlatch.h>
51 #include <asm/syscalls.h>
52 #include <asm/switch_to.h>
53 #include <asm/tm.h>
54 #include <asm/debug.h>
55 #ifdef CONFIG_PPC64
56 #include <asm/firmware.h>
57 #endif
58 #include <linux/kprobes.h>
59 #include <linux/kdebug.h>
60 
61 /* Transactional Memory debug */
62 #ifdef TM_DEBUG_SW
63 #define TM_DEBUG(x...) printk(KERN_INFO x)
64 #else
65 #define TM_DEBUG(x...) do { } while(0)
66 #endif
67 
68 extern unsigned long _get_SP(void);
69 
70 #ifndef CONFIG_SMP
71 struct task_struct *last_task_used_math = NULL;
72 struct task_struct *last_task_used_altivec = NULL;
73 struct task_struct *last_task_used_vsx = NULL;
74 struct task_struct *last_task_used_spe = NULL;
75 #endif
76 
77 #ifdef CONFIG_PPC_FPU
78 /*
79  * Make sure the floating-point register state in the
80  * the thread_struct is up to date for task tsk.
81  */
82 void flush_fp_to_thread(struct task_struct *tsk)
83 {
84 	if (tsk->thread.regs) {
85 		/*
86 		 * We need to disable preemption here because if we didn't,
87 		 * another process could get scheduled after the regs->msr
88 		 * test but before we have finished saving the FP registers
89 		 * to the thread_struct.  That process could take over the
90 		 * FPU, and then when we get scheduled again we would store
91 		 * bogus values for the remaining FP registers.
92 		 */
93 		preempt_disable();
94 		if (tsk->thread.regs->msr & MSR_FP) {
95 #ifdef CONFIG_SMP
96 			/*
97 			 * This should only ever be called for current or
98 			 * for a stopped child process.  Since we save away
99 			 * the FP register state on context switch on SMP,
100 			 * there is something wrong if a stopped child appears
101 			 * to still have its FP state in the CPU registers.
102 			 */
103 			BUG_ON(tsk != current);
104 #endif
105 			giveup_fpu(tsk);
106 		}
107 		preempt_enable();
108 	}
109 }
110 EXPORT_SYMBOL_GPL(flush_fp_to_thread);
111 #endif
112 
113 void enable_kernel_fp(void)
114 {
115 	WARN_ON(preemptible());
116 
117 #ifdef CONFIG_SMP
118 	if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
119 		giveup_fpu(current);
120 	else
121 		giveup_fpu(NULL);	/* just enables FP for kernel */
122 #else
123 	giveup_fpu(last_task_used_math);
124 #endif /* CONFIG_SMP */
125 }
126 EXPORT_SYMBOL(enable_kernel_fp);
127 
128 #ifdef CONFIG_ALTIVEC
129 void enable_kernel_altivec(void)
130 {
131 	WARN_ON(preemptible());
132 
133 #ifdef CONFIG_SMP
134 	if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
135 		giveup_altivec(current);
136 	else
137 		giveup_altivec_notask();
138 #else
139 	giveup_altivec(last_task_used_altivec);
140 #endif /* CONFIG_SMP */
141 }
142 EXPORT_SYMBOL(enable_kernel_altivec);
143 
144 /*
145  * Make sure the VMX/Altivec register state in the
146  * the thread_struct is up to date for task tsk.
147  */
148 void flush_altivec_to_thread(struct task_struct *tsk)
149 {
150 	if (tsk->thread.regs) {
151 		preempt_disable();
152 		if (tsk->thread.regs->msr & MSR_VEC) {
153 #ifdef CONFIG_SMP
154 			BUG_ON(tsk != current);
155 #endif
156 			giveup_altivec(tsk);
157 		}
158 		preempt_enable();
159 	}
160 }
161 EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
162 #endif /* CONFIG_ALTIVEC */
163 
164 #ifdef CONFIG_VSX
165 #if 0
166 /* not currently used, but some crazy RAID module might want to later */
167 void enable_kernel_vsx(void)
168 {
169 	WARN_ON(preemptible());
170 
171 #ifdef CONFIG_SMP
172 	if (current->thread.regs && (current->thread.regs->msr & MSR_VSX))
173 		giveup_vsx(current);
174 	else
175 		giveup_vsx(NULL);	/* just enable vsx for kernel - force */
176 #else
177 	giveup_vsx(last_task_used_vsx);
178 #endif /* CONFIG_SMP */
179 }
180 EXPORT_SYMBOL(enable_kernel_vsx);
181 #endif
182 
183 void giveup_vsx(struct task_struct *tsk)
184 {
185 	giveup_fpu(tsk);
186 	giveup_altivec(tsk);
187 	__giveup_vsx(tsk);
188 }
189 
190 void flush_vsx_to_thread(struct task_struct *tsk)
191 {
192 	if (tsk->thread.regs) {
193 		preempt_disable();
194 		if (tsk->thread.regs->msr & MSR_VSX) {
195 #ifdef CONFIG_SMP
196 			BUG_ON(tsk != current);
197 #endif
198 			giveup_vsx(tsk);
199 		}
200 		preempt_enable();
201 	}
202 }
203 EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
204 #endif /* CONFIG_VSX */
205 
206 #ifdef CONFIG_SPE
207 
208 void enable_kernel_spe(void)
209 {
210 	WARN_ON(preemptible());
211 
212 #ifdef CONFIG_SMP
213 	if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
214 		giveup_spe(current);
215 	else
216 		giveup_spe(NULL);	/* just enable SPE for kernel - force */
217 #else
218 	giveup_spe(last_task_used_spe);
219 #endif /* __SMP __ */
220 }
221 EXPORT_SYMBOL(enable_kernel_spe);
222 
223 void flush_spe_to_thread(struct task_struct *tsk)
224 {
225 	if (tsk->thread.regs) {
226 		preempt_disable();
227 		if (tsk->thread.regs->msr & MSR_SPE) {
228 #ifdef CONFIG_SMP
229 			BUG_ON(tsk != current);
230 #endif
231 			tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
232 			giveup_spe(tsk);
233 		}
234 		preempt_enable();
235 	}
236 }
237 #endif /* CONFIG_SPE */
238 
239 #ifndef CONFIG_SMP
240 /*
241  * If we are doing lazy switching of CPU state (FP, altivec or SPE),
242  * and the current task has some state, discard it.
243  */
244 void discard_lazy_cpu_state(void)
245 {
246 	preempt_disable();
247 	if (last_task_used_math == current)
248 		last_task_used_math = NULL;
249 #ifdef CONFIG_ALTIVEC
250 	if (last_task_used_altivec == current)
251 		last_task_used_altivec = NULL;
252 #endif /* CONFIG_ALTIVEC */
253 #ifdef CONFIG_VSX
254 	if (last_task_used_vsx == current)
255 		last_task_used_vsx = NULL;
256 #endif /* CONFIG_VSX */
257 #ifdef CONFIG_SPE
258 	if (last_task_used_spe == current)
259 		last_task_used_spe = NULL;
260 #endif
261 	preempt_enable();
262 }
263 #endif /* CONFIG_SMP */
264 
265 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
266 void do_send_trap(struct pt_regs *regs, unsigned long address,
267 		  unsigned long error_code, int signal_code, int breakpt)
268 {
269 	siginfo_t info;
270 
271 	current->thread.trap_nr = signal_code;
272 	if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
273 			11, SIGSEGV) == NOTIFY_STOP)
274 		return;
275 
276 	/* Deliver the signal to userspace */
277 	info.si_signo = SIGTRAP;
278 	info.si_errno = breakpt;	/* breakpoint or watchpoint id */
279 	info.si_code = signal_code;
280 	info.si_addr = (void __user *)address;
281 	force_sig_info(SIGTRAP, &info, current);
282 }
283 #else	/* !CONFIG_PPC_ADV_DEBUG_REGS */
284 void do_break (struct pt_regs *regs, unsigned long address,
285 		    unsigned long error_code)
286 {
287 	siginfo_t info;
288 
289 	current->thread.trap_nr = TRAP_HWBKPT;
290 	if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
291 			11, SIGSEGV) == NOTIFY_STOP)
292 		return;
293 
294 	if (debugger_break_match(regs))
295 		return;
296 
297 	/* Clear the breakpoint */
298 	hw_breakpoint_disable();
299 
300 	/* Deliver the signal to userspace */
301 	info.si_signo = SIGTRAP;
302 	info.si_errno = 0;
303 	info.si_code = TRAP_HWBKPT;
304 	info.si_addr = (void __user *)address;
305 	force_sig_info(SIGTRAP, &info, current);
306 }
307 #endif	/* CONFIG_PPC_ADV_DEBUG_REGS */
308 
309 static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk);
310 
311 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
312 /*
313  * Set the debug registers back to their default "safe" values.
314  */
315 static void set_debug_reg_defaults(struct thread_struct *thread)
316 {
317 	thread->iac1 = thread->iac2 = 0;
318 #if CONFIG_PPC_ADV_DEBUG_IACS > 2
319 	thread->iac3 = thread->iac4 = 0;
320 #endif
321 	thread->dac1 = thread->dac2 = 0;
322 #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
323 	thread->dvc1 = thread->dvc2 = 0;
324 #endif
325 	thread->dbcr0 = 0;
326 #ifdef CONFIG_BOOKE
327 	/*
328 	 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
329 	 */
330 	thread->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |	\
331 			DBCR1_IAC3US | DBCR1_IAC4US;
332 	/*
333 	 * Force Data Address Compare User/Supervisor bits to be User-only
334 	 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
335 	 */
336 	thread->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
337 #else
338 	thread->dbcr1 = 0;
339 #endif
340 }
341 
342 static void prime_debug_regs(struct thread_struct *thread)
343 {
344 	/*
345 	 * We could have inherited MSR_DE from userspace, since
346 	 * it doesn't get cleared on exception entry.  Make sure
347 	 * MSR_DE is clear before we enable any debug events.
348 	 */
349 	mtmsr(mfmsr() & ~MSR_DE);
350 
351 	mtspr(SPRN_IAC1, thread->iac1);
352 	mtspr(SPRN_IAC2, thread->iac2);
353 #if CONFIG_PPC_ADV_DEBUG_IACS > 2
354 	mtspr(SPRN_IAC3, thread->iac3);
355 	mtspr(SPRN_IAC4, thread->iac4);
356 #endif
357 	mtspr(SPRN_DAC1, thread->dac1);
358 	mtspr(SPRN_DAC2, thread->dac2);
359 #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
360 	mtspr(SPRN_DVC1, thread->dvc1);
361 	mtspr(SPRN_DVC2, thread->dvc2);
362 #endif
363 	mtspr(SPRN_DBCR0, thread->dbcr0);
364 	mtspr(SPRN_DBCR1, thread->dbcr1);
365 #ifdef CONFIG_BOOKE
366 	mtspr(SPRN_DBCR2, thread->dbcr2);
367 #endif
368 }
369 /*
370  * Unless neither the old or new thread are making use of the
371  * debug registers, set the debug registers from the values
372  * stored in the new thread.
373  */
374 static void switch_booke_debug_regs(struct thread_struct *new_thread)
375 {
376 	if ((current->thread.dbcr0 & DBCR0_IDM)
377 		|| (new_thread->dbcr0 & DBCR0_IDM))
378 			prime_debug_regs(new_thread);
379 }
380 #else	/* !CONFIG_PPC_ADV_DEBUG_REGS */
381 #ifndef CONFIG_HAVE_HW_BREAKPOINT
382 static void set_debug_reg_defaults(struct thread_struct *thread)
383 {
384 	thread->hw_brk.address = 0;
385 	thread->hw_brk.type = 0;
386 	set_breakpoint(&thread->hw_brk);
387 }
388 #endif /* !CONFIG_HAVE_HW_BREAKPOINT */
389 #endif	/* CONFIG_PPC_ADV_DEBUG_REGS */
390 
391 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
392 static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
393 {
394 	mtspr(SPRN_DAC1, dabr);
395 #ifdef CONFIG_PPC_47x
396 	isync();
397 #endif
398 	return 0;
399 }
400 #elif defined(CONFIG_PPC_BOOK3S)
401 static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
402 {
403 	mtspr(SPRN_DABR, dabr);
404 	if (cpu_has_feature(CPU_FTR_DABRX))
405 		mtspr(SPRN_DABRX, dabrx);
406 	return 0;
407 }
408 #else
409 static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
410 {
411 	return -EINVAL;
412 }
413 #endif
414 
415 static inline int set_dabr(struct arch_hw_breakpoint *brk)
416 {
417 	unsigned long dabr, dabrx;
418 
419 	dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
420 	dabrx = ((brk->type >> 3) & 0x7);
421 
422 	if (ppc_md.set_dabr)
423 		return ppc_md.set_dabr(dabr, dabrx);
424 
425 	return __set_dabr(dabr, dabrx);
426 }
427 
428 static inline int set_dawr(struct arch_hw_breakpoint *brk)
429 {
430 	unsigned long dawr, dawrx, mrd;
431 
432 	dawr = brk->address;
433 
434 	dawrx  = (brk->type & (HW_BRK_TYPE_READ | HW_BRK_TYPE_WRITE)) \
435 		                   << (63 - 58); //* read/write bits */
436 	dawrx |= ((brk->type & (HW_BRK_TYPE_TRANSLATE)) >> 2) \
437 		                   << (63 - 59); //* translate */
438 	dawrx |= (brk->type & (HW_BRK_TYPE_PRIV_ALL)) \
439 		                   >> 3; //* PRIM bits */
440 	/* dawr length is stored in field MDR bits 48:53.  Matches range in
441 	   doublewords (64 bits) baised by -1 eg. 0b000000=1DW and
442 	   0b111111=64DW.
443 	   brk->len is in bytes.
444 	   This aligns up to double word size, shifts and does the bias.
445 	*/
446 	mrd = ((brk->len + 7) >> 3) - 1;
447 	dawrx |= (mrd & 0x3f) << (63 - 53);
448 
449 	if (ppc_md.set_dawr)
450 		return ppc_md.set_dawr(dawr, dawrx);
451 	mtspr(SPRN_DAWR, dawr);
452 	mtspr(SPRN_DAWRX, dawrx);
453 	return 0;
454 }
455 
456 int set_breakpoint(struct arch_hw_breakpoint *brk)
457 {
458 	__get_cpu_var(current_brk) = *brk;
459 
460 	if (cpu_has_feature(CPU_FTR_DAWR))
461 		return set_dawr(brk);
462 
463 	return set_dabr(brk);
464 }
465 
466 #ifdef CONFIG_PPC64
467 DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
468 #endif
469 
470 static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
471 			      struct arch_hw_breakpoint *b)
472 {
473 	if (a->address != b->address)
474 		return false;
475 	if (a->type != b->type)
476 		return false;
477 	if (a->len != b->len)
478 		return false;
479 	return true;
480 }
481 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
482 static inline void tm_reclaim_task(struct task_struct *tsk)
483 {
484 	/* We have to work out if we're switching from/to a task that's in the
485 	 * middle of a transaction.
486 	 *
487 	 * In switching we need to maintain a 2nd register state as
488 	 * oldtask->thread.ckpt_regs.  We tm_reclaim(oldproc); this saves the
489 	 * checkpointed (tbegin) state in ckpt_regs and saves the transactional
490 	 * (current) FPRs into oldtask->thread.transact_fpr[].
491 	 *
492 	 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
493 	 */
494 	struct thread_struct *thr = &tsk->thread;
495 
496 	if (!thr->regs)
497 		return;
498 
499 	if (!MSR_TM_ACTIVE(thr->regs->msr))
500 		goto out_and_saveregs;
501 
502 	/* Stash the original thread MSR, as giveup_fpu et al will
503 	 * modify it.  We hold onto it to see whether the task used
504 	 * FP & vector regs.
505 	 */
506 	thr->tm_orig_msr = thr->regs->msr;
507 
508 	TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
509 		 "ccr=%lx, msr=%lx, trap=%lx)\n",
510 		 tsk->pid, thr->regs->nip,
511 		 thr->regs->ccr, thr->regs->msr,
512 		 thr->regs->trap);
513 
514 	tm_reclaim(thr, thr->regs->msr, TM_CAUSE_RESCHED);
515 
516 	TM_DEBUG("--- tm_reclaim on pid %d complete\n",
517 		 tsk->pid);
518 
519 out_and_saveregs:
520 	/* Always save the regs here, even if a transaction's not active.
521 	 * This context-switches a thread's TM info SPRs.  We do it here to
522 	 * be consistent with the restore path (in recheckpoint) which
523 	 * cannot happen later in _switch().
524 	 */
525 	tm_save_sprs(thr);
526 }
527 
528 static inline void tm_recheckpoint_new_task(struct task_struct *new)
529 {
530 	unsigned long msr;
531 
532 	if (!cpu_has_feature(CPU_FTR_TM))
533 		return;
534 
535 	/* Recheckpoint the registers of the thread we're about to switch to.
536 	 *
537 	 * If the task was using FP, we non-lazily reload both the original and
538 	 * the speculative FP register states.  This is because the kernel
539 	 * doesn't see if/when a TM rollback occurs, so if we take an FP
540 	 * unavoidable later, we are unable to determine which set of FP regs
541 	 * need to be restored.
542 	 */
543 	if (!new->thread.regs)
544 		return;
545 
546 	/* The TM SPRs are restored here, so that TEXASR.FS can be set
547 	 * before the trecheckpoint and no explosion occurs.
548 	 */
549 	tm_restore_sprs(&new->thread);
550 
551 	if (!MSR_TM_ACTIVE(new->thread.regs->msr))
552 		return;
553 	msr = new->thread.tm_orig_msr;
554 	/* Recheckpoint to restore original checkpointed register state. */
555 	TM_DEBUG("*** tm_recheckpoint of pid %d "
556 		 "(new->msr 0x%lx, new->origmsr 0x%lx)\n",
557 		 new->pid, new->thread.regs->msr, msr);
558 
559 	/* This loads the checkpointed FP/VEC state, if used */
560 	tm_recheckpoint(&new->thread, msr);
561 
562 	/* This loads the speculative FP/VEC state, if used */
563 	if (msr & MSR_FP) {
564 		do_load_up_transact_fpu(&new->thread);
565 		new->thread.regs->msr |=
566 			(MSR_FP | new->thread.fpexc_mode);
567 	}
568 #ifdef CONFIG_ALTIVEC
569 	if (msr & MSR_VEC) {
570 		do_load_up_transact_altivec(&new->thread);
571 		new->thread.regs->msr |= MSR_VEC;
572 	}
573 #endif
574 	/* We may as well turn on VSX too since all the state is restored now */
575 	if (msr & MSR_VSX)
576 		new->thread.regs->msr |= MSR_VSX;
577 
578 	TM_DEBUG("*** tm_recheckpoint of pid %d complete "
579 		 "(kernel msr 0x%lx)\n",
580 		 new->pid, mfmsr());
581 }
582 
583 static inline void __switch_to_tm(struct task_struct *prev)
584 {
585 	if (cpu_has_feature(CPU_FTR_TM)) {
586 		tm_enable();
587 		tm_reclaim_task(prev);
588 	}
589 }
590 #else
591 #define tm_recheckpoint_new_task(new)
592 #define __switch_to_tm(prev)
593 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
594 
595 struct task_struct *__switch_to(struct task_struct *prev,
596 	struct task_struct *new)
597 {
598 	struct thread_struct *new_thread, *old_thread;
599 	unsigned long flags;
600 	struct task_struct *last;
601 #ifdef CONFIG_PPC_BOOK3S_64
602 	struct ppc64_tlb_batch *batch;
603 #endif
604 
605 	/* Back up the TAR across context switches.
606 	 * Note that the TAR is not available for use in the kernel.  (To
607 	 * provide this, the TAR should be backed up/restored on exception
608 	 * entry/exit instead, and be in pt_regs.  FIXME, this should be in
609 	 * pt_regs anyway (for debug).)
610 	 * Save the TAR here before we do treclaim/trecheckpoint as these
611 	 * will change the TAR.
612 	 */
613 	save_tar(&prev->thread);
614 
615 	__switch_to_tm(prev);
616 
617 #ifdef CONFIG_SMP
618 	/* avoid complexity of lazy save/restore of fpu
619 	 * by just saving it every time we switch out if
620 	 * this task used the fpu during the last quantum.
621 	 *
622 	 * If it tries to use the fpu again, it'll trap and
623 	 * reload its fp regs.  So we don't have to do a restore
624 	 * every switch, just a save.
625 	 *  -- Cort
626 	 */
627 	if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
628 		giveup_fpu(prev);
629 #ifdef CONFIG_ALTIVEC
630 	/*
631 	 * If the previous thread used altivec in the last quantum
632 	 * (thus changing altivec regs) then save them.
633 	 * We used to check the VRSAVE register but not all apps
634 	 * set it, so we don't rely on it now (and in fact we need
635 	 * to save & restore VSCR even if VRSAVE == 0).  -- paulus
636 	 *
637 	 * On SMP we always save/restore altivec regs just to avoid the
638 	 * complexity of changing processors.
639 	 *  -- Cort
640 	 */
641 	if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
642 		giveup_altivec(prev);
643 #endif /* CONFIG_ALTIVEC */
644 #ifdef CONFIG_VSX
645 	if (prev->thread.regs && (prev->thread.regs->msr & MSR_VSX))
646 		/* VMX and FPU registers are already save here */
647 		__giveup_vsx(prev);
648 #endif /* CONFIG_VSX */
649 #ifdef CONFIG_SPE
650 	/*
651 	 * If the previous thread used spe in the last quantum
652 	 * (thus changing spe regs) then save them.
653 	 *
654 	 * On SMP we always save/restore spe regs just to avoid the
655 	 * complexity of changing processors.
656 	 */
657 	if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
658 		giveup_spe(prev);
659 #endif /* CONFIG_SPE */
660 
661 #else  /* CONFIG_SMP */
662 #ifdef CONFIG_ALTIVEC
663 	/* Avoid the trap.  On smp this this never happens since
664 	 * we don't set last_task_used_altivec -- Cort
665 	 */
666 	if (new->thread.regs && last_task_used_altivec == new)
667 		new->thread.regs->msr |= MSR_VEC;
668 #endif /* CONFIG_ALTIVEC */
669 #ifdef CONFIG_VSX
670 	if (new->thread.regs && last_task_used_vsx == new)
671 		new->thread.regs->msr |= MSR_VSX;
672 #endif /* CONFIG_VSX */
673 #ifdef CONFIG_SPE
674 	/* Avoid the trap.  On smp this this never happens since
675 	 * we don't set last_task_used_spe
676 	 */
677 	if (new->thread.regs && last_task_used_spe == new)
678 		new->thread.regs->msr |= MSR_SPE;
679 #endif /* CONFIG_SPE */
680 
681 #endif /* CONFIG_SMP */
682 
683 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
684 	switch_booke_debug_regs(&new->thread);
685 #else
686 /*
687  * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
688  * schedule DABR
689  */
690 #ifndef CONFIG_HAVE_HW_BREAKPOINT
691 	if (unlikely(hw_brk_match(&__get_cpu_var(current_brk), &new->thread.hw_brk)))
692 		set_breakpoint(&new->thread.hw_brk);
693 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
694 #endif
695 
696 
697 	new_thread = &new->thread;
698 	old_thread = &current->thread;
699 
700 #ifdef CONFIG_PPC64
701 	/*
702 	 * Collect processor utilization data per process
703 	 */
704 	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
705 		struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
706 		long unsigned start_tb, current_tb;
707 		start_tb = old_thread->start_tb;
708 		cu->current_tb = current_tb = mfspr(SPRN_PURR);
709 		old_thread->accum_tb += (current_tb - start_tb);
710 		new_thread->start_tb = current_tb;
711 	}
712 #endif /* CONFIG_PPC64 */
713 
714 #ifdef CONFIG_PPC_BOOK3S_64
715 	batch = &__get_cpu_var(ppc64_tlb_batch);
716 	if (batch->active) {
717 		current_thread_info()->local_flags |= _TLF_LAZY_MMU;
718 		if (batch->index)
719 			__flush_tlb_pending(batch);
720 		batch->active = 0;
721 	}
722 #endif /* CONFIG_PPC_BOOK3S_64 */
723 
724 	local_irq_save(flags);
725 
726 	/*
727 	 * We can't take a PMU exception inside _switch() since there is a
728 	 * window where the kernel stack SLB and the kernel stack are out
729 	 * of sync. Hard disable here.
730 	 */
731 	hard_irq_disable();
732 
733 	tm_recheckpoint_new_task(new);
734 
735 	last = _switch(old_thread, new_thread);
736 
737 #ifdef CONFIG_PPC_BOOK3S_64
738 	if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
739 		current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
740 		batch = &__get_cpu_var(ppc64_tlb_batch);
741 		batch->active = 1;
742 	}
743 #endif /* CONFIG_PPC_BOOK3S_64 */
744 
745 	local_irq_restore(flags);
746 
747 	return last;
748 }
749 
750 static int instructions_to_print = 16;
751 
752 static void show_instructions(struct pt_regs *regs)
753 {
754 	int i;
755 	unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
756 			sizeof(int));
757 
758 	printk("Instruction dump:");
759 
760 	for (i = 0; i < instructions_to_print; i++) {
761 		int instr;
762 
763 		if (!(i % 8))
764 			printk("\n");
765 
766 #if !defined(CONFIG_BOOKE)
767 		/* If executing with the IMMU off, adjust pc rather
768 		 * than print XXXXXXXX.
769 		 */
770 		if (!(regs->msr & MSR_IR))
771 			pc = (unsigned long)phys_to_virt(pc);
772 #endif
773 
774 		/* We use __get_user here *only* to avoid an OOPS on a
775 		 * bad address because the pc *should* only be a
776 		 * kernel address.
777 		 */
778 		if (!__kernel_text_address(pc) ||
779 		     __get_user(instr, (unsigned int __user *)pc)) {
780 			printk(KERN_CONT "XXXXXXXX ");
781 		} else {
782 			if (regs->nip == pc)
783 				printk(KERN_CONT "<%08x> ", instr);
784 			else
785 				printk(KERN_CONT "%08x ", instr);
786 		}
787 
788 		pc += sizeof(int);
789 	}
790 
791 	printk("\n");
792 }
793 
794 static struct regbit {
795 	unsigned long bit;
796 	const char *name;
797 } msr_bits[] = {
798 #if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
799 	{MSR_SF,	"SF"},
800 	{MSR_HV,	"HV"},
801 #endif
802 	{MSR_VEC,	"VEC"},
803 	{MSR_VSX,	"VSX"},
804 #ifdef CONFIG_BOOKE
805 	{MSR_CE,	"CE"},
806 #endif
807 	{MSR_EE,	"EE"},
808 	{MSR_PR,	"PR"},
809 	{MSR_FP,	"FP"},
810 	{MSR_ME,	"ME"},
811 #ifdef CONFIG_BOOKE
812 	{MSR_DE,	"DE"},
813 #else
814 	{MSR_SE,	"SE"},
815 	{MSR_BE,	"BE"},
816 #endif
817 	{MSR_IR,	"IR"},
818 	{MSR_DR,	"DR"},
819 	{MSR_PMM,	"PMM"},
820 #ifndef CONFIG_BOOKE
821 	{MSR_RI,	"RI"},
822 	{MSR_LE,	"LE"},
823 #endif
824 	{0,		NULL}
825 };
826 
827 static void printbits(unsigned long val, struct regbit *bits)
828 {
829 	const char *sep = "";
830 
831 	printk("<");
832 	for (; bits->bit; ++bits)
833 		if (val & bits->bit) {
834 			printk("%s%s", sep, bits->name);
835 			sep = ",";
836 		}
837 	printk(">");
838 }
839 
840 #ifdef CONFIG_PPC64
841 #define REG		"%016lx"
842 #define REGS_PER_LINE	4
843 #define LAST_VOLATILE	13
844 #else
845 #define REG		"%08lx"
846 #define REGS_PER_LINE	8
847 #define LAST_VOLATILE	12
848 #endif
849 
850 void show_regs(struct pt_regs * regs)
851 {
852 	int i, trap;
853 
854 	show_regs_print_info(KERN_DEFAULT);
855 
856 	printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
857 	       regs->nip, regs->link, regs->ctr);
858 	printk("REGS: %p TRAP: %04lx   %s  (%s)\n",
859 	       regs, regs->trap, print_tainted(), init_utsname()->release);
860 	printk("MSR: "REG" ", regs->msr);
861 	printbits(regs->msr, msr_bits);
862 	printk("  CR: %08lx  XER: %08lx\n", regs->ccr, regs->xer);
863 #ifdef CONFIG_PPC64
864 	printk("SOFTE: %ld\n", regs->softe);
865 #endif
866 	trap = TRAP(regs);
867 	if ((regs->trap != 0xc00) && cpu_has_feature(CPU_FTR_CFAR))
868 		printk("CFAR: "REG"\n", regs->orig_gpr3);
869 	if (trap == 0x300 || trap == 0x600)
870 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
871 		printk("DEAR: "REG", ESR: "REG"\n", regs->dar, regs->dsisr);
872 #else
873 		printk("DAR: "REG", DSISR: %08lx\n", regs->dar, regs->dsisr);
874 #endif
875 
876 	for (i = 0;  i < 32;  i++) {
877 		if ((i % REGS_PER_LINE) == 0)
878 			printk("\nGPR%02d: ", i);
879 		printk(REG " ", regs->gpr[i]);
880 		if (i == LAST_VOLATILE && !FULL_REGS(regs))
881 			break;
882 	}
883 	printk("\n");
884 #ifdef CONFIG_KALLSYMS
885 	/*
886 	 * Lookup NIP late so we have the best change of getting the
887 	 * above info out without failing
888 	 */
889 	printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
890 	printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
891 #endif
892 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
893 	printk("PACATMSCRATCH [%llx]\n", get_paca()->tm_scratch);
894 #endif
895 	show_stack(current, (unsigned long *) regs->gpr[1]);
896 	if (!user_mode(regs))
897 		show_instructions(regs);
898 }
899 
900 void exit_thread(void)
901 {
902 	discard_lazy_cpu_state();
903 }
904 
905 void flush_thread(void)
906 {
907 	discard_lazy_cpu_state();
908 
909 #ifdef CONFIG_HAVE_HW_BREAKPOINT
910 	flush_ptrace_hw_breakpoint(current);
911 #else /* CONFIG_HAVE_HW_BREAKPOINT */
912 	set_debug_reg_defaults(&current->thread);
913 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
914 }
915 
916 void
917 release_thread(struct task_struct *t)
918 {
919 }
920 
921 /*
922  * this gets called so that we can store coprocessor state into memory and
923  * copy the current task into the new thread.
924  */
925 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
926 {
927 	flush_fp_to_thread(src);
928 	flush_altivec_to_thread(src);
929 	flush_vsx_to_thread(src);
930 	flush_spe_to_thread(src);
931 
932 	*dst = *src;
933 
934 	clear_task_ebb(dst);
935 
936 	return 0;
937 }
938 
939 /*
940  * Copy a thread..
941  */
942 extern unsigned long dscr_default; /* defined in arch/powerpc/kernel/sysfs.c */
943 
944 int copy_thread(unsigned long clone_flags, unsigned long usp,
945 		unsigned long arg, struct task_struct *p)
946 {
947 	struct pt_regs *childregs, *kregs;
948 	extern void ret_from_fork(void);
949 	extern void ret_from_kernel_thread(void);
950 	void (*f)(void);
951 	unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
952 
953 	/* Copy registers */
954 	sp -= sizeof(struct pt_regs);
955 	childregs = (struct pt_regs *) sp;
956 	if (unlikely(p->flags & PF_KTHREAD)) {
957 		struct thread_info *ti = (void *)task_stack_page(p);
958 		memset(childregs, 0, sizeof(struct pt_regs));
959 		childregs->gpr[1] = sp + sizeof(struct pt_regs);
960 		childregs->gpr[14] = usp;	/* function */
961 #ifdef CONFIG_PPC64
962 		clear_tsk_thread_flag(p, TIF_32BIT);
963 		childregs->softe = 1;
964 #endif
965 		childregs->gpr[15] = arg;
966 		p->thread.regs = NULL;	/* no user register state */
967 		ti->flags |= _TIF_RESTOREALL;
968 		f = ret_from_kernel_thread;
969 	} else {
970 		struct pt_regs *regs = current_pt_regs();
971 		CHECK_FULL_REGS(regs);
972 		*childregs = *regs;
973 		if (usp)
974 			childregs->gpr[1] = usp;
975 		p->thread.regs = childregs;
976 		childregs->gpr[3] = 0;  /* Result from fork() */
977 		if (clone_flags & CLONE_SETTLS) {
978 #ifdef CONFIG_PPC64
979 			if (!is_32bit_task())
980 				childregs->gpr[13] = childregs->gpr[6];
981 			else
982 #endif
983 				childregs->gpr[2] = childregs->gpr[6];
984 		}
985 
986 		f = ret_from_fork;
987 	}
988 	sp -= STACK_FRAME_OVERHEAD;
989 
990 	/*
991 	 * The way this works is that at some point in the future
992 	 * some task will call _switch to switch to the new task.
993 	 * That will pop off the stack frame created below and start
994 	 * the new task running at ret_from_fork.  The new task will
995 	 * do some house keeping and then return from the fork or clone
996 	 * system call, using the stack frame created above.
997 	 */
998 	((unsigned long *)sp)[0] = 0;
999 	sp -= sizeof(struct pt_regs);
1000 	kregs = (struct pt_regs *) sp;
1001 	sp -= STACK_FRAME_OVERHEAD;
1002 	p->thread.ksp = sp;
1003 	p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
1004 				_ALIGN_UP(sizeof(struct thread_info), 16);
1005 
1006 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1007 	p->thread.ptrace_bps[0] = NULL;
1008 #endif
1009 
1010 #ifdef CONFIG_PPC_STD_MMU_64
1011 	if (mmu_has_feature(MMU_FTR_SLB)) {
1012 		unsigned long sp_vsid;
1013 		unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1014 
1015 		if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1016 			sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1017 				<< SLB_VSID_SHIFT_1T;
1018 		else
1019 			sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1020 				<< SLB_VSID_SHIFT;
1021 		sp_vsid |= SLB_VSID_KERNEL | llp;
1022 		p->thread.ksp_vsid = sp_vsid;
1023 	}
1024 #endif /* CONFIG_PPC_STD_MMU_64 */
1025 #ifdef CONFIG_PPC64
1026 	if (cpu_has_feature(CPU_FTR_DSCR)) {
1027 		p->thread.dscr_inherit = current->thread.dscr_inherit;
1028 		p->thread.dscr = current->thread.dscr;
1029 	}
1030 	if (cpu_has_feature(CPU_FTR_HAS_PPR))
1031 		p->thread.ppr = INIT_PPR;
1032 #endif
1033 	/*
1034 	 * The PPC64 ABI makes use of a TOC to contain function
1035 	 * pointers.  The function (ret_from_except) is actually a pointer
1036 	 * to the TOC entry.  The first entry is a pointer to the actual
1037 	 * function.
1038 	 */
1039 #ifdef CONFIG_PPC64
1040 	kregs->nip = *((unsigned long *)f);
1041 #else
1042 	kregs->nip = (unsigned long)f;
1043 #endif
1044 	return 0;
1045 }
1046 
1047 /*
1048  * Set up a thread for executing a new program
1049  */
1050 void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
1051 {
1052 #ifdef CONFIG_PPC64
1053 	unsigned long load_addr = regs->gpr[2];	/* saved by ELF_PLAT_INIT */
1054 #endif
1055 
1056 	/*
1057 	 * If we exec out of a kernel thread then thread.regs will not be
1058 	 * set.  Do it now.
1059 	 */
1060 	if (!current->thread.regs) {
1061 		struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1062 		current->thread.regs = regs - 1;
1063 	}
1064 
1065 	memset(regs->gpr, 0, sizeof(regs->gpr));
1066 	regs->ctr = 0;
1067 	regs->link = 0;
1068 	regs->xer = 0;
1069 	regs->ccr = 0;
1070 	regs->gpr[1] = sp;
1071 
1072 	/*
1073 	 * We have just cleared all the nonvolatile GPRs, so make
1074 	 * FULL_REGS(regs) return true.  This is necessary to allow
1075 	 * ptrace to examine the thread immediately after exec.
1076 	 */
1077 	regs->trap &= ~1UL;
1078 
1079 #ifdef CONFIG_PPC32
1080 	regs->mq = 0;
1081 	regs->nip = start;
1082 	regs->msr = MSR_USER;
1083 #else
1084 	if (!is_32bit_task()) {
1085 		unsigned long entry, toc;
1086 
1087 		/* start is a relocated pointer to the function descriptor for
1088 		 * the elf _start routine.  The first entry in the function
1089 		 * descriptor is the entry address of _start and the second
1090 		 * entry is the TOC value we need to use.
1091 		 */
1092 		__get_user(entry, (unsigned long __user *)start);
1093 		__get_user(toc, (unsigned long __user *)start+1);
1094 
1095 		/* Check whether the e_entry function descriptor entries
1096 		 * need to be relocated before we can use them.
1097 		 */
1098 		if (load_addr != 0) {
1099 			entry += load_addr;
1100 			toc   += load_addr;
1101 		}
1102 		regs->nip = entry;
1103 		regs->gpr[2] = toc;
1104 		regs->msr = MSR_USER64;
1105 	} else {
1106 		regs->nip = start;
1107 		regs->gpr[2] = 0;
1108 		regs->msr = MSR_USER32;
1109 	}
1110 #endif
1111 	discard_lazy_cpu_state();
1112 #ifdef CONFIG_VSX
1113 	current->thread.used_vsr = 0;
1114 #endif
1115 	memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
1116 	current->thread.fpscr.val = 0;
1117 #ifdef CONFIG_ALTIVEC
1118 	memset(current->thread.vr, 0, sizeof(current->thread.vr));
1119 	memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
1120 	current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
1121 	current->thread.vrsave = 0;
1122 	current->thread.used_vr = 0;
1123 #endif /* CONFIG_ALTIVEC */
1124 #ifdef CONFIG_SPE
1125 	memset(current->thread.evr, 0, sizeof(current->thread.evr));
1126 	current->thread.acc = 0;
1127 	current->thread.spefscr = 0;
1128 	current->thread.used_spe = 0;
1129 #endif /* CONFIG_SPE */
1130 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1131 	if (cpu_has_feature(CPU_FTR_TM))
1132 		regs->msr |= MSR_TM;
1133 	current->thread.tm_tfhar = 0;
1134 	current->thread.tm_texasr = 0;
1135 	current->thread.tm_tfiar = 0;
1136 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1137 }
1138 
1139 #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1140 		| PR_FP_EXC_RES | PR_FP_EXC_INV)
1141 
1142 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1143 {
1144 	struct pt_regs *regs = tsk->thread.regs;
1145 
1146 	/* This is a bit hairy.  If we are an SPE enabled  processor
1147 	 * (have embedded fp) we store the IEEE exception enable flags in
1148 	 * fpexc_mode.  fpexc_mode is also used for setting FP exception
1149 	 * mode (asyn, precise, disabled) for 'Classic' FP. */
1150 	if (val & PR_FP_EXC_SW_ENABLE) {
1151 #ifdef CONFIG_SPE
1152 		if (cpu_has_feature(CPU_FTR_SPE)) {
1153 			tsk->thread.fpexc_mode = val &
1154 				(PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
1155 			return 0;
1156 		} else {
1157 			return -EINVAL;
1158 		}
1159 #else
1160 		return -EINVAL;
1161 #endif
1162 	}
1163 
1164 	/* on a CONFIG_SPE this does not hurt us.  The bits that
1165 	 * __pack_fe01 use do not overlap with bits used for
1166 	 * PR_FP_EXC_SW_ENABLE.  Additionally, the MSR[FE0,FE1] bits
1167 	 * on CONFIG_SPE implementations are reserved so writing to
1168 	 * them does not change anything */
1169 	if (val > PR_FP_EXC_PRECISE)
1170 		return -EINVAL;
1171 	tsk->thread.fpexc_mode = __pack_fe01(val);
1172 	if (regs != NULL && (regs->msr & MSR_FP) != 0)
1173 		regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
1174 			| tsk->thread.fpexc_mode;
1175 	return 0;
1176 }
1177 
1178 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
1179 {
1180 	unsigned int val;
1181 
1182 	if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
1183 #ifdef CONFIG_SPE
1184 		if (cpu_has_feature(CPU_FTR_SPE))
1185 			val = tsk->thread.fpexc_mode;
1186 		else
1187 			return -EINVAL;
1188 #else
1189 		return -EINVAL;
1190 #endif
1191 	else
1192 		val = __unpack_fe01(tsk->thread.fpexc_mode);
1193 	return put_user(val, (unsigned int __user *) adr);
1194 }
1195 
1196 int set_endian(struct task_struct *tsk, unsigned int val)
1197 {
1198 	struct pt_regs *regs = tsk->thread.regs;
1199 
1200 	if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
1201 	    (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
1202 		return -EINVAL;
1203 
1204 	if (regs == NULL)
1205 		return -EINVAL;
1206 
1207 	if (val == PR_ENDIAN_BIG)
1208 		regs->msr &= ~MSR_LE;
1209 	else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
1210 		regs->msr |= MSR_LE;
1211 	else
1212 		return -EINVAL;
1213 
1214 	return 0;
1215 }
1216 
1217 int get_endian(struct task_struct *tsk, unsigned long adr)
1218 {
1219 	struct pt_regs *regs = tsk->thread.regs;
1220 	unsigned int val;
1221 
1222 	if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
1223 	    !cpu_has_feature(CPU_FTR_REAL_LE))
1224 		return -EINVAL;
1225 
1226 	if (regs == NULL)
1227 		return -EINVAL;
1228 
1229 	if (regs->msr & MSR_LE) {
1230 		if (cpu_has_feature(CPU_FTR_REAL_LE))
1231 			val = PR_ENDIAN_LITTLE;
1232 		else
1233 			val = PR_ENDIAN_PPC_LITTLE;
1234 	} else
1235 		val = PR_ENDIAN_BIG;
1236 
1237 	return put_user(val, (unsigned int __user *)adr);
1238 }
1239 
1240 int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
1241 {
1242 	tsk->thread.align_ctl = val;
1243 	return 0;
1244 }
1245 
1246 int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
1247 {
1248 	return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
1249 }
1250 
1251 static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
1252 				  unsigned long nbytes)
1253 {
1254 	unsigned long stack_page;
1255 	unsigned long cpu = task_cpu(p);
1256 
1257 	/*
1258 	 * Avoid crashing if the stack has overflowed and corrupted
1259 	 * task_cpu(p), which is in the thread_info struct.
1260 	 */
1261 	if (cpu < NR_CPUS && cpu_possible(cpu)) {
1262 		stack_page = (unsigned long) hardirq_ctx[cpu];
1263 		if (sp >= stack_page + sizeof(struct thread_struct)
1264 		    && sp <= stack_page + THREAD_SIZE - nbytes)
1265 			return 1;
1266 
1267 		stack_page = (unsigned long) softirq_ctx[cpu];
1268 		if (sp >= stack_page + sizeof(struct thread_struct)
1269 		    && sp <= stack_page + THREAD_SIZE - nbytes)
1270 			return 1;
1271 	}
1272 	return 0;
1273 }
1274 
1275 int validate_sp(unsigned long sp, struct task_struct *p,
1276 		       unsigned long nbytes)
1277 {
1278 	unsigned long stack_page = (unsigned long)task_stack_page(p);
1279 
1280 	if (sp >= stack_page + sizeof(struct thread_struct)
1281 	    && sp <= stack_page + THREAD_SIZE - nbytes)
1282 		return 1;
1283 
1284 	return valid_irq_stack(sp, p, nbytes);
1285 }
1286 
1287 EXPORT_SYMBOL(validate_sp);
1288 
1289 unsigned long get_wchan(struct task_struct *p)
1290 {
1291 	unsigned long ip, sp;
1292 	int count = 0;
1293 
1294 	if (!p || p == current || p->state == TASK_RUNNING)
1295 		return 0;
1296 
1297 	sp = p->thread.ksp;
1298 	if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
1299 		return 0;
1300 
1301 	do {
1302 		sp = *(unsigned long *)sp;
1303 		if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
1304 			return 0;
1305 		if (count > 0) {
1306 			ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
1307 			if (!in_sched_functions(ip))
1308 				return ip;
1309 		}
1310 	} while (count++ < 16);
1311 	return 0;
1312 }
1313 
1314 static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
1315 
1316 void show_stack(struct task_struct *tsk, unsigned long *stack)
1317 {
1318 	unsigned long sp, ip, lr, newsp;
1319 	int count = 0;
1320 	int firstframe = 1;
1321 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1322 	int curr_frame = current->curr_ret_stack;
1323 	extern void return_to_handler(void);
1324 	unsigned long rth = (unsigned long)return_to_handler;
1325 	unsigned long mrth = -1;
1326 #ifdef CONFIG_PPC64
1327 	extern void mod_return_to_handler(void);
1328 	rth = *(unsigned long *)rth;
1329 	mrth = (unsigned long)mod_return_to_handler;
1330 	mrth = *(unsigned long *)mrth;
1331 #endif
1332 #endif
1333 
1334 	sp = (unsigned long) stack;
1335 	if (tsk == NULL)
1336 		tsk = current;
1337 	if (sp == 0) {
1338 		if (tsk == current)
1339 			asm("mr %0,1" : "=r" (sp));
1340 		else
1341 			sp = tsk->thread.ksp;
1342 	}
1343 
1344 	lr = 0;
1345 	printk("Call Trace:\n");
1346 	do {
1347 		if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
1348 			return;
1349 
1350 		stack = (unsigned long *) sp;
1351 		newsp = stack[0];
1352 		ip = stack[STACK_FRAME_LR_SAVE];
1353 		if (!firstframe || ip != lr) {
1354 			printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
1355 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1356 			if ((ip == rth || ip == mrth) && curr_frame >= 0) {
1357 				printk(" (%pS)",
1358 				       (void *)current->ret_stack[curr_frame].ret);
1359 				curr_frame--;
1360 			}
1361 #endif
1362 			if (firstframe)
1363 				printk(" (unreliable)");
1364 			printk("\n");
1365 		}
1366 		firstframe = 0;
1367 
1368 		/*
1369 		 * See if this is an exception frame.
1370 		 * We look for the "regshere" marker in the current frame.
1371 		 */
1372 		if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
1373 		    && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
1374 			struct pt_regs *regs = (struct pt_regs *)
1375 				(sp + STACK_FRAME_OVERHEAD);
1376 			lr = regs->link;
1377 			printk("--- Exception: %lx at %pS\n    LR = %pS\n",
1378 			       regs->trap, (void *)regs->nip, (void *)lr);
1379 			firstframe = 1;
1380 		}
1381 
1382 		sp = newsp;
1383 	} while (count++ < kstack_depth_to_print);
1384 }
1385 
1386 #ifdef CONFIG_PPC64
1387 /* Called with hard IRQs off */
1388 void notrace __ppc64_runlatch_on(void)
1389 {
1390 	struct thread_info *ti = current_thread_info();
1391 	unsigned long ctrl;
1392 
1393 	ctrl = mfspr(SPRN_CTRLF);
1394 	ctrl |= CTRL_RUNLATCH;
1395 	mtspr(SPRN_CTRLT, ctrl);
1396 
1397 	ti->local_flags |= _TLF_RUNLATCH;
1398 }
1399 
1400 /* Called with hard IRQs off */
1401 void notrace __ppc64_runlatch_off(void)
1402 {
1403 	struct thread_info *ti = current_thread_info();
1404 	unsigned long ctrl;
1405 
1406 	ti->local_flags &= ~_TLF_RUNLATCH;
1407 
1408 	ctrl = mfspr(SPRN_CTRLF);
1409 	ctrl &= ~CTRL_RUNLATCH;
1410 	mtspr(SPRN_CTRLT, ctrl);
1411 }
1412 #endif /* CONFIG_PPC64 */
1413 
1414 unsigned long arch_align_stack(unsigned long sp)
1415 {
1416 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
1417 		sp -= get_random_int() & ~PAGE_MASK;
1418 	return sp & ~0xf;
1419 }
1420 
1421 static inline unsigned long brk_rnd(void)
1422 {
1423         unsigned long rnd = 0;
1424 
1425 	/* 8MB for 32bit, 1GB for 64bit */
1426 	if (is_32bit_task())
1427 		rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
1428 	else
1429 		rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
1430 
1431 	return rnd << PAGE_SHIFT;
1432 }
1433 
1434 unsigned long arch_randomize_brk(struct mm_struct *mm)
1435 {
1436 	unsigned long base = mm->brk;
1437 	unsigned long ret;
1438 
1439 #ifdef CONFIG_PPC_STD_MMU_64
1440 	/*
1441 	 * If we are using 1TB segments and we are allowed to randomise
1442 	 * the heap, we can put it above 1TB so it is backed by a 1TB
1443 	 * segment. Otherwise the heap will be in the bottom 1TB
1444 	 * which always uses 256MB segments and this may result in a
1445 	 * performance penalty.
1446 	 */
1447 	if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
1448 		base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
1449 #endif
1450 
1451 	ret = PAGE_ALIGN(base + brk_rnd());
1452 
1453 	if (ret < mm->brk)
1454 		return mm->brk;
1455 
1456 	return ret;
1457 }
1458 
1459 unsigned long randomize_et_dyn(unsigned long base)
1460 {
1461 	unsigned long ret = PAGE_ALIGN(base + brk_rnd());
1462 
1463 	if (ret < base)
1464 		return base;
1465 
1466 	return ret;
1467 }
1468