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