^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 1991, 1992 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/hardirq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kdebug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/nmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/cpu_entry_area.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static const char * const exception_stack_names[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) [ ESTACK_DF ] = "#DF",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) [ ESTACK_NMI ] = "NMI",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) [ ESTACK_DB ] = "#DB",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) [ ESTACK_MCE ] = "#MC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) [ ESTACK_VC ] = "#VC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) [ ESTACK_VC2 ] = "#VC2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) const char *stack_type_name(enum stack_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) BUILD_BUG_ON(N_EXCEPTION_STACKS != 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (type == STACK_TYPE_IRQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return "IRQ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (type == STACK_TYPE_ENTRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * On 64-bit, we have a generic entry stack that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * use for all the kernel entry points, including
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * SYSENTER.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return "ENTRY_TRAMPOLINE";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (type >= STACK_TYPE_EXCEPTION && type <= STACK_TYPE_EXCEPTION_LAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return exception_stack_names[type - STACK_TYPE_EXCEPTION];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * struct estack_pages - Page descriptor for exception stacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * @offs: Offset from the start of the exception stack area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * @size: Size of the exception stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * @type: Type to store in the stack_info struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct estack_pages {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) u32 offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) u16 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) u16 type;
^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) #define EPAGERANGE(st) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) [PFN_DOWN(CEA_ESTACK_OFFS(st)) ... \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) PFN_DOWN(CEA_ESTACK_OFFS(st) + CEA_ESTACK_SIZE(st) - 1)] = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .offs = CEA_ESTACK_OFFS(st), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .size = CEA_ESTACK_SIZE(st), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .type = STACK_TYPE_EXCEPTION + ESTACK_ ##st, }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * Array of exception stack page descriptors. If the stack is larger than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * PAGE_SIZE, all pages covering a particular stack will have the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * info. The guard pages including the not mapped DB2 stack are zeroed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static const
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct estack_pages estack_pages[CEA_ESTACK_PAGES] ____cacheline_aligned = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) EPAGERANGE(DF),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) EPAGERANGE(NMI),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) EPAGERANGE(DB),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) EPAGERANGE(MCE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) EPAGERANGE(VC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) EPAGERANGE(VC2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static __always_inline bool in_exception_stack(unsigned long *stack, struct stack_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) unsigned long begin, end, stk = (unsigned long)stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) const struct estack_pages *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct pt_regs *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) BUILD_BUG_ON(N_EXCEPTION_STACKS != 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) begin = (unsigned long)__this_cpu_read(cea_exception_stacks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * Handle the case where stack trace is collected _before_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * cea_exception_stacks had been initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!begin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) end = begin + sizeof(struct cea_exception_stacks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* Bail if @stack is outside the exception stack area. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (stk < begin || stk >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* Calc page offset from start of exception stacks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) k = (stk - begin) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* Lookup the page descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ep = &estack_pages[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* Guard page? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (!ep->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) begin += (unsigned long)ep->offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) end = begin + (unsigned long)ep->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) regs = (struct pt_regs *)end - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) info->type = ep->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) info->begin = (unsigned long *)begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) info->end = (unsigned long *)end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) info->next_sp = (unsigned long *)regs->sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static __always_inline bool in_irq_stack(unsigned long *stack, struct stack_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned long *end = (unsigned long *)this_cpu_read(hardirq_stack_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) unsigned long *begin = end - (IRQ_STACK_SIZE / sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * This is a software stack, so 'end' can be a valid stack pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * It just means the stack is empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (stack < begin || stack >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) info->type = STACK_TYPE_IRQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) info->begin = begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) info->end = end;
^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) * The next stack pointer is the first thing pushed by the entry code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * after switching to the irq stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) info->next_sp = (unsigned long *)*(end - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return true;
^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) bool noinstr get_stack_info_noinstr(unsigned long *stack, struct task_struct *task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct stack_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (in_task_stack(stack, task, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (task != current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (in_exception_stack(stack, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (in_irq_stack(stack, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (in_entry_stack(stack, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int get_stack_info(unsigned long *stack, struct task_struct *task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct stack_info *info, unsigned long *visit_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) task = task ? : current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!stack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!get_stack_info_noinstr(stack, task, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) goto unknown;
^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) * Make sure we don't iterate through any given stack more than once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * If it comes up a second time then there's something wrong going on:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * just break out and report an unknown stack type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (visit_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (*visit_mask & (1UL << info->type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (task == current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) *visit_mask |= 1UL << info->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) unknown:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) info->type = STACK_TYPE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }