Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_signal.c revision 1.14
      1 /*	$NetBSD: netbsd32_signal.c,v 1.14 2005/09/24 21:34:18 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_signal.c,v 1.14 2005/09/24 21:34:18 christos Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/malloc.h>
     37 #include <sys/mount.h>
     38 #include <sys/stat.h>
     39 #include <sys/time.h>
     40 #include <sys/signalvar.h>
     41 #include <sys/proc.h>
     42 #include <sys/wait.h>
     43 #include <sys/dirent.h>
     44 
     45 #include <uvm/uvm_extern.h>
     46 
     47 #include <compat/netbsd32/netbsd32.h>
     48 #include <compat/netbsd32/netbsd32_conv.h>
     49 #include <compat/netbsd32/netbsd32_syscallargs.h>
     50 
     51 #include <compat/sys/signal.h>
     52 #include <compat/sys/signalvar.h>
     53 #include <compat/sys/siginfo.h>
     54 #include <compat/sys/ucontext.h>
     55 
     56 static void netbsd32_si_to_si32(siginfo32_t *, const siginfo_t *);
     57 #ifdef unused
     58 static void netbsd32_si32_to_si(siginfo_t *, const siginfo32_t *);
     59 #endif
     60 
     61 
     62 int
     63 netbsd32_sigaction(l, v, retval)
     64 	struct lwp *l;
     65 	void *v;
     66 	register_t *retval;
     67 {
     68 	struct netbsd32_sigaction_args /* {
     69 		syscallarg(int) signum;
     70 		syscallarg(const netbsd32_sigactionp_t) nsa;
     71 		syscallarg(netbsd32_sigactionp_t) osa;
     72 	} */ *uap = v;
     73 	struct sigaction nsa, osa;
     74 	struct netbsd32_sigaction *sa32p, sa32;
     75 	int error;
     76 
     77 	if (SCARG(uap, nsa)) {
     78 		sa32p =
     79 		    (struct netbsd32_sigaction *)NETBSD32PTR64(SCARG(uap, nsa));
     80 		if (copyin(sa32p, &sa32, sizeof(sa32)))
     81 			return EFAULT;
     82 		nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
     83 		nsa.sa_mask = sa32.netbsd32_sa_mask;
     84 		nsa.sa_flags = sa32.netbsd32_sa_flags;
     85 	}
     86 	error = sigaction1(l->l_proc, SCARG(uap, signum),
     87 			   SCARG(uap, nsa) ? &nsa : 0,
     88 			   SCARG(uap, osa) ? &osa : 0,
     89 			   NULL, 0);
     90 
     91 	if (error)
     92 		return (error);
     93 
     94 	if (SCARG(uap, osa)) {
     95 		sa32.netbsd32_sa_handler = (netbsd32_sigactionp_t)(u_long)osa.sa_handler;
     96 		sa32.netbsd32_sa_mask = osa.sa_mask;
     97 		sa32.netbsd32_sa_flags = osa.sa_flags;
     98 		sa32p =
     99 		    (struct netbsd32_sigaction *)NETBSD32PTR64(SCARG(uap, osa));
    100 		if (copyout(&sa32, sa32p, sizeof(sa32)))
    101 			return EFAULT;
    102 	}
    103 
    104 	return (0);
    105 }
    106 
    107 int
    108 netbsd32___sigaltstack14(l, v, retval)
    109 	struct lwp *l;
    110 	void *v;
    111 	register_t *retval;
    112 {
    113 	struct netbsd32___sigaltstack14_args /* {
    114 		syscallarg(const netbsd32_sigaltstackp_t) nss;
    115 		syscallarg(netbsd32_sigaltstackp_t) oss;
    116 	} */ *uap = v;
    117 	struct netbsd32_sigaltstack s32;
    118 	struct sigaltstack nss, oss;
    119 	int error;
    120 
    121 	if (SCARG(uap, nss)) {
    122 		error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, nss)), &s32,
    123 		    sizeof(s32));
    124 		if (error)
    125 			return (error);
    126 		nss.ss_sp = (void *)NETBSD32PTR64(s32.ss_sp);
    127 		nss.ss_size = (size_t)s32.ss_size;
    128 		nss.ss_flags = s32.ss_flags;
    129 	}
    130 	error = sigaltstack1(l->l_proc,
    131 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    132 	if (error)
    133 		return (error);
    134 	if (SCARG(uap, oss)) {
    135 		s32.ss_sp = (netbsd32_voidp)(u_long)oss.ss_sp;
    136 		s32.ss_size = (netbsd32_size_t)oss.ss_size;
    137 		s32.ss_flags = oss.ss_flags;
    138 		error = copyout(&s32, (caddr_t)NETBSD32PTR64(SCARG(uap, oss)),
    139 		    sizeof(s32));
    140 		if (error)
    141 			return (error);
    142 	}
    143 	return (0);
    144 }
    145 
    146 /* ARGSUSED */
    147 int
    148 netbsd32___sigaction14(l, v, retval)
    149 	struct lwp *l;
    150 	void *v;
    151 	register_t *retval;
    152 {
    153 	struct netbsd32___sigaction14_args /* {
    154 		syscallarg(int) signum;
    155 		syscallarg(const struct sigaction *) nsa;
    156 		syscallarg(struct sigaction *) osa;
    157 	} */ *uap = v;
    158 	struct netbsd32_sigaction sa32;
    159 	struct sigaction nsa, osa;
    160 	int error;
    161 
    162 	if (SCARG(uap, nsa)) {
    163 		error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, nsa)), &sa32,
    164 		    sizeof(sa32));
    165 		if (error)
    166 			return (error);
    167 		nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
    168 		nsa.sa_mask = sa32.netbsd32_sa_mask;
    169 		nsa.sa_flags = sa32.netbsd32_sa_flags;
    170 	}
    171 	error = sigaction1(l->l_proc, SCARG(uap, signum),
    172 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    173 	    NULL, 0);
    174 	if (error)
    175 		return (error);
    176 	if (SCARG(uap, osa)) {
    177 		sa32.netbsd32_sa_handler = (netbsd32_voidp)(u_long)osa.sa_handler;
    178 		sa32.netbsd32_sa_mask = osa.sa_mask;
    179 		sa32.netbsd32_sa_flags = osa.sa_flags;
    180 		error = copyout(&sa32, (caddr_t)NETBSD32PTR64(SCARG(uap, osa)),
    181 		    sizeof(sa32));
    182 		if (error)
    183 			return (error);
    184 	}
    185 	return (0);
    186 }
    187 
    188 /* ARGSUSED */
    189 int
    190 netbsd32___sigaction_sigtramp(l, v, retval)
    191 	struct lwp *l;
    192 	void *v;
    193 	register_t *retval;
    194 {
    195 	struct netbsd32___sigaction_sigtramp_args /* {
    196 		syscallarg(int) signum;
    197 		syscallarg(const netbsd32_sigactionp_t) nsa;
    198 		syscallarg(netbsd32_sigactionp_t) osa;
    199 		syscallarg(netbsd32_voidp) tramp;
    200 		syscallarg(int) vers;
    201 	} */ *uap = v;
    202 	struct proc *p = l->l_proc;
    203 	struct netbsd32_sigaction sa32;
    204 	struct sigaction nsa, osa;
    205 	int error;
    206 
    207 	if (SCARG(uap, nsa)) {
    208 		error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, nsa)), &sa32,
    209 		    sizeof(sa32));
    210 		if (error)
    211 			return (error);
    212 		nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
    213 		nsa.sa_mask = sa32.netbsd32_sa_mask;
    214 		nsa.sa_flags = sa32.netbsd32_sa_flags;
    215 	}
    216 	error = sigaction1(p, SCARG(uap, signum),
    217 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    218 	    NETBSD32PTR64(SCARG(uap, tramp)), SCARG(uap, vers));
    219 	if (error)
    220 		return (error);
    221 	if (SCARG(uap, osa)) {
    222 		sa32.netbsd32_sa_handler = (netbsd32_voidp)(u_long)osa.sa_handler;
    223 		sa32.netbsd32_sa_mask = osa.sa_mask;
    224 		sa32.netbsd32_sa_flags = osa.sa_flags;
    225 		error = copyout(&sa32, (caddr_t)NETBSD32PTR64(SCARG(uap, osa)),
    226 		    sizeof(sa32));
    227 		if (error)
    228 			return (error);
    229 	}
    230 	return (0);
    231 }
    232 
    233 #ifdef unused
    234 static void
    235 netbsd32_si32_to_si(siginfo_t *si, const siginfo32_t *si32)
    236 {
    237 	memset(si, 0, sizeof (*si));
    238 	si->si_signo = si32->si_signo;
    239 	si->si_code = si32->si_code;
    240 	si->si_errno = si32->si_errno;
    241 
    242 	switch (si32->si_signo) {
    243 	case SIGILL:
    244 	case SIGBUS:
    245 	case SIGSEGV:
    246 	case SIGFPE:
    247 	case SIGTRAP:
    248 		si->si_addr = (void *)NETBSD32PTR64(si32->si_addr);
    249 		si->si_trap = si32->si_trap;
    250 		break;
    251 	case SIGALRM:
    252 	case SIGVTALRM:
    253 	case SIGPROF:
    254 		si->si_pid = si32->si_pid;
    255 		si->si_uid = si32->si_uid;
    256 		/*
    257 		 * XXX sival_ptr is currently unused.
    258 		 */
    259 		si->si_sigval.sival_int = si32->si_sigval.sival_int;
    260 		break;
    261 	case SIGCHLD:
    262 		si->si_pid = si32->si_pid;
    263 		si->si_uid = si32->si_uid;
    264 		si->si_utime = si32->si_utime;
    265 		si->si_stime = si32->si_stime;
    266 		break;
    267 	case SIGURG:
    268 	case SIGIO:
    269 		si->si_band = si32->si_band;
    270 		si->si_fd = si32->si_fd;
    271 		break;
    272 	}
    273 }
    274 #endif
    275 
    276 static void
    277 netbsd32_si_to_si32(siginfo32_t *si32, const siginfo_t *si)
    278 {
    279 	memset(si32, 0, sizeof (*si32));
    280 	si32->si_signo = si->si_signo;
    281 	si32->si_code = si->si_code;
    282 	si32->si_errno = si->si_errno;
    283 
    284 	switch (si32->si_signo) {
    285 	case SIGILL:
    286 	case SIGBUS:
    287 	case SIGSEGV:
    288 	case SIGFPE:
    289 	case SIGTRAP:
    290 		si32->si_addr = (uint32_t)(uintptr_t)si->si_addr;
    291 		si32->si_trap = si->si_trap;
    292 		break;
    293 	case SIGALRM:
    294 	case SIGVTALRM:
    295 	case SIGPROF:
    296 		si32->si_pid = si->si_pid;
    297 		si32->si_uid = si->si_uid;
    298 		/*
    299 		 * XXX sival_ptr is currently unused.
    300 		 */
    301 		si32->si_sigval.sival_int = si->si_sigval.sival_int;
    302 		break;
    303 	case SIGCHLD:
    304 		si32->si_pid = si->si_pid;
    305 		si32->si_uid = si->si_uid;
    306 		si32->si_status = si->si_status;
    307 		si32->si_utime = si->si_utime;
    308 		si32->si_stime = si->si_stime;
    309 		break;
    310 	case SIGURG:
    311 	case SIGIO:
    312 		si32->si_band = si->si_band;
    313 		si32->si_fd = si->si_fd;
    314 		break;
    315 	}
    316 }
    317 
    318 void
    319 getucontext32(struct lwp *l, ucontext32_t *ucp)
    320 {
    321 	struct proc	*p;
    322 
    323 	p = l->l_proc;
    324 
    325 	ucp->uc_flags = 0;
    326 	ucp->uc_link = (uint32_t)(intptr_t)l->l_ctxlink;
    327 
    328 	(void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
    329 	ucp->uc_flags |= _UC_SIGMASK;
    330 
    331 	/*
    332 	 * The (unsupplied) definition of the `current execution stack'
    333 	 * in the System V Interface Definition appears to allow returning
    334 	 * the main context stack.
    335 	 */
    336 	if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
    337 		ucp->uc_stack.ss_sp = USRSTACK32;
    338 		ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
    339 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
    340 	} else {
    341 		/* Simply copy alternate signal execution stack. */
    342 		ucp->uc_stack.ss_sp =
    343 		    (uint32_t)(intptr_t)p->p_sigctx.ps_sigstk.ss_sp;
    344 		ucp->uc_stack.ss_size = p->p_sigctx.ps_sigstk.ss_size;
    345 		ucp->uc_stack.ss_flags = p->p_sigctx.ps_sigstk.ss_flags;
    346 	}
    347 	ucp->uc_flags |= _UC_STACK;
    348 
    349 	cpu_getmcontext32(l, &ucp->uc_mcontext, &ucp->uc_flags);
    350 }
    351 
    352 /* ARGSUSED */
    353 int
    354 netbsd32_getcontext(struct lwp *l, void *v, register_t *retval)
    355 {
    356 	struct netbsd32_getcontext_args /* {
    357 		syscallarg(netbsd32_ucontextp) ucp;
    358 	} */ *uap = v;
    359 	ucontext32_t uc;
    360 
    361 	getucontext32(l, &uc);
    362 
    363 	return copyout(&uc, NETBSD32PTR64(SCARG(uap, ucp)),
    364 	    sizeof (ucontext32_t));
    365 }
    366 
    367 int
    368 setucontext32(struct lwp *l, const ucontext32_t *ucp)
    369 {
    370 	struct proc	*p;
    371 	int		error;
    372 
    373 	p = l->l_proc;
    374 	if ((error = cpu_setmcontext32(l, &ucp->uc_mcontext,
    375 	     ucp->uc_flags)) != 0)
    376 		return (error);
    377 	l->l_ctxlink = (void *)(intptr_t)ucp->uc_link;
    378 	/*
    379 	 * We might want to take care of the stack portion here but currently
    380 	 * don't; see the comment in getucontext().
    381 	 */
    382 	if ((ucp->uc_flags & _UC_SIGMASK) != 0)
    383 		sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
    384 
    385 	return 0;
    386 }
    387 
    388 /* ARGSUSED */
    389 int
    390 netbsd32_setcontext(struct lwp *l, void *v, register_t *retval)
    391 {
    392 	struct netbsd32_setcontext_args /* {
    393 		syscallarg(netbsd32_ucontextp) ucp;
    394 	} */ *uap = v;
    395 	ucontext32_t uc;
    396 	int error;
    397 	void *p;
    398 
    399 	p = NETBSD32PTR64(SCARG(uap, ucp));
    400 	if (p == NULL)
    401 		exit1(l, W_EXITCODE(0, 0));
    402 	else if ((error = copyin(p, &uc, sizeof (uc))) != 0 ||
    403 	    (error = setucontext32(l, &uc)) != 0)
    404 		return (error);
    405 
    406 	return (EJUSTRETURN);
    407 }
    408 
    409 static int
    410 netbsd32_sigtimedwait_put_info(const void *src, void *dst, size_t size)
    411 {
    412 	const siginfo_t *info = src;
    413 	siginfo32_t info32;
    414 
    415 	netbsd32_si_to_si32(&info32, info);
    416 
    417 	return copyout(&info32, dst, sizeof(info32));
    418 }
    419 
    420 static int
    421 netbsd32_sigtimedwait_fetch_timeout(const void *src, void *dst, size_t size)
    422 {
    423 	struct timespec *ts = dst;
    424 	struct netbsd32_timespec ts32;
    425 	int error;
    426 
    427 	error = copyin(src, &ts32, sizeof(ts32));
    428 	if (error)
    429 		return error;
    430 
    431 	netbsd32_to_timespec(&ts32, ts);
    432 	return 0;
    433 }
    434 
    435 static int
    436 netbsd32_sigtimedwait_put_timeout(const void *src, void *dst, size_t size)
    437 {
    438 	const struct timespec *ts = src;
    439 	struct netbsd32_timespec ts32;
    440 
    441 	netbsd32_from_timespec(ts, &ts32);
    442 
    443 	return copyout(&ts32, dst, sizeof(ts32));
    444 }
    445 
    446 int
    447 netbsd32___sigtimedwait(struct lwp *l, void *v, register_t *retval)
    448 {
    449 	struct netbsd32___sigtimedwait_args /* {
    450 		syscallarg(netbsd32_sigsetp_t) set;
    451 		syscallarg(netbsd32_siginfop_t) info;
    452 		syscallarg(netbsd32_timespecp_t) timeout;
    453 	} */ *uap = v;
    454 	struct sys___sigtimedwait_args ua;
    455 
    456 	NETBSD32TOP_UAP(set, const sigset_t);
    457 	NETBSD32TOP_UAP(info, siginfo_t);
    458 	NETBSD32TOP_UAP(timeout, struct timespec);
    459 
    460 	return __sigtimedwait1(l, &ua, retval, netbsd32_sigtimedwait_put_info,
    461 	    netbsd32_sigtimedwait_fetch_timeout,
    462 	    netbsd32_sigtimedwait_put_timeout);
    463 }
    464