xmon.c 90.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4
/*
 * Routines providing a simple monitor for use on the PowerMac.
 *
5
 * Copyright (C) 1996-2005 Paul Mackerras.
6 7
 * Copyright (C) 2001 PPC64 Team, IBM Corp
 * Copyrignt (C) 2006 Michael Ellerman, IBM Corp
L
Linus Torvalds 已提交
8
 */
9 10

#include <linux/kernel.h>
L
Linus Torvalds 已提交
11
#include <linux/errno.h>
12
#include <linux/sched/signal.h>
L
Linus Torvalds 已提交
13 14 15 16 17
#include <linux/smp.h>
#include <linux/mm.h>
#include <linux/reboot.h>
#include <linux/delay.h>
#include <linux/kallsyms.h>
18
#include <linux/kmsg_dump.h>
L
Linus Torvalds 已提交
19
#include <linux/cpumask.h>
20
#include <linux/export.h>
21
#include <linux/sysrq.h>
A
Andrew Morton 已提交
22
#include <linux/interrupt.h>
23
#include <linux/irq.h>
24
#include <linux/bug.h>
25
#include <linux/nmi.h>
26
#include <linux/ctype.h>
27
#include <linux/highmem.h>
28
#include <linux/security.h>
L
Linus Torvalds 已提交
29

30
#include <asm/debugfs.h>
L
Linus Torvalds 已提交
31
#include <asm/ptrace.h>
32
#include <asm/smp.h>
L
Linus Torvalds 已提交
33 34 35
#include <asm/string.h>
#include <asm/prom.h>
#include <asm/machdep.h>
P
Paul Mackerras 已提交
36
#include <asm/xmon.h>
L
Linus Torvalds 已提交
37 38 39
#include <asm/processor.h>
#include <asm/mmu.h>
#include <asm/mmu_context.h>
40
#include <asm/plpar_wrappers.h>
L
Linus Torvalds 已提交
41 42 43
#include <asm/cputable.h>
#include <asm/rtas.h>
#include <asm/sstep.h>
44
#include <asm/irq_regs.h>
45 46
#include <asm/spu.h>
#include <asm/spu_priv1.h>
47
#include <asm/setjmp.h>
48
#include <asm/reg.h>
49
#include <asm/debug.h>
50
#include <asm/hw_breakpoint.h>
51
#include <asm/xive.h>
52 53
#include <asm/opal.h>
#include <asm/firmware.h>
54
#include <asm/code-patching.h>
55
#include <asm/sections.h>
56
#include <asm/inst.h>
57

P
Paul Mackerras 已提交
58
#ifdef CONFIG_PPC64
L
Linus Torvalds 已提交
59
#include <asm/hvcall.h>
P
Paul Mackerras 已提交
60 61
#include <asm/paca.h>
#endif
L
Linus Torvalds 已提交
62 63

#include "nonstdio.h"
64
#include "dis-asm.h"
65
#include "xmon_bpts.h"
L
Linus Torvalds 已提交
66 67

#ifdef CONFIG_SMP
68
static cpumask_t cpus_in_xmon = CPU_MASK_NONE;
L
Linus Torvalds 已提交
69 70 71
static unsigned long xmon_taken = 1;
static int xmon_owner;
static int xmon_gate;
72 73
#else
#define xmon_owner 0
L
Linus Torvalds 已提交
74 75
#endif /* CONFIG_SMP */

76 77 78
#ifdef CONFIG_PPC_PSERIES
static int set_indicator_token = RTAS_UNKNOWN_SERVICE;
#endif
79
static unsigned long in_xmon __read_mostly = 0;
80
static int xmon_on = IS_ENABLED(CONFIG_XMON_DEFAULT);
81
static bool xmon_is_ro = IS_ENABLED(CONFIG_XMON_DEFAULT_RO_MODE);
L
Linus Torvalds 已提交
82 83 84

static unsigned long adrs;
static int size = 1;
85
#define MAX_DUMP (64 * 1024)
L
Linus Torvalds 已提交
86
static unsigned long ndump = 64;
87
#define MAX_IDUMP (MAX_DUMP >> 2)
L
Linus Torvalds 已提交
88 89 90 91
static unsigned long nidump = 16;
static unsigned long ncsum = 4096;
static int termch;
static char tmpstr[128];
92
static int tracing_enabled;
L
Linus Torvalds 已提交
93 94 95

static long bus_error_jmp[JMP_BUF_LEN];
static int catch_memory_errors;
96
static int catch_spr_faults;
L
Linus Torvalds 已提交
97 98 99 100 101
static long *xmon_fault_jmp[NR_CPUS];

/* Breakpoint stuff */
struct bpt {
	unsigned long	address;
102
	struct ppc_inst	*instr;
L
Linus Torvalds 已提交
103 104 105 106 107 108
	atomic_t	ref_count;
	int		enabled;
	unsigned long	pad;
};

/* Bits in bpt.enabled */
109 110 111
#define BP_CIABR	1
#define BP_TRAP		2
#define BP_DABR		4
L
Linus Torvalds 已提交
112 113

static struct bpt bpts[NBPTS];
114
static struct bpt dabr[HBP_NUM_MAX];
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123
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);
124
static int mread_instr(unsigned long, struct ppc_inst *);
L
Linus Torvalds 已提交
125 126 127 128 129
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);
130
static void show_pte(unsigned long);
L
Linus Torvalds 已提交
131 132
static void prdump(unsigned long, long);
static int ppc_inst_dump(unsigned long, long, int);
133
static void dump_log_buf(void);
134 135 136 137 138 139 140 141 142 143

#ifdef CONFIG_PPC_POWERNV
static void dump_opal_msglog(void);
#else
static inline void dump_opal_msglog(void)
{
	printf("Machine is not running OPAL firmware.\n");
}
#endif

L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
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 *);
159
static int  read_spr(int, unsigned long *);
L
Linus Torvalds 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173
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 已提交
174
static void proccall(void);
175
static void show_tasks(void);
L
Linus Torvalds 已提交
176 177
void dump_segments(void);
static void symbol_lookup(void);
178 179
static void xmon_show_stack(unsigned long sp, unsigned long lr,
			    unsigned long pc);
L
Linus Torvalds 已提交
180 181 182 183
static void xmon_print_symbol(unsigned long address, const char *mid,
			      const char *after);
static const char *getvecname(unsigned long vec);

184 185
static int do_spu_cmd(void);

186 187 188
#ifdef CONFIG_44x
static void dump_tlb_44x(void);
#endif
189 190 191
#ifdef CONFIG_PPC_BOOK3E
static void dump_tlb_book3e(void);
#endif
192

193 194
static void clear_all_bpt(void);

P
Paul Mackerras 已提交
195 196 197 198 199
#ifdef CONFIG_PPC64
#define REG		"%.16lx"
#else
#define REG		"%.8lx"
#endif
L
Linus Torvalds 已提交
200

201 202 203
#ifdef __LITTLE_ENDIAN__
#define GETWORD(v)	(((v)[3] << 24) + ((v)[2] << 16) + ((v)[1] << 8) + (v)[0])
#else
L
Linus Torvalds 已提交
204
#define GETWORD(v)	(((v)[0] << 24) + ((v)[1] << 16) + ((v)[2] << 8) + (v)[3])
205
#endif
L
Linus Torvalds 已提交
206

207 208
static const char *xmon_ro_msg = "Operation disabled: xmon in read-only mode\n";

L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222
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\
223 224 225 226
  d1	dump 1 byte values\n\
  d2	dump 2 byte values\n\
  d4	dump 4 byte values\n\
  d8	dump 8 byte values\n\
L
Linus Torvalds 已提交
227 228 229
  di	dump instructions\n\
  df	dump float values\n\
  dd	dump double values\n\
230
  dl    dump the kernel log buffer\n"
231 232 233 234
#ifdef CONFIG_PPC_POWERNV
  "\
  do    dump the OPAL message log\n"
#endif
235 236 237 238 239 240
#ifdef CONFIG_PPC64
  "\
  dp[#]	dump paca for current cpu, or cpu #\n\
  dpa	dump paca for all possible cpus\n"
#endif
  "\
241
  dr	dump stream of raw bytes\n\
242
  dv	dump virtual address translation \n\
243
  dt	dump the tracing buffers (uses printk)\n\
244
  dtc	dump the tracing buffers for current CPU (uses printk)\n\
245 246 247 248 249 250 251
"
#ifdef CONFIG_PPC_POWERNV
"  dx#   dump xive on CPU #\n\
  dxi#  dump xive irq state #\n\
  dxa   dump xive on all CPUs\n"
#endif
"  e	print exception information\n\
L
Linus Torvalds 已提交
252 253 254
  f	flush cache\n\
  la	lookup symbol+offset of specified address\n\
  ls	lookup address of specified symbol\n\
255
  lp s [#]	lookup address of percpu symbol s for current cpu, or cpu #\n\
L
Linus Torvalds 已提交
256 257 258 259 260 261 262
  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 已提交
263
  p 	call a procedure\n\
264
  P 	list processes/tasks\n\
L
Linus Torvalds 已提交
265
  r	print registers\n\
266
  s	single step\n"
267
#ifdef CONFIG_SPU_BASE
268
"  ss	stop execution on all spus\n\
269
  sr	restore execution on stopped spus\n\
270
  sf  #	dump spu fields for spu # (in hex)\n\
271
  sd  #	dump spu local store for spu # (in hex)\n\
272
  sdi #	disassemble spu local store for spu # (in hex)\n"
273 274
#endif
"  S	print special registers\n\
275 276 277
  Sa    print all SPRs\n\
  Sr #	read SPR #\n\
  Sw #v write v to SPR #\n\
L
Linus Torvalds 已提交
278 279
  t	print backtrace\n\
  x	exit monitor and recover\n\
280
  X	exit monitor and don't recover\n"
281
#if defined(CONFIG_PPC64) && !defined(CONFIG_PPC_BOOK3E)
P
Paul Mackerras 已提交
282
"  u	dump segment table or SLB\n"
283
#elif defined(CONFIG_PPC_BOOK3S_32)
P
Paul Mackerras 已提交
284
"  u	dump segment registers\n"
285
#elif defined(CONFIG_44x) || defined(CONFIG_PPC_BOOK3E)
286 287
"  u	dump TLB\n"
#endif
288
"  U	show uptime information\n"
P
Paul Mackerras 已提交
289
"  ?	help\n"
290
"  # n	limit output to n lines per page (for dp, dpa, dl)\n"
291 292
"  zr	reboot\n"
"  zh	halt\n"
L
Linus Torvalds 已提交
293 294
;

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
#ifdef CONFIG_SECURITY
static bool xmon_is_locked_down(void)
{
	static bool lockdown;

	if (!lockdown) {
		lockdown = !!security_locked_down(LOCKDOWN_XMON_RW);
		if (lockdown) {
			printf("xmon: Disabled due to kernel lockdown\n");
			xmon_is_ro = true;
		}
	}

	if (!xmon_is_ro) {
		xmon_is_ro = !!security_locked_down(LOCKDOWN_XMON_WR);
		if (xmon_is_ro)
			printf("xmon: Read-only due to kernel lockdown\n");
	}

	return lockdown;
}
#else /* CONFIG_SECURITY */
static inline bool xmon_is_locked_down(void)
{
	return false;
}
#endif

L
Linus Torvalds 已提交
323 324
static struct pt_regs *xmon_regs;

P
Paul Mackerras 已提交
325
static inline void sync(void)
L
Linus Torvalds 已提交
326 327 328 329
{
	asm volatile("sync; isync");
}

P
Paul Mackerras 已提交
330 331 332 333 334 335 336 337 338
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 已提交
339

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
/**
 * write_ciabr() - write the CIABR SPR
 * @ciabr:	The value to write.
 *
 * This function writes a value to the CIARB register either directly
 * through mtspr instruction if the kernel is in HV privilege mode or
 * call a hypervisor function to achieve the same in case the kernel
 * is in supervisor privilege mode.
 */
static void write_ciabr(unsigned long ciabr)
{
	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
		return;

	if (cpu_has_feature(CPU_FTR_HVMODE)) {
		mtspr(SPRN_CIABR, ciabr);
		return;
	}
358
	plpar_set_ciabr(ciabr);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
}

/**
 * set_ciabr() - set the CIABR
 * @addr:	The value to set.
 *
 * This function sets the correct privilege value into the the HW
 * breakpoint address before writing it up in the CIABR register.
 */
static void set_ciabr(unsigned long addr)
{
	addr &= ~CIABR_PRIV;

	if (cpu_has_feature(CPU_FTR_HVMODE))
		addr |= CIABR_PRIV_HYPER;
	else
		addr |= CIABR_PRIV_SUPER;
	write_ciabr(addr);
}

L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
/*
 * 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.
	 */
399
	if (set_indicator_token == RTAS_UNKNOWN_SERVICE)
L
Linus Torvalds 已提交
400
		return;
401

402 403
	rtas_call_unlocked(&args, set_indicator_token, 3, 1, NULL,
			   SURVEILLANCE_TOKEN, 0, 0);
404

L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418
#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;
419

L
Linus Torvalds 已提交
420
	for (;;) {
421 422 423 424
		last_speaker = cmpxchg(&xmon_speaker, 0, me);
		if (last_speaker == 0)
			return;

425 426 427 428 429
		/*
		 * Wait a full second for the lock, we might be on a slow
		 * console, but check every 100us.
		 */
		timeout = 10000;
L
Linus Torvalds 已提交
430
		while (xmon_speaker == last_speaker) {
431 432
			if (--timeout > 0) {
				udelay(100);
L
Linus Torvalds 已提交
433
				continue;
434 435
			}

L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448
			/* hostile takeover */
			prev = cmpxchg(&xmon_speaker, last_speaker, me);
			if (prev == last_speaker)
				return;
			break;
		}
	}
}

