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 struct target_sigcontext
{
26 /* General-purpose registers. */
29 abi_ulong __gregs
[53];
30 abi_ulong tp
; /* Aliases gregs[TREG_TP]. */
31 abi_ulong sp
; /* Aliases gregs[TREG_SP]. */
32 abi_ulong lr
; /* Aliases gregs[TREG_LR]. */
35 abi_ulong pc
; /* Program counter. */
36 abi_ulong ics
; /* In Interrupt Critical Section? */
37 abi_ulong faultnum
; /* Fault number. */
41 struct target_ucontext
{
44 target_stack_t tuc_stack
;
45 struct target_sigcontext tuc_mcontext
;
46 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
49 struct target_rt_sigframe
{
50 unsigned char save_area
[16]; /* caller save area */
51 struct target_siginfo info
;
52 struct target_ucontext uc
;
56 #define INSN_MOVELI_R10_139 0x00045fe551483000ULL /* { moveli r10, 139 } */
57 #define INSN_SWINT1 0x286b180051485000ULL /* { swint1 } */
60 static void setup_sigcontext(struct target_sigcontext
*sc
,
61 CPUArchState
*env
, int signo
)
65 for (i
= 0; i
< TILEGX_R_COUNT
; ++i
) {
66 __put_user(env
->regs
[i
], &sc
->gregs
[i
]);
69 __put_user(env
->pc
, &sc
->pc
);
70 __put_user(0, &sc
->ics
);
71 __put_user(signo
, &sc
->faultnum
);
74 static void restore_sigcontext(CPUTLGState
*env
, struct target_sigcontext
*sc
)
78 for (i
= 0; i
< TILEGX_R_COUNT
; ++i
) {
79 __get_user(env
->regs
[i
], &sc
->gregs
[i
]);
82 __get_user(env
->pc
, &sc
->pc
);
85 static abi_ulong
get_sigframe(struct target_sigaction
*ka
, CPUArchState
*env
,
88 unsigned long sp
= get_sp_from_cpustate(env
);
90 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- frame_size
))) {
94 sp
= target_sigsp(sp
, ka
) - frame_size
;
99 void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
100 target_siginfo_t
*info
,
101 target_sigset_t
*set
, CPUArchState
*env
)
103 abi_ulong frame_addr
;
104 struct target_rt_sigframe
*frame
;
105 unsigned long restorer
;
107 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
108 trace_user_setup_rt_frame(env
, frame_addr
);
109 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
113 /* Always write at least the signal number for the stack backtracer. */
114 if (ka
->sa_flags
& TARGET_SA_SIGINFO
) {
115 /* At sigreturn time, restore the callee-save registers too. */
116 tswap_siginfo(&frame
->info
, info
);
117 /* regs->flags |= PT_FLAGS_RESTORE_REGS; FIXME: we can skip it? */
119 __put_user(info
->si_signo
, &frame
->info
.si_signo
);
122 /* Create the ucontext. */
123 __put_user(0, &frame
->uc
.tuc_flags
);
124 __put_user(0, &frame
->uc
.tuc_link
);
125 target_save_altstack(&frame
->uc
.tuc_stack
, env
);
126 setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
, info
->si_signo
);
128 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
129 restorer
= (unsigned long) ka
->sa_restorer
;
131 __put_user(INSN_MOVELI_R10_139
, &frame
->retcode
[0]);
132 __put_user(INSN_SWINT1
, &frame
->retcode
[1]);
133 restorer
= frame_addr
+ offsetof(struct target_rt_sigframe
, retcode
);
135 env
->pc
= (unsigned long) ka
->_sa_handler
;
136 env
->regs
[TILEGX_R_SP
] = (unsigned long) frame
;
137 env
->regs
[TILEGX_R_LR
] = restorer
;
138 env
->regs
[0] = (unsigned long) sig
;
139 env
->regs
[1] = (unsigned long) &frame
->info
;
140 env
->regs
[2] = (unsigned long) &frame
->uc
;
141 /* regs->flags |= PT_FLAGS_CALLER_SAVES; FIXME: we can skip it? */
143 unlock_user_struct(frame
, frame_addr
, 1);
150 long do_rt_sigreturn(CPUTLGState
*env
)
152 abi_ulong frame_addr
= env
->regs
[TILEGX_R_SP
];
153 struct target_rt_sigframe
*frame
;
156 trace_user_do_rt_sigreturn(env
, frame_addr
);
157 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
160 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
163 restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
);
164 if (do_sigaltstack(frame_addr
+ offsetof(struct target_rt_sigframe
,
166 0, env
->regs
[TILEGX_R_SP
]) == -EFAULT
) {
170 unlock_user_struct(frame
, frame_addr
, 0);
171 return -TARGET_QEMU_ESIGRETURN
;
175 unlock_user_struct(frame
, frame_addr
, 0);
176 force_sig(TARGET_SIGSEGV
);
177 return -TARGET_QEMU_ESIGRETURN
;