Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_signal.c revision 1.19.2.2
      1 /*	$NetBSD: netbsd32_signal.c,v 1.19.2.2 2007/03/12 05:52:32 rmind 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.19.2.2 2007/03/12 05:52:32 rmind 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, 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((void *)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,
    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, (void *)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((void *)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, 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, (void *)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 netbsd32_sigaction sa32;
    202 	struct sigaction nsa, osa;
    203 	int error;
    204 
    205 	if (SCARG(uap, nsa)) {
    206 		error = copyin((void *)NETBSD32PTR64(SCARG(uap, nsa)), &sa32,
    207 		    sizeof(sa32));
    208 		if (error)
    209 			return (error);
    210 		nsa.sa_handler = (void *)NETBSD32PTR64(sa32.netbsd32_sa_handler);
    211 		nsa.sa_mask = sa32.netbsd32_sa_mask;
    212 		nsa.sa_flags = sa32.netbsd32_sa_flags;
    213 	}
    214 	error = sigaction1(l, SCARG(uap, signum),
    215 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
    216 	    NETBSD32PTR64(SCARG(uap, tramp)), SCARG(uap, vers));
    217 	if (error)
    218 		return (error);
    219 	if (SCARG(uap, osa)) {
    220 		sa32.netbsd32_sa_handler = (netbsd32_voidp)(u_long)osa.sa_handler;
    221 		sa32.netbsd32_sa_mask = osa.sa_mask;
    222 		sa32.netbsd32_sa_flags = osa.sa_flags;
    223 		error = copyout(&sa32, (void *)NETBSD32PTR64(SCARG(uap, osa)),
    224 		    sizeof(sa32));
    225 		if (error)
    226 			return (error);
    227 	}
    228 	return (0);
    229 }
    230 
    231 #ifdef unused
    232 static void
    233 netbsd32_si32_to_si(siginfo_t *si, const siginfo32_t *si32)
    234 {
    235 	memset(si, 0, sizeof (*si));
    236 	si->si_signo = si32->si_signo;
    237 	si->si_code = si32->si_code;
    238 	si->si_errno = si32->si_errno;
    239 
    240 	switch (si32->si_signo) {
    241 	case SIGILL:
    242 	case SIGBUS:
    243 	case SIGSEGV:
    244 	case SIGFPE:
    245 	case SIGTRAP:
    246 		si->si_addr = (void *)NETBSD32PTR64(si32->si_addr);
    247 		si->si_trap = si32->si_trap;
    248 		break;
    249 	case SIGALRM:
    250 	case SIGVTALRM:
    251 	case SIGPROF:
    252 		si->si_pid = si32->si_pid;
    253 		si->si_uid = si32->si_uid;
    254 		/*
    255 		 * XXX sival_ptr is currently unused.
    256 		 */
    257 		si->si_sigval.sival_int = si32->si_sigval.sival_int;
    258 		break;
    259 	case SIGCHLD:
    260 		si->si_pid = si32->si_pid;
    261 		si->si_uid = si32->si_uid;
    262 		si->si_utime = si32->si_utime;
    263 		si->si_stime = si32->si_stime;
    264 		break;
    265 	case SIGURG:
    266 	case SIGIO:
    267 		si->si_band = si32->si_band;
    268 		si->si_fd = si32->si_fd;
    269 		break;
    270 	}
    271 }
    272 #endif
    273 
    274 void
    275 netbsd32_si_to_si32(siginfo32_t *si32, const siginfo_t *si)
    276 {
    277 	memset(si32, 0, sizeof (*si32));
    278 	si32->si_signo = si->si_signo;
    279 	si32->si_code = si->si_code;
    280 	si32->si_errno = si->si_errno;
    281 
    282 	switch (si32->si_signo) {
    283 	case 0:	/* SA */
    284 		si32->si_sigval.sival_int = si->si_sigval.sival_int;
    285 		break;
    286 	case SIGILL:
    287 	case SIGBUS:
    288 	case SIGSEGV:
    289 	case SIGFPE:
    290 	case SIGTRAP:
    291 		si32->si_addr = (uint32_t)(uintptr_t)si->si_addr;
    292 		si32->si_trap = si->si_trap;
    293 		break;
    294 	case SIGALRM:
    295 	case SIGVTALRM:
    296 	case SIGPROF:
    297 		si32->si_pid = si->si_pid;
    298 		si32->si_uid = si->si_uid;
    299 		/*
    300 		 * XXX sival_ptr is currently unused.
    301 		 */
    302 		si32->si_sigval.sival_int = si->si_sigval.sival_int;
    303 		break;
    304 	case SIGCHLD:
    305 		si32->si_pid = si->si_pid;
    306 		si32->si_uid = si->si_uid;
    307 		si32->si_status = si->si_status;
    308 		si32->si_utime = si->si_utime;
    309 		si32->si_stime = si->si_stime;
    310 		break;
    311 	case SIGURG:
    312 	case SIGIO:
    313 		si32->si_band = si->si_band;
    314 		si32->si_fd = si->si_fd;
    315 		break;
    316 	}
    317 }
    318 
    319 void
    320 getucontext32(struct lwp *l, ucontext32_t *ucp)
    321 {
    322 	struct proc *p = l->l_proc;
    323 
    324 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    325 
    326 	ucp->uc_flags = 0;
    327 	ucp->uc_link = (uint32_t)(intptr_t)l->l_ctxlink;
    328 
    329 	ucp->uc_sigmask = l->l_sigmask;
    330 	ucp->uc_flags |= _UC_SIGMASK;
    331 
    332 	/*
    333 	 * The (unsupplied) definition of the `current execution stack'
    334 	 * in the System V Interface Definition appears to allow returning
    335 	 * the main context stack.
    336 	 */
    337 	if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
    338 		ucp->uc_stack.ss_sp = USRSTACK32;
    339 		ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
    340 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
    341 	} else {
    342 		/* Simply copy alternate signal execution stack. */
    343 		ucp->uc_stack.ss_sp =
    344 		    (uint32_t)(intptr_t)l->l_sigstk.ss_sp;
    345 		ucp->uc_stack.ss_size = l->l_sigstk.ss_size;
    346 		ucp->uc_stack.ss_flags = l->l_sigstk.ss_flags;
    347 	}
    348 	ucp->uc_flags |= _UC_STACK;
    349 	mutex_exit(&p->p_smutex);
    350 	cpu_getmcontext32(l, &ucp->uc_mcontext, &ucp->uc_flags);
    351 	mutex_enter(&p->p_smutex);
    352 }
    353 
    354 /* ARGSUSED */
    355 int
    356 netbsd32_getcontext(struct lwp *l, void *v, register_t *retval)
    357 {
    358 	struct netbsd32_getcontext_args /* {
    359 		syscallarg(netbsd32_ucontextp) ucp;
    360 	} */ *uap = v;
    361 	struct proc *p = l->l_proc;
    362 	ucontext32_t uc;
    363 
    364 	mutex_enter(&p->p_smutex);
    365 	getucontext32(l, &uc);
    366 	mutex_exit(&p->p_smutex);
    367 
    368 	return copyout(&uc, NETBSD32PTR64(SCARG(uap, ucp)),
    369 	    sizeof (ucontext32_t));
    370 }
    371 
    372 int
    373 setucontext32(struct lwp *l, const ucontext32_t *ucp)
    374 {
    375 	struct proc *p = l->l_proc;
    376 	int error;
    377 
    378 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    379 
    380 	if ((ucp->uc_flags & _UC_SIGMASK) != 0) {
    381 		error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL);
    382 		if (error != 0)
    383 			return error;
    384 	}
    385 
    386 	mutex_exit(&p->p_smutex);
    387 	error = cpu_setmcontext32(l, &ucp->uc_mcontext, ucp->uc_flags);
    388 	mutex_enter(&p->p_smutex);
    389 	if (error != 0)
    390 		return (error);
    391 
    392 	l->l_ctxlink = (void *)(intptr_t)ucp->uc_link;
    393 
    394 	/*
    395 	 * If there was stack information, update whether or not we are
    396 	 * still running on an alternate signal stack.
    397 	 */
    398 	if ((ucp->uc_flags & _UC_STACK) != 0) {
    399 		if (ucp->uc_stack.ss_flags & SS_ONSTACK)
    400 			l->l_sigstk.ss_flags |= SS_ONSTACK;
    401 		else
    402 			l->l_sigstk.ss_flags &= ~SS_ONSTACK;
    403 	}
    404 
    405 	return 0;
    406 }
    407 
    408 /* ARGSUSED */
    409 int
    410 netbsd32_setcontext(struct lwp *l, void *v, register_t *retval)
    411 {
    412 	struct netbsd32_setcontext_args /* {
    413 		syscallarg(netbsd32_ucontextp) ucp;
    414 	} */ *uap = v;
    415 	ucontext32_t uc;
    416 	int error;
    417 	struct proc *p = l->l_proc;
    418 
    419 	error = copyin(NETBSD32PTR64(SCARG(uap, ucp)), &uc, sizeof (uc));
    420 	if (error)
    421 		return (error);
    422 	if (!(uc.uc_flags & _UC_CPU))
    423 		return (EINVAL);
    424 	mutex_enter(&p->p_smutex);
    425 	error = setucontext32(l, &uc);
    426 	mutex_exit(&p->p_smutex);
    427 	if (error)
    428 		return (error);
    429 
    430 	return (EJUSTRETURN);
    431 }
    432 
    433 static int
    434 netbsd32_sigtimedwait_put_info(const void *src, void *dst, size_t size)
    435 {
    436 	const siginfo_t *info = src;
    437 	siginfo32_t info32;
    438 
    439 	netbsd32_si_to_si32(&info32, info);
    440 
    441 	return copyout(&info32, dst, sizeof(info32));
    442 }
    443 
    444 static int
    445 netbsd32_sigtimedwait_fetch_timeout(const void *src, void *dst, size_t size)
    446 {
    447 	struct timespec *ts = dst;
    448 	struct netbsd32_timespec ts32;
    449 	int error;
    450 
    451 	error = copyin(src, &ts32, sizeof(ts32));
    452 	if (error)
    453 		return error;
    454 
    455 	netbsd32_to_timespec(&ts32, ts);
    456 	return 0;
    457 }
    458 
    459 static int
    460 netbsd32_sigtimedwait_put_timeout(const void *src, void *dst, size_t size)
    461 {
    462 	const struct timespec *ts = src;
    463 	struct netbsd32_timespec ts32;
    464 
    465 	netbsd32_from_timespec(ts, &ts32);
    466 
    467 	return copyout(&ts32, dst, sizeof(ts32));
    468 }
    469 
    470 int
    471 netbsd32___sigtimedwait(struct lwp *l, void *v, register_t *retval)
    472 {
    473 	struct netbsd32___sigtimedwait_args /* {
    474 		syscallarg(netbsd32_sigsetp_t) set;
    475 		syscallarg(netbsd32_siginfop_t) info;
    476 		syscallarg(netbsd32_timespecp_t) timeout;
    477 	} */ *uap = v;
    478 	struct sys___sigtimedwait_args ua;
    479 
    480 	NETBSD32TOP_UAP(set, const sigset_t);
    481 	NETBSD32TOP_UAP(info, siginfo_t);
    482 	NETBSD32TOP_UAP(timeout, struct timespec);
    483 
    484 	return __sigtimedwait1(l, &ua, retval, netbsd32_sigtimedwait_put_info,
    485 	    netbsd32_sigtimedwait_fetch_timeout,
    486 	    netbsd32_sigtimedwait_put_timeout);
    487 }
    488