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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * H8/300 KGDB support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2015 Yoshinori Sato <ysato@users.sourceforge.jp>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * License.  See the file "COPYING" in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kgdb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kdebug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	{ "er0", GDB_SIZEOF_REG, offsetof(struct pt_regs, er0) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	{ "er1", GDB_SIZEOF_REG, offsetof(struct pt_regs, er1) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	{ "er2", GDB_SIZEOF_REG, offsetof(struct pt_regs, er2) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	{ "er3", GDB_SIZEOF_REG, offsetof(struct pt_regs, er3) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	{ "er4", GDB_SIZEOF_REG, offsetof(struct pt_regs, er4) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	{ "er5", GDB_SIZEOF_REG, offsetof(struct pt_regs, er5) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	{ "er6", GDB_SIZEOF_REG, offsetof(struct pt_regs, er6) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	{ "sp", GDB_SIZEOF_REG, offsetof(struct pt_regs, sp) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	{ "ccr", GDB_SIZEOF_REG, offsetof(struct pt_regs, ccr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	{ "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, pc) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	{ "cycles", GDB_SIZEOF_REG, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #if defined(CONFIG_CPU_H8S)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	{ "exr", GDB_SIZEOF_REG, offsetof(struct pt_regs, exr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	{ "tick", GDB_SIZEOF_REG, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	{ "inst", GDB_SIZEOF_REG, -1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (regno >= DBG_MAX_REG_NUM || regno < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	switch (regno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	case GDB_CCR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #if defined(CONFIG_CPU_H8S)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	case GDB_EXR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		*(u32 *)mem = *(u16 *)((void *)regs +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 				       dbg_reg_def[regno].offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		if (dbg_reg_def[regno].offset >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			       dbg_reg_def[regno].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 			memset(mem, 0, dbg_reg_def[regno].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	return dbg_reg_def[regno].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (regno >= DBG_MAX_REG_NUM || regno < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	switch (regno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	case GDB_CCR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #if defined(CONFIG_CPU_H8S)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	case GDB_EXR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		*(u16 *)((void *)regs +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			 dbg_reg_def[regno].offset) = *(u32 *)mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		       dbg_reg_def[regno].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) asmlinkage void h8300_kgdb_trap(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	regs->pc &= 0x00ffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (kgdb_handle_exception(10, SIGTRAP, 0, regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (*(u16 *)(regs->pc) == *(u16 *)&arch_kgdb_ops.gdb_bpt_instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		regs->pc += BREAK_INSTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	regs->pc |= regs->ccr << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	memset((char *)gdb_regs, 0, NUMREGBYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	gdb_regs[GDB_SP] = p->thread.ksp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	gdb_regs[GDB_PC] = KSTK_EIP(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	regs->pc = pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int kgdb_arch_handle_exception(int vector, int signo, int err_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 				char *remcom_in_buffer, char *remcom_out_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 				struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	switch (remcom_in_buffer[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	case 's':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	case 'c':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		/* handle the optional parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		ptr = &remcom_in_buffer[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		if (kgdb_hex2long(&ptr, &addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			regs->pc = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return -1; /* this means that we do not want to exit from the handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int kgdb_arch_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return 0;
^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) void kgdb_arch_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	/* Nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) const struct kgdb_arch arch_kgdb_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	/* Breakpoint instruction: trapa #2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	.gdb_bpt_instr = { 0x57, 0x20 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) };