18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  Originally written by Glenn Engel, Lake Stevens Instrument Division
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *  Contributed by HP Systems
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
78c2ecf20Sopenharmony_ci *  Send complaints, suggestions etc. to <andy@waldorf-gmbh.de>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *  Copyright (C) 1995 Andreas Busse
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *  Copyright (C) 2003 MontaVista Software Inc.
128c2ecf20Sopenharmony_ci *  Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *  Copyright (C) 2004-2005 MontaVista Software Inc.
158c2ecf20Sopenharmony_ci *  Author: Manish Lachwani, mlachwani@mvista.com or manish@koffee-break.com
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci *  Copyright (C) 2007-2008 Wind River Systems, Inc.
188c2ecf20Sopenharmony_ci *  Author/Maintainer: Jason Wessel, jason.wessel@windriver.com
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *  This file is licensed under the terms of the GNU General Public License
218c2ecf20Sopenharmony_ci *  version 2. This program is licensed "as is" without any warranty of any
228c2ecf20Sopenharmony_ci *  kind, whether express or implied.
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/ptrace.h>		/* for linux pt_regs struct */
268c2ecf20Sopenharmony_ci#include <linux/kgdb.h>
278c2ecf20Sopenharmony_ci#include <linux/kdebug.h>
288c2ecf20Sopenharmony_ci#include <linux/sched.h>
298c2ecf20Sopenharmony_ci#include <linux/smp.h>
308c2ecf20Sopenharmony_ci#include <asm/inst.h>
318c2ecf20Sopenharmony_ci#include <asm/fpu.h>
328c2ecf20Sopenharmony_ci#include <asm/cacheflush.h>
338c2ecf20Sopenharmony_ci#include <asm/processor.h>
348c2ecf20Sopenharmony_ci#include <asm/sigcontext.h>
358c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
368c2ecf20Sopenharmony_ci#include <asm/irq_regs.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic struct hard_trap_info {
398c2ecf20Sopenharmony_ci	unsigned char tt;	/* Trap type code for MIPS R3xxx and R4xxx */
408c2ecf20Sopenharmony_ci	unsigned char signo;	/* Signal that we map this trap into */
418c2ecf20Sopenharmony_ci} hard_trap_info[] = {
428c2ecf20Sopenharmony_ci	{ 6, SIGBUS },		/* instruction bus error */
438c2ecf20Sopenharmony_ci	{ 7, SIGBUS },		/* data bus error */
448c2ecf20Sopenharmony_ci	{ 9, SIGTRAP },		/* break */
458c2ecf20Sopenharmony_ci/*	{ 11, SIGILL }, */	/* CPU unusable */
468c2ecf20Sopenharmony_ci	{ 12, SIGFPE },		/* overflow */
478c2ecf20Sopenharmony_ci	{ 13, SIGTRAP },	/* trap */
488c2ecf20Sopenharmony_ci	{ 14, SIGSEGV },	/* virtual instruction cache coherency */
498c2ecf20Sopenharmony_ci	{ 15, SIGFPE },		/* floating point exception */
508c2ecf20Sopenharmony_ci	{ 23, SIGSEGV },	/* watch */
518c2ecf20Sopenharmony_ci	{ 31, SIGSEGV },	/* virtual data cache coherency */
528c2ecf20Sopenharmony_ci	{ 0, 0}			/* Must be last */
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistruct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	{ "zero", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[0]) },
588c2ecf20Sopenharmony_ci	{ "at", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[1]) },
598c2ecf20Sopenharmony_ci	{ "v0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[2]) },
608c2ecf20Sopenharmony_ci	{ "v1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[3]) },
618c2ecf20Sopenharmony_ci	{ "a0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[4]) },
628c2ecf20Sopenharmony_ci	{ "a1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[5]) },
638c2ecf20Sopenharmony_ci	{ "a2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[6]) },
648c2ecf20Sopenharmony_ci	{ "a3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[7]) },
658c2ecf20Sopenharmony_ci	{ "t0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[8]) },
668c2ecf20Sopenharmony_ci	{ "t1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[9]) },
678c2ecf20Sopenharmony_ci	{ "t2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[10]) },
688c2ecf20Sopenharmony_ci	{ "t3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[11]) },
698c2ecf20Sopenharmony_ci	{ "t4", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[12]) },
708c2ecf20Sopenharmony_ci	{ "t5", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[13]) },
718c2ecf20Sopenharmony_ci	{ "t6", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[14]) },
728c2ecf20Sopenharmony_ci	{ "t7", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[15]) },
738c2ecf20Sopenharmony_ci	{ "s0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[16]) },
748c2ecf20Sopenharmony_ci	{ "s1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[17]) },
758c2ecf20Sopenharmony_ci	{ "s2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[18]) },
768c2ecf20Sopenharmony_ci	{ "s3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[19]) },
778c2ecf20Sopenharmony_ci	{ "s4", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[20]) },
788c2ecf20Sopenharmony_ci	{ "s5", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[21]) },
798c2ecf20Sopenharmony_ci	{ "s6", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[22]) },
808c2ecf20Sopenharmony_ci	{ "s7", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[23]) },
818c2ecf20Sopenharmony_ci	{ "t8", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[24]) },
828c2ecf20Sopenharmony_ci	{ "t9", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[25]) },
838c2ecf20Sopenharmony_ci	{ "k0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[26]) },
848c2ecf20Sopenharmony_ci	{ "k1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[27]) },
858c2ecf20Sopenharmony_ci	{ "gp", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[28]) },
868c2ecf20Sopenharmony_ci	{ "sp", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[29]) },
878c2ecf20Sopenharmony_ci	{ "s8", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[30]) },
888c2ecf20Sopenharmony_ci	{ "ra", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[31]) },
898c2ecf20Sopenharmony_ci	{ "sr", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_status) },
908c2ecf20Sopenharmony_ci	{ "lo", GDB_SIZEOF_REG, offsetof(struct pt_regs, lo) },
918c2ecf20Sopenharmony_ci	{ "hi", GDB_SIZEOF_REG, offsetof(struct pt_regs, hi) },
928c2ecf20Sopenharmony_ci	{ "bad", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_badvaddr) },
938c2ecf20Sopenharmony_ci	{ "cause", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_cause) },
948c2ecf20Sopenharmony_ci	{ "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, cp0_epc) },
958c2ecf20Sopenharmony_ci	{ "f0", GDB_SIZEOF_REG, 0 },
968c2ecf20Sopenharmony_ci	{ "f1", GDB_SIZEOF_REG, 1 },
978c2ecf20Sopenharmony_ci	{ "f2", GDB_SIZEOF_REG, 2 },
988c2ecf20Sopenharmony_ci	{ "f3", GDB_SIZEOF_REG, 3 },
998c2ecf20Sopenharmony_ci	{ "f4", GDB_SIZEOF_REG, 4 },
1008c2ecf20Sopenharmony_ci	{ "f5", GDB_SIZEOF_REG, 5 },
1018c2ecf20Sopenharmony_ci	{ "f6", GDB_SIZEOF_REG, 6 },
1028c2ecf20Sopenharmony_ci	{ "f7", GDB_SIZEOF_REG, 7 },
1038c2ecf20Sopenharmony_ci	{ "f8", GDB_SIZEOF_REG, 8 },
1048c2ecf20Sopenharmony_ci	{ "f9", GDB_SIZEOF_REG, 9 },
1058c2ecf20Sopenharmony_ci	{ "f10", GDB_SIZEOF_REG, 10 },
1068c2ecf20Sopenharmony_ci	{ "f11", GDB_SIZEOF_REG, 11 },
1078c2ecf20Sopenharmony_ci	{ "f12", GDB_SIZEOF_REG, 12 },
1088c2ecf20Sopenharmony_ci	{ "f13", GDB_SIZEOF_REG, 13 },
1098c2ecf20Sopenharmony_ci	{ "f14", GDB_SIZEOF_REG, 14 },
1108c2ecf20Sopenharmony_ci	{ "f15", GDB_SIZEOF_REG, 15 },
1118c2ecf20Sopenharmony_ci	{ "f16", GDB_SIZEOF_REG, 16 },
1128c2ecf20Sopenharmony_ci	{ "f17", GDB_SIZEOF_REG, 17 },
1138c2ecf20Sopenharmony_ci	{ "f18", GDB_SIZEOF_REG, 18 },
1148c2ecf20Sopenharmony_ci	{ "f19", GDB_SIZEOF_REG, 19 },
1158c2ecf20Sopenharmony_ci	{ "f20", GDB_SIZEOF_REG, 20 },
1168c2ecf20Sopenharmony_ci	{ "f21", GDB_SIZEOF_REG, 21 },
1178c2ecf20Sopenharmony_ci	{ "f22", GDB_SIZEOF_REG, 22 },
1188c2ecf20Sopenharmony_ci	{ "f23", GDB_SIZEOF_REG, 23 },
1198c2ecf20Sopenharmony_ci	{ "f24", GDB_SIZEOF_REG, 24 },
1208c2ecf20Sopenharmony_ci	{ "f25", GDB_SIZEOF_REG, 25 },
1218c2ecf20Sopenharmony_ci	{ "f26", GDB_SIZEOF_REG, 26 },
1228c2ecf20Sopenharmony_ci	{ "f27", GDB_SIZEOF_REG, 27 },
1238c2ecf20Sopenharmony_ci	{ "f28", GDB_SIZEOF_REG, 28 },
1248c2ecf20Sopenharmony_ci	{ "f29", GDB_SIZEOF_REG, 29 },
1258c2ecf20Sopenharmony_ci	{ "f30", GDB_SIZEOF_REG, 30 },
1268c2ecf20Sopenharmony_ci	{ "f31", GDB_SIZEOF_REG, 31 },
1278c2ecf20Sopenharmony_ci	{ "fsr", GDB_SIZEOF_REG, 0 },
1288c2ecf20Sopenharmony_ci	{ "fir", GDB_SIZEOF_REG, 0 },
1298c2ecf20Sopenharmony_ci};
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ciint dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	int fp_reg;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (regno < 0 || regno >= DBG_MAX_REG_NUM)
1368c2ecf20Sopenharmony_ci		return -EINVAL;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	if (dbg_reg_def[regno].offset != -1 && regno < 38) {
1398c2ecf20Sopenharmony_ci		memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
1408c2ecf20Sopenharmony_ci		       dbg_reg_def[regno].size);
1418c2ecf20Sopenharmony_ci	} else if (current && dbg_reg_def[regno].offset != -1 && regno < 72) {
1428c2ecf20Sopenharmony_ci		/* FP registers 38 -> 69 */
1438c2ecf20Sopenharmony_ci		if (!(regs->cp0_status & ST0_CU1))
1448c2ecf20Sopenharmony_ci			return 0;
1458c2ecf20Sopenharmony_ci		if (regno == 70) {
1468c2ecf20Sopenharmony_ci			/* Process the fcr31/fsr (register 70) */
1478c2ecf20Sopenharmony_ci			memcpy((void *)&current->thread.fpu.fcr31, mem,
1488c2ecf20Sopenharmony_ci			       dbg_reg_def[regno].size);
1498c2ecf20Sopenharmony_ci			goto out_save;
1508c2ecf20Sopenharmony_ci		} else if (regno == 71) {
1518c2ecf20Sopenharmony_ci			/* Ignore the fir (register 71) */
1528c2ecf20Sopenharmony_ci			goto out_save;
1538c2ecf20Sopenharmony_ci		}
1548c2ecf20Sopenharmony_ci		fp_reg = dbg_reg_def[regno].offset;
1558c2ecf20Sopenharmony_ci		memcpy((void *)&current->thread.fpu.fpr[fp_reg], mem,
1568c2ecf20Sopenharmony_ci		       dbg_reg_def[regno].size);
1578c2ecf20Sopenharmony_ciout_save:
1588c2ecf20Sopenharmony_ci		restore_fp(current);
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return 0;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cichar *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	int fp_reg;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	if (regno >= DBG_MAX_REG_NUM || regno < 0)
1698c2ecf20Sopenharmony_ci		return NULL;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	if (dbg_reg_def[regno].offset != -1 && regno < 38) {
1728c2ecf20Sopenharmony_ci		/* First 38 registers */
1738c2ecf20Sopenharmony_ci		memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
1748c2ecf20Sopenharmony_ci		       dbg_reg_def[regno].size);
1758c2ecf20Sopenharmony_ci	} else if (current && dbg_reg_def[regno].offset != -1 && regno < 72) {
1768c2ecf20Sopenharmony_ci		/* FP registers 38 -> 69 */
1778c2ecf20Sopenharmony_ci		if (!(regs->cp0_status & ST0_CU1))
1788c2ecf20Sopenharmony_ci			goto out;
1798c2ecf20Sopenharmony_ci		save_fp(current);
1808c2ecf20Sopenharmony_ci		if (regno == 70) {
1818c2ecf20Sopenharmony_ci			/* Process the fcr31/fsr (register 70) */
1828c2ecf20Sopenharmony_ci			memcpy(mem, (void *)&current->thread.fpu.fcr31,
1838c2ecf20Sopenharmony_ci			       dbg_reg_def[regno].size);
1848c2ecf20Sopenharmony_ci			goto out;
1858c2ecf20Sopenharmony_ci		} else if (regno == 71) {
1868c2ecf20Sopenharmony_ci			/* Ignore the fir (register 71) */
1878c2ecf20Sopenharmony_ci			memset(mem, 0, dbg_reg_def[regno].size);
1888c2ecf20Sopenharmony_ci			goto out;
1898c2ecf20Sopenharmony_ci		}
1908c2ecf20Sopenharmony_ci		fp_reg = dbg_reg_def[regno].offset;
1918c2ecf20Sopenharmony_ci		memcpy(mem, (void *)&current->thread.fpu.fpr[fp_reg],
1928c2ecf20Sopenharmony_ci		       dbg_reg_def[regno].size);
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ciout:
1968c2ecf20Sopenharmony_ci	return dbg_reg_def[regno].name;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_civoid arch_kgdb_breakpoint(void)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	__asm__ __volatile__(
2038c2ecf20Sopenharmony_ci		".globl breakinst\n\t"
2048c2ecf20Sopenharmony_ci		".set\tnoreorder\n\t"
2058c2ecf20Sopenharmony_ci		"nop\n"
2068c2ecf20Sopenharmony_ci		"breakinst:\tbreak\n\t"
2078c2ecf20Sopenharmony_ci		"nop\n\t"
2088c2ecf20Sopenharmony_ci		".set\treorder");
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_civoid kgdb_call_nmi_hook(void *ignored)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	mm_segment_t old_fs;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	old_fs = get_fs();
2168c2ecf20Sopenharmony_ci	set_fs(KERNEL_DS);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	set_fs(old_fs);
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic int compute_signal(int tt)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	struct hard_trap_info *ht;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
2288c2ecf20Sopenharmony_ci		if (ht->tt == tt)
2298c2ecf20Sopenharmony_ci			return ht->signo;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	return SIGHUP;		/* default for things we don't know about */
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci/*
2358c2ecf20Sopenharmony_ci * Similar to regs_to_gdb_regs() except that process is sleeping and so
2368c2ecf20Sopenharmony_ci * we may not be able to get all the info.
2378c2ecf20Sopenharmony_ci */
2388c2ecf20Sopenharmony_civoid sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	int reg;
2418c2ecf20Sopenharmony_ci#if (KGDB_GDB_REG_SIZE == 32)
2428c2ecf20Sopenharmony_ci	u32 *ptr = (u32 *)gdb_regs;
2438c2ecf20Sopenharmony_ci#else
2448c2ecf20Sopenharmony_ci	u64 *ptr = (u64 *)gdb_regs;
2458c2ecf20Sopenharmony_ci#endif
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	for (reg = 0; reg < 16; reg++)
2488c2ecf20Sopenharmony_ci		*(ptr++) = 0;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/* S0 - S7 */
2518c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg16;
2528c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg17;
2538c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg18;
2548c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg19;
2558c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg20;
2568c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg21;
2578c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg22;
2588c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg23;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	for (reg = 24; reg < 28; reg++)
2618c2ecf20Sopenharmony_ci		*(ptr++) = 0;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/* GP, SP, FP, RA */
2648c2ecf20Sopenharmony_ci	*(ptr++) = (long)p;
2658c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg29;
2668c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg30;
2678c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg31;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.cp0_status;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	/* lo, hi */
2728c2ecf20Sopenharmony_ci	*(ptr++) = 0;
2738c2ecf20Sopenharmony_ci	*(ptr++) = 0;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/*
2768c2ecf20Sopenharmony_ci	 * BadVAddr, Cause
2778c2ecf20Sopenharmony_ci	 * Ideally these would come from the last exception frame up the stack
2788c2ecf20Sopenharmony_ci	 * but that requires unwinding, otherwise we can't know much for sure.
2798c2ecf20Sopenharmony_ci	 */
2808c2ecf20Sopenharmony_ci	*(ptr++) = 0;
2818c2ecf20Sopenharmony_ci	*(ptr++) = 0;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	/*
2848c2ecf20Sopenharmony_ci	 * PC
2858c2ecf20Sopenharmony_ci	 * use return address (RA), i.e. the moment after return from resume()
2868c2ecf20Sopenharmony_ci	 */
2878c2ecf20Sopenharmony_ci	*(ptr++) = p->thread.reg31;
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_civoid kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	regs->cp0_epc = pc;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci/*
2968c2ecf20Sopenharmony_ci * Calls linux_debug_hook before the kernel dies. If KGDB is enabled,
2978c2ecf20Sopenharmony_ci * then try to fall into the debugger
2988c2ecf20Sopenharmony_ci */
2998c2ecf20Sopenharmony_cistatic int kgdb_mips_notify(struct notifier_block *self, unsigned long cmd,
3008c2ecf20Sopenharmony_ci			    void *ptr)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct die_args *args = (struct die_args *)ptr;
3038c2ecf20Sopenharmony_ci	struct pt_regs *regs = args->regs;
3048c2ecf20Sopenharmony_ci	int trap = (regs->cp0_cause & 0x7c) >> 2;
3058c2ecf20Sopenharmony_ci	mm_segment_t old_fs;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci#ifdef CONFIG_KPROBES
3088c2ecf20Sopenharmony_ci	/*
3098c2ecf20Sopenharmony_ci	 * Return immediately if the kprobes fault notifier has set
3108c2ecf20Sopenharmony_ci	 * DIE_PAGE_FAULT.
3118c2ecf20Sopenharmony_ci	 */
3128c2ecf20Sopenharmony_ci	if (cmd == DIE_PAGE_FAULT)
3138c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
3148c2ecf20Sopenharmony_ci#endif /* CONFIG_KPROBES */
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	/* Userspace events, ignore. */
3178c2ecf20Sopenharmony_ci	if (user_mode(regs))
3188c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	/* Kernel mode. Set correct address limit */
3218c2ecf20Sopenharmony_ci	old_fs = get_fs();
3228c2ecf20Sopenharmony_ci	set_fs(KERNEL_DS);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (atomic_read(&kgdb_active) != -1)
3258c2ecf20Sopenharmony_ci		kgdb_nmicallback(smp_processor_id(), regs);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	if (kgdb_handle_exception(trap, compute_signal(trap), cmd, regs)) {
3288c2ecf20Sopenharmony_ci		set_fs(old_fs);
3298c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	if (atomic_read(&kgdb_setting_breakpoint))
3338c2ecf20Sopenharmony_ci		if ((trap == 9) && (regs->cp0_epc == (unsigned long)breakinst))
3348c2ecf20Sopenharmony_ci			regs->cp0_epc += 4;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	/* In SMP mode, __flush_cache_all does IPI */
3378c2ecf20Sopenharmony_ci	local_irq_enable();
3388c2ecf20Sopenharmony_ci	__flush_cache_all();
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	set_fs(old_fs);
3418c2ecf20Sopenharmony_ci	return NOTIFY_STOP;
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
3458c2ecf20Sopenharmony_ciint kgdb_ll_trap(int cmd, const char *str,
3468c2ecf20Sopenharmony_ci		 struct pt_regs *regs, long err, int trap, int sig)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	struct die_args args = {
3498c2ecf20Sopenharmony_ci		.regs	= regs,
3508c2ecf20Sopenharmony_ci		.str	= str,
3518c2ecf20Sopenharmony_ci		.err	= err,
3528c2ecf20Sopenharmony_ci		.trapnr = trap,
3538c2ecf20Sopenharmony_ci		.signr	= sig,
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	};
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	if (!kgdb_io_module_registered)
3588c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	return kgdb_mips_notify(NULL, cmd, &args);
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cistatic struct notifier_block kgdb_notifier = {
3658c2ecf20Sopenharmony_ci	.notifier_call = kgdb_mips_notify,
3668c2ecf20Sopenharmony_ci};
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci/*
3698c2ecf20Sopenharmony_ci * Handle the 'c' command
3708c2ecf20Sopenharmony_ci */
3718c2ecf20Sopenharmony_ciint kgdb_arch_handle_exception(int vector, int signo, int err_code,
3728c2ecf20Sopenharmony_ci			       char *remcom_in_buffer, char *remcom_out_buffer,
3738c2ecf20Sopenharmony_ci			       struct pt_regs *regs)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	char *ptr;
3768c2ecf20Sopenharmony_ci	unsigned long address;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	switch (remcom_in_buffer[0]) {
3798c2ecf20Sopenharmony_ci	case 'c':
3808c2ecf20Sopenharmony_ci		/* handle the optional parameter */
3818c2ecf20Sopenharmony_ci		ptr = &remcom_in_buffer[1];
3828c2ecf20Sopenharmony_ci		if (kgdb_hex2long(&ptr, &address))
3838c2ecf20Sopenharmony_ci			regs->cp0_epc = address;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci		return 0;
3868c2ecf20Sopenharmony_ci	}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	return -1;
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ciconst struct kgdb_arch arch_kgdb_ops = {
3928c2ecf20Sopenharmony_ci#ifdef CONFIG_CPU_BIG_ENDIAN
3938c2ecf20Sopenharmony_ci	.gdb_bpt_instr = { spec_op << 2, 0x00, 0x00, break_op },
3948c2ecf20Sopenharmony_ci#else
3958c2ecf20Sopenharmony_ci	.gdb_bpt_instr = { break_op, 0x00, 0x00, spec_op << 2 },
3968c2ecf20Sopenharmony_ci#endif
3978c2ecf20Sopenharmony_ci};
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ciint kgdb_arch_init(void)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	register_die_notifier(&kgdb_notifier);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	return 0;
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci/*
4078c2ecf20Sopenharmony_ci *	kgdb_arch_exit - Perform any architecture specific uninitalization.
4088c2ecf20Sopenharmony_ci *
4098c2ecf20Sopenharmony_ci *	This function will handle the uninitalization of any architecture
4108c2ecf20Sopenharmony_ci *	specific callbacks, for dynamic registration and unregistration.
4118c2ecf20Sopenharmony_ci */
4128c2ecf20Sopenharmony_civoid kgdb_arch_exit(void)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	unregister_die_notifier(&kgdb_notifier);
4158c2ecf20Sopenharmony_ci}
416