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)  * @file backtrace.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * @remark Copyright 2002 OProfile authors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * @remark Read the file COPYING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * @author John Levon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * @author David Smith
^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/oprofile.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <asm/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/unwind.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static struct stack_frame_ia32 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) dump_user_backtrace_32(struct stack_frame_ia32 *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	/* Also check accessibility of one struct frame_head beyond: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct stack_frame_ia32 bufhead[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct stack_frame_ia32 *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	unsigned long bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if (bytes != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	oprofile_add_trace(bufhead[0].return_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	/* frame pointers should strictly progress back up the stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	* (towards higher addresses) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (head >= fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	return fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct stack_frame_ia32 *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	/* User process is IA32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (!current || !test_thread_flag(TIF_IA32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	head = (struct stack_frame_ia32 *) regs->bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	while (depth-- && head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		head = dump_user_backtrace_32(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #endif /* CONFIG_COMPAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	/* Also check accessibility of one struct frame_head beyond: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct stack_frame bufhead[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	unsigned long bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (bytes != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	oprofile_add_trace(bufhead[0].return_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	/* frame pointers should strictly progress back up the stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	 * (towards higher addresses) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (head >= bufhead[0].next_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return bufhead[0].next_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) x86_backtrace(struct pt_regs * const regs, unsigned int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (!user_mode(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		struct unwind_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		if (!depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		oprofile_add_trace(regs->ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		if (!--depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		for (unwind_start(&state, current, regs, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		     !unwind_done(&state); unwind_next_frame(&state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			addr = unwind_get_return_address(&state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			if (!addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			oprofile_add_trace(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			if (!--depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 				break;
^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;
^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 (x86_backtrace_32(regs, depth))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	while (depth-- && head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		head = dump_user_backtrace(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }