1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2008 CodeSourcery
3 Copyright (C) 2021 Zhaofeng Li
4
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26 #include "unwind_i.h"
27
28 #ifdef __linux__
29
30 /*
31 The stub looks like:
32
33 addi x17, zero, 139 0x08b00893
34 ecall 0x00000073
35
36 See <https://github.com/torvalds/linux/blob/44db63d1ad8d71c6932cbe007eb41f31c434d140/arch/riscv/kernel/vdso/rt_sigreturn.S>.
37 */
38 #define SIGRETURN_I0 0x08b00893
39 #define SIGRETURN_I1 0x00000073
40
41 #endif /* __linux__ */
42
43 int
unw_is_signal_frame(unw_cursor_t * cursor)44 unw_is_signal_frame (unw_cursor_t *cursor)
45 {
46 #ifdef __linux__
47 struct cursor *c = (struct cursor*) cursor;
48 unw_word_t i0, i1, ip;
49 unw_addr_space_t as;
50 unw_accessors_t *a;
51 void *arg;
52 int ret;
53
54 as = c->dwarf.as;
55 a = unw_get_accessors_int (as);
56 arg = c->dwarf.as_arg;
57
58 ip = c->dwarf.ip;
59
60 if (!ip || !a->access_mem || (ip & (sizeof(unw_word_t) - 1)))
61 return 0;
62
63 if ((ret = (*a->access_mem) (as, ip, &i0, 0, arg)) < 0)
64 return ret;
65
66 if ((ret = (*a->access_mem) (as, ip + 4, &i1, 0, arg)) < 0)
67 return ret;
68
69 if ((i0 & 0xffffffff) == SIGRETURN_I0 && (i1 & 0xffffffff) == SIGRETURN_I1)
70 {
71 Debug (8, "cursor at signal frame\n");
72 return 1;
73 }
74
75 return 0;
76 #else
77 return -UNW_ENOINFO;
78 #endif
79 }
80