xref: /openbmc/qemu/linux-user/hppa/signal.c (revision c00506aa)
1 /*
2  *  Emulation of Linux signals
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 #include "qemu/osdep.h"
20 #include "qemu.h"
21 #include "signal-common.h"
22 #include "linux-user/trace.h"
23 
24 struct target_sigcontext {
25     abi_ulong sc_flags;
26     abi_ulong sc_gr[32];
27     uint64_t sc_fr[32];
28     abi_ulong sc_iasq[2];
29     abi_ulong sc_iaoq[2];
30     abi_ulong sc_sar;
31 };
32 
33 struct target_ucontext {
34     abi_uint tuc_flags;
35     abi_ulong tuc_link;
36     target_stack_t tuc_stack;
37     abi_uint pad[1];
38     struct target_sigcontext tuc_mcontext;
39     target_sigset_t tuc_sigmask;
40 };
41 
42 struct target_rt_sigframe {
43     abi_uint tramp[9];
44     target_siginfo_t info;
45     struct target_ucontext uc;
46     /* hidden location of upper halves of pa2.0 64-bit gregs */
47 };
48 
49 static void setup_sigcontext(struct target_sigcontext *sc, CPUArchState *env)
50 {
51     int flags = 0;
52     int i;
53 
54     /* ??? if on_sig_stack, flags |= 1 (PARISC_SC_FLAG_ONSTACK).  */
55 
56     if (env->iaoq_f < TARGET_PAGE_SIZE) {
57         /* In the gateway page, executing a syscall.  */
58         flags |= 2; /* PARISC_SC_FLAG_IN_SYSCALL */
59         __put_user(env->gr[31], &sc->sc_iaoq[0]);
60         __put_user(env->gr[31] + 4, &sc->sc_iaoq[1]);
61     } else {
62         __put_user(env->iaoq_f, &sc->sc_iaoq[0]);
63         __put_user(env->iaoq_b, &sc->sc_iaoq[1]);
64     }
65     __put_user(0, &sc->sc_iasq[0]);
66     __put_user(0, &sc->sc_iasq[1]);
67     __put_user(flags, &sc->sc_flags);
68 
69     __put_user(cpu_hppa_get_psw(env), &sc->sc_gr[0]);
70     for (i = 1; i < 32; ++i) {
71         __put_user(env->gr[i], &sc->sc_gr[i]);
72     }
73 
74     __put_user((uint64_t)env->fr0_shadow << 32, &sc->sc_fr[0]);
75     for (i = 1; i < 32; ++i) {
76         __put_user(env->fr[i], &sc->sc_fr[i]);
77     }
78 
79     __put_user(env->cr[CR_SAR], &sc->sc_sar);
80 }
81 
82 static void restore_sigcontext(CPUArchState *env, struct target_sigcontext *sc)
83 {
84     target_ulong psw;
85     int i;
86 
87     __get_user(psw, &sc->sc_gr[0]);
88     cpu_hppa_put_psw(env, psw);
89 
90     for (i = 1; i < 32; ++i) {
91         __get_user(env->gr[i], &sc->sc_gr[i]);
92     }
93     for (i = 0; i < 32; ++i) {
94         __get_user(env->fr[i], &sc->sc_fr[i]);
95     }
96     cpu_hppa_loaded_fr0(env);
97 
98     __get_user(env->iaoq_f, &sc->sc_iaoq[0]);
99     __get_user(env->iaoq_b, &sc->sc_iaoq[1]);
100     __get_user(env->cr[CR_SAR], &sc->sc_sar);
101 }
102 
103 /* No, this doesn't look right, but it's copied straight from the kernel.  */
104 #define PARISC_RT_SIGFRAME_SIZE32 \
105     ((sizeof(struct target_rt_sigframe) + 48 + 64) & -64)
106 
107 void setup_rt_frame(int sig, struct target_sigaction *ka,
108                     target_siginfo_t *info,
109                     target_sigset_t *set, CPUArchState *env)
110 {
111     abi_ulong frame_addr, sp, haddr;
112     struct target_rt_sigframe *frame;
113     int i;
114     TaskState *ts = (TaskState *)thread_cpu->opaque;
115 
116     sp = get_sp_from_cpustate(env);
117     if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
118         sp = (ts->sigaltstack_used.ss_sp + 0x7f) & ~0x3f;
119     }
120     frame_addr = QEMU_ALIGN_UP(sp, 64);
121     sp = frame_addr + PARISC_RT_SIGFRAME_SIZE32;
122 
123     trace_user_setup_rt_frame(env, frame_addr);
124 
125     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
126         goto give_sigsegv;
127     }
128 
129     tswap_siginfo(&frame->info, info);
130     frame->uc.tuc_flags = 0;
131     frame->uc.tuc_link = 0;
132 
133     target_save_altstack(&frame->uc.tuc_stack, env);
134 
135     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
136         __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
137     }
138 
139     setup_sigcontext(&frame->uc.tuc_mcontext, env);
140 
141     __put_user(0x34190000, frame->tramp + 0); /* ldi 0,%r25 */
142     __put_user(0x3414015a, frame->tramp + 1); /* ldi __NR_rt_sigreturn,%r20 */
143     __put_user(0xe4008200, frame->tramp + 2); /* be,l 0x100(%sr2,%r0) */
144     __put_user(0x08000240, frame->tramp + 3); /* nop */
145 
146     unlock_user_struct(frame, frame_addr, 1);
147 
148     env->gr[2] = h2g(frame->tramp);
149     env->gr[30] = sp;
150     env->gr[26] = sig;
151     env->gr[25] = h2g(&frame->info);
152     env->gr[24] = h2g(&frame->uc);
153 
154     haddr = ka->_sa_handler;
155     if (haddr & 2) {
156         /* Function descriptor.  */
157         target_ulong *fdesc, dest;
158 
159         haddr &= -4;
160         if (!lock_user_struct(VERIFY_READ, fdesc, haddr, 1)) {
161             goto give_sigsegv;
162         }
163         __get_user(dest, fdesc);
164         __get_user(env->gr[19], fdesc + 1);
165         unlock_user_struct(fdesc, haddr, 1);
166         haddr = dest;
167     }
168     env->iaoq_f = haddr;
169     env->iaoq_b = haddr + 4;
170     return;
171 
172  give_sigsegv:
173     force_sigsegv(sig);
174 }
175 
176 long do_rt_sigreturn(CPUArchState *env)
177 {
178     abi_ulong frame_addr = env->gr[30] - PARISC_RT_SIGFRAME_SIZE32;
179     struct target_rt_sigframe *frame;
180     sigset_t set;
181 
182     trace_user_do_rt_sigreturn(env, frame_addr);
183     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
184         goto badframe;
185     }
186     target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
187     set_sigmask(&set);
188 
189     restore_sigcontext(env, &frame->uc.tuc_mcontext);
190     unlock_user_struct(frame, frame_addr, 0);
191 
192     if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
193                                              uc.tuc_stack),
194                        0, env->gr[30]) == -EFAULT) {
195         goto badframe;
196     }
197 
198     unlock_user_struct(frame, frame_addr, 0);
199     return -TARGET_QEMU_ESIGRETURN;
200 
201  badframe:
202     force_sig(TARGET_SIGSEGV);
203     return -TARGET_QEMU_ESIGRETURN;
204 }
205