Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Common arm64 stack unwinder code.
0004  *
0005  * To implement a new arm64 stack unwinder:
0006  *     1) Include this header
0007  *
0008  *     2) Call into unwind_next_common() from your top level unwind
0009  *        function, passing it the validation and translation callbacks
0010  *        (though the later can be NULL if no translation is required).
0011  *
0012  * See: arch/arm64/kernel/stacktrace.c for the reference implementation.
0013  *
0014  * Copyright (C) 2012 ARM Ltd.
0015  */
0016 #ifndef __ASM_STACKTRACE_COMMON_H
0017 #define __ASM_STACKTRACE_COMMON_H
0018 
0019 #include <linux/bitmap.h>
0020 #include <linux/bitops.h>
0021 #include <linux/kprobes.h>
0022 #include <linux/types.h>
0023 
0024 enum stack_type {
0025     STACK_TYPE_UNKNOWN,
0026     STACK_TYPE_TASK,
0027     STACK_TYPE_IRQ,
0028     STACK_TYPE_OVERFLOW,
0029     STACK_TYPE_SDEI_NORMAL,
0030     STACK_TYPE_SDEI_CRITICAL,
0031     STACK_TYPE_HYP,
0032     __NR_STACK_TYPES
0033 };
0034 
0035 struct stack_info {
0036     unsigned long low;
0037     unsigned long high;
0038     enum stack_type type;
0039 };
0040 
0041 /*
0042  * A snapshot of a frame record or fp/lr register values, along with some
0043  * accounting information necessary for robust unwinding.
0044  *
0045  * @fp:          The fp value in the frame record (or the real fp)
0046  * @pc:          The lr value in the frame record (or the real lr)
0047  *
0048  * @stacks_done: Stacks which have been entirely unwound, for which it is no
0049  *               longer valid to unwind to.
0050  *
0051  * @prev_fp:     The fp that pointed to this frame record, or a synthetic value
0052  *               of 0. This is used to ensure that within a stack, each
0053  *               subsequent frame record is at an increasing address.
0054  * @prev_type:   The type of stack this frame record was on, or a synthetic
0055  *               value of STACK_TYPE_UNKNOWN. This is used to detect a
0056  *               transition from one stack to another.
0057  *
0058  * @kr_cur:      When KRETPROBES is selected, holds the kretprobe instance
0059  *               associated with the most recently encountered replacement lr
0060  *               value.
0061  *
0062  * @task:        The task being unwound.
0063  */
0064 struct unwind_state {
0065     unsigned long fp;
0066     unsigned long pc;
0067     DECLARE_BITMAP(stacks_done, __NR_STACK_TYPES);
0068     unsigned long prev_fp;
0069     enum stack_type prev_type;
0070 #ifdef CONFIG_KRETPROBES
0071     struct llist_node *kr_cur;
0072 #endif
0073     struct task_struct *task;
0074 };
0075 
0076 static inline bool on_stack(unsigned long sp, unsigned long size,
0077                 unsigned long low, unsigned long high,
0078                 enum stack_type type, struct stack_info *info)
0079 {
0080     if (!low)
0081         return false;
0082 
0083     if (sp < low || sp + size < sp || sp + size > high)
0084         return false;
0085 
0086     if (info) {
0087         info->low = low;
0088         info->high = high;
0089         info->type = type;
0090     }
0091     return true;
0092 }
0093 
0094 static inline void unwind_init_common(struct unwind_state *state,
0095                       struct task_struct *task)
0096 {
0097     state->task = task;
0098 #ifdef CONFIG_KRETPROBES
0099     state->kr_cur = NULL;
0100 #endif
0101 
0102     /*
0103      * Prime the first unwind.
0104      *
0105      * In unwind_next() we'll check that the FP points to a valid stack,
0106      * which can't be STACK_TYPE_UNKNOWN, and the first unwind will be
0107      * treated as a transition to whichever stack that happens to be. The
0108      * prev_fp value won't be used, but we set it to 0 such that it is
0109      * definitely not an accessible stack address.
0110      */
0111     bitmap_zero(state->stacks_done, __NR_STACK_TYPES);
0112     state->prev_fp = 0;
0113     state->prev_type = STACK_TYPE_UNKNOWN;
0114 }
0115 
0116 /*
0117  * stack_trace_translate_fp_fn() - Translates a non-kernel frame pointer to
0118  * a kernel address.
0119  *
0120  * @fp:   the frame pointer to be updated to its kernel address.
0121  * @type: the stack type associated with frame pointer @fp
0122  *
0123  * Returns true and success and @fp is updated to the corresponding
0124  * kernel virtual address; otherwise returns false.
0125  */
0126 typedef bool (*stack_trace_translate_fp_fn)(unsigned long *fp,
0127                         enum stack_type type);
0128 
0129 /*
0130  * on_accessible_stack_fn() - Check whether a stack range is on any
0131  * of the possible stacks.
0132  *
0133  * @tsk:  task whose stack is being unwound
0134  * @sp:   stack address being checked
0135  * @size: size of the stack range being checked
0136  * @info: stack unwinding context
0137  */
0138 typedef bool (*on_accessible_stack_fn)(const struct task_struct *tsk,
0139                        unsigned long sp, unsigned long size,
0140                        struct stack_info *info);
0141 
0142 static inline int unwind_next_common(struct unwind_state *state,
0143                      struct stack_info *info,
0144                      on_accessible_stack_fn accessible,
0145                      stack_trace_translate_fp_fn translate_fp)
0146 {
0147     unsigned long fp = state->fp, kern_fp = fp;
0148     struct task_struct *tsk = state->task;
0149 
0150     if (fp & 0x7)
0151         return -EINVAL;
0152 
0153     if (!accessible(tsk, fp, 16, info))
0154         return -EINVAL;
0155 
0156     if (test_bit(info->type, state->stacks_done))
0157         return -EINVAL;
0158 
0159     /*
0160      * If fp is not from the current address space perform the necessary
0161      * translation before dereferencing it to get the next fp.
0162      */
0163     if (translate_fp && !translate_fp(&kern_fp, info->type))
0164         return -EINVAL;
0165 
0166     /*
0167      * As stacks grow downward, any valid record on the same stack must be
0168      * at a strictly higher address than the prior record.
0169      *
0170      * Stacks can nest in several valid orders, e.g.
0171      *
0172      * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL
0173      * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW
0174      * HYP -> OVERFLOW
0175      *
0176      * ... but the nesting itself is strict. Once we transition from one
0177      * stack to another, it's never valid to unwind back to that first
0178      * stack.
0179      */
0180     if (info->type == state->prev_type) {
0181         if (fp <= state->prev_fp)
0182             return -EINVAL;
0183     } else {
0184         __set_bit(state->prev_type, state->stacks_done);
0185     }
0186 
0187     /*
0188      * Record this frame record's values and location. The prev_fp and
0189      * prev_type are only meaningful to the next unwind_next() invocation.
0190      */
0191     state->fp = READ_ONCE(*(unsigned long *)(kern_fp));
0192     state->pc = READ_ONCE(*(unsigned long *)(kern_fp + 8));
0193     state->prev_fp = fp;
0194     state->prev_type = info->type;
0195 
0196     return 0;
0197 }
0198 
0199 #endif  /* __ASM_STACKTRACE_COMMON_H */