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