2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
21 #include "target_signal.h"
22 #include "signal-common.h"
23 #include "linux-user/trace.h"
25 struct target_sigcontext
{
26 struct target_pt_regs regs
;
31 struct target_ucontext
{
34 target_stack_t tuc_stack
;
35 struct target_sigcontext tuc_mcontext
;
36 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
39 struct target_rt_sigframe
{
42 struct target_siginfo info
;
43 struct target_sigcontext sc
;
44 struct target_ucontext uc
;
45 unsigned char retcode
[16]; /* trampoline code */
48 /* This is the asm-generic/ucontext.h version */
50 static int restore_sigcontext(CPUOpenRISCState
*regs
,
51 struct target_sigcontext
*sc
)
54 unsigned long old_usp
;
56 /* Alwys make any pending restarted system call return -EINTR */
57 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
59 /* restore the regs from &sc->regs (same as sc, since regs is first)
60 * (sc is already checked for VERIFY_READ since the sigframe was
61 * checked in sys_sigreturn previously)
64 if (copy_from_user(regs
, &sc
, sizeof(struct target_pt_regs
))) {
68 /* make sure the U-flag is set so user-mode cannot fool us */
72 /* restore the old USP as it was before we stacked the sc etc.
73 * (we cannot just pop the sigcontext since we aligned the sp and
74 * stuff after pushing it)
77 __get_user(old_usp
, &sc
->usp
);
78 phx_signal("old_usp 0x%lx", old_usp
);
80 __PHX__ REALLY
/* ??? */
82 regs
->gpr
[1] = old_usp
;
84 /* TODO: the other ports use regs->orig_XX to disable syscall checks
85 * after this completes, but we don't use that mechanism. maybe we can
96 /* Set up a signal frame. */
98 static void setup_sigcontext(struct target_sigcontext
*sc
,
99 CPUOpenRISCState
*regs
,
102 unsigned long usp
= cpu_get_gpr(regs
, 1);
104 /* copy the regs. they are first in sc so we can use sc directly */
106 /*copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
108 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
109 the signal handler. The frametype will be restored to its previous
110 value in restore_sigcontext. */
111 /*regs->frametype = CRIS_FRAME_NORMAL;*/
113 /* then some other stuff */
114 __put_user(mask
, &sc
->oldmask
);
115 __put_user(usp
, &sc
->usp
);
118 static inline unsigned long align_sigframe(unsigned long sp
)
123 static inline abi_ulong
get_sigframe(struct target_sigaction
*ka
,
124 CPUOpenRISCState
*regs
,
127 unsigned long sp
= get_sp_from_cpustate(regs
);
128 int onsigstack
= on_sig_stack(sp
);
131 sp
= target_sigsp(sp
, ka
);
133 sp
= align_sigframe(sp
- frame_size
);
136 * If we are on the alternate signal stack and would overflow it, don't.
137 * Return an always-bogus address instead so we will die with SIGSEGV.
140 if (onsigstack
&& !likely(on_sig_stack(sp
))) {
147 void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
148 target_siginfo_t
*info
,
149 target_sigset_t
*set
, CPUOpenRISCState
*env
)
152 abi_ulong frame_addr
;
153 unsigned long return_ip
;
154 struct target_rt_sigframe
*frame
;
155 abi_ulong info_addr
, uc_addr
;
157 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
158 trace_user_setup_rt_frame(env
, frame_addr
);
159 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
163 info_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
164 __put_user(info_addr
, &frame
->pinfo
);
165 uc_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
166 __put_user(uc_addr
, &frame
->puc
);
168 if (ka
->sa_flags
& SA_SIGINFO
) {
169 tswap_siginfo(&frame
->info
, info
);
172 /*err |= __clear_user(&frame->uc, offsetof(ucontext_t, uc_mcontext));*/
173 __put_user(0, &frame
->uc
.tuc_flags
);
174 __put_user(0, &frame
->uc
.tuc_link
);
175 target_save_altstack(&frame
->uc
.tuc_stack
, env
);
176 setup_sigcontext(&frame
->sc
, env
, set
->sig
[0]);
178 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
180 /* trampoline - the desired return ip is the retcode itself */
181 return_ip
= (unsigned long)&frame
->retcode
;
182 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
183 __put_user(0xa960, (short *)(frame
->retcode
+ 0));
184 __put_user(TARGET_NR_rt_sigreturn
, (short *)(frame
->retcode
+ 2));
185 __put_user(0x20000001, (unsigned long *)(frame
->retcode
+ 4));
186 __put_user(0x15000000, (unsigned long *)(frame
->retcode
+ 8));
192 /* TODO what is the current->exec_domain stuff and invmap ? */
194 /* Set up registers for signal handler */
195 env
->pc
= (unsigned long)ka
->_sa_handler
; /* what we enter NOW */
196 cpu_set_gpr(env
, 9, (unsigned long)return_ip
); /* what we enter LATER */
197 cpu_set_gpr(env
, 3, (unsigned long)sig
); /* arg 1: signo */
198 cpu_set_gpr(env
, 4, (unsigned long)&frame
->info
); /* arg 2: (siginfo_t*) */
199 cpu_set_gpr(env
, 5, (unsigned long)&frame
->uc
); /* arg 3: ucontext */
201 /* actually move the usp to reflect the stacked frame */
202 cpu_set_gpr(env
, 1, (unsigned long)frame
);
207 unlock_user_struct(frame
, frame_addr
, 1);
211 long do_sigreturn(CPUOpenRISCState
*env
)
213 trace_user_do_sigreturn(env
, 0);
214 fprintf(stderr
, "do_sigreturn: not implemented\n");
215 return -TARGET_ENOSYS
;
218 long do_rt_sigreturn(CPUOpenRISCState
*env
)
220 trace_user_do_rt_sigreturn(env
, 0);
221 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
222 return -TARGET_ENOSYS
;