2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "user-internals.h"
23 #include "signal-common.h"
24 #include "linux-user/trace.h"
26 struct target_sigcontext
{
27 target_ulong r0
, r1
, r2
, r3
;
28 target_ulong r4
, r5
, r6
, r7
;
29 target_ulong r8
, r9
, r10
, r11
;
30 target_ulong r12
, r13
, r14
, r15
;
31 target_ulong r16
, r17
, r18
, r19
;
32 target_ulong r20
, r21
, r22
, r23
;
33 target_ulong r24
, r25
, r26
, r27
;
34 target_ulong r28
, r29
, r30
, r31
;
47 target_ulong pred
[NUM_PREGS
];
50 struct target_ucontext
{
51 unsigned long uc_flags
;
52 target_ulong uc_link
; /* target pointer */
53 target_stack_t uc_stack
;
54 struct target_sigcontext uc_mcontext
;
55 target_sigset_t uc_sigmask
;
58 struct target_rt_sigframe
{
60 struct target_siginfo info
;
61 struct target_ucontext uc
;
64 static abi_ulong
get_sigframe(struct target_sigaction
*ka
,
65 CPUHexagonState
*regs
, size_t framesize
)
67 abi_ulong sp
= get_sp_from_cpustate(regs
);
69 /* This is the X/Open sanctioned signal stack switching. */
70 sp
= target_sigsp(sp
, ka
) - framesize
;
72 sp
= QEMU_ALIGN_DOWN(sp
, 8);
77 static void setup_sigcontext(struct target_sigcontext
*sc
, CPUHexagonState
*env
)
79 __put_user(env
->gpr
[HEX_REG_R00
], &sc
->r0
);
80 __put_user(env
->gpr
[HEX_REG_R01
], &sc
->r1
);
81 __put_user(env
->gpr
[HEX_REG_R02
], &sc
->r2
);
82 __put_user(env
->gpr
[HEX_REG_R03
], &sc
->r3
);
83 __put_user(env
->gpr
[HEX_REG_R04
], &sc
->r4
);
84 __put_user(env
->gpr
[HEX_REG_R05
], &sc
->r5
);
85 __put_user(env
->gpr
[HEX_REG_R06
], &sc
->r6
);
86 __put_user(env
->gpr
[HEX_REG_R07
], &sc
->r7
);
87 __put_user(env
->gpr
[HEX_REG_R08
], &sc
->r8
);
88 __put_user(env
->gpr
[HEX_REG_R09
], &sc
->r9
);
89 __put_user(env
->gpr
[HEX_REG_R10
], &sc
->r10
);
90 __put_user(env
->gpr
[HEX_REG_R11
], &sc
->r11
);
91 __put_user(env
->gpr
[HEX_REG_R12
], &sc
->r12
);
92 __put_user(env
->gpr
[HEX_REG_R13
], &sc
->r13
);
93 __put_user(env
->gpr
[HEX_REG_R14
], &sc
->r14
);
94 __put_user(env
->gpr
[HEX_REG_R15
], &sc
->r15
);
95 __put_user(env
->gpr
[HEX_REG_R16
], &sc
->r16
);
96 __put_user(env
->gpr
[HEX_REG_R17
], &sc
->r17
);
97 __put_user(env
->gpr
[HEX_REG_R18
], &sc
->r18
);
98 __put_user(env
->gpr
[HEX_REG_R19
], &sc
->r19
);
99 __put_user(env
->gpr
[HEX_REG_R20
], &sc
->r20
);
100 __put_user(env
->gpr
[HEX_REG_R21
], &sc
->r21
);
101 __put_user(env
->gpr
[HEX_REG_R22
], &sc
->r22
);
102 __put_user(env
->gpr
[HEX_REG_R23
], &sc
->r23
);
103 __put_user(env
->gpr
[HEX_REG_R24
], &sc
->r24
);
104 __put_user(env
->gpr
[HEX_REG_R25
], &sc
->r25
);
105 __put_user(env
->gpr
[HEX_REG_R26
], &sc
->r26
);
106 __put_user(env
->gpr
[HEX_REG_R27
], &sc
->r27
);
107 __put_user(env
->gpr
[HEX_REG_R28
], &sc
->r28
);
108 __put_user(env
->gpr
[HEX_REG_R29
], &sc
->r29
);
109 __put_user(env
->gpr
[HEX_REG_R30
], &sc
->r30
);
110 __put_user(env
->gpr
[HEX_REG_R31
], &sc
->r31
);
111 __put_user(env
->gpr
[HEX_REG_SA0
], &sc
->sa0
);
112 __put_user(env
->gpr
[HEX_REG_LC0
], &sc
->lc0
);
113 __put_user(env
->gpr
[HEX_REG_SA1
], &sc
->sa1
);
114 __put_user(env
->gpr
[HEX_REG_LC1
], &sc
->lc1
);
115 __put_user(env
->gpr
[HEX_REG_M0
], &sc
->m0
);
116 __put_user(env
->gpr
[HEX_REG_M1
], &sc
->m1
);
117 __put_user(env
->gpr
[HEX_REG_USR
], &sc
->usr
);
118 __put_user(env
->gpr
[HEX_REG_GP
], &sc
->gp
);
119 __put_user(env
->gpr
[HEX_REG_UGP
], &sc
->ugp
);
120 __put_user(env
->gpr
[HEX_REG_PC
], &sc
->pc
);
123 for (i
= 0; i
< NUM_PREGS
; i
++) {
124 __put_user(env
->pred
[i
], &(sc
->pred
[i
]));
128 static void setup_ucontext(struct target_ucontext
*uc
,
129 CPUHexagonState
*env
, target_sigset_t
*set
)
131 __put_user(0, &(uc
->uc_flags
));
132 __put_user(0, &(uc
->uc_link
));
134 target_save_altstack(&uc
->uc_stack
, env
);
137 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
138 __put_user(set
->sig
[i
], &(uc
->uc_sigmask
.sig
[i
]));
141 setup_sigcontext(&uc
->uc_mcontext
, env
);
144 static inline void install_sigtramp(uint32_t *tramp
)
146 __put_user(0x7800d166, tramp
+ 0); /* { r6=#__NR_rt_sigreturn } */
147 __put_user(0x5400c004, tramp
+ 1); /* { trap0(#1) } */
150 void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
151 target_siginfo_t
*info
,
152 target_sigset_t
*set
, CPUHexagonState
*env
)
154 abi_ulong frame_addr
;
155 struct target_rt_sigframe
*frame
;
157 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
158 trace_user_setup_rt_frame(env
, frame_addr
);
160 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
164 setup_ucontext(&frame
->uc
, env
, set
);
167 * The on-stack signal trampoline is no longer executed;
168 * however, the libgcc signal frame unwinding code checks
169 * for the presence of these two numeric magic values.
171 install_sigtramp(frame
->tramp
);
173 env
->gpr
[HEX_REG_PC
] = ka
->_sa_handler
;
174 env
->gpr
[HEX_REG_SP
] = frame_addr
;
175 env
->gpr
[HEX_REG_R00
] = sig
;
176 env
->gpr
[HEX_REG_R01
] =
177 frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
178 env
->gpr
[HEX_REG_R02
] =
179 frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
180 env
->gpr
[HEX_REG_LR
] = default_rt_sigreturn
;
185 unlock_user_struct(frame
, frame_addr
, 1);
186 if (sig
== TARGET_SIGSEGV
) {
187 ka
->_sa_handler
= TARGET_SIG_DFL
;
189 force_sig(TARGET_SIGSEGV
);
192 static void restore_sigcontext(CPUHexagonState
*env
,
193 struct target_sigcontext
*sc
)
195 __get_user(env
->gpr
[HEX_REG_R00
], &sc
->r0
);
196 __get_user(env
->gpr
[HEX_REG_R01
], &sc
->r1
);
197 __get_user(env
->gpr
[HEX_REG_R02
], &sc
->r2
);
198 __get_user(env
->gpr
[HEX_REG_R03
], &sc
->r3
);
199 __get_user(env
->gpr
[HEX_REG_R04
], &sc
->r4
);
200 __get_user(env
->gpr
[HEX_REG_R05
], &sc
->r5
);
201 __get_user(env
->gpr
[HEX_REG_R06
], &sc
->r6
);
202 __get_user(env
->gpr
[HEX_REG_R07
], &sc
->r7
);
203 __get_user(env
->gpr
[HEX_REG_R08
], &sc
->r8
);
204 __get_user(env
->gpr
[HEX_REG_R09
], &sc
->r9
);
205 __get_user(env
->gpr
[HEX_REG_R10
], &sc
->r10
);
206 __get_user(env
->gpr
[HEX_REG_R11
], &sc
->r11
);
207 __get_user(env
->gpr
[HEX_REG_R12
], &sc
->r12
);
208 __get_user(env
->gpr
[HEX_REG_R13
], &sc
->r13
);
209 __get_user(env
->gpr
[HEX_REG_R14
], &sc
->r14
);
210 __get_user(env
->gpr
[HEX_REG_R15
], &sc
->r15
);
211 __get_user(env
->gpr
[HEX_REG_R16
], &sc
->r16
);
212 __get_user(env
->gpr
[HEX_REG_R17
], &sc
->r17
);
213 __get_user(env
->gpr
[HEX_REG_R18
], &sc
->r18
);
214 __get_user(env
->gpr
[HEX_REG_R19
], &sc
->r19
);
215 __get_user(env
->gpr
[HEX_REG_R20
], &sc
->r20
);
216 __get_user(env
->gpr
[HEX_REG_R21
], &sc
->r21
);
217 __get_user(env
->gpr
[HEX_REG_R22
], &sc
->r22
);
218 __get_user(env
->gpr
[HEX_REG_R23
], &sc
->r23
);
219 __get_user(env
->gpr
[HEX_REG_R24
], &sc
->r24
);
220 __get_user(env
->gpr
[HEX_REG_R25
], &sc
->r25
);
221 __get_user(env
->gpr
[HEX_REG_R26
], &sc
->r26
);
222 __get_user(env
->gpr
[HEX_REG_R27
], &sc
->r27
);
223 __get_user(env
->gpr
[HEX_REG_R28
], &sc
->r28
);
224 __get_user(env
->gpr
[HEX_REG_R29
], &sc
->r29
);
225 __get_user(env
->gpr
[HEX_REG_R30
], &sc
->r30
);
226 __get_user(env
->gpr
[HEX_REG_R31
], &sc
->r31
);
227 __get_user(env
->gpr
[HEX_REG_SA0
], &sc
->sa0
);
228 __get_user(env
->gpr
[HEX_REG_LC0
], &sc
->lc0
);
229 __get_user(env
->gpr
[HEX_REG_SA1
], &sc
->sa1
);
230 __get_user(env
->gpr
[HEX_REG_LC1
], &sc
->lc1
);
231 __get_user(env
->gpr
[HEX_REG_M0
], &sc
->m0
);
232 __get_user(env
->gpr
[HEX_REG_M1
], &sc
->m1
);
233 __get_user(env
->gpr
[HEX_REG_USR
], &sc
->usr
);
234 __get_user(env
->gpr
[HEX_REG_GP
], &sc
->gp
);
235 __get_user(env
->gpr
[HEX_REG_UGP
], &sc
->ugp
);
236 __get_user(env
->gpr
[HEX_REG_PC
], &sc
->pc
);
239 for (i
= 0; i
< NUM_PREGS
; i
++) {
240 __get_user(env
->pred
[i
], &(sc
->pred
[i
]));
244 static void restore_ucontext(CPUHexagonState
*env
, struct target_ucontext
*uc
)
247 target_sigset_t target_set
;
250 target_sigemptyset(&target_set
);
251 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
252 __get_user(target_set
.sig
[i
], &(uc
->uc_sigmask
.sig
[i
]));
255 target_to_host_sigset_internal(&blocked
, &target_set
);
256 set_sigmask(&blocked
);
258 restore_sigcontext(env
, &uc
->uc_mcontext
);
261 long do_rt_sigreturn(CPUHexagonState
*env
)
263 struct target_rt_sigframe
*frame
;
264 abi_ulong frame_addr
;
266 frame_addr
= env
->gpr
[HEX_REG_SP
];
267 trace_user_do_sigreturn(env
, frame_addr
);
268 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
272 restore_ucontext(env
, &frame
->uc
);
273 target_restore_altstack(&frame
->uc
.uc_stack
, env
);
275 unlock_user_struct(frame
, frame_addr
, 0);
276 return -QEMU_ESIGRETURN
;
279 unlock_user_struct(frame
, frame_addr
, 0);
280 force_sig(TARGET_SIGSEGV
);
284 void setup_sigtramp(abi_ulong sigtramp_page
)
286 uint32_t *tramp
= lock_user(VERIFY_WRITE
, sigtramp_page
, 4 * 2, 0);
287 assert(tramp
!= NULL
);
289 default_rt_sigreturn
= sigtramp_page
;
290 install_sigtramp(tramp
);
292 unlock_user(tramp
, sigtramp_page
, 4 * 2);