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 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 /* Update the dwarf cursor.
74 Set the location of the registers to the corresponding addresses of the
75 uc_mcontext / sigcontext structure contents. */
76 #define ROFF(n) (FREEBSD_SC_UCONTEXT_OFF + FREEBSD_UC_MCONTEXT_OFF + \
77 FREEBSD_MC_R0_OFF + (n) * 4)
78 #define SL(n) \
79 c->dwarf.loc[UNW_ARM_R ## n] = DWARF_LOC (sc_addr + ROFF(n), 0);
80 SL(0); SL(1); SL(2); SL(3); SL(4); SL(5); SL(6); SL(7);
81 SL(8); SL(9); SL(10); SL(11); SL(12); SL(13); SL(14); SL(15);
82 #undef SL
83 #undef ROFF
84
85 /* Set SP/CFA and PC/IP. */
86 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_ARM_R13], &c->dwarf.cfa);
87 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_ARM_R15], &c->dwarf.ip);
88
89 return 1;
90 }
91
92 /* Returns 1 in case of a non-RT signal frame and 2 in case of a RT signal
93 frame. */
94 int
unw_is_signal_frame(unw_cursor_t * cursor)95 unw_is_signal_frame (unw_cursor_t *cursor)
96 {
97 struct cursor *c = (struct cursor *) cursor;
98 unw_word_t w0, w1, w2, w3, ip;
99 unw_addr_space_t as;
100 unw_accessors_t *a;
101 void *arg;
102 int ret;
103
104 as = c->dwarf.as;
105 a = unw_get_accessors_int (as);
106 arg = c->dwarf.as_arg;
107
108 ip = c->dwarf.ip;
109
110 if ((ret = (*a->access_mem) (as, ip, &w0, 0, arg)) < 0)
111 return ret;
112 if ((ret = (*a->access_mem) (as, ip + 4, &w1, 0, arg)) < 0)
113 return ret;
114 if ((ret = (*a->access_mem) (as, ip + 8, &w2, 0, arg)) < 0)
115 return ret;
116 if ((ret = (*a->access_mem) (as, ip + 12, &w3, 0, arg)) < 0)
117 return ret;
118
119 if (w0 == 0xe1a0000d && w1 == 0xe2800040 && w2 == 0xe59f700c &&
120 w3 == 0xef0001a1)
121 return UNW_ARM_FRAME_SIGRETURN;
122
123 if ((ret = (*a->access_mem) (as, ip - 4, &w0, 0, arg)) < 0)
124 return ret;
125 if (w0 == 0xef000000)
126 return UNW_ARM_FRAME_SYSCALL;
127
128 return 0;
129 }
130