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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * kgdb support for ARC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2012 Synopsys, Inc. (www.synopsys.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kgdb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <asm/disasm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) static void to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 			struct callee_regs *cregs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	int regno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	for (regno = 0; regno <= 26; regno++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		gdb_regs[_R0 + regno] = get_reg(regno, kernel_regs, cregs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	for (regno = 27; regno < GDB_MAX_REGS; regno++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		gdb_regs[regno] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	gdb_regs[_FP]		= kernel_regs->fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	gdb_regs[__SP]		= kernel_regs->sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	gdb_regs[_BLINK]	= kernel_regs->blink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	gdb_regs[_RET]		= kernel_regs->ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	gdb_regs[_STATUS32]	= kernel_regs->status32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	gdb_regs[_LP_COUNT]	= kernel_regs->lp_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	gdb_regs[_LP_END]	= kernel_regs->lp_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	gdb_regs[_LP_START]	= kernel_regs->lp_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	gdb_regs[_BTA]		= kernel_regs->bta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	gdb_regs[_STOP_PC]	= kernel_regs->ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static void from_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 			struct callee_regs *cregs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	int regno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	for (regno = 0; regno <= 26; regno++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		set_reg(regno, gdb_regs[regno + _R0], kernel_regs, cregs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	kernel_regs->fp		= gdb_regs[_FP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	kernel_regs->sp		= gdb_regs[__SP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	kernel_regs->blink	= gdb_regs[_BLINK];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	kernel_regs->ret	= gdb_regs[_RET];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	kernel_regs->status32	= gdb_regs[_STATUS32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	kernel_regs->lp_count	= gdb_regs[_LP_COUNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	kernel_regs->lp_end	= gdb_regs[_LP_END];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	kernel_regs->lp_start	= gdb_regs[_LP_START];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	kernel_regs->bta	= gdb_regs[_BTA];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	to_gdb_regs(gdb_regs, kernel_regs, (struct callee_regs *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		current->thread.callee_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	from_gdb_regs(gdb_regs, kernel_regs, (struct callee_regs *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		current->thread.callee_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 				 struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		to_gdb_regs(gdb_regs, task_pt_regs(task),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			(struct callee_regs *) task->thread.callee_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) struct single_step_data_t {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	uint16_t opcode[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	unsigned long address[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	int is_branch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int armed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) } single_step_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static void undo_single_step(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (single_step_data.armed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		for (i = 0; i < (single_step_data.is_branch ? 2 : 1); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			memcpy((void *) single_step_data.address[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 				&single_step_data.opcode[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 				BREAK_INSTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			flush_icache_range(single_step_data.address[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				single_step_data.address[i] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 				BREAK_INSTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		single_step_data.armed = 0;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static void place_trap(unsigned long address, void *save)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	memcpy(save, (void *) address, BREAK_INSTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	memcpy((void *) address, &arch_kgdb_ops.gdb_bpt_instr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		BREAK_INSTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	flush_icache_range(address, address + BREAK_INSTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static void do_single_step(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	single_step_data.is_branch = disasm_next_pc((unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		regs->ret, regs, (struct callee_regs *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		current->thread.callee_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		&single_step_data.address[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		&single_step_data.address[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	place_trap(single_step_data.address[0], &single_step_data.opcode[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (single_step_data.is_branch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		place_trap(single_step_data.address[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			&single_step_data.opcode[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	single_step_data.armed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			       char *remcomInBuffer, char *remcomOutBuffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			       struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	undo_single_step(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	switch (remcomInBuffer[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	case 's':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	case 'c':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		ptr = &remcomInBuffer[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (kgdb_hex2long(&ptr, &addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			regs->ret = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	case 'D':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	case 'k':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		atomic_set(&kgdb_cpu_doing_single_step, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		if (remcomInBuffer[0] == 's') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			do_single_step(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			atomic_set(&kgdb_cpu_doing_single_step,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				   smp_processor_id());
^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) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int kgdb_arch_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	single_step_data.armed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void kgdb_trap(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	/* trap_s 3 is used for breakpoints that overwrite existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 * instructions, while trap_s 4 is used for compiled breakpoints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	 * with trap_s 3 breakpoints the original instruction needs to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	 * restored and continuation needs to start at the location of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	 * breakpoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	 * with trap_s 4 (compiled) breakpoints, continuation needs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	 * start after the breakpoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (regs->ecr_param == 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		instruction_pointer(regs) -= BREAK_INSTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	kgdb_handle_exception(1, SIGTRAP, 0, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) void kgdb_arch_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	instruction_pointer(regs) = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) void kgdb_call_nmi_hook(void *ignored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	/* Default implementation passes get_irq_regs() but we don't */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	kgdb_nmicallback(raw_smp_processor_id(), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) const struct kgdb_arch arch_kgdb_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	/* breakpoint instruction: TRAP_S 0x3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) #ifdef CONFIG_CPU_BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	.gdb_bpt_instr		= {0x78, 0x7e},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	.gdb_bpt_instr		= {0x7e, 0x78},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) };