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