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 "signal-common.h"
22 #include "linux-user/trace.h"
24 #define MCONTEXT_VERSION 2
26 struct target_sigcontext
{
28 unsigned long gregs
[32];
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
{
40 struct target_siginfo info
;
41 struct target_ucontext uc
;
44 static int rt_setup_ucontext(struct target_ucontext
*uc
, CPUNios2State
*env
)
46 unsigned long *gregs
= uc
->tuc_mcontext
.gregs
;
48 __put_user(MCONTEXT_VERSION
, &uc
->tuc_mcontext
.version
);
49 __put_user(env
->regs
[1], &gregs
[0]);
50 __put_user(env
->regs
[2], &gregs
[1]);
51 __put_user(env
->regs
[3], &gregs
[2]);
52 __put_user(env
->regs
[4], &gregs
[3]);
53 __put_user(env
->regs
[5], &gregs
[4]);
54 __put_user(env
->regs
[6], &gregs
[5]);
55 __put_user(env
->regs
[7], &gregs
[6]);
56 __put_user(env
->regs
[8], &gregs
[7]);
57 __put_user(env
->regs
[9], &gregs
[8]);
58 __put_user(env
->regs
[10], &gregs
[9]);
59 __put_user(env
->regs
[11], &gregs
[10]);
60 __put_user(env
->regs
[12], &gregs
[11]);
61 __put_user(env
->regs
[13], &gregs
[12]);
62 __put_user(env
->regs
[14], &gregs
[13]);
63 __put_user(env
->regs
[15], &gregs
[14]);
64 __put_user(env
->regs
[16], &gregs
[15]);
65 __put_user(env
->regs
[17], &gregs
[16]);
66 __put_user(env
->regs
[18], &gregs
[17]);
67 __put_user(env
->regs
[19], &gregs
[18]);
68 __put_user(env
->regs
[20], &gregs
[19]);
69 __put_user(env
->regs
[21], &gregs
[20]);
70 __put_user(env
->regs
[22], &gregs
[21]);
71 __put_user(env
->regs
[23], &gregs
[22]);
72 __put_user(env
->regs
[R_RA
], &gregs
[23]);
73 __put_user(env
->regs
[R_FP
], &gregs
[24]);
74 __put_user(env
->regs
[R_GP
], &gregs
[25]);
75 __put_user(env
->regs
[R_EA
], &gregs
[27]);
76 __put_user(env
->regs
[R_SP
], &gregs
[28]);
81 static int rt_restore_ucontext(CPUNios2State
*env
, struct target_ucontext
*uc
,
85 abi_ulong off
, frame_addr
= env
->regs
[R_SP
];
86 unsigned long *gregs
= uc
->tuc_mcontext
.gregs
;
89 /* Always make any pending restarted system calls return -EINTR */
90 /* current->restart_block.fn = do_no_restart_syscall; */
92 __get_user(temp
, &uc
->tuc_mcontext
.version
);
93 if (temp
!= MCONTEXT_VERSION
) {
97 /* restore passed registers */
98 __get_user(env
->regs
[1], &gregs
[0]);
99 __get_user(env
->regs
[2], &gregs
[1]);
100 __get_user(env
->regs
[3], &gregs
[2]);
101 __get_user(env
->regs
[4], &gregs
[3]);
102 __get_user(env
->regs
[5], &gregs
[4]);
103 __get_user(env
->regs
[6], &gregs
[5]);
104 __get_user(env
->regs
[7], &gregs
[6]);
105 __get_user(env
->regs
[8], &gregs
[7]);
106 __get_user(env
->regs
[9], &gregs
[8]);
107 __get_user(env
->regs
[10], &gregs
[9]);
108 __get_user(env
->regs
[11], &gregs
[10]);
109 __get_user(env
->regs
[12], &gregs
[11]);
110 __get_user(env
->regs
[13], &gregs
[12]);
111 __get_user(env
->regs
[14], &gregs
[13]);
112 __get_user(env
->regs
[15], &gregs
[14]);
113 __get_user(env
->regs
[16], &gregs
[15]);
114 __get_user(env
->regs
[17], &gregs
[16]);
115 __get_user(env
->regs
[18], &gregs
[17]);
116 __get_user(env
->regs
[19], &gregs
[18]);
117 __get_user(env
->regs
[20], &gregs
[19]);
118 __get_user(env
->regs
[21], &gregs
[20]);
119 __get_user(env
->regs
[22], &gregs
[21]);
120 __get_user(env
->regs
[23], &gregs
[22]);
121 /* gregs[23] is handled below */
122 /* Verify, should this be settable */
123 __get_user(env
->regs
[R_FP
], &gregs
[24]);
124 /* Verify, should this be settable */
125 __get_user(env
->regs
[R_GP
], &gregs
[25]);
126 /* Not really necessary no user settable bits */
127 __get_user(temp
, &gregs
[26]);
128 __get_user(env
->regs
[R_EA
], &gregs
[27]);
130 __get_user(env
->regs
[R_RA
], &gregs
[23]);
131 __get_user(env
->regs
[R_SP
], &gregs
[28]);
133 off
= offsetof(struct target_rt_sigframe
, uc
.tuc_stack
);
134 err
= do_sigaltstack(frame_addr
+ off
, 0, get_sp_from_cpustate(env
));
135 if (err
== -EFAULT
) {
143 static void *get_sigframe(struct target_sigaction
*ka
, CPUNios2State
*env
,
148 /* This is the X/Open sanctioned signal stack switching. */
149 usp
= target_sigsp(get_sp_from_cpustate(env
), ka
);
151 /* Verify, is it 32 or 64 bit aligned */
152 return (void *)((usp
- frame_size
) & -8UL);
155 void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
156 target_siginfo_t
*info
,
157 target_sigset_t
*set
,
160 struct target_rt_sigframe
*frame
;
163 frame
= get_sigframe(ka
, env
, sizeof(*frame
));
165 if (ka
->sa_flags
& SA_SIGINFO
) {
166 tswap_siginfo(&frame
->info
, info
);
169 /* Create the ucontext. */
170 __put_user(0, &frame
->uc
.tuc_flags
);
171 __put_user(0, &frame
->uc
.tuc_link
);
172 target_save_altstack(&frame
->uc
.tuc_stack
, env
);
173 err
|= rt_setup_ucontext(&frame
->uc
, env
);
174 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
175 __put_user((abi_ulong
)set
->sig
[i
],
176 (abi_ulong
*)&frame
->uc
.tuc_sigmask
.sig
[i
]);
183 /* Set up to return from userspace; jump to fixed address sigreturn
184 trampoline on kuser page. */
185 env
->regs
[R_RA
] = (unsigned long) (0x1044);
187 /* Set up registers for signal handler */
188 env
->regs
[R_SP
] = (unsigned long) frame
;
189 env
->regs
[4] = (unsigned long) sig
;
190 env
->regs
[5] = (unsigned long) &frame
->info
;
191 env
->regs
[6] = (unsigned long) &frame
->uc
;
192 env
->regs
[R_EA
] = (unsigned long) ka
->_sa_handler
;
196 if (sig
== TARGET_SIGSEGV
) {
197 ka
->_sa_handler
= TARGET_SIG_DFL
;
203 long do_sigreturn(CPUNios2State
*env
)
205 trace_user_do_sigreturn(env
, 0);
206 qemu_log_mask(LOG_UNIMP
, "do_sigreturn: not implemented\n");
207 return -TARGET_ENOSYS
;
210 long do_rt_sigreturn(CPUNios2State
*env
)
212 /* Verify, can we follow the stack back */
213 abi_ulong frame_addr
= env
->regs
[R_SP
];
214 struct target_rt_sigframe
*frame
;
218 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
222 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
223 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
225 if (rt_restore_ucontext(env
, &frame
->uc
, &rval
)) {
229 unlock_user_struct(frame
, frame_addr
, 0);
233 unlock_user_struct(frame
, frame_addr
, 0);
234 force_sig(TARGET_SIGSEGV
);