1 /*- 2 * Copyright (c) 1994-1995 S�ren Schmidt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software withough specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: linux_signal.c,v 1.8 1997/02/22 09:38:24 peter Exp $ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/sysproto.h> 34 #include <sys/proc.h> 35 #include <sys/signalvar.h> 36 37 #include <i386/linux/linux.h> 38 #include <i386/linux/linux_proto.h> 39 #include <i386/linux/linux_util.h> 40 41 static sigset_t 42 linux_to_bsd_sigset(linux_sigset_t mask) { 43 int b, l; 44 sigset_t new = 0; 45 46 for (l = 1; l <= LINUX_NSIG; l++) { 47 if (mask & (1 << (l - 1))) { 48 if ((b = linux_to_bsd_signal[l])) 49 new |= (1 << (b - 1)); 50 } 51 } 52 return new; 53 } 54 55 static linux_sigset_t 56 bsd_to_linux_sigset(sigset_t mask) { 57 int b, l; 58 sigset_t new = 0; 59 60 for (b = 1; b <= NSIG; b++) { 61 if (mask & (1 << (b - 1))) { 62 if ((l = bsd_to_linux_signal[b])) 63 new |= (1 << (l - 1)); 64 } 65 } 66 return new; 67 } 68 69 static void 70 linux_to_bsd_sigaction(linux_sigaction_t *lsa, struct sigaction *bsa) 71 { 72 bsa->sa_mask = linux_to_bsd_sigset(lsa->sa_mask); 73 bsa->sa_handler = lsa->sa_handler; 74 bsa->sa_flags = 0; 75 if (lsa->sa_flags & LINUX_SA_NOCLDSTOP) 76 bsa->sa_flags |= SA_NOCLDSTOP; 77 if (lsa->sa_flags & LINUX_SA_ONSTACK) 78 bsa->sa_flags |= SA_ONSTACK; 79 if (lsa->sa_flags & LINUX_SA_RESTART) 80 bsa->sa_flags |= SA_RESTART; 81 if (lsa->sa_flags & LINUX_SA_ONESHOT) 82 bsa->sa_flags |= SA_RESETHAND; 83 if (lsa->sa_flags & LINUX_SA_NOMASK) 84 bsa->sa_flags |= SA_NODEFER; 85 } 86 87 static void 88 bsd_to_linux_sigaction(struct sigaction *bsa, linux_sigaction_t *lsa) 89 { 90 lsa->sa_handler = bsa->sa_handler; 91 lsa->sa_restorer = NULL; /* unsupported */ 92 lsa->sa_mask = bsd_to_linux_sigset(bsa->sa_mask); 93 lsa->sa_flags = 0; 94 if (bsa->sa_flags & SA_NOCLDSTOP) 95 lsa->sa_flags |= LINUX_SA_NOCLDSTOP; 96 if (bsa->sa_flags & SA_ONSTACK) 97 lsa->sa_flags |= LINUX_SA_ONSTACK; 98 if (bsa->sa_flags & SA_RESTART) 99 lsa->sa_flags |= LINUX_SA_RESTART; 100 if (bsa->sa_flags & SA_RESETHAND) 101 lsa->sa_flags |= LINUX_SA_ONESHOT; 102 if (bsa->sa_flags & SA_NODEFER) 103 lsa->sa_flags |= LINUX_SA_NOMASK; 104 } 105 106 int 107 linux_sigaction(struct proc *p, struct linux_sigaction_args *args, int *retval) 108 { 109 linux_sigaction_t linux_sa; 110 struct sigaction *nsa = NULL, *osa = NULL, bsd_sa; 111 struct sigaction_args sa; 112 int error; 113 caddr_t sg = stackgap_init(); 114 115 #ifdef DEBUG 116 printf("Linux-emul(%d): sigaction(%d, %08x, %08x)\n", p->p_pid, args->sig, 117 args->nsa, args->osa); 118 #endif 119 120 if (args->osa) 121 osa = (struct sigaction *)stackgap_alloc(&sg, sizeof(struct sigaction)); 122 123 if (args->nsa) { 124 nsa = (struct sigaction *)stackgap_alloc(&sg, sizeof(struct sigaction)); 125 if (error = copyin(args->nsa, &linux_sa, sizeof(linux_sigaction_t))) 126 return error; 127 linux_to_bsd_sigaction(&linux_sa, &bsd_sa); 128 if (error = copyout(&bsd_sa, nsa, sizeof(struct sigaction))) 129 return error; 130 } 131 sa.signum = linux_to_bsd_signal[args->sig]; 132 sa.nsa = nsa; 133 sa.osa = osa; 134 if ((error = sigaction(p, &sa, retval))) 135 return error; 136 137 if (args->osa) { 138 if (error = copyin(osa, &bsd_sa, sizeof(struct sigaction))) 139 return error; 140 bsd_to_linux_sigaction(&bsd_sa, &linux_sa); 141 if (error = copyout(&linux_sa, args->osa, sizeof(linux_sigaction_t))) 142 return error; 143 } 144 return 0; 145 } 146 147 int 148 linux_signal(struct proc *p, struct linux_signal_args *args, int *retval) 149 { 150 caddr_t sg; 151 struct sigaction_args sa_args; 152 struct sigaction *osa, *nsa, tmpsa; 153 int error; 154 155 #ifdef DEBUG 156 printf("Linux-emul(%d): signal(%d, %08x)\n", p->p_pid, 157 args->sig, args->handler); 158 #endif 159 sg = stackgap_init(); 160 nsa = stackgap_alloc(&sg, sizeof *nsa); 161 osa = stackgap_alloc(&sg, sizeof *osa); 162 163 tmpsa.sa_handler = args->handler; 164 tmpsa.sa_mask = (sigset_t) 0; 165 tmpsa.sa_flags = SA_RESETHAND | SA_NODEFER; 166 if ((error = copyout(&tmpsa, nsa, sizeof tmpsa))) 167 return error; 168 169 sa_args.signum = linux_to_bsd_signal[args->sig]; 170 sa_args.osa = osa; 171 sa_args.nsa = nsa; 172 if ((error = sigaction(p, &sa_args, retval))) 173 return error; 174 175 if ((error = copyin(osa, &tmpsa, sizeof *osa))) 176 return error; 177 178 *retval = (int)tmpsa.sa_handler; 179 180 return 0; 181 } 182 183 184 int 185 linux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args, 186 int *retval) 187 { 188 int error, s; 189 sigset_t mask; 190 sigset_t omask; 191 192 #ifdef DEBUG 193 printf("Linux-emul(%d): sigprocmask(%d, *, *)\n", p->p_pid, args->how); 194 #endif 195 196 *retval = 0; 197 198 if (args->omask != NULL) { 199 omask = bsd_to_linux_sigset(p->p_sigmask); 200 if (error = copyout(&omask, args->omask, sizeof(sigset_t))) 201 return error; 202 } 203 if (!(args->mask)) 204 return 0; 205 if (error = copyin(args->mask, &mask, sizeof(linux_sigset_t))) 206 return error; 207 208 mask = linux_to_bsd_sigset(mask); 209 s = splhigh(); 210 switch (args->how) { 211 case LINUX_SIG_BLOCK: 212 p->p_sigmask |= (mask & ~sigcantmask); 213 break; 214 case LINUX_SIG_UNBLOCK: 215 p->p_sigmask &= ~mask; 216 break; 217 case LINUX_SIG_SETMASK: 218 p->p_sigmask = (mask & ~sigcantmask); 219 break; 220 default: 221 error = EINVAL; 222 break; 223 } 224 splx(s); 225 return error; 226 } 227 228 int 229 linux_siggetmask(struct proc *p, struct linux_siggetmask_args *args, int *retval) 230 { 231 #ifdef DEBUG 232 printf("Linux-emul(%d): siggetmask()\n", p->p_pid); 233 #endif 234 *retval = bsd_to_linux_sigset(p->p_sigmask); 235 return 0; 236 } 237 238 int 239 linux_sigsetmask(struct proc *p, struct linux_sigsetmask_args *args,int *retval) 240 { 241 int s; 242 sigset_t mask; 243 244 #ifdef DEBUG 245 printf("Linux-emul(%d): sigsetmask(%08x)\n", p->p_pid, args->mask); 246 #endif 247 *retval = bsd_to_linux_sigset(p->p_sigmask); 248 249 mask = linux_to_bsd_sigset(args->mask); 250 s = splhigh(); 251 p->p_sigmask = mask & ~sigcantmask; 252 splx(s); 253 return 0; 254 } 255 256 int 257 linux_sigpending(struct proc *p, struct linux_sigpending_args *args,int *retval) 258 { 259 linux_sigset_t linux_sig; 260 261 #ifdef DEBUG 262 printf("Linux-emul(%d): sigpending(*)\n", p->p_pid); 263 #endif 264 linux_sig = bsd_to_linux_sigset(p->p_siglist & p->p_sigmask); 265 return copyout(&linux_sig, args->mask, sizeof(linux_sig)); 266 } 267 268 /* 269 * Linux has two extra args, restart and oldmask. We dont use these, 270 * but it seems that "restart" is actually a context pointer that 271 * enables the signal to happen with a different register set. 272 */ 273 int 274 linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args,int *retval) 275 { 276 struct sigsuspend_args tmp; 277 278 #ifdef DEBUG 279 printf("Linux-emul(%d): sigsuspend(%08x)\n", p->p_pid, args->mask); 280 #endif 281 tmp.mask = linux_to_bsd_sigset(args->mask); 282 return sigsuspend(p, &tmp , retval); 283 } 284 285 int 286 linux_pause(struct proc *p, struct linux_pause_args *args,int *retval) 287 { 288 struct sigsuspend_args tmp; 289 290 #ifdef DEBUG 291 printf("Linux-emul(%d): pause()\n", p->p_pid); 292 #endif 293 tmp.mask = p->p_sigmask; 294 return sigsuspend(p, &tmp , retval); 295 } 296 297 int 298 linux_kill(struct proc *p, struct linux_kill_args *args, int *retval) 299 { 300 struct kill_args /* { 301 int pid; 302 int signum; 303 } */ tmp; 304 305 #ifdef DEBUG 306 printf("Linux-emul(%d): kill(%d, %d)\n", 307 p->p_pid, args->pid, args->signum); 308 #endif 309 tmp.pid = args->pid; 310 tmp.signum = linux_to_bsd_signal[args->signum]; 311 return kill(p, &tmp, retval); 312 } 313