Home | History | Annotate | Line # | Download | only in netbsd32
      1 /*	$NetBSD: netbsd32_sem.c,v 1.12 2019/02/03 03:20:23 thorpej Exp $	*/
      2 
      3 /*
      4  *  Copyright (c) 2006 The NetBSD Foundation.
      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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  *  POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_sem.c,v 1.12 2019/02/03 03:20:23 thorpej Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/kernel.h>
     36 #include <sys/dirent.h>
     37 #include <sys/condvar.h>
     38 #include <sys/ksem.h>
     39 #include <sys/mount.h>
     40 #include <sys/proc.h>
     41 #include <sys/syscallargs.h>
     42 
     43 #include <compat/netbsd32/netbsd32.h>
     44 #include <compat/netbsd32/netbsd32_syscallargs.h>
     45 #include <compat/netbsd32/netbsd32_conv.h>
     46 
     47 static int
     48 netbsd32_ksem_copyin(const void *src, void *dst, size_t size)
     49 {
     50 	intptr_t *argp = dst;
     51 	netbsd32_intptr_t arg32;
     52 	int error;
     53 
     54 	KASSERT(size == sizeof(intptr_t));
     55 
     56 	if ((error = copyin(src, &arg32, sizeof(arg32))) != 0)
     57 		return error;
     58 	*argp = arg32;
     59 	return 0;
     60 }
     61 
     62 static int
     63 netbsd32_ksem_copyout(const void *src, void *dst, size_t size)
     64 {
     65 	const intptr_t *idp = src;
     66 	netbsd32_intptr_t id32, *outidp = dst;
     67 
     68 	KASSERT(size == sizeof(intptr_t));
     69 
     70 	/* Returning a kernel pointer to userspace sucks badly :-( */
     71 	/* (Luckily, it's not actually a pointer. --thorpej */
     72 	id32 = (netbsd32_intptr_t)*idp;
     73 	return copyout(&id32, outidp, sizeof(id32));
     74 }
     75 
     76 int
     77 netbsd32__ksem_init(struct lwp *l, const struct netbsd32__ksem_init_args *uap, register_t *retval)
     78 {
     79 	/* {
     80 		syscallarg(unsigned int) value;
     81 		syscallarg(netbsd32_semidp_t) idp;
     82 	} */
     83 
     84 	return do_ksem_init(l, SCARG(uap, value),
     85 	    SCARG_P32(uap, idp), netbsd32_ksem_copyin, netbsd32_ksem_copyout);
     86 }
     87 
     88 int
     89 netbsd32__ksem_open(struct lwp *l, const struct netbsd32__ksem_open_args *uap, register_t *retval)
     90 {
     91 	/* {
     92 		syscallarg(const netbsd32_charp) name;
     93 		syscallarg(int) oflag;
     94 		syscallarg(mode_t) mode;
     95 		syscallarg(unsigned int) value;
     96 		syscallarg(netbsd32_semidp_t) idp;
     97 	} */
     98 
     99 	return do_ksem_open(l, SCARG_P32(uap, name),
    100 	    SCARG(uap, oflag), SCARG(uap, mode), SCARG(uap, value),
    101 	    SCARG_P32(uap, idp), netbsd32_ksem_copyout);
    102 }
    103 
    104 int
    105 netbsd32__ksem_unlink(struct lwp *l, const struct netbsd32__ksem_unlink_args *uap, register_t *retval)
    106 {
    107 	/* {
    108 		syscallarg(const netbsd32_charp) name;
    109 	} */
    110 	struct sys__ksem_unlink_args ua;
    111 
    112 	NETBSD32TOP_UAP(name, const char);
    113 	return sys__ksem_unlink(l, &ua, retval);
    114 }
    115 
    116 int
    117 netbsd32__ksem_close(struct lwp *l, const struct netbsd32__ksem_close_args *uap, register_t *retval)
    118 {
    119 	/* {
    120 		syscallarg(netbsd32_intptr_t) id;
    121 	} */
    122 	struct sys__ksem_close_args ua;
    123 
    124 	NETBSD32TOX_UAP(id, intptr_t);
    125 	return sys__ksem_close(l, &ua, retval);
    126 }
    127 
    128 int
    129 netbsd32__ksem_post(struct lwp *l, const struct netbsd32__ksem_post_args *uap, register_t *retval)
    130 {
    131 	/* {
    132 		syscallarg(netbsd32_intptr_t) id;
    133 	} */
    134 	struct sys__ksem_post_args ua;
    135 
    136 	NETBSD32TOX_UAP(id, intptr_t);
    137 	return sys__ksem_post(l, &ua, retval);
    138 }
    139 
    140 int
    141 netbsd32__ksem_wait(struct lwp *l, const struct netbsd32__ksem_wait_args *uap, register_t *retval)
    142 {
    143 	/* {
    144 		syscallarg(netbsd32_intptr_t) id;
    145 	} */
    146 
    147 	return do_ksem_wait(l, SCARG(uap, id), false, NULL);
    148 }
    149 
    150 int
    151 netbsd32__ksem_trywait(struct lwp *l, const struct netbsd32__ksem_trywait_args *uap, register_t *retval)
    152 {
    153 	/* {
    154 		syscallarg(netbsd32_intptr_t) id;
    155 	} */
    156 
    157 	return do_ksem_wait(l, SCARG(uap, id), true, NULL);
    158 }
    159 
    160 int
    161 netbsd32__ksem_timedwait(struct lwp *l, const struct netbsd32__ksem_timedwait_args *uap,
    162     register_t *retval)
    163 {
    164 	/* {
    165 		intptr_t id;
    166 		const netbsd32_timespecp_t abstime;
    167 	} */
    168 	struct netbsd32_timespec ts32;
    169 	struct timespec ts;
    170 	intptr_t id;
    171 	int error;
    172 
    173 	id = SCARG(uap, id);
    174 
    175 	error = copyin(SCARG_P32(uap, abstime), &ts32, sizeof(ts32));
    176 	if (error != 0)
    177 		return error;
    178 	netbsd32_to_timespec(&ts32, &ts);
    179 
    180 	if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
    181 		return EINVAL;
    182 
    183 	error = do_ksem_wait(l, id, false, &ts);
    184 	if (error == EWOULDBLOCK)
    185 		error = ETIMEDOUT;
    186 	return error;
    187 }
    188 
    189 int
    190 netbsd32__ksem_destroy(struct lwp *l, const struct netbsd32__ksem_destroy_args *uap, register_t *retval)
    191 {
    192 	/* {
    193 		syscallarg(netbsd32_intptr_t) id;
    194 	} */
    195 	struct sys__ksem_destroy_args ua;
    196 
    197 	NETBSD32TOX_UAP(id, intptr_t);
    198 	return sys__ksem_destroy(l, &ua, retval);
    199 }
    200 
    201 int
    202 netbsd32__ksem_getvalue(struct lwp *l, const struct netbsd32__ksem_getvalue_args *uap, register_t *retval)
    203 {
    204 	/* {
    205 		syscallarg(netbsd32_intptr_t) id;
    206 		syscallarg(netbsd32_intp) value;
    207 	} */
    208 	struct sys__ksem_getvalue_args ua;
    209 
    210 	NETBSD32TOX_UAP(id, intptr_t);
    211 	NETBSD32TOP_UAP(value, unsigned int);
    212 	return sys__ksem_getvalue(l, &ua, retval);
    213 }
    214