static void release_output_lock(void)
{
	xmon_speaker = 0;
}
449 450 451

int cpus_are_in_xmon(void)
{
452
	return !cpumask_empty(&cpus_in_xmon);
453
}
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

static bool wait_for_other_cpus(int ncpus)
{
	unsigned long timeout;

	/* We wait for 2s, which is a metric "little while" */
	for (timeout = 20000; timeout != 0; --timeout) {
		if (cpumask_weight(&cpus_in_xmon) >= ncpus)
			return true;
		udelay(100);
		barrier();
	}

	return false;
}
469 470 471 472
#else /* CONFIG_SMP */
static inline void get_output_lock(void) {}
static inline void release_output_lock(void) {}
#endif
L
Linus Torvalds 已提交
473

474 475
static inline int unrecoverable_excp(struct pt_regs *regs)
{
476
#if defined(CONFIG_4xx) || defined(CONFIG_PPC_BOOK3E)
477
	/* We have no MSR_RI bit on 4xx or Book3e, so we simply return false */
478 479 480 481 482 483
	return 0;
#else
	return ((regs->msr & MSR_RI) == 0);
#endif
}

484 485 486 487 488 489 490
static void xmon_touch_watchdogs(void)
{
	touch_softlockup_watchdog_sync();
	rcu_cpu_stall_reset();
	touch_nmi_watchdog();
}

491
static int xmon_core(struct pt_regs *regs, int fromipi)
L
Linus Torvalds 已提交
492 493 494 495
{
	int cmd = 0;
	struct bpt *bp;
	long recurse_jmp[JMP_BUF_LEN];
496
	bool locked_down;
L
Linus Torvalds 已提交
497
	unsigned long offset;
498
	unsigned long flags;
L
Linus Torvalds 已提交
499 500 501 502 503
#ifdef CONFIG_SMP
	int cpu;
	int secondary;
#endif

504
	local_irq_save(flags);
505
	hard_irq_disable();
L
Linus Torvalds 已提交
506

507 508
	locked_down = xmon_is_locked_down();

509 510 511 512
	if (!fromipi) {
		tracing_enabled = tracing_is_on();
		tracing_off();
	}
513

L
Linus Torvalds 已提交
514 515 516 517 518 519 520 521 522 523
	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();
524
	if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
525 526 527 528 529 530
		/*
		 * We catch SPR read/write faults here because the 0x700, 0xf60
		 * etc. handlers don't call debugger_fault_handler().
		 */
		if (catch_spr_faults)
			longjmp(bus_error_jmp, 1);
L
Linus Torvalds 已提交
531 532 533 534 535
		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)));
536
		release_output_lock();
L
Linus Torvalds 已提交
537 538 539 540 541
		longjmp(xmon_fault_jmp[cpu], 1);
	}

	if (setjmp(recurse_jmp) != 0) {
		if (!in_xmon || !xmon_gate) {
542
			get_output_lock();
L
Linus Torvalds 已提交
543 544
			printf("xmon: WARNING: bad recursive fault "
			       "on cpu 0x%x\n", cpu);
545
			release_output_lock();
L
Linus Torvalds 已提交
546 547 548 549 550 551 552 553 554
			goto waiting;
		}
		secondary = !(xmon_taken && cpu == xmon_owner);
		goto cmdloop;
	}

	xmon_fault_jmp[cpu] = recurse_jmp;

	bp = NULL;
555
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT))
L
Linus Torvalds 已提交
556
		bp = at_breakpoint(regs->nip);
557
	if (bp || unrecoverable_excp(regs))
L
Linus Torvalds 已提交
558 559 560 561
		fromipi = 0;

	if (!fromipi) {
		get_output_lock();
562 563
		if (!locked_down)
			excprint(regs);
L
Linus Torvalds 已提交
564
		if (bp) {
565
			printf("cpu 0x%x stopped at breakpoint 0x%tx (",
L
Linus Torvalds 已提交
566 567 568
			       cpu, BP_NUM(bp));
			xmon_print_symbol(regs->nip, " ", ")\n");
		}
569
		if (unrecoverable_excp(regs))
L
Linus Torvalds 已提交
570 571 572 573 574
			printf("WARNING: exception is not recoverable, "
			       "can't continue\n");
		release_output_lock();
	}

575 576
	cpumask_set_cpu(cpu, &cpus_in_xmon);

L
Linus Torvalds 已提交
577 578
 waiting:
	secondary = 1;
579
	spin_begin();
L
Linus Torvalds 已提交
580 581
	while (secondary && !xmon_gate) {
		if (in_xmon == 0) {
582 583
			if (fromipi) {
				spin_end();
L
Linus Torvalds 已提交
584
				goto leave;
585
			}
L
Linus Torvalds 已提交
586 587
			secondary = test_and_set_bit(0, &in_xmon);
		}
588 589
		spin_cpu_relax();
		touch_nmi_watchdog();
L
Linus Torvalds 已提交
590
	}
591
	spin_end();
L
Linus Torvalds 已提交
592 593 594 595 596 597 598 599 600

	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) {
601 602 603 604 605 606 607 608 609 610 611
			/*
			 * A system reset (trap == 0x100) can be triggered on
			 * all CPUs, so when we come in via 0x100 try waiting
			 * for the other CPUs to come in before we send the
			 * debugger break (IPI). This is similar to
			 * crash_kexec_secondary().
			 */
			if (TRAP(regs) != 0x100 || !wait_for_other_cpus(ncpus))
				smp_send_debugger_break();

			wait_for_other_cpus(ncpus);
L
Linus Torvalds 已提交
612 613 614
		}
		remove_bpts();
		disable_surveillance();
615 616 617 618 619 620 621 622

		if (!locked_down) {
			/* for breakpoint or single step, print curr insn */
			if (bp || TRAP(regs) == 0xd00)
				ppc_inst_dump(regs->nip, 1, 0);
			printf("enter ? for help\n");
		}

L
Linus Torvalds 已提交
623 624 625
		mb();
		xmon_gate = 1;
		barrier();
626
		touch_nmi_watchdog();
L
Linus Torvalds 已提交
627 628 629 630 631
	}

 cmdloop:
	while (in_xmon) {
		if (secondary) {
632
			spin_begin();
L
Linus Torvalds 已提交
633 634 635
			if (cpu == xmon_owner) {
				if (!test_and_set_bit(0, &xmon_taken)) {
					secondary = 0;
636
					spin_end();
L
Linus Torvalds 已提交
637 638 639 640
					continue;
				}
				/* missed it */
				while (cpu == xmon_owner)
641
					spin_cpu_relax();
L
Linus Torvalds 已提交
642
			}
643 644
			spin_cpu_relax();
			touch_nmi_watchdog();
L
Linus Torvalds 已提交
645
		} else {
646 647 648
			if (!locked_down)
				cmd = cmds(regs);
			if (locked_down || cmd != 0) {
L
Linus Torvalds 已提交
649 650 651 652 653 654 655 656 657 658 659 660
				/* exiting xmon */
				insert_bpts();
				xmon_gate = 0;
				wmb();
				in_xmon = 0;
				break;
			}
			/* have switched to some other cpu */
			secondary = 1;
		}
	}
 leave:
661
	cpumask_clear_cpu(cpu, &cpus_in_xmon);
L
Linus Torvalds 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
	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) {
677
			printf("Stopped at breakpoint %tx (", BP_NUM(bp));
L
Linus Torvalds 已提交
678 679
			xmon_print_symbol(regs->nip, " ", ")\n");
		}
680
		if (unrecoverable_excp(regs))
L
Linus Torvalds 已提交
681 682 683 684
			printf("WARNING: exception is not recoverable, "
			       "can't continue\n");
		remove_bpts();
		disable_surveillance();
685 686 687 688 689 690
		if (!locked_down) {
			/* for breakpoint or single step, print current insn */
			if (bp || TRAP(regs) == 0xd00)
				ppc_inst_dump(regs->nip, 1, 0);
			printf("enter ? for help\n");
		}
L
Linus Torvalds 已提交
691 692
	}

693 694
	if (!locked_down)
		cmd = cmds(regs);
L
Linus Torvalds 已提交
695 696 697 698 699

	insert_bpts();
	in_xmon = 0;
#endif

700 701 702 703 704 705 706 707 708
#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
709
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
L
Linus Torvalds 已提交
710 711
		bp = at_breakpoint(regs->nip);
		if (bp != NULL) {
712
			int stepped = emulate_step(regs, ppc_inst_read(bp->instr));
L
Linus Torvalds 已提交
713 714 715 716 717
			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",
718
				    IS_RFID(ppc_inst_read(bp->instr))? "rfid": "mtmsrd");
L
Linus Torvalds 已提交
719 720 721
			}
		}
	}
722
#endif
723 724 725 726
	if (locked_down)
		clear_all_bpt();
	else
		insert_cpu_bpts();
L
Linus Torvalds 已提交
727

728
	xmon_touch_watchdogs();
729
	local_irq_restore(flags);
L
Linus Torvalds 已提交
730

731
	return cmd != 'X' && cmd != EOF;
L
Linus Torvalds 已提交
732 733 734 735 736 737 738
}

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

	if (excp == NULL) {
739
		ppc_save_regs(&regs);
L
Linus Torvalds 已提交
740 741
		excp = &regs;
	}
742

L
Linus Torvalds 已提交
743 744
	return xmon_core(excp, 0);
}
P
Paul Mackerras 已提交
745 746
EXPORT_SYMBOL(xmon);

747
irqreturn_t xmon_irq(int irq, void *d)
P
Paul Mackerras 已提交
748 749 750 751
{
	unsigned long flags;
	local_irq_save(flags);
	printf("Keyboard interrupt\n");
752
	xmon(get_irq_regs());
P
Paul Mackerras 已提交
753 754 755
	local_irq_restore(flags);
	return IRQ_HANDLED;
}
L
Linus Torvalds 已提交
756

757
static int xmon_bpt(struct pt_regs *regs)
L
Linus Torvalds 已提交
758 759 760 761
{
	struct bpt *bp;
	unsigned long offset;

762
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
L
Linus Torvalds 已提交
763 764 765 766
		return 0;

	/* Are we at the trap at bp->instr[1] for some bp? */
	bp = in_breakpoint_table(regs->nip, &offset);
767 768
	if (bp != NULL && (offset == 4 || offset == 8)) {
		regs->nip = bp->address + offset;
L
Linus Torvalds 已提交
769 770 771 772 773 774 775 776 777 778 779 780 781 782
		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;
}

783
static int xmon_sstep(struct pt_regs *regs)
L
Linus Torvalds 已提交
784 785 786 787 788 789 790
{
	if (user_mode(regs))
		return 0;
	xmon_core(regs, 0);
	return 1;
}

791
static int xmon_break_match(struct pt_regs *regs)
L
Linus Torvalds 已提交
792
{
793 794
	int i;

795
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
L
Linus Torvalds 已提交
796
		return 0;
797 798 799 800 801 802 803
	for (i = 0; i < nr_wp_slots(); i++) {
		if (dabr[i].enabled)
			goto found;
	}
	return 0;

found:
L
Linus Torvalds 已提交
804 805 806 807
	xmon_core(regs, 0);
	return 1;
}

808
static int xmon_iabr_match(struct pt_regs *regs)
L
Linus Torvalds 已提交
809
{
810
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
L
Linus Torvalds 已提交
811
		return 0;
812
	if (iabr == NULL)
L
Linus Torvalds 已提交
813 814 815 816 817
		return 0;
	xmon_core(regs, 0);
	return 1;
}

818
static int xmon_ipi(struct pt_regs *regs)
L
Linus Torvalds 已提交
819 820
{
#ifdef CONFIG_SMP
821
	if (in_xmon && !cpumask_test_cpu(smp_processor_id(), &cpus_in_xmon))
L
Linus Torvalds 已提交
822 823 824 825 826
		xmon_core(regs, 1);
#endif
	return 0;
}

827
static int xmon_fault_handler(struct pt_regs *regs)
L
Linus Torvalds 已提交
828 829 830 831 832 833 834
{
	struct bpt *bp;
	unsigned long offset;

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

835
	if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
L
Linus Torvalds 已提交
836 837 838 839 840 841 842 843 844 845
		bp = in_breakpoint_table(regs->nip, &offset);
		if (bp != NULL) {
			regs->nip = bp->address + offset;
			atomic_dec(&bp->ref_count);
		}
	}

	return 0;
}

846 847 848 849 850 851 852 853 854 855
/* Force enable xmon if not already enabled */
static inline void force_enable_xmon(void)
{
	/* Enable xmon hooks if needed */
	if (!xmon_on) {
		printf("xmon: Enabling debugger hooks\n");
		xmon_on = 1;
	}
}

L
Linus Torvalds 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
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;

872 873
	off = nip - (unsigned long)bpt_table;
	if (off >= sizeof(bpt_table))
L
Linus Torvalds 已提交
874
		return NULL;
875 876
	*offp = off & (BPT_SIZE - 1);
	if (off & 3)
L
Linus Torvalds 已提交
877
		return NULL;
878
	return bpts + (off / BPT_SIZE);
L
Linus Torvalds 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891 892
}

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;
893
			bp->instr = (void *)(bpt_table + ((bp - bpts) * BPT_WORDS));
L
Linus Torvalds 已提交
894 895 896 897 898 899 900 901 902 903 904
			return bp;
		}
	}

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

static void insert_bpts(void)
{
	int i;
905 906
	struct ppc_inst instr, instr2;
	struct bpt *bp, *bp2;
L
Linus Torvalds 已提交
907 908 909

	bp = bpts;
	for (i = 0; i < NBPTS; ++i, ++bp) {
910
		if ((bp->enabled & (BP_TRAP|BP_CIABR)) == 0)
L
Linus Torvalds 已提交
911
			continue;
912
		if (!mread_instr(bp->address, &instr)) {
L
Linus Torvalds 已提交
913 914 915 916 917
			printf("Couldn't read instruction at %lx, "
			       "disabling breakpoint there\n", bp->address);
			bp->enabled = 0;
			continue;
		}
918
		if (IS_MTMSRD(instr) || IS_RFID(instr)) {
L
Linus Torvalds 已提交
919 920 921 922 923
			printf("Breakpoint at %lx is on an mtmsrd or rfid "
			       "instruction, disabling it\n", bp->address);
			bp->enabled = 0;
			continue;
		}
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
		/*
		 * Check the address is not a suffix by looking for a prefix in
		 * front of it.
		 */
		if (mread_instr(bp->address - 4, &instr2) == 8) {
			printf("Breakpoint at %lx is on the second word of a prefixed instruction, disabling it\n",
			       bp->address);
			bp->enabled = 0;
			continue;
		}
		/*
		 * We might still be a suffix - if the prefix has already been
		 * replaced by a breakpoint we won't catch it with the above
		 * test.
		 */
		bp2 = at_breakpoint(bp->address - 4);
		if (bp2 && ppc_inst_prefixed(ppc_inst_read(bp2->instr))) {
			printf("Breakpoint at %lx is on the second word of a prefixed instruction, disabling it\n",
			       bp->address);
			bp->enabled = 0;
			continue;
		}

947
		patch_instruction(bp->instr, instr);
M
Michael Ellerman 已提交
948
		patch_instruction(ppc_inst_next(bp->instr, &instr),
949
				  ppc_inst(bpinstr));
950
		if (bp->enabled & BP_CIABR)
L
Linus Torvalds 已提交
951
			continue;
952 953
		if (patch_instruction((struct ppc_inst *)bp->address,
				      ppc_inst(bpinstr)) != 0) {
L
Linus Torvalds 已提交
954 955 956 957 958 959 960 961 962 963
			printf("Couldn't write instruction at %lx, "
			       "disabling breakpoint there\n", bp->address);
			bp->enabled &= ~BP_TRAP;
			continue;
		}
	}
}

static void insert_cpu_bpts(void)
{
964
	int i;
965 966
	struct arch_hw_breakpoint brk;

967 968 969 970 971 972 973
	for (i = 0; i < nr_wp_slots(); i++) {
		if (dabr[i].enabled) {
			brk.address = dabr[i].address;
			brk.type = (dabr[i].enabled & HW_BRK_TYPE_DABR) | HW_BRK_TYPE_PRIV_ALL;
			brk.len = 8;
			__set_breakpoint(i, &brk);
		}
974
	}
975 976 977

	if (iabr)
		set_ciabr(iabr->address);
L
Linus Torvalds 已提交
978 979 980 981 982 983
}

static void remove_bpts(void)
{
	int i;
	struct bpt *bp;
984
	struct ppc_inst instr;
L
Linus Torvalds 已提交
985 986 987

	bp = bpts;
	for (i = 0; i < NBPTS; ++i, ++bp) {
988
		if ((bp->enabled & (BP_TRAP|BP_CIABR)) != BP_TRAP)
L
Linus Torvalds 已提交
989
			continue;
990
		if (mread_instr(bp->address, &instr)
991
		    && ppc_inst_equal(instr, ppc_inst(bpinstr))
992
		    && patch_instruction(
993
			(struct ppc_inst *)bp->address, ppc_inst_read(bp->instr)) != 0)
L
Linus Torvalds 已提交
994 995 996 997 998 999 1000
			printf("Couldn't remove breakpoint at %lx\n",
			       bp->address);
	}
}

static void remove_cpu_bpts(void)
{
1001
	hw_breakpoint_disable();
1002
	write_ciabr(0);
L
Linus Torvalds 已提交
1003 1004
}

1005 1006 1007 1008
/* Based on uptime_proc_show(). */
static void
show_uptime(void)
{
1009
	struct timespec64 uptime;
1010 1011 1012 1013 1014

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

1015
		ktime_get_coarse_boottime_ts64(&uptime);
1016 1017 1018 1019 1020 1021 1022 1023 1024
		printf("Uptime: %lu.%.2lu seconds\n", (unsigned long)uptime.tv_sec,
			((unsigned long)uptime.tv_nsec / (NSEC_PER_SEC/100)));

		sync();
		__delay(200);						\
	}
	catch_memory_errors = 0;
}

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
static void set_lpp_cmd(void)
{
	unsigned long lpp;

	if (!scanhex(&lpp)) {
		printf("Invalid number.\n");
		lpp = 0;
	}
	xmon_set_pagination_lpp(lpp);
}
L
Linus Torvalds 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
/* Command interpreting routine */
static char *last_cmd;

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

	last_cmd = NULL;
	xmon_regs = excp;
1045

1046
	xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
1047

L
Linus Torvalds 已提交
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
	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':
1076 1077 1078 1079
				if (xmon_is_ro) {
					printf(xmon_ro_msg);
					break;
				}
L
Linus Torvalds 已提交
1080 1081 1082
				memzcan();
				break;
			case 'i':
1083
				show_mem(0, NULL);
L
Linus Torvalds 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
				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':
1112 1113
			if (do_spu_cmd() == 0)
				break;
L
Linus Torvalds 已提交
1114 1115 1116 1117 1118
			if (do_step(excp))
				return cmd;
			break;
		case 'x':
		case 'X':
1119 1120
			if (tracing_enabled)
				tracing_on();
1121
			return cmd;
L
Linus Torvalds 已提交
1122
		case EOF:
1123 1124
			printf(" <no input ...>\n");
			mdelay(2000);
L
Linus Torvalds 已提交
1125 1126
			return cmd;
		case '?':
I
Ishizaki Kou 已提交
1127
			xmon_puts(help_string);
L
Linus Torvalds 已提交
1128
			break;
1129 1130 1131
		case '#':
			set_lpp_cmd();
			break;
L
Linus Torvalds 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
		case 'b':
			bpt_cmds();
			break;
		case 'C':
			csum();
			break;
		case 'c':
			if (cpu_cmd())
				return 0;
			break;
		case 'z':
			bootcmds();
			break;
P
Paul Mackerras 已提交
1145
		case 'p':
1146 1147 1148 1149
			if (xmon_is_ro) {
				printf(xmon_ro_msg);
				break;
			}
P
Paul Mackerras 已提交
1150
			proccall();
L
Linus Torvalds 已提交
1151
			break;
1152 1153 1154
		case 'P':
			show_tasks();
			break;
1155
#ifdef CONFIG_PPC_BOOK3S
L
Linus Torvalds 已提交
1156 1157 1158
		case 'u':
			dump_segments();
			break;
1159
#elif defined(CONFIG_44x)
1160 1161 1162
		case 'u':
			dump_tlb_44x();
			break;
1163
#elif defined(CONFIG_PPC_BOOK3E)
1164 1165 1166
		case 'u':
			dump_tlb_book3e();
			break;
P
Paul Mackerras 已提交
1167
#endif
1168 1169 1170
		case 'U':
			show_uptime();
			break;
L
Linus Torvalds 已提交
1171 1172
		default:
			printf("Unrecognized command: ");
1173
			do {
L
Linus Torvalds 已提交
1174 1175 1176 1177 1178
				if (' ' < cmd && cmd <= '~')
					putchar(cmd);
				else
					printf("\\x%x", cmd);
				cmd = inchar();
1179
			} while (cmd != '\n');
L
Linus Torvalds 已提交
1180 1181 1182 1183 1184 1185
			printf(" (type ? for help)\n");
			break;
		}
	}
}

1186 1187 1188 1189 1190 1191 1192 1193
#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 已提交
1194 1195 1196 1197 1198 1199
/*
 * Step a single instruction.
 * Some instructions we emulate, others we execute with MSR_SE set.
 */
static int do_step(struct pt_regs *regs)
{
1200
	struct ppc_inst instr;
L
Linus Torvalds 已提交
1201 1202
	int stepped;

1203
	force_enable_xmon();
L
Linus Torvalds 已提交
1204
	/* check we are in 64-bit kernel mode, translation enabled */
1205
	if ((regs->msr & (MSR_64BIT|MSR_PR|MSR_IR)) == (MSR_64BIT|MSR_IR)) {
1206
		if (mread_instr(regs->nip, &instr)) {
L
Linus Torvalds 已提交
1207 1208 1209 1210 1211 1212 1213
			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) {
1214
				set_trap(regs, 0xd00);
L
Linus Torvalds 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
				printf("stepped to ");
				xmon_print_symbol(regs->nip, " ", "\n");
				ppc_inst_dump(regs->nip, 1, 0);
				return 0;
			}
		}
	}
	regs->msr |= MSR_SE;
	return 1;
}
1225
#endif
L
Linus Torvalds 已提交
1226 1227 1228

static void bootcmds(void)
{
1229
	char tmp[64];
L
Linus Torvalds 已提交
1230 1231 1232
	int cmd;

	cmd = inchar();
1233 1234 1235 1236
	if (cmd == 'r') {
		getstring(tmp, 64);
		ppc_md.restart(tmp);
	} else if (cmd == 'h') {
L
Linus Torvalds 已提交
1237
		ppc_md.halt();
1238
	} else if (cmd == 'p') {
1239 1240
		if (pm_power_off)
			pm_power_off();
1241
	}
L
Linus Torvalds 已提交
1242 1243 1244 1245 1246
}

static int cpu_cmd(void)
{
#ifdef CONFIG_SMP
1247
	unsigned long cpu, first_cpu, last_cpu;
L
Linus Torvalds 已提交
1248 1249 1250 1251 1252
	int timeout;

	if (!scanhex(&cpu)) {
		/* print cpus waiting or in xmon */
		printf("cpus stopped:");
1253
		last_cpu = first_cpu = NR_CPUS;
1254
		for_each_possible_cpu(cpu) {
1255
			if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
1256 1257 1258 1259
				if (cpu == last_cpu + 1) {
					last_cpu = cpu;
				} else {
					if (last_cpu != first_cpu)
1260
						printf("-0x%lx", last_cpu);
1261
					last_cpu = first_cpu = cpu;
1262
					printf(" 0x%lx", cpu);
1263
				}
L
Linus Torvalds 已提交
1264 1265
			}
		}
1266
		if (last_cpu != first_cpu)
1267
			printf("-0x%lx", last_cpu);
L
Linus Torvalds 已提交
1268 1269 1270 1271
		printf("\n");
		return 0;
	}
	/* try to switch to cpu specified */
1272
	if (!cpumask_test_cpu(cpu, &cpus_in_xmon)) {
1273
		printf("cpu 0x%lx isn't in xmon\n", cpu);
1274 1275 1276 1277
#ifdef CONFIG_PPC64
		printf("backtrace of paca[0x%lx].saved_r1 (possibly stale):\n", cpu);
		xmon_show_stack(paca_ptrs[cpu]->saved_r1, 0, 0);
#endif
L
Linus Torvalds 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
		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();
1291
			printf("cpu 0x%lx didn't take control\n", cpu);
L
Linus Torvalds 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
			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) {
1353
			printf("csum stopped at "REG"\n", adrs+i);
L
Linus Torvalds 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
			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)
{
1366
	struct ppc_inst instr;
L
Linus Torvalds 已提交
1367 1368

	addr &= ~3;
1369
	if (!is_kernel_addr(addr)) {
L
Linus Torvalds 已提交
1370 1371 1372
		printf("Breakpoints may only be placed at kernel addresses\n");
		return 0;
	}
1373
	if (!mread_instr(addr, &instr)) {
L
Linus Torvalds 已提交
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
		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;
}

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
static int find_free_data_bpt(void)
{
	int i;

	for (i = 0; i < nr_wp_slots(); i++) {
		if (!dabr[i].enabled)
			return i;
	}
	printf("Couldn't find free breakpoint register\n");
	return -1;
}

static void print_data_bpts(void)
{
	int i;

	for (i = 0; i < nr_wp_slots(); i++) {
		if (!dabr[i].enabled)
			continue;

		printf("   data   "REG"  [", dabr[i].address);
		if (dabr[i].enabled & 1)
			printf("r");
		if (dabr[i].enabled & 2)
			printf("w");
		printf("]\n");
	}
}

1414
static char *breakpoint_help_string =
L
Linus Torvalds 已提交
1415 1416 1417 1418 1419
    "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"
1420
    "bi <addr> [cnt]  set hardware instr breakpoint (POWER8 only)\n"
L
Linus Torvalds 已提交
1421 1422 1423 1424 1425 1426 1427 1428
    "bd <addr> [cnt]  set hardware data breakpoint\n"
    "";

static void
bpt_cmds(void)
{
	int cmd;
	unsigned long a;
1429
	int i;
L
Linus Torvalds 已提交
1430 1431 1432
	struct bpt *bp;

	cmd = inchar();
1433

L
Linus Torvalds 已提交
1434
	switch (cmd) {
1435 1436 1437
#ifndef CONFIG_PPC_8xx
	static const char badaddr[] = "Only kernel addresses are permitted for breakpoints\n";
	int mode;
L
Linus Torvalds 已提交
1438
	case 'd':	/* bd - hardware data breakpoint */
1439 1440 1441 1442
		if (xmon_is_ro) {
			printf(xmon_ro_msg);
			break;
		}
1443 1444 1445 1446
		if (!ppc_breakpoint_available()) {
			printf("Hardware data breakpoint not supported on this cpu\n");
			break;
		}
1447 1448
		i = find_free_data_bpt();
		if (i < 0)
1449
			break;
L
Linus Torvalds 已提交
1450 1451 1452 1453 1454 1455 1456 1457
		mode = 7;
		cmd = inchar();
		if (cmd == 'r')
			mode = 5;
		else if (cmd == 'w')
			mode = 6;
		else
			termch = cmd;
1458 1459 1460 1461
		dabr[i].address = 0;
		dabr[i].enabled = 0;
		if (scanhex(&dabr[i].address)) {
			if (!is_kernel_addr(dabr[i].address)) {
L
Linus Torvalds 已提交
1462 1463 1464
				printf(badaddr);
				break;
			}
1465 1466
			dabr[i].address &= ~HW_BRK_TYPE_DABR;
			dabr[i].enabled = mode | BP_DABR;
L
Linus Torvalds 已提交
1467
		}
1468 1469

		force_enable_xmon();
L
Linus Torvalds 已提交
1470 1471 1472
		break;

	case 'i':	/* bi - hardware instr breakpoint */
1473 1474 1475 1476
		if (xmon_is_ro) {
			printf(xmon_ro_msg);
			break;
		}
1477
		if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
L
Linus Torvalds 已提交
1478 1479 1480 1481 1482
			printf("Hardware instruction breakpoint "
			       "not supported on this cpu\n");
			break;
		}
		if (iabr) {
1483
			iabr->enabled &= ~BP_CIABR;
L
Linus Torvalds 已提交
1484 1485 1486 1487 1488 1489 1490 1491
			iabr = NULL;
		}
		if (!scanhex(&a))
			break;
		if (!check_bp_loc(a))
			break;
		bp = new_breakpoint(a);
		if (bp != NULL) {
1492
			bp->enabled |= BP_CIABR;
L
Linus Torvalds 已提交
1493
			iabr = bp;
1494
			force_enable_xmon();
L
Linus Torvalds 已提交
1495 1496
		}
		break;
P
Paul Mackerras 已提交
1497
#endif
L
Linus Torvalds 已提交
1498 1499 1500 1501 1502 1503 1504

	case 'c':
		if (!scanhex(&a)) {
			/* clear all breakpoints */
			for (i = 0; i < NBPTS; ++i)
				bpts[i].enabled = 0;
			iabr = NULL;
1505 1506 1507
			for (i = 0; i < nr_wp_slots(); i++)
				dabr[i].enabled = 0;

L
Linus Torvalds 已提交
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
			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);
1518
			if (bp == NULL) {
1519
				printf("No breakpoint at %lx\n", a);
L
Linus Torvalds 已提交
1520 1521 1522 1523
				break;
			}
		}

1524
		printf("Cleared breakpoint %tx (", BP_NUM(bp));
L
Linus Torvalds 已提交
1525 1526 1527 1528 1529 1530
		xmon_print_symbol(bp->address, " ", ")\n");
		bp->enabled = 0;
		break;

	default:
		termch = cmd;
1531
		cmd = skipbl();
L
Linus Torvalds 已提交
1532 1533 1534 1535 1536
		if (cmd == '?') {
			printf(breakpoint_help_string);
			break;
		}
		termch = cmd;
1537 1538

		if (xmon_is_ro || !scanhex(&a)) {
L
Linus Torvalds 已提交
1539 1540
			/* print all breakpoints */
			printf("   type            address\n");
1541
			print_data_bpts();
L
Linus Torvalds 已提交
1542 1543 1544
			for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
				if (!bp->enabled)
					continue;
1545
				printf("%tx %s   ", BP_NUM(bp),
1546
				    (bp->enabled & BP_CIABR) ? "inst": "trap");
L
Linus Torvalds 已提交
1547 1548 1549 1550 1551 1552 1553 1554
				xmon_print_symbol(bp->address, "  ", "\n");
			}
			break;
		}

		if (!check_bp_loc(a))
			break;
		bp = new_breakpoint(a);
1555
		if (bp != NULL) {
L
Linus Torvalds 已提交
1556
			bp->enabled |= BP_TRAP;
1557 1558
			force_enable_xmon();
		}
L
Linus Torvalds 已提交
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
		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;
1573 1574 1575 1576 1577 1578
	case 0x380:
		if (radix_enabled())
			ret = "(Data Access Out of Range)";
		else
			ret = "(Data SLB Access)";
		break;
L
Linus Torvalds 已提交
1579
	case 0x400:	ret = "(Instruction Access)"; break;
1580 1581 1582 1583 1584 1585
	case 0x480:
		if (radix_enabled())
			ret = "(Instruction Access Out of Range)";
		else
			ret = "(Instruction SLB Access)";
		break;
L
Linus Torvalds 已提交
1586 1587 1588 1589 1590
	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;
1591 1592
	case 0x980:	ret = "(Hypervisor Decrementer)"; break;
	case 0xa00:	ret = "(Doorbell)"; break;
L
Linus Torvalds 已提交
1593 1594
	case 0xc00:	ret = "(System Call)"; break;
	case 0xd00:	ret = "(Single Step)"; break;
1595 1596 1597
	case 0xe40:	ret = "(Emulation Assist)"; break;
	case 0xe60:	ret = "(HMI)"; break;
	case 0xe80:	ret = "(Hypervisor Doorbell)"; break;
L
Linus Torvalds 已提交
1598 1599 1600
	case 0xf00:	ret = "(Performance Monitor)"; break;
	case 0xf20:	ret = "(Altivec Unavailable)"; break;
	case 0x1300:	ret = "(Instruction Breakpoint)"; break;
1601 1602
	case 0x1500:	ret = "(Denormalisation)"; break;
	case 0x1700:	ret = "(Altivec Assist)"; break;
L
Linus Torvalds 已提交
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
	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 已提交
1620
		name = kallsyms_lookup(pc, &size, &offset, NULL, tmpstr);
L
Linus Torvalds 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629
		if (name != NULL) {
			*startp = pc - offset;
			*endp = pc - offset + size;
		}
		sync();
	}
	catch_memory_errors = 0;
}

1630 1631 1632
#define LRSAVE_OFFSET		(STACK_FRAME_LR_SAVE * sizeof(unsigned long))
#define MARKER_OFFSET		(STACK_FRAME_MARKER * sizeof(unsigned long))

L
Linus Torvalds 已提交
1633 1634 1635
static void xmon_show_stack(unsigned long sp, unsigned long lr,
			    unsigned long pc)
{
1636
	int max_to_print = 64;
L
Linus Torvalds 已提交
1637 1638 1639 1640 1641
	unsigned long ip;
	unsigned long newsp;
	unsigned long marker;
	struct pt_regs regs;

1642
	while (max_to_print--) {
1643
		if (!is_kernel_addr(sp)) {
L
Linus Torvalds 已提交
1644 1645 1646 1647 1648
			if (sp != 0)
				printf("SP (%lx) is in userspace\n", sp);
			break;
		}

P
Paul Mackerras 已提交
1649
		if (!mread(sp + LRSAVE_OFFSET, &ip, sizeof(unsigned long))
L
Linus Torvalds 已提交
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
		    || !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 已提交
1668
				mread(newsp + LRSAVE_OFFSET, &nextip,
L
Linus Torvalds 已提交
1669 1670
				      sizeof(unsigned long));
			if (lr == ip) {
1671
				if (!is_kernel_addr(lr)
L
Linus Torvalds 已提交
1672 1673 1674 1675
				    || (fnstart <= lr && lr < fnend))
					printip = 0;
			} else if (lr == nextip) {
				printip = 0;
1676
			} else if (is_kernel_addr(lr)
L
Linus Torvalds 已提交
1677 1678 1679 1680 1681
				   && !(fnstart <= lr && lr < fnend)) {
				printf("[link register   ] ");
				xmon_print_symbol(lr, " ", "\n");
			}
			if (printip) {
P
Paul Mackerras 已提交
1682
				printf("["REG"] ", sp);
L
Linus Torvalds 已提交
1683 1684 1685 1686 1687
				xmon_print_symbol(ip, " ", " (unreliable)\n");
			}
			pc = lr = 0;

		} else {
P
Paul Mackerras 已提交
1688
			printf("["REG"] ", sp);
L
Linus Torvalds 已提交
1689 1690 1691 1692 1693
			xmon_print_symbol(ip, " ", "\n");
		}

		/* Look for "regshere" marker to see if this is
		   an exception frame. */
P
Paul Mackerras 已提交
1694
		if (mread(sp + MARKER_OFFSET, &marker, sizeof(unsigned long))
1695
		    && marker == STACK_FRAME_REGS_MARKER) {
1696
			if (mread(sp + STACK_FRAME_OVERHEAD, &regs, sizeof(regs))
L
Linus Torvalds 已提交
1697 1698
			    != sizeof(regs)) {
				printf("Couldn't read registers at %lx\n",
1699
				       sp + STACK_FRAME_OVERHEAD);
L
Linus Torvalds 已提交
1700 1701
				break;
			}
1702
			printf("--- Exception: %lx %s at ", regs.trap,
L
Linus Torvalds 已提交
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
			       getvecname(TRAP(&regs)));
			pc = regs.nip;
			lr = regs.link;
			xmon_print_symbol(pc, " ", "\n");
		}

		if (newsp == 0)
			break;

		sp = newsp;
1713
	}
L
Linus Torvalds 已提交
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
}

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)
{
1729
#ifdef CONFIG_BUG
1730
	const struct bug_entry *bug;
L
Linus Torvalds 已提交
1731 1732 1733 1734 1735
	unsigned long addr;

	if (regs->msr & MSR_PR)
		return;		/* not in kernel */
	addr = regs->nip;	/* address of trap instruction */
1736
	if (!is_kernel_addr(addr))
L
Linus Torvalds 已提交
1737 1738 1739 1740
		return;
	bug = find_bug(regs->nip);
	if (bug == NULL)
		return;
1741
	if (is_warning_bug(bug))
L
Linus Torvalds 已提交
1742 1743
		return;

1744
#ifdef CONFIG_DEBUG_BUGVERBOSE
1745 1746
	printf("kernel BUG at %s:%u!\n",
	       bug->file, bug->line);
1747
#else
1748
	printf("kernel BUG at %px!\n", (void *)bug->bug_addr);
1749
#endif
1750
#endif /* CONFIG_BUG */
L
Linus Torvalds 已提交
1751 1752
}

1753
static void excprint(struct pt_regs *fp)
L
Linus Torvalds 已提交
1754 1755 1756 1757 1758 1759 1760 1761
{
	unsigned long trap;

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

	trap = TRAP(fp);
1762
	printf("Vector: %lx %s at [%px]\n", fp->trap, getvecname(trap), fp);
L
Linus Torvalds 已提交
1763 1764 1765
	printf("    pc: ");
	xmon_print_symbol(fp->nip, ": ", "\n");

1766
	printf("    lr: ");
L
Linus Torvalds 已提交
1767 1768 1769 1770 1771
	xmon_print_symbol(fp->link, ": ", "\n");

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

1772
	if (trap == 0x300 || trap == 0x380 || trap == 0x600 || trap == 0x200) {
L
Linus Torvalds 已提交
1773 1774 1775 1776 1777
		printf("   dar: %lx\n", fp->dar);
		if (trap != 0x380)
			printf(" dsisr: %lx\n", fp->dsisr);
	}

1778
	printf("  current = 0x%px\n", current);
P
Paul Mackerras 已提交
1779
#ifdef CONFIG_PPC64
1780
	printf("  paca    = 0x%px\t irqmask: 0x%02x\t irq_happened: 0x%02x\n",
1781
	       local_paca, local_paca->irq_soft_mask, local_paca->irq_happened);
P
Paul Mackerras 已提交
1782
#endif
L
Linus Torvalds 已提交
1783
	if (current) {
1784
		printf("    pid   = %d, comm = %s\n",
L
Linus Torvalds 已提交
1785 1786 1787 1788 1789
		       current->pid, current->comm);
	}

	if (trap == 0x700)
		print_bug_trap(fp);
1790 1791

	printf(linux_banner);
L
Linus Torvalds 已提交
1792 1793
}

1794
static void prregs(struct pt_regs *fp)
L
Linus Torvalds 已提交
1795
{
P
Paul Mackerras 已提交
1796
	int n, trap;
L
Linus Torvalds 已提交
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
	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 已提交
1809
			printf("*** Error reading registers from "REG"\n",
L
Linus Torvalds 已提交
1810 1811 1812 1813 1814 1815 1816
			       base);
			return;
		}
		catch_memory_errors = 0;
		fp = &regs;
	}

P
Paul Mackerras 已提交
1817
#ifdef CONFIG_PPC64
L
Linus Torvalds 已提交
1818 1819
	if (FULL_REGS(fp)) {
		for (n = 0; n < 16; ++n)
1820
			printf("R%.2d = "REG"   R%.2d = "REG"\n",
L
Linus Torvalds 已提交
1821 1822 1823
			       n, fp->gpr[n], n+16, fp->gpr[n+16]);
	} else {
		for (n = 0; n < 7; ++n)
1824
			printf("R%.2d = "REG"   R%.2d = "REG"\n",
L
Linus Torvalds 已提交
1825 1826
			       n, fp->gpr[n], n+7, fp->gpr[n+7]);
	}
P
Paul Mackerras 已提交
1827 1828
#else
	for (n = 0; n < 32; ++n) {
1829
		printf("R%.2d = %.8lx%s", n, fp->gpr[n],
P
Paul Mackerras 已提交
1830 1831 1832 1833 1834 1835 1836
		       (n & 3) == 3? "\n": "   ");
		if (n == 12 && !FULL_REGS(fp)) {
			printf("\n");
			break;
		}
	}
#endif
L
Linus Torvalds 已提交
1837 1838
	printf("pc  = ");
	xmon_print_symbol(fp->nip, " ", "\n");
1839
	if (!trap_is_syscall(fp) && cpu_has_feature(CPU_FTR_CFAR)) {
1840 1841 1842
		printf("cfar= ");
		xmon_print_symbol(fp->orig_gpr3, " ", "\n");
	}
L
Linus Torvalds 已提交
1843 1844
	printf("lr  = ");
	xmon_print_symbol(fp->link, " ", "\n");
P
Paul Mackerras 已提交
1845 1846
	printf("msr = "REG"   cr  = %.8lx\n", fp->msr, fp->ccr);
	printf("ctr = "REG"   xer = "REG"   trap = %4lx\n",
L
Linus Torvalds 已提交
1847
	       fp->ctr, fp->xer, fp->trap);
P
Paul Mackerras 已提交
1848 1849 1850
	trap = TRAP(fp);
	if (trap == 0x300 || trap == 0x380 || trap == 0x600)
		printf("dar = "REG"   dsisr = %.8lx\n", fp->dar, fp->dsisr);
L
Linus Torvalds 已提交
1851 1852
}

1853
static void cacheflush(void)
L
Linus Torvalds 已提交
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
{
	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;
}

1885 1886 1887 1888 1889
extern unsigned long xmon_mfspr(int spr, unsigned long default_value);
extern void xmon_mtspr(int spr, unsigned long value);

static int
read_spr(int n, unsigned long *vp)
L
Linus Torvalds 已提交
1890 1891
{
	unsigned long ret = -1UL;
1892
	int ok = 0;
L
Linus Torvalds 已提交
1893 1894

	if (setjmp(bus_error_jmp) == 0) {
1895
		catch_spr_faults = 1;
L
Linus Torvalds 已提交
1896 1897
		sync();

1898
		ret = xmon_mfspr(n, *vp);
L
Linus Torvalds 已提交
1899 1900

		sync();
1901 1902
		*vp = ret;
		ok = 1;
L
Linus Torvalds 已提交
1903
	}
1904
	catch_spr_faults = 0;
L
Linus Torvalds 已提交
1905

1906
	return ok;
L
Linus Torvalds 已提交
1907 1908
}

1909
static void
L
Linus Torvalds 已提交
1910 1911
write_spr(int n, unsigned long val)
{
1912 1913 1914 1915 1916
	if (xmon_is_ro) {
		printf(xmon_ro_msg);
		return;
	}

L
Linus Torvalds 已提交
1917
	if (setjmp(bus_error_jmp) == 0) {
1918
		catch_spr_faults = 1;
L
Linus Torvalds 已提交
1919 1920
		sync();

1921
		xmon_mtspr(n, val);
L
Linus Torvalds 已提交
1922 1923

		sync();
1924 1925
	} else {
		printf("SPR 0x%03x (%4d) Faulted during write\n", n, n);
L
Linus Torvalds 已提交
1926
	}
1927
	catch_spr_faults = 0;
L
Linus Torvalds 已提交
1928 1929
}

1930 1931 1932 1933 1934 1935 1936 1937
static void dump_206_sprs(void)
{
#ifdef CONFIG_PPC64
	if (!cpu_has_feature(CPU_FTR_ARCH_206))
		return;

	/* Actually some of these pre-date 2.06, but whatevs */

1938
	printf("srr0   = %.16lx  srr1  = %.16lx dsisr  = %.8lx\n",
1939
		mfspr(SPRN_SRR0), mfspr(SPRN_SRR1), mfspr(SPRN_DSISR));
1940
	printf("dscr   = %.16lx  ppr   = %.16lx pir    = %.8lx\n",
1941
		mfspr(SPRN_DSCR), mfspr(SPRN_PPR), mfspr(SPRN_PIR));
1942 1943
	printf("amr    = %.16lx  uamor = %.16lx\n",
		mfspr(SPRN_AMR), mfspr(SPRN_UAMOR));
1944 1945 1946 1947

	if (!(mfmsr() & MSR_HV))
		return;

1948
	printf("sdr1   = %.16lx  hdar  = %.16lx hdsisr = %.8lx\n",
1949
		mfspr(SPRN_SDR1), mfspr(SPRN_HDAR), mfspr(SPRN_HDSISR));
1950
	printf("hsrr0  = %.16lx hsrr1  = %.16lx hdec   = %.16lx\n",
1951
		mfspr(SPRN_HSRR0), mfspr(SPRN_HSRR1), mfspr(SPRN_HDEC));
1952
	printf("lpcr   = %.16lx  pcr   = %.16lx lpidr  = %.8lx\n",
1953
		mfspr(SPRN_LPCR), mfspr(SPRN_PCR), mfspr(SPRN_LPID));
1954 1955
	printf("hsprg0 = %.16lx hsprg1 = %.16lx amor   = %.16lx\n",
		mfspr(SPRN_HSPRG0), mfspr(SPRN_HSPRG1), mfspr(SPRN_AMOR));
B
Balbir Singh 已提交
1956
	printf("dabr   = %.16lx dabrx  = %.16lx\n",
1957 1958 1959 1960
		mfspr(SPRN_DABR), mfspr(SPRN_DABRX));
#endif
}

1961 1962 1963 1964 1965 1966 1967 1968
static void dump_207_sprs(void)
{
#ifdef CONFIG_PPC64
	unsigned long msr;

	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
		return;

1969
	printf("dpdes  = %.16lx  tir   = %.16lx cir    = %.8lx\n",
1970 1971
		mfspr(SPRN_DPDES), mfspr(SPRN_TIR), mfspr(SPRN_CIR));

1972
	printf("fscr   = %.16lx  tar   = %.16lx pspb   = %.8lx\n",
1973 1974 1975 1976 1977
		mfspr(SPRN_FSCR), mfspr(SPRN_TAR), mfspr(SPRN_PSPB));

	msr = mfmsr();
	if (msr & MSR_TM) {
		/* Only if TM has been enabled in the kernel */
B
Balbir Singh 已提交
1978
		printf("tfhar  = %.16lx  tfiar = %.16lx texasr = %.16lx\n",
1979 1980 1981 1982
			mfspr(SPRN_TFHAR), mfspr(SPRN_TFIAR),
			mfspr(SPRN_TEXASR));
	}

B
Balbir Singh 已提交
1983
	printf("mmcr0  = %.16lx  mmcr1 = %.16lx mmcr2  = %.16lx\n",
1984
		mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCR2));
1985
	printf("pmc1   = %.8lx pmc2 = %.8lx  pmc3 = %.8lx  pmc4   = %.8lx\n",
1986 1987
		mfspr(SPRN_PMC1), mfspr(SPRN_PMC2),
		mfspr(SPRN_PMC3), mfspr(SPRN_PMC4));
1988
	printf("mmcra  = %.16lx   siar = %.16lx pmc5   = %.8lx\n",
1989
		mfspr(SPRN_MMCRA), mfspr(SPRN_SIAR), mfspr(SPRN_PMC5));
1990
	printf("sdar   = %.16lx   sier = %.16lx pmc6   = %.8lx\n",
1991
		mfspr(SPRN_SDAR), mfspr(SPRN_SIER), mfspr(SPRN_PMC6));
B
Balbir Singh 已提交
1992
	printf("ebbhr  = %.16lx  ebbrr = %.16lx bescr  = %.16lx\n",
1993
		mfspr(SPRN_EBBHR), mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
1994
	printf("iamr   = %.16lx\n", mfspr(SPRN_IAMR));
1995 1996 1997 1998

	if (!(msr & MSR_HV))
		return;

B
Balbir Singh 已提交
1999
	printf("hfscr  = %.16lx  dhdes = %.16lx rpr    = %.16lx\n",
2000
		mfspr(SPRN_HFSCR), mfspr(SPRN_DHDES), mfspr(SPRN_RPR));
2001 2002 2003 2004 2005 2006 2007
	printf("dawr0  = %.16lx dawrx0 = %.16lx\n",
	       mfspr(SPRN_DAWR0), mfspr(SPRN_DAWRX0));
	if (nr_wp_slots() > 1) {
		printf("dawr1  = %.16lx dawrx1 = %.16lx\n",
		       mfspr(SPRN_DAWR1), mfspr(SPRN_DAWRX1));
	}
	printf("ciabr  = %.16lx\n", mfspr(SPRN_CIABR));
2008 2009
#endif
}
L
Linus Torvalds 已提交
2010

2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
static void dump_300_sprs(void)
{
#ifdef CONFIG_PPC64
	bool hv = mfmsr() & MSR_HV;

	if (!cpu_has_feature(CPU_FTR_ARCH_300))
		return;

	printf("pidr   = %.16lx  tidr  = %.16lx\n",
		mfspr(SPRN_PID), mfspr(SPRN_TIDR));
2021 2022
	printf("psscr  = %.16lx\n",
		hv ? mfspr(SPRN_PSSCR) : mfspr(SPRN_PSSCR_PR));
2023 2024 2025 2026

	if (!hv)
		return;

2027 2028
	printf("ptcr   = %.16lx  asdr  = %.16lx\n",
		mfspr(SPRN_PTCR), mfspr(SPRN_ASDR));
2029 2030 2031
#endif
}

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
static void dump_one_spr(int spr, bool show_unimplemented)
{
	unsigned long val;

	val = 0xdeadbeef;
	if (!read_spr(spr, &val)) {
		printf("SPR 0x%03x (%4d) Faulted during read\n", spr, spr);
		return;
	}

	if (val == 0xdeadbeef) {
		/* Looks like read was a nop, confirm */
		val = 0x0badcafe;
		if (!read_spr(spr, &val)) {
			printf("SPR 0x%03x (%4d) Faulted during read\n", spr, spr);
			return;
		}

		if (val == 0x0badcafe) {
			if (show_unimplemented)
				printf("SPR 0x%03x (%4d) Unimplemented\n", spr, spr);
			return;
		}
	}

	printf("SPR 0x%03x (%4d) = 0x%lx\n", spr, spr, val);
}

2060
static void super_regs(void)
L
Linus Torvalds 已提交
2061
{
2062
	static unsigned long regno;
L
Linus Torvalds 已提交
2063
	int cmd;
2064
	int spr;
L
Linus Torvalds 已提交
2065 2066

	cmd = skipbl();
2067 2068 2069

	switch (cmd) {
	case '\n': {
2070
		unsigned long sp, toc;
L
Linus Torvalds 已提交
2071 2072 2073
		asm("mr %0,1" : "=r" (sp) :);
		asm("mr %0,2" : "=r" (toc) :);

2074
		printf("msr    = "REG"  sprg0 = "REG"\n",
P
Paul Mackerras 已提交
2075
		       mfmsr(), mfspr(SPRN_SPRG0));
2076
		printf("pvr    = "REG"  sprg1 = "REG"\n",
2077
		       mfspr(SPRN_PVR), mfspr(SPRN_SPRG1));
2078
		printf("dec    = "REG"  sprg2 = "REG"\n",
P
Paul Mackerras 已提交
2079
		       mfspr(SPRN_DEC), mfspr(SPRN_SPRG2));
2080 2081 2082
		printf("sp     = "REG"  sprg3 = "REG"\n", sp, mfspr(SPRN_SPRG3));
		printf("toc    = "REG"  dar   = "REG"\n", toc, mfspr(SPRN_DAR));

2083
		dump_206_sprs();
2084
		dump_207_sprs();
2085
		dump_300_sprs();
2086

L
Linus Torvalds 已提交
2087 2088
		return;
	}
2089 2090 2091 2092 2093
	case 'w': {
		unsigned long val;
		scanhex(&regno);
		val = 0;
		read_spr(regno, &val);
L
Linus Torvalds 已提交
2094 2095
		scanhex(&val);
		write_spr(regno, val);
2096 2097 2098
		dump_one_spr(regno, true);
		break;
	}
L
Linus Torvalds 已提交
2099
	case 'r':
2100 2101 2102 2103 2104 2105 2106
		scanhex(&regno);
		dump_one_spr(regno, true);
		break;
	case 'a':
		/* dump ALL SPRs */
		for (spr = 1; spr < 1024; ++spr)
			dump_one_spr(spr, false);
L
Linus Torvalds 已提交
2107 2108
		break;
	}
2109

L
Linus Torvalds 已提交
2110 2111 2112 2113 2114 2115
	scannl();
}

/*
 * Stuff for reading and writing memory safely
 */
2116
static int
L
Linus Torvalds 已提交
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
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 已提交
2130
			*(u16 *)q = *(u16 *)p;
L
Linus Torvalds 已提交
2131 2132
			break;
		case 4:
P
Paul Mackerras 已提交
2133
			*(u32 *)q = *(u32 *)p;
L
Linus Torvalds 已提交
2134 2135
			break;
		case 8:
P
Paul Mackerras 已提交
2136
			*(u64 *)q = *(u64 *)p;
L
Linus Torvalds 已提交
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
			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;
}

2153
static int
L
Linus Torvalds 已提交
2154 2155 2156 2157 2158 2159
mwrite(unsigned long adrs, void *buf, int size)
{
	volatile int n;
	char *p, *q;

	n = 0;
2160 2161 2162 2163 2164 2165

	if (xmon_is_ro) {
		printf(xmon_ro_msg);
		return n;
	}

L
Linus Torvalds 已提交
2166 2167 2168 2169 2170 2171 2172
	if (setjmp(bus_error_jmp) == 0) {
		catch_memory_errors = 1;
		sync();
		p = (char *) adrs;
		q = (char *) buf;
		switch (size) {
		case 2:
P
Paul Mackerras 已提交
2173
			*(u16 *)p = *(u16 *)q;
L
Linus Torvalds 已提交
2174 2175
			break;
		case 4:
P
Paul Mackerras 已提交
2176
			*(u32 *)p = *(u32 *)q;
L
Linus Torvalds 已提交
2177 2178
			break;
		case 8:
P
Paul Mackerras 已提交
2179
			*(u64 *)p = *(u64 *)q;
L
Linus Torvalds 已提交
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
			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 {
2192
		printf("*** Error writing address "REG"\n", adrs + n);
L
Linus Torvalds 已提交
2193 2194 2195 2196 2197
	}
	catch_memory_errors = 0;
	return n;
}

2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
static int
mread_instr(unsigned long adrs, struct ppc_inst *instr)
{
	volatile int n;

	n = 0;
	if (setjmp(bus_error_jmp) == 0) {
		catch_memory_errors = 1;
		sync();
		*instr = ppc_inst_read((struct ppc_inst *)adrs);
		sync();
		/* wait a little while to see if we get a machine check */
		__delay(200);
		n = ppc_inst_len(*instr);
	}
	catch_memory_errors = 0;
	return n;
}

L
Linus Torvalds 已提交
2217
static int fault_type;
P
Paul Mackerras 已提交
2218
static int fault_except;
L
Linus Torvalds 已提交
2219 2220
static char *fault_chars[] = { "--", "**", "##" };

P
Paul Mackerras 已提交
2221
static int handle_fault(struct pt_regs *regs)
L
Linus Torvalds 已提交
2222
{
P
Paul Mackerras 已提交
2223
	fault_except = TRAP(regs);
L
Linus Torvalds 已提交
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
	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))

2243
static void
L
Linus Torvalds 已提交
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
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;

2268
static char *memex_help_string =
L
Linus Torvalds 已提交
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
    "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"
    "";

2283
static char *memex_subcmd_help_string =
L
Linus Torvalds 已提交
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
    "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"
    "";

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 2331 2332 2333 2334 2335 2336 2337 2338
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 已提交
2339
		printf(REG"%c", adrs, brev? 'r': ' ');
L
Linus Torvalds 已提交
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 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 2382 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 2409 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 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
		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;
			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;
	}
}

