1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2008 CodeSourcery
3
4 This file is part of libunwind.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
24
25
26 #include "unwind_i.h"
27 #include "offsets.h"
28 #include <ucontext.h>
29
30 #ifndef UNW_REMOTE_ONLY
31
32 HIDDEN inline int
riscv_local_resume(unw_addr_space_t as,unw_cursor_t * cursor,void * arg)33 riscv_local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
34 {
35 #ifdef __linux__
36 struct cursor *c = (struct cursor *) cursor;
37 ucontext_t *uc = c->uc;
38
39 unw_word_t *mcontext = (unw_word_t*) &uc->uc_mcontext;
40 mcontext[0] = c->dwarf.ip;
41
42 if (c->sigcontext_format == RISCV_SCF_NONE)
43 {
44 /* Restore PC in RA */
45 mcontext[1] = c->dwarf.ip;
46
47 Debug (8, "resuming at ip=0x%lx via setcontext()\n", c->dwarf.ip);
48
49 setcontext(uc);
50 }
51 else
52 {
53 struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
54 unw_word_t *regs = (unw_word_t*)sc;
55
56 regs[0] = c->dwarf.ip;
57 for (int i = UNW_RISCV_X1; i <= UNW_RISCV_F31; ++i) {
58 regs[i] = mcontext[i];
59 }
60
61 Debug (8, "resuming at ip=0x%lx via sigreturn() (trampoline @ 0x%lx, sp @ 0x%lx)\n", c->dwarf.ip, c->sigcontext_pc, c->sigcontext_sp);
62
63 // Jump back to the trampoline
64 __asm__ __volatile__ (
65 "mv sp, %0\n"
66 "jr %1 \n"
67 : : "r" (c->sigcontext_sp), "r" (c->sigcontext_pc)
68 );
69 }
70
71 unreachable();
72 #else
73 # warning Implement me
74 #endif
75 return -UNW_EINVAL;
76 }
77
78 #endif /* !UNW_REMOTE_ONLY */
79
80 static inline int
establish_machine_state(struct cursor * c)81 establish_machine_state (struct cursor *c)
82 {
83 unw_addr_space_t as = c->dwarf.as;
84 void *arg = c->dwarf.as_arg;
85 unw_fpreg_t fpval;
86 unw_word_t val;
87 int reg;
88
89 Debug (8, "copying out cursor state\n");
90
91 for (reg = UNW_RISCV_X1; reg <= UNW_REG_LAST; ++reg)
92 {
93 Debug (16, "copying %s %d\n", unw_regname (reg), reg);
94 if (unw_is_fpreg (reg))
95 {
96 if (tdep_access_fpreg (c, reg, &fpval, 0) >= 0)
97 as->acc.access_fpreg (as, reg, &fpval, 1, arg);
98 }
99 else
100 {
101 if (tdep_access_reg (c, reg, &val, 0) >= 0)
102 as->acc.access_reg (as, reg, &val, 1, arg);
103 }
104 }
105
106 return 0;
107 }
108
109 int
unw_resume(unw_cursor_t * cursor)110 unw_resume (unw_cursor_t *cursor)
111 {
112 struct cursor *c = (struct cursor *) cursor;
113 int ret;
114
115 Debug (1, "(cursor=%p)\n", c);
116
117 if ((ret = establish_machine_state (c)) < 0)
118 return ret;
119
120 return (*c->dwarf.as->acc.resume) (c->dwarf.as, (unw_cursor_t *)c,
121 c->dwarf.as_arg);
122 }
123