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