2455
static int
L
Linus Torvalds 已提交
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469
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;
}

2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
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");
}

2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
static void dump_tracing(void)
{
	int c;

	c = inchar();
	if (c == 'c')
		ftrace_dump(DUMP_ORIG);
	else
		ftrace_dump(DUMP_ALL);
}

2503 2504 2505 2506
#ifdef CONFIG_PPC64
static void dump_one_paca(int cpu)
{
	struct paca_struct *p;
2507
#ifdef CONFIG_PPC_BOOK3S_64
2508 2509
	int i = 0;
#endif
2510 2511 2512 2513 2514 2515 2516 2517 2518

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

	catch_memory_errors = 1;
	sync();

2519
	p = paca_ptrs[cpu];
2520

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

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

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

2531 2532
	DUMP(p, lock_token, "%#-*x");
	DUMP(p, paca_index, "%#-*x");
2533 2534 2535
	DUMP(p, kernel_toc, "%#-*llx");
	DUMP(p, kernelbase, "%#-*llx");
	DUMP(p, kernel_msr, "%#-*llx");
2536
	DUMP(p, emergency_sp, "%-*px");
2537
#ifdef CONFIG_PPC_BOOK3S_64
2538 2539 2540 2541 2542
	DUMP(p, nmi_emergency_sp, "%-*px");
	DUMP(p, mc_emergency_sp, "%-*px");
	DUMP(p, in_nmi, "%#-*x");
	DUMP(p, in_mce, "%#-*x");
	DUMP(p, hmi_event_available, "%#-*x");
2543
#endif
2544
	DUMP(p, data_offset, "%#-*llx");
2545 2546 2547
	DUMP(p, hw_cpu_id, "%#-*x");
	DUMP(p, cpu_start, "%#-*x");
	DUMP(p, kexec_state, "%#-*x");
2548
#ifdef CONFIG_PPC_BOOK3S_64
2549 2550 2551
	if (!early_radix_enabled()) {
		for (i = 0; i < SLB_NUM_BOLTED; i++) {
			u64 esid, vsid;
2552

2553 2554
			if (!p->slb_shadow_ptr)
				continue;
2555

2556 2557
			esid = be64_to_cpu(p->slb_shadow_ptr->save_area[i].esid);
			vsid = be64_to_cpu(p->slb_shadow_ptr->save_area[i].vsid);
2558

2559 2560 2561 2562 2563 2564
			if (esid || vsid) {
				printf(" %-*s[%d] = 0x%016llx 0x%016llx\n",
				       22, "slb_shadow", i, esid, vsid);
			}
		}
		DUMP(p, vmalloc_sllp, "%#-*x");
2565 2566 2567
		DUMP(p, stab_rr, "%#-*x");
		DUMP(p, slb_used_bitmap, "%#-*x");
		DUMP(p, slb_kern_bitmap, "%#-*x");
2568 2569 2570 2571 2572 2573

		if (!early_cpu_has_feature(CPU_FTR_ARCH_300)) {
			DUMP(p, slb_cache_ptr, "%#-*x");
			for (i = 0; i < SLB_CACHE_ENTRIES; i++)
				printf(" %-*s[%d] = 0x%016x\n",
				       22, "slb_cache", i, p->slb_cache[i]);
2574
		}
2575
	}
2576

2577
	DUMP(p, rfi_flush_fallback_area, "%-*px");
2578
#endif
2579
	DUMP(p, dscr_default, "%#-*llx");
2580
#ifdef CONFIG_PPC_BOOK3E
2581 2582 2583 2584 2585 2586
	DUMP(p, pgd, "%-*px");
	DUMP(p, kernel_pgd, "%-*px");
	DUMP(p, tcd_ptr, "%-*px");
	DUMP(p, mc_kstack, "%-*px");
	DUMP(p, crit_kstack, "%-*px");
	DUMP(p, dbg_kstack, "%-*px");
2587
#endif
2588
	DUMP(p, __current, "%-*px");
2589
	DUMP(p, kstack, "%#-*llx");
2590
	printf(" %-*s = 0x%016llx\n", 25, "kstack_base", p->kstack & ~(THREAD_SIZE - 1));
2591 2592 2593
#ifdef CONFIG_STACKPROTECTOR
	DUMP(p, canary, "%#-*lx");
#endif
2594
	DUMP(p, saved_r1, "%#-*llx");
2595
#ifdef CONFIG_PPC_BOOK3E
2596
	DUMP(p, trap_save, "%#-*x");
2597
#endif
2598 2599
	DUMP(p, irq_soft_mask, "%#-*x");
	DUMP(p, irq_happened, "%#-*x");
2600 2601 2602 2603
#ifdef CONFIG_MMIOWB
	DUMP(p, mmiowb_state.nesting_count, "%#-*x");
	DUMP(p, mmiowb_state.mmiowb_pending, "%#-*x");
#endif
2604 2605
	DUMP(p, irq_work_pending, "%#-*x");
	DUMP(p, sprg_vdso, "%#-*llx");
2606 2607

#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2608
	DUMP(p, tm_scratch, "%#-*llx");
2609 2610 2611
#endif

#ifdef CONFIG_PPC_POWERNV
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
	DUMP(p, idle_state, "%#-*lx");
	if (!early_cpu_has_feature(CPU_FTR_ARCH_300)) {
		DUMP(p, thread_idle_state, "%#-*x");
		DUMP(p, subcore_sibling_mask, "%#-*x");
	} else {
#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
		DUMP(p, requested_psscr, "%#-*llx");
		DUMP(p, dont_stop.counter, "%#-*x");
#endif
	}
2622
#endif
2623

2624 2625
	DUMP(p, accounting.utime, "%#-*lx");
	DUMP(p, accounting.stime, "%#-*lx");
2626
#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
2627
	DUMP(p, accounting.utime_scaled, "%#-*lx");
2628
#endif
2629 2630
	DUMP(p, accounting.starttime, "%#-*lx");
	DUMP(p, accounting.starttime_user, "%#-*lx");
2631
#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
2632 2633
	DUMP(p, accounting.startspurr, "%#-*lx");
	DUMP(p, accounting.utime_sspurr, "%#-*lx");
2634
#endif
2635
	DUMP(p, accounting.steal_time, "%#-*lx");
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
#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

2675 2676 2677 2678
#ifdef CONFIG_PPC_POWERNV
static void dump_one_xive(int cpu)
{
	unsigned int hwid = get_hard_smp_processor_id(cpu);
2679
	bool hv = cpu_has_feature(CPU_FTR_HVMODE);
2680

2681 2682 2683 2684 2685 2686 2687 2688
	if (hv) {
		opal_xive_dump(XIVE_DUMP_TM_HYP, hwid);
		opal_xive_dump(XIVE_DUMP_TM_POOL, hwid);
		opal_xive_dump(XIVE_DUMP_TM_OS, hwid);
		opal_xive_dump(XIVE_DUMP_TM_USER, hwid);
		opal_xive_dump(XIVE_DUMP_VP, hwid);
		opal_xive_dump(XIVE_DUMP_EMU_STATE, hwid);
	}
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716

	if (setjmp(bus_error_jmp) != 0) {
		catch_memory_errors = 0;
		printf("*** Error dumping xive on cpu %d\n", cpu);
		return;
	}

	catch_memory_errors = 1;
	sync();
	xmon_xive_do_dump(cpu);
	sync();
	__delay(200);
	catch_memory_errors = 0;
}

static void dump_all_xives(void)
{
	int cpu;

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

	for_each_possible_cpu(cpu)
		dump_one_xive(cpu);
}

2717
static void dump_one_xive_irq(u32 num, struct irq_data *d)
2718
{
2719
	xmon_xive_get_irq_config(num, d);
2720 2721
}

2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
static void dump_all_xive_irq(void)
{
	unsigned int i;
	struct irq_desc *desc;

	for_each_irq_desc(i, desc) {
		struct irq_data *d = irq_desc_get_irq_data(desc);
		unsigned int hwirq;

		if (!d)
			continue;

		hwirq = (unsigned int)irqd_to_hwirq(d);
		/* IPIs are special (HW number 0) */
		if (hwirq)
2737
			dump_one_xive_irq(hwirq, d);
2738 2739 2740
	}
}

2741 2742 2743 2744 2745
static void dump_xives(void)
{
	unsigned long num;
	int c;

2746 2747 2748 2749 2750
	if (!xive_enabled()) {
		printf("Xive disabled on this system\n");
		return;
	}

2751 2752 2753 2754 2755 2756
	c = inchar();
	if (c == 'a') {
		dump_all_xives();
		return;
	} else if (c == 'i') {
		if (scanhex(&num))
2757
			dump_one_xive_irq(num, NULL);
2758 2759
		else
			dump_all_xive_irq();
2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
		return;
	}

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

	if (scanhex(&num))
		dump_one_xive(num);
	else
		dump_one_xive(xmon_owner);
}
#endif /* CONFIG_PPC_POWERNV */

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 2797
static void dump_by_size(unsigned long addr, long count, int size)
{
	unsigned char temp[16];
	int i, j;
	u64 val;

	count = ALIGN(count, 16);

	for (i = 0; i < count; i += 16, addr += 16) {
		printf(REG, addr);

		if (mread(addr, temp, 16) != 16) {
			printf("\nFaulted reading %d bytes from 0x"REG"\n", 16, addr);
			return;
		}

		for (j = 0; j < 16; j += size) {
			putchar(' ');
			switch (size) {
			case 1: val = temp[j]; break;
			case 2: val = *(u16 *)&temp[j]; break;
			case 4: val = *(u32 *)&temp[j]; break;
			case 8: val = *(u64 *)&temp[j]; break;
			default: val = 0;
			}

2798
			printf("%0*llx", size * 2, val);
2799
		}
2800 2801 2802 2803 2804 2805
		printf("  |");
		for (j = 0; j < 16; ++j) {
			val = temp[j];
			putchar(' ' <= val && val <= '~' ? val : '.');
		}
		printf("|\n");
2806 2807 2808
	}
}

2809
static void
L
Linus Torvalds 已提交
2810 2811
dump(void)
{
2812
	static char last[] = { "d?\n" };
L
Linus Torvalds 已提交
2813 2814 2815
	int c;

	c = inchar();
2816 2817 2818

#ifdef CONFIG_PPC64
	if (c == 'p') {
2819
		xmon_start_pagination();
2820
		dump_pacas();
2821
		xmon_end_pagination();
2822 2823 2824
		return;
	}
#endif
2825 2826 2827 2828 2829 2830 2831 2832
#ifdef CONFIG_PPC_POWERNV
	if (c == 'x') {
		xmon_start_pagination();
		dump_xives();
		xmon_end_pagination();
		return;
	}
#endif
2833

2834 2835 2836 2837 2838
	if (c == 't') {
		dump_tracing();
		return;
	}

2839
	if (c == '\n')
L
Linus Torvalds 已提交
2840
		termch = c;
2841

L
Linus Torvalds 已提交
2842 2843 2844 2845 2846 2847 2848
	scanhex((void *)&adrs);
	if (termch != '\n')
		termch = 0;
	if (c == 'i') {
		scanhex(&nidump);
		if (nidump == 0)
			nidump = 16;
2849 2850
		else if (nidump > MAX_IDUMP)
			nidump = MAX_IDUMP;
L
Linus Torvalds 已提交
2851 2852
		adrs += ppc_inst_dump(adrs, nidump, 1);
		last_cmd = "di\n";
2853 2854
	} else if (c == 'l') {
		dump_log_buf();
2855 2856
	} else if (c == 'o') {
		dump_opal_msglog();
2857 2858 2859
	} else if (c == 'v') {
		/* dump virtual to physical translation */
		show_pte(adrs);
2860 2861 2862 2863 2864 2865 2866
	} else if (c == 'r') {
		scanhex(&ndump);
		if (ndump == 0)
			ndump = 64;
		xmon_rawdump(adrs, ndump);
		adrs += ndump;
		last_cmd = "dr\n";
L
Linus Torvalds 已提交
2867 2868 2869 2870 2871 2872
	} else {
		scanhex(&ndump);
		if (ndump == 0)
			ndump = 64;
		else if (ndump > MAX_DUMP)
			ndump = MAX_DUMP;
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888

		switch (c) {
		case '8':
		case '4':
		case '2':
		case '1':
			ndump = ALIGN(ndump, 16);
			dump_by_size(adrs, ndump, c - '0');
			last[1] = c;
			last_cmd = last;
			break;
		default:
			prdump(adrs, ndump);
			last_cmd = "d\n";
		}

L
Linus Torvalds 已提交
2889 2890 2891 2892
		adrs += ndump;
	}
}

2893
static void
L
Linus Torvalds 已提交
2894 2895 2896 2897 2898 2899
prdump(unsigned long adrs, long ndump)
{
	long n, m, c, r, nr;
	unsigned char temp[16];

	for (n = ndump; n > 0;) {
P
Paul Mackerras 已提交
2900
		printf(REG, adrs);
L
Linus Torvalds 已提交
2901 2902 2903 2904 2905
		putchar(' ');
		r = n < 16? n: 16;
		nr = mread(adrs, temp, r);
		adrs += nr;
		for (m = 0; m < r; ++m) {
2906
			if ((m & (sizeof(long) - 1)) == 0 && m > 0)
P
Paul Mackerras 已提交
2907
				putchar(' ');
L
Linus Torvalds 已提交
2908 2909 2910 2911 2912
			if (m < nr)
				printf("%.2x", temp[m]);
			else
				printf("%s", fault_chars[fault_type]);
		}
P
Paul Mackerras 已提交
2913
		for (; m < 16; ++m) {
2914
			if ((m & (sizeof(long) - 1)) == 0)
P
Paul Mackerras 已提交
2915
				putchar(' ');
L
Linus Torvalds 已提交
2916
			printf("  ");
P
Paul Mackerras 已提交
2917
		}
L
Linus Torvalds 已提交
2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
		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;
	}
}

2935 2936
typedef int (*instruction_dump_func)(unsigned long inst, unsigned long addr);

2937
static int
2938 2939
generic_inst_dump(unsigned long adr, long count, int praddr,
			instruction_dump_func dump_func)
L
Linus Torvalds 已提交
2940 2941 2942
{
	int nr, dotted;
	unsigned long first_adr;
2943
	struct ppc_inst inst, last_inst = ppc_inst(0);
L
Linus Torvalds 已提交
2944 2945 2946 2947 2948 2949 2950 2951
	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 已提交
2952
				printf(REG"  %s%s%s%s\n", adr, x, x, x, x);
L
Linus Torvalds 已提交
2953 2954 2955
			}
			break;
		}
2956
		inst = ppc_inst(GETWORD(val));
2957
		if (adr > first_adr && ppc_inst_equal(inst, last_inst)) {
L
Linus Torvalds 已提交
2958 2959 2960 2961 2962 2963 2964 2965 2966
			if (!dotted) {
				printf(" ...\n");
				dotted = 1;
			}
			continue;
		}
		dotted = 0;
		last_inst = inst;
		if (praddr)
2967
			printf(REG"  %.8x", adr, ppc_inst_val(inst));
L
Linus Torvalds 已提交
2968
		printf("\t");
2969
		dump_func(ppc_inst_val(inst), adr);
L
Linus Torvalds 已提交
2970 2971 2972 2973 2974
		printf("\n");
	}
	return adr - first_adr;
}

2975
static int
2976 2977 2978 2979 2980
ppc_inst_dump(unsigned long adr, long count, int praddr)
{
	return generic_inst_dump(adr, count, praddr, print_insn_powerpc);
}

L
Linus Torvalds 已提交
2981 2982 2983 2984 2985 2986
void
print_address(unsigned long addr)
{
	xmon_print_symbol(addr, "\t# ", "");
}

2987
static void
2988 2989
dump_log_buf(void)
{
2990 2991 2992
	struct kmsg_dumper dumper = { .active = 1 };
	unsigned char buf[128];
	size_t len;
2993

2994
	if (setjmp(bus_error_jmp) != 0) {
2995
		printf("Error dumping printk buffer!\n");
2996 2997
		return;
	}
2998

2999 3000
	catch_memory_errors = 1;
	sync();
3001

3002
	kmsg_dump_rewind_nolock(&dumper);
3003
	xmon_start_pagination();
3004 3005 3006 3007
	while (kmsg_dump_get_line_nolock(&dumper, false, buf, sizeof(buf), &len)) {
		buf[len] = '\0';
		printf("%s", buf);
	}
3008
	xmon_end_pagination();
3009

3010 3011 3012 3013
	sync();
	/* wait a little while to see if we get a machine check */
	__delay(200);
	catch_memory_errors = 0;
3014
}
L
Linus Torvalds 已提交
3015

3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054
#ifdef CONFIG_PPC_POWERNV
static void dump_opal_msglog(void)
{
	unsigned char buf[128];
	ssize_t res;
	loff_t pos = 0;

	if (!firmware_has_feature(FW_FEATURE_OPAL)) {
		printf("Machine is not running OPAL firmware.\n");
		return;
	}

	if (setjmp(bus_error_jmp) != 0) {
		printf("Error dumping OPAL msglog!\n");
		return;
	}

	catch_memory_errors = 1;
	sync();

	xmon_start_pagination();
	while ((res = opal_msglog_copy(buf, pos, sizeof(buf) - 1))) {
		if (res < 0) {
			printf("Error dumping OPAL msglog! Error: %zd\n", res);
			break;
		}
		buf[res] = '\0';
		printf("%s", buf);
		pos += res;
	}
	xmon_end_pagination();

	sync();
	/* wait a little while to see if we get a machine check */
	__delay(200);
	catch_memory_errors = 0;
}
#endif

L
Linus Torvalds 已提交
3055 3056 3057 3058 3059 3060 3061 3062 3063
/*
 * 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 */

3064
static void
L
Linus Torvalds 已提交
3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075
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':
3076 3077 3078 3079
		if (xmon_is_ro) {
			printf(xmon_ro_msg);
			break;
		}
L
Linus Torvalds 已提交
3080 3081 3082
		memmove((void *)mdest, (void *)msrc, mcount);
		break;
	case 's':
3083 3084 3085 3086
		if (xmon_is_ro) {
			printf(xmon_ro_msg);
			break;
		}
L
Linus Torvalds 已提交
3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
		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;
	}
}

3098
static void
L
Linus Torvalds 已提交
3099 3100 3101 3102 3103 3104 3105 3106
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 )
3107
				printf("%px %.2x # %px %.2x\n", p1 - 1,
L
Linus Torvalds 已提交
3108 3109 3110 3111 3112 3113 3114 3115
					p1[-1], p2 - 1, p2[-1]);
	if( prt > maxpr )
		printf("Total of %d differences\n", prt);
}

static unsigned mend;
static unsigned mask;

3116
static void
L
Linus Torvalds 已提交
3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148
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;

3149
static void
L
Linus Torvalds 已提交
3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
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)
3167
			printf("%.8lx\n", a - mskip);
L
Linus Torvalds 已提交
3168 3169 3170 3171 3172
		ook = ok;
		if (a + mskip < a)
			break;
	}
	if (ook)
3173
		printf("%.8lx\n", a - mskip);
L
Linus Torvalds 已提交
3174 3175
}

3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193
static void show_task(struct task_struct *tsk)
{
	char state;

	/*
	 * Cloned from kdb_task_state_char(), which is not entirely
	 * appropriate for calling from xmon. This could be moved
	 * to a common, generic, routine used by both.
	 */
	state = (tsk->state == 0) ? 'R' :
		(tsk->state < 0) ? 'U' :
		(tsk->state & TASK_UNINTERRUPTIBLE) ? 'D' :
		(tsk->state & TASK_STOPPED) ? 'T' :
		(tsk->state & TASK_TRACED) ? 'C' :
		(tsk->exit_state & EXIT_ZOMBIE) ? 'Z' :
		(tsk->exit_state & EXIT_DEAD) ? 'E' :
		(tsk->state & TASK_INTERRUPTIBLE) ? 'S' : '?';

3194 3195
	printf("%16px %16lx %16px %6d %6d %c %2d %s\n", tsk,
		tsk->thread.ksp, tsk->thread.regs,
3196
		tsk->pid, rcu_dereference(tsk->parent)->pid,
3197
		state, task_cpu(tsk),
3198 3199 3200
		tsk->comm);
}

3201
#ifdef CONFIG_PPC_BOOK3S_64
3202
static void format_pte(void *ptep, unsigned long pte)
3203
{
3204 3205
	pte_t entry = __pte(pte);

3206 3207 3208 3209
	printf("ptep @ 0x%016lx = 0x%016lx\n", (unsigned long)ptep, pte);
	printf("Maps physical address = 0x%016lx\n", pte & PTE_RPN_MASK);

	printf("Flags = %s%s%s%s%s\n",
3210 3211 3212 3213 3214
	       pte_young(entry) ? "Accessed " : "",
	       pte_dirty(entry) ? "Dirty " : "",
	       pte_read(entry)  ? "Read " : "",
	       pte_write(entry) ? "Write " : "",
	       pte_exec(entry)  ? "Exec " : "");
3215 3216 3217 3218 3219 3220 3221
}

static void show_pte(unsigned long addr)
{
	unsigned long tskv = 0;
	struct task_struct *tsk = NULL;
	struct mm_struct *mm;
3222 3223
	pgd_t *pgdp;
	p4d_t *p4dp;
3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239
	pud_t *pudp;
	pmd_t *pmdp;
	pte_t *ptep;

	if (!scanhex(&tskv))
		mm = &init_mm;
	else
		tsk = (struct task_struct *)tskv;

	if (tsk == NULL)
		mm = &init_mm;
	else
		mm = tsk->active_mm;

	if (setjmp(bus_error_jmp) != 0) {
		catch_memory_errors = 0;
3240
		printf("*** Error dumping pte for task %px\n", tsk);
3241 3242 3243 3244 3245 3246
		return;
	}

	catch_memory_errors = 1;
	sync();

3247
	if (mm == &init_mm)
3248
		pgdp = pgd_offset_k(addr);
3249
	else
3250 3251
		pgdp = pgd_offset(mm, addr);

3252 3253 3254 3255
	p4dp = p4d_offset(pgdp, addr);

	if (p4d_none(*p4dp)) {
		printf("No valid P4D\n");
3256 3257 3258
		return;
	}

3259 3260
	if (p4d_is_leaf(*p4dp)) {
		format_pte(p4dp, p4d_val(*p4dp));
3261 3262 3263
		return;
	}

3264 3265 3266
	printf("p4dp @ 0x%px = 0x%016lx\n", p4dp, p4d_val(*p4dp));

	pudp = pud_offset(p4dp, addr);
3267 3268 3269 3270 3271 3272

	if (pud_none(*pudp)) {
		printf("No valid PUD\n");
		return;
	}

3273
	if (pud_is_leaf(*pudp)) {
3274 3275 3276 3277
		format_pte(pudp, pud_val(*pudp));
		return;
	}

3278
	printf("pudp @ 0x%px = 0x%016lx\n", pudp, pud_val(*pudp));
3279 3280 3281 3282 3283 3284 3285 3286

	pmdp = pmd_offset(pudp, addr);

	if (pmd_none(*pmdp)) {
		printf("No valid PMD\n");
		return;
	}

3287
	if (pmd_is_leaf(*pmdp)) {
3288 3289 3290
		format_pte(pmdp, pmd_val(*pmdp));
		return;
	}
3291
	printf("pmdp @ 0x%px = 0x%016lx\n", pmdp, pmd_val(*pmdp));
3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311

	ptep = pte_offset_map(pmdp, addr);
	if (pte_none(*ptep)) {
		printf("no valid PTE\n");
		return;
	}

	format_pte(ptep, pte_val(*ptep));

	sync();
	__delay(200);
	catch_memory_errors = 0;
}
#else
static void show_pte(unsigned long addr)
{
	printf("show_pte not yet implemented\n");
}
#endif /* CONFIG_PPC_BOOK3S_64 */

3312 3313 3314 3315 3316
static void show_tasks(void)
{
	unsigned long tskv;
	struct task_struct *tsk = NULL;

3317
	printf("     task_struct     ->thread.ksp    ->thread.regs    PID   PPID S  P CMD\n");
3318 3319 3320 3321 3322 3323

	if (scanhex(&tskv))
		tsk = (struct task_struct *)tskv;

	if (setjmp(bus_error_jmp) != 0) {
		catch_memory_errors = 0;
3324
		printf("*** Error dumping task %px\n", tsk);
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341
		return;
	}

	catch_memory_errors = 1;
	sync();

	if (tsk)
		show_task(tsk);
	else
		for_each_process(tsk)
			show_task(tsk);

	sync();
	__delay(200);
	catch_memory_errors = 0;
}

3342
static void proccall(void)
P
Paul Mackerras 已提交
3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370
{
	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();
3371
		printf("return value is 0x%lx\n", ret);
P
Paul Mackerras 已提交
3372 3373 3374 3375 3376 3377
	} else {
		printf("*** %x exception occurred\n", fault_except);
	}
	catch_memory_errors = 0;
}

