/* SPDX-License-Identifier: GPL-2.0-only */1/*2* linux/arch/arm/lib/backtrace-clang.S3*4* Copyright (C) 2019 Nathan Huckleberry5*6*/7#include <linux/kern_levels.h>8#include <linux/linkage.h>9#include <asm/assembler.h>10.text1112/* fp is 0 or stack frame */1314#define frame r415#define sv_fp r516#define sv_pc r617#define mask r718#define sv_lr r819#define loglvl r92021ENTRY(c_backtrace)2223#if !defined(CONFIG_FRAME_POINTER) || !defined(CONFIG_PRINTK)24ret lr25ENDPROC(c_backtrace)26#else272829/*30* Clang does not store pc or sp in function prologues so we don't know exactly31* where the function starts.32*33* We can treat the current frame's lr as the saved pc and the preceding34* frame's lr as the current frame's lr, but we can't trace the most recent35* call. Inserting a false stack frame allows us to reference the function36* called last in the stacktrace.37*38* If the call instruction was a bl we can look at the callers branch39* instruction to calculate the saved pc. We can recover the pc in most cases,40* but in cases such as calling function pointers we cannot. In this case,41* default to using the lr. This will be some address in the function, but will42* not be the function start.43*44* Unfortunately due to the stack frame layout we can't dump r0 - r3, but these45* are less frequently saved.46*47* Stack frame layout:48* <larger addresses>49* saved lr50* frame=> saved fp51* optionally saved caller registers (r4 - r10)52* optionally saved arguments (r0 - r3)53* <top of stack frame>54* <smaller addresses>55*56* Functions start with the following code sequence:57* corrected pc => stmfd sp!, {..., fp, lr}58* add fp, sp, #x59* stmfd sp!, {r0 - r3} (optional)60*61*62*63*64*65*66* The diagram below shows an example stack setup for dump_stack.67*68* The frame for c_backtrace has pointers to the code of dump_stack. This is69* why the frame of c_backtrace is used to for the pc calculation of70* dump_stack. This is why we must move back a frame to print dump_stack.71*72* The stored locals for dump_stack are in dump_stack's frame. This means that73* to fully print dump_stack's frame we need both the frame for dump_stack (for74* locals) and the frame that was called by dump_stack (for pc).75*76* To print locals we must know where the function start is. If we read the77* function prologue opcodes we can determine which variables are stored in the78* stack frame.79*80* To find the function start of dump_stack we can look at the stored LR of81* show_stack. It points at the instruction directly after the bl dump_stack.82* We can then read the offset from the bl opcode to determine where the branch83* takes us. The address calculated must be the start of dump_stack.84*85* c_backtrace frame dump_stack:86* {[LR] } ============| ...87* {[FP] } =======| | bl c_backtrace88* | |=> ...89* {[R4-R10]} |90* {[R0-R3] } | show_stack:91* dump_stack frame | ...92* {[LR] } =============| bl dump_stack93* {[FP] } <=======| |=> ...94* {[R4-R10]}95* {[R0-R3] }96*/9798stmfd sp!, {r4 - r9, fp, lr} @ Save an extra register99@ to ensure 8 byte alignment100movs frame, r0 @ if frame pointer is zero101beq no_frame @ we have no stack frames102mov loglvl, r2103tst r1, #0x10 @ 26 or 32-bit mode?104moveq mask, #0xfc000003105movne mask, #0 @ mask for 32-bit106107/*108* Switches the current frame to be the frame for dump_stack.109*/110add frame, sp, #24 @ switch to false frame111for_each_frame: tst frame, mask @ Check for address exceptions112bne no_frame113114/*115* sv_fp is the stack frame with the locals for the current considered116* function.117*118* sv_pc is the saved lr frame the frame above. This is a pointer to a code119* address within the current considered function, but it is not the function120* start. This value gets updated to be the function start later if it is121* possible.122*/1231001: ldr sv_pc, [frame, #4] @ get saved 'pc'1241002: ldr sv_fp, [frame, #0] @ get saved fp125126teq sv_fp, mask @ make sure next frame exists127beq no_frame128129/*130* sv_lr is the lr from the function that called the current function. This is131* a pointer to a code address in the current function's caller. sv_lr-4 is132* the instruction used to call the current function.133*134* This sv_lr can be used to calculate the function start if the function was135* called using a bl instruction. If the function start can be recovered sv_pc136* is overwritten with the function start.137*138* If the current function was called using a function pointer we cannot139* recover the function start and instead continue with sv_pc as an arbitrary140* value within the current function. If this is the case we cannot print141* registers for the current function, but the stacktrace is still printed142* properly.143*/1441003: ldr sv_lr, [sv_fp, #4] @ get saved lr from next frame1451461004: ldr r0, [sv_lr, #-4] @ get call instruction147ldr r3, .Lopcode+4148and r2, r3, r0 @ is this a bl call149teq r2, r3150bne finished_setup @ give up if it's not151and r0, #0xffffff @ get call offset 24-bit int152lsl r0, r0, #8 @ sign extend offset153asr r0, r0, #8154ldr sv_pc, [sv_fp, #4] @ get lr address155add sv_pc, sv_pc, #-4 @ get call instruction address156add sv_pc, sv_pc, #8 @ take care of prefetch157add sv_pc, sv_pc, r0, lsl #2@ find function start158159finished_setup:160161bic sv_pc, sv_pc, mask @ mask PC/LR for the mode162163/*164* Print the function (sv_pc) and where it was called from (sv_lr).165*/166mov r0, sv_pc167168mov r1, sv_lr169mov r2, frame170bic r1, r1, mask @ mask PC/LR for the mode171mov r3, loglvl172bl dump_backtrace_entry173174/*175* Test if the function start is a stmfd instruction to determine which176* registers were stored in the function prologue.177*178* If we could not recover the sv_pc because we were called through a function179* pointer the comparison will fail and no registers will print. Unwinding will180* continue as if there had been no registers stored in this frame.181*/1821005: ldr r1, [sv_pc, #0] @ if stmfd sp!, {..., fp, lr}183ldr r3, .Lopcode @ instruction exists,184teq r3, r1, lsr #11185ldr r0, [frame] @ locals are stored in186@ the preceding frame187subeq r0, r0, #4188mov r2, loglvl189bleq dump_backtrace_stm @ dump saved registers190191/*192* If we are out of frames or if the next frame is invalid.193*/194teq sv_fp, #0 @ zero saved fp means195beq no_frame @ no further frames196197cmp sv_fp, frame @ next frame must be198mov frame, sv_fp @ above the current frame199#ifdef CONFIG_IRQSTACKS200@201@ Kernel stacks may be discontiguous in memory. If the next202@ frame is below the previous frame, accept it as long as it203@ lives in kernel memory.204@205cmpls sv_fp, #PAGE_OFFSET206#endif207bhi for_each_frame2082091006: adr r0, .Lbad210mov r1, loglvl211mov r2, frame212bl _printk213no_frame: ldmfd sp!, {r4 - r9, fp, pc}214ENDPROC(c_backtrace)215.pushsection __ex_table,"a"216.align 3217.long 1001b, 1006b218.long 1002b, 1006b219.long 1003b, 1006b220.long 1004b, finished_setup221.long 1005b, 1006b222.popsection223224.Lbad: .asciz "%sBacktrace aborted due to bad frame pointer <%p>\n"225.align226.Lopcode: .word 0xe92d4800 >> 11 @ stmfd sp!, {... fp, lr}227.word 0x0b000000 @ bl if these bits are set228229#endif230231232