Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_signal.c revision 1.13
      1 /*	$NetBSD: netbsd32_signal.c,v 1.13 2005/09/24 17:29:47 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.13 2005/09/24 17:29:47 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 int
     57 netbsd32_sigaction(l, v, retval)
     58 	struct lwp *l;
     59 	void *v;
     60 	register_t *retval;
     61 {
     62 	struct netbsd32_sigaction_args /* {
     63 		syscallarg(int) signum;
     64 		syscallarg(const netbsd32_sigactionp_t) nsa;
     65 		syscallarg(netbsd32_sigactionp_t) osa;
     66 	} */ *uap = v;
     67 	struct sigaction nsa, osa;
     68 	struct netbsd32_sigaction *sa32p, sa32;
     69 	int error;
     70 
     71 	if (SCARG(uap, nsa)) {
     72 		sa32p =
     73 		    (struct netbsd32_sigaction *)NETBSD32PTR64(SCARG(uap, nsa));
     74 		if (copyin(sa32p, &sa32, sizeof(sa32)))
     75 			return EFAULT;
     76 		nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
     77 		nsa.sa_mask = sa32.netbsd32_sa_mask;
     78 		nsa.sa_flags = sa32.netbsd32_sa_flags;
     79 	}
     80 	error = sigaction1(l->l_proc, SCARG(uap, signum),
     81 			   SCARG(uap, nsa) ? &nsa : 0,
     82 			   SCARG(uap, osa) ? &osa : 0,
     83 			   NULL, 0);
     84 
     85 	if (error)
     86 		return (error);
     87 
     88 	if (SCARG(uap, osa)) {
     89 		sa32.netbsd32_sa_handler = (netbsd32_sigactionp_t)(u_long)osa.sa_handler;
     90 		sa32.netbsd32_sa_mask = osa.sa_mask;
     91 		sa32.netbsd32_sa_flags = osa.sa_flags;
     92 		sa32p =
     93 		    (struct netbsd32_sigaction *)NETBSD32PTR64(SCARG(uap, osa));
     94 		if (copyout(&sa32, sa32p, sizeof(sa32)))
     95 			return EFAULT;
     96 	}
     97 
     98 	return (0);
     99 }
    100 
    101 int
    102 netbsd32___sigaltstack14(l, v, retval)
    103 	struct lwp *l;
    104 	void *v;
    105 	register_t *retval;
    106 {
    107 	struct netbsd32___sigaltstack14_args /* {
    108 		syscallarg(const netbsd32_sigaltstackp_t) nss;
    109 		syscallarg(netbsd32_sigaltstackp_t) oss;
    110 	} */ *uap = v;
    111 	struct netbsd32_sigaltstack s32;
    112 	struct sigaltstack nss, oss;
    113 	int error;
    114 
    115 	if (SCARG(uap, nss)) {
    116 		error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, nss)), &s32,
    117 		    sizeof(s32));
    118 		if (error)
    119 			return (error);
    120 		nss.ss_sp = (void *)NETBSD32PTR64(s32.ss_sp);
    121 		nss.ss_size = (size_t)s32.ss_size;
    122 		nss.ss_flags = s32.ss_flags;
    123 	}
    124 	error = sigaltstack1(l->l_proc,
    125 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
    126 	if (error)
    127 		return (error);
    128 	if (SCARG(uap, oss)) {
    129 		s32.ss_sp = (netbsd32_voidp)(u_long)oss.ss_sp;
    130 		s32.ss_size = (netbsd32_size_t)oss.ss_size;
    131 		s32.ss_flags = oss.ss_flags;
    132 		error = copyout(&s32, (caddr_t)NETBSD32PTR64(SCARG(uap, oss)),
    133 		    sizeof(s32));
    134 		if (error)
    135 			return (error);
    136 	}
    137 	return (0);
    138 }
    139 
    140 /* ARGSUSED */
    141 int
    142 netbsd32___sigaction14(l, v, retval)
    143 	struct lwp *l;
    144 	void *v;
    145 	register_t *retval;
    146 {
    147 	struct netbsd32___sigaction14_args /* {
    148 		syscallarg(int) signum;
    149 		syscallarg(const struct sigaction *) nsa;
    150 		syscallarg(struct sigaction *) osa;
    151 	} */ *uap = v;
    152 	struct netbsd32_sigaction sa32;
    153 	struct sigaction nsa, osa;
    154 	int error;
    155 
    156 	if (SCARG(uap, nsa)) {
    157 		error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, nsa)), &sa32,
    158 		    sizeof(sa32));
    159 		if (error)
    160 			return (error);
    161 		nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
    162 		nsa.sa_mask = sa32.netbsd32_sa_mask;
    163 		nsa.sa_flags = sa32.netbsd32_sa_flags;
    164 	}
    165 	error = sigaction1(l->l_proc, SCARG(uap, signum),
    166 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    167 	    NULL, 0);
    168 	if (error)
    169 		return (error);
    170 	if (SCARG(uap, osa)) {
    171 		sa32.netbsd32_sa_handler = (netbsd32_voidp)(u_long)osa.sa_handler;
    172 		sa32.netbsd32_sa_mask = osa.sa_mask;
    173 		sa32.netbsd32_sa_flags = osa.sa_flags;
    174 		error = copyout(&sa32, (caddr_t)NETBSD32PTR64(SCARG(uap, osa)),
    175 		    sizeof(sa32));
    176 		if (error)
    177 			return (error);
    178 	}
    179 	return (0);
    180 }
    181 
    182 /* ARGSUSED */
    183 int
    184 netbsd32___sigaction_sigtramp(l, v, retval)
    185 	struct lwp *l;
    186 	void *v;
    187 	register_t *retval;
    188 {
    189 	struct netbsd32___sigaction_sigtramp_args /* {
    190 		syscallarg(int) signum;
    191 		syscallarg(const netbsd32_sigactionp_t) nsa;
    192 		syscallarg(netbsd32_sigactionp_t) osa;
    193 		syscallarg(netbsd32_voidp) tramp;
    194 		syscallarg(int) vers;
    195 	} */ *uap = v;
    196 	struct proc *p = l->l_proc;
    197 	struct netbsd32_sigaction sa32;
    198 	struct sigaction nsa, osa;
    199 	int error;
    200 
    201 	if (SCARG(uap, nsa)) {
    202 		error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, nsa)), &sa32,
    203 		    sizeof(sa32));
    204 		if (error)
    205 			return (error);
    206 		nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
    207 		nsa.sa_mask = sa32.netbsd32_sa_mask;
    208 		nsa.sa_flags = sa32.netbsd32_sa_flags;
    209 	}
    210 	error = sigaction1(p, SCARG(uap, signum),
    211 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    212 	    NETBSD32PTR64(SCARG(uap, tramp)), SCARG(uap, vers));
    213 	if (error)
    214 		return (error);
    215 	if (SCARG(uap, osa)) {
    216 		sa32.netbsd32_sa_handler = (netbsd32_voidp)(u_long)osa.sa_handler;
    217 		sa32.netbsd32_sa_mask = osa.sa_mask;
    218 		sa32.netbsd32_sa_flags = osa.sa_flags;
    219 		error = copyout(&sa32, (caddr_t)NETBSD32PTR64(SCARG(uap, osa)),
    220 		    sizeof(sa32));
    221 		if (error)
    222 			return (error);
    223 	}
    224 	return (0);
    225 }
    226 
    227 void
    228 netbsd32_si32_to_si(siginfo_t *si, const siginfo32_t *si32)
    229 {
    230 	memset(si, 0, sizeof (*si));
    231 	si->si_signo = si32->si_signo;
    232 	si->si_code = si32->si_code;
    233 	si->si_errno = si32->si_errno;
    234 
    235 	switch (si32->si_signo) {
    236 	case SIGILL:
    237 	case SIGBUS:
    238 	case SIGSEGV:
    239 	case SIGFPE:
    240 	case SIGTRAP:
    241 		si->si_addr = (void *)NETBSD32PTR64(si32->si_addr);
    242 		si->si_trap = si32->si_trap;
    243 		break;
    244 	case SIGALRM:
    245 	case SIGVTALRM:
    246 	case SIGPROF:
    247 		si->si_pid = si32->si_pid;
    248 		si->si_uid = si32->si_uid;
    249 		/*
    250 		 * XXX sival_ptr is currently unused.
    251 		 */
    252 		si->si_sigval.sival_int = si32->si_sigval.sival_int;
    253 		break;
    254 	case SIGCHLD:
    255 		si->si_pid = si32->si_pid;
    256 		si->si_uid = si32->si_uid;
    257 		si->si_utime = si32->si_utime;
    258 		si->si_stime = si32->si_stime;
    259 		break;
    260 	case SIGURG:
    261 	case SIGIO:
    262 		si->si_band = si32->si_band;
    263 		si->si_fd = si32->si_fd;
    264 		break;
    265 	}
    266 }
    267 
    268 void
    269 netbsd32_si_to_si32(siginfo32_t *si32, const siginfo_t *si)
    270 {
    271 	memset(si32, 0, sizeof (*si32));
    272 	si32->si_signo = si->si_signo;
    273 	si32->si_code = si->si_code;
    274 	si32->si_errno = si->si_errno;
    275 
    276 	switch (si32->si_signo) {
    277 	case SIGILL:
    278 	case SIGBUS:
    279 	case SIGSEGV:
    280 	case SIGFPE:
    281 	case SIGTRAP:
    282 		si32->si_addr = (uint32_t)(uintptr_t)si->si_addr;
    283 		si32->si_trap = si->si_trap;
    284 		break;
    285 	case SIGALRM:
    286 	case SIGVTALRM:
    287 	case SIGPROF:
    288 		si32->si_pid = si->si_pid;
    289 		si32->si_uid = si->si_uid;
    290 		/*
    291 		 * XXX sival_ptr is currently unused.
    292 		 */
    293 		si32->si_sigval.sival_int = si->si_sigval.sival_int;
    294 		break;
    295 	case SIGCHLD:
    296 		si32->si_pid = si->si_pid;
    297 		si32->si_uid = si->si_uid;
    298 		si32->si_status = si->si_status;
    299 		si32->si_utime = si->si_utime;
    300 		si32->si_stime = si->si_stime;
    301 		break;
    302 	case SIGURG:
    303 	case SIGIO:
    304 		si32->si_band = si->si_band;
    305 		si32->si_fd = si->si_fd;
    306 		break;
    307 	}
    308 }
    309 
    310 void
    311 getucontext32(struct lwp *l, ucontext32_t *ucp)
    312 {
    313 	struct proc	*p;
    314 
    315 	p = l->l_proc;
    316 
    317 	ucp->uc_flags = 0;
    318 	ucp->uc_link = (uint32_t)(intptr_t)l->l_ctxlink;
    319 
    320 	(void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
    321 	ucp->uc_flags |= _UC_SIGMASK;
    322 
    323 	/*
    324 	 * The (unsupplied) definition of the `current execution stack'
    325 	 * in the System V Interface Definition appears to allow returning
    326 	 * the main context stack.
    327 	 */
    328 	if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
    329 		ucp->uc_stack.ss_sp = USRSTACK32;
    330 		ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
    331 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
    332 	} else {
    333 		/* Simply copy alternate signal execution stack. */
    334 		ucp->uc_stack.ss_sp =
    335 		    (uint32_t)(intptr_t)p->p_sigctx.ps_sigstk.ss_sp;
    336 		ucp->uc_stack.ss_size = p->p_sigctx.ps_sigstk.ss_size;
    337 		ucp->uc_stack.ss_flags = p->p_sigctx.ps_sigstk.ss_flags;
    338 	}
    339 	ucp->uc_flags |= _UC_STACK;
    340 
    341 	cpu_getmcontext32(l, &ucp->uc_mcontext, &ucp->uc_flags);
    342 }
    343 
    344 /* ARGSUSED */
    345 int
    346 netbsd32_getcontext(struct lwp *l, void *v, register_t *retval)
    347 {
    348 	struct netbsd32_getcontext_args /* {
    349 		syscallarg(netbsd32_ucontextp) ucp;
    350 	} */ *uap = v;
    351 	ucontext32_t uc;
    352 
    353 	getucontext32(l, &uc);
    354 
    355 	return copyout(&uc, NETBSD32PTR64(SCARG(uap, ucp)),
    356 	    sizeof (ucontext32_t));
    357 }
    358 
    359 int
    360 setucontext32(struct lwp *l, const ucontext32_t *ucp)
    361 {
    362 	struct proc	*p;
    363 	int		error;
    364 
    365 	p = l->l_proc;
    366 	if ((error = cpu_setmcontext32(l, &ucp->uc_mcontext,
    367 	     ucp->uc_flags)) != 0)
    368 		return (error);
    369 	l->l_ctxlink = (void *)(intptr_t)ucp->uc_link;
    370 	/*
    371 	 * We might want to take care of the stack portion here but currently
    372 	 * don't; see the comment in getucontext().
    373 	 */
    374 	if ((ucp->uc_flags & _UC_SIGMASK) != 0)
    375 		sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
    376 
    377 	return 0;
    378 }
    379 
    380 /* ARGSUSED */
    381 int
    382 netbsd32_setcontext(struct lwp *l, void *v, register_t *retval)
    383 {
    384 	struct netbsd32_setcontext_args /* {
    385 		syscallarg(netbsd32_ucontextp) ucp;
    386 	} */ *uap = v;
    387 	ucontext32_t uc;
    388 	int error;
    389 	void *p;
    390 
    391 	p = NETBSD32PTR64(SCARG(uap, ucp));
    392 	if (p == NULL)
    393 		exit1(l, W_EXITCODE(0, 0));
    394 	else if ((error = copyin(p, &uc, sizeof (uc))) != 0 ||
    395 	    (error = setucontext32(l, &uc)) != 0)
    396 		return (error);
    397 
    398 	return (EJUSTRETURN);
    399 }
    400 
    401 static int
    402 netbsd32_sigtimedwait_put_info(const void *src, void *dst, size_t size)
    403 {
    404 	const siginfo_t *info = src;
    405 	siginfo32_t info32;
    406 
    407 	netbsd32_si_to_si32(&info32, info);
    408 
    409 	return copyout(&info32, dst, sizeof(info32));
    410 }
    411 
    412 static int
    413 netbsd32_sigtimedwait_fetch_timeout(const void *src, void *dst, size_t size)
    414 {
    415 	struct timespec *ts = dst;
    416 	struct netbsd32_timespec ts32;
    417 	int error;
    418 
    419 	error = copyin(src, &ts32, sizeof(ts32));
    420 	if (error)
    421 		return error;
    422 
    423 	netbsd32_to_timespec(&ts32, ts);
    424 	return 0;
    425 }
    426 
    427 static int
    428 netbsd32_sigtimedwait_put_timeout(const void *src, void *dst, size_t size)
    429 {
    430 	const struct timespec *ts = src;
    431 	struct netbsd32_timespec ts32;
    432 
    433 	netbsd32_from_timespec(ts, &ts32);
    434 
    435 	return copyout(&ts32, dst, sizeof(ts32));
    436 }
    437 
    438 int
    439 netbsd32___sigtimedwait(struct lwp *l, void *v, register_t *retval)
    440 {
    441 	struct netbsd32___sigtimedwait_args /* {
    442 		syscallarg(netbsd32_sigsetp_t) set;
    443 		syscallarg(netbsd32_siginfop_t) info;
    444 		syscallarg(netbsd32_timespecp_t) timeout;
    445 	} */ *uap = v;
    446 	struct sys___sigtimedwait_args ua;
    447 
    448 	NETBSD32TOP_UAP(set, const sigset_t);
    449 	NETBSD32TOP_UAP(info, siginfo_t);
    450 	NETBSD32TOP_UAP(timeout, struct timespec);
    451 
    452 	return __sigtimedwait1(l, &ua, retval, netbsd32_sigtimedwait_put_info,
    453 	    netbsd32_sigtimedwait_fetch_timeout,
    454 	    netbsd32_sigtimedwait_put_timeout);
    455 }
    456