L
Linus Torvalds 已提交
3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394
/* 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
3395
static const char *regnames[N_PTREGS] = {
L
Linus Torvalds 已提交
3396 3397 3398 3399
	"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 已提交
3400 3401 3402 3403 3404 3405
	"pc", "msr", "or3", "ctr", "lr", "xer", "ccr",
#ifdef CONFIG_PPC64
	"softe",
#else
	"mq",
#endif
L
Linus Torvalds 已提交
3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429
	"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;
3430 3431 3432 3433
		i = match_string(regnames, N_PTREGS, regname);
		if (i < 0) {
			printf("invalid register name '%%%s'\n", regname);
			return 0;
L
Linus Torvalds 已提交
3434
		}
3435 3436 3437 3438 3439 3440
		if (xmon_regs == NULL) {
			printf("regs not available\n");
			return 0;
		}
		*vp = ((unsigned long *)xmon_regs)[i];
		return 1;
L
Linus Torvalds 已提交
3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460
	}

	/* 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();
3461
			if (isspace(c) || c == '\0') {
L
Linus Torvalds 已提交
3462 3463 3464 3465 3466 3467
				termch = c;
				break;
			}
			tmpstr[i] = c;
		}
		tmpstr[i++] = 0;
3468 3469 3470 3471 3472 3473 3474 3475
		*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 已提交
3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498
		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;
}

3499
static void
L
Linus Torvalds 已提交
3500 3501 3502 3503 3504 3505 3506 3507 3508 3509
scannl(void)
{
	int c;

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

3510
static int hexdigit(int c)
L
Linus Torvalds 已提交
3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526
{
	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();
3527 3528 3529 3530 3531
	if (c == '\n') {
		*s = 0;
		return;
	}

L
Linus Torvalds 已提交
3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545
	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;

3546
static void
L
Linus Torvalds 已提交
3547 3548 3549 3550 3551
flush_input(void)
{
	lineptr = NULL;
}

3552
static int
L
Linus Torvalds 已提交
3553 3554 3555
inchar(void)
{
	if (lineptr == NULL || *lineptr == 0) {
3556
		if (xmon_gets(line, sizeof(line)) == NULL) {
L
Linus Torvalds 已提交
3557 3558 3559 3560 3561 3562 3563 3564
			lineptr = NULL;
			return EOF;
		}
		lineptr = line;
	}
	return *lineptr++;
}

3565
static void
L
Linus Torvalds 已提交
3566 3567 3568 3569 3570 3571 3572 3573 3574 3575
take_input(char *str)
{
	lineptr = str;
}


static void
symbol_lookup(void)
{
	int type = inchar();
3576 3577
	unsigned long addr, cpu;
	void __percpu *ptr = NULL;
L
Linus Torvalds 已提交
3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597
	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();
		}
3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625
		catch_memory_errors = 0;
		termch = 0;
		break;
	case 'p':
		getstring(tmp, 64);
		if (setjmp(bus_error_jmp) == 0) {
			catch_memory_errors = 1;
			sync();
			ptr = (void __percpu *)kallsyms_lookup_name(tmp);
			sync();
		}

		if (ptr &&
		    ptr >= (void __percpu *)__per_cpu_start &&
		    ptr < (void __percpu *)__per_cpu_end)
		{
			if (scanhex(&cpu) && cpu < num_possible_cpus()) {
				addr = (unsigned long)per_cpu_ptr(ptr, cpu);
			} else {
				cpu = raw_smp_processor_id();
				addr = (unsigned long)this_cpu_ptr(ptr);
			}

			printf("%s for cpu 0x%lx: %lx\n", tmp, cpu, addr);
		} else {
			printf("Percpu symbol '%s' not found.\n", tmp);
		}

L
Linus Torvalds 已提交
3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640
		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 已提交
3641
	printf(REG, address);
L
Linus Torvalds 已提交
3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
	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);
}

3662
#ifdef CONFIG_PPC_BOOK3S_64
M
Michael Ellerman 已提交
3663
void dump_segments(void)
L
Linus Torvalds 已提交
3664 3665
{
	int i;
3666
	unsigned long esid,vsid;
W
will schmidt 已提交
3667
	unsigned long llp;
L
Linus Torvalds 已提交
3668

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

3671
	for (i = 0; i < mmu_slb_size; i++) {
W
will schmidt 已提交
3672 3673
		asm volatile("slbmfee  %0,%1" : "=r" (esid) : "r" (i));
		asm volatile("slbmfev  %0,%1" : "=r" (vsid) : "r" (i));
3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695

		if (!esid && !vsid)
			continue;

		printf("%02d %016lx %016lx", i, esid, vsid);

		if (!(esid & SLB_ESID_V)) {
			printf("\n");
			continue;
		}

		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);
W
will schmidt 已提交
3696
		}
L
Linus Torvalds 已提交
3697 3698
	}
}
P
Paul Mackerras 已提交
3699 3700
#endif

3701
#ifdef CONFIG_PPC_BOOK3S_32
P
Paul Mackerras 已提交
3702 3703 3704 3705 3706 3707
void dump_segments(void)
{
	int i;

	printf("sr0-15 =");
	for (i = 0; i < 16; ++i)
3708
		printf(" %x", mfsrin(i << 28));
P
Paul Mackerras 已提交
3709 3710 3711 3712
	printf("\n");
}
#endif

3713 3714 3715 3716 3717 3718 3719 3720 3721 3722
#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));
3723
		printf("[%02x] %08lx %08lx %08lx ", i, w0, w1, w2);
3724
		if (w0 & PPC44x_TLB_VALID) {
3725
			printf("V %08lx -> %01lx%08lx %c%c%c%c%c",
3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738
			       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 */
3739

3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
#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 */

3884
static void xmon_init(int enable)
O
Olaf Hering 已提交
3885 3886 3887 3888 3889 3890 3891
{
	if (enable) {
		__debugger = xmon;
		__debugger_ipi = xmon_ipi;
		__debugger_bpt = xmon_bpt;
		__debugger_sstep = xmon_sstep;
		__debugger_iabr_match = xmon_iabr_match;
3892
		__debugger_break_match = xmon_break_match;
O
Olaf Hering 已提交
3893
		__debugger_fault_handler = xmon_fault_handler;
3894 3895 3896 3897 3898 3899 3900 3901

#ifdef CONFIG_PPC_PSERIES
		/*
		 * Get the token here to avoid trying to get a lock
		 * during the crash, causing a deadlock.
		 */
		set_indicator_token = rtas_token("set-indicator");
#endif
O
Olaf Hering 已提交
3902 3903 3904 3905 3906 3907
	} else {
		__debugger = NULL;
		__debugger_ipi = NULL;
		__debugger_bpt = NULL;
		__debugger_sstep = NULL;
		__debugger_iabr_match = NULL;
3908
		__debugger_break_match = NULL;
O
Olaf Hering 已提交
3909 3910
		__debugger_fault_handler = NULL;
	}
L
Linus Torvalds 已提交
3911
}
3912 3913

#ifdef CONFIG_MAGIC_SYSRQ
3914
static void sysrq_handle_xmon(int key)
3915
{
3916 3917 3918 3919 3920
	if (xmon_is_locked_down()) {
		clear_all_bpt();
		xmon_init(0);
		return;
	}
3921 3922
	/* ensure xmon is enabled */
	xmon_init(1);
3923
	debugger(get_irq_regs());
3924 3925
	if (!xmon_on)
		xmon_init(0);
3926 3927
}

3928
static const struct sysrq_key_op sysrq_xmon_op = {
3929
	.handler =	sysrq_handle_xmon,
3930
	.help_msg =	"xmon(x)",
3931 3932 3933 3934 3935 3936 3937 3938
	.action_msg =	"Entering xmon",
};

static int __init setup_xmon_sysrq(void)
{
	register_sysrq_key('x', &sysrq_xmon_op);
	return 0;
}
3939
device_initcall(setup_xmon_sysrq);
3940
#endif /* CONFIG_MAGIC_SYSRQ */
3941

3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954
static void clear_all_bpt(void)
{
	int i;

	/* clear/unpatch all breakpoints */
	remove_bpts();
	remove_cpu_bpts();

	/* Disable all breakpoints */
	for (i = 0; i < NBPTS; ++i)
		bpts[i].enabled = 0;

	/* Clear any data or iabr breakpoints */
3955 3956 3957
	iabr = NULL;
	for (i = 0; i < nr_wp_slots(); i++)
		dabr[i].enabled = 0;
3958 3959
}

3960
#ifdef CONFIG_DEBUG_FS
3961 3962 3963 3964 3965
static int xmon_dbgfs_set(void *data, u64 val)
{
	xmon_on = !!val;
	xmon_init(xmon_on);

3966
	/* make sure all breakpoints removed when disabling */
3967
	if (!xmon_on) {
3968
		clear_all_bpt();
3969 3970 3971 3972 3973
		get_output_lock();
		printf("xmon: All breakpoints cleared\n");
		release_output_lock();
	}

3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994
	return 0;
}

static int xmon_dbgfs_get(void *data, u64 *val)
{
	*val = xmon_on;
	return 0;
}

DEFINE_SIMPLE_ATTRIBUTE(xmon_dbgfs_ops, xmon_dbgfs_get,
			xmon_dbgfs_set, "%llu\n");

static int __init setup_xmon_dbgfs(void)
{
	debugfs_create_file("xmon", 0600, powerpc_debugfs_root, NULL,
				&xmon_dbgfs_ops);
	return 0;
}
device_initcall(setup_xmon_dbgfs);
#endif /* CONFIG_DEBUG_FS */

3995
static int xmon_early __initdata;
3996 3997 3998

static int __init early_parse_xmon(char *p)
{
3999 4000 4001 4002 4003
	if (xmon_is_locked_down()) {
		xmon_init(0);
		xmon_early = 0;
		xmon_on = 0;
	} else if (!p || strncmp(p, "early", 5) == 0) {
4004 4005 4006
		/* just "xmon" is equivalent to "xmon=early" */
		xmon_init(1);
		xmon_early = 1;
4007 4008
		xmon_on = 1;
	} else if (strncmp(p, "on", 2) == 0) {
4009
		xmon_init(1);
4010
		xmon_on = 1;
4011 4012 4013 4014 4015 4016 4017 4018
	} else if (strncmp(p, "rw", 2) == 0) {
		xmon_init(1);
		xmon_on = 1;
		xmon_is_ro = false;
	} else if (strncmp(p, "ro", 2) == 0) {
		xmon_init(1);
		xmon_on = 1;
		xmon_is_ro = true;
4019 4020
	} else if (strncmp(p, "off", 3) == 0)
		xmon_on = 0;
4021 4022 4023 4024 4025 4026 4027 4028 4029
	else
		return 1;

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

void __init xmon_setup(void)
{
4030
	if (xmon_on)
4031 4032 4033 4034
		xmon_init(1);
	if (xmon_early)
		debugger(NULL);
}
4035

4036
#ifdef CONFIG_SPU_BASE
4037 4038 4039 4040 4041

struct spu_info {
	struct spu *spu;
	u64 saved_mfc_sr1_RW;
	u32 saved_spu_runcntl_RW;
4042
	unsigned long dump_addr;
4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061
	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;
4062 4063
		spu_info[spu->number].dump_addr = (unsigned long)
				spu_info[spu->number].spu->local_store;
4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095
	}
}

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;
4096 4097 4098 4099

			printf("Stopped spu %.2d (was %s)\n", i,
					spu_info[i].saved_spu_runcntl_RW ?
					"running" : "stopped");
4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143
		} 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;
	}
}

4144
#define DUMP_WIDTH	23
4145
#define DUMP_VALUE(format, field, value)				\
4146 4147 4148 4149 4150
do {									\
	if (setjmp(bus_error_jmp) == 0) {				\
		catch_memory_errors = 1;				\
		sync();							\
		printf("  %-*s = "format"\n", DUMP_WIDTH,		\
4151
				#field, value);				\
4152 4153 4154 4155 4156 4157 4158 4159 4160 4161
		sync();							\
		__delay(200);						\
	} else {							\
		catch_memory_errors = 0;				\
		printf("  %-*s = *** Error reading field.\n",		\
					DUMP_WIDTH, #field);		\
	}								\
	catch_memory_errors = 0;					\
} while (0)

4162 4163 4164
#define DUMP_FIELD(obj, format, field)	\
	DUMP_VALUE(format, field, obj->field)

4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175
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);
4176 4177 4178 4179 4180 4181 4182
	DUMP_FIELD(spu, "%llu", class_0_pending);
	DUMP_FIELD(spu, "0x%llx", class_0_dar);
	DUMP_FIELD(spu, "0x%llx", class_1_dar);
	DUMP_FIELD(spu, "0x%llx", class_1_dsisr);
	DUMP_FIELD(spu, "0x%x", irqs[0]);
	DUMP_FIELD(spu, "0x%x", irqs[1]);
	DUMP_FIELD(spu, "0x%x", irqs[2]);
4183 4184 4185 4186 4187
	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);
4188
	DUMP_FIELD(spu, "0x%llx", timestamp);
4189 4190
	DUMP_FIELD(spu, "0x%lx", problem_phys);
	DUMP_FIELD(spu, "0x%p", problem);
4191 4192 4193 4194 4195 4196
	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));
4197
	DUMP_FIELD(spu, "0x%p", priv2);
4198
	DUMP_FIELD(spu, "0x%p", pdata);
4199 4200
}

4201 4202 4203 4204 4205 4206 4207
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)
4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218
{
	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;
4219
		printf("*** Error: accessing spu info for spu %ld\n", num);
4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233
		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;
	}

4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244
	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;
	}
4245 4246 4247 4248

	spu_info[num].dump_addr = addr;
}

4249 4250
static int do_spu_cmd(void)
{
4251
	static unsigned long num = 0;
4252
	int cmd, subcmd = 0;
4253 4254 4255 4256 4257 4258 4259 4260 4261

	cmd = inchar();
	switch (cmd) {
	case 's':
		stop_spus();
		break;
	case 'r':
		restart_spus();
		break;
4262
	case 'd':
4263 4264 4265
		subcmd = inchar();
		if (isxdigit(subcmd) || subcmd == '\n')
			termch = subcmd;
4266
		/* fall through */
4267
	case 'f':
4268 4269
		scanhex(&num);
		if (num >= XMON_NUM_SPUS || !spu_info[num].spu) {
4270
			printf("*** Error: invalid spu number\n");
4271 4272 4273 4274 4275 4276 4277 4278
			return 0;
		}

		switch (cmd) {
		case 'f':
			dump_spu_fields(spu_info[num].spu);
			break;
		default:
4279
			dump_spu_ls(num, subcmd);
4280 4281 4282
			break;
		}

4283
		break;
4284 4285 4286 4287 4288 4289
	default:
		return -1;
	}

	return 0;
}
4290
#else /* ! CONFIG_SPU_BASE */
4291 4292 4293 4294 4295
static int do_spu_cmd(void)
{
	return -1;
}
#endif