Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/arch/arm/lib/backtrace.S |
| 4 | * |
| 5 | * Copyright (C) 1995, 1996 Russell King |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * 27/03/03 Ian Molton Clean up CONFIG_CPU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
Russell King | 24c66df | 2016-11-15 13:55:59 +0000 | [diff] [blame] | 9 | #include <linux/kern_levels.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #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 King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 17 | #define sv_fp r5 |
| 18 | #define sv_pc r6 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #define mask r7 |
| 20 | #define offset r8 |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | ENTRY(c_backtrace) |
| 23 | |
Malcolm Parsons | 3ee357f | 2006-03-25 21:58:03 +0000 | [diff] [blame] | 24 | #if !defined(CONFIG_FRAME_POINTER) || !defined(CONFIG_PRINTK) |
Russell King | 6ebbf2c | 2014-06-30 16:29:12 +0100 | [diff] [blame] | 25 | ret lr |
Catalin Marinas | 93ed397 | 2008-08-28 11:22:32 +0100 | [diff] [blame] | 26 | ENDPROC(c_backtrace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | stmfd sp!, {r4 - r8, lr} @ Save an extra register so we have a location... |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 29 | movs frame, r0 @ if frame pointer is zero |
| 30 | beq no_frame @ we have no stack frames |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 32 | tst r1, #0x10 @ 26 or 32-bit mode? |
Catalin Marinas | 8b59278 | 2009-07-24 12:32:57 +0100 | [diff] [blame] | 33 | ARM( moveq mask, #0xfc000003 ) |
| 34 | THUMB( moveq mask, #0xfc000000 ) |
| 35 | THUMB( orreq mask, #0x03 ) |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 36 | movne mask, #0 @ mask for 32-bit |
| 37 | |
| 38 | 1: stmfd sp!, {pc} @ calculate offset of PC stored |
| 39 | ldr r0, [sp], #4 @ by stmfd for this CPU |
| 40 | adr r1, 1b |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | sub offset, r0, r1 |
| 42 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 43 | /* |
| 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 | */ |
| 58 | for_each_frame: tst frame, mask @ Check for address exceptions |
| 59 | bne no_frame |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 61 | 1001: ldr sv_pc, [frame, #0] @ get saved pc |
| 62 | 1002: 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 | |
| 67 | 1003: ldr r2, [sv_pc, #-4] @ if stmfd sp!, {args} exists, |
| 68 | ldr r3, .Ldsi+4 @ adjust saved 'pc' back one |
Lvqiang Huang | 6938983 | 2019-08-01 08:15:23 +0100 | [diff] [blame] | 69 | teq r3, r2, lsr #11 @ instruction |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 70 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | bl dump_backtrace_entry |
| 77 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 78 | ldr r1, [sv_pc, #-4] @ if stmfd sp!, {args} exists, |
| 79 | ldr r3, .Ldsi+4 |
Russell King | ef41b5c | 2013-10-20 15:34:10 +0100 | [diff] [blame] | 80 | teq r3, r1, lsr #11 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 81 | ldreq r0, [frame, #-8] @ get sp |
| 82 | subeq r0, r0, #4 @ point at the last arg |
Russell King | 24c66df | 2016-11-15 13:55:59 +0000 | [diff] [blame] | 83 | bleq dump_backtrace_stm @ dump saved registers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 85 | 1004: ldr r1, [sv_pc, #0] @ if stmfd sp!, {..., fp, ip, lr, pc} |
| 86 | ldr r3, .Ldsi @ instruction exists, |
Russell King | ef41b5c | 2013-10-20 15:34:10 +0100 | [diff] [blame] | 87 | teq r3, r1, lsr #11 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 88 | subeq r0, frame, #16 |
Russell King | 24c66df | 2016-11-15 13:55:59 +0000 | [diff] [blame] | 89 | bleq dump_backtrace_stm @ dump saved registers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 91 | teq sv_fp, #0 @ zero saved fp means |
| 92 | beq no_frame @ no further frames |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 94 | cmp sv_fp, frame @ next frame must be |
| 95 | mov frame, sv_fp @ above the current frame |
| 96 | bhi for_each_frame |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 98 | 1006: adr r0, .Lbad |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | mov r1, frame |
| 100 | bl printk |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 101 | no_frame: ldmfd sp!, {r4 - r8, pc} |
Catalin Marinas | 93ed397 | 2008-08-28 11:22:32 +0100 | [diff] [blame] | 102 | ENDPROC(c_backtrace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 104 | .pushsection __ex_table,"a" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | .align 3 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 106 | .long 1001b, 1006b |
| 107 | .long 1002b, 1006b |
| 108 | .long 1003b, 1006b |
| 109 | .long 1004b, 1006b |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 110 | .popsection |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | .Lbad: .asciz "Backtrace aborted due to bad frame pointer <%p>\n" |
| 113 | .align |
Russell King | ef41b5c | 2013-10-20 15:34:10 +0100 | [diff] [blame] | 114 | .Ldsi: .word 0xe92dd800 >> 11 @ stmfd sp!, {... fp, ip, lr, pc} |
| 115 | .word 0xe92d0000 >> 11 @ stmfd sp!, {} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | #endif |