1 /* $NetBSD: netbsd32_sem.c,v 1.13 2026/09/20 13:45:00 riastradh 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.13 2026/09/20 13:45:00 riastradh 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 memset(&ua, 0, sizeof(ua)); 113 NETBSD32TOP_UAP(name, const char); 114 return sys__ksem_unlink(l, &ua, retval); 115 } 116 117 int 118 netbsd32__ksem_close(struct lwp *l, const struct netbsd32__ksem_close_args *uap, register_t *retval) 119 { 120 /* { 121 syscallarg(netbsd32_intptr_t) id; 122 } */ 123 struct sys__ksem_close_args ua; 124 125 memset(&ua, 0, sizeof(ua)); 126 NETBSD32TOX_UAP(id, intptr_t); 127 return sys__ksem_close(l, &ua, retval); 128 } 129 130 int 131 netbsd32__ksem_post(struct lwp *l, const struct netbsd32__ksem_post_args *uap, register_t *retval) 132 { 133 /* { 134 syscallarg(netbsd32_intptr_t) id; 135 } */ 136 struct sys__ksem_post_args ua; 137 138 memset(&ua, 0, sizeof(ua)); 139 NETBSD32TOX_UAP(id, intptr_t); 140 return sys__ksem_post(l, &ua, retval); 141 } 142 143 int 144 netbsd32__ksem_wait(struct lwp *l, const struct netbsd32__ksem_wait_args *uap, register_t *retval) 145 { 146 /* { 147 syscallarg(netbsd32_intptr_t) id; 148 } */ 149 150 return do_ksem_wait(l, SCARG(uap, id), false, NULL); 151 } 152 153 int 154 netbsd32__ksem_trywait(struct lwp *l, const struct netbsd32__ksem_trywait_args *uap, register_t *retval) 155 { 156 /* { 157 syscallarg(netbsd32_intptr_t) id; 158 } */ 159 160 return do_ksem_wait(l, SCARG(uap, id), true, NULL); 161 } 162 163 int 164 netbsd32__ksem_timedwait(struct lwp *l, const struct netbsd32__ksem_timedwait_args *uap, 165 register_t *retval) 166 { 167 /* { 168 intptr_t id; 169 const netbsd32_timespecp_t abstime; 170 } */ 171 struct netbsd32_timespec ts32; 172 struct timespec ts; 173 intptr_t id; 174 int error; 175 176 id = SCARG(uap, id); 177 178 error = copyin(SCARG_P32(uap, abstime), &ts32, sizeof(ts32)); 179 if (error != 0) 180 return error; 181 netbsd32_to_timespec(&ts32, &ts); 182 183 if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000) 184 return EINVAL; 185 186 error = do_ksem_wait(l, id, false, &ts); 187 if (error == EWOULDBLOCK) 188 error = ETIMEDOUT; 189 return error; 190 } 191 192 int 193 netbsd32__ksem_destroy(struct lwp *l, const struct netbsd32__ksem_destroy_args *uap, register_t *retval) 194 { 195 /* { 196 syscallarg(netbsd32_intptr_t) id; 197 } */ 198 struct sys__ksem_destroy_args ua; 199 200 memset(&ua, 0, sizeof(ua)); 201 NETBSD32TOX_UAP(id, intptr_t); 202 return sys__ksem_destroy(l, &ua, retval); 203 } 204 205 int 206 netbsd32__ksem_getvalue(struct lwp *l, const struct netbsd32__ksem_getvalue_args *uap, register_t *retval) 207 { 208 /* { 209 syscallarg(netbsd32_intptr_t) id; 210 syscallarg(netbsd32_intp) value; 211 } */ 212 struct sys__ksem_getvalue_args ua; 213 214 memset(&ua, 0, sizeof(ua)); 215 NETBSD32TOX_UAP(id, intptr_t); 216 NETBSD32TOP_UAP(value, unsigned int); 217 return sys__ksem_getvalue(l, &ua, retval); 218 } 219