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