1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2008 CodeSourcery
3 Copyright 2011 Linaro Limited
4 Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
5 Copyright 2015 The FreeBSD Foundation
6
7 Portions of this software were developed by Konstantin Belousov
8 under sponsorship from the FreeBSD Foundation.
9
10 This file is part of libunwind.
11
12 Permission is hereby granted, free of charge, to any person obtaining
13 a copy of this software and associated documentation files (the
14 "Software"), to deal in the Software without restriction, including
15 without limitation the rights to use, copy, modify, merge, publish,
16 distribute, sublicense, and/or sell copies of the Software, and to
17 permit persons to whom the Software is furnished to do so, subject to
18 the following conditions:
19
20 The above copyright notice and this permission notice shall be
21 included in all copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
30
31 #include <stdio.h>
32 #include <signal.h>
33 #include "unwind_i.h"
34 #include "offsets.h"
35 #include "ex_tables.h"
36
37 HIDDEN int
arm_handle_signal_frame(unw_cursor_t * cursor)38 arm_handle_signal_frame (unw_cursor_t *cursor)
39 {
40 struct cursor *c = (struct cursor *) cursor;
41 int i, ret, fmt;
42 unw_word_t sc_addr, sp, sp_addr = c->dwarf.cfa;
43 struct dwarf_loc sp_loc = DWARF_LOC (sp_addr, 0);
44
45 if ((ret = dwarf_get (&c->dwarf, sp_loc, &sp)) < 0)
46 return -UNW_EUNSPEC;
47 fmt = unw_is_signal_frame(cursor);
48
49 c->dwarf.pi_valid = 0;
50
51 if (fmt == UNW_ARM_FRAME_SYSCALL)
52 {
53 c->sigcontext_format = ARM_SCF_FREEBSD_SYSCALL;
54 c->frame_info.frame_type = UNW_ARM_FRAME_SYSCALL;
55 c->frame_info.cfa_reg_offset = 0;
56 c->dwarf.loc[UNW_ARM_R7] = c->dwarf.loc[UNW_ARM_R12];
57 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_ARM_R14], &c->dwarf.ip);
58 return 1;
59 }
60
61 c->sigcontext_format = ARM_SCF_FREEBSD_SIGFRAME;
62 sc_addr = sp_addr;
63
64 /* Save the SP and PC to be able to return execution at this point
65 later in time (unw_resume). */
66 c->sigcontext_sp = c->dwarf.cfa;
67 c->sigcontext_pc = c->dwarf.ip;
68
69 c->sigcontext_addr = sc_addr;
70 c->frame_info.frame_type = UNW_ARM_FRAME_SIGRETURN;
71 c->frame_info.cfa_reg_offset = sc_addr - sp_addr;
72
73 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
74 c->dwarf.loc[i] = DWARF_NULL_LOC;
75
76 /* Update the dwarf cursor.
77 Set the location of the registers to the corresponding addresses of the
78 uc_mcontext / sigcontext structure contents. */
79 #define ROFF(n) (FREEBSD_SC_UCONTEXT_OFF + FREEBSD_UC_MCONTEXT_OFF + \
80 FREEBSD_MC_R0_OFF + (n) * 4)
81 #define SL(n) \
82 c->dwarf.loc[UNW_ARM_R ## n] = DWARF_LOC (sc_addr + ROFF(n), 0);
83 SL(0); SL(1); SL(2); SL(3); SL(4); SL(5); SL(6); SL(7);
84 SL(8); SL(9); SL(10); SL(11); SL(12); SL(13); SL(14); SL(15);
85 #undef SL
86 #undef ROFF
87
88 /* Set SP/CFA and PC/IP. */
89 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_ARM_R13], &c->dwarf.cfa);
90 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_ARM_R15], &c->dwarf.ip);
91
92 return 1;
93 }
94
95 /* Returns 1 in case of a non-RT signal frame and 2 in case of a RT signal
96 frame. */
97 int
unw_is_signal_frame(unw_cursor_t * cursor)98 unw_is_signal_frame (unw_cursor_t *cursor)
99 {
100 struct cursor *c = (struct cursor *) cursor;
101 unw_word_t w0, w1, w2, w3, ip;
102 unw_addr_space_t as;
103 unw_accessors_t *a;
104 void *arg;
105 int ret;
106
107 as = c->dwarf.as;
108 a = unw_get_accessors_int (as);
109 arg = c->dwarf.as_arg;
110
111 ip = c->dwarf.ip;
112
113 if ((ret = (*a->access_mem) (as, ip, &w0, 0, arg)) < 0)
114 return ret;
115 if ((ret = (*a->access_mem) (as, ip + 4, &w1, 0, arg)) < 0)
116 return ret;
117 if ((ret = (*a->access_mem) (as, ip + 8, &w2, 0, arg)) < 0)
118 return ret;
119 if ((ret = (*a->access_mem) (as, ip + 12, &w3, 0, arg)) < 0)
120 return ret;
121
122 if (w0 == 0xe1a0000d && w1 == 0xe2800040 && w2 == 0xe59f700c &&
123 w3 == 0xef0001a1)
124 return UNW_ARM_FRAME_SIGRETURN;
125
126 if ((ret = (*a->access_mem) (as, ip - 4, &w0, 0, arg)) < 0)
127 return ret;
128 if (w0 == 0xef000000)
129 return UNW_ARM_FRAME_SYSCALL;
130
131 return 0;
132 }
133