Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/lib/backtrace.S |
| 3 | * |
| 4 | * Copyright (C) 1995, 1996 Russell King |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * 27/03/03 Ian Molton Clean up CONFIG_CPU |
| 11 | * |
| 12 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/linkage.h> |
| 14 | #include <asm/assembler.h> |
| 15 | .text |
| 16 | |
| 17 | @ fp is 0 or stack frame |
| 18 | |
| 19 | #define frame r4 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 20 | #define sv_fp r5 |
| 21 | #define sv_pc r6 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #define mask r7 |
| 23 | #define offset r8 |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | ENTRY(c_backtrace) |
| 26 | |
Malcolm Parsons | 3ee357f | 2006-03-25 21:58:03 +0000 | [diff] [blame] | 27 | #if !defined(CONFIG_FRAME_POINTER) || !defined(CONFIG_PRINTK) |
Russell King | 6ebbf2c | 2014-06-30 16:29:12 +0100 | [diff] [blame] | 28 | ret lr |
Catalin Marinas | 93ed397 | 2008-08-28 11:22:32 +0100 | [diff] [blame] | 29 | ENDPROC(c_backtrace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | 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] | 32 | movs frame, r0 @ if frame pointer is zero |
| 33 | beq no_frame @ we have no stack frames |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 35 | tst r1, #0x10 @ 26 or 32-bit mode? |
Catalin Marinas | 8b59278 | 2009-07-24 12:32:57 +0100 | [diff] [blame] | 36 | ARM( moveq mask, #0xfc000003 ) |
| 37 | THUMB( moveq mask, #0xfc000000 ) |
| 38 | THUMB( orreq mask, #0x03 ) |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 39 | movne mask, #0 @ mask for 32-bit |
| 40 | |
| 41 | 1: stmfd sp!, {pc} @ calculate offset of PC stored |
| 42 | ldr r0, [sp], #4 @ by stmfd for this CPU |
| 43 | adr r1, 1b |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | sub offset, r0, r1 |
| 45 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 46 | /* |
| 47 | * Stack frame layout: |
| 48 | * optionally saved caller registers (r4 - r10) |
| 49 | * saved fp |
| 50 | * saved sp |
| 51 | * saved lr |
| 52 | * frame => saved pc |
| 53 | * optionally saved arguments (r0 - r3) |
| 54 | * saved sp => <next word> |
| 55 | * |
| 56 | * Functions start with the following code sequence: |
| 57 | * mov ip, sp |
| 58 | * stmfd sp!, {r0 - r3} (optional) |
| 59 | * corrected pc => stmfd sp!, {..., fp, ip, lr, pc} |
| 60 | */ |
| 61 | for_each_frame: tst frame, mask @ Check for address exceptions |
| 62 | bne no_frame |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 64 | 1001: ldr sv_pc, [frame, #0] @ get saved pc |
| 65 | 1002: ldr sv_fp, [frame, #-12] @ get saved fp |
| 66 | |
| 67 | sub sv_pc, sv_pc, offset @ Correct PC for prefetching |
| 68 | bic sv_pc, sv_pc, mask @ mask PC/LR for the mode |
| 69 | |
| 70 | 1003: ldr r2, [sv_pc, #-4] @ if stmfd sp!, {args} exists, |
| 71 | ldr r3, .Ldsi+4 @ adjust saved 'pc' back one |
| 72 | teq r3, r2, lsr #10 @ instruction |
| 73 | subne r0, sv_pc, #4 @ allow for mov |
| 74 | subeq r0, sv_pc, #8 @ allow for mov + stmia |
| 75 | |
| 76 | ldr r1, [frame, #-4] @ get saved lr |
| 77 | mov r2, frame |
| 78 | bic r1, r1, mask @ mask PC/LR for the mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | bl dump_backtrace_entry |
| 80 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 81 | ldr r1, [sv_pc, #-4] @ if stmfd sp!, {args} exists, |
| 82 | ldr r3, .Ldsi+4 |
Russell King | ef41b5c | 2013-10-20 15:34:10 +0100 | [diff] [blame] | 83 | teq r3, r1, lsr #11 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 84 | ldreq r0, [frame, #-8] @ get sp |
| 85 | subeq r0, r0, #4 @ point at the last arg |
| 86 | bleq .Ldumpstm @ dump saved registers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 88 | 1004: ldr r1, [sv_pc, #0] @ if stmfd sp!, {..., fp, ip, lr, pc} |
| 89 | ldr r3, .Ldsi @ instruction exists, |
Russell King | ef41b5c | 2013-10-20 15:34:10 +0100 | [diff] [blame] | 90 | teq r3, r1, lsr #11 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 91 | subeq r0, frame, #16 |
| 92 | bleq .Ldumpstm @ dump saved registers |
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 | teq sv_fp, #0 @ zero saved fp means |
| 95 | beq no_frame @ no further frames |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 97 | cmp sv_fp, frame @ next frame must be |
| 98 | mov frame, sv_fp @ above the current frame |
| 99 | bhi for_each_frame |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 101 | 1006: adr r0, .Lbad |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | mov r1, frame |
| 103 | bl printk |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 104 | no_frame: ldmfd sp!, {r4 - r8, pc} |
Catalin Marinas | 93ed397 | 2008-08-28 11:22:32 +0100 | [diff] [blame] | 105 | ENDPROC(c_backtrace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 107 | .pushsection __ex_table,"a" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .align 3 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 109 | .long 1001b, 1006b |
| 110 | .long 1002b, 1006b |
| 111 | .long 1003b, 1006b |
| 112 | .long 1004b, 1006b |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 113 | .popsection |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | #define instr r4 |
| 116 | #define reg r5 |
| 117 | #define stack r6 |
| 118 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 119 | .Ldumpstm: stmfd sp!, {instr, reg, stack, r7, lr} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | mov stack, r0 |
| 121 | mov instr, r1 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 122 | mov reg, #10 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | mov r7, #0 |
| 124 | 1: mov r3, #1 |
Catalin Marinas | 8b59278 | 2009-07-24 12:32:57 +0100 | [diff] [blame] | 125 | ARM( tst instr, r3, lsl reg ) |
| 126 | THUMB( lsl r3, reg ) |
| 127 | THUMB( tst instr, r3 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | beq 2f |
| 129 | add r7, r7, #1 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 130 | teq r7, #6 |
Russell King | ef41b5c | 2013-10-20 15:34:10 +0100 | [diff] [blame] | 131 | moveq r7, #0 |
| 132 | adr r3, .Lcr |
| 133 | addne r3, r3, #1 @ skip newline |
| 134 | ldr r2, [stack], #-4 |
| 135 | mov r1, reg |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | adr r0, .Lfp |
| 137 | bl printk |
| 138 | 2: subs reg, reg, #1 |
| 139 | bpl 1b |
| 140 | teq r7, #0 |
| 141 | adrne r0, .Lcr |
| 142 | blne printk |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 143 | ldmfd sp!, {instr, reg, stack, r7, pc} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Russell King | ef41b5c | 2013-10-20 15:34:10 +0100 | [diff] [blame] | 145 | .Lfp: .asciz " r%d:%08x%s" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | .Lcr: .asciz "\n" |
| 147 | .Lbad: .asciz "Backtrace aborted due to bad frame pointer <%p>\n" |
| 148 | .align |
Russell King | ef41b5c | 2013-10-20 15:34:10 +0100 | [diff] [blame] | 149 | .Ldsi: .word 0xe92dd800 >> 11 @ stmfd sp!, {... fp, ip, lr, pc} |
| 150 | .word 0xe92d0000 >> 11 @ stmfd sp!, {} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | #endif |