Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * SuperH KGDB support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2008 - 2012  Paul Mundt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Single stepping taken from the old stub by Henry Bell and Jeremy Siegel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kgdb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kdebug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <asm/traps.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /* Macros for single step instruction identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define OPCODE_BT(op)		(((op) & 0xff00) == 0x8900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define OPCODE_BF(op)		(((op) & 0xff00) == 0x8b00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define OPCODE_BTF_DISP(op)	(((op) & 0x80) ? (((op) | 0xffffff80) << 1) : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 				 (((op) & 0x7f ) << 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define OPCODE_BFS(op)		(((op) & 0xff00) == 0x8f00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define OPCODE_BTS(op)		(((op) & 0xff00) == 0x8d00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define OPCODE_BRA(op)		(((op) & 0xf000) == 0xa000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define OPCODE_BRA_DISP(op)	(((op) & 0x800) ? (((op) | 0xfffff800) << 1) : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 				 (((op) & 0x7ff) << 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define OPCODE_BRAF(op)		(((op) & 0xf0ff) == 0x0023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define OPCODE_BRAF_REG(op)	(((op) & 0x0f00) >> 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define OPCODE_BSR(op)		(((op) & 0xf000) == 0xb000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define OPCODE_BSR_DISP(op)	(((op) & 0x800) ? (((op) | 0xfffff800) << 1) : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 				 (((op) & 0x7ff) << 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define OPCODE_BSRF(op)		(((op) & 0xf0ff) == 0x0003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define OPCODE_BSRF_REG(op)	(((op) >> 8) & 0xf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define OPCODE_JMP(op)		(((op) & 0xf0ff) == 0x402b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define OPCODE_JMP_REG(op)	(((op) >> 8) & 0xf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define OPCODE_JSR(op)		(((op) & 0xf0ff) == 0x400b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define OPCODE_JSR_REG(op)	(((op) >> 8) & 0xf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define OPCODE_RTS(op)		((op) == 0xb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define OPCODE_RTE(op)		((op) == 0x2b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define SR_T_BIT_MASK           0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define STEP_OPCODE             0xc33d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) /* Calculate the new address for after a step */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static short *get_step_address(struct pt_regs *linux_regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	insn_size_t op = __raw_readw(linux_regs->pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	/* BT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (OPCODE_BT(op)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		if (linux_regs->sr & SR_T_BIT_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			addr = linux_regs->pc + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	/* BTS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	else if (OPCODE_BTS(op)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		if (linux_regs->sr & SR_T_BIT_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			addr = linux_regs->pc + 4;	/* Not in delay slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	/* BF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	else if (OPCODE_BF(op)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		if (!(linux_regs->sr & SR_T_BIT_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			addr = linux_regs->pc + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	/* BFS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	else if (OPCODE_BFS(op)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		if (!(linux_regs->sr & SR_T_BIT_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			addr = linux_regs->pc + 4 + OPCODE_BTF_DISP(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			addr = linux_regs->pc + 4;	/* Not in delay slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	/* BRA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	else if (OPCODE_BRA(op))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		addr = linux_regs->pc + 4 + OPCODE_BRA_DISP(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/* BRAF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	else if (OPCODE_BRAF(op))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		addr = linux_regs->pc + 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		    + linux_regs->regs[OPCODE_BRAF_REG(op)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	/* BSR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	else if (OPCODE_BSR(op))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		addr = linux_regs->pc + 4 + OPCODE_BSR_DISP(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	/* BSRF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	else if (OPCODE_BSRF(op))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		addr = linux_regs->pc + 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		    + linux_regs->regs[OPCODE_BSRF_REG(op)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	/* JMP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	else if (OPCODE_JMP(op))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		addr = linux_regs->regs[OPCODE_JMP_REG(op)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* JSR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	else if (OPCODE_JSR(op))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		addr = linux_regs->regs[OPCODE_JSR_REG(op)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	/* RTS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	else if (OPCODE_RTS(op))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		addr = linux_regs->pr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	/* RTE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	else if (OPCODE_RTE(op))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		addr = linux_regs->regs[15];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	/* Other */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		addr = linux_regs->pc + instruction_size(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	flush_icache_range(addr, addr + instruction_size(op));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return (short *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  * Replace the instruction immediately after the current instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * (i.e. next in the expected flow of control) with a trap instruction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  * so that returning will cause only a single instruction to be executed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  * Note that this model is slightly broken for instructions with delay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  * slots (e.g. B[TF]S, BSR, BRA etc), where both the branch and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * instruction in the delay slot will be executed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static unsigned long stepped_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static insn_size_t stepped_opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static void do_single_step(struct pt_regs *linux_regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	/* Determine where the target instruction will send us to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	unsigned short *addr = get_step_address(linux_regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	stepped_address = (int)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	/* Replace it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	stepped_opcode = __raw_readw((long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	*addr = STEP_OPCODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	/* Flush and return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	flush_icache_range((long)addr, (long)addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			   instruction_size(stepped_opcode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* Undo a single step */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static void undo_single_step(struct pt_regs *linux_regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	/* If we have stepped, put back the old instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	/* Use stepped_address in case we stopped elsewhere */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (stepped_opcode != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		__raw_writew(stepped_opcode, stepped_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		flush_icache_range(stepped_address, stepped_address + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	stepped_opcode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	{ "r0",		GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[0]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	{ "r1",		GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[1]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	{ "r2",		GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[2]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	{ "r3",		GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[3]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	{ "r4",		GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[4]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	{ "r5",		GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[5]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	{ "r6",		GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[6]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	{ "r7",		GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[7]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	{ "r8",		GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[8]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	{ "r9",		GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[9]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	{ "r10",	GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[10]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	{ "r11",	GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[11]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	{ "r12",	GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[12]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	{ "r13",	GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[13]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	{ "r14",	GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[14]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	{ "r15",	GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[15]) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	{ "pc",		GDB_SIZEOF_REG, offsetof(struct pt_regs, pc) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	{ "pr",		GDB_SIZEOF_REG, offsetof(struct pt_regs, pr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	{ "sr",		GDB_SIZEOF_REG, offsetof(struct pt_regs, sr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	{ "gbr",	GDB_SIZEOF_REG, offsetof(struct pt_regs, gbr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	{ "mach",	GDB_SIZEOF_REG, offsetof(struct pt_regs, mach) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	{ "macl",	GDB_SIZEOF_REG, offsetof(struct pt_regs, macl) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	{ "vbr",	GDB_SIZEOF_REG, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (regno < 0 || regno >= DBG_MAX_REG_NUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (dbg_reg_def[regno].offset != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		       dbg_reg_def[regno].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (regno >= DBG_MAX_REG_NUM || regno < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (dbg_reg_def[regno].size != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		       dbg_reg_def[regno].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	switch (regno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	case GDB_VBR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		__asm__ __volatile__ ("stc vbr, %0" : "=r" (mem));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return dbg_reg_def[regno].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	struct pt_regs *thread_regs = task_pt_regs(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	int reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	/* Initialize to zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	for (reg = 0; reg < DBG_MAX_REG_NUM; reg++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		gdb_regs[reg] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	 * Copy out GP regs 8 to 14.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	 * switch_to() relies on SR.RB toggling, so regs 0->7 are banked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	 * and need privileged instructions to get to. The r15 value we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	 * fetch from the thread info directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	for (reg = GDB_R8; reg < GDB_R15; reg++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		gdb_regs[reg] = thread_regs->regs[reg];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	gdb_regs[GDB_R15] = p->thread.sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	gdb_regs[GDB_PC] = p->thread.pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	 * Additional registers we have context for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	gdb_regs[GDB_PR] = thread_regs->pr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	gdb_regs[GDB_GBR] = thread_regs->gbr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			       char *remcomInBuffer, char *remcomOutBuffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			       struct pt_regs *linux_regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	/* Undo any stepping we may have done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	undo_single_step(linux_regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	switch (remcomInBuffer[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	case 'c':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	case 's':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		/* try to read optional parameter, pc unchanged if no parm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		ptr = &remcomInBuffer[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		if (kgdb_hex2long(&ptr, &addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			linux_regs->pc = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	case 'D':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	case 'k':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		atomic_set(&kgdb_cpu_doing_single_step, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		if (remcomInBuffer[0] == 's') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			do_single_step(linux_regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			kgdb_single_step = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			atomic_set(&kgdb_cpu_doing_single_step,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 				   raw_smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	/* this means that we do not want to exit from the handler: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (exception == 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		return instruction_pointer(regs) - 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	return instruction_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	regs->pc = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  * The primary entry points for the kgdb debug trap table entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) BUILD_TRAP_HANDLER(singlestep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	TRAP_HANDLER_DECL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	kgdb_handle_exception(0, SIGTRAP, 0, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static int __kgdb_notify(struct die_args *args, unsigned long cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	case DIE_BREAKPOINT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		 * This means a user thread is single stepping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		 * a system call which should be ignored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		if (test_thread_flag(TIF_SINGLESTEP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		ret = kgdb_handle_exception(args->trapnr & 0xff, args->signr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 					    args->err, args->regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	return NOTIFY_STOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	ret = __kgdb_notify(ptr, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static struct notifier_block kgdb_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	.notifier_call	= kgdb_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	 * Lowest-prio notifier priority, we want to be notified last:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	.priority	= -INT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) int kgdb_arch_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	return register_die_notifier(&kgdb_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) void kgdb_arch_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	unregister_die_notifier(&kgdb_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) const struct kgdb_arch arch_kgdb_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	/* Breakpoint instruction: trapa #0x3c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) #ifdef CONFIG_CPU_LITTLE_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	.gdb_bpt_instr		= { 0x3c, 0xc3 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	.gdb_bpt_instr		= { 0xc3, 0x3c },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) };