xen-hvm: create separate function for ioreq server initialization
[qemu.git] / linux-user / tilegx / signal.c
blobd0ed3de569d0676b8b7cb454475fac47588dc99d
1 /*
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"
20 #include "qemu.h"
21 #include "target_signal.h"
22 #include "signal-common.h"
23 #include "linux-user/trace.h"
25 struct target_sigcontext {
26 union {
27 /* General-purpose registers. */
28 abi_ulong gregs[56];
29 struct {
30 abi_ulong __gregs[53];
31 abi_ulong tp; /* Aliases gregs[TREG_TP]. */
32 abi_ulong sp; /* Aliases gregs[TREG_SP]. */
33 abi_ulong lr; /* Aliases gregs[TREG_LR]. */
36 abi_ulong pc; /* Program counter. */
37 abi_ulong ics; /* In Interrupt Critical Section? */
38 abi_ulong faultnum; /* Fault number. */
39 abi_ulong pad[5];
42 struct target_ucontext {
43 abi_ulong tuc_flags;
44 abi_ulong tuc_link;
45 target_stack_t tuc_stack;
46 struct target_sigcontext tuc_mcontext;
47 target_sigset_t tuc_sigmask; /* mask last for extensibility */
50 struct target_rt_sigframe {
51 unsigned char save_area[16]; /* caller save area */
52 struct target_siginfo info;
53 struct target_ucontext uc;
54 abi_ulong retcode[2];
57 #define INSN_MOVELI_R10_139 0x00045fe551483000ULL /* { moveli r10, 139 } */
58 #define INSN_SWINT1 0x286b180051485000ULL /* { swint1 } */
61 static void setup_sigcontext(struct target_sigcontext *sc,
62 CPUArchState *env, int signo)
64 int i;
66 for (i = 0; i < TILEGX_R_COUNT; ++i) {
67 __put_user(env->regs[i], &sc->gregs[i]);
70 __put_user(env->pc, &sc->pc);
71 __put_user(0, &sc->ics);
72 __put_user(signo, &sc->faultnum);
75 static void restore_sigcontext(CPUTLGState *env, struct target_sigcontext *sc)
77 int i;
79 for (i = 0; i < TILEGX_R_COUNT; ++i) {
80 __get_user(env->regs[i], &sc->gregs[i]);
83 __get_user(env->pc, &sc->pc);
86 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUArchState *env,
87 size_t frame_size)
89 unsigned long sp = get_sp_from_cpustate(env);
91 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) {
92 return -1UL;
95 sp = target_sigsp(sp, ka) - frame_size;
96 sp &= -16UL;
97 return sp;
100 void setup_rt_frame(int sig, struct target_sigaction *ka,
101 target_siginfo_t *info,
102 target_sigset_t *set, CPUArchState *env)
104 abi_ulong frame_addr;
105 struct target_rt_sigframe *frame;
106 unsigned long restorer;
108 frame_addr = get_sigframe(ka, env, sizeof(*frame));
109 trace_user_setup_rt_frame(env, frame_addr);
110 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
111 goto give_sigsegv;
114 /* Always write at least the signal number for the stack backtracer. */
115 if (ka->sa_flags & TARGET_SA_SIGINFO) {
116 /* At sigreturn time, restore the callee-save registers too. */
117 tswap_siginfo(&frame->info, info);
118 /* regs->flags |= PT_FLAGS_RESTORE_REGS; FIXME: we can skip it? */
119 } else {
120 __put_user(info->si_signo, &frame->info.si_signo);
123 /* Create the ucontext. */
124 __put_user(0, &frame->uc.tuc_flags);
125 __put_user(0, &frame->uc.tuc_link);
126 target_save_altstack(&frame->uc.tuc_stack, env);
127 setup_sigcontext(&frame->uc.tuc_mcontext, env, info->si_signo);
129 if (ka->sa_flags & TARGET_SA_RESTORER) {
130 restorer = (unsigned long) ka->sa_restorer;
131 } else {
132 __put_user(INSN_MOVELI_R10_139, &frame->retcode[0]);
133 __put_user(INSN_SWINT1, &frame->retcode[1]);
134 restorer = frame_addr + offsetof(struct target_rt_sigframe, retcode);
136 env->pc = (unsigned long) ka->_sa_handler;
137 env->regs[TILEGX_R_SP] = (unsigned long) frame;
138 env->regs[TILEGX_R_LR] = restorer;
139 env->regs[0] = (unsigned long) sig;
140 env->regs[1] = (unsigned long) &frame->info;
141 env->regs[2] = (unsigned long) &frame->uc;
142 /* regs->flags |= PT_FLAGS_CALLER_SAVES; FIXME: we can skip it? */
144 unlock_user_struct(frame, frame_addr, 1);
145 return;
147 give_sigsegv:
148 force_sigsegv(sig);
151 long do_rt_sigreturn(CPUTLGState *env)
153 abi_ulong frame_addr = env->regs[TILEGX_R_SP];
154 struct target_rt_sigframe *frame;
155 sigset_t set;
157 trace_user_do_rt_sigreturn(env, frame_addr);
158 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
159 goto badframe;
161 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
162 set_sigmask(&set);
164 restore_sigcontext(env, &frame->uc.tuc_mcontext);
165 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
166 uc.tuc_stack),
167 0, env->regs[TILEGX_R_SP]) == -EFAULT) {
168 goto badframe;
171 unlock_user_struct(frame, frame_addr, 0);
172 return -TARGET_QEMU_ESIGRETURN;
175 badframe:
176 unlock_user_struct(frame, frame_addr, 0);
177 force_sig(TARGET_SIGSEGV);
178 return -TARGET_QEMU_ESIGRETURN;