blob: 582925238d65ea261cc126078462ce2a66421549 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/arch/arm/lib/backtrace.S
4 *
5 * Copyright (C) 1995, 1996 Russell King
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 27/03/03 Ian Molton Clean up CONFIG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Russell King24c66df2016-11-15 13:55:59 +00009#include <linux/kern_levels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/linkage.h>
11#include <asm/assembler.h>
12 .text
13
14@ fp is 0 or stack frame
15
16#define frame r4
Russell King7ab3f8d2007-03-02 15:01:36 +000017#define sv_fp r5
18#define sv_pc r6
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define mask r7
20#define offset r8
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022ENTRY(c_backtrace)
23
Malcolm Parsons3ee357f2006-03-25 21:58:03 +000024#if !defined(CONFIG_FRAME_POINTER) || !defined(CONFIG_PRINTK)
Russell King6ebbf2c2014-06-30 16:29:12 +010025 ret lr
Catalin Marinas93ed3972008-08-28 11:22:32 +010026ENDPROC(c_backtrace)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 stmfd sp!, {r4 - r8, lr} @ Save an extra register so we have a location...
Russell King7ab3f8d2007-03-02 15:01:36 +000029 movs frame, r0 @ if frame pointer is zero
30 beq no_frame @ we have no stack frames
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Russell King7ab3f8d2007-03-02 15:01:36 +000032 tst r1, #0x10 @ 26 or 32-bit mode?
Catalin Marinas8b592782009-07-24 12:32:57 +010033 ARM( moveq mask, #0xfc000003 )
34 THUMB( moveq mask, #0xfc000000 )
35 THUMB( orreq mask, #0x03 )
Russell King7ab3f8d2007-03-02 15:01:36 +000036 movne mask, #0 @ mask for 32-bit
37
381: stmfd sp!, {pc} @ calculate offset of PC stored
39 ldr r0, [sp], #4 @ by stmfd for this CPU
40 adr r1, 1b
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 sub offset, r0, r1
42
Russell King7ab3f8d2007-03-02 15:01:36 +000043/*
44 * Stack frame layout:
45 * optionally saved caller registers (r4 - r10)
46 * saved fp
47 * saved sp
48 * saved lr
49 * frame => saved pc
50 * optionally saved arguments (r0 - r3)
51 * saved sp => <next word>
52 *
53 * Functions start with the following code sequence:
54 * mov ip, sp
55 * stmfd sp!, {r0 - r3} (optional)
56 * corrected pc => stmfd sp!, {..., fp, ip, lr, pc}
57 */
58for_each_frame: tst frame, mask @ Check for address exceptions
59 bne no_frame
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Russell King7ab3f8d2007-03-02 15:01:36 +0000611001: ldr sv_pc, [frame, #0] @ get saved pc
621002: ldr sv_fp, [frame, #-12] @ get saved fp
63
64 sub sv_pc, sv_pc, offset @ Correct PC for prefetching
65 bic sv_pc, sv_pc, mask @ mask PC/LR for the mode
66
671003: ldr r2, [sv_pc, #-4] @ if stmfd sp!, {args} exists,
68 ldr r3, .Ldsi+4 @ adjust saved 'pc' back one
Lvqiang Huang69389832019-08-01 08:15:23 +010069 teq r3, r2, lsr #11 @ instruction
Russell King7ab3f8d2007-03-02 15:01:36 +000070 subne r0, sv_pc, #4 @ allow for mov
71 subeq r0, sv_pc, #8 @ allow for mov + stmia
72
73 ldr r1, [frame, #-4] @ get saved lr
74 mov r2, frame
75 bic r1, r1, mask @ mask PC/LR for the mode
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 bl dump_backtrace_entry
77
Russell King7ab3f8d2007-03-02 15:01:36 +000078 ldr r1, [sv_pc, #-4] @ if stmfd sp!, {args} exists,
79 ldr r3, .Ldsi+4
Russell Kingef41b5c2013-10-20 15:34:10 +010080 teq r3, r1, lsr #11
Russell King7ab3f8d2007-03-02 15:01:36 +000081 ldreq r0, [frame, #-8] @ get sp
82 subeq r0, r0, #4 @ point at the last arg
Russell King24c66df2016-11-15 13:55:59 +000083 bleq dump_backtrace_stm @ dump saved registers
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Russell King7ab3f8d2007-03-02 15:01:36 +0000851004: ldr r1, [sv_pc, #0] @ if stmfd sp!, {..., fp, ip, lr, pc}
86 ldr r3, .Ldsi @ instruction exists,
Russell Kingef41b5c2013-10-20 15:34:10 +010087 teq r3, r1, lsr #11
Russell King7ab3f8d2007-03-02 15:01:36 +000088 subeq r0, frame, #16
Russell King24c66df2016-11-15 13:55:59 +000089 bleq dump_backtrace_stm @ dump saved registers
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Russell King7ab3f8d2007-03-02 15:01:36 +000091 teq sv_fp, #0 @ zero saved fp means
92 beq no_frame @ no further frames
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Russell King7ab3f8d2007-03-02 15:01:36 +000094 cmp sv_fp, frame @ next frame must be
95 mov frame, sv_fp @ above the current frame
96 bhi for_each_frame
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Russell King7ab3f8d2007-03-02 15:01:36 +0000981006: adr r0, .Lbad
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 mov r1, frame
100 bl printk
Russell King7ab3f8d2007-03-02 15:01:36 +0000101no_frame: ldmfd sp!, {r4 - r8, pc}
Catalin Marinas93ed3972008-08-28 11:22:32 +0100102ENDPROC(c_backtrace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Russell King42604152010-04-19 10:15:03 +0100104 .pushsection __ex_table,"a"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .align 3
Russell King7ab3f8d2007-03-02 15:01:36 +0000106 .long 1001b, 1006b
107 .long 1002b, 1006b
108 .long 1003b, 1006b
109 .long 1004b, 1006b
Russell King42604152010-04-19 10:15:03 +0100110 .popsection
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112.Lbad: .asciz "Backtrace aborted due to bad frame pointer <%p>\n"
113 .align
Russell Kingef41b5c2013-10-20 15:34:10 +0100114.Ldsi: .word 0xe92dd800 >> 11 @ stmfd sp!, {... fp, ip, lr, pc}
115 .word 0xe92d0000 >> 11 @ stmfd sp!, {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117#endif