xmon.c 68.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * Routines providing a simple monitor for use on the PowerMac.
 *
4
 * Copyright (C) 1996-2005 Paul Mackerras.
5 6
 * Copyright (C) 2001 PPC64 Team, IBM Corp
 * Copyrignt (C) 2006 Michael Ellerman, IBM Corp
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/smp.h>
#include <linux/mm.h>
#include <linux/reboot.h>
#include <linux/delay.h>
#include <linux/kallsyms.h>
20
#include <linux/kmsg_dump.h>
L
Linus Torvalds 已提交
21
#include <linux/cpumask.h>
22
#include <linux/export.h>
23
#include <linux/sysrq.h>
A
Andrew Morton 已提交
24
#include <linux/interrupt.h>
25
#include <linux/irq.h>
26
#include <linux/bug.h>
L
Linus Torvalds 已提交
27 28 29 30 31

#include <asm/ptrace.h>
#include <asm/string.h>
#include <asm/prom.h>
#include <asm/machdep.h>
P
Paul Mackerras 已提交
32
#include <asm/xmon.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39
#include <asm/processor.h>
#include <asm/pgtable.h>
#include <asm/mmu.h>
#include <asm/mmu_context.h>
#include <asm/cputable.h>
#include <asm/rtas.h>
#include <asm/sstep.h>
40
#include <asm/irq_regs.h>
41 42
#include <asm/spu.h>
#include <asm/spu_priv1.h>
43
#include <asm/setjmp.h>
44
#include <asm/reg.h>
45
#include <asm/debug.h>
46
#include <asm/hw_breakpoint.h>
P
Paul Mackerras 已提交
47 48

#ifdef CONFIG_PPC64
L
Linus Torvalds 已提交
49
#include <asm/hvcall.h>
P
Paul Mackerras 已提交
50 51
#include <asm/paca.h>
#endif
L
Linus Torvalds 已提交
52 53

#include "nonstdio.h"
54
#include "dis-asm.h"
L
Linus Torvalds 已提交
55 56

#ifdef CONFIG_SMP
57
static cpumask_t cpus_in_xmon = CPU_MASK_NONE;
L
Linus Torvalds 已提交
58 59 60
static unsigned long xmon_taken = 1;
static int xmon_owner;
static int xmon_gate;
61 62
#else
#define xmon_owner 0
L
Linus Torvalds 已提交
63 64
#endif /* CONFIG_SMP */

65
static unsigned long in_xmon __read_mostly = 0;
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

static unsigned long adrs;
static int size = 1;
#define MAX_DUMP (128 * 1024)
static unsigned long ndump = 64;
static unsigned long nidump = 16;
static unsigned long ncsum = 4096;
static int termch;
static char tmpstr[128];

static long bus_error_jmp[JMP_BUF_LEN];
static int catch_memory_errors;
static long *xmon_fault_jmp[NR_CPUS];

/* Breakpoint stuff */
struct bpt {
	unsigned long	address;
	unsigned int	instr[2];
	atomic_t	ref_count;
	int		enabled;
	unsigned long	pad;
};

/* Bits in bpt.enabled */
#define BP_IABR_TE	1		/* IABR translation enabled */
#define BP_IABR		2
#define BP_TRAP		8
#define BP_DABR		0x10

#define NBPTS	256
static struct bpt bpts[NBPTS];
static struct bpt dabr;
static struct bpt *iabr;
static unsigned bpinstr = 0x7fe00008;	/* trap */

#define BP_NUM(bp)	((bp) - bpts + 1)

/* Prototypes */
static int cmds(struct pt_regs *);
static int mread(unsigned long, void *, int);
static int mwrite(unsigned long, void *, int);
static int handle_fault(struct pt_regs *);
static void byterev(unsigned char *, int);
static void memex(void);
static int bsesc(void);
static void dump(void);
static void prdump(unsigned long, long);
static int ppc_inst_dump(unsigned long, long, int);
114
static void dump_log_buf(void);
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
static void backtrace(struct pt_regs *);
static void excprint(struct pt_regs *);
static void prregs(struct pt_regs *);
static void memops(int);
static void memlocate(void);
static void memzcan(void);
static void memdiffs(unsigned char *, unsigned char *, unsigned, unsigned);
int skipbl(void);
int scanhex(unsigned long *valp);
static void scannl(void);
static int hexdigit(int);
void getstring(char *, int);
static void flush_input(void);
static int inchar(void);
static void take_input(char *);
static unsigned long read_spr(int);
static void write_spr(int, unsigned long);
static void super_regs(void);
static void remove_bpts(void);
static void insert_bpts(void);
static void remove_cpu_bpts(void);
static void insert_cpu_bpts(void);
static struct bpt *at_breakpoint(unsigned long pc);
static struct bpt *in_breakpoint_table(unsigned long pc, unsigned long *offp);
static int  do_step(struct pt_regs *);
static void bpt_cmds(void);
static void cacheflush(void);
static int  cpu_cmd(void);
static void csum(void);
static void bootcmds(void);
P
Paul Mackerras 已提交
145
static void proccall(void);
L
Linus Torvalds 已提交
146 147
void dump_segments(void);
static void symbol_lookup(void);
148 149
static void xmon_show_stack(unsigned long sp, unsigned long lr,
			    unsigned long pc);
L
Linus Torvalds 已提交
150 151 152 153
static void xmon_print_symbol(unsigned long address, const char *mid,
			      const char *after);
static const char *getvecname(unsigned long vec);

154 155
static int do_spu_cmd(void);

156 157 158
#ifdef CONFIG_44x
static void dump_tlb_44x(void);
#endif
159 160 161
#ifdef CONFIG_PPC_BOOK3E
static void dump_tlb_book3e(void);
#endif
162

163
static int xmon_no_auto_backtrace;
164

P
Paul Mackerras 已提交
165 166 167 168 169 170 171 172
extern void xmon_enter(void);
extern void xmon_leave(void);

#ifdef CONFIG_PPC64
#define REG		"%.16lx"
#else
#define REG		"%.8lx"
#endif
L
Linus Torvalds 已提交
173

174 175 176
#ifdef __LITTLE_ENDIAN__
#define GETWORD(v)	(((v)[3] << 24) + ((v)[2] << 16) + ((v)[1] << 8) + (v)[0])
#else
L
Linus Torvalds 已提交
177
#define GETWORD(v)	(((v)[0] << 24) + ((v)[1] << 16) + ((v)[2] << 8) + (v)[3])
178
#endif
L
Linus Torvalds 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

#define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
			 || ('a' <= (c) && (c) <= 'f') \
			 || ('A' <= (c) && (c) <= 'F'))
#define isalnum(c)	(('0' <= (c) && (c) <= '9') \
			 || ('a' <= (c) && (c) <= 'z') \
			 || ('A' <= (c) && (c) <= 'Z'))
#define isspace(c)	(c == ' ' || c == '\t' || c == 10 || c == 13 || c == 0)

static char *help_string = "\
Commands:\n\
  b	show breakpoints\n\
  bd	set data breakpoint\n\
  bi	set instruction breakpoint\n\
  bc	clear breakpoint\n"
#ifdef CONFIG_SMP
  "\
  c	print cpus stopped in xmon\n\
  c#	try to switch to cpu number h (in hex)\n"
#endif
  "\
  C	checksum\n\
  d	dump bytes\n\
  di	dump instructions\n\
  df	dump float values\n\
  dd	dump double values\n\
205 206 207 208 209 210 211
  dl    dump the kernel log buffer\n"
#ifdef CONFIG_PPC64
  "\
  dp[#]	dump paca for current cpu, or cpu #\n\
  dpa	dump paca for all possible cpus\n"
#endif
  "\
212
  dr	dump stream of raw bytes\n\
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221 222 223
  e	print exception information\n\
  f	flush cache\n\
  la	lookup symbol+offset of specified address\n\
  ls	lookup address of specified symbol\n\
  m	examine/change memory\n\
  mm	move a block of memory\n\
  ms	set a block of memory\n\
  md	compare two blocks of memory\n\
  ml	locate a block of memory\n\
  mz	zero a block of memory\n\
  mi	show information about memory allocation\n\
P
Paul Mackerras 已提交
224
  p 	call a procedure\n\
L
Linus Torvalds 已提交
225
  r	print registers\n\
226
  s	single step\n"
227
#ifdef CONFIG_SPU_BASE
228
"  ss	stop execution on all spus\n\
229
  sr	restore execution on stopped spus\n\
230
  sf  #	dump spu fields for spu # (in hex)\n\
231
  sd  #	dump spu local store for spu # (in hex)\n\
232
  sdi #	disassemble spu local store for spu # (in hex)\n"
233 234
#endif
"  S	print special registers\n\
L
Linus Torvalds 已提交
235 236
  t	print backtrace\n\
  x	exit monitor and recover\n\
P
Paul Mackerras 已提交
237
  X	exit monitor and dont recover\n"
238
#if defined(CONFIG_PPC64) && !defined(CONFIG_PPC_BOOK3E)
P
Paul Mackerras 已提交
239
"  u	dump segment table or SLB\n"
240
#elif defined(CONFIG_PPC_STD_MMU_32)
P
Paul Mackerras 已提交
241
"  u	dump segment registers\n"
242
#elif defined(CONFIG_44x) || defined(CONFIG_PPC_BOOK3E)
243 244
"  u	dump TLB\n"
#endif
P
Paul Mackerras 已提交
245 246
"  ?	help\n"
"  zr	reboot\n\
L
Linus Torvalds 已提交
247 248 249 250 251
  zh	halt\n"
;

static struct pt_regs *xmon_regs;

P
Paul Mackerras 已提交
252
static inline void sync(void)
L
Linus Torvalds 已提交
253 254 255 256
{
	asm volatile("sync; isync");
}

P
Paul Mackerras 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270
static inline void store_inst(void *p)
{
	asm volatile ("dcbst 0,%0; sync; icbi 0,%0; isync" : : "r" (p));
}

static inline void cflush(void *p)
{
	asm volatile ("dcbf 0,%0; icbi 0,%0" : : "r" (p));
}

static inline void cinval(void *p)
{
	asm volatile ("dcbi 0,%0; icbi 0,%0" : : "r" (p));
}
L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

/*
 * Disable surveillance (the service processor watchdog function)
 * while we are in xmon.
 * XXX we should re-enable it when we leave. :)
 */
#define SURVEILLANCE_TOKEN	9000

static inline void disable_surveillance(void)
{
#ifdef CONFIG_PPC_PSERIES
	/* Since this can't be a module, args should end up below 4GB. */
	static struct rtas_args args;

	/*
	 * At this point we have got all the cpus we can into
	 * xmon, so there is hopefully no other cpu calling RTAS
	 * at the moment, even though we don't take rtas.lock.
	 * If we did try to take rtas.lock there would be a
	 * real possibility of deadlock.
	 */
	args.token = rtas_token("set-indicator");
	if (args.token == RTAS_UNKNOWN_SERVICE)
		return;
	args.nargs = 3;
	args.nret = 1;
	args.rets = &args.args[3];
	args.args[0] = SURVEILLANCE_TOKEN;
	args.args[1] = 0;
	args.args[2] = 0;
	enter_rtas(__pa(&args));
#endif /* CONFIG_PPC_PSERIES */
}

#ifdef CONFIG_SMP
static int xmon_speaker;

static void get_output_lock(void)
{
	int me = smp_processor_id() + 0x100;
	int last_speaker = 0, prev;
	long timeout;

	if (xmon_speaker == me)
		return;
316

L
Linus Torvalds 已提交
317
	for (;;) {
318 319 320 321
		last_speaker = cmpxchg(&xmon_speaker, 0, me);
		if (last_speaker == 0)
			return;

322 323 324 325 326
		/*
		 * Wait a full second for the lock, we might be on a slow
		 * console, but check every 100us.
		 */
		timeout = 10000;
L
Linus Torvalds 已提交
327
		while (xmon_speaker == last_speaker) {
328 329
			if (--timeout > 0) {
				udelay(100);
L
Linus Torvalds 已提交
330
				continue;
331 332
			}

L
Linus Torvalds 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345
			/* hostile takeover */
			prev = cmpxchg(&xmon_speaker, last_speaker, me);
			if (prev == last_speaker)
				return;
			break;
		}
	}
}

static void release_output_lock(void)
{
	xmon_speaker = 0;
}
346 347 348

int cpus_are_in_xmon(void)
{
349
	return !cpumask_empty(&cpus_in_xmon);
350
}
L
Linus Torvalds 已提交
351 352
#endif

353 354
static inline int unrecoverable_excp(struct pt_regs *regs)
{
355
#if defined(CONFIG_4xx) || defined(CONFIG_PPC_BOOK3E)
356
	/* We have no MSR_RI bit on 4xx or Book3e, so we simply return false */
357 358 359 360 361 362
	return 0;
#else
	return ((regs->msr & MSR_RI) == 0);
#endif
}

363
static int xmon_core(struct pt_regs *regs, int fromipi)
L
Linus Torvalds 已提交
364 365 366 367 368
{
	int cmd = 0;
	struct bpt *bp;
	long recurse_jmp[JMP_BUF_LEN];
	unsigned long offset;
369
	unsigned long flags;
L
Linus Torvalds 已提交
370 371 372 373 374 375
#ifdef CONFIG_SMP
	int cpu;
	int secondary;
	unsigned long timeout;
#endif

376
	local_irq_save(flags);
L
Linus Torvalds 已提交
377 378 379 380 381 382 383 384 385 386 387

	bp = in_breakpoint_table(regs->nip, &offset);
	if (bp != NULL) {
		regs->nip = bp->address + offset;
		atomic_dec(&bp->ref_count);
	}

	remove_cpu_bpts();

#ifdef CONFIG_SMP
	cpu = smp_processor_id();
388
	if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
L
Linus Torvalds 已提交
389 390 391 392 393
		get_output_lock();
		excprint(regs);
		printf("cpu 0x%x: Exception %lx %s in xmon, "
		       "returning to main loop\n",
		       cpu, regs->trap, getvecname(TRAP(regs)));
394
		release_output_lock();
L
Linus Torvalds 已提交
395 396 397 398 399
		longjmp(xmon_fault_jmp[cpu], 1);
	}

	if (setjmp(recurse_jmp) != 0) {
		if (!in_xmon || !xmon_gate) {
400
			get_output_lock();
L
Linus Torvalds 已提交
401 402
			printf("xmon: WARNING: bad recursive fault "
			       "on cpu 0x%x\n", cpu);
403
			release_output_lock();
L
Linus Torvalds 已提交
404 405 406 407 408 409 410 411 412
			goto waiting;
		}
		secondary = !(xmon_taken && cpu == xmon_owner);
		goto cmdloop;
	}

	xmon_fault_jmp[cpu] = recurse_jmp;

	bp = NULL;
413
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT))
L
Linus Torvalds 已提交
414
		bp = at_breakpoint(regs->nip);
415
	if (bp || unrecoverable_excp(regs))
L
Linus Torvalds 已提交
416 417 418 419 420 421
		fromipi = 0;

	if (!fromipi) {
		get_output_lock();
		excprint(regs);
		if (bp) {
422
			printf("cpu 0x%x stopped at breakpoint 0x%lx (",
L
Linus Torvalds 已提交
423 424 425
			       cpu, BP_NUM(bp));
			xmon_print_symbol(regs->nip, " ", ")\n");
		}
426
		if (unrecoverable_excp(regs))
L
Linus Torvalds 已提交
427 428 429 430 431
			printf("WARNING: exception is not recoverable, "
			       "can't continue\n");
		release_output_lock();
	}

432 433
	cpumask_set_cpu(cpu, &cpus_in_xmon);

L
Linus Torvalds 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
 waiting:
	secondary = 1;
	while (secondary && !xmon_gate) {
		if (in_xmon == 0) {
			if (fromipi)
				goto leave;
			secondary = test_and_set_bit(0, &in_xmon);
		}
		barrier();
	}

	if (!secondary && !xmon_gate) {
		/* we are the first cpu to come in */
		/* interrupt other cpu(s) */
		int ncpus = num_online_cpus();

		xmon_owner = cpu;
		mb();
		if (ncpus > 1) {
453
			smp_send_debugger_break();
L
Linus Torvalds 已提交
454 455
			/* wait for other cpus to come in */
			for (timeout = 100000000; timeout != 0; --timeout) {
456
				if (cpumask_weight(&cpus_in_xmon) >= ncpus)
L
Linus Torvalds 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
					break;
				barrier();
			}
		}
		remove_bpts();
		disable_surveillance();
		/* for breakpoint or single step, print the current instr. */
		if (bp || TRAP(regs) == 0xd00)
			ppc_inst_dump(regs->nip, 1, 0);
		printf("enter ? for help\n");
		mb();
		xmon_gate = 1;
		barrier();
	}

 cmdloop:
	while (in_xmon) {
		if (secondary) {
			if (cpu == xmon_owner) {
				if (!test_and_set_bit(0, &xmon_taken)) {
					secondary = 0;
					continue;
				}
				/* missed it */
				while (cpu == xmon_owner)
					barrier();
			}
			barrier();
		} else {
			cmd = cmds(regs);
			if (cmd != 0) {
				/* exiting xmon */
				insert_bpts();
				xmon_gate = 0;
				wmb();
				in_xmon = 0;
				break;
			}
			/* have switched to some other cpu */
			secondary = 1;
		}
	}
 leave:
500
	cpumask_clear_cpu(cpu, &cpus_in_xmon);
L
Linus Torvalds 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	xmon_fault_jmp[cpu] = NULL;
#else
	/* UP is simple... */
	if (in_xmon) {
		printf("Exception %lx %s in xmon, returning to main loop\n",
		       regs->trap, getvecname(TRAP(regs)));
		longjmp(xmon_fault_jmp[0], 1);
	}
	if (setjmp(recurse_jmp) == 0) {
		xmon_fault_jmp[0] = recurse_jmp;
		in_xmon = 1;

		excprint(regs);
		bp = at_breakpoint(regs->nip);
		if (bp) {
516
			printf("Stopped at breakpoint %lx (", BP_NUM(bp));
L
Linus Torvalds 已提交
517 518
			xmon_print_symbol(regs->nip, " ", ")\n");
		}
519
		if (unrecoverable_excp(regs))
L
Linus Torvalds 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
			printf("WARNING: exception is not recoverable, "
			       "can't continue\n");
		remove_bpts();
		disable_surveillance();
		/* for breakpoint or single step, print the current instr. */
		if (bp || TRAP(regs) == 0xd00)
			ppc_inst_dump(regs->nip, 1, 0);
		printf("enter ? for help\n");
	}

	cmd = cmds(regs);

	insert_bpts();
	in_xmon = 0;
#endif

536 537 538 539 540 541 542 543 544
#ifdef CONFIG_BOOKE
	if (regs->msr & MSR_DE) {
		bp = at_breakpoint(regs->nip);
		if (bp != NULL) {
			regs->nip = (unsigned long) &bp->instr[0];
			atomic_inc(&bp->ref_count);
		}
	}
#else
545
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
L
Linus Torvalds 已提交
546 547 548 549 550 551 552 553 554 555 556 557
		bp = at_breakpoint(regs->nip);
		if (bp != NULL) {
			int stepped = emulate_step(regs, bp->instr[0]);
			if (stepped == 0) {
				regs->nip = (unsigned long) &bp->instr[0];
				atomic_inc(&bp->ref_count);
			} else if (stepped < 0) {
				printf("Couldn't single-step %s instruction\n",
				    (IS_RFID(bp->instr[0])? "rfid": "mtmsrd"));
			}
		}
	}
558
#endif
L
Linus Torvalds 已提交
559 560
	insert_cpu_bpts();

561
	local_irq_restore(flags);
L
Linus Torvalds 已提交
562

563
	return cmd != 'X' && cmd != EOF;
L
Linus Torvalds 已提交
564 565 566 567 568 569 570
}

int xmon(struct pt_regs *excp)
{
	struct pt_regs regs;

	if (excp == NULL) {
571
		ppc_save_regs(&regs);
L
Linus Torvalds 已提交
572 573
		excp = &regs;
	}
574

L
Linus Torvalds 已提交
575 576
	return xmon_core(excp, 0);
}
P
Paul Mackerras 已提交
577 578
EXPORT_SYMBOL(xmon);

579
irqreturn_t xmon_irq(int irq, void *d)
P
Paul Mackerras 已提交
580 581 582 583
{
	unsigned long flags;
	local_irq_save(flags);
	printf("Keyboard interrupt\n");
584
	xmon(get_irq_regs());
P
Paul Mackerras 已提交
585 586 587
	local_irq_restore(flags);
	return IRQ_HANDLED;
}
L
Linus Torvalds 已提交
588

589
static int xmon_bpt(struct pt_regs *regs)
L
Linus Torvalds 已提交
590 591 592 593
{
	struct bpt *bp;
	unsigned long offset;

594
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
		return 0;

	/* Are we at the trap at bp->instr[1] for some bp? */
	bp = in_breakpoint_table(regs->nip, &offset);
	if (bp != NULL && offset == 4) {
		regs->nip = bp->address + 4;
		atomic_dec(&bp->ref_count);
		return 1;
	}

	/* Are we at a breakpoint? */
	bp = at_breakpoint(regs->nip);
	if (!bp)
		return 0;

	xmon_core(regs, 0);

	return 1;
}

615
static int xmon_sstep(struct pt_regs *regs)
L
Linus Torvalds 已提交
616 617 618 619 620 621 622
{
	if (user_mode(regs))
		return 0;
	xmon_core(regs, 0);
	return 1;
}

623
static int xmon_break_match(struct pt_regs *regs)
L
Linus Torvalds 已提交
624
{
625
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
L
Linus Torvalds 已提交
626
		return 0;
627 628
	if (dabr.enabled == 0)
		return 0;
L
Linus Torvalds 已提交
629 630 631 632
	xmon_core(regs, 0);
	return 1;
}

633
static int xmon_iabr_match(struct pt_regs *regs)
L
Linus Torvalds 已提交
634
{
635
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
L
Linus Torvalds 已提交
636
		return 0;
637
	if (iabr == NULL)
L
Linus Torvalds 已提交
638 639 640 641 642
		return 0;
	xmon_core(regs, 0);
	return 1;
}

643
static int xmon_ipi(struct pt_regs *regs)
L
Linus Torvalds 已提交
644 645
{
#ifdef CONFIG_SMP
646
	if (in_xmon && !cpumask_test_cpu(smp_processor_id(), &cpus_in_xmon))
L
Linus Torvalds 已提交
647 648 649 650 651
		xmon_core(regs, 1);
#endif
	return 0;
}

652
static int xmon_fault_handler(struct pt_regs *regs)
L
Linus Torvalds 已提交
653 654 655 656 657 658 659
{
	struct bpt *bp;
	unsigned long offset;

	if (in_xmon && catch_memory_errors)
		handle_fault(regs);	/* doesn't return */

660
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
L
Linus Torvalds 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
		bp = in_breakpoint_table(regs->nip, &offset);
		if (bp != NULL) {
			regs->nip = bp->address + offset;
			atomic_dec(&bp->ref_count);
		}
	}

	return 0;
}

static struct bpt *at_breakpoint(unsigned long pc)
{
	int i;
	struct bpt *bp;

	bp = bpts;
	for (i = 0; i < NBPTS; ++i, ++bp)
		if (bp->enabled && pc == bp->address)
			return bp;
	return NULL;
}

static struct bpt *in_breakpoint_table(unsigned long nip, unsigned long *offp)
{
	unsigned long off;

	off = nip - (unsigned long) bpts;
	if (off >= sizeof(bpts))
		return NULL;
	off %= sizeof(struct bpt);
	if (off != offsetof(struct bpt, instr[0])
	    && off != offsetof(struct bpt, instr[1]))
		return NULL;
	*offp = off - offsetof(struct bpt, instr[0]);
	return (struct bpt *) (nip - off);
}

static struct bpt *new_breakpoint(unsigned long a)
{
	struct bpt *bp;

	a &= ~3UL;
	bp = at_breakpoint(a);
	if (bp)
		return bp;

	for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
		if (!bp->enabled && atomic_read(&bp->ref_count) == 0) {
			bp->address = a;
			bp->instr[1] = bpinstr;
			store_inst(&bp->instr[1]);
			return bp;
		}
	}

	printf("Sorry, no free breakpoints.  Please clear one first.\n");
	return NULL;
}

static void insert_bpts(void)
{
	int i;
	struct bpt *bp;

	bp = bpts;
	for (i = 0; i < NBPTS; ++i, ++bp) {
		if ((bp->enabled & (BP_TRAP|BP_IABR)) == 0)
			continue;
		if (mread(bp->address, &bp->instr[0], 4) != 4) {
			printf("Couldn't read instruction at %lx, "
			       "disabling breakpoint there\n", bp->address);
			bp->enabled = 0;
			continue;
		}
		if (IS_MTMSRD(bp->instr[0]) || IS_RFID(bp->instr[0])) {
			printf("Breakpoint at %lx is on an mtmsrd or rfid "
			       "instruction, disabling it\n", bp->address);
			bp->enabled = 0;
			continue;
		}
		store_inst(&bp->instr[0]);
		if (bp->enabled & BP_IABR)
			continue;
		if (mwrite(bp->address, &bpinstr, 4) != 4) {
			printf("Couldn't write instruction at %lx, "
			       "disabling breakpoint there\n", bp->address);
			bp->enabled &= ~BP_TRAP;
			continue;
		}
		store_inst((void *)bp->address);
	}
}

static void insert_cpu_bpts(void)
{
756 757 758 759 760 761
	struct arch_hw_breakpoint brk;

	if (dabr.enabled) {
		brk.address = dabr.address;
		brk.type = (dabr.enabled & HW_BRK_TYPE_DABR) | HW_BRK_TYPE_PRIV_ALL;
		brk.len = 8;
762
		__set_breakpoint(&brk);
763
	}
L
Linus Torvalds 已提交
764
	if (iabr && cpu_has_feature(CPU_FTR_IABR))
P
Paul Mackerras 已提交
765
		mtspr(SPRN_IABR, iabr->address
L
Linus Torvalds 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
			 | (iabr->enabled & (BP_IABR|BP_IABR_TE)));
}

static void remove_bpts(void)
{
	int i;
	struct bpt *bp;
	unsigned instr;

	bp = bpts;
	for (i = 0; i < NBPTS; ++i, ++bp) {
		if ((bp->enabled & (BP_TRAP|BP_IABR)) != BP_TRAP)
			continue;
		if (mread(bp->address, &instr, 4) == 4
		    && instr == bpinstr
		    && mwrite(bp->address, &bp->instr, 4) != 4)
			printf("Couldn't remove breakpoint at %lx\n",
			       bp->address);
		else
			store_inst((void *)bp->address);
	}
}

static void remove_cpu_bpts(void)
{
791
	hw_breakpoint_disable();
L
Linus Torvalds 已提交
792
	if (cpu_has_feature(CPU_FTR_IABR))
P
Paul Mackerras 已提交
793
		mtspr(SPRN_IABR, 0);
L
Linus Torvalds 已提交
794 795 796 797 798 799 800 801 802 803 804 805
}

/* Command interpreting routine */
static char *last_cmd;

static int
cmds(struct pt_regs *excp)
{
	int cmd = 0;

	last_cmd = NULL;
	xmon_regs = excp;
806 807 808 809 810 811

	if (!xmon_no_auto_backtrace) {
		xmon_no_auto_backtrace = 1;
		xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
	}

L
Linus Torvalds 已提交
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
	for(;;) {
#ifdef CONFIG_SMP
		printf("%x:", smp_processor_id());
#endif /* CONFIG_SMP */
		printf("mon> ");
		flush_input();
		termch = 0;
		cmd = skipbl();
		if( cmd == '\n' ) {
			if (last_cmd == NULL)
				continue;
			take_input(last_cmd);
			last_cmd = NULL;
			cmd = inchar();
		}
		switch (cmd) {
		case 'm':
			cmd = inchar();
			switch (cmd) {
			case 'm':
			case 's':
			case 'd':
				memops(cmd);
				break;
			case 'l':
				memlocate();
				break;
			case 'z':
				memzcan();
				break;
			case 'i':
843
				show_mem(0);
L
Linus Torvalds 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
				break;
			default:
				termch = cmd;
				memex();
			}
			break;
		case 'd':
			dump();
			break;
		case 'l':
			symbol_lookup();
			break;
		case 'r':
			prregs(excp);	/* print regs */
			break;
		case 'e':
			excprint(excp);
			break;
		case 'S':
			super_regs();
			break;
		case 't':
			backtrace(excp);
			break;
		case 'f':
			cacheflush();
			break;
		case 's':
872 873
			if (do_spu_cmd() == 0)
				break;
L
Linus Torvalds 已提交
874 875 876 877 878
			if (do_step(excp))
				return cmd;
			break;
		case 'x':
		case 'X':
879
			return cmd;
L
Linus Torvalds 已提交
880
		case EOF:
881 882
			printf(" <no input ...>\n");
			mdelay(2000);
L
Linus Torvalds 已提交
883 884
			return cmd;
		case '?':
I
Ishizaki Kou 已提交
885
			xmon_puts(help_string);
L
Linus Torvalds 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898 899
			break;
		case 'b':
			bpt_cmds();
			break;
		case 'C':
			csum();
			break;
		case 'c':
			if (cpu_cmd())
				return 0;
			break;
		case 'z':
			bootcmds();
			break;
P
Paul Mackerras 已提交
900 901
		case 'p':
			proccall();
L
Linus Torvalds 已提交
902
			break;
P
Paul Mackerras 已提交
903
#ifdef CONFIG_PPC_STD_MMU
L
Linus Torvalds 已提交
904 905 906
		case 'u':
			dump_segments();
			break;
907
#elif defined(CONFIG_4xx)
908 909 910
		case 'u':
			dump_tlb_44x();
			break;
911
#elif defined(CONFIG_PPC_BOOK3E)
912 913 914
		case 'u':
			dump_tlb_book3e();
			break;
P
Paul Mackerras 已提交
915
#endif
L
Linus Torvalds 已提交
916 917
		default:
			printf("Unrecognized command: ");
918
			do {
L
Linus Torvalds 已提交
919 920 921 922 923
				if (' ' < cmd && cmd <= '~')
					putchar(cmd);
				else
					printf("\\x%x", cmd);
				cmd = inchar();
924
			} while (cmd != '\n');
L
Linus Torvalds 已提交
925 926 927 928 929 930
			printf(" (type ? for help)\n");
			break;
		}
	}
}

931 932 933 934 935 936 937 938
#ifdef CONFIG_BOOKE
static int do_step(struct pt_regs *regs)
{
	regs->msr |= MSR_DE;
	mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
	return 1;
}
#else
L
Linus Torvalds 已提交
939 940 941 942 943 944 945 946 947 948
/*
 * Step a single instruction.
 * Some instructions we emulate, others we execute with MSR_SE set.
 */
static int do_step(struct pt_regs *regs)
{
	unsigned int instr;
	int stepped;

	/* check we are in 64-bit kernel mode, translation enabled */
949
	if ((regs->msr & (MSR_64BIT|MSR_PR|MSR_IR)) == (MSR_64BIT|MSR_IR)) {
L
Linus Torvalds 已提交
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
		if (mread(regs->nip, &instr, 4) == 4) {
			stepped = emulate_step(regs, instr);
			if (stepped < 0) {
				printf("Couldn't single-step %s instruction\n",
				       (IS_RFID(instr)? "rfid": "mtmsrd"));
				return 0;
			}
			if (stepped > 0) {
				regs->trap = 0xd00 | (regs->trap & 1);
				printf("stepped to ");
				xmon_print_symbol(regs->nip, " ", "\n");
				ppc_inst_dump(regs->nip, 1, 0);
				return 0;
			}
		}
	}
	regs->msr |= MSR_SE;
	return 1;
}
969
#endif
L
Linus Torvalds 已提交
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

static void bootcmds(void)
{
	int cmd;

	cmd = inchar();
	if (cmd == 'r')
		ppc_md.restart(NULL);
	else if (cmd == 'h')
		ppc_md.halt();
	else if (cmd == 'p')
		ppc_md.power_off();
}

static int cpu_cmd(void)
{
#ifdef CONFIG_SMP
987
	unsigned long cpu, first_cpu, last_cpu;
L
Linus Torvalds 已提交
988 989 990 991 992
	int timeout;

	if (!scanhex(&cpu)) {
		/* print cpus waiting or in xmon */
		printf("cpus stopped:");
993
		last_cpu = first_cpu = NR_CPUS;
994
		for_each_possible_cpu(cpu) {
995
			if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
996 997 998 999
				if (cpu == last_cpu + 1) {
					last_cpu = cpu;
				} else {
					if (last_cpu != first_cpu)
1000
						printf("-0x%lx", last_cpu);
1001
					last_cpu = first_cpu = cpu;
1002
					printf(" 0x%lx", cpu);
1003
				}
L
Linus Torvalds 已提交
1004 1005
			}
		}
1006
		if (last_cpu != first_cpu)
1007
			printf("-0x%lx", last_cpu);
L
Linus Torvalds 已提交
1008 1009 1010 1011
		printf("\n");
		return 0;
	}
	/* try to switch to cpu specified */
1012
	if (!cpumask_test_cpu(cpu, &cpus_in_xmon)) {
L
Linus Torvalds 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
		printf("cpu 0x%x isn't in xmon\n", cpu);
		return 0;
	}
	xmon_taken = 0;
	mb();
	xmon_owner = cpu;
	timeout = 10000000;
	while (!xmon_taken) {
		if (--timeout == 0) {
			if (test_and_set_bit(0, &xmon_taken))
				break;
			/* take control back */
			mb();
			xmon_owner = smp_processor_id();
1027
			printf("cpu 0x%x didn't take control\n", cpu);
L
Linus Torvalds 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
			return 0;
		}
		barrier();
	}
	return 1;
#else
	return 0;
#endif /* CONFIG_SMP */
}

static unsigned short fcstab[256] = {
	0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
	0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
	0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
	0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
	0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
	0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
	0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
	0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
	0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
	0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
	0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
	0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
	0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
	0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
	0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
	0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
	0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
	0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
	0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
	0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
	0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
	0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
	0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
	0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
	0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
	0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
	0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
	0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
	0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
	0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
	0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
	0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
};

#define FCS(fcs, c)	(((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff])

static void
csum(void)
{
	unsigned int i;
	unsigned short fcs;
	unsigned char v;

	if (!scanhex(&adrs))
		return;
	if (!scanhex(&ncsum))
		return;
	fcs = 0xffff;
	for (i = 0; i < ncsum; ++i) {
		if (mread(adrs+i, &v, 1) == 0) {
1089
			printf("csum stopped at "REG"\n", adrs+i);
L
Linus Torvalds 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
			break;
		}
		fcs = FCS(fcs, v);
	}
	printf("%x\n", fcs);
}

/*
 * Check if this is a suitable place to put a breakpoint.
 */
static long check_bp_loc(unsigned long addr)
{
	unsigned int instr;

	addr &= ~3;
1105
	if (!is_kernel_addr(addr)) {
L
Linus Torvalds 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
		printf("Breakpoints may only be placed at kernel addresses\n");
		return 0;
	}
	if (!mread(addr, &instr, sizeof(instr))) {
		printf("Can't read instruction at address %lx\n", addr);
		return 0;
	}
	if (IS_MTMSRD(instr) || IS_RFID(instr)) {
		printf("Breakpoints may not be placed on mtmsrd or rfid "
		       "instructions\n");
		return 0;
	}
	return 1;
}

1121
static char *breakpoint_help_string =
L
Linus Torvalds 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
    "Breakpoint command usage:\n"
    "b                show breakpoints\n"
    "b <addr> [cnt]   set breakpoint at given instr addr\n"
    "bc               clear all breakpoints\n"
    "bc <n/addr>      clear breakpoint number n or at addr\n"
    "bi <addr> [cnt]  set hardware instr breakpoint (POWER3/RS64 only)\n"
    "bd <addr> [cnt]  set hardware data breakpoint\n"
    "";

static void
bpt_cmds(void)
{
	int cmd;
	unsigned long a;
	int mode, i;
	struct bpt *bp;
	const char badaddr[] = "Only kernel addresses are permitted "
		"for breakpoints\n";

	cmd = inchar();
	switch (cmd) {
P
Paul Mackerras 已提交
1143
#ifndef CONFIG_8xx
L
Linus Torvalds 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	case 'd':	/* bd - hardware data breakpoint */
		mode = 7;
		cmd = inchar();
		if (cmd == 'r')
			mode = 5;
		else if (cmd == 'w')
			mode = 6;
		else
			termch = cmd;
		dabr.address = 0;
		dabr.enabled = 0;
		if (scanhex(&dabr.address)) {
1156
			if (!is_kernel_addr(dabr.address)) {
L
Linus Torvalds 已提交
1157 1158 1159
				printf(badaddr);
				break;
			}
1160
			dabr.address &= ~HW_BRK_TYPE_DABR;
L
Linus Torvalds 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
			dabr.enabled = mode | BP_DABR;
		}
		break;

	case 'i':	/* bi - hardware instr breakpoint */
		if (!cpu_has_feature(CPU_FTR_IABR)) {
			printf("Hardware instruction breakpoint "
			       "not supported on this cpu\n");
			break;
		}
		if (iabr) {
			iabr->enabled &= ~(BP_IABR | BP_IABR_TE);
			iabr = NULL;
		}
		if (!scanhex(&a))
			break;
		if (!check_bp_loc(a))
			break;
		bp = new_breakpoint(a);
		if (bp != NULL) {
			bp->enabled |= BP_IABR | BP_IABR_TE;
			iabr = bp;
		}
		break;
P
Paul Mackerras 已提交
1185
#endif
L
Linus Torvalds 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203

	case 'c':
		if (!scanhex(&a)) {
			/* clear all breakpoints */
			for (i = 0; i < NBPTS; ++i)
				bpts[i].enabled = 0;
			iabr = NULL;
			dabr.enabled = 0;
			printf("All breakpoints cleared\n");
			break;
		}

		if (a <= NBPTS && a >= 1) {
			/* assume a breakpoint number */
			bp = &bpts[a-1];	/* bp nums are 1 based */
		} else {
			/* assume a breakpoint address */
			bp = at_breakpoint(a);
1204
			if (bp == NULL) {
1205
				printf("No breakpoint at %lx\n", a);
L
Linus Torvalds 已提交
1206 1207 1208 1209
				break;
			}
		}

1210
		printf("Cleared breakpoint %lx (", BP_NUM(bp));
L
Linus Torvalds 已提交
1211 1212 1213 1214 1215 1216
		xmon_print_symbol(bp->address, " ", ")\n");
		bp->enabled = 0;
		break;

	default:
		termch = cmd;
1217
		cmd = skipbl();
L
Linus Torvalds 已提交
1218 1219 1220 1221 1222 1223 1224 1225 1226
		if (cmd == '?') {
			printf(breakpoint_help_string);
			break;
		}
		termch = cmd;
		if (!scanhex(&a)) {
			/* print all breakpoints */
			printf("   type            address\n");
			if (dabr.enabled) {
P
Paul Mackerras 已提交
1227
				printf("   data   "REG"  [", dabr.address);
L
Linus Torvalds 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
				if (dabr.enabled & 1)
					printf("r");
				if (dabr.enabled & 2)
					printf("w");
				printf("]\n");
			}
			for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
				if (!bp->enabled)
					continue;
				printf("%2x %s   ", BP_NUM(bp),
				    (bp->enabled & BP_IABR)? "inst": "trap");
				xmon_print_symbol(bp->address, "  ", "\n");
			}
			break;
		}

		if (!check_bp_loc(a))
			break;
		bp = new_breakpoint(a);
		if (bp != NULL)
			bp->enabled |= BP_TRAP;
		break;
	}
}

/* Very cheap human name for vector lookup. */
static
const char *getvecname(unsigned long vec)
{
	char *ret;

	switch (vec) {
	case 0x100:	ret = "(System Reset)"; break;
	case 0x200:	ret = "(Machine Check)"; break;
	case 0x300:	ret = "(Data Access)"; break;
	case 0x380:	ret = "(Data SLB Access)"; break;
	case 0x400:	ret = "(Instruction Access)"; break;
	case 0x480:	ret = "(Instruction SLB Access)"; break;
	case 0x500:	ret = "(Hardware Interrupt)"; break;
	case 0x600:	ret = "(Alignment)"; break;
	case 0x700:	ret = "(Program Check)"; break;
	case 0x800:	ret = "(FPU Unavailable)"; break;
	case 0x900:	ret = "(Decrementer)"; break;
1271 1272
	case 0x980:	ret = "(Hypervisor Decrementer)"; break;
	case 0xa00:	ret = "(Doorbell)"; break;
L
Linus Torvalds 已提交
1273 1274
	case 0xc00:	ret = "(System Call)"; break;
	case 0xd00:	ret = "(Single Step)"; break;
1275 1276 1277
	case 0xe40:	ret = "(Emulation Assist)"; break;
	case 0xe60:	ret = "(HMI)"; break;
	case 0xe80:	ret = "(Hypervisor Doorbell)"; break;
L
Linus Torvalds 已提交
1278 1279 1280
	case 0xf00:	ret = "(Performance Monitor)"; break;
	case 0xf20:	ret = "(Altivec Unavailable)"; break;
	case 0x1300:	ret = "(Instruction Breakpoint)"; break;
1281 1282
	case 0x1500:	ret = "(Denormalisation)"; break;
	case 0x1700:	ret = "(Altivec Assist)"; break;
L
Linus Torvalds 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
	default: ret = "";
	}
	return ret;
}

static void get_function_bounds(unsigned long pc, unsigned long *startp,
				unsigned long *endp)
{
	unsigned long size, offset;
	const char *name;

	*startp = *endp = 0;
	if (pc == 0)
		return;
	if (setjmp(bus_error_jmp) == 0) {
		catch_memory_errors = 1;
		sync();
A
Alexey Dobriyan 已提交
1300
		name = kallsyms_lookup(pc, &size, &offset, NULL, tmpstr);
L
Linus Torvalds 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309
		if (name != NULL) {
			*startp = pc - offset;
			*endp = pc - offset + size;
		}
		sync();
	}
	catch_memory_errors = 0;
}

1310 1311 1312
#define LRSAVE_OFFSET		(STACK_FRAME_LR_SAVE * sizeof(unsigned long))
#define MARKER_OFFSET		(STACK_FRAME_MARKER * sizeof(unsigned long))

L
Linus Torvalds 已提交
1313 1314 1315
static void xmon_show_stack(unsigned long sp, unsigned long lr,
			    unsigned long pc)
{
1316
	int max_to_print = 64;
L
Linus Torvalds 已提交
1317 1318 1319 1320 1321
	unsigned long ip;
	unsigned long newsp;
	unsigned long marker;
	struct pt_regs regs;

1322
	while (max_to_print--) {
L
Linus Torvalds 已提交
1323 1324 1325 1326 1327 1328
		if (sp < PAGE_OFFSET) {
			if (sp != 0)
				printf("SP (%lx) is in userspace\n", sp);
			break;
		}

P
Paul Mackerras 已提交
1329
		if (!mread(sp + LRSAVE_OFFSET, &ip, sizeof(unsigned long))
L
Linus Torvalds 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
		    || !mread(sp, &newsp, sizeof(unsigned long))) {
			printf("Couldn't read stack frame at %lx\n", sp);
			break;
		}

		/*
		 * For the first stack frame, try to work out if
		 * LR and/or the saved LR value in the bottommost
		 * stack frame are valid.
		 */
		if ((pc | lr) != 0) {
			unsigned long fnstart, fnend;
			unsigned long nextip;
			int printip = 1;

			get_function_bounds(pc, &fnstart, &fnend);
			nextip = 0;
			if (newsp > sp)
P
Paul Mackerras 已提交
1348
				mread(newsp + LRSAVE_OFFSET, &nextip,
L
Linus Torvalds 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
				      sizeof(unsigned long));
			if (lr == ip) {
				if (lr < PAGE_OFFSET
				    || (fnstart <= lr && lr < fnend))
					printip = 0;
			} else if (lr == nextip) {
				printip = 0;
			} else if (lr >= PAGE_OFFSET
				   && !(fnstart <= lr && lr < fnend)) {
				printf("[link register   ] ");
				xmon_print_symbol(lr, " ", "\n");
			}
			if (printip) {
P
Paul Mackerras 已提交
1362
				printf("["REG"] ", sp);
L
Linus Torvalds 已提交
1363 1364 1365 1366 1367
				xmon_print_symbol(ip, " ", " (unreliable)\n");
			}
			pc = lr = 0;

		} else {
P
Paul Mackerras 已提交
1368
			printf("["REG"] ", sp);
L
Linus Torvalds 已提交
1369 1370 1371 1372 1373
			xmon_print_symbol(ip, " ", "\n");
		}

		/* Look for "regshere" marker to see if this is
		   an exception frame. */
P
Paul Mackerras 已提交
1374
		if (mread(sp + MARKER_OFFSET, &marker, sizeof(unsigned long))
1375
		    && marker == STACK_FRAME_REGS_MARKER) {
1376
			if (mread(sp + STACK_FRAME_OVERHEAD, &regs, sizeof(regs))
L
Linus Torvalds 已提交
1377 1378
			    != sizeof(regs)) {
				printf("Couldn't read registers at %lx\n",
1379
				       sp + STACK_FRAME_OVERHEAD);
L
Linus Torvalds 已提交
1380 1381
				break;
			}
1382
			printf("--- Exception: %lx %s at ", regs.trap,
L
Linus Torvalds 已提交
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
			       getvecname(TRAP(&regs)));
			pc = regs.nip;
			lr = regs.link;
			xmon_print_symbol(pc, " ", "\n");
		}

		if (newsp == 0)
			break;

		sp = newsp;
1393
	}
L
Linus Torvalds 已提交
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
}

static void backtrace(struct pt_regs *excp)
{
	unsigned long sp;

	if (scanhex(&sp))
		xmon_show_stack(sp, 0, 0);
	else
		xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
	scannl();
}

static void print_bug_trap(struct pt_regs *regs)
{
1409
#ifdef CONFIG_BUG
1410
	const struct bug_entry *bug;
L
Linus Torvalds 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
	unsigned long addr;

	if (regs->msr & MSR_PR)
		return;		/* not in kernel */
	addr = regs->nip;	/* address of trap instruction */
	if (addr < PAGE_OFFSET)
		return;
	bug = find_bug(regs->nip);
	if (bug == NULL)
		return;
1421
	if (is_warning_bug(bug))
L
Linus Torvalds 已提交
1422 1423
		return;

1424
#ifdef CONFIG_DEBUG_BUGVERBOSE
1425 1426
	printf("kernel BUG at %s:%u!\n",
	       bug->file, bug->line);
1427 1428 1429
#else
	printf("kernel BUG at %p!\n", (void *)bug->bug_addr);
#endif
1430
#endif /* CONFIG_BUG */
L
Linus Torvalds 已提交
1431 1432
}

1433
static void excprint(struct pt_regs *fp)
L
Linus Torvalds 已提交
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
{
	unsigned long trap;

#ifdef CONFIG_SMP
	printf("cpu 0x%x: ", smp_processor_id());
#endif /* CONFIG_SMP */

	trap = TRAP(fp);
	printf("Vector: %lx %s at [%lx]\n", fp->trap, getvecname(trap), fp);
	printf("    pc: ");
	xmon_print_symbol(fp->nip, ": ", "\n");

	printf("    lr: ", fp->link);
	xmon_print_symbol(fp->link, ": ", "\n");

	printf("    sp: %lx\n", fp->gpr[1]);
	printf("   msr: %lx\n", fp->msr);

1452
	if (trap == 0x300 || trap == 0x380 || trap == 0x600 || trap == 0x200) {
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457 1458
		printf("   dar: %lx\n", fp->dar);
		if (trap != 0x380)
			printf(" dsisr: %lx\n", fp->dsisr);
	}

	printf("  current = 0x%lx\n", current);
P
Paul Mackerras 已提交
1459
#ifdef CONFIG_PPC64
1460 1461
	printf("  paca    = 0x%lx\t softe: %d\t irq_happened: 0x%02x\n",
	       local_paca, local_paca->soft_enabled, local_paca->irq_happened);
P
Paul Mackerras 已提交
1462
#endif
L
Linus Torvalds 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471
	if (current) {
		printf("    pid   = %ld, comm = %s\n",
		       current->pid, current->comm);
	}

	if (trap == 0x700)
		print_bug_trap(fp);
}

1472
static void prregs(struct pt_regs *fp)
L
Linus Torvalds 已提交
1473
{
P
Paul Mackerras 已提交
1474
	int n, trap;
L
Linus Torvalds 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
	unsigned long base;
	struct pt_regs regs;

	if (scanhex(&base)) {
		if (setjmp(bus_error_jmp) == 0) {
			catch_memory_errors = 1;
			sync();
			regs = *(struct pt_regs *)base;
			sync();
			__delay(200);
		} else {
			catch_memory_errors = 0;
P
Paul Mackerras 已提交
1487
			printf("*** Error reading registers from "REG"\n",
L
Linus Torvalds 已提交
1488 1489 1490 1491 1492 1493 1494
			       base);
			return;
		}
		catch_memory_errors = 0;
		fp = &regs;
	}

P
Paul Mackerras 已提交
1495
#ifdef CONFIG_PPC64
L
Linus Torvalds 已提交
1496 1497
	if (FULL_REGS(fp)) {
		for (n = 0; n < 16; ++n)
P
Paul Mackerras 已提交
1498
			printf("R%.2ld = "REG"   R%.2ld = "REG"\n",
L
Linus Torvalds 已提交
1499 1500 1501
			       n, fp->gpr[n], n+16, fp->gpr[n+16]);
	} else {
		for (n = 0; n < 7; ++n)
P
Paul Mackerras 已提交
1502
			printf("R%.2ld = "REG"   R%.2ld = "REG"\n",
L
Linus Torvalds 已提交
1503 1504
			       n, fp->gpr[n], n+7, fp->gpr[n+7]);
	}
P
Paul Mackerras 已提交
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
#else
	for (n = 0; n < 32; ++n) {
		printf("R%.2d = %.8x%s", n, fp->gpr[n],
		       (n & 3) == 3? "\n": "   ");
		if (n == 12 && !FULL_REGS(fp)) {
			printf("\n");
			break;
		}
	}
#endif
L
Linus Torvalds 已提交
1515 1516
	printf("pc  = ");
	xmon_print_symbol(fp->nip, " ", "\n");
1517 1518 1519 1520
	if (TRAP(fp) != 0xc00 && cpu_has_feature(CPU_FTR_CFAR)) {
		printf("cfar= ");
		xmon_print_symbol(fp->orig_gpr3, " ", "\n");
	}
L
Linus Torvalds 已提交
1521 1522
	printf("lr  = ");
	xmon_print_symbol(fp->link, " ", "\n");
P
Paul Mackerras 已提交
1523 1524
	printf("msr = "REG"   cr  = %.8lx\n", fp->msr, fp->ccr);
	printf("ctr = "REG"   xer = "REG"   trap = %4lx\n",
L
Linus Torvalds 已提交
1525
	       fp->ctr, fp->xer, fp->trap);
P
Paul Mackerras 已提交
1526 1527 1528
	trap = TRAP(fp);
	if (trap == 0x300 || trap == 0x380 || trap == 0x600)
		printf("dar = "REG"   dsisr = %.8lx\n", fp->dar, fp->dsisr);
L
Linus Torvalds 已提交
1529 1530
}

1531
static void cacheflush(void)
L
Linus Torvalds 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
{
	int cmd;
	unsigned long nflush;

	cmd = inchar();
	if (cmd != 'i')
		termch = cmd;
	scanhex((void *)&adrs);
	if (termch != '\n')
		termch = 0;
	nflush = 1;
	scanhex(&nflush);
	nflush = (nflush + L1_CACHE_BYTES - 1) / L1_CACHE_BYTES;
	if (setjmp(bus_error_jmp) == 0) {
		catch_memory_errors = 1;
		sync();

		if (cmd != 'i') {
			for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
				cflush((void *) adrs);
		} else {
			for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
				cinval((void *) adrs);
		}
		sync();
		/* wait a little while to see if we get a machine check */
		__delay(200);
	}
	catch_memory_errors = 0;
}

1563
static unsigned long
L
Linus Torvalds 已提交
1564 1565 1566 1567 1568
read_spr(int n)
{
	unsigned int instrs[2];
	unsigned long (*code)(void);
	unsigned long ret = -1UL;
1569 1570
#ifdef CONFIG_PPC64
	unsigned long opd[3];
L
Linus Torvalds 已提交
1571 1572 1573 1574

	opd[0] = (unsigned long)instrs;
	opd[1] = 0;
	opd[2] = 0;
1575 1576 1577 1578 1579 1580 1581 1582
	code = (unsigned long (*)(void)) opd;
#else
	code = (unsigned long (*)(void)) instrs;
#endif

	/* mfspr r3,n; blr */
	instrs[0] = 0x7c6002a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
	instrs[1] = 0x4e800020;
L
Linus Torvalds 已提交
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
	store_inst(instrs);
	store_inst(instrs+1);

	if (setjmp(bus_error_jmp) == 0) {
		catch_memory_errors = 1;
		sync();

		ret = code();

		sync();
		/* wait a little while to see if we get a machine check */
		__delay(200);
		n = size;
	}

	return ret;
}

1601
static void
L
Linus Torvalds 已提交
1602 1603 1604 1605
write_spr(int n, unsigned long val)
{
	unsigned int instrs[2];
	unsigned long (*code)(unsigned long);
1606
#ifdef CONFIG_PPC64
L
Linus Torvalds 已提交
1607 1608 1609 1610 1611
	unsigned long opd[3];

	opd[0] = (unsigned long)instrs;
	opd[1] = 0;
	opd[2] = 0;
1612 1613 1614 1615 1616 1617 1618
	code = (unsigned long (*)(unsigned long)) opd;
#else
	code = (unsigned long (*)(unsigned long)) instrs;
#endif

	instrs[0] = 0x7c6003a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
	instrs[1] = 0x4e800020;
L
Linus Torvalds 已提交
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
	store_inst(instrs);
	store_inst(instrs+1);

	if (setjmp(bus_error_jmp) == 0) {
		catch_memory_errors = 1;
		sync();

		code(val);

		sync();
		/* wait a little while to see if we get a machine check */
		__delay(200);
		n = size;
	}
}

static unsigned long regno;
extern char exc_prolog;
extern char dec_exc;

1639
static void super_regs(void)
L
Linus Torvalds 已提交
1640 1641 1642 1643 1644 1645
{
	int cmd;
	unsigned long val;

	cmd = skipbl();
	if (cmd == '\n') {
1646
		unsigned long sp, toc;
L
Linus Torvalds 已提交
1647 1648 1649
		asm("mr %0,1" : "=r" (sp) :);
		asm("mr %0,2" : "=r" (toc) :);

P
Paul Mackerras 已提交
1650 1651 1652
		printf("msr  = "REG"  sprg0= "REG"\n",
		       mfmsr(), mfspr(SPRN_SPRG0));
		printf("pvr  = "REG"  sprg1= "REG"\n",
1653
		       mfspr(SPRN_PVR), mfspr(SPRN_SPRG1));
P
Paul Mackerras 已提交
1654 1655 1656 1657
		printf("dec  = "REG"  sprg2= "REG"\n",
		       mfspr(SPRN_DEC), mfspr(SPRN_SPRG2));
		printf("sp   = "REG"  sprg3= "REG"\n", sp, mfspr(SPRN_SPRG3));
		printf("toc  = "REG"  dar  = "REG"\n", toc, mfspr(SPRN_DAR));
L
Linus Torvalds 已提交
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678

		return;
	}

	scanhex(&regno);
	switch (cmd) {
	case 'w':
		val = read_spr(regno);
		scanhex(&val);
		write_spr(regno, val);
		/* fall through */
	case 'r':
		printf("spr %lx = %lx\n", regno, read_spr(regno));
		break;
	}
	scannl();
}

/*
 * Stuff for reading and writing memory safely
 */
1679
static int
L
Linus Torvalds 已提交
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
mread(unsigned long adrs, void *buf, int size)
{
	volatile int n;
	char *p, *q;

	n = 0;
	if (setjmp(bus_error_jmp) == 0) {
		catch_memory_errors = 1;
		sync();
		p = (char *)adrs;
		q = (char *)buf;
		switch (size) {
		case 2:
P
Paul Mackerras 已提交
1693
			*(u16 *)q = *(u16 *)p;
L
Linus Torvalds 已提交
1694 1695
			break;
		case 4:
P
Paul Mackerras 已提交
1696
			*(u32 *)q = *(u32 *)p;
L
Linus Torvalds 已提交
1697 1698
			break;
		case 8:
P
Paul Mackerras 已提交
1699
			*(u64 *)q = *(u64 *)p;
L
Linus Torvalds 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
			break;
		default:
			for( ; n < size; ++n) {
				*q++ = *p++;
				sync();
			}
		}
		sync();
		/* wait a little while to see if we get a machine check */
		__delay(200);
		n = size;
	}
	catch_memory_errors = 0;
	return n;
}

1716
static int
L
Linus Torvalds 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
mwrite(unsigned long adrs, void *buf, int size)
{
	volatile int n;
	char *p, *q;

	n = 0;
	if (setjmp(bus_error_jmp) == 0) {
		catch_memory_errors = 1;
		sync();
		p = (char *) adrs;
		q = (char *) buf;
		switch (size) {
		case 2:
P
Paul Mackerras 已提交
1730
			*(u16 *)p = *(u16 *)q;
L
Linus Torvalds 已提交
1731 1732
			break;
		case 4:
P
Paul Mackerras 已提交
1733
			*(u32 *)p = *(u32 *)q;
L
Linus Torvalds 已提交
1734 1735
			break;
		case 8:
P
Paul Mackerras 已提交
1736
			*(u64 *)p = *(u64 *)q;
L
Linus Torvalds 已提交
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
			break;
		default:
			for ( ; n < size; ++n) {
				*p++ = *q++;
				sync();
			}
		}
		sync();
		/* wait a little while to see if we get a machine check */
		__delay(200);
		n = size;
	} else {
1749
		printf("*** Error writing address "REG"\n", adrs + n);
L
Linus Torvalds 已提交
1750 1751 1752 1753 1754 1755
	}
	catch_memory_errors = 0;
	return n;
}

static int fault_type;
P
Paul Mackerras 已提交
1756
static int fault_except;
L
Linus Torvalds 已提交
1757 1758
static char *fault_chars[] = { "--", "**", "##" };

P
Paul Mackerras 已提交
1759
static int handle_fault(struct pt_regs *regs)
L
Linus Torvalds 已提交
1760
{
P
Paul Mackerras 已提交
1761
	fault_except = TRAP(regs);
L
Linus Torvalds 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
	switch (TRAP(regs)) {
	case 0x200:
		fault_type = 0;
		break;
	case 0x300:
	case 0x380:
		fault_type = 1;
		break;
	default:
		fault_type = 2;
	}

	longjmp(bus_error_jmp, 1);

	return 0;
}

#define SWAP(a, b, t)	((t) = (a), (a) = (b), (b) = (t))

1781
static void
L
Linus Torvalds 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
byterev(unsigned char *val, int size)
{
	int t;
	
	switch (size) {
	case 2:
		SWAP(val[0], val[1], t);
		break;
	case 4:
		SWAP(val[0], val[3], t);
		SWAP(val[1], val[2], t);
		break;
	case 8: /* is there really any use for this? */
		SWAP(val[0], val[7], t);
		SWAP(val[1], val[6], t);
		SWAP(val[2], val[5], t);
		SWAP(val[3], val[4], t);
		break;
	}
}

static int brev;
static int mnoread;

1806
static char *memex_help_string =
L
Linus Torvalds 已提交
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
    "Memory examine command usage:\n"
    "m [addr] [flags] examine/change memory\n"
    "  addr is optional.  will start where left off.\n"
    "  flags may include chars from this set:\n"
    "    b   modify by bytes (default)\n"
    "    w   modify by words (2 byte)\n"
    "    l   modify by longs (4 byte)\n"
    "    d   modify by doubleword (8 byte)\n"
    "    r   toggle reverse byte order mode\n"
    "    n   do not read memory (for i/o spaces)\n"
    "    .   ok to read (default)\n"
    "NOTE: flags are saved as defaults\n"
    "";

1821
static char *memex_subcmd_help_string =
L
Linus Torvalds 已提交
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
    "Memory examine subcommands:\n"
    "  hexval   write this val to current location\n"
    "  'string' write chars from string to this location\n"
    "  '        increment address\n"
    "  ^        decrement address\n"
    "  /        increment addr by 0x10.  //=0x100, ///=0x1000, etc\n"
    "  \\        decrement addr by 0x10.  \\\\=0x100, \\\\\\=0x1000, etc\n"
    "  `        clear no-read flag\n"
    "  ;        stay at this addr\n"
    "  v        change to byte mode\n"
    "  w        change to word (2 byte) mode\n"
    "  l        change to long (4 byte) mode\n"
    "  u        change to doubleword (8 byte) mode\n"
    "  m addr   change current addr\n"
    "  n        toggle no-read flag\n"
    "  r        toggle byte reverse flag\n"
    "  < count  back up count bytes\n"
    "  > count  skip forward count bytes\n"
    "  x        exit this mode\n"
    "";

1843
static void
L
Linus Torvalds 已提交
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
memex(void)
{
	int cmd, inc, i, nslash;
	unsigned long n;
	unsigned char val[16];

	scanhex((void *)&adrs);
	cmd = skipbl();
	if (cmd == '?') {
		printf(memex_help_string);
		return;
	} else {
		termch = cmd;
	}
	last_cmd = "m\n";
	while ((cmd = skipbl()) != '\n') {
		switch( cmd ){
		case 'b':	size = 1;	break;
		case 'w':	size = 2;	break;
		case 'l':	size = 4;	break;
		case 'd':	size = 8;	break;
		case 'r': 	brev = !brev;	break;
		case 'n':	mnoread = 1;	break;
		case '.':	mnoread = 0;	break;
		}
	}
	if( size <= 0 )
		size = 1;
	else if( size > 8 )
		size = 8;
	for(;;){
		if (!mnoread)
			n = mread(adrs, val, size);
P
Paul Mackerras 已提交
1877
		printf(REG"%c", adrs, brev? 'r': ' ');
L
Linus Torvalds 已提交
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
		if (!mnoread) {
			if (brev)
				byterev(val, size);
			putchar(' ');
			for (i = 0; i < n; ++i)
				printf("%.2x", val[i]);
			for (; i < size; ++i)
				printf("%s", fault_chars[fault_type]);
		}
		putchar(' ');
		inc = size;
		nslash = 0;
		for(;;){
			if( scanhex(&n) ){
				for (i = 0; i < size; ++i)
					val[i] = n >> (i * 8);
				if (!brev)
					byterev(val, size);
				mwrite(adrs, val, size);
				inc = size;
			}
			cmd = skipbl();
			if (cmd == '\n')
				break;
			inc = 0;
			switch (cmd) {
			case '\'':
				for(;;){
					n = inchar();
					if( n == '\\' )
						n = bsesc();
					else if( n == '\'' )
						break;
					for (i = 0; i < size; ++i)
						val[i] = n >> (i * 8);
					if (!brev)
						byterev(val, size);
					mwrite(adrs, val, size);
					adrs += size;
				}
				adrs -= size;
				inc = size;
				break;
			case ',':
				adrs += size;
				break;
			case '.':
				mnoread = 0;
				break;
			case ';':
				break;
			case 'x':
			case EOF:
				scannl();
				return;
			case 'b':
			case 'v':
				size = 1;
				break;
			case 'w':
				size = 2;
				break;
			case 'l':
				size = 4;
				break;
			case 'u':
				size = 8;
				break;
			case '^':
				adrs -= size;
				break;
				break;
			case '/':
				if (nslash > 0)
					adrs -= 1 << nslash;
				else
					nslash = 0;
				nslash += 4;
				adrs += 1 << nslash;
				break;
			case '\\':
				if (nslash < 0)
					adrs += 1 << -nslash;
				else
					nslash = 0;
				nslash -= 4;
				adrs -= 1 << -nslash;
				break;
			case 'm':
				scanhex((void *)&adrs);
				break;
			case 'n':
				mnoread = 1;
				break;
			case 'r':
				brev = !brev;
				break;
			case '<':
				n = size;
				scanhex(&n);
				adrs -= n;
				break;
			case '>':
				n = size;
				scanhex(&n);
				adrs += n;
				break;
			case '?':
				printf(memex_subcmd_help_string);
				break;
			}
		}
		adrs += inc;
	}
}

1994
static int
L
Linus Torvalds 已提交
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
bsesc(void)
{
	int c;

	c = inchar();
	switch( c ){
	case 'n':	c = '\n';	break;
	case 'r':	c = '\r';	break;
	case 'b':	c = '\b';	break;
	case 't':	c = '\t';	break;
	}
	return c;
}

2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
static void xmon_rawdump (unsigned long adrs, long ndump)
{
	long n, m, r, nr;
	unsigned char temp[16];

	for (n = ndump; n > 0;) {
		r = n < 16? n: 16;
		nr = mread(adrs, temp, r);
		adrs += nr;
		for (m = 0; m < r; ++m) {
			if (m < nr)
				printf("%.2x", temp[m]);
			else
				printf("%s", fault_chars[fault_type]);
		}
		n -= r;
		if (nr < r)
			break;
	}
	printf("\n");
}

2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
#ifdef CONFIG_PPC64
static void dump_one_paca(int cpu)
{
	struct paca_struct *p;

	if (setjmp(bus_error_jmp) != 0) {
		printf("*** Error dumping paca for cpu 0x%x!\n", cpu);
		return;
	}

	catch_memory_errors = 1;
	sync();

	p = &paca[cpu];

	printf("paca for cpu 0x%x @ %p:\n", cpu, p);

	printf(" %-*s = %s\n", 16, "possible", cpu_possible(cpu) ? "yes" : "no");
	printf(" %-*s = %s\n", 16, "present", cpu_present(cpu) ? "yes" : "no");
	printf(" %-*s = %s\n", 16, "online", cpu_online(cpu) ? "yes" : "no");

#define DUMP(paca, name, format) \
	printf(" %-*s = %#-*"format"\t(0x%lx)\n", 16, #name, 18, paca->name, \
		offsetof(struct paca_struct, name));

	DUMP(p, lock_token, "x");
	DUMP(p, paca_index, "x");
	DUMP(p, kernel_toc, "lx");
	DUMP(p, kernelbase, "lx");
	DUMP(p, kernel_msr, "lx");
#ifdef CONFIG_PPC_STD_MMU_64
	DUMP(p, stab_real, "lx");
	DUMP(p, stab_addr, "lx");
#endif
	DUMP(p, emergency_sp, "p");
2066 2067 2068 2069
#ifdef CONFIG_PPC_BOOK3S_64
	DUMP(p, mc_emergency_sp, "p");
	DUMP(p, in_mce, "x");
#endif
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
	DUMP(p, data_offset, "lx");
	DUMP(p, hw_cpu_id, "x");
	DUMP(p, cpu_start, "x");
	DUMP(p, kexec_state, "x");
	DUMP(p, __current, "p");
	DUMP(p, kstack, "lx");
	DUMP(p, stab_rr, "lx");
	DUMP(p, saved_r1, "lx");
	DUMP(p, trap_save, "x");
	DUMP(p, soft_enabled, "x");
	DUMP(p, irq_happened, "x");
	DUMP(p, io_sync, "x");
	DUMP(p, irq_work_pending, "x");
	DUMP(p, nap_state_lost, "x");

#undef DUMP

	catch_memory_errors = 0;
	sync();
}

static void dump_all_pacas(void)
{
	int cpu;

	if (num_possible_cpus() == 0) {
		printf("No possible cpus, use 'dp #' to dump individual cpus\n");
		return;
	}

	for_each_possible_cpu(cpu)
		dump_one_paca(cpu);
}

static void dump_pacas(void)
{
	unsigned long num;
	int c;

	c = inchar();
	if (c == 'a') {
		dump_all_pacas();
		return;
	}

	termch = c;	/* Put c back, it wasn't 'a' */

	if (scanhex(&num))
		dump_one_paca(num);
	else
		dump_one_paca(xmon_owner);
}
#endif

L
Linus Torvalds 已提交
2124 2125 2126
#define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
			 || ('a' <= (c) && (c) <= 'f') \
			 || ('A' <= (c) && (c) <= 'F'))
2127
static void
L
Linus Torvalds 已提交
2128 2129 2130 2131 2132
dump(void)
{
	int c;

	c = inchar();
2133 2134 2135 2136 2137 2138 2139 2140

#ifdef CONFIG_PPC64
	if (c == 'p') {
		dump_pacas();
		return;
	}
#endif

L
Linus Torvalds 已提交
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
	if ((isxdigit(c) && c != 'f' && c != 'd') || c == '\n')
		termch = c;
	scanhex((void *)&adrs);
	if (termch != '\n')
		termch = 0;
	if (c == 'i') {
		scanhex(&nidump);
		if (nidump == 0)
			nidump = 16;
		else if (nidump > MAX_DUMP)
			nidump = MAX_DUMP;
		adrs += ppc_inst_dump(adrs, nidump, 1);
		last_cmd = "di\n";
2154 2155
	} else if (c == 'l') {
		dump_log_buf();
2156 2157 2158 2159 2160 2161 2162
	} else if (c == 'r') {
		scanhex(&ndump);
		if (ndump == 0)
			ndump = 64;
		xmon_rawdump(adrs, ndump);
		adrs += ndump;
		last_cmd = "dr\n";
L
Linus Torvalds 已提交
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
	} else {
		scanhex(&ndump);
		if (ndump == 0)
			ndump = 64;
		else if (ndump > MAX_DUMP)
			ndump = MAX_DUMP;
		prdump(adrs, ndump);
		adrs += ndump;
		last_cmd = "d\n";
	}
}

2175
static void
L
Linus Torvalds 已提交
2176 2177 2178 2179 2180 2181
prdump(unsigned long adrs, long ndump)
{
	long n, m, c, r, nr;
	unsigned char temp[16];

	for (n = ndump; n > 0;) {
P
Paul Mackerras 已提交
2182
		printf(REG, adrs);
L
Linus Torvalds 已提交
2183 2184 2185 2186 2187
		putchar(' ');
		r = n < 16? n: 16;
		nr = mread(adrs, temp, r);
		adrs += nr;
		for (m = 0; m < r; ++m) {
2188
			if ((m & (sizeof(long) - 1)) == 0 && m > 0)
P
Paul Mackerras 已提交
2189
				putchar(' ');
L
Linus Torvalds 已提交
2190 2191 2192 2193 2194
			if (m < nr)
				printf("%.2x", temp[m]);
			else
				printf("%s", fault_chars[fault_type]);
		}
P
Paul Mackerras 已提交
2195
		for (; m < 16; ++m) {
2196
			if ((m & (sizeof(long) - 1)) == 0)
P
Paul Mackerras 已提交
2197
				putchar(' ');
L
Linus Torvalds 已提交
2198
			printf("  ");
P
Paul Mackerras 已提交
2199
		}
L
Linus Torvalds 已提交
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
		printf("  |");
		for (m = 0; m < r; ++m) {
			if (m < nr) {
				c = temp[m];
				putchar(' ' <= c && c <= '~'? c: '.');
			} else
				putchar(' ');
		}
		n -= r;
		for (; m < 16; ++m)
			putchar(' ');
		printf("|\n");
		if (nr < r)
			break;
	}
}

2217 2218
typedef int (*instruction_dump_func)(unsigned long inst, unsigned long addr);

2219
static int
2220 2221
generic_inst_dump(unsigned long adr, long count, int praddr,
			instruction_dump_func dump_func)
L
Linus Torvalds 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
{
	int nr, dotted;
	unsigned long first_adr;
	unsigned long inst, last_inst = 0;
	unsigned char val[4];

	dotted = 0;
	for (first_adr = adr; count > 0; --count, adr += 4) {
		nr = mread(adr, val, 4);
		if (nr == 0) {
			if (praddr) {
				const char *x = fault_chars[fault_type];
P
Paul Mackerras 已提交
2234
				printf(REG"  %s%s%s%s\n", adr, x, x, x, x);
L
Linus Torvalds 已提交
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
			}
			break;
		}
		inst = GETWORD(val);
		if (adr > first_adr && inst == last_inst) {
			if (!dotted) {
				printf(" ...\n");
				dotted = 1;
			}
			continue;
		}
		dotted = 0;
		last_inst = inst;
		if (praddr)
P
Paul Mackerras 已提交
2249
			printf(REG"  %.8x", adr, inst);
L
Linus Torvalds 已提交
2250
		printf("\t");
2251
		dump_func(inst, adr);
L
Linus Torvalds 已提交
2252 2253 2254 2255 2256
		printf("\n");
	}
	return adr - first_adr;
}

2257
static int
2258 2259 2260 2261 2262
ppc_inst_dump(unsigned long adr, long count, int praddr)
{
	return generic_inst_dump(adr, count, praddr, print_insn_powerpc);
}

L
Linus Torvalds 已提交
2263 2264 2265 2266 2267 2268
void
print_address(unsigned long addr)
{
	xmon_print_symbol(addr, "\t# ", "");
}

2269 2270 2271
void
dump_log_buf(void)
{
2272 2273 2274
	struct kmsg_dumper dumper = { .active = 1 };
	unsigned char buf[128];
	size_t len;
2275

2276
	if (setjmp(bus_error_jmp) != 0) {
2277
		printf("Error dumping printk buffer!\n");
2278 2279
		return;
	}
2280

2281 2282
	catch_memory_errors = 1;
	sync();
2283

2284 2285 2286 2287 2288
	kmsg_dump_rewind_nolock(&dumper);
	while (kmsg_dump_get_line_nolock(&dumper, false, buf, sizeof(buf), &len)) {
		buf[len] = '\0';
		printf("%s", buf);
	}
2289

2290 2291 2292 2293
	sync();
	/* wait a little while to see if we get a machine check */
	__delay(200);
	catch_memory_errors = 0;
2294
}
L
Linus Torvalds 已提交
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304

/*
 * Memory operations - move, set, print differences
 */
static unsigned long mdest;		/* destination address */
static unsigned long msrc;		/* source address */
static unsigned long mval;		/* byte value to set memory to */
static unsigned long mcount;		/* # bytes to affect */
static unsigned long mdiffs;		/* max # differences to print */

2305
static void
L
Linus Torvalds 已提交
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
memops(int cmd)
{
	scanhex((void *)&mdest);
	if( termch != '\n' )
		termch = 0;
	scanhex((void *)(cmd == 's'? &mval: &msrc));
	if( termch != '\n' )
		termch = 0;
	scanhex((void *)&mcount);
	switch( cmd ){
	case 'm':
		memmove((void *)mdest, (void *)msrc, mcount);
		break;
	case 's':
		memset((void *)mdest, mval, mcount);
		break;
	case 'd':
		if( termch != '\n' )
			termch = 0;
		scanhex((void *)&mdiffs);
		memdiffs((unsigned char *)mdest, (unsigned char *)msrc, mcount, mdiffs);
		break;
	}
}

2331
static void
L
Linus Torvalds 已提交
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
memdiffs(unsigned char *p1, unsigned char *p2, unsigned nb, unsigned maxpr)
{
	unsigned n, prt;

	prt = 0;
	for( n = nb; n > 0; --n )
		if( *p1++ != *p2++ )
			if( ++prt <= maxpr )
				printf("%.16x %.2x # %.16x %.2x\n", p1 - 1,
					p1[-1], p2 - 1, p2[-1]);
	if( prt > maxpr )
		printf("Total of %d differences\n", prt);
}

static unsigned mend;
static unsigned mask;

2349
static void
L
Linus Torvalds 已提交
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
memlocate(void)
{
	unsigned a, n;
	unsigned char val[4];

	last_cmd = "ml";
	scanhex((void *)&mdest);
	if (termch != '\n') {
		termch = 0;
		scanhex((void *)&mend);
		if (termch != '\n') {
			termch = 0;
			scanhex((void *)&mval);
			mask = ~0;
			if (termch != '\n') termch = 0;
			scanhex((void *)&mask);
		}
	}
	n = 0;
	for (a = mdest; a < mend; a += 4) {
		if (mread(a, val, 4) == 4
			&& ((GETWORD(val) ^ mval) & mask) == 0) {
			printf("%.16x:  %.16x\n", a, GETWORD(val));
			if (++n >= 10)
				break;
		}
	}
}

static unsigned long mskip = 0x1000;
static unsigned long mlim = 0xffffffff;

2382
static void
L
Linus Torvalds 已提交
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
memzcan(void)
{
	unsigned char v;
	unsigned a;
	int ok, ook;

	scanhex(&mdest);
	if (termch != '\n') termch = 0;
	scanhex(&mskip);
	if (termch != '\n') termch = 0;
	scanhex(&mlim);
	ook = 0;
	for (a = mdest; a < mlim; a += mskip) {
		ok = mread(a, &v, 1);
		if (ok && !ook) {
			printf("%.8x .. ", a);
		} else if (!ok && ook)
			printf("%.8x\n", a - mskip);
		ook = ok;
		if (a + mskip < a)
			break;
	}
	if (ook)
		printf("%.8x\n", a - mskip);
}

2409
static void proccall(void)
P
Paul Mackerras 已提交
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437
{
	unsigned long args[8];
	unsigned long ret;
	int i;
	typedef unsigned long (*callfunc_t)(unsigned long, unsigned long,
			unsigned long, unsigned long, unsigned long,
			unsigned long, unsigned long, unsigned long);
	callfunc_t func;

	if (!scanhex(&adrs))
		return;
	if (termch != '\n')
		termch = 0;
	for (i = 0; i < 8; ++i)
		args[i] = 0;
	for (i = 0; i < 8; ++i) {
		if (!scanhex(&args[i]) || termch == '\n')
			break;
		termch = 0;
	}
	func = (callfunc_t) adrs;
	ret = 0;
	if (setjmp(bus_error_jmp) == 0) {
		catch_memory_errors = 1;
		sync();
		ret = func(args[0], args[1], args[2], args[3],
			   args[4], args[5], args[6], args[7]);
		sync();
2438
		printf("return value is 0x%lx\n", ret);
P
Paul Mackerras 已提交
2439 2440 2441 2442 2443 2444
	} else {
		printf("*** %x exception occurred\n", fault_except);
	}
	catch_memory_errors = 0;
}

L
Linus Torvalds 已提交
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
/* Input scanning routines */
int
skipbl(void)
{
	int c;

	if( termch != 0 ){
		c = termch;
		termch = 0;
	} else
		c = inchar();
	while( c == ' ' || c == '\t' )
		c = inchar();
	return c;
}

#define N_PTREGS	44
static char *regnames[N_PTREGS] = {
	"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
	"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
	"r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
	"r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
P
Paul Mackerras 已提交
2467 2468 2469 2470 2471 2472
	"pc", "msr", "or3", "ctr", "lr", "xer", "ccr",
#ifdef CONFIG_PPC64
	"softe",
#else
	"mq",
#endif
L
Linus Torvalds 已提交
2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
	"trap", "dar", "dsisr", "res"
};

int
scanhex(unsigned long *vp)
{
	int c, d;
	unsigned long v;

	c = skipbl();
	if (c == '%') {
		/* parse register name */
		char regname[8];
		int i;

		for (i = 0; i < sizeof(regname) - 1; ++i) {
			c = inchar();
			if (!isalnum(c)) {
				termch = c;
				break;
			}
			regname[i] = c;
		}
		regname[i] = 0;
		for (i = 0; i < N_PTREGS; ++i) {
			if (strcmp(regnames[i], regname) == 0) {
				if (xmon_regs == NULL) {
					printf("regs not available\n");
					return 0;
				}
				*vp = ((unsigned long *)xmon_regs)[i];
				return 1;
			}
		}
		printf("invalid register name '%%%s'\n", regname);
		return 0;
	}

	/* skip leading "0x" if any */

	if (c == '0') {
		c = inchar();
		if (c == 'x') {
			c = inchar();
		} else {
			d = hexdigit(c);
			if (d == EOF) {
				termch = c;
				*vp = 0;
				return 1;
			}
		}
	} else if (c == '$') {
		int i;
		for (i=0; i<63; i++) {
			c = inchar();
			if (isspace(c)) {
				termch = c;
				break;
			}
			tmpstr[i] = c;
		}
		tmpstr[i++] = 0;
2536 2537 2538 2539 2540 2541 2542 2543
		*vp = 0;
		if (setjmp(bus_error_jmp) == 0) {
			catch_memory_errors = 1;
			sync();
			*vp = kallsyms_lookup_name(tmpstr);
			sync();
		}
		catch_memory_errors = 0;
L
Linus Torvalds 已提交
2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
		if (!(*vp)) {
			printf("unknown symbol '%s'\n", tmpstr);
			return 0;
		}
		return 1;
	}

	d = hexdigit(c);
	if (d == EOF) {
		termch = c;
		return 0;
	}
	v = 0;
	do {
		v = (v << 4) + d;
		c = inchar();
		d = hexdigit(c);
	} while (d != EOF);
	termch = c;
	*vp = v;
	return 1;
}

2567
static void
L
Linus Torvalds 已提交
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
scannl(void)
{
	int c;

	c = termch;
	termch = 0;
	while( c != '\n' )
		c = inchar();
}

2578
static int hexdigit(int c)
L
Linus Torvalds 已提交
2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608
{
	if( '0' <= c && c <= '9' )
		return c - '0';
	if( 'A' <= c && c <= 'F' )
		return c - ('A' - 10);
	if( 'a' <= c && c <= 'f' )
		return c - ('a' - 10);
	return EOF;
}

void
getstring(char *s, int size)
{
	int c;

	c = skipbl();
	do {
		if( size > 1 ){
			*s++ = c;
			--size;
		}
		c = inchar();
	} while( c != ' ' && c != '\t' && c != '\n' );
	termch = c;
	*s = 0;
}

static char line[256];
static char *lineptr;

2609
static void
L
Linus Torvalds 已提交
2610 2611 2612 2613 2614
flush_input(void)
{
	lineptr = NULL;
}

2615
static int
L
Linus Torvalds 已提交
2616 2617 2618
inchar(void)
{
	if (lineptr == NULL || *lineptr == 0) {
2619
		if (xmon_gets(line, sizeof(line)) == NULL) {
L
Linus Torvalds 已提交
2620 2621 2622 2623 2624 2625 2626 2627
			lineptr = NULL;
			return EOF;
		}
		lineptr = line;
	}
	return *lineptr++;
}

2628
static void
L
Linus Torvalds 已提交
2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
take_input(char *str)
{
	lineptr = str;
}


static void
symbol_lookup(void)
{
	int type = inchar();
	unsigned long addr;
	static char tmp[64];

	switch (type) {
	case 'a':
		if (scanhex(&addr))
			xmon_print_symbol(addr, ": ", "\n");
		termch = 0;
		break;
	case 's':
		getstring(tmp, 64);
		if (setjmp(bus_error_jmp) == 0) {
			catch_memory_errors = 1;
			sync();
			addr = kallsyms_lookup_name(tmp);
			if (addr)
				printf("%s: %lx\n", tmp, addr);
			else
				printf("Symbol '%s' not found.\n", tmp);
			sync();
		}
		catch_memory_errors = 0;
		termch = 0;
		break;
	}
}


/* Print an address in numeric and symbolic form (if possible) */
static void xmon_print_symbol(unsigned long address, const char *mid,
			      const char *after)
{
	char *modname;
	const char *name = NULL;
	unsigned long offset, size;

P
Paul Mackerras 已提交
2675
	printf(REG, address);
L
Linus Torvalds 已提交
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695
	if (setjmp(bus_error_jmp) == 0) {
		catch_memory_errors = 1;
		sync();
		name = kallsyms_lookup(address, &size, &offset, &modname,
				       tmpstr);
		sync();
		/* wait a little while to see if we get a machine check */
		__delay(200);
	}

	catch_memory_errors = 0;

	if (name) {
		printf("%s%s+%#lx/%#lx", mid, name, offset, size);
		if (modname)
			printf(" [%s]", modname);
	}
	printf("%s", after);
}

