Home | History | Annotate | Line # | Download | only in common
linux32_ipccall.c revision 1.5
      1 /* $NetBSD: linux32_ipccall.c,v 1.5 2009/11/16 08:49:32 joerg Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2008 Nicolas Joly
      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 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: linux32_ipccall.c,v 1.5 2009/11/16 08:49:32 joerg Exp $");
     31 
     32 #if defined(_KERNEL_OPT)
     33 #include "opt_sysv.h"
     34 #endif
     35 
     36 #include <sys/param.h>
     37 #include <sys/vnode.h>
     38 #include <sys/msg.h>
     39 #include <sys/sem.h>
     40 #include <sys/shm.h>
     41 
     42 #include <sys/syscallargs.h>
     43 
     44 #include <compat/netbsd32/netbsd32.h>
     45 
     46 #include <compat/linux/common/linux_types.h>
     47 #include <compat/linux32/common/linux32_types.h>
     48 #include <compat/linux32/common/linux32_signal.h>
     49 #include <compat/linux32/linux32_syscallargs.h>
     50 #include <compat/linux32/common/linux32_ipc.h>
     51 #include <compat/linux32/common/linux32_sem.h>
     52 #include <compat/linux32/common/linux32_shm.h>
     53 
     54 #define LINUX32_IPC_semop         1
     55 #define LINUX32_IPC_semget        2
     56 #define LINUX32_IPC_semctl        3
     57 #define LINUX32_IPC_msgsnd        11
     58 #define LINUX32_IPC_msgrcv        12
     59 #define LINUX32_IPC_msgget        13
     60 #define LINUX32_IPC_msgctl        14
     61 #define LINUX32_IPC_shmat         21
     62 #define LINUX32_IPC_shmdt         22
     63 #define LINUX32_IPC_shmget        23
     64 #define LINUX32_IPC_shmctl        24
     65 
     66 #ifdef SYSVSEM
     67 static void
     68 bsd_to_linux32_semid_ds(struct semid_ds *, struct linux32_semid_ds *);
     69 static void
     70 bsd_to_linux32_semid64_ds(struct semid_ds *, struct linux32_semid64_ds *);
     71 static void
     72 linux32_to_bsd_semid_ds(struct linux32_semid_ds *, struct semid_ds *);
     73 static void
     74 linux32_to_bsd_semid64_ds(struct linux32_semid64_ds *, struct semid_ds *);
     75 
     76 static int
     77 linux32_semop(struct lwp *, const struct linux32_sys_ipc_args *, register_t *);
     78 static int
     79 linux32_semget(struct lwp *, const struct linux32_sys_ipc_args *, register_t *);
     80 static int
     81 linux32_semctl(struct lwp *, const struct linux32_sys_ipc_args *, register_t *);
     82 #endif /* SYSVSEM */
     83 
     84 #ifdef SYSVSHM
     85 static void
     86 bsd_to_linux32_shmid_ds(struct shmid_ds *, struct linux32_shmid_ds *);
     87 static void
     88 linux32_to_bsd_shmid_ds(struct linux32_shmid_ds *, struct shmid_ds *);
     89 static void
     90 bsd_to_linux32_shmid64_ds(struct shmid_ds *, struct linux32_shmid64_ds *);
     91 static void
     92 linux32_to_bsd_shmid64_ds(struct linux32_shmid64_ds *, struct shmid_ds *);
     93 
     94 static int
     95 linux32_shmat(struct lwp *, const struct linux32_sys_ipc_args *, register_t *);
     96 static int
     97 linux32_shmdt(struct lwp *, const struct linux32_sys_ipc_args *, register_t *);
     98 static int
     99 linux32_shmget(struct lwp *, const struct linux32_sys_ipc_args *, register_t *);
    100 static int
    101 linux32_shmctl(struct lwp *, const struct linux32_sys_ipc_args *, register_t *);
    102 #endif /* SYSVSHM */
    103 
    104 #ifdef SYSVMSG
    105 static int linux32_msgsnd(struct lwp *, const struct linux32_sys_ipc_args *,
    106     register_t *);
    107 static int linux32_msgrcv(struct lwp *, const struct linux32_sys_ipc_args *,
    108     register_t *);
    109 static int linux32_msgget(struct lwp *, const struct linux32_sys_ipc_args *,
    110     register_t *);
    111 static int linux32_msgctl(struct lwp *, const struct linux32_sys_ipc_args *,
    112     register_t *);
    113 #endif
    114 
    115 int
    116 linux32_sys_ipc(struct lwp *l, const struct linux32_sys_ipc_args *uap,
    117     register_t *retval)
    118 {
    119 	/* {
    120 		syscallarg(int) what;
    121 		syscallarg(int) a1;
    122 		syscallarg(int) a2;
    123 		syscallarg(int) a3;
    124 		syscallarg(netbsd32_voidp) ptr;
    125 	} */
    126 
    127 	switch (SCARG(uap, what)) {
    128 #ifdef SYSVSEM
    129 	case LINUX32_IPC_semop:
    130 		return linux32_semop(l, uap, retval);;
    131 	case LINUX32_IPC_semget:
    132 		return linux32_semget(l, uap, retval);
    133 	case LINUX32_IPC_semctl:
    134 		return linux32_semctl(l, uap, retval);
    135 #endif /* SYSVSEM */
    136 #ifdef SYSVMSG
    137 	case LINUX32_IPC_msgsnd:
    138 		return linux32_msgsnd(l, uap, retval);
    139 	case LINUX32_IPC_msgrcv:
    140 		return linux32_msgrcv(l, uap, retval);
    141 	case LINUX32_IPC_msgget:
    142 		return linux32_msgget(l, uap, retval);
    143 	case LINUX32_IPC_msgctl:
    144 		return linux32_msgctl(l, uap, retval);
    145 #endif
    146 #ifdef SYSVSHM
    147 	case LINUX32_IPC_shmat:
    148 		return linux32_shmat(l, uap, retval);
    149 	case LINUX32_IPC_shmdt:
    150 		return linux32_shmdt(l, uap, retval);
    151 	case LINUX32_IPC_shmget:
    152 		return linux32_shmget(l, uap, retval);
    153 	case LINUX32_IPC_shmctl:
    154 		return linux32_shmctl(l, uap, retval);
    155 #endif /* SYSVSHM */
    156 	default:
    157 		return ENOSYS;
    158 	}
    159 
    160 }
    161 
    162 static void
    163 bsd_to_linux32_ipc_perm(struct ipc_perm *bpp, struct linux32_ipc_perm *lpp)
    164 {
    165 	lpp->l_key = bpp->_key;
    166 	lpp->l_uid = bpp->uid;
    167 	lpp->l_gid = bpp->gid;
    168 	lpp->l_cuid = bpp->cuid;
    169 	lpp->l_cgid = bpp->cgid;
    170 	lpp->l_mode = bpp->mode;
    171 	lpp->l_seq = bpp->_seq;
    172 }
    173 
    174 static void
    175 linux32_to_bsd_ipc_perm(struct linux32_ipc_perm *lpp, struct ipc_perm *bpp)
    176 {
    177 	bpp->_key = lpp->l_key;
    178 	bpp->uid = lpp->l_uid;
    179 	bpp->gid = lpp->l_gid;
    180 	bpp->cuid = lpp->l_cuid;
    181 	bpp->cgid = lpp->l_cgid;
    182 	bpp->mode = lpp->l_mode;
    183 	bpp->_seq = lpp->l_seq;
    184 }
    185 
    186 static void
    187 bsd_to_linux32_ipc64_perm(struct ipc_perm *bpp, struct linux32_ipc64_perm *lpp)
    188 {
    189 	lpp->l_key = bpp->_key;
    190 	lpp->l_uid = bpp->uid;
    191 	lpp->l_gid = bpp->gid;
    192 	lpp->l_cuid = bpp->cuid;
    193 	lpp->l_cgid = bpp->cgid;
    194 	lpp->l_mode = bpp->mode;
    195 	lpp->l_seq = bpp->_seq;
    196 }
    197 
    198 static void
    199 linux32_to_bsd_ipc64_perm(struct linux32_ipc64_perm *lpp, struct ipc_perm *bpp)
    200 {
    201 	bpp->_key = lpp->l_key;
    202 	bpp->uid = lpp->l_uid;
    203 	bpp->gid = lpp->l_gid;
    204 	bpp->cuid = lpp->l_cuid;
    205 	bpp->cgid = lpp->l_cgid;
    206 	bpp->mode = lpp->l_mode;
    207 	bpp->_seq = lpp->l_seq;
    208 }
    209 
    210 #ifdef SYSVSEM
    211 static void
    212 bsd_to_linux32_semid_ds(struct semid_ds *bsp, struct linux32_semid_ds *lsp)
    213 {
    214 	bsd_to_linux32_ipc_perm(&bsp->sem_perm, &lsp->l_sem_perm);
    215 	lsp->l_sem_otime = bsp->sem_otime;
    216 	lsp->l_sem_ctime = bsp->sem_ctime;
    217 	lsp->l_sem_nsems = bsp->sem_nsems;
    218 	NETBSD32PTR32(lsp->l_sem_base, bsp->_sem_base);
    219 }
    220 
    221 static void
    222 bsd_to_linux32_semid64_ds(struct semid_ds *bsp, struct linux32_semid64_ds *lsp)
    223 {
    224 	bsd_to_linux32_ipc64_perm(&bsp->sem_perm, &lsp->l_sem_perm);
    225 	lsp->l_sem_otime = bsp->sem_otime;
    226 	lsp->l_sem_ctime = bsp->sem_ctime;
    227 	lsp->l_sem_nsems = bsp->sem_nsems;
    228 }
    229 
    230 static void
    231 linux32_to_bsd_semid_ds(struct linux32_semid_ds *lsp, struct semid_ds *bsp)
    232 {
    233 	linux32_to_bsd_ipc_perm(&lsp->l_sem_perm, &bsp->sem_perm);
    234 	bsp->sem_otime = lsp->l_sem_otime;
    235 	bsp->sem_ctime = lsp->l_sem_ctime;
    236 	bsp->sem_nsems = lsp->l_sem_nsems;
    237 	bsp->_sem_base = NETBSD32PTR64(lsp->l_sem_base);
    238 }
    239 
    240 static void
    241 linux32_to_bsd_semid64_ds(struct linux32_semid64_ds *lsp, struct semid_ds *bsp)
    242 {
    243 	linux32_to_bsd_ipc64_perm(&lsp->l_sem_perm, &bsp->sem_perm);
    244 	bsp->sem_otime = lsp->l_sem_otime;
    245 	bsp->sem_ctime = lsp->l_sem_ctime;
    246 	bsp->sem_nsems = lsp->l_sem_nsems;
    247 }
    248 
    249 static int
    250 linux32_semop(struct lwp *l, const struct linux32_sys_ipc_args *uap,
    251     register_t *retval)
    252 {
    253 	struct sys_semop_args ua;
    254 
    255 	SCARG(&ua, semid) = SCARG(uap, a1);
    256 	SCARG(&ua, sops) = SCARG_P32(uap, ptr);
    257 	SCARG(&ua, nsops) = SCARG(uap, a2);
    258 
    259 	return sys_semop(l, &ua, retval);
    260 }
    261 
    262 static int
    263 linux32_semget(struct lwp *l, const struct linux32_sys_ipc_args *uap,
    264     register_t *retval)
    265 {
    266 	struct sys_semget_args ua;
    267 
    268 	SCARG(&ua, key) = SCARG(uap, a1);
    269 	SCARG(&ua, nsems) = SCARG(uap, a2);
    270 	SCARG(&ua, semflg) = SCARG(uap, a3);
    271 
    272 	return sys_semget(l, &ua, retval);
    273 }
    274 
    275 static int
    276 linux32_semctl(struct lwp *l, const struct linux32_sys_ipc_args *uap,
    277     register_t *retval)
    278 {
    279 	int lcmd, cmd, error;
    280 	struct semid_ds bs;
    281 	struct linux32_semid_ds ls;
    282 	struct linux32_semid64_ds ls64;
    283 	union linux32_semun lsem;
    284 	union __semun bsem;
    285 	void *buf = NULL;
    286 
    287 	if ((error = copyin(SCARG_P32(uap, ptr), &lsem, sizeof lsem)))
    288 		return error;
    289 
    290 	lcmd = SCARG(uap, a3);
    291 
    292 	switch (lcmd & ~LINUX32_IPC_64) {
    293 	case LINUX32_IPC_RMID:
    294 		cmd = IPC_RMID;
    295 		break;
    296 	case LINUX32_IPC_STAT:
    297 		cmd = IPC_STAT;
    298 		buf = &bs;
    299 		break;
    300 	case LINUX32_IPC_SET:
    301 		if (lcmd & LINUX32_IPC_64) {
    302 			error = copyin(NETBSD32PTR64(lsem.l_buf), &ls64,
    303 			    sizeof ls64);
    304 			linux32_to_bsd_semid64_ds(&ls64, &bs);
    305 		} else {
    306 			error = copyin(NETBSD32PTR64(lsem.l_buf), &ls,
    307 			    sizeof ls);
    308 			linux32_to_bsd_semid_ds(&ls, &bs);
    309 		}
    310 		if (error)
    311 			return error;
    312 		cmd = IPC_SET;
    313 		buf = &bs;
    314 		break;
    315 	case LINUX32_GETVAL:
    316 		cmd = GETVAL;
    317 		break;
    318 	case LINUX32_SETVAL:
    319 		cmd = SETVAL;
    320 		bsem.val = lsem.l_val;
    321 		buf = &bsem;
    322 		break;
    323 	case LINUX32_GETPID:
    324 		cmd = GETPID;
    325 		break;
    326 	case LINUX32_GETNCNT:
    327 		cmd = GETNCNT;
    328 		break;
    329 	case LINUX32_GETZCNT:
    330 		cmd = GETZCNT;
    331 		break;
    332 	case LINUX32_GETALL:
    333 		cmd = GETALL;
    334 		bsem.array = NETBSD32PTR64(lsem.l_array);
    335 		buf = &bsem;
    336 		break;
    337 	case LINUX32_SETALL:
    338 		cmd = SETALL;
    339 		bsem.array = NETBSD32PTR64(lsem.l_array);
    340 		buf = &bsem;
    341 		break;
    342 	default:
    343 		return EINVAL;
    344 	}
    345 
    346 	error = semctl1(l, SCARG(uap, a1), SCARG(uap, a2), cmd, buf, retval);
    347 	if (error)
    348 		return error;
    349 
    350 	switch (lcmd) {
    351 	case LINUX32_IPC_STAT:
    352 		bsd_to_linux32_semid_ds(&bs, &ls);
    353 		error = copyout(&ls, NETBSD32PTR64(lsem.l_buf), sizeof ls);
    354 		break;
    355 	case LINUX32_IPC_STAT|LINUX32_IPC_64:
    356 		bsd_to_linux32_semid64_ds(&bs, &ls64);
    357 		error = copyout(&ls64, NETBSD32PTR64(lsem.l_buf), sizeof ls64);
    358 		break;
    359 	default:
    360 		break;
    361 	}
    362 
    363 	return error;
    364 }
    365 #endif /* SYSVSEM */
    366 
    367 #ifdef SYSVMSG
    368 
    369 static int
    370 linux32_msgsnd(struct lwp *l, const struct linux32_sys_ipc_args *uap, register_t *retval)
    371 {
    372 	struct sys_msgsnd_args bma;
    373 
    374 	SCARG(&bma, msqid) = SCARG(uap, a1);
    375 	SCARG(&bma, msgp) = SCARG_P32(uap, ptr);
    376 	SCARG(&bma, msgsz) = SCARG(uap, a2);
    377 	SCARG(&bma, msgflg) = SCARG(uap, a3);
    378 
    379 	printf("linux32_msgsnd: %d %zu\n", SCARG(&bma, msqid), SCARG(&bma, msgsz));
    380 
    381 	return sys_msgsnd(l, &bma, retval);
    382 }
    383 
    384 /*
    385  * This kludge is used for the 6th argument to the msgrcv system
    386  * call, to get around the maximum of 5 arguments to a syscall in Linux.
    387  */
    388 struct linux32_msgrcv_msgarg {
    389 	netbsd32_pointer_t msg;
    390 	int type;
    391 };
    392 
    393 static int
    394 linux32_msgrcv(struct lwp *l, const struct linux32_sys_ipc_args *uap, register_t *retval)
    395 {
    396 	struct sys_msgrcv_args bma;
    397 	struct linux32_msgrcv_msgarg kluge;
    398 	int error;
    399 
    400 	if ((error = copyin(SCARG_P32(uap, ptr), &kluge, sizeof kluge)))
    401 		return error;
    402 
    403 	SCARG(&bma, msqid) = SCARG(uap, a1);
    404 	SCARG(&bma, msgp) = NETBSD32PTR64(kluge.msg);
    405 	SCARG(&bma, msgsz) = SCARG(uap, a2);
    406 	SCARG(&bma, msgtyp) = kluge.type;
    407 	SCARG(&bma, msgflg) = SCARG(uap, a3);
    408 
    409 	printf("linux32_msgrcv: %d %zu\n", SCARG(&bma, msqid), SCARG(&bma, msgsz));
    410 
    411 	return sys_msgrcv(l, &bma, retval);
    412 }
    413 
    414 static int
    415 linux32_msgget(struct lwp *l, const struct linux32_sys_ipc_args *uap, register_t *retval)
    416 {
    417 	struct sys_msgget_args bma;
    418 
    419 	SCARG(&bma, key) = (key_t)(linux32_key_t)SCARG(uap, a1);
    420 	SCARG(&bma, msgflg) = SCARG(uap, a2);
    421 
    422 	return sys_msgget(l, &bma, retval);
    423 }
    424 
    425 
    426 static void
    427 linux32_to_bsd_msqid_ds(struct linux32_msqid_ds *lmp, struct msqid_ds *bmp)
    428 {
    429 
    430 	memset(bmp, 0, sizeof(*bmp));
    431 	linux32_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    432 	bmp->_msg_first = NETBSD32PTR64(lmp->l_msg_first);
    433 	bmp->_msg_last = NETBSD32PTR64(lmp->l_msg_last);
    434 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    435 	bmp->msg_qnum = lmp->l_msg_qnum;
    436 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    437 	bmp->msg_lspid = lmp->l_msg_lspid;
    438 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    439 	bmp->msg_stime = lmp->l_msg_stime;
    440 	bmp->msg_rtime = lmp->l_msg_rtime;
    441 	bmp->msg_ctime = lmp->l_msg_ctime;
    442 }
    443 
    444 void
    445 linux32_to_bsd_msqid64_ds(struct linux32_msqid64_ds *lmp, struct msqid_ds *bmp)
    446 {
    447 
    448 	memset(bmp, 0, sizeof(*bmp));
    449 	linux_to_bsd_ipc64_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    450 	bmp->msg_stime = lmp->l_msg_stime;
    451 	bmp->msg_rtime = lmp->l_msg_rtime;
    452 	bmp->msg_ctime = lmp->l_msg_ctime;
    453 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    454 	bmp->msg_qnum = lmp->l_msg_qnum;
    455 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    456 	bmp->msg_lspid = lmp->l_msg_lspid;
    457 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    458 }
    459 
    460 static void
    461 bsd_to_linux32_msqid_ds(struct msqid_ds *bmp, struct linux32_msqid_ds *lmp)
    462 {
    463 
    464 	memset(lmp, 0, sizeof(*lmp));
    465 	bsd_to_linux32_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    466 	NETBSD32PTR32(lmp->l_msg_first, bmp->_msg_first);
    467 	NETBSD32PTR32(lmp->l_msg_last, bmp->_msg_last);
    468 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    469 	lmp->l_msg_qnum = bmp->msg_qnum;
    470 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    471 	lmp->l_msg_lspid = bmp->msg_lspid;
    472 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    473 	lmp->l_msg_stime = bmp->msg_stime;
    474 	lmp->l_msg_rtime = bmp->msg_rtime;
    475 	lmp->l_msg_ctime = bmp->msg_ctime;
    476 }
    477 
    478 void
    479 bsd_to_linux32_msqid64_ds(struct msqid_ds *bmp, struct linux32_msqid64_ds *lmp)
    480 {
    481 
    482 	memset(lmp, 0, sizeof(*lmp));
    483 	bsd_to_linux_ipc64_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    484 	NETBSD32PTR32(lmp->l_msg_stime, bmp->msg_stime);
    485 	NETBSD32PTR32(lmp->l_msg_rtime, bmp->msg_rtime);
    486 	lmp->l_msg_ctime = bmp->msg_ctime;
    487 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    488 	lmp->l_msg_qnum = bmp->msg_qnum;
    489 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    490 	lmp->l_msg_lspid = bmp->msg_lspid;
    491 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    492 }
    493 
    494 static int
    495 linux32_msgctl(struct lwp *l, const struct linux32_sys_ipc_args *uap, register_t *retval)
    496 {
    497 	struct msqid_ds bm, *bmp = NULL;
    498 	struct linux32_msqid_ds lm;
    499 	struct linux32_msqid64_ds lm64;
    500 	int cmd, lcmd, error;
    501 	void *data = SCARG_P32(uap, ptr);
    502 
    503 	lcmd = SCARG(uap, a2);
    504 
    505 	switch (lcmd & ~LINUX32_IPC_64) {
    506 	case LINUX32_IPC_STAT:
    507 		cmd = IPC_STAT;
    508 		bmp = &bm;
    509 		break;
    510 	case LINUX32_IPC_SET:
    511 		if (lcmd & LINUX32_IPC_64) {
    512 			error = copyin(data, &lm64, sizeof lm64);
    513 			linux32_to_bsd_msqid64_ds(&lm64, &bm);
    514 		} else {
    515 			error = copyin(data, &lm, sizeof lm);
    516 			linux32_to_bsd_msqid_ds(&lm, &bm);
    517 		}
    518 		if (error)
    519 			return error;
    520 		cmd = IPC_SET;
    521 		bmp = &bm;
    522 		break;
    523 	case LINUX32_IPC_RMID:
    524 		cmd = IPC_RMID;
    525 		break;
    526 	default:
    527 		return EINVAL;
    528 	}
    529 
    530 	if ((error = msgctl1(l, SCARG(uap, a1), cmd, bmp)))
    531 		return error;
    532 
    533 	switch (lcmd) {
    534 	case LINUX32_IPC_STAT:
    535 		bsd_to_linux32_msqid_ds(&bm, &lm);
    536 		error = copyout(&lm, data, sizeof lm);
    537 		break;
    538 	case LINUX32_IPC_STAT|LINUX32_IPC_64:
    539 		bsd_to_linux32_msqid64_ds(&bm, &lm64);
    540 		error = copyout(&lm64, data, sizeof lm64);
    541 		break;
    542 	default:
    543 		break;
    544 	}
    545 
    546 	return error;
    547 }
    548 #endif /* SYSVMSG */
    549 
    550 #ifdef SYSVSHM
    551 static void
    552 bsd_to_linux32_shmid_ds(struct shmid_ds *bsp, struct linux32_shmid_ds *lsp)
    553 {
    554 	bsd_to_linux32_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    555 	lsp->l_shm_segsz = bsp->shm_segsz;
    556 	lsp->l_shm_atime = bsp->shm_atime;
    557 	lsp->l_shm_dtime = bsp->shm_dtime;
    558 	lsp->l_shm_ctime = bsp->shm_ctime;
    559 	lsp->l_shm_cpid = bsp->shm_cpid;
    560 	lsp->l_shm_lpid = bsp->shm_lpid;
    561 	lsp->l_shm_nattch = bsp->shm_nattch;
    562 	NETBSD32PTR32(lsp->l_private2, bsp->_shm_internal);
    563 }
    564 
    565 static void
    566 linux32_to_bsd_shmid_ds(struct linux32_shmid_ds *lsp, struct shmid_ds *bsp)
    567 {
    568 	linux32_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    569 	bsp->shm_segsz = lsp->l_shm_segsz;
    570 	bsp->shm_atime = lsp->l_shm_atime;
    571 	bsp->shm_dtime = lsp->l_shm_dtime;
    572 	bsp->shm_ctime = lsp->l_shm_ctime;
    573 	bsp->shm_cpid = lsp->l_shm_cpid;
    574 	bsp->shm_lpid = lsp->l_shm_lpid;
    575 	bsp->shm_nattch = lsp->l_shm_nattch;
    576 	bsp->_shm_internal = NETBSD32PTR64(lsp->l_private2);
    577 }
    578 
    579 static void
    580 bsd_to_linux32_shmid64_ds(struct shmid_ds *bsp, struct linux32_shmid64_ds *lsp)
    581 {
    582 	bsd_to_linux32_ipc64_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    583 	lsp->l_shm_segsz = bsp->shm_segsz;
    584 	lsp->l_shm_atime = bsp->shm_atime;
    585 	lsp->l_shm_dtime = bsp->shm_dtime;
    586 	lsp->l_shm_ctime = bsp->shm_ctime;
    587 	lsp->l_shm_cpid = bsp->shm_cpid;
    588 	lsp->l_shm_lpid = bsp->shm_lpid;
    589 	lsp->l_shm_nattch = bsp->shm_nattch;
    590 	lsp->l___unused5 = NETBSD32PTR32I(bsp->_shm_internal);
    591 }
    592 
    593 static void
    594 linux32_to_bsd_shmid64_ds(struct linux32_shmid64_ds *lsp, struct shmid_ds *bsp)
    595 {
    596 	linux32_to_bsd_ipc64_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    597 	bsp->shm_segsz = lsp->l_shm_segsz;
    598 	bsp->shm_atime = lsp->l_shm_atime;
    599 	bsp->shm_dtime = lsp->l_shm_dtime;
    600 	bsp->shm_ctime = lsp->l_shm_ctime;
    601 	bsp->shm_cpid = lsp->l_shm_cpid;
    602 	bsp->shm_lpid = lsp->l_shm_lpid;
    603 	bsp->shm_nattch = lsp->l_shm_nattch;
    604 	bsp->_shm_internal = NETBSD32IPTR64(lsp->l___unused5);
    605 }
    606 
    607 static int
    608 linux32_shmat(struct lwp *l, const struct linux32_sys_ipc_args *uap,
    609     register_t *retval)
    610 {
    611 	struct sys_shmat_args ua;
    612 	netbsd32_pointer_t addr32;
    613 	int error;
    614 
    615 	SCARG(&ua, shmid) = SCARG(uap, a1);
    616 	SCARG(&ua, shmaddr) = SCARG_P32(uap, ptr);
    617 	SCARG(&ua, shmflg) = SCARG(uap, a2);
    618 
    619 	if ((error = sys_shmat(l, &ua, retval)))
    620 		return error;
    621 
    622 	NETBSD32PTR32(addr32, (const void *)(uintptr_t)retval[0]);
    623 
    624 	error = copyout(&addr32, NETBSD32IPTR64(SCARG(uap, a3)), sizeof addr32);
    625 	if (error == 0)
    626 		retval[0] = 0;
    627 
    628 	return error;
    629 }
    630 
    631 static int
    632 linux32_shmdt(struct lwp *l, const struct linux32_sys_ipc_args *uap,
    633     register_t *retval)
    634 {
    635 	struct sys_shmdt_args ua;
    636 
    637 	SCARG(&ua, shmaddr) = SCARG_P32(uap, ptr);
    638 
    639 	return sys_shmdt(l, &ua, retval);
    640 }
    641 
    642 static int
    643 linux32_shmget(struct lwp *l, const struct linux32_sys_ipc_args *uap,
    644     register_t *retval)
    645 {
    646 	struct sys_shmget_args ua;
    647 
    648 	SCARG(&ua, key) = SCARG(uap, a1);
    649 	SCARG(&ua, size) = SCARG(uap, a2);
    650 	SCARG(&ua, shmflg) = SCARG(uap, a3) | _SHM_RMLINGER;
    651 
    652 	return sys_shmget(l, &ua, retval);
    653 }
    654 
    655 static int
    656 linux32_shmctl(struct lwp *l, const struct linux32_sys_ipc_args *uap,
    657     register_t *retval)
    658 {
    659 	int shmid, cmd, error;
    660 	struct shmid_ds bs;
    661 	struct linux32_shmid_ds ls;
    662 	struct linux32_shmid64_ds ls64;
    663 
    664 	shmid = SCARG(uap, a1);
    665 	cmd = SCARG(uap, a2);
    666 
    667 	switch (cmd & ~LINUX32_IPC_64) {
    668 
    669 	case LINUX32_SHM_STAT:
    670 		return ENOSYS;
    671 
    672 	case LINUX32_IPC_STAT:
    673 		error = shmctl1(l, shmid, IPC_STAT, &bs);
    674 		if (error != 0)
    675 			return error;
    676 		if (cmd & LINUX32_IPC_64) {
    677 			bsd_to_linux32_shmid64_ds(&bs, &ls64);
    678 			error = copyout(&ls64, SCARG_P32(uap, ptr), sizeof ls64);
    679 		} else {
    680 			bsd_to_linux32_shmid_ds(&bs, &ls);
    681 			error = copyout(&ls, SCARG_P32(uap, ptr), sizeof ls);
    682 		}
    683 		return error;
    684 
    685 	case LINUX32_IPC_SET:
    686 		if (cmd & LINUX32_IPC_64) {
    687 			error = copyin(SCARG_P32(uap, ptr), &ls64, sizeof ls64);
    688 			linux32_to_bsd_shmid64_ds(&ls64, &bs);
    689 		} else {
    690 			error = copyin(SCARG_P32(uap, ptr), &ls, sizeof ls);
    691 			linux32_to_bsd_shmid_ds(&ls, &bs);
    692 		}
    693 		if (error != 0)
    694 			return error;
    695 		return shmctl1(l, shmid, IPC_SET, &bs);
    696 
    697 	case LINUX32_IPC_RMID:
    698 		return shmctl1(l, shmid, IPC_RMID, NULL);
    699 
    700 	case LINUX32_SHM_LOCK:
    701 		return shmctl1(l, shmid, SHM_LOCK, NULL);
    702 
    703 	case LINUX32_SHM_UNLOCK:
    704 		return shmctl1(l, shmid, SHM_UNLOCK, NULL);
    705 
    706 	case LINUX32_IPC_INFO:
    707 	case LINUX32_SHM_INFO:
    708 		return ENOSYS;
    709 
    710 	default:
    711 		return EINVAL;
    712 	}
    713 }
    714 #endif /* SYSVSHM */
    715