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/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <asm/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <asm/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <asm/unwind.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) unsigned long unwind_get_return_address(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	if (unwind_done(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	return __kernel_text_address(state->ip) ? state->ip : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) EXPORT_SYMBOL_GPL(unwind_get_return_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static bool outside_of_stack(struct unwind_state *state, unsigned long sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	return (sp <= state->sp) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		(sp > state->stack_info.end - sizeof(struct stack_frame));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static bool update_stack_info(struct unwind_state *state, unsigned long sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct stack_info *info = &state->stack_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	unsigned long *mask = &state->stack_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	/* New stack pointer leaves the current stack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (get_stack_info(sp, state->task, info, mask) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	    !on_stack(info, sp, sizeof(struct stack_frame)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		/* 'sp' does not point to a valid stack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	return true;
^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 bool is_final_pt_regs(struct unwind_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 				    struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	/* user mode or kernel thread pt_regs at the bottom of task stack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (task_pt_regs(state->task) == regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	/* user mode pt_regs at the bottom of irq stack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	return state->stack_info.type == STACK_TYPE_IRQ &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	       state->stack_info.end - sizeof(struct pt_regs) == (unsigned long)regs &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	       READ_ONCE_NOCHECK(regs->psw.mask) & PSW_MASK_PSTATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) bool unwind_next_frame(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct stack_info *info = &state->stack_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct stack_frame *sf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct pt_regs *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	unsigned long sp, ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	bool reliable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	regs = state->regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (unlikely(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		sp = state->sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		sf = (struct stack_frame *) sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		ip = READ_ONCE_NOCHECK(sf->gprs[8]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		reliable = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		regs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		if (!__kernel_text_address(ip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			/* skip bogus %r14 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			state->regs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			return unwind_next_frame(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		sf = (struct stack_frame *) state->sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		sp = READ_ONCE_NOCHECK(sf->back_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		if (likely(sp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			/* Non-zero back-chain points to the previous frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			if (unlikely(outside_of_stack(state, sp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 				if (!update_stack_info(state, sp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 					goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			sf = (struct stack_frame *) sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			ip = READ_ONCE_NOCHECK(sf->gprs[8]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			reliable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			/* No back-chain, look for a pt_regs structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			sp = state->sp + STACK_FRAME_OVERHEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			if (!on_stack(info, sp, sizeof(struct pt_regs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			regs = (struct pt_regs *) sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			if (is_final_pt_regs(state, regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 				goto out_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			ip = READ_ONCE_NOCHECK(regs->psw.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			sp = READ_ONCE_NOCHECK(regs->gprs[15]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			if (unlikely(outside_of_stack(state, sp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				if (!update_stack_info(state, sp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 					goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			reliable = true;
^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) 	/* Sanity check: ABI requires SP to be aligned 8 bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (sp & 0x7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, ip, (void *) sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/* Update unwind state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	state->sp = sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	state->ip = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	state->regs = regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	state->reliable = reliable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	state->error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) out_stop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	state->stack_info.type = STACK_TYPE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) EXPORT_SYMBOL_GPL(unwind_next_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) void __unwind_start(struct unwind_state *state, struct task_struct *task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		    struct pt_regs *regs, unsigned long first_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct stack_info *info = &state->stack_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct stack_frame *sf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	unsigned long ip, sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	memset(state, 0, sizeof(*state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	state->task = task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	state->regs = regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	/* Don't even attempt to start from user mode regs: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (regs && user_mode(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		info->type = STACK_TYPE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	/* Get the instruction pointer from pt_regs or the stack frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		ip = regs->psw.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		sp = regs->gprs[15];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	} else if (task == current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		sp = current_frame_address();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		sp = task->thread.ksp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	/* Get current stack pointer and initialize stack info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (!update_stack_info(state, sp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		/* Something is wrong with the stack pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		info->type = STACK_TYPE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		state->error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (!regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		/* Stack frame is within valid stack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		sf = (struct stack_frame *)sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		ip = READ_ONCE_NOCHECK(sf->gprs[8]);
^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) 	ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, ip, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	/* Update unwind state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	state->sp = sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	state->ip = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	state->reliable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (!first_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	/* Skip through the call chain to the specified starting frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	while (!unwind_done(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		if (on_stack(&state->stack_info, first_frame, sizeof(struct stack_frame))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			if (state->sp >= first_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		unwind_next_frame(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) EXPORT_SYMBOL_GPL(__unwind_start);