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) #include <linux/oprofile.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <asm/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <asm/inst.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) struct stackframe {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	unsigned long sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	unsigned long pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	unsigned long ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static inline int get_mem(unsigned long addr, unsigned long *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	unsigned long *address = (unsigned long *) addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	if (!access_ok(address, sizeof(unsigned long)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	if (__copy_from_user_inatomic(result, address, sizeof(unsigned long)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		return -3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * These two instruction helpers were taken from process.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static inline int is_ra_save_ins(union mips_instruction *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	/* sw / sd $ra, offset($sp) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	return (ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		&& ip->i_format.rs == 29 && ip->i_format.rt == 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static inline int is_sp_move_ins(union mips_instruction *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	/* addiu/daddiu sp,sp,-imm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (ip->i_format.rs != 29 || ip->i_format.rt != 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	if (ip->i_format.opcode == addiu_op || ip->i_format.opcode == daddiu_op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * Looks for specific instructions that mark the end of a function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * This usually means we ran into the code area of the previous function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static inline int is_end_of_function_marker(union mips_instruction *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	/* jr ra */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (ip->r_format.func == jr_op && ip->r_format.rs == 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	/* lui gp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (ip->i_format.opcode == lui_op && ip->i_format.rt == 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * TODO for userspace stack unwinding:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * - handle cases where the stack is adjusted inside a function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  *     (generally doesn't happen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * - find optimal value for max_instr_check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * - try to find a better way to handle leaf functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static inline int unwind_user_frame(struct stackframe *old_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 				    const unsigned int max_instr_check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct stackframe new_frame = *old_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	off_t ra_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	size_t stack_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (old_frame->pc == 0 || old_frame->sp == 0 || old_frame->ra == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return -9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	for (addr = new_frame.pc; (addr + max_instr_check > new_frame.pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		&& (!ra_offset || !stack_size); --addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		union mips_instruction ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		if (get_mem(addr, (unsigned long *) &ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			return -11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (is_sp_move_ins(&ip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			int stack_adjustment = ip.i_format.simmediate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			if (stack_adjustment > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				/* This marks the end of the previous function,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 				   which means we overran. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			stack_size = (unsigned long) stack_adjustment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		} else if (is_ra_save_ins(&ip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			int ra_slot = ip.i_format.simmediate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			if (ra_slot < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 				/* This shouldn't happen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			ra_offset = ra_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		} else if (is_end_of_function_marker(&ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (!ra_offset || !stack_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (ra_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		new_frame.ra = old_frame->sp + ra_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		if (get_mem(new_frame.ra, &(new_frame.ra)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			return -13;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (stack_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		new_frame.sp = old_frame->sp + stack_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		if (get_mem(new_frame.sp, &(new_frame.sp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			return -14;
^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) 	if (new_frame.sp > old_frame->sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return -2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	new_frame.pc = old_frame->ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	*old_frame = new_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return 0;
^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) static inline void do_user_backtrace(unsigned long low_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				     struct stackframe *frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 				     unsigned int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	const unsigned int max_instr_check = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	const unsigned long high_addr = low_addr + THREAD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	while (depth-- && !unwind_user_frame(frame, max_instr_check)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		oprofile_add_trace(frame->ra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (frame->sp < low_addr || frame->sp > high_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #ifndef CONFIG_KALLSYMS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static inline void do_kernel_backtrace(unsigned long low_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				       struct stackframe *frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 				       unsigned int depth) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static inline void do_kernel_backtrace(unsigned long low_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				       struct stackframe *frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				       unsigned int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	while (depth-- && frame->pc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		frame->pc = unwind_stack_by_address(low_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 						    &(frame->sp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 						    frame->pc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 						    &(frame->ra));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		oprofile_add_trace(frame->ra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void notrace op_mips_backtrace(struct pt_regs *const regs, unsigned int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	struct stackframe frame = { .sp = regs->regs[29],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 				    .pc = regs->cp0_epc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 				    .ra = regs->regs[31] };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	const int userspace = user_mode(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	const unsigned long low_addr = ALIGN(frame.sp, THREAD_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (userspace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		do_user_backtrace(low_addr, &frame, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		do_kernel_backtrace(low_addr, &frame, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }