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 | */ |
Russell King | 24c66df | 2016-11-15 13:55:59 +0000 | [diff] [blame^] | 13 | #include <linux/kern_levels.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/linkage.h> |
| 15 | #include <asm/assembler.h> |
| 16 | .text |
| 17 | |
| 18 | @ fp is 0 or stack frame |
| 19 | |
| 20 | #define frame r4 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 21 | #define sv_fp r5 |
| 22 | #define sv_pc r6 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #define mask r7 |
| 24 | #define offset r8 |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | ENTRY(c_backtrace) |
| 27 | |
Malcolm Parsons | 3ee357f | 2006-03-25 21:58:03 +0000 | [diff] [blame] | 28 | #if !defined(CONFIG_FRAME_POINTER) || !defined(CONFIG_PRINTK) |
Russell King | 6ebbf2c | 2014-06-30 16:29:12 +0100 | [diff] [blame] | 29 | ret lr |
Catalin Marinas | 93ed397 | 2008-08-28 11:22:32 +0100 | [diff] [blame] | 30 | ENDPROC(c_backtrace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | 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] | 33 | movs frame, r0 @ if frame pointer is zero |
| 34 | beq no_frame @ we have no stack frames |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 36 | tst r1, #0x10 @ 26 or 32-bit mode? |
Catalin Marinas | 8b59278 | 2009-07-24 12:32:57 +0100 | [diff] [blame] | 37 | ARM( moveq mask, #0xfc000003 ) |
| 38 | THUMB( moveq mask, #0xfc000000 ) |
| 39 | THUMB( orreq mask, #0x03 ) |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 40 | movne mask, #0 @ mask for 32-bit |
| 41 | |
| 42 | 1: stmfd sp!, {pc} @ calculate offset of PC stored |
| 43 | ldr r0, [sp], #4 @ by stmfd for this CPU |
| 44 | adr r1, 1b |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | sub offset, r0, r1 |
| 46 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 47 | /* |
| 48 | * Stack frame layout: |
| 49 | * optionally saved caller registers (r4 - r10) |
| 50 | * saved fp |
| 51 | * saved sp |
| 52 | * saved lr |
| 53 | * frame => saved pc |
| 54 | * optionally saved arguments (r0 - r3) |
| 55 | * saved sp => <next word> |
| 56 | * |
| 57 | * Functions start with the following code sequence: |
| 58 | * mov ip, sp |
| 59 | * stmfd sp!, {r0 - r3} (optional) |
| 60 | * corrected pc => stmfd sp!, {..., fp, ip, lr, pc} |
| 61 | */ |
| 62 | for_each_frame: tst frame, mask @ Check for address exceptions |
| 63 | bne no_frame |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 65 | 1001: ldr sv_pc, [frame, #0] @ get saved pc |
| 66 | 1002: ldr sv_fp, [frame, #-12] @ get saved fp |
| 67 | |
| 68 | sub sv_pc, sv_pc, offset @ Correct PC for prefetching |
| 69 | bic sv_pc, sv_pc, mask @ mask PC/LR for the mode |
| 70 | |
| 71 | 1003: ldr r2, [sv_pc, #-4] @ if stmfd sp!, {args} exists, |
| 72 | ldr r3, .Ldsi+4 @ adjust saved 'pc' back one |
| 73 | teq r3, r2, lsr #10 @ instruction |
| 74 | subne r0, sv_pc, #4 @ allow for mov |
| 75 | subeq r0, sv_pc, #8 @ allow for mov + stmia |
| 76 | |
| 77 | ldr r1, [frame, #-4] @ get saved lr |
| 78 | mov r2, frame |
| 79 | bic r1, r1, mask @ mask PC/LR for the mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | bl dump_backtrace_entry |
| 81 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 82 | ldr r1, [sv_pc, #-4] @ if stmfd sp!, {args} exists, |
| 83 | ldr r3, .Ldsi+4 |
Russell King | ef41b5c | 2013-10-20 15:34:10 +0100 | [diff] [blame] | 84 | teq r3, r1, lsr #11 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 85 | ldreq r0, [frame, #-8] @ get sp |
| 86 | subeq r0, r0, #4 @ point at the last arg |
Russell King | 24c66df | 2016-11-15 13:55:59 +0000 | [diff] [blame^] | 87 | bleq dump_backtrace_stm @ dump saved registers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 89 | 1004: ldr r1, [sv_pc, #0] @ if stmfd sp!, {..., fp, ip, lr, pc} |
| 90 | ldr r3, .Ldsi @ instruction exists, |
Russell King | ef41b5c | 2013-10-20 15:34:10 +0100 | [diff] [blame] | 91 | teq r3, r1, lsr #11 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 92 | subeq r0, frame, #16 |
Russell King | 24c66df | 2016-11-15 13:55:59 +0000 | [diff] [blame^] | 93 | bleq dump_backtrace_stm @ dump saved registers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 95 | teq sv_fp, #0 @ zero saved fp means |
| 96 | beq no_frame @ no further frames |
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 | cmp sv_fp, frame @ next frame must be |
| 99 | mov frame, sv_fp @ above the current frame |
| 100 | bhi for_each_frame |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 102 | 1006: adr r0, .Lbad |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | mov r1, frame |
| 104 | bl printk |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 105 | no_frame: ldmfd sp!, {r4 - r8, pc} |
Catalin Marinas | 93ed397 | 2008-08-28 11:22:32 +0100 | [diff] [blame] | 106 | ENDPROC(c_backtrace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 108 | .pushsection __ex_table,"a" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | .align 3 |
Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 110 | .long 1001b, 1006b |
| 111 | .long 1002b, 1006b |
| 112 | .long 1003b, 1006b |
| 113 | .long 1004b, 1006b |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 114 | .popsection |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | .Lbad: .asciz "Backtrace aborted due to bad frame pointer <%p>\n" |
| 117 | .align |
Russell King | ef41b5c | 2013-10-20 15:34:10 +0100 | [diff] [blame] | 118 | .Ldsi: .word 0xe92dd800 >> 11 @ stmfd sp!, {... fp, ip, lr, pc} |
| 119 | .word 0xe92d0000 >> 11 @ stmfd sp!, {} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | #endif |