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 unsigned long *gregs
= uc
->tuc_mcontext
.gregs
;
87 /* Always make any pending restarted system calls return -EINTR */
88 /* current->restart_block.fn = do_no_restart_syscall; */
90 __get_user(temp
, &uc
->tuc_mcontext
.version
);
91 if (temp
!= MCONTEXT_VERSION
) {
95 /* restore passed registers */
96 __get_user(env
->regs
[1], &gregs
[0]);
97 __get_user(env
->regs
[2], &gregs
[1]);
98 __get_user(env
->regs
[3], &gregs
[2]);
99 __get_user(env
->regs
[4], &gregs
[3]);
100 __get_user(env
->regs
[5], &gregs
[4]);
101 __get_user(env
->regs
[6], &gregs
[5]);
102 __get_user(env
->regs
[7], &gregs
[6]);
103 __get_user(env
->regs
[8], &gregs
[7]);
104 __get_user(env
->regs
[9], &gregs
[8]);
105 __get_user(env
->regs
[10], &gregs
[9]);
106 __get_user(env
->regs
[11], &gregs
[10]);
107 __get_user(env
->regs
[12], &gregs
[11]);
108 __get_user(env
->regs
[13], &gregs
[12]);
109 __get_user(env
->regs
[14], &gregs
[13]);
110 __get_user(env
->regs
[15], &gregs
[14]);
111 __get_user(env
->regs
[16], &gregs
[15]);
112 __get_user(env
->regs
[17], &gregs
[16]);
113 __get_user(env
->regs
[18], &gregs
[17]);
114 __get_user(env
->regs
[19], &gregs
[18]);
115 __get_user(env
->regs
[20], &gregs
[19]);
116 __get_user(env
->regs
[21], &gregs
[20]);
117 __get_user(env
->regs
[22], &gregs
[21]);
118 __get_user(env
->regs
[23], &gregs
[22]);
119 /* gregs[23] is handled below */
120 /* Verify, should this be settable */
121 __get_user(env
->regs
[R_FP
], &gregs
[24]);
122 /* Verify, should this be settable */
123 __get_user(env
->regs
[R_GP
], &gregs
[25]);
124 /* Not really necessary no user settable bits */
125 __get_user(temp
, &gregs
[26]);
126 __get_user(env
->regs
[R_EA
], &gregs
[27]);
128 __get_user(env
->regs
[R_RA
], &gregs
[23]);
129 __get_user(env
->regs
[R_SP
], &gregs
[28]);
131 target_restore_altstack(&uc
->tuc_stack
, env
);
137 static void *get_sigframe(struct target_sigaction
*ka
, CPUNios2State
*env
,
142 /* This is the X/Open sanctioned signal stack switching. */
143 usp
= target_sigsp(get_sp_from_cpustate(env
), ka
);
145 /* Verify, is it 32 or 64 bit aligned */
146 return (void *)((usp
- frame_size
) & -8UL);
149 void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
150 target_siginfo_t
*info
,
151 target_sigset_t
*set
,
154 struct target_rt_sigframe
*frame
;
157 frame
= get_sigframe(ka
, env
, sizeof(*frame
));
159 if (ka
->sa_flags
& SA_SIGINFO
) {
160 tswap_siginfo(&frame
->info
, info
);
163 /* Create the ucontext. */
164 __put_user(0, &frame
->uc
.tuc_flags
);
165 __put_user(0, &frame
->uc
.tuc_link
);
166 target_save_altstack(&frame
->uc
.tuc_stack
, env
);
167 err
|= rt_setup_ucontext(&frame
->uc
, env
);
168 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
169 __put_user((abi_ulong
)set
->sig
[i
],
170 (abi_ulong
*)&frame
->uc
.tuc_sigmask
.sig
[i
]);
177 /* Set up to return from userspace; jump to fixed address sigreturn
178 trampoline on kuser page. */
179 env
->regs
[R_RA
] = (unsigned long) (0x1044);
181 /* Set up registers for signal handler */
182 env
->regs
[R_SP
] = (unsigned long) frame
;
183 env
->regs
[4] = (unsigned long) sig
;
184 env
->regs
[5] = (unsigned long) &frame
->info
;
185 env
->regs
[6] = (unsigned long) &frame
->uc
;
186 env
->regs
[R_EA
] = (unsigned long) ka
->_sa_handler
;
190 if (sig
== TARGET_SIGSEGV
) {
191 ka
->_sa_handler
= TARGET_SIG_DFL
;
197 long do_sigreturn(CPUNios2State
*env
)
199 trace_user_do_sigreturn(env
, 0);
200 qemu_log_mask(LOG_UNIMP
, "do_sigreturn: not implemented\n");
201 return -TARGET_ENOSYS
;
204 long do_rt_sigreturn(CPUNios2State
*env
)
206 /* Verify, can we follow the stack back */
207 abi_ulong frame_addr
= env
->regs
[R_SP
];
208 struct target_rt_sigframe
*frame
;
212 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
216 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
217 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
219 if (rt_restore_ucontext(env
, &frame
->uc
, &rval
)) {
223 unlock_user_struct(frame
, frame_addr
, 0);
227 unlock_user_struct(frame
, frame_addr
, 0);
228 force_sig(TARGET_SIGSEGV
);