xref: /openbmc/linux/arch/powerpc/xmon/xmon.c (revision 93d90ad7)
1 /*
2  * Routines providing a simple monitor for use on the PowerMac.
3  *
4  * Copyright (C) 1996-2005 Paul Mackerras.
5  * Copyright (C) 2001 PPC64 Team, IBM Corp
6  * Copyrignt (C) 2006 Michael Ellerman, IBM Corp
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/smp.h>
16 #include <linux/mm.h>
17 #include <linux/reboot.h>
18 #include <linux/delay.h>
19 #include <linux/kallsyms.h>
20 #include <linux/kmsg_dump.h>
21 #include <linux/cpumask.h>
22 #include <linux/export.h>
23 #include <linux/sysrq.h>
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/bug.h>
27 #include <linux/nmi.h>
28 
29 #include <asm/ptrace.h>
30 #include <asm/string.h>
31 #include <asm/prom.h>
32 #include <asm/machdep.h>
33 #include <asm/xmon.h>
34 #include <asm/processor.h>
35 #include <asm/pgtable.h>
36 #include <asm/mmu.h>
37 #include <asm/mmu_context.h>
38 #include <asm/cputable.h>
39 #include <asm/rtas.h>
40 #include <asm/sstep.h>
41 #include <asm/irq_regs.h>
42 #include <asm/spu.h>
43 #include <asm/spu_priv1.h>
44 #include <asm/setjmp.h>
45 #include <asm/reg.h>
46 #include <asm/debug.h>
47 #include <asm/hw_breakpoint.h>
48 
49 #ifdef CONFIG_PPC64
50 #include <asm/hvcall.h>
51 #include <asm/paca.h>
52 #endif
53 
54 #if defined(CONFIG_PPC_SPLPAR)
55 #include <asm/plpar_wrappers.h>
56 #else
57 static inline long plapr_set_ciabr(unsigned long ciabr) {return 0; };
58 #endif
59 
60 #include "nonstdio.h"
61 #include "dis-asm.h"
62 
63 #ifdef CONFIG_SMP
64 static cpumask_t cpus_in_xmon = CPU_MASK_NONE;
65 static unsigned long xmon_taken = 1;
66 static int xmon_owner;
67 static int xmon_gate;
68 #else
69 #define xmon_owner 0
70 #endif /* CONFIG_SMP */
71 
72 static unsigned long in_xmon __read_mostly = 0;
73 
74 static unsigned long adrs;
75 static int size = 1;
76 #define MAX_DUMP (128 * 1024)
77 static unsigned long ndump = 64;
78 static unsigned long nidump = 16;
79 static unsigned long ncsum = 4096;
80 static int termch;
81 static char tmpstr[128];
82 
83 static long bus_error_jmp[JMP_BUF_LEN];
84 static int catch_memory_errors;
85 static long *xmon_fault_jmp[NR_CPUS];
86 
87 /* Breakpoint stuff */
88 struct bpt {
89 	unsigned long	address;
90 	unsigned int	instr[2];
91 	atomic_t	ref_count;
92 	int		enabled;
93 	unsigned long	pad;
94 };
95 
96 /* Bits in bpt.enabled */
97 #define BP_CIABR	1
98 #define BP_TRAP		2
99 #define BP_DABR		4
100 
101 #define NBPTS	256
102 static struct bpt bpts[NBPTS];
103 static struct bpt dabr;
104 static struct bpt *iabr;
105 static unsigned bpinstr = 0x7fe00008;	/* trap */
106 
107 #define BP_NUM(bp)	((bp) - bpts + 1)
108 
109 /* Prototypes */
110 static int cmds(struct pt_regs *);
111 static int mread(unsigned long, void *, int);
112 static int mwrite(unsigned long, void *, int);
113 static int handle_fault(struct pt_regs *);
114 static void byterev(unsigned char *, int);
115 static void memex(void);
116 static int bsesc(void);
117 static void dump(void);
118 static void prdump(unsigned long, long);
119 static int ppc_inst_dump(unsigned long, long, int);
120 static void dump_log_buf(void);
121 static void backtrace(struct pt_regs *);
122 static void excprint(struct pt_regs *);
123 static void prregs(struct pt_regs *);
124 static void memops(int);
125 static void memlocate(void);
126 static void memzcan(void);
127 static void memdiffs(unsigned char *, unsigned char *, unsigned, unsigned);
128 int skipbl(void);
129 int scanhex(unsigned long *valp);
130 static void scannl(void);
131 static int hexdigit(int);
132 void getstring(char *, int);
133 static void flush_input(void);
134 static int inchar(void);
135 static void take_input(char *);
136 static unsigned long read_spr(int);
137 static void write_spr(int, unsigned long);
138 static void super_regs(void);
139 static void remove_bpts(void);
140 static void insert_bpts(void);
141 static void remove_cpu_bpts(void);
142 static void insert_cpu_bpts(void);
143 static struct bpt *at_breakpoint(unsigned long pc);
144 static struct bpt *in_breakpoint_table(unsigned long pc, unsigned long *offp);
145 static int  do_step(struct pt_regs *);
146 static void bpt_cmds(void);
147 static void cacheflush(void);
148 static int  cpu_cmd(void);
149 static void csum(void);
150 static void bootcmds(void);
151 static void proccall(void);
152 void dump_segments(void);
153 static void symbol_lookup(void);
154 static void xmon_show_stack(unsigned long sp, unsigned long lr,
155 			    unsigned long pc);
156 static void xmon_print_symbol(unsigned long address, const char *mid,
157 			      const char *after);
158 static const char *getvecname(unsigned long vec);
159 
160 static int do_spu_cmd(void);
161 
162 #ifdef CONFIG_44x
163 static void dump_tlb_44x(void);
164 #endif
165 #ifdef CONFIG_PPC_BOOK3E
166 static void dump_tlb_book3e(void);
167 #endif
168 
169 static int xmon_no_auto_backtrace;
170 
171 extern void xmon_enter(void);
172 extern void xmon_leave(void);
173 
174 #ifdef CONFIG_PPC64
175 #define REG		"%.16lx"
176 #else
177 #define REG		"%.8lx"
178 #endif
179 
180 #ifdef __LITTLE_ENDIAN__
181 #define GETWORD(v)	(((v)[3] << 24) + ((v)[2] << 16) + ((v)[1] << 8) + (v)[0])
182 #else
183 #define GETWORD(v)	(((v)[0] << 24) + ((v)[1] << 16) + ((v)[2] << 8) + (v)[3])
184 #endif
185 
186 #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
187 			 || ('a' <= (c) && (c) <= 'f') \
188 			 || ('A' <= (c) && (c) <= 'F'))
189 #define isalnum(c)	(('0' <= (c) && (c) <= '9') \
190 			 || ('a' <= (c) && (c) <= 'z') \
191 			 || ('A' <= (c) && (c) <= 'Z'))
192 #define isspace(c)	(c == ' ' || c == '\t' || c == 10 || c == 13 || c == 0)
193 
194 static char *help_string = "\
195 Commands:\n\
196   b	show breakpoints\n\
197   bd	set data breakpoint\n\
198   bi	set instruction breakpoint\n\
199   bc	clear breakpoint\n"
200 #ifdef CONFIG_SMP
201   "\
202   c	print cpus stopped in xmon\n\
203   c#	try to switch to cpu number h (in hex)\n"
204 #endif
205   "\
206   C	checksum\n\
207   d	dump bytes\n\
208   di	dump instructions\n\
209   df	dump float values\n\
210   dd	dump double values\n\
211   dl    dump the kernel log buffer\n"
212 #ifdef CONFIG_PPC64
213   "\
214   dp[#]	dump paca for current cpu, or cpu #\n\
215   dpa	dump paca for all possible cpus\n"
216 #endif
217   "\
218   dr	dump stream of raw bytes\n\
219   e	print exception information\n\
220   f	flush cache\n\
221   la	lookup symbol+offset of specified address\n\
222   ls	lookup address of specified symbol\n\
223   m	examine/change memory\n\
224   mm	move a block of memory\n\
225   ms	set a block of memory\n\
226   md	compare two blocks of memory\n\
227   ml	locate a block of memory\n\
228   mz	zero a block of memory\n\
229   mi	show information about memory allocation\n\
230   p 	call a procedure\n\
231   r	print registers\n\
232   s	single step\n"
233 #ifdef CONFIG_SPU_BASE
234 "  ss	stop execution on all spus\n\
235   sr	restore execution on stopped spus\n\
236   sf  #	dump spu fields for spu # (in hex)\n\
237   sd  #	dump spu local store for spu # (in hex)\n\
238   sdi #	disassemble spu local store for spu # (in hex)\n"
239 #endif
240 "  S	print special registers\n\
241   t	print backtrace\n\
242   x	exit monitor and recover\n\
243   X	exit monitor and dont recover\n"
244 #if defined(CONFIG_PPC64) && !defined(CONFIG_PPC_BOOK3E)
245 "  u	dump segment table or SLB\n"
246 #elif defined(CONFIG_PPC_STD_MMU_32)
247 "  u	dump segment registers\n"
248 #elif defined(CONFIG_44x) || defined(CONFIG_PPC_BOOK3E)
249 "  u	dump TLB\n"
250 #endif
251 "  ?	help\n"
252 "  zr	reboot\n\
253   zh	halt\n"
254 ;
255 
256 static struct pt_regs *xmon_regs;
257 
258 static inline void sync(void)
259 {
260 	asm volatile("sync; isync");
261 }
262 
263 static inline void store_inst(void *p)
264 {
265 	asm volatile ("dcbst 0,%0; sync; icbi 0,%0; isync" : : "r" (p));
266 }
267 
268 static inline void cflush(void *p)
269 {
270 	asm volatile ("dcbf 0,%0; icbi 0,%0" : : "r" (p));
271 }
272 
273 static inline void cinval(void *p)
274 {
275 	asm volatile ("dcbi 0,%0; icbi 0,%0" : : "r" (p));
276 }
277 
278 /**
279  * write_ciabr() - write the CIABR SPR
280  * @ciabr:	The value to write.
281  *
282  * This function writes a value to the CIARB register either directly
283  * through mtspr instruction if the kernel is in HV privilege mode or
284  * call a hypervisor function to achieve the same in case the kernel
285  * is in supervisor privilege mode.
286  */
287 static void write_ciabr(unsigned long ciabr)
288 {
289 	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
290 		return;
291 
292 	if (cpu_has_feature(CPU_FTR_HVMODE)) {
293 		mtspr(SPRN_CIABR, ciabr);
294 		return;
295 	}
296 	plapr_set_ciabr(ciabr);
297 }
298 
299 /**
300  * set_ciabr() - set the CIABR
301  * @addr:	The value to set.
302  *
303  * This function sets the correct privilege value into the the HW
304  * breakpoint address before writing it up in the CIABR register.
305  */
306 static void set_ciabr(unsigned long addr)
307 {
308 	addr &= ~CIABR_PRIV;
309 
310 	if (cpu_has_feature(CPU_FTR_HVMODE))
311 		addr |= CIABR_PRIV_HYPER;
312 	else
313 		addr |= CIABR_PRIV_SUPER;
314 	write_ciabr(addr);
315 }
316 
317 /*
318  * Disable surveillance (the service processor watchdog function)
319  * while we are in xmon.
320  * XXX we should re-enable it when we leave. :)
321  */
322 #define SURVEILLANCE_TOKEN	9000
323 
324 static inline void disable_surveillance(void)
325 {
326 #ifdef CONFIG_PPC_PSERIES
327 	/* Since this can't be a module, args should end up below 4GB. */
328 	static struct rtas_args args;
329 
330 	/*
331 	 * At this point we have got all the cpus we can into
332 	 * xmon, so there is hopefully no other cpu calling RTAS
333 	 * at the moment, even though we don't take rtas.lock.
334 	 * If we did try to take rtas.lock there would be a
335 	 * real possibility of deadlock.
336 	 */
337 	args.token = rtas_token("set-indicator");
338 	if (args.token == RTAS_UNKNOWN_SERVICE)
339 		return;
340 	args.nargs = cpu_to_be32(3);
341 	args.nret = cpu_to_be32(1);
342 	args.rets = &args.args[3];
343 	args.args[0] = cpu_to_be32(SURVEILLANCE_TOKEN);
344 	args.args[1] = 0;
345 	args.args[2] = 0;
346 	enter_rtas(__pa(&args));
347 #endif /* CONFIG_PPC_PSERIES */
348 }
349 
350 #ifdef CONFIG_SMP
351 static int xmon_speaker;
352 
353 static void get_output_lock(void)
354 {
355 	int me = smp_processor_id() + 0x100;
356 	int last_speaker = 0, prev;
357 	long timeout;
358 
359 	if (xmon_speaker == me)
360 		return;
361 
362 	for (;;) {
363 		last_speaker = cmpxchg(&xmon_speaker, 0, me);
364 		if (last_speaker == 0)
365 			return;
366 
367 		/*
368 		 * Wait a full second for the lock, we might be on a slow
369 		 * console, but check every 100us.
370 		 */
371 		timeout = 10000;
372 		while (xmon_speaker == last_speaker) {
373 			if (--timeout > 0) {
374 				udelay(100);
375 				continue;
376 			}
377 
378 			/* hostile takeover */
379 			prev = cmpxchg(&xmon_speaker, last_speaker, me);
380 			if (prev == last_speaker)
381 				return;
382 			break;
383 		}
384 	}
385 }
386 
387 static void release_output_lock(void)
388 {
389 	xmon_speaker = 0;
390 }
391 
392 int cpus_are_in_xmon(void)
393 {
394 	return !cpumask_empty(&cpus_in_xmon);
395 }
396 #endif
397 
398 static inline int unrecoverable_excp(struct pt_regs *regs)
399 {
400 #if defined(CONFIG_4xx) || defined(CONFIG_PPC_BOOK3E)
401 	/* We have no MSR_RI bit on 4xx or Book3e, so we simply return false */
402 	return 0;
403 #else
404 	return ((regs->msr & MSR_RI) == 0);
405 #endif
406 }
407 
408 static int xmon_core(struct pt_regs *regs, int fromipi)
409 {
410 	int cmd = 0;
411 	struct bpt *bp;
412 	long recurse_jmp[JMP_BUF_LEN];
413 	unsigned long offset;
414 	unsigned long flags;
415 #ifdef CONFIG_SMP
416 	int cpu;
417 	int secondary;
418 	unsigned long timeout;
419 #endif
420 
421 	local_irq_save(flags);
422 	hard_irq_disable();
423 
424 	bp = in_breakpoint_table(regs->nip, &offset);
425 	if (bp != NULL) {
426 		regs->nip = bp->address + offset;
427 		atomic_dec(&bp->ref_count);
428 	}
429 
430 	remove_cpu_bpts();
431 
432 #ifdef CONFIG_SMP
433 	cpu = smp_processor_id();
434 	if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
435 		get_output_lock();
436 		excprint(regs);
437 		printf("cpu 0x%x: Exception %lx %s in xmon, "
438 		       "returning to main loop\n",
439 		       cpu, regs->trap, getvecname(TRAP(regs)));
440 		release_output_lock();
441 		longjmp(xmon_fault_jmp[cpu], 1);
442 	}
443 
444 	if (setjmp(recurse_jmp) != 0) {
445 		if (!in_xmon || !xmon_gate) {
446 			get_output_lock();
447 			printf("xmon: WARNING: bad recursive fault "
448 			       "on cpu 0x%x\n", cpu);
449 			release_output_lock();
450 			goto waiting;
451 		}
452 		secondary = !(xmon_taken && cpu == xmon_owner);
453 		goto cmdloop;
454 	}
455 
456 	xmon_fault_jmp[cpu] = recurse_jmp;
457 
458 	bp = NULL;
459 	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT))
460 		bp = at_breakpoint(regs->nip);
461 	if (bp || unrecoverable_excp(regs))
462 		fromipi = 0;
463 
464 	if (!fromipi) {
465 		get_output_lock();
466 		excprint(regs);
467 		if (bp) {
468 			printf("cpu 0x%x stopped at breakpoint 0x%lx (",
469 			       cpu, BP_NUM(bp));
470 			xmon_print_symbol(regs->nip, " ", ")\n");
471 		}
472 		if (unrecoverable_excp(regs))
473 			printf("WARNING: exception is not recoverable, "
474 			       "can't continue\n");
475 		release_output_lock();
476 	}
477 
478 	cpumask_set_cpu(cpu, &cpus_in_xmon);
479 
480  waiting:
481 	secondary = 1;
482 	while (secondary && !xmon_gate) {
483 		if (in_xmon == 0) {
484 			if (fromipi)
485 				goto leave;
486 			secondary = test_and_set_bit(0, &in_xmon);
487 		}
488 		barrier();
489 	}
490 
491 	if (!secondary && !xmon_gate) {
492 		/* we are the first cpu to come in */
493 		/* interrupt other cpu(s) */
494 		int ncpus = num_online_cpus();
495 
496 		xmon_owner = cpu;
497 		mb();
498 		if (ncpus > 1) {
499 			smp_send_debugger_break();
500 			/* wait for other cpus to come in */
501 			for (timeout = 100000000; timeout != 0; --timeout) {
502 				if (cpumask_weight(&cpus_in_xmon) >= ncpus)
503 					break;
504 				barrier();
505 			}
506 		}
507 		remove_bpts();
508 		disable_surveillance();
509 		/* for breakpoint or single step, print the current instr. */
510 		if (bp || TRAP(regs) == 0xd00)
511 			ppc_inst_dump(regs->nip, 1, 0);
512 		printf("enter ? for help\n");
513 		mb();
514 		xmon_gate = 1;
515 		barrier();
516 	}
517 
518  cmdloop:
519 	while (in_xmon) {
520 		if (secondary) {
521 			if (cpu == xmon_owner) {
522 				if (!test_and_set_bit(0, &xmon_taken)) {
523 					secondary = 0;
524 					continue;
525 				}
526 				/* missed it */
527 				while (cpu == xmon_owner)
528 					barrier();
529 			}
530 			barrier();
531 		} else {
532 			cmd = cmds(regs);
533 			if (cmd != 0) {
534 				/* exiting xmon */
535 				insert_bpts();
536 				xmon_gate = 0;
537 				wmb();
538 				in_xmon = 0;
539 				break;
540 			}
541 			/* have switched to some other cpu */
542 			secondary = 1;
543 		}
544 	}
545  leave:
546 	cpumask_clear_cpu(cpu, &cpus_in_xmon);
547 	xmon_fault_jmp[cpu] = NULL;
548 #else
549 	/* UP is simple... */
550 	if (in_xmon) {
551 		printf("Exception %lx %s in xmon, returning to main loop\n",
552 		       regs->trap, getvecname(TRAP(regs)));
553 		longjmp(xmon_fault_jmp[0], 1);
554 	}
555 	if (setjmp(recurse_jmp) == 0) {
556 		xmon_fault_jmp[0] = recurse_jmp;
557 		in_xmon = 1;
558 
559 		excprint(regs);
560 		bp = at_breakpoint(regs->nip);
561 		if (bp) {
562 			printf("Stopped at breakpoint %lx (", BP_NUM(bp));
563 			xmon_print_symbol(regs->nip, " ", ")\n");
564 		}
565 		if (unrecoverable_excp(regs))
566 			printf("WARNING: exception is not recoverable, "
567 			       "can't continue\n");
568 		remove_bpts();
569 		disable_surveillance();
570 		/* for breakpoint or single step, print the current instr. */
571 		if (bp || TRAP(regs) == 0xd00)
572 			ppc_inst_dump(regs->nip, 1, 0);
573 		printf("enter ? for help\n");
574 	}
575 
576 	cmd = cmds(regs);
577 
578 	insert_bpts();
579 	in_xmon = 0;
580 #endif
581 
582 #ifdef CONFIG_BOOKE
583 	if (regs->msr & MSR_DE) {
584 		bp = at_breakpoint(regs->nip);
585 		if (bp != NULL) {
586 			regs->nip = (unsigned long) &bp->instr[0];
587 			atomic_inc(&bp->ref_count);
588 		}
589 	}
590 #else
591 	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
592 		bp = at_breakpoint(regs->nip);
593 		if (bp != NULL) {
594 			int stepped = emulate_step(regs, bp->instr[0]);
595 			if (stepped == 0) {
596 				regs->nip = (unsigned long) &bp->instr[0];
597 				atomic_inc(&bp->ref_count);
598 			} else if (stepped < 0) {
599 				printf("Couldn't single-step %s instruction\n",
600 				    (IS_RFID(bp->instr[0])? "rfid": "mtmsrd"));
601 			}
602 		}
603 	}
604 #endif
605 	insert_cpu_bpts();
606 
607 	touch_nmi_watchdog();
608 	local_irq_restore(flags);
609 
610 	return cmd != 'X' && cmd != EOF;
611 }
612 
613 int xmon(struct pt_regs *excp)
614 {
615 	struct pt_regs regs;
616 
617 	if (excp == NULL) {
618 		ppc_save_regs(&regs);
619 		excp = &regs;
620 	}
621 
622 	return xmon_core(excp, 0);
623 }
624 EXPORT_SYMBOL(xmon);
625 
626 irqreturn_t xmon_irq(int irq, void *d)
627 {
628 	unsigned long flags;
629 	local_irq_save(flags);
630 	printf("Keyboard interrupt\n");
631 	xmon(get_irq_regs());
632 	local_irq_restore(flags);
633 	return IRQ_HANDLED;
634 }
635 
636 static int xmon_bpt(struct pt_regs *regs)
637 {
638 	struct bpt *bp;
639 	unsigned long offset;
640 
641 	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
642 		return 0;
643 
644 	/* Are we at the trap at bp->instr[1] for some bp? */
645 	bp = in_breakpoint_table(regs->nip, &offset);
646 	if (bp != NULL && offset == 4) {
647 		regs->nip = bp->address + 4;
648 		atomic_dec(&bp->ref_count);
649 		return 1;
650 	}
651 
652 	/* Are we at a breakpoint? */
653 	bp = at_breakpoint(regs->nip);
654 	if (!bp)
655 		return 0;
656 
657 	xmon_core(regs, 0);
658 
659 	return 1;
660 }
661 
662 static int xmon_sstep(struct pt_regs *regs)
663 {
664 	if (user_mode(regs))
665 		return 0;
666 	xmon_core(regs, 0);
667 	return 1;
668 }
669 
670 static int xmon_break_match(struct pt_regs *regs)
671 {
672 	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
673 		return 0;
674 	if (dabr.enabled == 0)
675 		return 0;
676 	xmon_core(regs, 0);
677 	return 1;
678 }
679 
680 static int xmon_iabr_match(struct pt_regs *regs)
681 {
682 	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
683 		return 0;
684 	if (iabr == NULL)
685 		return 0;
686 	xmon_core(regs, 0);
687 	return 1;
688 }
689 
690 static int xmon_ipi(struct pt_regs *regs)
691 {
692 #ifdef CONFIG_SMP
693 	if (in_xmon && !cpumask_test_cpu(smp_processor_id(), &cpus_in_xmon))
694 		xmon_core(regs, 1);
695 #endif
696 	return 0;
697 }
698 
699 static int xmon_fault_handler(struct pt_regs *regs)
700 {
701 	struct bpt *bp;
702 	unsigned long offset;
703 
704 	if (in_xmon && catch_memory_errors)
705 		handle_fault(regs);	/* doesn't return */
706 
707 	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
708 		bp = in_breakpoint_table(regs->nip, &offset);
709 		if (bp != NULL) {
710 			regs->nip = bp->address + offset;
711 			atomic_dec(&bp->ref_count);
712 		}
713 	}
714 
715 	return 0;
716 }
717 
718 static struct bpt *at_breakpoint(unsigned long pc)
719 {
720 	int i;
721 	struct bpt *bp;
722 
723 	bp = bpts;
724 	for (i = 0; i < NBPTS; ++i, ++bp)
725 		if (bp->enabled && pc == bp->address)
726 			return bp;
727 	return NULL;
728 }
729 
730 static struct bpt *in_breakpoint_table(unsigned long nip, unsigned long *offp)
731 {
732 	unsigned long off;
733 
734 	off = nip - (unsigned long) bpts;
735 	if (off >= sizeof(bpts))
736 		return NULL;
737 	off %= sizeof(struct bpt);
738 	if (off != offsetof(struct bpt, instr[0])
739 	    && off != offsetof(struct bpt, instr[1]))
740 		return NULL;
741 	*offp = off - offsetof(struct bpt, instr[0]);
742 	return (struct bpt *) (nip - off);
743 }
744 
745 static struct bpt *new_breakpoint(unsigned long a)
746 {
747 	struct bpt *bp;
748 
749 	a &= ~3UL;
750 	bp = at_breakpoint(a);
751 	if (bp)
752 		return bp;
753 
754 	for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
755 		if (!bp->enabled && atomic_read(&bp->ref_count) == 0) {
756 			bp->address = a;
757 			bp->instr[1] = bpinstr;
758 			store_inst(&bp->instr[1]);
759 			return bp;
760 		}
761 	}
762 
763 	printf("Sorry, no free breakpoints.  Please clear one first.\n");
764 	return NULL;
765 }
766 
767 static void insert_bpts(void)
768 {
769 	int i;
770 	struct bpt *bp;
771 
772 	bp = bpts;
773 	for (i = 0; i < NBPTS; ++i, ++bp) {
774 		if ((bp->enabled & (BP_TRAP|BP_CIABR)) == 0)
775 			continue;
776 		if (mread(bp->address, &bp->instr[0], 4) != 4) {
777 			printf("Couldn't read instruction at %lx, "
778 			       "disabling breakpoint there\n", bp->address);
779 			bp->enabled = 0;
780 			continue;
781 		}
782 		if (IS_MTMSRD(bp->instr[0]) || IS_RFID(bp->instr[0])) {
783 			printf("Breakpoint at %lx is on an mtmsrd or rfid "
784 			       "instruction, disabling it\n", bp->address);
785 			bp->enabled = 0;
786 			continue;
787 		}
788 		store_inst(&bp->instr[0]);
789 		if (bp->enabled & BP_CIABR)
790 			continue;
791 		if (mwrite(bp->address, &bpinstr, 4) != 4) {
792 			printf("Couldn't write instruction at %lx, "
793 			       "disabling breakpoint there\n", bp->address);
794 			bp->enabled &= ~BP_TRAP;
795 			continue;
796 		}
797 		store_inst((void *)bp->address);
798 	}
799 }
800 
801 static void insert_cpu_bpts(void)
802 {
803 	struct arch_hw_breakpoint brk;
804 
805 	if (dabr.enabled) {
806 		brk.address = dabr.address;
807 		brk.type = (dabr.enabled & HW_BRK_TYPE_DABR) | HW_BRK_TYPE_PRIV_ALL;
808 		brk.len = 8;
809 		__set_breakpoint(&brk);
810 	}
811 
812 	if (iabr)
813 		set_ciabr(iabr->address);
814 }
815 
816 static void remove_bpts(void)
817 {
818 	int i;
819 	struct bpt *bp;
820 	unsigned instr;
821 
822 	bp = bpts;
823 	for (i = 0; i < NBPTS; ++i, ++bp) {
824 		if ((bp->enabled & (BP_TRAP|BP_CIABR)) != BP_TRAP)
825 			continue;
826 		if (mread(bp->address, &instr, 4) == 4
827 		    && instr == bpinstr
828 		    && mwrite(bp->address, &bp->instr, 4) != 4)
829 			printf("Couldn't remove breakpoint at %lx\n",
830 			       bp->address);
831 		else
832 			store_inst((void *)bp->address);
833 	}
834 }
835 
836 static void remove_cpu_bpts(void)
837 {
838 	hw_breakpoint_disable();
839 	write_ciabr(0);
840 }
841 
842 /* Command interpreting routine */
843 static char *last_cmd;
844 
845 static int
846 cmds(struct pt_regs *excp)
847 {
848 	int cmd = 0;
849 
850 	last_cmd = NULL;
851 	xmon_regs = excp;
852 
853 	if (!xmon_no_auto_backtrace) {
854 		xmon_no_auto_backtrace = 1;
855 		xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
856 	}
857 
858 	for(;;) {
859 #ifdef CONFIG_SMP
860 		printf("%x:", smp_processor_id());
861 #endif /* CONFIG_SMP */
862 		printf("mon> ");
863 		flush_input();
864 		termch = 0;
865 		cmd = skipbl();
866 		if( cmd == '\n' ) {
867 			if (last_cmd == NULL)
868 				continue;
869 			take_input(last_cmd);
870 			last_cmd = NULL;
871 			cmd = inchar();
872 		}
873 		switch (cmd) {
874 		case 'm':
875 			cmd = inchar();
876 			switch (cmd) {
877 			case 'm':
878 			case 's':
879 			case 'd':
880 				memops(cmd);
881 				break;
882 			case 'l':
883 				memlocate();
884 				break;
885 			case 'z':
886 				memzcan();
887 				break;
888 			case 'i':
889 				show_mem(0);
890 				break;
891 			default:
892 				termch = cmd;
893 				memex();
894 			}
895 			break;
896 		case 'd':
897 			dump();
898 			break;
899 		case 'l':
900 			symbol_lookup();
901 			break;
902 		case 'r':
903 			prregs(excp);	/* print regs */
904 			break;
905 		case 'e':
906 			excprint(excp);
907 			break;
908 		case 'S':
909 			super_regs();
910 			break;
911 		case 't':
912 			backtrace(excp);
913 			break;
914 		case 'f':
915 			cacheflush();
916 			break;
917 		case 's':
918 			if (do_spu_cmd() == 0)
919 				break;
920 			if (do_step(excp))
921 				return cmd;
922 			break;
923 		case 'x':
924 		case 'X':
925 			return cmd;
926 		case EOF:
927 			printf(" <no input ...>\n");
928 			mdelay(2000);
929 			return cmd;
930 		case '?':
931 			xmon_puts(help_string);
932 			break;
933 		case 'b':
934 			bpt_cmds();
935 			break;
936 		case 'C':
937 			csum();
938 			break;
939 		case 'c':
940 			if (cpu_cmd())
941 				return 0;
942 			break;
943 		case 'z':
944 			bootcmds();
945 			break;
946 		case 'p':
947 			proccall();
948 			break;
949 #ifdef CONFIG_PPC_STD_MMU
950 		case 'u':
951 			dump_segments();
952 			break;
953 #elif defined(CONFIG_44x)
954 		case 'u':
955 			dump_tlb_44x();
956 			break;
957 #elif defined(CONFIG_PPC_BOOK3E)
958 		case 'u':
959 			dump_tlb_book3e();
960 			break;
961 #endif
962 		default:
963 			printf("Unrecognized command: ");
964 			do {
965 				if (' ' < cmd && cmd <= '~')
966 					putchar(cmd);
967 				else
968 					printf("\\x%x", cmd);
969 				cmd = inchar();
970 			} while (cmd != '\n');
971 			printf(" (type ? for help)\n");
972 			break;
973 		}
974 	}
975 }
976 
977 #ifdef CONFIG_BOOKE
978 static int do_step(struct pt_regs *regs)
979 {
980 	regs->msr |= MSR_DE;
981 	mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
982 	return 1;
983 }
984 #else
985 /*
986  * Step a single instruction.
987  * Some instructions we emulate, others we execute with MSR_SE set.
988  */
989 static int do_step(struct pt_regs *regs)
990 {
991 	unsigned int instr;
992 	int stepped;
993 
994 	/* check we are in 64-bit kernel mode, translation enabled */
995 	if ((regs->msr & (MSR_64BIT|MSR_PR|MSR_IR)) == (MSR_64BIT|MSR_IR)) {
996 		if (mread(regs->nip, &instr, 4) == 4) {
997 			stepped = emulate_step(regs, instr);
998 			if (stepped < 0) {
999 				printf("Couldn't single-step %s instruction\n",
1000 				       (IS_RFID(instr)? "rfid": "mtmsrd"));
1001 				return 0;
1002 			}
1003 			if (stepped > 0) {
1004 				regs->trap = 0xd00 | (regs->trap & 1);
1005 				printf("stepped to ");
1006 				xmon_print_symbol(regs->nip, " ", "\n");
1007 				ppc_inst_dump(regs->nip, 1, 0);
1008 				return 0;
1009 			}
1010 		}
1011 	}
1012 	regs->msr |= MSR_SE;
1013 	return 1;
1014 }
1015 #endif
1016 
1017 static void bootcmds(void)
1018 {
1019 	int cmd;
1020 
1021 	cmd = inchar();
1022 	if (cmd == 'r')
1023 		ppc_md.restart(NULL);
1024 	else if (cmd == 'h')
1025 		ppc_md.halt();
1026 	else if (cmd == 'p')
1027 		if (pm_power_off)
1028 			pm_power_off();
1029 }
1030 
1031 static int cpu_cmd(void)
1032 {
1033 #ifdef CONFIG_SMP
1034 	unsigned long cpu, first_cpu, last_cpu;
1035 	int timeout;
1036 
1037 	if (!scanhex(&cpu)) {
1038 		/* print cpus waiting or in xmon */
1039 		printf("cpus stopped:");
1040 		last_cpu = first_cpu = NR_CPUS;
1041 		for_each_possible_cpu(cpu) {
1042 			if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
1043 				if (cpu == last_cpu + 1) {
1044 					last_cpu = cpu;
1045 				} else {
1046 					if (last_cpu != first_cpu)
1047 						printf("-0x%lx", last_cpu);
1048 					last_cpu = first_cpu = cpu;
1049 					printf(" 0x%lx", cpu);
1050 				}
1051 			}
1052 		}
1053 		if (last_cpu != first_cpu)
1054 			printf("-0x%lx", last_cpu);
1055 		printf("\n");
1056 		return 0;
1057 	}
1058 	/* try to switch to cpu specified */
1059 	if (!cpumask_test_cpu(cpu, &cpus_in_xmon)) {
1060 		printf("cpu 0x%x isn't in xmon\n", cpu);
1061 		return 0;
1062 	}
1063 	xmon_taken = 0;
1064 	mb();
1065 	xmon_owner = cpu;
1066 	timeout = 10000000;
1067 	while (!xmon_taken) {
1068 		if (--timeout == 0) {
1069 			if (test_and_set_bit(0, &xmon_taken))
1070 				break;
1071 			/* take control back */
1072 			mb();
1073 			xmon_owner = smp_processor_id();
1074 			printf("cpu 0x%x didn't take control\n", cpu);
1075 			return 0;
1076 		}
1077 		barrier();
1078 	}
1079 	return 1;
1080 #else
1081 	return 0;
1082 #endif /* CONFIG_SMP */
1083 }
1084 
1085 static unsigned short fcstab[256] = {
1086 	0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
1087 	0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
1088 	0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
1089 	0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
1090 	0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
1091 	0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
1092 	0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
1093 	0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
1094 	0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
1095 	0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
1096 	0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
1097 	0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
1098 	0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
1099 	0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
1100 	0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
1101 	0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
1102 	0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
1103 	0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
1104 	0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
1105 	0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
1106 	0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
1107 	0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
1108 	0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
1109 	0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
1110 	0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
1111 	0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
1112 	0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
1113 	0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
1114 	0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
1115 	0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
1116 	0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
1117 	0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
1118 };
1119 
1120 #define FCS(fcs, c)	(((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff])
1121 
1122 static void
1123 csum(void)
1124 {
1125 	unsigned int i;
1126 	unsigned short fcs;
1127 	unsigned char v;
1128 
1129 	if (!scanhex(&adrs))
1130 		return;
1131 	if (!scanhex(&ncsum))
1132 		return;
1133 	fcs = 0xffff;
1134 	for (i = 0; i < ncsum; ++i) {
1135 		if (mread(adrs+i, &v, 1) == 0) {
1136 			printf("csum stopped at "REG"\n", adrs+i);
1137 			break;
1138 		}
1139 		fcs = FCS(fcs, v);
1140 	}
1141 	printf("%x\n", fcs);
1142 }
1143 
1144 /*
1145  * Check if this is a suitable place to put a breakpoint.
1146  */
1147 static long check_bp_loc(unsigned long addr)
1148 {
1149 	unsigned int instr;
1150 
1151 	addr &= ~3;
1152 	if (!is_kernel_addr(addr)) {
1153 		printf("Breakpoints may only be placed at kernel addresses\n");
1154 		return 0;
1155 	}
1156 	if (!mread(addr, &instr, sizeof(instr))) {
1157 		printf("Can't read instruction at address %lx\n", addr);
1158 		return 0;
1159 	}
1160 	if (IS_MTMSRD(instr) || IS_RFID(instr)) {
1161 		printf("Breakpoints may not be placed on mtmsrd or rfid "
1162 		       "instructions\n");
1163 		return 0;
1164 	}
1165 	return 1;
1166 }
1167 
1168 static char *breakpoint_help_string =
1169     "Breakpoint command usage:\n"
1170     "b                show breakpoints\n"
1171     "b <addr> [cnt]   set breakpoint at given instr addr\n"
1172     "bc               clear all breakpoints\n"
1173     "bc <n/addr>      clear breakpoint number n or at addr\n"
1174     "bi <addr> [cnt]  set hardware instr breakpoint (POWER8 only)\n"
1175     "bd <addr> [cnt]  set hardware data breakpoint\n"
1176     "";
1177 
1178 static void
1179 bpt_cmds(void)
1180 {
1181 	int cmd;
1182 	unsigned long a;
1183 	int mode, i;
1184 	struct bpt *bp;
1185 	const char badaddr[] = "Only kernel addresses are permitted "
1186 		"for breakpoints\n";
1187 
1188 	cmd = inchar();
1189 	switch (cmd) {
1190 #ifndef CONFIG_8xx
1191 	case 'd':	/* bd - hardware data breakpoint */
1192 		mode = 7;
1193 		cmd = inchar();
1194 		if (cmd == 'r')
1195 			mode = 5;
1196 		else if (cmd == 'w')
1197 			mode = 6;
1198 		else
1199 			termch = cmd;
1200 		dabr.address = 0;
1201 		dabr.enabled = 0;
1202 		if (scanhex(&dabr.address)) {
1203 			if (!is_kernel_addr(dabr.address)) {
1204 				printf(badaddr);
1205 				break;
1206 			}
1207 			dabr.address &= ~HW_BRK_TYPE_DABR;
1208 			dabr.enabled = mode | BP_DABR;
1209 		}
1210 		break;
1211 
1212 	case 'i':	/* bi - hardware instr breakpoint */
1213 		if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
1214 			printf("Hardware instruction breakpoint "
1215 			       "not supported on this cpu\n");
1216 			break;
1217 		}
1218 		if (iabr) {
1219 			iabr->enabled &= ~BP_CIABR;
1220 			iabr = NULL;
1221 		}
1222 		if (!scanhex(&a))
1223 			break;
1224 		if (!check_bp_loc(a))
1225 			break;
1226 		bp = new_breakpoint(a);
1227 		if (bp != NULL) {
1228 			bp->enabled |= BP_CIABR;
1229 			iabr = bp;
1230 		}
1231 		break;
1232 #endif
1233 
1234 	case 'c':
1235 		if (!scanhex(&a)) {
1236 			/* clear all breakpoints */
1237 			for (i = 0; i < NBPTS; ++i)
1238 				bpts[i].enabled = 0;
1239 			iabr = NULL;
1240 			dabr.enabled = 0;
1241 			printf("All breakpoints cleared\n");
1242 			break;
1243 		}
1244 
1245 		if (a <= NBPTS && a >= 1) {
1246 			/* assume a breakpoint number */
1247 			bp = &bpts[a-1];	/* bp nums are 1 based */
1248 		} else {
1249 			/* assume a breakpoint address */
1250 			bp = at_breakpoint(a);
1251 			if (bp == NULL) {
1252 				printf("No breakpoint at %lx\n", a);
1253 				break;
1254 			}
1255 		}
1256 
1257 		printf("Cleared breakpoint %lx (", BP_NUM(bp));
1258 		xmon_print_symbol(bp->address, " ", ")\n");
1259 		bp->enabled = 0;
1260 		break;
1261 
1262 	default:
1263 		termch = cmd;
1264 		cmd = skipbl();
1265 		if (cmd == '?') {
1266 			printf(breakpoint_help_string);
1267 			break;
1268 		}
1269 		termch = cmd;
1270 		if (!scanhex(&a)) {
1271 			/* print all breakpoints */
1272 			printf("   type            address\n");
1273 			if (dabr.enabled) {
1274 				printf("   data   "REG"  [", dabr.address);
1275 				if (dabr.enabled & 1)
1276 					printf("r");
1277 				if (dabr.enabled & 2)
1278 					printf("w");
1279 				printf("]\n");
1280 			}
1281 			for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
1282 				if (!bp->enabled)
1283 					continue;
1284 				printf("%2x %s   ", BP_NUM(bp),
1285 				    (bp->enabled & BP_CIABR) ? "inst": "trap");
1286 				xmon_print_symbol(bp->address, "  ", "\n");
1287 			}
1288 			break;
1289 		}
1290 
1291 		if (!check_bp_loc(a))
1292 			break;
1293 		bp = new_breakpoint(a);
1294 		if (bp != NULL)
1295 			bp->enabled |= BP_TRAP;
1296 		break;
1297 	}
1298 }
1299 
1300 /* Very cheap human name for vector lookup. */
1301 static
1302 const char *getvecname(unsigned long vec)
1303 {
1304 	char *ret;
1305 
1306 	switch (vec) {
1307 	case 0x100:	ret = "(System Reset)"; break;
1308 	case 0x200:	ret = "(Machine Check)"; break;
1309 	case 0x300:	ret = "(Data Access)"; break;
1310 	case 0x380:	ret = "(Data SLB Access)"; break;
1311 	case 0x400:	ret = "(Instruction Access)"; break;
1312 	case 0x480:	ret = "(Instruction SLB Access)"; break;
1313 	case 0x500:	ret = "(Hardware Interrupt)"; break;
1314 	case 0x600:	ret = "(Alignment)"; break;
1315 	case 0x700:	ret = "(Program Check)"; break;
1316 	case 0x800:	ret = "(FPU Unavailable)"; break;
1317 	case 0x900:	ret = "(Decrementer)"; break;
1318 	case 0x980:	ret = "(Hypervisor Decrementer)"; break;
1319 	case 0xa00:	ret = "(Doorbell)"; break;
1320 	case 0xc00:	ret = "(System Call)"; break;
1321 	case 0xd00:	ret = "(Single Step)"; break;
1322 	case 0xe40:	ret = "(Emulation Assist)"; break;
1323 	case 0xe60:	ret = "(HMI)"; break;
1324 	case 0xe80:	ret = "(Hypervisor Doorbell)"; break;
1325 	case 0xf00:	ret = "(Performance Monitor)"; break;
1326 	case 0xf20:	ret = "(Altivec Unavailable)"; break;
1327 	case 0x1300:	ret = "(Instruction Breakpoint)"; break;
1328 	case 0x1500:	ret = "(Denormalisation)"; break;
1329 	case 0x1700:	ret = "(Altivec Assist)"; break;
1330 	default: ret = "";
1331 	}
1332 	return ret;
1333 }
1334 
1335 static void get_function_bounds(unsigned long pc, unsigned long *startp,
1336 				unsigned long *endp)
1337 {
1338 	unsigned long size, offset;
1339 	const char *name;
1340 
1341 	*startp = *endp = 0;
1342 	if (pc == 0)
1343 		return;
1344 	if (setjmp(bus_error_jmp) == 0) {
1345 		catch_memory_errors = 1;
1346 		sync();
1347 		name = kallsyms_lookup(pc, &size, &offset, NULL, tmpstr);
1348 		if (name != NULL) {
1349 			*startp = pc - offset;
1350 			*endp = pc - offset + size;
1351 		}
1352 		sync();
1353 	}
1354 	catch_memory_errors = 0;
1355 }
1356 
1357 #define LRSAVE_OFFSET		(STACK_FRAME_LR_SAVE * sizeof(unsigned long))
1358 #define MARKER_OFFSET		(STACK_FRAME_MARKER * sizeof(unsigned long))
1359 
1360 static void xmon_show_stack(unsigned long sp, unsigned long lr,
1361 			    unsigned long pc)
1362 {
1363 	int max_to_print = 64;
1364 	unsigned long ip;
1365 	unsigned long newsp;
1366 	unsigned long marker;
1367 	struct pt_regs regs;
1368 
1369 	while (max_to_print--) {
1370 		if (sp < PAGE_OFFSET) {
1371 			if (sp != 0)
1372 				printf("SP (%lx) is in userspace\n", sp);
1373 			break;
1374 		}
1375 
1376 		if (!mread(sp + LRSAVE_OFFSET, &ip, sizeof(unsigned long))
1377 		    || !mread(sp, &newsp, sizeof(unsigned long))) {
1378 			printf("Couldn't read stack frame at %lx\n", sp);
1379 			break;
1380 		}
1381 
1382 		/*
1383 		 * For the first stack frame, try to work out if
1384 		 * LR and/or the saved LR value in the bottommost
1385 		 * stack frame are valid.
1386 		 */
1387 		if ((pc | lr) != 0) {
1388 			unsigned long fnstart, fnend;
1389 			unsigned long nextip;
1390 			int printip = 1;
1391 
1392 			get_function_bounds(pc, &fnstart, &fnend);
1393 			nextip = 0;
1394 			if (newsp > sp)
1395 				mread(newsp + LRSAVE_OFFSET, &nextip,
1396 				      sizeof(unsigned long));
1397 			if (lr == ip) {
1398 				if (lr < PAGE_OFFSET
1399 				    || (fnstart <= lr && lr < fnend))
1400 					printip = 0;
1401 			} else if (lr == nextip) {
1402 				printip = 0;
1403 			} else if (lr >= PAGE_OFFSET
1404 				   && !(fnstart <= lr && lr < fnend)) {
1405 				printf("[link register   ] ");
1406 				xmon_print_symbol(lr, " ", "\n");
1407 			}
1408 			if (printip) {
1409 				printf("["REG"] ", sp);
1410 				xmon_print_symbol(ip, " ", " (unreliable)\n");
1411 			}
1412 			pc = lr = 0;
1413 
1414 		} else {
1415 			printf("["REG"] ", sp);
1416 			xmon_print_symbol(ip, " ", "\n");
1417 		}
1418 
1419 		/* Look for "regshere" marker to see if this is
1420 		   an exception frame. */
1421 		if (mread(sp + MARKER_OFFSET, &marker, sizeof(unsigned long))
1422 		    && marker == STACK_FRAME_REGS_MARKER) {
1423 			if (mread(sp + STACK_FRAME_OVERHEAD, &regs, sizeof(regs))
1424 			    != sizeof(regs)) {
1425 				printf("Couldn't read registers at %lx\n",
1426 				       sp + STACK_FRAME_OVERHEAD);
1427 				break;
1428 			}
1429 			printf("--- Exception: %lx %s at ", regs.trap,
1430 			       getvecname(TRAP(&regs)));
1431 			pc = regs.nip;
1432 			lr = regs.link;
1433 			xmon_print_symbol(pc, " ", "\n");
1434 		}
1435 
1436 		if (newsp == 0)
1437 			break;
1438 
1439 		sp = newsp;
1440 	}
1441 }
1442 
1443 static void backtrace(struct pt_regs *excp)
1444 {
1445 	unsigned long sp;
1446 
1447 	if (scanhex(&sp))
1448 		xmon_show_stack(sp, 0, 0);
1449 	else
1450 		xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
1451 	scannl();
1452 }
1453 
1454 static void print_bug_trap(struct pt_regs *regs)
1455 {
1456 #ifdef CONFIG_BUG
1457 	const struct bug_entry *bug;
1458 	unsigned long addr;
1459 
1460 	if (regs->msr & MSR_PR)
1461 		return;		/* not in kernel */
1462 	addr = regs->nip;	/* address of trap instruction */
1463 	if (addr < PAGE_OFFSET)
1464 		return;
1465 	bug = find_bug(regs->nip);
1466 	if (bug == NULL)
1467 		return;
1468 	if (is_warning_bug(bug))
1469 		return;
1470 
1471 #ifdef CONFIG_DEBUG_BUGVERBOSE
1472 	printf("kernel BUG at %s:%u!\n",
1473 	       bug->file, bug->line);
1474 #else
1475 	printf("kernel BUG at %p!\n", (void *)bug->bug_addr);
1476 #endif
1477 #endif /* CONFIG_BUG */
1478 }
1479 
1480 static void excprint(struct pt_regs *fp)
1481 {
1482 	unsigned long trap;
1483 
1484 #ifdef CONFIG_SMP
1485 	printf("cpu 0x%x: ", smp_processor_id());
1486 #endif /* CONFIG_SMP */
1487 
1488 	trap = TRAP(fp);
1489 	printf("Vector: %lx %s at [%lx]\n", fp->trap, getvecname(trap), fp);
1490 	printf("    pc: ");
1491 	xmon_print_symbol(fp->nip, ": ", "\n");
1492 
1493 	printf("    lr: ", fp->link);
1494 	xmon_print_symbol(fp->link, ": ", "\n");
1495 
1496 	printf("    sp: %lx\n", fp->gpr[1]);
1497 	printf("   msr: %lx\n", fp->msr);
1498 
1499 	if (trap == 0x300 || trap == 0x380 || trap == 0x600 || trap == 0x200) {
1500 		printf("   dar: %lx\n", fp->dar);
1501 		if (trap != 0x380)
1502 			printf(" dsisr: %lx\n", fp->dsisr);
1503 	}
1504 
1505 	printf("  current = 0x%lx\n", current);
1506 #ifdef CONFIG_PPC64
1507 	printf("  paca    = 0x%lx\t softe: %d\t irq_happened: 0x%02x\n",
1508 	       local_paca, local_paca->soft_enabled, local_paca->irq_happened);
1509 #endif
1510 	if (current) {
1511 		printf("    pid   = %ld, comm = %s\n",
1512 		       current->pid, current->comm);
1513 	}
1514 
1515 	if (trap == 0x700)
1516 		print_bug_trap(fp);
1517 }
1518 
1519 static void prregs(struct pt_regs *fp)
1520 {
1521 	int n, trap;
1522 	unsigned long base;
1523 	struct pt_regs regs;
1524 
1525 	if (scanhex(&base)) {
1526 		if (setjmp(bus_error_jmp) == 0) {
1527 			catch_memory_errors = 1;
1528 			sync();
1529 			regs = *(struct pt_regs *)base;
1530 			sync();
1531 			__delay(200);
1532 		} else {
1533 			catch_memory_errors = 0;
1534 			printf("*** Error reading registers from "REG"\n",
1535 			       base);
1536 			return;
1537 		}
1538 		catch_memory_errors = 0;
1539 		fp = &regs;
1540 	}
1541 
1542 #ifdef CONFIG_PPC64
1543 	if (FULL_REGS(fp)) {
1544 		for (n = 0; n < 16; ++n)
1545 			printf("R%.2ld = "REG"   R%.2ld = "REG"\n",
1546 			       n, fp->gpr[n], n+16, fp->gpr[n+16]);
1547 	} else {
1548 		for (n = 0; n < 7; ++n)
1549 			printf("R%.2ld = "REG"   R%.2ld = "REG"\n",
1550 			       n, fp->gpr[n], n+7, fp->gpr[n+7]);
1551 	}
1552 #else
1553 	for (n = 0; n < 32; ++n) {
1554 		printf("R%.2d = %.8x%s", n, fp->gpr[n],
1555 		       (n & 3) == 3? "\n": "   ");
1556 		if (n == 12 && !FULL_REGS(fp)) {
1557 			printf("\n");
1558 			break;
1559 		}
1560 	}
1561 #endif
1562 	printf("pc  = ");
1563 	xmon_print_symbol(fp->nip, " ", "\n");
1564 	if (TRAP(fp) != 0xc00 && cpu_has_feature(CPU_FTR_CFAR)) {
1565 		printf("cfar= ");
1566 		xmon_print_symbol(fp->orig_gpr3, " ", "\n");
1567 	}
1568 	printf("lr  = ");
1569 	xmon_print_symbol(fp->link, " ", "\n");
1570 	printf("msr = "REG"   cr  = %.8lx\n", fp->msr, fp->ccr);
1571 	printf("ctr = "REG"   xer = "REG"   trap = %4lx\n",
1572 	       fp->ctr, fp->xer, fp->trap);
1573 	trap = TRAP(fp);
1574 	if (trap == 0x300 || trap == 0x380 || trap == 0x600)
1575 		printf("dar = "REG"   dsisr = %.8lx\n", fp->dar, fp->dsisr);
1576 }
1577 
1578 static void cacheflush(void)
1579 {
1580 	int cmd;
1581 	unsigned long nflush;
1582 
1583 	cmd = inchar();
1584 	if (cmd != 'i')
1585 		termch = cmd;
1586 	scanhex((void *)&adrs);
1587 	if (termch != '\n')
1588 		termch = 0;
1589 	nflush = 1;
1590 	scanhex(&nflush);
1591 	nflush = (nflush + L1_CACHE_BYTES - 1) / L1_CACHE_BYTES;
1592 	if (setjmp(bus_error_jmp) == 0) {
1593 		catch_memory_errors = 1;
1594 		sync();
1595 
1596 		if (cmd != 'i') {
1597 			for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
1598 				cflush((void *) adrs);
1599 		} else {
1600 			for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
1601 				cinval((void *) adrs);
1602 		}
1603 		sync();
1604 		/* wait a little while to see if we get a machine check */
1605 		__delay(200);
1606 	}
1607 	catch_memory_errors = 0;
1608 }
1609 
1610 static unsigned long
1611 read_spr(int n)
1612 {
1613 	unsigned int instrs[2];
1614 	unsigned long (*code)(void);
1615 	unsigned long ret = -1UL;
1616 #ifdef CONFIG_PPC64
1617 	unsigned long opd[3];
1618 
1619 	opd[0] = (unsigned long)instrs;
1620 	opd[1] = 0;
1621 	opd[2] = 0;
1622 	code = (unsigned long (*)(void)) opd;
1623 #else
1624 	code = (unsigned long (*)(void)) instrs;
1625 #endif
1626 
1627 	/* mfspr r3,n; blr */
1628 	instrs[0] = 0x7c6002a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
1629 	instrs[1] = 0x4e800020;
1630 	store_inst(instrs);
1631 	store_inst(instrs+1);
1632 
1633 	if (setjmp(bus_error_jmp) == 0) {
1634 		catch_memory_errors = 1;
1635 		sync();
1636 
1637 		ret = code();
1638 
1639 		sync();
1640 		/* wait a little while to see if we get a machine check */
1641 		__delay(200);
1642 		n = size;
1643 	}
1644 
1645 	return ret;
1646 }
1647 
1648 static void
1649 write_spr(int n, unsigned long val)
1650 {
1651 	unsigned int instrs[2];
1652 	unsigned long (*code)(unsigned long);
1653 #ifdef CONFIG_PPC64
1654 	unsigned long opd[3];
1655 
1656 	opd[0] = (unsigned long)instrs;
1657 	opd[1] = 0;
1658 	opd[2] = 0;
1659 	code = (unsigned long (*)(unsigned long)) opd;
1660 #else
1661 	code = (unsigned long (*)(unsigned long)) instrs;
1662 #endif
1663 
1664 	instrs[0] = 0x7c6003a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
1665 	instrs[1] = 0x4e800020;
1666 	store_inst(instrs);
1667 	store_inst(instrs+1);
1668 
1669 	if (setjmp(bus_error_jmp) == 0) {
1670 		catch_memory_errors = 1;
1671 		sync();
1672 
1673 		code(val);
1674 
1675 		sync();
1676 		/* wait a little while to see if we get a machine check */
1677 		__delay(200);
1678 		n = size;
1679 	}
1680 }
1681 
1682 static unsigned long regno;
1683 extern char exc_prolog;
1684 extern char dec_exc;
1685 
1686 static void super_regs(void)
1687 {
1688 	int cmd;
1689 	unsigned long val;
1690 
1691 	cmd = skipbl();
1692 	if (cmd == '\n') {
1693 		unsigned long sp, toc;
1694 		asm("mr %0,1" : "=r" (sp) :);
1695 		asm("mr %0,2" : "=r" (toc) :);
1696 
1697 		printf("msr  = "REG"  sprg0= "REG"\n",
1698 		       mfmsr(), mfspr(SPRN_SPRG0));
1699 		printf("pvr  = "REG"  sprg1= "REG"\n",
1700 		       mfspr(SPRN_PVR), mfspr(SPRN_SPRG1));
1701 		printf("dec  = "REG"  sprg2= "REG"\n",
1702 		       mfspr(SPRN_DEC), mfspr(SPRN_SPRG2));
1703 		printf("sp   = "REG"  sprg3= "REG"\n", sp, mfspr(SPRN_SPRG3));
1704 		printf("toc  = "REG"  dar  = "REG"\n", toc, mfspr(SPRN_DAR));
1705 
1706 		return;
1707 	}
1708 
1709 	scanhex(&regno);
1710 	switch (cmd) {
1711 	case 'w':
1712 		val = read_spr(regno);
1713 		scanhex(&val);
1714 		write_spr(regno, val);
1715 		/* fall through */
1716 	case 'r':
1717 		printf("spr %lx = %lx\n", regno, read_spr(regno));
1718 		break;
1719 	}
1720 	scannl();
1721 }
1722 
1723 /*
1724  * Stuff for reading and writing memory safely
1725  */
1726 static int
1727 mread(unsigned long adrs, void *buf, int size)
1728 {
1729 	volatile int n;
1730 	char *p, *q;
1731 
1732 	n = 0;
1733 	if (setjmp(bus_error_jmp) == 0) {
1734 		catch_memory_errors = 1;
1735 		sync();
1736 		p = (char *)adrs;
1737 		q = (char *)buf;
1738 		switch (size) {
1739 		case 2:
1740 			*(u16 *)q = *(u16 *)p;
1741 			break;
1742 		case 4:
1743 			*(u32 *)q = *(u32 *)p;
1744 			break;
1745 		case 8:
1746 			*(u64 *)q = *(u64 *)p;
1747 			break;
1748 		default:
1749 			for( ; n < size; ++n) {
1750 				*q++ = *p++;
1751 				sync();
1752 			}
1753 		}
1754 		sync();
1755 		/* wait a little while to see if we get a machine check */
1756 		__delay(200);
1757 		n = size;
1758 	}
1759 	catch_memory_errors = 0;
1760 	return n;
1761 }
1762 
1763 static int
1764 mwrite(unsigned long adrs, void *buf, int size)
1765 {
1766 	volatile int n;
1767 	char *p, *q;
1768 
1769 	n = 0;
1770 	if (setjmp(bus_error_jmp) == 0) {
1771 		catch_memory_errors = 1;
1772 		sync();
1773 		p = (char *) adrs;
1774 		q = (char *) buf;
1775 		switch (size) {
1776 		case 2:
1777 			*(u16 *)p = *(u16 *)q;
1778 			break;
1779 		case 4:
1780 			*(u32 *)p = *(u32 *)q;
1781 			break;
1782 		case 8:
1783 			*(u64 *)p = *(u64 *)q;
1784 			break;
1785 		default:
1786 			for ( ; n < size; ++n) {
1787 				*p++ = *q++;
1788 				sync();
1789 			}
1790 		}
1791 		sync();
1792 		/* wait a little while to see if we get a machine check */
1793 		__delay(200);
1794 		n = size;
1795 	} else {
1796 		printf("*** Error writing address "REG"\n", adrs + n);
1797 	}
1798 	catch_memory_errors = 0;
1799 	return n;
1800 }
1801 
1802 static int fault_type;
1803 static int fault_except;
1804 static char *fault_chars[] = { "--", "**", "##" };
1805 
1806 static int handle_fault(struct pt_regs *regs)
1807 {
1808 	fault_except = TRAP(regs);
1809 	switch (TRAP(regs)) {
1810 	case 0x200:
1811 		fault_type = 0;
1812 		break;
1813 	case 0x300:
1814 	case 0x380:
1815 		fault_type = 1;
1816 		break;
1817 	default:
1818 		fault_type = 2;
1819 	}
1820 
1821 	longjmp(bus_error_jmp, 1);
1822 
1823 	return 0;
1824 }
1825 
1826 #define SWAP(a, b, t)	((t) = (a), (a) = (b), (b) = (t))
1827 
1828 static void
1829 byterev(unsigned char *val, int size)
1830 {
1831 	int t;
1832 
1833 	switch (size) {
1834 	case 2:
1835 		SWAP(val[0], val[1], t);
1836 		break;
1837 	case 4:
1838 		SWAP(val[0], val[3], t);
1839 		SWAP(val[1], val[2], t);
1840 		break;
1841 	case 8: /* is there really any use for this? */
1842 		SWAP(val[0], val[7], t);
1843 		SWAP(val[1], val[6], t);
1844 		SWAP(val[2], val[5], t);
1845 		SWAP(val[3], val[4], t);
1846 		break;
1847 	}
1848 }
1849 
1850 static int brev;
1851 static int mnoread;
1852 
1853 static char *memex_help_string =
1854     "Memory examine command usage:\n"
1855     "m [addr] [flags] examine/change memory\n"
1856     "  addr is optional.  will start where left off.\n"
1857     "  flags may include chars from this set:\n"
1858     "    b   modify by bytes (default)\n"
1859     "    w   modify by words (2 byte)\n"
1860     "    l   modify by longs (4 byte)\n"
1861     "    d   modify by doubleword (8 byte)\n"
1862     "    r   toggle reverse byte order mode\n"
1863     "    n   do not read memory (for i/o spaces)\n"
1864     "    .   ok to read (default)\n"
1865     "NOTE: flags are saved as defaults\n"
1866     "";
1867 
1868 static char *memex_subcmd_help_string =
1869     "Memory examine subcommands:\n"
1870     "  hexval   write this val to current location\n"
1871     "  'string' write chars from string to this location\n"
1872     "  '        increment address\n"
1873     "  ^        decrement address\n"
1874     "  /        increment addr by 0x10.  //=0x100, ///=0x1000, etc\n"
1875     "  \\        decrement addr by 0x10.  \\\\=0x100, \\\\\\=0x1000, etc\n"
1876     "  `        clear no-read flag\n"
1877     "  ;        stay at this addr\n"
1878     "  v        change to byte mode\n"
1879     "  w        change to word (2 byte) mode\n"
1880     "  l        change to long (4 byte) mode\n"
1881     "  u        change to doubleword (8 byte) mode\n"
1882     "  m addr   change current addr\n"
1883     "  n        toggle no-read flag\n"
1884     "  r        toggle byte reverse flag\n"
1885     "  < count  back up count bytes\n"
1886     "  > count  skip forward count bytes\n"
1887     "  x        exit this mode\n"
1888     "";
1889 
1890 static void
1891 memex(void)
1892 {
1893 	int cmd, inc, i, nslash;
1894 	unsigned long n;
1895 	unsigned char val[16];
1896 
1897 	scanhex((void *)&adrs);
1898 	cmd = skipbl();
1899 	if (cmd == '?') {
1900 		printf(memex_help_string);
1901 		return;
1902 	} else {
1903 		termch = cmd;
1904 	}
1905 	last_cmd = "m\n";
1906 	while ((cmd = skipbl()) != '\n') {
1907 		switch( cmd ){
1908 		case 'b':	size = 1;	break;
1909 		case 'w':	size = 2;	break;
1910 		case 'l':	size = 4;	break;
1911 		case 'd':	size = 8;	break;
1912 		case 'r': 	brev = !brev;	break;
1913 		case 'n':	mnoread = 1;	break;
1914 		case '.':	mnoread = 0;	break;
1915 		}
1916 	}
1917 	if( size <= 0 )
1918 		size = 1;
1919 	else if( size > 8 )
1920 		size = 8;
1921 	for(;;){
1922 		if (!mnoread)
1923 			n = mread(adrs, val, size);
1924 		printf(REG"%c", adrs, brev? 'r': ' ');
1925 		if (!mnoread) {
1926 			if (brev)
1927 				byterev(val, size);
1928 			putchar(' ');
1929 			for (i = 0; i < n; ++i)
1930 				printf("%.2x", val[i]);
1931 			for (; i < size; ++i)
1932 				printf("%s", fault_chars[fault_type]);
1933 		}
1934 		putchar(' ');
1935 		inc = size;
1936 		nslash = 0;
1937 		for(;;){
1938 			if( scanhex(&n) ){
1939 				for (i = 0; i < size; ++i)
1940 					val[i] = n >> (i * 8);
1941 				if (!brev)
1942 					byterev(val, size);
1943 				mwrite(adrs, val, size);
1944 				inc = size;
1945 			}
1946 			cmd = skipbl();
1947 			if (cmd == '\n')
1948 				break;
1949 			inc = 0;
1950 			switch (cmd) {
1951 			case '\'':
1952 				for(;;){
1953 					n = inchar();
1954 					if( n == '\\' )
1955 						n = bsesc();
1956 					else if( n == '\'' )
1957 						break;
1958 					for (i = 0; i < size; ++i)
1959 						val[i] = n >> (i * 8);
1960 					if (!brev)
1961 						byterev(val, size);
1962 					mwrite(adrs, val, size);
1963 					adrs += size;
1964 				}
1965 				adrs -= size;
1966 				inc = size;
1967 				break;
1968 			case ',':
1969 				adrs += size;
1970 				break;
1971 			case '.':
1972 				mnoread = 0;
1973 				break;
1974 			case ';':
1975 				break;
1976 			case 'x':
1977 			case EOF:
1978 				scannl();
1979 				return;
1980 			case 'b':
1981 			case 'v':
1982 				size = 1;
1983 				break;
1984 			case 'w':
1985 				size = 2;
1986 				break;
1987 			case 'l':
1988 				size = 4;
1989 				break;
1990 			case 'u':
1991 				size = 8;
1992 				break;
1993 			case '^':
1994 				adrs -= size;
1995 				break;
1996 				break;
1997 			case '/':
1998 				if (nslash > 0)
1999 					adrs -= 1 << nslash;
2000 				else
2001 					nslash = 0;
2002 				nslash += 4;
2003 				adrs += 1 << nslash;
2004 				break;
2005 			case '\\':
2006 				if (nslash < 0)
2007 					adrs += 1 << -nslash;
2008 				else
2009 					nslash = 0;
2010 				nslash -= 4;
2011 				adrs -= 1 << -nslash;
2012 				break;
2013 			case 'm':
2014 				scanhex((void *)&adrs);
2015 				break;
2016 			case 'n':
2017 				mnoread = 1;
2018 				break;
2019 			case 'r':
2020 				brev = !brev;
2021 				break;
2022 			case '<':
2023 				n = size;
2024 				scanhex(&n);
2025 				adrs -= n;
2026 				break;
2027 			case '>':
2028 				n = size;
2029 				scanhex(&n);
2030 				adrs += n;
2031 				break;
2032 			case '?':
2033 				printf(memex_subcmd_help_string);
2034 				break;
2035 			}
2036 		}
2037 		adrs += inc;
2038 	}
2039 }
2040 
2041 static int
2042 bsesc(void)
2043 {
2044 	int c;
2045 
2046 	c = inchar();
2047 	switch( c ){
2048 	case 'n':	c = '\n';	break;
2049 	case 'r':	c = '\r';	break;
2050 	case 'b':	c = '\b';	break;
2051 	case 't':	c = '\t';	break;
2052 	}
2053 	return c;
2054 }
2055 
2056 static void xmon_rawdump (unsigned long adrs, long ndump)
2057 {
2058 	long n, m, r, nr;
2059 	unsigned char temp[16];
2060 
2061 	for (n = ndump; n > 0;) {
2062 		r = n < 16? n: 16;
2063 		nr = mread(adrs, temp, r);
2064 		adrs += nr;
2065 		for (m = 0; m < r; ++m) {
2066 			if (m < nr)
2067 				printf("%.2x", temp[m]);
2068 			else
2069 				printf("%s", fault_chars[fault_type]);
2070 		}
2071 		n -= r;
2072 		if (nr < r)
2073 			break;
2074 	}
2075 	printf("\n");
2076 }
2077 
2078 #ifdef CONFIG_PPC64
2079 static void dump_one_paca(int cpu)
2080 {
2081 	struct paca_struct *p;
2082 
2083 	if (setjmp(bus_error_jmp) != 0) {
2084 		printf("*** Error dumping paca for cpu 0x%x!\n", cpu);
2085 		return;
2086 	}
2087 
2088 	catch_memory_errors = 1;
2089 	sync();
2090 
2091 	p = &paca[cpu];
2092 
2093 	printf("paca for cpu 0x%x @ %p:\n", cpu, p);
2094 
2095 	printf(" %-*s = %s\n", 16, "possible", cpu_possible(cpu) ? "yes" : "no");
2096 	printf(" %-*s = %s\n", 16, "present", cpu_present(cpu) ? "yes" : "no");
2097 	printf(" %-*s = %s\n", 16, "online", cpu_online(cpu) ? "yes" : "no");
2098 
2099 #define DUMP(paca, name, format) \
2100 	printf(" %-*s = %#-*"format"\t(0x%lx)\n", 16, #name, 18, paca->name, \
2101 		offsetof(struct paca_struct, name));
2102 
2103 	DUMP(p, lock_token, "x");
2104 	DUMP(p, paca_index, "x");
2105 	DUMP(p, kernel_toc, "lx");
2106 	DUMP(p, kernelbase, "lx");
2107 	DUMP(p, kernel_msr, "lx");
2108 	DUMP(p, emergency_sp, "p");
2109 #ifdef CONFIG_PPC_BOOK3S_64
2110 	DUMP(p, mc_emergency_sp, "p");
2111 	DUMP(p, in_mce, "x");
2112 #endif
2113 	DUMP(p, data_offset, "lx");
2114 	DUMP(p, hw_cpu_id, "x");
2115 	DUMP(p, cpu_start, "x");
2116 	DUMP(p, kexec_state, "x");
2117 	DUMP(p, __current, "p");
2118 	DUMP(p, kstack, "lx");
2119 	DUMP(p, stab_rr, "lx");
2120 	DUMP(p, saved_r1, "lx");
2121 	DUMP(p, trap_save, "x");
2122 	DUMP(p, soft_enabled, "x");
2123 	DUMP(p, irq_happened, "x");
2124 	DUMP(p, io_sync, "x");
2125 	DUMP(p, irq_work_pending, "x");
2126 	DUMP(p, nap_state_lost, "x");
2127 
2128 #undef DUMP
2129 
2130 	catch_memory_errors = 0;
2131 	sync();
2132 }
2133 
2134 static void dump_all_pacas(void)
2135 {
2136 	int cpu;
2137 
2138 	if (num_possible_cpus() == 0) {
2139 		printf("No possible cpus, use 'dp #' to dump individual cpus\n");
2140 		return;
2141 	}
2142 
2143 	for_each_possible_cpu(cpu)
2144 		dump_one_paca(cpu);
2145 }
2146 
2147 static void dump_pacas(void)
2148 {
2149 	unsigned long num;
2150 	int c;
2151 
2152 	c = inchar();
2153 	if (c == 'a') {
2154 		dump_all_pacas();
2155 		return;
2156 	}
2157 
2158 	termch = c;	/* Put c back, it wasn't 'a' */
2159 
2160 	if (scanhex(&num))
2161 		dump_one_paca(num);
2162 	else
2163 		dump_one_paca(xmon_owner);
2164 }
2165 #endif
2166 
2167 #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
2168 			 || ('a' <= (c) && (c) <= 'f') \
2169 			 || ('A' <= (c) && (c) <= 'F'))
2170 static void
2171 dump(void)
2172 {
2173 	int c;
2174 
2175 	c = inchar();
2176 
2177 #ifdef CONFIG_PPC64
2178 	if (c == 'p') {
2179 		dump_pacas();
2180 		return;
2181 	}
2182 #endif
2183 
2184 	if ((isxdigit(c) && c != 'f' && c != 'd') || c == '\n')
2185 		termch = c;
2186 	scanhex((void *)&adrs);
2187 	if (termch != '\n')
2188 		termch = 0;
2189 	if (c == 'i') {
2190 		scanhex(&nidump);
2191 		if (nidump == 0)
2192 			nidump = 16;
2193 		else if (nidump > MAX_DUMP)
2194 			nidump = MAX_DUMP;
2195 		adrs += ppc_inst_dump(adrs, nidump, 1);
2196 		last_cmd = "di\n";
2197 	} else if (c == 'l') {
2198 		dump_log_buf();
2199 	} else if (c == 'r') {
2200 		scanhex(&ndump);
2201 		if (ndump == 0)
2202 			ndump = 64;
2203 		xmon_rawdump(adrs, ndump);
2204 		adrs += ndump;
2205 		last_cmd = "dr\n";
2206 	} else {
2207 		scanhex(&ndump);
2208 		if (ndump == 0)
2209 			ndump = 64;
2210 		else if (ndump > MAX_DUMP)
2211 			ndump = MAX_DUMP;
2212 		prdump(adrs, ndump);
2213 		adrs += ndump;
2214 		last_cmd = "d\n";
2215 	}
2216 }
2217 
2218 static void
2219 prdump(unsigned long adrs, long ndump)
2220 {
2221 	long n, m, c, r, nr;
2222 	unsigned char temp[16];
2223 
2224 	for (n = ndump; n > 0;) {
2225 		printf(REG, adrs);
2226 		putchar(' ');
2227 		r = n < 16? n: 16;
2228 		nr = mread(adrs, temp, r);
2229 		adrs += nr;
2230 		for (m = 0; m < r; ++m) {
2231 			if ((m & (sizeof(long) - 1)) == 0 && m > 0)
2232 				putchar(' ');
2233 			if (m < nr)
2234 				printf("%.2x", temp[m]);
2235 			else
2236 				printf("%s", fault_chars[fault_type]);
2237 		}
2238 		for (; m < 16; ++m) {
2239 			if ((m & (sizeof(long) - 1)) == 0)
2240 				putchar(' ');
2241 			printf("  ");
2242 		}
2243 		printf("  |");
2244 		for (m = 0; m < r; ++m) {
2245 			if (m < nr) {
2246 				c = temp[m];
2247 				putchar(' ' <= c && c <= '~'? c: '.');
2248 			} else
2249 				putchar(' ');
2250 		}
2251 		n -= r;
2252 		for (; m < 16; ++m)
2253 			putchar(' ');
2254 		printf("|\n");
2255 		if (nr < r)
2256 			break;
2257 	}
2258 }
2259 
2260 typedef int (*instruction_dump_func)(unsigned long inst, unsigned long addr);
2261 
2262 static int
2263 generic_inst_dump(unsigned long adr, long count, int praddr,
2264 			instruction_dump_func dump_func)
2265 {
2266 	int nr, dotted;
2267 	unsigned long first_adr;
2268 	unsigned long inst, last_inst = 0;
2269 	unsigned char val[4];
2270 
2271 	dotted = 0;
2272 	for (first_adr = adr; count > 0; --count, adr += 4) {
2273 		nr = mread(adr, val, 4);
2274 		if (nr == 0) {
2275 			if (praddr) {
2276 				const char *x = fault_chars[fault_type];
2277 				printf(REG"  %s%s%s%s\n", adr, x, x, x, x);
2278 			}
2279 			break;
2280 		}
2281 		inst = GETWORD(val);
2282 		if (adr > first_adr && inst == last_inst) {
2283 			if (!dotted) {
2284 				printf(" ...\n");
2285 				dotted = 1;
2286 			}
2287 			continue;
2288 		}
2289 		dotted = 0;
2290 		last_inst = inst;
2291 		if (praddr)
2292 			printf(REG"  %.8x", adr, inst);
2293 		printf("\t");
2294 		dump_func(inst, adr);
2295 		printf("\n");
2296 	}
2297 	return adr - first_adr;
2298 }
2299 
2300 static int
2301 ppc_inst_dump(unsigned long adr, long count, int praddr)
2302 {
2303 	return generic_inst_dump(adr, count, praddr, print_insn_powerpc);
2304 }
2305 
2306 void
2307 print_address(unsigned long addr)
2308 {
2309 	xmon_print_symbol(addr, "\t# ", "");
2310 }
2311 
2312 void
2313 dump_log_buf(void)
2314 {
2315 	struct kmsg_dumper dumper = { .active = 1 };
2316 	unsigned char buf[128];
2317 	size_t len;
2318 
2319 	if (setjmp(bus_error_jmp) != 0) {
2320 		printf("Error dumping printk buffer!\n");
2321 		return;
2322 	}
2323 
2324 	catch_memory_errors = 1;
2325 	sync();
2326 
2327 	kmsg_dump_rewind_nolock(&dumper);
2328 	while (kmsg_dump_get_line_nolock(&dumper, false, buf, sizeof(buf), &len)) {
2329 		buf[len] = '\0';
2330 		printf("%s", buf);
2331 	}
2332 
2333 	sync();
2334 	/* wait a little while to see if we get a machine check */
2335 	__delay(200);
2336 	catch_memory_errors = 0;
2337 }
2338 
2339 /*
2340  * Memory operations - move, set, print differences
2341  */
2342 static unsigned long mdest;		/* destination address */
2343 static unsigned long msrc;		/* source address */
2344 static unsigned long mval;		/* byte value to set memory to */
2345 static unsigned long mcount;		/* # bytes to affect */
2346 static unsigned long mdiffs;		/* max # differences to print */
2347 
2348 static void
2349 memops(int cmd)
2350 {
2351 	scanhex((void *)&mdest);
2352 	if( termch != '\n' )
2353 		termch = 0;
2354 	scanhex((void *)(cmd == 's'? &mval: &msrc));
2355 	if( termch != '\n' )
2356 		termch = 0;
2357 	scanhex((void *)&mcount);
2358 	switch( cmd ){
2359 	case 'm':
2360 		memmove((void *)mdest, (void *)msrc, mcount);
2361 		break;
2362 	case 's':
2363 		memset((void *)mdest, mval, mcount);
2364 		break;
2365 	case 'd':
2366 		if( termch != '\n' )
2367 			termch = 0;
2368 		scanhex((void *)&mdiffs);
2369 		memdiffs((unsigned char *)mdest, (unsigned char *)msrc, mcount, mdiffs);
2370 		break;
2371 	}
2372 }
2373 
2374 static void
2375 memdiffs(unsigned char *p1, unsigned char *p2, unsigned nb, unsigned maxpr)
2376 {
2377 	unsigned n, prt;
2378 
2379 	prt = 0;
2380 	for( n = nb; n > 0; --n )
2381 		if( *p1++ != *p2++ )
2382 			if( ++prt <= maxpr )
2383 				printf("%.16x %.2x # %.16x %.2x\n", p1 - 1,
2384 					p1[-1], p2 - 1, p2[-1]);
2385 	if( prt > maxpr )
2386 		printf("Total of %d differences\n", prt);
2387 }
2388 
2389 static unsigned mend;
2390 static unsigned mask;
2391 
2392 static void
2393 memlocate(void)
2394 {
2395 	unsigned a, n;
2396 	unsigned char val[4];
2397 
2398 	last_cmd = "ml";
2399 	scanhex((void *)&mdest);
2400 	if (termch != '\n') {
2401 		termch = 0;
2402 		scanhex((void *)&mend);
2403 		if (termch != '\n') {
2404 			termch = 0;
2405 			scanhex((void *)&mval);
2406 			mask = ~0;
2407 			if (termch != '\n') termch = 0;
2408 			scanhex((void *)&mask);
2409 		}
2410 	}
2411 	n = 0;
2412 	for (a = mdest; a < mend; a += 4) {
2413 		if (mread(a, val, 4) == 4
2414 			&& ((GETWORD(val) ^ mval) & mask) == 0) {
2415 			printf("%.16x:  %.16x\n", a, GETWORD(val));
2416 			if (++n >= 10)
2417 				break;
2418 		}
2419 	}
2420 }
2421 
2422 static unsigned long mskip = 0x1000;
2423 static unsigned long mlim = 0xffffffff;
2424 
2425 static void
2426 memzcan(void)
2427 {
2428 	unsigned char v;
2429 	unsigned a;
2430 	int ok, ook;
2431 
2432 	scanhex(&mdest);
2433 	if (termch != '\n') termch = 0;
2434 	scanhex(&mskip);
2435 	if (termch != '\n') termch = 0;
2436 	scanhex(&mlim);
2437 	ook = 0;
2438 	for (a = mdest; a < mlim; a += mskip) {
2439 		ok = mread(a, &v, 1);
2440 		if (ok && !ook) {
2441 			printf("%.8x .. ", a);
2442 		} else if (!ok && ook)
2443 			printf("%.8x\n", a - mskip);
2444 		ook = ok;
2445 		if (a + mskip < a)
2446 			break;
2447 	}
2448 	if (ook)
2449 		printf("%.8x\n", a - mskip);
2450 }
2451 
2452 static void proccall(void)
2453 {
2454 	unsigned long args[8];
2455 	unsigned long ret;
2456 	int i;
2457 	typedef unsigned long (*callfunc_t)(unsigned long, unsigned long,
2458 			unsigned long, unsigned long, unsigned long,
2459 			unsigned long, unsigned long, unsigned long);
2460 	callfunc_t func;
2461 
2462 	if (!scanhex(&adrs))
2463 		return;
2464 	if (termch != '\n')
2465 		termch = 0;
2466 	for (i = 0; i < 8; ++i)
2467 		args[i] = 0;
2468 	for (i = 0; i < 8; ++i) {
2469 		if (!scanhex(&args[i]) || termch == '\n')
2470 			break;
2471 		termch = 0;
2472 	}
2473 	func = (callfunc_t) adrs;
2474 	ret = 0;
2475 	if (setjmp(bus_error_jmp) == 0) {
2476 		catch_memory_errors = 1;
2477 		sync();
2478 		ret = func(args[0], args[1], args[2], args[3],
2479 			   args[4], args[5], args[6], args[7]);
2480 		sync();
2481 		printf("return value is 0x%lx\n", ret);
2482 	} else {
2483 		printf("*** %x exception occurred\n", fault_except);
2484 	}
2485 	catch_memory_errors = 0;
2486 }
2487 
2488 /* Input scanning routines */
2489 int
2490 skipbl(void)
2491 {
2492 	int c;
2493 
2494 	if( termch != 0 ){
2495 		c = termch;
2496 		termch = 0;
2497 	} else
2498 		c = inchar();
2499 	while( c == ' ' || c == '\t' )
2500 		c = inchar();
2501 	return c;
2502 }
2503 
2504 #define N_PTREGS	44
2505 static char *regnames[N_PTREGS] = {
2506 	"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2507 	"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
2508 	"r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
2509 	"r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
2510 	"pc", "msr", "or3", "ctr", "lr", "xer", "ccr",
2511 #ifdef CONFIG_PPC64
2512 	"softe",
2513 #else
2514 	"mq",
2515 #endif
2516 	"trap", "dar", "dsisr", "res"
2517 };
2518 
2519 int
2520 scanhex(unsigned long *vp)
2521 {
2522 	int c, d;
2523 	unsigned long v;
2524 
2525 	c = skipbl();
2526 	if (c == '%') {
2527 		/* parse register name */
2528 		char regname[8];
2529 		int i;
2530 
2531 		for (i = 0; i < sizeof(regname) - 1; ++i) {
2532 			c = inchar();
2533 			if (!isalnum(c)) {
2534 				termch = c;
2535 				break;
2536 			}
2537 			regname[i] = c;
2538 		}
2539 		regname[i] = 0;
2540 		for (i = 0; i < N_PTREGS; ++i) {
2541 			if (strcmp(regnames[i], regname) == 0) {
2542 				if (xmon_regs == NULL) {
2543 					printf("regs not available\n");
2544 					return 0;
2545 				}
2546 				*vp = ((unsigned long *)xmon_regs)[i];
2547 				return 1;
2548 			}
2549 		}
2550 		printf("invalid register name '%%%s'\n", regname);
2551 		return 0;
2552 	}
2553 
2554 	/* skip leading "0x" if any */
2555 
2556 	if (c == '0') {
2557 		c = inchar();
2558 		if (c == 'x') {
2559 			c = inchar();
2560 		} else {
2561 			d = hexdigit(c);
2562 			if (d == EOF) {
2563 				termch = c;
2564 				*vp = 0;
2565 				return 1;
2566 			}
2567 		}
2568 	} else if (c == '$') {
2569 		int i;
2570 		for (i=0; i<63; i++) {
2571 			c = inchar();
2572 			if (isspace(c)) {
2573 				termch = c;
2574 				break;
2575 			}
2576 			tmpstr[i] = c;
2577 		}
2578 		tmpstr[i++] = 0;
2579 		*vp = 0;
2580 		if (setjmp(bus_error_jmp) == 0) {
2581 			catch_memory_errors = 1;
2582 			sync();
2583 			*vp = kallsyms_lookup_name(tmpstr);
2584 			sync();
2585 		}
2586 		catch_memory_errors = 0;
2587 		if (!(*vp)) {
2588 			printf("unknown symbol '%s'\n", tmpstr);
2589 			return 0;
2590 		}
2591 		return 1;
2592 	}
2593 
2594 	d = hexdigit(c);
2595 	if (d == EOF) {
2596 		termch = c;
2597 		return 0;
2598 	}
2599 	v = 0;
2600 	do {
2601 		v = (v << 4) + d;
2602 		c = inchar();
2603 		d = hexdigit(c);
2604 	} while (d != EOF);
2605 	termch = c;
2606 	*vp = v;
2607 	return 1;
2608 }
2609 
2610 static void
2611 scannl(void)
2612 {
2613 	int c;
2614 
2615 	c = termch;
2616 	termch = 0;
2617 	while( c != '\n' )
2618 		c = inchar();
2619 }
2620 
2621 static int hexdigit(int c)
2622 {
2623 	if( '0' <= c && c <= '9' )
2624 		return c - '0';
2625 	if( 'A' <= c && c <= 'F' )
2626 		return c - ('A' - 10);
2627 	if( 'a' <= c && c <= 'f' )
2628 		return c - ('a' - 10);
2629 	return EOF;
2630 }
2631 
2632 void
2633 getstring(char *s, int size)
2634 {
2635 	int c;
2636 
2637 	c = skipbl();
2638 	do {
2639 		if( size > 1 ){
2640 			*s++ = c;
2641 			--size;
2642 		}
2643 		c = inchar();
2644 	} while( c != ' ' && c != '\t' && c != '\n' );
2645 	termch = c;
2646 	*s = 0;
2647 }
2648 
2649 static char line[256];
2650 static char *lineptr;
2651 
2652 static void
2653 flush_input(void)
2654 {
2655 	lineptr = NULL;
2656 }
2657 
2658 static int
2659 inchar(void)
2660 {
2661 	if (lineptr == NULL || *lineptr == 0) {
2662 		if (xmon_gets(line, sizeof(line)) == NULL) {
2663 			lineptr = NULL;
2664 			return EOF;
2665 		}
2666 		lineptr = line;
2667 	}
2668 	return *lineptr++;
2669 }
2670 
2671 static void
2672 take_input(char *str)
2673 {
2674 	lineptr = str;
2675 }
2676 
2677 
2678 static void
2679 symbol_lookup(void)
2680 {
2681 	int type = inchar();
2682 	unsigned long addr;
2683 	static char tmp[64];
2684 
2685 	switch (type) {
2686 	case 'a':
2687 		if (scanhex(&addr))
2688 			xmon_print_symbol(addr, ": ", "\n");
2689 		termch = 0;
2690 		break;
2691 	case 's':
2692 		getstring(tmp, 64);
2693 		if (setjmp(bus_error_jmp) == 0) {
2694 			catch_memory_errors = 1;
2695 			sync();
2696 			addr = kallsyms_lookup_name(tmp);
2697 			if (addr)
2698 				printf("%s: %lx\n", tmp, addr);
2699 			else
2700 				printf("Symbol '%s' not found.\n", tmp);
2701 			sync();
2702 		}
2703 		catch_memory_errors = 0;
2704 		termch = 0;
2705 		break;
2706 	}
2707 }
2708 
2709 
2710 /* Print an address in numeric and symbolic form (if possible) */
2711 static void xmon_print_symbol(unsigned long address, const char *mid,
2712 			      const char *after)
2713 {
2714 	char *modname;
2715 	const char *name = NULL;
2716 	unsigned long offset, size;
2717 
2718 	printf(REG, address);
2719 	if (setjmp(bus_error_jmp) == 0) {
2720 		catch_memory_errors = 1;
2721 		sync();
2722 		name = kallsyms_lookup(address, &size, &offset, &modname,
2723 				       tmpstr);
2724 		sync();
2725 		/* wait a little while to see if we get a machine check */
2726 		__delay(200);
2727 	}
2728 
2729 	catch_memory_errors = 0;
2730 
2731 	if (name) {
2732 		printf("%s%s+%#lx/%#lx", mid, name, offset, size);
2733 		if (modname)
2734 			printf(" [%s]", modname);
2735 	}
2736 	printf("%s", after);
2737 }
2738 
2739 #ifdef CONFIG_PPC_BOOK3S_64
2740 void dump_segments(void)
2741 {
2742 	int i;
2743 	unsigned long esid,vsid,valid;
2744 	unsigned long llp;
2745 
2746 	printf("SLB contents of cpu 0x%x\n", smp_processor_id());
2747 
2748 	for (i = 0; i < mmu_slb_size; i++) {
2749 		asm volatile("slbmfee  %0,%1" : "=r" (esid) : "r" (i));
2750 		asm volatile("slbmfev  %0,%1" : "=r" (vsid) : "r" (i));
2751 		valid = (esid & SLB_ESID_V);
2752 		if (valid | esid | vsid) {
2753 			printf("%02d %016lx %016lx", i, esid, vsid);
2754 			if (valid) {
2755 				llp = vsid & SLB_VSID_LLP;
2756 				if (vsid & SLB_VSID_B_1T) {
2757 					printf("  1T  ESID=%9lx  VSID=%13lx LLP:%3lx \n",
2758 						GET_ESID_1T(esid),
2759 						(vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T,
2760 						llp);
2761 				} else {
2762 					printf(" 256M ESID=%9lx  VSID=%13lx LLP:%3lx \n",
2763 						GET_ESID(esid),
2764 						(vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT,
2765 						llp);
2766 				}
2767 			} else
2768 				printf("\n");
2769 		}
2770 	}
2771 }
2772 #endif
2773 
2774 #ifdef CONFIG_PPC_STD_MMU_32
2775 void dump_segments(void)
2776 {
2777 	int i;
2778 
2779 	printf("sr0-15 =");
2780 	for (i = 0; i < 16; ++i)
2781 		printf(" %x", mfsrin(i));
2782 	printf("\n");
2783 }
2784 #endif
2785 
2786 #ifdef CONFIG_44x
2787 static void dump_tlb_44x(void)
2788 {
2789 	int i;
2790 
2791 	for (i = 0; i < PPC44x_TLB_SIZE; i++) {
2792 		unsigned long w0,w1,w2;
2793 		asm volatile("tlbre  %0,%1,0" : "=r" (w0) : "r" (i));
2794 		asm volatile("tlbre  %0,%1,1" : "=r" (w1) : "r" (i));
2795 		asm volatile("tlbre  %0,%1,2" : "=r" (w2) : "r" (i));
2796 		printf("[%02x] %08x %08x %08x ", i, w0, w1, w2);
2797 		if (w0 & PPC44x_TLB_VALID) {
2798 			printf("V %08x -> %01x%08x %c%c%c%c%c",
2799 			       w0 & PPC44x_TLB_EPN_MASK,
2800 			       w1 & PPC44x_TLB_ERPN_MASK,
2801 			       w1 & PPC44x_TLB_RPN_MASK,
2802 			       (w2 & PPC44x_TLB_W) ? 'W' : 'w',
2803 			       (w2 & PPC44x_TLB_I) ? 'I' : 'i',
2804 			       (w2 & PPC44x_TLB_M) ? 'M' : 'm',
2805 			       (w2 & PPC44x_TLB_G) ? 'G' : 'g',
2806 			       (w2 & PPC44x_TLB_E) ? 'E' : 'e');
2807 		}
2808 		printf("\n");
2809 	}
2810 }
2811 #endif /* CONFIG_44x */
2812 
2813 #ifdef CONFIG_PPC_BOOK3E
2814 static void dump_tlb_book3e(void)
2815 {
2816 	u32 mmucfg, pidmask, lpidmask;
2817 	u64 ramask;
2818 	int i, tlb, ntlbs, pidsz, lpidsz, rasz, lrat = 0;
2819 	int mmu_version;
2820 	static const char *pgsz_names[] = {
2821 		"  1K",
2822 		"  2K",
2823 		"  4K",
2824 		"  8K",
2825 		" 16K",
2826 		" 32K",
2827 		" 64K",
2828 		"128K",
2829 		"256K",
2830 		"512K",
2831 		"  1M",
2832 		"  2M",
2833 		"  4M",
2834 		"  8M",
2835 		" 16M",
2836 		" 32M",
2837 		" 64M",
2838 		"128M",
2839 		"256M",
2840 		"512M",
2841 		"  1G",
2842 		"  2G",
2843 		"  4G",
2844 		"  8G",
2845 		" 16G",
2846 		" 32G",
2847 		" 64G",
2848 		"128G",
2849 		"256G",
2850 		"512G",
2851 		"  1T",
2852 		"  2T",
2853 	};
2854 
2855 	/* Gather some infos about the MMU */
2856 	mmucfg = mfspr(SPRN_MMUCFG);
2857 	mmu_version = (mmucfg & 3) + 1;
2858 	ntlbs = ((mmucfg >> 2) & 3) + 1;
2859 	pidsz = ((mmucfg >> 6) & 0x1f) + 1;
2860 	lpidsz = (mmucfg >> 24) & 0xf;
2861 	rasz = (mmucfg >> 16) & 0x7f;
2862 	if ((mmu_version > 1) && (mmucfg & 0x10000))
2863 		lrat = 1;
2864 	printf("Book3E MMU MAV=%d.0,%d TLBs,%d-bit PID,%d-bit LPID,%d-bit RA\n",
2865 	       mmu_version, ntlbs, pidsz, lpidsz, rasz);
2866 	pidmask = (1ul << pidsz) - 1;
2867 	lpidmask = (1ul << lpidsz) - 1;
2868 	ramask = (1ull << rasz) - 1;
2869 
2870 	for (tlb = 0; tlb < ntlbs; tlb++) {
2871 		u32 tlbcfg;
2872 		int nent, assoc, new_cc = 1;
2873 		printf("TLB %d:\n------\n", tlb);
2874 		switch(tlb) {
2875 		case 0:
2876 			tlbcfg = mfspr(SPRN_TLB0CFG);
2877 			break;
2878 		case 1:
2879 			tlbcfg = mfspr(SPRN_TLB1CFG);
2880 			break;
2881 		case 2:
2882 			tlbcfg = mfspr(SPRN_TLB2CFG);
2883 			break;
2884 		case 3:
2885 			tlbcfg = mfspr(SPRN_TLB3CFG);
2886 			break;
2887 		default:
2888 			printf("Unsupported TLB number !\n");
2889 			continue;
2890 		}
2891 		nent = tlbcfg & 0xfff;
2892 		assoc = (tlbcfg >> 24) & 0xff;
2893 		for (i = 0; i < nent; i++) {
2894 			u32 mas0 = MAS0_TLBSEL(tlb);
2895 			u32 mas1 = MAS1_TSIZE(BOOK3E_PAGESZ_4K);
2896 			u64 mas2 = 0;
2897 			u64 mas7_mas3;
2898 			int esel = i, cc = i;
2899 
2900 			if (assoc != 0) {
2901 				cc = i / assoc;
2902 				esel = i % assoc;
2903 				mas2 = cc * 0x1000;
2904 			}
2905 
2906 			mas0 |= MAS0_ESEL(esel);
2907 			mtspr(SPRN_MAS0, mas0);
2908 			mtspr(SPRN_MAS1, mas1);
2909 			mtspr(SPRN_MAS2, mas2);
2910 			asm volatile("tlbre  0,0,0" : : : "memory");
2911 			mas1 = mfspr(SPRN_MAS1);
2912 			mas2 = mfspr(SPRN_MAS2);
2913 			mas7_mas3 = mfspr(SPRN_MAS7_MAS3);
2914 			if (assoc && (i % assoc) == 0)
2915 				new_cc = 1;
2916 			if (!(mas1 & MAS1_VALID))
2917 				continue;
2918 			if (assoc == 0)
2919 				printf("%04x- ", i);
2920 			else if (new_cc)
2921 				printf("%04x-%c", cc, 'A' + esel);
2922 			else
2923 				printf("    |%c", 'A' + esel);
2924 			new_cc = 0;
2925 			printf(" %016llx %04x %s %c%c AS%c",
2926 			       mas2 & ~0x3ffull,
2927 			       (mas1 >> 16) & 0x3fff,
2928 			       pgsz_names[(mas1 >> 7) & 0x1f],
2929 			       mas1 & MAS1_IND ? 'I' : ' ',
2930 			       mas1 & MAS1_IPROT ? 'P' : ' ',
2931 			       mas1 & MAS1_TS ? '1' : '0');
2932 			printf(" %c%c%c%c%c%c%c",
2933 			       mas2 & MAS2_X0 ? 'a' : ' ',
2934 			       mas2 & MAS2_X1 ? 'v' : ' ',
2935 			       mas2 & MAS2_W  ? 'w' : ' ',
2936 			       mas2 & MAS2_I  ? 'i' : ' ',
2937 			       mas2 & MAS2_M  ? 'm' : ' ',
2938 			       mas2 & MAS2_G  ? 'g' : ' ',
2939 			       mas2 & MAS2_E  ? 'e' : ' ');
2940 			printf(" %016llx", mas7_mas3 & ramask & ~0x7ffull);
2941 			if (mas1 & MAS1_IND)
2942 				printf(" %s\n",
2943 				       pgsz_names[(mas7_mas3 >> 1) & 0x1f]);
2944 			else
2945 				printf(" U%c%c%c S%c%c%c\n",
2946 				       mas7_mas3 & MAS3_UX ? 'x' : ' ',
2947 				       mas7_mas3 & MAS3_UW ? 'w' : ' ',
2948 				       mas7_mas3 & MAS3_UR ? 'r' : ' ',
2949 				       mas7_mas3 & MAS3_SX ? 'x' : ' ',
2950 				       mas7_mas3 & MAS3_SW ? 'w' : ' ',
2951 				       mas7_mas3 & MAS3_SR ? 'r' : ' ');
2952 		}
2953 	}
2954 }
2955 #endif /* CONFIG_PPC_BOOK3E */
2956 
2957 static void xmon_init(int enable)
2958 {
2959 	if (enable) {
2960 		__debugger = xmon;
2961 		__debugger_ipi = xmon_ipi;
2962 		__debugger_bpt = xmon_bpt;
2963 		__debugger_sstep = xmon_sstep;
2964 		__debugger_iabr_match = xmon_iabr_match;
2965 		__debugger_break_match = xmon_break_match;
2966 		__debugger_fault_handler = xmon_fault_handler;
2967 	} else {
2968 		__debugger = NULL;
2969 		__debugger_ipi = NULL;
2970 		__debugger_bpt = NULL;
2971 		__debugger_sstep = NULL;
2972 		__debugger_iabr_match = NULL;
2973 		__debugger_break_match = NULL;
2974 		__debugger_fault_handler = NULL;
2975 	}
2976 }
2977 
2978 #ifdef CONFIG_MAGIC_SYSRQ
2979 static void sysrq_handle_xmon(int key)
2980 {
2981 	/* ensure xmon is enabled */
2982 	xmon_init(1);
2983 	debugger(get_irq_regs());
2984 }
2985 
2986 static struct sysrq_key_op sysrq_xmon_op = {
2987 	.handler =	sysrq_handle_xmon,
2988 	.help_msg =	"xmon(x)",
2989 	.action_msg =	"Entering xmon",
2990 };
2991 
2992 static int __init setup_xmon_sysrq(void)
2993 {
2994 	register_sysrq_key('x', &sysrq_xmon_op);
2995 	return 0;
2996 }
2997 __initcall(setup_xmon_sysrq);
2998 #endif /* CONFIG_MAGIC_SYSRQ */
2999 
3000 static int __initdata xmon_early, xmon_off;
3001 
3002 static int __init early_parse_xmon(char *p)
3003 {
3004 	if (!p || strncmp(p, "early", 5) == 0) {
3005 		/* just "xmon" is equivalent to "xmon=early" */
3006 		xmon_init(1);
3007 		xmon_early = 1;
3008 	} else if (strncmp(p, "on", 2) == 0)
3009 		xmon_init(1);
3010 	else if (strncmp(p, "off", 3) == 0)
3011 		xmon_off = 1;
3012 	else if (strncmp(p, "nobt", 4) == 0)
3013 		xmon_no_auto_backtrace = 1;
3014 	else
3015 		return 1;
3016 
3017 	return 0;
3018 }
3019 early_param("xmon", early_parse_xmon);
3020 
3021 void __init xmon_setup(void)
3022 {
3023 #ifdef CONFIG_XMON_DEFAULT
3024 	if (!xmon_off)
3025 		xmon_init(1);
3026 #endif
3027 	if (xmon_early)
3028 		debugger(NULL);
3029 }
3030 
3031 #ifdef CONFIG_SPU_BASE
3032 
3033 struct spu_info {
3034 	struct spu *spu;
3035 	u64 saved_mfc_sr1_RW;
3036 	u32 saved_spu_runcntl_RW;
3037 	unsigned long dump_addr;
3038 	u8 stopped_ok;
3039 };
3040 
3041 #define XMON_NUM_SPUS	16	/* Enough for current hardware */
3042 
3043 static struct spu_info spu_info[XMON_NUM_SPUS];
3044 
3045 void xmon_register_spus(struct list_head *list)
3046 {
3047 	struct spu *spu;
3048 
3049 	list_for_each_entry(spu, list, full_list) {
3050 		if (spu->number >= XMON_NUM_SPUS) {
3051 			WARN_ON(1);
3052 			continue;
3053 		}
3054 
3055 		spu_info[spu->number].spu = spu;
3056 		spu_info[spu->number].stopped_ok = 0;
3057 		spu_info[spu->number].dump_addr = (unsigned long)
3058 				spu_info[spu->number].spu->local_store;
3059 	}
3060 }
3061 
3062 static void stop_spus(void)
3063 {
3064 	struct spu *spu;
3065 	int i;
3066 	u64 tmp;
3067 
3068 	for (i = 0; i < XMON_NUM_SPUS; i++) {
3069 		if (!spu_info[i].spu)
3070 			continue;
3071 
3072 		if (setjmp(bus_error_jmp) == 0) {
3073 			catch_memory_errors = 1;
3074 			sync();
3075 
3076 			spu = spu_info[i].spu;
3077 
3078 			spu_info[i].saved_spu_runcntl_RW =
3079 				in_be32(&spu->problem->spu_runcntl_RW);
3080 
3081 			tmp = spu_mfc_sr1_get(spu);
3082 			spu_info[i].saved_mfc_sr1_RW = tmp;
3083 
3084 			tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
3085 			spu_mfc_sr1_set(spu, tmp);
3086 
3087 			sync();
3088 			__delay(200);
3089 
3090 			spu_info[i].stopped_ok = 1;
3091 
3092 			printf("Stopped spu %.2d (was %s)\n", i,
3093 					spu_info[i].saved_spu_runcntl_RW ?
3094 					"running" : "stopped");
3095 		} else {
3096 			catch_memory_errors = 0;
3097 			printf("*** Error stopping spu %.2d\n", i);
3098 		}
3099 		catch_memory_errors = 0;
3100 	}
3101 }
3102 
3103 static void restart_spus(void)
3104 {
3105 	struct spu *spu;
3106 	int i;
3107 
3108 	for (i = 0; i < XMON_NUM_SPUS; i++) {
3109 		if (!spu_info[i].spu)
3110 			continue;
3111 
3112 		if (!spu_info[i].stopped_ok) {
3113 			printf("*** Error, spu %d was not successfully stopped"
3114 					", not restarting\n", i);
3115 			continue;
3116 		}
3117 
3118 		if (setjmp(bus_error_jmp) == 0) {
3119 			catch_memory_errors = 1;
3120 			sync();
3121 
3122 			spu = spu_info[i].spu;
3123 			spu_mfc_sr1_set(spu, spu_info[i].saved_mfc_sr1_RW);
3124 			out_be32(&spu->problem->spu_runcntl_RW,
3125 					spu_info[i].saved_spu_runcntl_RW);
3126 
3127 			sync();
3128 			__delay(200);
3129 
3130 			printf("Restarted spu %.2d\n", i);
3131 		} else {
3132 			catch_memory_errors = 0;
3133 			printf("*** Error restarting spu %.2d\n", i);
3134 		}
3135 		catch_memory_errors = 0;
3136 	}
3137 }
3138 
3139 #define DUMP_WIDTH	23
3140 #define DUMP_VALUE(format, field, value)				\
3141 do {									\
3142 	if (setjmp(bus_error_jmp) == 0) {				\
3143 		catch_memory_errors = 1;				\
3144 		sync();							\
3145 		printf("  %-*s = "format"\n", DUMP_WIDTH,		\
3146 				#field, value);				\
3147 		sync();							\
3148 		__delay(200);						\
3149 	} else {							\
3150 		catch_memory_errors = 0;				\
3151 		printf("  %-*s = *** Error reading field.\n",		\
3152 					DUMP_WIDTH, #field);		\
3153 	}								\
3154 	catch_memory_errors = 0;					\
3155 } while (0)
3156 
3157 #define DUMP_FIELD(obj, format, field)	\
3158 	DUMP_VALUE(format, field, obj->field)
3159 
3160 static void dump_spu_fields(struct spu *spu)
3161 {
3162 	printf("Dumping spu fields at address %p:\n", spu);
3163 
3164 	DUMP_FIELD(spu, "0x%x", number);
3165 	DUMP_FIELD(spu, "%s", name);
3166 	DUMP_FIELD(spu, "0x%lx", local_store_phys);
3167 	DUMP_FIELD(spu, "0x%p", local_store);
3168 	DUMP_FIELD(spu, "0x%lx", ls_size);
3169 	DUMP_FIELD(spu, "0x%x", node);
3170 	DUMP_FIELD(spu, "0x%lx", flags);
3171 	DUMP_FIELD(spu, "%d", class_0_pending);
3172 	DUMP_FIELD(spu, "0x%lx", class_0_dar);
3173 	DUMP_FIELD(spu, "0x%lx", class_1_dar);
3174 	DUMP_FIELD(spu, "0x%lx", class_1_dsisr);
3175 	DUMP_FIELD(spu, "0x%lx", irqs[0]);
3176 	DUMP_FIELD(spu, "0x%lx", irqs[1]);
3177 	DUMP_FIELD(spu, "0x%lx", irqs[2]);
3178 	DUMP_FIELD(spu, "0x%x", slb_replace);
3179 	DUMP_FIELD(spu, "%d", pid);
3180 	DUMP_FIELD(spu, "0x%p", mm);
3181 	DUMP_FIELD(spu, "0x%p", ctx);
3182 	DUMP_FIELD(spu, "0x%p", rq);
3183 	DUMP_FIELD(spu, "0x%p", timestamp);
3184 	DUMP_FIELD(spu, "0x%lx", problem_phys);
3185 	DUMP_FIELD(spu, "0x%p", problem);
3186 	DUMP_VALUE("0x%x", problem->spu_runcntl_RW,
3187 			in_be32(&spu->problem->spu_runcntl_RW));
3188 	DUMP_VALUE("0x%x", problem->spu_status_R,
3189 			in_be32(&spu->problem->spu_status_R));
3190 	DUMP_VALUE("0x%x", problem->spu_npc_RW,
3191 			in_be32(&spu->problem->spu_npc_RW));
3192 	DUMP_FIELD(spu, "0x%p", priv2);
3193 	DUMP_FIELD(spu, "0x%p", pdata);
3194 }
3195 
3196 int
3197 spu_inst_dump(unsigned long adr, long count, int praddr)
3198 {
3199 	return generic_inst_dump(adr, count, praddr, print_insn_spu);
3200 }
3201 
3202 static void dump_spu_ls(unsigned long num, int subcmd)
3203 {
3204 	unsigned long offset, addr, ls_addr;
3205 
3206 	if (setjmp(bus_error_jmp) == 0) {
3207 		catch_memory_errors = 1;
3208 		sync();
3209 		ls_addr = (unsigned long)spu_info[num].spu->local_store;
3210 		sync();
3211 		__delay(200);
3212 	} else {
3213 		catch_memory_errors = 0;
3214 		printf("*** Error: accessing spu info for spu %d\n", num);
3215 		return;
3216 	}
3217 	catch_memory_errors = 0;
3218 
3219 	if (scanhex(&offset))
3220 		addr = ls_addr + offset;
3221 	else
3222 		addr = spu_info[num].dump_addr;
3223 
3224 	if (addr >= ls_addr + LS_SIZE) {
3225 		printf("*** Error: address outside of local store\n");
3226 		return;
3227 	}
3228 
3229 	switch (subcmd) {
3230 	case 'i':
3231 		addr += spu_inst_dump(addr, 16, 1);
3232 		last_cmd = "sdi\n";
3233 		break;
3234 	default:
3235 		prdump(addr, 64);
3236 		addr += 64;
3237 		last_cmd = "sd\n";
3238 		break;
3239 	}
3240 
3241 	spu_info[num].dump_addr = addr;
3242 }
3243 
3244 static int do_spu_cmd(void)
3245 {
3246 	static unsigned long num = 0;
3247 	int cmd, subcmd = 0;
3248 
3249 	cmd = inchar();
3250 	switch (cmd) {
3251 	case 's':
3252 		stop_spus();
3253 		break;
3254 	case 'r':
3255 		restart_spus();
3256 		break;
3257 	case 'd':
3258 		subcmd = inchar();
3259 		if (isxdigit(subcmd) || subcmd == '\n')
3260 			termch = subcmd;
3261 	case 'f':
3262 		scanhex(&num);
3263 		if (num >= XMON_NUM_SPUS || !spu_info[num].spu) {
3264 			printf("*** Error: invalid spu number\n");
3265 			return 0;
3266 		}
3267 
3268 		switch (cmd) {
3269 		case 'f':
3270 			dump_spu_fields(spu_info[num].spu);
3271 			break;
3272 		default:
3273 			dump_spu_ls(num, subcmd);
3274 			break;
3275 		}
3276 
3277 		break;
3278 	default:
3279 		return -1;
3280 	}
3281 
3282 	return 0;
3283 }
3284 #else /* ! CONFIG_SPU_BASE */
3285 static int do_spu_cmd(void)
3286 {
3287 	return -1;
3288 }
3289 #endif
3290