2696
#ifdef CONFIG_PPC_BOOK3S_64
L
Linus Torvalds 已提交
2697 2698 2699
static void dump_slb(void)
{
	int i;
W
will schmidt 已提交
2700 2701
	unsigned long esid,vsid,valid;
	unsigned long llp;
L
Linus Torvalds 已提交
2702

2703
	printf("SLB contents of cpu 0x%x\n", smp_processor_id());
L
Linus Torvalds 已提交
2704

2705
	for (i = 0; i < mmu_slb_size; i++) {
W
will schmidt 已提交
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726
		asm volatile("slbmfee  %0,%1" : "=r" (esid) : "r" (i));
		asm volatile("slbmfev  %0,%1" : "=r" (vsid) : "r" (i));
		valid = (esid & SLB_ESID_V);
		if (valid | esid | vsid) {
			printf("%02d %016lx %016lx", i, esid, vsid);
			if (valid) {
				llp = vsid & SLB_VSID_LLP;
				if (vsid & SLB_VSID_B_1T) {
					printf("  1T  ESID=%9lx  VSID=%13lx LLP:%3lx \n",
						GET_ESID_1T(esid),
						(vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T,
						llp);
				} else {
					printf(" 256M ESID=%9lx  VSID=%13lx LLP:%3lx \n",
						GET_ESID(esid),
						(vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT,
						llp);
				}
			} else
				printf("\n");
		}
L
Linus Torvalds 已提交
2727 2728 2729 2730 2731 2732
	}
}

static void dump_stab(void)
{
	int i;
2733
	unsigned long *tmp = (unsigned long *)local_paca->stab_addr;
L
Linus Torvalds 已提交
2734

2735
	printf("Segment table contents of cpu 0x%x\n", smp_processor_id());
L
Linus Torvalds 已提交
2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749

	for (i = 0; i < PAGE_SIZE/16; i++) {
		unsigned long a, b;

		a = *tmp++;
		b = *tmp++;

		if (a || b) {
			printf("%03d %016lx ", i, a);
			printf("%016lx\n", b);
		}
	}
}

P
Paul Mackerras 已提交
2750 2751
void dump_segments(void)
{
2752
	if (mmu_has_feature(MMU_FTR_SLB))
P
Paul Mackerras 已提交
2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
		dump_slb();
	else
		dump_stab();
}
#endif

#ifdef CONFIG_PPC_STD_MMU_32
void dump_segments(void)
{
	int i;

	printf("sr0-15 =");
	for (i = 0; i < 16; ++i)
		printf(" %x", mfsrin(i));
	printf("\n");
}
#endif

2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
#ifdef CONFIG_44x
static void dump_tlb_44x(void)
{
	int i;

	for (i = 0; i < PPC44x_TLB_SIZE; i++) {
		unsigned long w0,w1,w2;
		asm volatile("tlbre  %0,%1,0" : "=r" (w0) : "r" (i));
		asm volatile("tlbre  %0,%1,1" : "=r" (w1) : "r" (i));
		asm volatile("tlbre  %0,%1,2" : "=r" (w2) : "r" (i));
		printf("[%02x] %08x %08x %08x ", i, w0, w1, w2);
		if (w0 & PPC44x_TLB_VALID) {
			printf("V %08x -> %01x%08x %c%c%c%c%c",
			       w0 & PPC44x_TLB_EPN_MASK,
			       w1 & PPC44x_TLB_ERPN_MASK,
			       w1 & PPC44x_TLB_RPN_MASK,
			       (w2 & PPC44x_TLB_W) ? 'W' : 'w',
			       (w2 & PPC44x_TLB_I) ? 'I' : 'i',
			       (w2 & PPC44x_TLB_M) ? 'M' : 'm',
			       (w2 & PPC44x_TLB_G) ? 'G' : 'g',
			       (w2 & PPC44x_TLB_E) ? 'E' : 'e');
		}
		printf("\n");
	}
}
#endif /* CONFIG_44x */
2797

2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941
#ifdef CONFIG_PPC_BOOK3E
static void dump_tlb_book3e(void)
{
	u32 mmucfg, pidmask, lpidmask;
	u64 ramask;
	int i, tlb, ntlbs, pidsz, lpidsz, rasz, lrat = 0;
	int mmu_version;
	static const char *pgsz_names[] = {
		"  1K",
		"  2K",
		"  4K",
		"  8K",
		" 16K",
		" 32K",
		" 64K",
		"128K",
		"256K",
		"512K",
		"  1M",
		"  2M",
		"  4M",
		"  8M",
		" 16M",
		" 32M",
		" 64M",
		"128M",
		"256M",
		"512M",
		"  1G",
		"  2G",
		"  4G",
		"  8G",
		" 16G",
		" 32G",
		" 64G",
		"128G",
		"256G",
		"512G",
		"  1T",
		"  2T",
	};

	/* Gather some infos about the MMU */
	mmucfg = mfspr(SPRN_MMUCFG);
	mmu_version = (mmucfg & 3) + 1;
	ntlbs = ((mmucfg >> 2) & 3) + 1;
	pidsz = ((mmucfg >> 6) & 0x1f) + 1;
	lpidsz = (mmucfg >> 24) & 0xf;
	rasz = (mmucfg >> 16) & 0x7f;
	if ((mmu_version > 1) && (mmucfg & 0x10000))
		lrat = 1;
	printf("Book3E MMU MAV=%d.0,%d TLBs,%d-bit PID,%d-bit LPID,%d-bit RA\n",
	       mmu_version, ntlbs, pidsz, lpidsz, rasz);
	pidmask = (1ul << pidsz) - 1;
	lpidmask = (1ul << lpidsz) - 1;
	ramask = (1ull << rasz) - 1;

	for (tlb = 0; tlb < ntlbs; tlb++) {
		u32 tlbcfg;
		int nent, assoc, new_cc = 1;
		printf("TLB %d:\n------\n", tlb);
		switch(tlb) {
		case 0:
			tlbcfg = mfspr(SPRN_TLB0CFG);
			break;
		case 1:
			tlbcfg = mfspr(SPRN_TLB1CFG);
			break;
		case 2:
			tlbcfg = mfspr(SPRN_TLB2CFG);
			break;
		case 3:
			tlbcfg = mfspr(SPRN_TLB3CFG);
			break;
		default:
			printf("Unsupported TLB number !\n");
			continue;
		}
		nent = tlbcfg & 0xfff;
		assoc = (tlbcfg >> 24) & 0xff;
		for (i = 0; i < nent; i++) {
			u32 mas0 = MAS0_TLBSEL(tlb);
			u32 mas1 = MAS1_TSIZE(BOOK3E_PAGESZ_4K);
			u64 mas2 = 0;
			u64 mas7_mas3;
			int esel = i, cc = i;

			if (assoc != 0) {
				cc = i / assoc;
				esel = i % assoc;
				mas2 = cc * 0x1000;
			}

			mas0 |= MAS0_ESEL(esel);
			mtspr(SPRN_MAS0, mas0);
			mtspr(SPRN_MAS1, mas1);
			mtspr(SPRN_MAS2, mas2);
			asm volatile("tlbre  0,0,0" : : : "memory");
			mas1 = mfspr(SPRN_MAS1);
			mas2 = mfspr(SPRN_MAS2);
			mas7_mas3 = mfspr(SPRN_MAS7_MAS3);
			if (assoc && (i % assoc) == 0)
				new_cc = 1;
			if (!(mas1 & MAS1_VALID))
				continue;
			if (assoc == 0)
				printf("%04x- ", i);
			else if (new_cc)
				printf("%04x-%c", cc, 'A' + esel);
			else
				printf("    |%c", 'A' + esel);
			new_cc = 0;
			printf(" %016llx %04x %s %c%c AS%c",
			       mas2 & ~0x3ffull,
			       (mas1 >> 16) & 0x3fff,
			       pgsz_names[(mas1 >> 7) & 0x1f],
			       mas1 & MAS1_IND ? 'I' : ' ',
			       mas1 & MAS1_IPROT ? 'P' : ' ',
			       mas1 & MAS1_TS ? '1' : '0');
			printf(" %c%c%c%c%c%c%c",
			       mas2 & MAS2_X0 ? 'a' : ' ',
			       mas2 & MAS2_X1 ? 'v' : ' ',
			       mas2 & MAS2_W  ? 'w' : ' ',
			       mas2 & MAS2_I  ? 'i' : ' ',
			       mas2 & MAS2_M  ? 'm' : ' ',
			       mas2 & MAS2_G  ? 'g' : ' ',
			       mas2 & MAS2_E  ? 'e' : ' ');
			printf(" %016llx", mas7_mas3 & ramask & ~0x7ffull);
			if (mas1 & MAS1_IND)
				printf(" %s\n",
				       pgsz_names[(mas7_mas3 >> 1) & 0x1f]);
			else
				printf(" U%c%c%c S%c%c%c\n",
				       mas7_mas3 & MAS3_UX ? 'x' : ' ',
				       mas7_mas3 & MAS3_UW ? 'w' : ' ',
				       mas7_mas3 & MAS3_UR ? 'r' : ' ',
				       mas7_mas3 & MAS3_SX ? 'x' : ' ',
				       mas7_mas3 & MAS3_SW ? 'w' : ' ',
				       mas7_mas3 & MAS3_SR ? 'r' : ' ');
		}
	}
}
#endif /* CONFIG_PPC_BOOK3E */

2942
static void xmon_init(int enable)
O
Olaf Hering 已提交
2943 2944 2945 2946 2947 2948 2949
{
	if (enable) {
		__debugger = xmon;
		__debugger_ipi = xmon_ipi;
		__debugger_bpt = xmon_bpt;
		__debugger_sstep = xmon_sstep;
		__debugger_iabr_match = xmon_iabr_match;
2950
		__debugger_break_match = xmon_break_match;
O
Olaf Hering 已提交
2951 2952 2953 2954 2955 2956 2957
		__debugger_fault_handler = xmon_fault_handler;
	} else {
		__debugger = NULL;
		__debugger_ipi = NULL;
		__debugger_bpt = NULL;
		__debugger_sstep = NULL;
		__debugger_iabr_match = NULL;
2958
		__debugger_break_match = NULL;
O
Olaf Hering 已提交
2959 2960
		__debugger_fault_handler = NULL;
	}
L
Linus Torvalds 已提交
2961
}
2962 2963

#ifdef CONFIG_MAGIC_SYSRQ
2964
static void sysrq_handle_xmon(int key)
2965 2966 2967
{
	/* ensure xmon is enabled */
	xmon_init(1);
2968
	debugger(get_irq_regs());
2969 2970
}

2971
static struct sysrq_key_op sysrq_xmon_op = {
2972
	.handler =	sysrq_handle_xmon,
2973
	.help_msg =	"xmon(x)",
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
	.action_msg =	"Entering xmon",
};

static int __init setup_xmon_sysrq(void)
{
	register_sysrq_key('x', &sysrq_xmon_op);
	return 0;
}
__initcall(setup_xmon_sysrq);
#endif /* CONFIG_MAGIC_SYSRQ */
2984

2985
static int __initdata xmon_early, xmon_off;
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014

static int __init early_parse_xmon(char *p)
{
	if (!p || strncmp(p, "early", 5) == 0) {
		/* just "xmon" is equivalent to "xmon=early" */
		xmon_init(1);
		xmon_early = 1;
	} else if (strncmp(p, "on", 2) == 0)
		xmon_init(1);
	else if (strncmp(p, "off", 3) == 0)
		xmon_off = 1;
	else if (strncmp(p, "nobt", 4) == 0)
		xmon_no_auto_backtrace = 1;
	else
		return 1;

	return 0;
}
early_param("xmon", early_parse_xmon);

void __init xmon_setup(void)
{
#ifdef CONFIG_XMON_DEFAULT
	if (!xmon_off)
		xmon_init(1);
#endif
	if (xmon_early)
		debugger(NULL);
}
3015

3016
#ifdef CONFIG_SPU_BASE
3017 3018 3019 3020 3021

struct spu_info {
	struct spu *spu;
	u64 saved_mfc_sr1_RW;
	u32 saved_spu_runcntl_RW;
3022
	unsigned long dump_addr;
3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041
	u8 stopped_ok;
};

#define XMON_NUM_SPUS	16	/* Enough for current hardware */

static struct spu_info spu_info[XMON_NUM_SPUS];

void xmon_register_spus(struct list_head *list)
{
	struct spu *spu;

	list_for_each_entry(spu, list, full_list) {
		if (spu->number >= XMON_NUM_SPUS) {
			WARN_ON(1);
			continue;
		}

		spu_info[spu->number].spu = spu;
		spu_info[spu->number].stopped_ok = 0;
3042 3043
		spu_info[spu->number].dump_addr = (unsigned long)
				spu_info[spu->number].spu->local_store;
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075
	}
}

static void stop_spus(void)
{
	struct spu *spu;
	int i;
	u64 tmp;

	for (i = 0; i < XMON_NUM_SPUS; i++) {
		if (!spu_info[i].spu)
			continue;

		if (setjmp(bus_error_jmp) == 0) {
			catch_memory_errors = 1;
			sync();

			spu = spu_info[i].spu;

			spu_info[i].saved_spu_runcntl_RW =
				in_be32(&spu->problem->spu_runcntl_RW);

			tmp = spu_mfc_sr1_get(spu);
			spu_info[i].saved_mfc_sr1_RW = tmp;

			tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
			spu_mfc_sr1_set(spu, tmp);

			sync();
			__delay(200);

			spu_info[i].stopped_ok = 1;
3076 3077 3078 3079

			printf("Stopped spu %.2d (was %s)\n", i,
					spu_info[i].saved_spu_runcntl_RW ?
					"running" : "stopped");
3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
		} else {
			catch_memory_errors = 0;
			printf("*** Error stopping spu %.2d\n", i);
		}
		catch_memory_errors = 0;
	}
}

static void restart_spus(void)
{
	struct spu *spu;
	int i;

	for (i = 0; i < XMON_NUM_SPUS; i++) {
		if (!spu_info[i].spu)
			continue;

		if (!spu_info[i].stopped_ok) {
			printf("*** Error, spu %d was not successfully stopped"
					", not restarting\n", i);
			continue;
		}

		if (setjmp(bus_error_jmp) == 0) {
			catch_memory_errors = 1;
			sync();

			spu = spu_info[i].spu;
			spu_mfc_sr1_set(spu, spu_info[i].saved_mfc_sr1_RW);
			out_be32(&spu->problem->spu_runcntl_RW,
					spu_info[i].saved_spu_runcntl_RW);

			sync();
			__delay(200);

			printf("Restarted spu %.2d\n", i);
		} else {
			catch_memory_errors = 0;
			printf("*** Error restarting spu %.2d\n", i);
		}
		catch_memory_errors = 0;
	}
}

3124
#define DUMP_WIDTH	23
3125
#define DUMP_VALUE(format, field, value)				\
3126 3127 3128 3129 3130
do {									\
	if (setjmp(bus_error_jmp) == 0) {				\
		catch_memory_errors = 1;				\
		sync();							\
		printf("  %-*s = "format"\n", DUMP_WIDTH,		\
3131
				#field, value);				\
3132 3133 3134 3135 3136 3137 3138 3139 3140 3141
		sync();							\
		__delay(200);						\
	} else {							\
		catch_memory_errors = 0;				\
		printf("  %-*s = *** Error reading field.\n",		\
					DUMP_WIDTH, #field);		\
	}								\
	catch_memory_errors = 0;					\
} while (0)

3142 3143 3144
#define DUMP_FIELD(obj, format, field)	\
	DUMP_VALUE(format, field, obj->field)

3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156
static void dump_spu_fields(struct spu *spu)
{
	printf("Dumping spu fields at address %p:\n", spu);

	DUMP_FIELD(spu, "0x%x", number);
	DUMP_FIELD(spu, "%s", name);
	DUMP_FIELD(spu, "0x%lx", local_store_phys);
	DUMP_FIELD(spu, "0x%p", local_store);
	DUMP_FIELD(spu, "0x%lx", ls_size);
	DUMP_FIELD(spu, "0x%x", node);
	DUMP_FIELD(spu, "0x%lx", flags);
	DUMP_FIELD(spu, "%d", class_0_pending);
3157 3158 3159
	DUMP_FIELD(spu, "0x%lx", class_0_dar);
	DUMP_FIELD(spu, "0x%lx", class_1_dar);
	DUMP_FIELD(spu, "0x%lx", class_1_dsisr);
3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170
	DUMP_FIELD(spu, "0x%lx", irqs[0]);
	DUMP_FIELD(spu, "0x%lx", irqs[1]);
	DUMP_FIELD(spu, "0x%lx", irqs[2]);
	DUMP_FIELD(spu, "0x%x", slb_replace);
	DUMP_FIELD(spu, "%d", pid);
	DUMP_FIELD(spu, "0x%p", mm);
	DUMP_FIELD(spu, "0x%p", ctx);
	DUMP_FIELD(spu, "0x%p", rq);
	DUMP_FIELD(spu, "0x%p", timestamp);
	DUMP_FIELD(spu, "0x%lx", problem_phys);
	DUMP_FIELD(spu, "0x%p", problem);
3171 3172 3173 3174 3175 3176
	DUMP_VALUE("0x%x", problem->spu_runcntl_RW,
			in_be32(&spu->problem->spu_runcntl_RW));
	DUMP_VALUE("0x%x", problem->spu_status_R,
			in_be32(&spu->problem->spu_status_R));
	DUMP_VALUE("0x%x", problem->spu_npc_RW,
			in_be32(&spu->problem->spu_npc_RW));
3177
	DUMP_FIELD(spu, "0x%p", priv2);
3178
	DUMP_FIELD(spu, "0x%p", pdata);
3179 3180
}

3181 3182 3183 3184 3185 3186 3187
int
spu_inst_dump(unsigned long adr, long count, int praddr)
{
	return generic_inst_dump(adr, count, praddr, print_insn_spu);
}

static void dump_spu_ls(unsigned long num, int subcmd)
3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213
{
	unsigned long offset, addr, ls_addr;

	if (setjmp(bus_error_jmp) == 0) {
		catch_memory_errors = 1;
		sync();
		ls_addr = (unsigned long)spu_info[num].spu->local_store;
		sync();
		__delay(200);
	} else {
		catch_memory_errors = 0;
		printf("*** Error: accessing spu info for spu %d\n", num);
		return;
	}
	catch_memory_errors = 0;

	if (scanhex(&offset))
		addr = ls_addr + offset;
	else
		addr = spu_info[num].dump_addr;

	if (addr >= ls_addr + LS_SIZE) {
		printf("*** Error: address outside of local store\n");
		return;
	}

3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224
	switch (subcmd) {
	case 'i':
		addr += spu_inst_dump(addr, 16, 1);
		last_cmd = "sdi\n";
		break;
	default:
		prdump(addr, 64);
		addr += 64;
		last_cmd = "sd\n";
		break;
	}
3225 3226 3227 3228

	spu_info[num].dump_addr = addr;
}

3229 3230
static int do_spu_cmd(void)
{
3231
	static unsigned long num = 0;
3232
	int cmd, subcmd = 0;
3233 3234 3235 3236 3237 3238 3239 3240 3241

	cmd = inchar();
	switch (cmd) {
	case 's':
		stop_spus();
		break;
	case 'r':
		restart_spus();
		break;
3242
	case 'd':
3243 3244 3245 3246
		subcmd = inchar();
		if (isxdigit(subcmd) || subcmd == '\n')
			termch = subcmd;
	case 'f':
3247 3248
		scanhex(&num);
		if (num >= XMON_NUM_SPUS || !spu_info[num].spu) {
3249
			printf("*** Error: invalid spu number\n");
3250 3251 3252 3253 3254 3255 3256 3257
			return 0;
		}

		switch (cmd) {
		case 'f':
			dump_spu_fields(spu_info[num].spu);
			break;
		default:
3258
			dump_spu_ls(num, subcmd);
3259 3260 3261
			break;
		}

3262
		break;
3263 3264 3265 3266 3267 3268
	default:
		return -1;
	}

	return 0;
}
3269
#else /* ! CONFIG_SPU_BASE */
3270 3271 3272 3273 3274
static int do_spu_cmd(void)
{
	return -1;
}
#endif