xref: /openbmc/qemu/linux-user/hppa/signal.c (revision 259ebed4)
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 "user-internals.h"
22 #include "signal-common.h"
23 #include "linux-user/trace.h"
24 #include "vdso-asmoffset.h"
25 
26 struct target_sigcontext {
27     abi_ulong sc_flags;
28     abi_ulong sc_gr[32];
29     abi_ullong sc_fr[32];
30     abi_ulong sc_iasq[2];
31     abi_ulong sc_iaoq[2];
32     abi_ulong sc_sar;
33 };
34 
35 struct target_ucontext {
36     abi_uint tuc_flags;
37     abi_ulong tuc_link;
38     target_stack_t tuc_stack;
39     abi_uint pad[1];
40     struct target_sigcontext tuc_mcontext;
41     target_sigset_t tuc_sigmask;
42 };
43 
44 struct target_rt_sigframe {
45     abi_uint tramp[2];  /* syscall restart return address */
46     target_siginfo_t info;
47     struct target_ucontext uc;
48     /* hidden location of upper halves of pa2.0 64-bit gregs */
49 };
50 
51 QEMU_BUILD_BUG_ON(sizeof(struct target_rt_sigframe) != sizeof_rt_sigframe);
52 QEMU_BUILD_BUG_ON(offsetof(struct target_rt_sigframe, uc.tuc_mcontext)
53                   != offsetof_sigcontext);
54 QEMU_BUILD_BUG_ON(offsetof(struct target_sigcontext, sc_gr)
55                   != offsetof_sigcontext_gr);
56 QEMU_BUILD_BUG_ON(offsetof(struct target_sigcontext, sc_fr)
57                   != offsetof_sigcontext_fr);
58 QEMU_BUILD_BUG_ON(offsetof(struct target_sigcontext, sc_iaoq)
59                   != offsetof_sigcontext_iaoq);
60 QEMU_BUILD_BUG_ON(offsetof(struct target_sigcontext, sc_sar)
61                   != offsetof_sigcontext_sar);
62 
63 
64 static void setup_sigcontext(struct target_sigcontext *sc, CPUArchState *env)
65 {
66     int i;
67 
68     __put_user(env->iaoq_f, &sc->sc_iaoq[0]);
69     __put_user(env->iaoq_b, &sc->sc_iaoq[1]);
70     __put_user(0, &sc->sc_iasq[0]);
71     __put_user(0, &sc->sc_iasq[1]);
72     __put_user(0, &sc->sc_flags);
73 
74     __put_user(cpu_hppa_get_psw(env), &sc->sc_gr[0]);
75     for (i = 1; i < 32; ++i) {
76         __put_user(env->gr[i], &sc->sc_gr[i]);
77     }
78 
79     __put_user((uint64_t)env->fr0_shadow << 32, &sc->sc_fr[0]);
80     for (i = 1; i < 32; ++i) {
81         __put_user(env->fr[i], &sc->sc_fr[i]);
82     }
83 
84     __put_user(env->cr[CR_SAR], &sc->sc_sar);
85 }
86 
87 static void restore_sigcontext(CPUArchState *env, struct target_sigcontext *sc)
88 {
89     target_ulong psw;
90     int i;
91 
92     __get_user(psw, &sc->sc_gr[0]);
93     cpu_hppa_put_psw(env, psw);
94 
95     for (i = 1; i < 32; ++i) {
96         __get_user(env->gr[i], &sc->sc_gr[i]);
97     }
98     for (i = 0; i < 32; ++i) {
99         __get_user(env->fr[i], &sc->sc_fr[i]);
100     }
101     cpu_hppa_loaded_fr0(env);
102 
103     __get_user(env->iaoq_f, &sc->sc_iaoq[0]);
104     __get_user(env->iaoq_b, &sc->sc_iaoq[1]);
105     __get_user(env->cr[CR_SAR], &sc->sc_sar);
106 }
107 
108 void setup_rt_frame(int sig, struct target_sigaction *ka,
109                     target_siginfo_t *info,
110                     target_sigset_t *set, CPUArchState *env)
111 {
112     abi_ulong frame_addr, sp, haddr;
113     struct target_rt_sigframe *frame;
114     int i;
115     TaskState *ts = (TaskState *)thread_cpu->opaque;
116 
117     sp = get_sp_from_cpustate(env);
118     if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
119         sp = (ts->sigaltstack_used.ss_sp + 0x7f) & ~0x3f;
120     }
121     frame_addr = QEMU_ALIGN_UP(sp, SIGFRAME);
122     sp = frame_addr + PARISC_RT_SIGFRAME_SIZE32;
123 
124     trace_user_setup_rt_frame(env, frame_addr);
125 
126     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
127         goto give_sigsegv;
128     }
129 
130     tswap_siginfo(&frame->info, info);
131     frame->uc.tuc_flags = 0;
132     frame->uc.tuc_link = 0;
133 
134     target_save_altstack(&frame->uc.tuc_stack, env);
135 
136     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
137         __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
138     }
139 
140     setup_sigcontext(&frame->uc.tuc_mcontext, env);
141 
142     unlock_user_struct(frame, frame_addr, 1);
143 
144     env->gr[2] = default_rt_sigreturn;
145     env->gr[30] = sp;
146     env->gr[26] = sig;
147     env->gr[25] = h2g(&frame->info);
148     env->gr[24] = h2g(&frame->uc);
149 
150     haddr = ka->_sa_handler;
151     if (haddr & 2) {
152         /* Function descriptor.  */
153         target_ulong *fdesc, dest;
154 
155         haddr &= -4;
156         fdesc = lock_user(VERIFY_READ, haddr, 2 * sizeof(target_ulong), 1);
157         if (!fdesc) {
158             goto give_sigsegv;
159         }
160         __get_user(dest, fdesc);
161         __get_user(env->gr[19], fdesc + 1);
162         unlock_user(fdesc, haddr, 0);
163         haddr = dest;
164     }
165     env->iaoq_f = haddr;
166     env->iaoq_b = haddr + 4;
167     env->psw_n = 0;
168     return;
169 
170  give_sigsegv:
171     force_sigsegv(sig);
172 }
173 
174 long do_rt_sigreturn(CPUArchState *env)
175 {
176     abi_ulong frame_addr = env->gr[30] - PARISC_RT_SIGFRAME_SIZE32;
177     struct target_rt_sigframe *frame;
178     sigset_t set;
179 
180     trace_user_do_rt_sigreturn(env, frame_addr);
181     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
182         goto badframe;
183     }
184     target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
185     set_sigmask(&set);
186 
187     restore_sigcontext(env, &frame->uc.tuc_mcontext);
188     target_restore_altstack(&frame->uc.tuc_stack, env);
189 
190     unlock_user_struct(frame, frame_addr, 0);
191     return -QEMU_ESIGRETURN;
192 
193  badframe:
194     force_sig(TARGET_SIGSEGV);
195     return -QEMU_ESIGRETURN;
196 }
197 
198 void setup_sigtramp(abi_ulong sigtramp_page)
199 {
200     uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 6*4, 0);
201     abi_ulong SIGFRAME_CONTEXT_REGS32;
202     assert(tramp != NULL);
203 
204     SIGFRAME_CONTEXT_REGS32 = offsetof(struct target_rt_sigframe, uc.tuc_mcontext);
205     SIGFRAME_CONTEXT_REGS32 -= PARISC_RT_SIGFRAME_SIZE32;
206 
207     __put_user(SIGFRAME_CONTEXT_REGS32, tramp + 0);
208     __put_user(0x08000240, tramp + 1);  /* nop - b/c dwarf2 unwind routines */
209     __put_user(0x34190000, tramp + 2);  /* ldi 0, %r25 (in_syscall=0) */
210     __put_user(0x3414015a, tramp + 3);  /* ldi __NR_rt_sigreturn, %r20 */
211     __put_user(0xe4008200, tramp + 4);  /* ble 0x100(%sr2, %r0) */
212     __put_user(0x08000240, tramp + 5);  /* nop */
213 
214     default_rt_sigreturn = (sigtramp_page + 8) | 3;
215     unlock_user(tramp, sigtramp_page, 6*4);
216 }
217