Home | History | Annotate | Line # | Download | only in netbsd32
      1 /*	$NetBSD: netbsd32_ipc.c,v 1.25 2026/09/20 13:45:00 riastradh 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.25 2026/09/20 13:45:00 riastradh 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 	memset(&ua, 0, sizeof(ua));
    202 	NETBSD32TOX_UAP(key, key_t);
    203 	NETBSD32TO64_UAP(nsems);
    204 	NETBSD32TO64_UAP(semflg);
    205 	return sys_semget(l, &ua, retval);
    206 }
    207 
    208 #define SMALL_SOPS	8
    209 
    210 CTASSERT(sizeof(struct netbsd32_sembuf) == sizeof(struct sembuf));
    211 
    212 static int
    213 netbsd32_do_semop(struct lwp *l, int semid, const netbsd32_sembufp_t usops,
    214     size_t nsops, const struct netbsd32_timespec *utimeout, register_t *retval)
    215 {
    216 	struct netbsd32_sembuf small_sops[SMALL_SOPS];
    217 	struct netbsd32_sembuf *sops = small_sops;
    218 	struct netbsd32_timespec ts32;
    219 	struct timespec timeout;
    220 	int error;
    221 
    222 	do_semop_init();
    223 
    224 	SEM_PRINTF(("do_semop(%d, %p, %zu)\n", semid, NETBSD32PTR64(usops),
    225 	    nsops));
    226 
    227 	if (nsops <= SMALL_SOPS) {
    228 		KASSERT(sops == small_sops);
    229 	} else if (seminfo.semopm > 0 && nsops <= (size_t)seminfo.semopm) {
    230 		sops = kmem_alloc(nsops * sizeof(*sops), KM_SLEEP);
    231 	} else {
    232 		SEM_PRINTF(("too many sops (max=%d, nsops=%zu)\n",
    233 		    seminfo.semopm, nsops));
    234 		error = E2BIG;
    235 		goto out;
    236 	}
    237 
    238 	/* netbsd32_sembuf == sembuf, see CTASSERT above */
    239 	error = copyin(NETBSD32PTR64(usops), sops, nsops * sizeof(sops[0]));
    240 	if (error) {
    241 		SEM_PRINTF(("error = %d from copyin(%p, %p, %zu)\n", error,
    242 		    NETBSD32PTR64(usops), &sops, nsops * sizeof(sops[0])));
    243 		goto out;
    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 			goto out;
    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 out:
    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 	memset(&ua, 0, sizeof(ua));
    304 	NETBSD32TO64_UAP(flag);
    305 	return sys_semconfig(l, &ua, retval);
    306 }
    307 #endif /* SYSVSEM */
    308 
    309 #if defined(SYSVMSG)
    310 
    311 int
    312 netbsd32___msgctl50(struct lwp *l, const struct netbsd32___msgctl50_args *uap,
    313     register_t *retval)
    314 {
    315 	/* {
    316 		syscallarg(int) msqid;
    317 		syscallarg(int) cmd;
    318 		syscallarg(netbsd32_msqid_dsp_t) buf;
    319 	} */
    320 	struct msqid_ds ds;
    321 	struct netbsd32_msqid_ds ds32;
    322 	int error, cmd;
    323 
    324 	cmd = SCARG(uap, cmd);
    325 	if (cmd == IPC_SET) {
    326 		error = copyin(SCARG_P32(uap, buf), &ds32, sizeof(ds32));
    327 		if (error)
    328 			return error;
    329 		netbsd32_to_msqid_ds(&ds32, &ds);
    330 	}
    331 
    332 	error = msgctl1(l, SCARG(uap, msqid), cmd,
    333 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &ds : NULL);
    334 
    335 	if (error == 0 && cmd == IPC_STAT) {
    336 		netbsd32_from_msqid_ds(&ds, &ds32);
    337 		error = copyout(&ds32, SCARG_P32(uap, buf), sizeof(ds32));
    338 	}
    339 
    340 	return error;
    341 }
    342 
    343 int
    344 netbsd32_msgget(struct lwp *l, const struct netbsd32_msgget_args *uap,
    345     register_t *retval)
    346 {
    347 	/* {
    348 		syscallarg(netbsd32_key_t) key;
    349 		syscallarg(int) msgflg;
    350 	} */
    351 	struct sys_msgget_args ua;
    352 
    353 	memset(&ua, 0, sizeof(ua));
    354 	NETBSD32TOX_UAP(key, key_t);
    355 	NETBSD32TO64_UAP(msgflg);
    356 	return sys_msgget(l, &ua, retval);
    357 }
    358 
    359 static int
    360 netbsd32_msgsnd_fetch_type(const void *src, void *dst, size_t size)
    361 {
    362 	netbsd32_long l32;
    363 	long *l = dst;
    364 	int error;
    365 
    366 	KASSERT(size == sizeof(netbsd32_long));
    367 
    368 	error = copyin(src, &l32, sizeof(l32));
    369 	if (!error)
    370 		*l = l32;
    371 	return error;
    372 }
    373 
    374 int
    375 netbsd32_msgsnd(struct lwp *l, const struct netbsd32_msgsnd_args *uap,
    376     register_t *retval)
    377 {
    378 	/* {
    379 		syscallarg(int) msqid;
    380 		syscallarg(const netbsd32_voidp) msgp;
    381 		syscallarg(netbsd32_size_t) msgsz;
    382 		syscallarg(int) msgflg;
    383 	} */
    384 
    385 	return msgsnd1(l, SCARG(uap, msqid),
    386 	    SCARG_P32(uap, msgp), SCARG(uap, msgsz),
    387 	    SCARG(uap, msgflg), sizeof(netbsd32_long),
    388 	    netbsd32_msgsnd_fetch_type);
    389 }
    390 
    391 static int
    392 netbsd32_msgrcv_put_type(const void *src, void *dst, size_t size)
    393 {
    394 	netbsd32_long l32;
    395 	const long *l = src;
    396 
    397 	KASSERT(size == sizeof(netbsd32_long));
    398 
    399 	l32 = (netbsd32_long)(*l);
    400 	return copyout(&l32, dst, sizeof(l32));
    401 }
    402 
    403 int
    404 netbsd32_msgrcv(struct lwp *l, const struct netbsd32_msgrcv_args *uap,
    405     register_t *retval)
    406 {
    407 	/* {
    408 		syscallarg(int) msqid;
    409 		syscallarg(netbsd32_voidp) msgp;
    410 		syscallarg(netbsd32_size_t) msgsz;
    411 		syscallarg(netbsd32_long) msgtyp;
    412 		syscallarg(int) msgflg;
    413 	} */
    414 
    415 	return msgrcv1(l, SCARG(uap, msqid),
    416 	    SCARG_P32(uap, msgp), SCARG(uap, msgsz),
    417 	    SCARG(uap, msgtyp), SCARG(uap, msgflg), sizeof(netbsd32_long),
    418 	    netbsd32_msgrcv_put_type, retval);
    419 }
    420 #endif /* SYSVMSG */
    421 
    422 #if defined(SYSVSHM)
    423 
    424 int
    425 netbsd32_shmat(struct lwp *l, const struct netbsd32_shmat_args *uap,
    426     register_t *retval)
    427 {
    428 	/* {
    429 		syscallarg(int) shmid;
    430 		syscallarg(const netbsd32_voidp) shmaddr;
    431 		syscallarg(int) shmflg;
    432 	} */
    433 	struct sys_shmat_args ua;
    434 
    435 	memset(&ua, 0, sizeof(ua));
    436 	NETBSD32TO64_UAP(shmid);
    437 	NETBSD32TOP_UAP(shmaddr, void);
    438 	NETBSD32TO64_UAP(shmflg);
    439 	return sys_shmat(l, &ua, retval);
    440 }
    441 
    442 int
    443 netbsd32___shmctl50(struct lwp *l, const struct netbsd32___shmctl50_args *uap,
    444     register_t *retval)
    445 {
    446 	/* {
    447 		syscallarg(int) shmid;
    448 		syscallarg(int) cmd;
    449 		syscallarg(netbsd32_shmid_dsp_t) buf;
    450 	} */
    451 	struct shmid_ds ds;
    452 	struct netbsd32_shmid_ds ds32;
    453 	int error, cmd;
    454 
    455 	cmd = SCARG(uap, cmd);
    456 	if (cmd == IPC_SET) {
    457 		error = copyin(SCARG_P32(uap, buf), &ds32, sizeof(ds32));
    458 		if (error)
    459 			return error;
    460 		netbsd32_to_shmid_ds(&ds32, &ds);
    461 	}
    462 
    463 	error = shmctl1(l, SCARG(uap, shmid), cmd,
    464 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &ds : NULL);
    465 
    466 	if (error == 0 && cmd == IPC_STAT) {
    467 		netbsd32_from_shmid_ds(&ds, &ds32);
    468 		error = copyout(&ds32, SCARG_P32(uap, buf), sizeof(ds32));
    469 	}
    470 
    471 	return error;
    472 }
    473 
    474 int
    475 netbsd32_shmdt(struct lwp *l, const struct netbsd32_shmdt_args *uap,
    476     register_t *retval)
    477 {
    478 	/* {
    479 		syscallarg(const netbsd32_voidp) shmaddr;
    480 	} */
    481 	struct sys_shmdt_args ua;
    482 
    483 	memset(&ua, 0, sizeof(ua));
    484 	NETBSD32TOP_UAP(shmaddr, const char);
    485 	return sys_shmdt(l, &ua, retval);
    486 }
    487 
    488 int
    489 netbsd32_shmget(struct lwp *l, const struct netbsd32_shmget_args *uap,
    490     register_t *retval)
    491 {
    492 	/* {
    493 		syscallarg(netbsd32_key_t) key;
    494 		syscallarg(netbsd32_size_t) size;
    495 		syscallarg(int) shmflg;
    496 	} */
    497 	struct sys_shmget_args ua;
    498 
    499 	memset(&ua, 0, sizeof(ua));
    500 	NETBSD32TOX_UAP(key, key_t);
    501 	NETBSD32TOX_UAP(size, size_t);
    502 	NETBSD32TO64_UAP(shmflg);
    503 	return sys_shmget(l, &ua, retval);
    504 }
    505 #endif /* SYSVSHM */
    506