1 /* $NetBSD: netbsd32_ipc.c,v 1.23 2025/05/23 09:47:34 hannken 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_ipc.c,v 1.23 2025/05/23 09:47:34 hannken Exp $"); 31 32 #if defined(_KERNEL_OPT) 33 #include "opt_sysv.h" 34 #endif 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/ipc.h> 39 #include <sys/msg.h> 40 #include <sys/sem.h> 41 #include <sys/shm.h> 42 #include <sys/mount.h> 43 #include <sys/module.h> 44 #include <sys/dirent.h> 45 #include <sys/syscallvar.h> 46 47 #include <sys/syscallargs.h> 48 #include <sys/proc.h> 49 50 #include <compat/netbsd32/netbsd32.h> 51 #include <compat/netbsd32/netbsd32_syscall.h> 52 #include <compat/netbsd32/netbsd32_syscallargs.h> 53 #include <compat/netbsd32/netbsd32_conv.h> 54 55 #define _PKG_ENTRY(name) \ 56 { NETBSD32_SYS_ ## name, 0, (sy_call_t *)name } 57 58 #define _PKG_ENTRY2(code, name) \ 59 { NETBSD32_SYS_ ## code, 0, (sy_call_t *)name } 60 61 static const struct syscall_package compat_sysvipc_syscalls[] = { 62 #if defined(SYSVSEM) 63 _PKG_ENTRY(netbsd32_____semctl50), 64 _PKG_ENTRY(netbsd32_semget), 65 _PKG_ENTRY(netbsd32_semop), 66 _PKG_ENTRY(netbsd32_semconfig), 67 _PKG_ENTRY(netbsd32_semtimedop), 68 #endif /* SYSVSEM */ 69 70 #if defined(SYSVSHM) 71 _PKG_ENTRY(netbsd32_shmat), 72 _PKG_ENTRY(netbsd32___shmctl50), 73 _PKG_ENTRY(netbsd32_shmdt), 74 _PKG_ENTRY(netbsd32_shmget), 75 #endif /* SYSVSHM */ 76 77 #if defined(SYSVMSG) 78 _PKG_ENTRY(netbsd32___msgctl50), 79 _PKG_ENTRY(netbsd32_msgget), 80 _PKG_ENTRY(netbsd32_msgsnd), 81 _PKG_ENTRY(netbsd32_msgrcv), 82 #endif /* SYSVMSG */ 83 { 0, 0, NULL } 84 }; 85 86 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_sysvipc, 87 "compat_netbsd32,sysv_ipc"); 88 89 #ifdef SEM_DEBUG 90 #define SEM_PRINTF(a) printf a 91 #else 92 #define SEM_PRINTF(a) 93 #endif 94 95 static int 96 compat_netbsd32_sysvipc_modcmd(modcmd_t cmd, void *arg) 97 { 98 int error; 99 100 switch (cmd) { 101 case MODULE_CMD_INIT: 102 error = syscall_establish(&emul_netbsd32, 103 compat_sysvipc_syscalls); 104 break; 105 case MODULE_CMD_FINI: 106 error = syscall_disestablish(&emul_netbsd32, 107 compat_sysvipc_syscalls); 108 break; 109 default: 110 error = ENOTTY; 111 break; 112 } 113 return error; 114 } 115 116 #if defined(SYSVSEM) 117 118 int 119 netbsd32_____semctl50(struct lwp *l, 120 const struct netbsd32_____semctl50_args *uap, 121 register_t *retval) 122 { 123 /* { 124 syscallarg(int) semid; 125 syscallarg(int) semnum; 126 syscallarg(int) cmd; 127 syscallarg(netbsd32_semunp_t) arg; 128 } */ 129 struct semid_ds sembuf; 130 struct netbsd32_semid_ds sembuf32; 131 int cmd, error; 132 void *pass_arg; 133 union __semun karg; 134 union netbsd32_semun karg32; 135 136 cmd = SCARG(uap, cmd); 137 138 switch (cmd) { 139 case IPC_SET: 140 case IPC_STAT: 141 pass_arg = &sembuf; 142 break; 143 144 case GETALL: 145 case SETVAL: 146 case SETALL: 147 pass_arg = &karg; 148 break; 149 default: 150 pass_arg = NULL; 151 break; 152 } 153 154 if (pass_arg) { 155 error = copyin(SCARG_P32(uap, arg), &karg32, sizeof(karg32)); 156 if (error) 157 return error; 158 if (pass_arg == &karg) { 159 switch (cmd) { 160 case GETALL: 161 case SETALL: 162 karg.array = NETBSD32PTR64(karg32.array); 163 break; 164 case SETVAL: 165 karg.val = karg32.val; 166 break; 167 } 168 } 169 if (cmd == IPC_SET) { 170 error = copyin(NETBSD32PTR64(karg32.buf), &sembuf32, 171 sizeof(sembuf32)); 172 if (error) 173 return error; 174 netbsd32_to_semid_ds(&sembuf32, &sembuf); 175 } 176 } 177 178 error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd, 179 pass_arg, retval); 180 181 if (error == 0 && cmd == IPC_STAT) { 182 netbsd32_from_semid_ds(&sembuf, &sembuf32); 183 error = copyout(&sembuf32, NETBSD32PTR64(karg32.buf), 184 sizeof(sembuf32)); 185 } 186 187 return error; 188 } 189 190 int 191 netbsd32_semget(struct lwp *l, const struct netbsd32_semget_args *uap, 192 register_t *retval) 193 { 194 /* { 195 syscallarg(netbsd32_key_t) key; 196 syscallarg(int) nsems; 197 syscallarg(int) semflg; 198 } */ 199 struct sys_semget_args ua; 200 201 NETBSD32TOX_UAP(key, key_t); 202 NETBSD32TO64_UAP(nsems); 203 NETBSD32TO64_UAP(semflg); 204 return sys_semget(l, &ua, retval); 205 } 206 207 #define SMALL_SOPS 8 208 209 CTASSERT(sizeof(struct netbsd32_sembuf) == sizeof(struct sembuf)); 210 211 static int 212 netbsd32_do_semop(struct lwp *l, int semid, const netbsd32_sembufp_t usops, 213 size_t nsops, const struct netbsd32_timespec *utimeout, register_t *retval) 214 { 215 struct netbsd32_sembuf small_sops[SMALL_SOPS]; 216 struct netbsd32_sembuf *sops; 217 struct netbsd32_timespec ts32; 218 struct timespec timeout; 219 int error; 220 221 do_semop_init(); 222 223 SEM_PRINTF(("do_semop(%d, %p, %zu)\n", semid, NETBSD32PTR64(usops), 224 nsops)); 225 226 if (nsops <= SMALL_SOPS) { 227 sops = small_sops; 228 } else if (seminfo.semopm > 0 && nsops <= (size_t)seminfo.semopm) { 229 sops = kmem_alloc(nsops * sizeof(*sops), KM_SLEEP); 230 } else { 231 SEM_PRINTF(("too many sops (max=%d, nsops=%zu)\n", 232 seminfo.semopm, nsops)); 233 return (E2BIG); 234 } 235 236 /* netbsd32_sembuf == sembuf, see CTASSERT above */ 237 error = copyin(NETBSD32PTR64(usops), sops, nsops * sizeof(sops[0])); 238 if (error) { 239 SEM_PRINTF(("error = %d from copyin(%p, %p, %zu)\n", error, 240 NETBSD32PTR64(usops), &sops, nsops * sizeof(sops[0]))); 241 if (sops != small_sops) 242 kmem_free(sops, nsops * sizeof(*sops)); 243 return error; 244 } 245 246 if (utimeout) { 247 error = copyin(utimeout, &ts32, sizeof(ts32)); 248 if (error) { 249 SEM_PRINTF(("error = %d from copyin(%p, %p, %zu)\n", 250 error, utimeout, &ts32, sizeof(ts32))); 251 return error; 252 } 253 netbsd32_to_timespec(&ts32, &timeout); 254 } 255 256 error = do_semop1(l, semid, (struct sembuf*)sops, nsops, 257 utimeout ? &timeout : NULL, retval); 258 259 if (sops != small_sops) 260 kmem_free(sops, nsops * sizeof(*sops)); 261 262 return error; 263 } 264 265 int 266 netbsd32_semop(struct lwp *l, const struct netbsd32_semop_args *uap, 267 register_t *retval) 268 { 269 /* { 270 syscallarg(int) semid; 271 syscallarg(netbsd32_sembufp_t) sops; 272 syscallarg(netbsd32_size_t) nsops; 273 } */ 274 275 return netbsd32_do_semop(l, SCARG(uap, semid), SCARG(uap,sops), 276 SCARG(uap, nsops), NULL, retval); 277 } 278 279 int 280 netbsd32_semtimedop(struct lwp *l, const struct netbsd32_semtimedop_args *uap, 281 register_t *retval) 282 { 283 /* { 284 syscallarg(int) semid; 285 syscallarg(netbsd32_sembufp_t) sops; 286 syscallarg(netbsd32_size_t) nsops; 287 syscallarg(netbsd32_timespecp_t) timeout; 288 } */ 289 290 return netbsd32_do_semop(l, SCARG(uap, semid), SCARG(uap,sops), 291 SCARG(uap, nsops), SCARG_P32(uap,timeout), retval); 292 } 293 294 int 295 netbsd32_semconfig(struct lwp *l, const struct netbsd32_semconfig_args *uap, 296 register_t *retval) 297 { 298 /* { 299 syscallarg(int) flag; 300 } */ 301 struct sys_semconfig_args ua; 302 303 NETBSD32TO64_UAP(flag); 304 return sys_semconfig(l, &ua, retval); 305 } 306 #endif /* SYSVSEM */ 307 308 #if defined(SYSVMSG) 309 310 int 311 netbsd32___msgctl50(struct lwp *l, const struct netbsd32___msgctl50_args *uap, 312 register_t *retval) 313 { 314 /* { 315 syscallarg(int) msqid; 316 syscallarg(int) cmd; 317 syscallarg(netbsd32_msqid_dsp_t) buf; 318 } */ 319 struct msqid_ds ds; 320 struct netbsd32_msqid_ds ds32; 321 int error, cmd; 322 323 cmd = SCARG(uap, cmd); 324 if (cmd == IPC_SET) { 325 error = copyin(SCARG_P32(uap, buf), &ds32, sizeof(ds32)); 326 if (error) 327 return error; 328 netbsd32_to_msqid_ds(&ds32, &ds); 329 } 330 331 error = msgctl1(l, SCARG(uap, msqid), cmd, 332 (cmd == IPC_SET || cmd == IPC_STAT) ? &ds : NULL); 333 334 if (error == 0 && cmd == IPC_STAT) { 335 netbsd32_from_msqid_ds(&ds, &ds32); 336 error = copyout(&ds32, SCARG_P32(uap, buf), sizeof(ds32)); 337 } 338 339 return error; 340 } 341 342 int 343 netbsd32_msgget(struct lwp *l, const struct netbsd32_msgget_args *uap, 344 register_t *retval) 345 { 346 /* { 347 syscallarg(netbsd32_key_t) key; 348 syscallarg(int) msgflg; 349 } */ 350 struct sys_msgget_args ua; 351 352 NETBSD32TOX_UAP(key, key_t); 353 NETBSD32TO64_UAP(msgflg); 354 return sys_msgget(l, &ua, retval); 355 } 356 357 static int 358 netbsd32_msgsnd_fetch_type(const void *src, void *dst, size_t size) 359 { 360 netbsd32_long l32; 361 long *l = dst; 362 int error; 363 364 KASSERT(size == sizeof(netbsd32_long)); 365 366 error = copyin(src, &l32, sizeof(l32)); 367 if (!error) 368 *l = l32; 369 return error; 370 } 371 372 int 373 netbsd32_msgsnd(struct lwp *l, const struct netbsd32_msgsnd_args *uap, 374 register_t *retval) 375 { 376 /* { 377 syscallarg(int) msqid; 378 syscallarg(const netbsd32_voidp) msgp; 379 syscallarg(netbsd32_size_t) msgsz; 380 syscallarg(int) msgflg; 381 } */ 382 383 return msgsnd1(l, SCARG(uap, msqid), 384 SCARG_P32(uap, msgp), SCARG(uap, msgsz), 385 SCARG(uap, msgflg), sizeof(netbsd32_long), 386 netbsd32_msgsnd_fetch_type); 387 } 388 389 static int 390 netbsd32_msgrcv_put_type(const void *src, void *dst, size_t size) 391 { 392 netbsd32_long l32; 393 const long *l = src; 394 395 KASSERT(size == sizeof(netbsd32_long)); 396 397 l32 = (netbsd32_long)(*l); 398 return copyout(&l32, dst, sizeof(l32)); 399 } 400 401 int 402 netbsd32_msgrcv(struct lwp *l, const struct netbsd32_msgrcv_args *uap, 403 register_t *retval) 404 { 405 /* { 406 syscallarg(int) msqid; 407 syscallarg(netbsd32_voidp) msgp; 408 syscallarg(netbsd32_size_t) msgsz; 409 syscallarg(netbsd32_long) msgtyp; 410 syscallarg(int) msgflg; 411 } */ 412 413 return msgrcv1(l, SCARG(uap, msqid), 414 SCARG_P32(uap, msgp), SCARG(uap, msgsz), 415 SCARG(uap, msgtyp), SCARG(uap, msgflg), sizeof(netbsd32_long), 416 netbsd32_msgrcv_put_type, retval); 417 } 418 #endif /* SYSVMSG */ 419 420 #if defined(SYSVSHM) 421 422 int 423 netbsd32_shmat(struct lwp *l, const struct netbsd32_shmat_args *uap, 424 register_t *retval) 425 { 426 /* { 427 syscallarg(int) shmid; 428 syscallarg(const netbsd32_voidp) shmaddr; 429 syscallarg(int) shmflg; 430 } */ 431 struct sys_shmat_args ua; 432 433 NETBSD32TO64_UAP(shmid); 434 NETBSD32TOP_UAP(shmaddr, void); 435 NETBSD32TO64_UAP(shmflg); 436 return sys_shmat(l, &ua, retval); 437 } 438 439 int 440 netbsd32___shmctl50(struct lwp *l, const struct netbsd32___shmctl50_args *uap, 441 register_t *retval) 442 { 443 /* { 444 syscallarg(int) shmid; 445 syscallarg(int) cmd; 446 syscallarg(netbsd32_shmid_dsp_t) buf; 447 } */ 448 struct shmid_ds ds; 449 struct netbsd32_shmid_ds ds32; 450 int error, cmd; 451 452 cmd = SCARG(uap, cmd); 453 if (cmd == IPC_SET) { 454 error = copyin(SCARG_P32(uap, buf), &ds32, sizeof(ds32)); 455 if (error) 456 return error; 457 netbsd32_to_shmid_ds(&ds32, &ds); 458 } 459 460 error = shmctl1(l, SCARG(uap, shmid), cmd, 461 (cmd == IPC_SET || cmd == IPC_STAT) ? &ds : NULL); 462 463 if (error == 0 && cmd == IPC_STAT) { 464 netbsd32_from_shmid_ds(&ds, &ds32); 465 error = copyout(&ds32, SCARG_P32(uap, buf), sizeof(ds32)); 466 } 467 468 return error; 469 } 470 471 int 472 netbsd32_shmdt(struct lwp *l, const struct netbsd32_shmdt_args *uap, 473 register_t *retval) 474 { 475 /* { 476 syscallarg(const netbsd32_voidp) shmaddr; 477 } */ 478 struct sys_shmdt_args ua; 479 480 NETBSD32TOP_UAP(shmaddr, const char); 481 return sys_shmdt(l, &ua, retval); 482 } 483 484 int 485 netbsd32_shmget(struct lwp *l, const struct netbsd32_shmget_args *uap, 486 register_t *retval) 487 { 488 /* { 489 syscallarg(netbsd32_key_t) key; 490 syscallarg(netbsd32_size_t) size; 491 syscallarg(int) shmflg; 492 } */ 493 struct sys_shmget_args ua; 494 495 NETBSD32TOX_UAP(key, key_t); 496 NETBSD32TOX_UAP(size, size_t); 497 NETBSD32TO64_UAP(shmflg); 498 return sys_shmget(l, &ua, retval); 499 } 500 #endif /* SYSVSHM */ 501