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