Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / compat / svr4_32 / svr4_32_ipc.c
blobe08249296f22a11a494ff42c42dcfadd0a53c1ca
1 /* $NetBSD: svr4_32_ipc.c,v 1.19 2007/12/20 23:03:06 dsl Exp $ */
3 /*-
4 * Copyright (c) 1995 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: svr4_32_ipc.c,v 1.19 2007/12/20 23:03:06 dsl Exp $");
35 #if defined(_KERNEL_OPT)
36 #include "opt_sysv.h"
37 #endif
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/shm.h>
42 #include <sys/msg.h>
43 #include <sys/sem.h>
44 #include <sys/proc.h>
45 #include <sys/uio.h>
46 #include <sys/time.h>
47 #include <sys/malloc.h>
48 #include <sys/mman.h>
49 #include <sys/systm.h>
50 #include <sys/stat.h>
52 #include <sys/mount.h>
53 #include <sys/syscallargs.h>
55 #include <compat/svr4_32/svr4_32_types.h>
56 #include <compat/svr4_32/svr4_32_signal.h>
57 #include <compat/svr4_32/svr4_32_lwp.h>
58 #include <compat/svr4_32/svr4_32_ucontext.h>
59 #include <compat/svr4_32/svr4_32_syscallargs.h>
60 #include <compat/svr4_32/svr4_32_util.h>
61 #include <compat/svr4_32/svr4_32_ipc.h>
63 #if defined(SYSVMSG) || defined(SYSVSHM) || defined(SYSVSEM)
64 static void svr4_32_to_bsd_ipc_perm(const struct svr4_32_ipc_perm *,
65 struct ipc_perm *);
66 static void bsd_to_svr4_32_ipc_perm(const struct ipc_perm *,
67 struct svr4_32_ipc_perm *);
68 #endif
70 #ifdef SYSVSEM
71 static void bsd_to_svr4_32_semid_ds(const struct semid_ds *,
72 struct svr4_32_semid_ds *);
73 static void svr4_32_to_bsd_semid_ds(const struct svr4_32_semid_ds *,
74 struct semid_ds *);
75 #endif
77 #ifdef SYSVMSG
78 static void bsd_to_svr4_32_msqid_ds(const struct msqid_ds *,
79 struct svr4_32_msqid_ds *);
80 static void svr4_32_to_bsd_msqid_ds(const struct svr4_32_msqid_ds *,
81 struct msqid_ds *);
82 #endif
84 #ifdef SYSVSHM
85 static void bsd_to_svr4_32_shmid_ds(const struct shmid_ds *,
86 struct svr4_32_shmid_ds *);
87 static void svr4_32_to_bsd_shmid_ds(const struct svr4_32_shmid_ds *,
88 struct shmid_ds *);
89 #endif
91 #if defined(SYSVMSG) || defined(SYSVSHM) || defined(SYSVSEM)
93 static void
94 svr4_32_to_bsd_ipc_perm(const struct svr4_32_ipc_perm *spp, struct ipc_perm *bpp)
96 bpp->_key = spp->key;
97 bpp->uid = spp->uid;
98 bpp->gid = spp->gid;
99 bpp->cuid = spp->cuid;
100 bpp->cgid = spp->cgid;
101 bpp->mode = spp->mode;
102 bpp->_seq = spp->seq;
105 static void
106 bsd_to_svr4_32_ipc_perm(const struct ipc_perm *bpp, struct svr4_32_ipc_perm *spp)
108 spp->key = bpp->_key;
109 spp->uid = bpp->uid;
110 spp->gid = bpp->gid;
111 spp->cuid = bpp->cuid;
112 spp->cgid = bpp->cgid;
113 spp->mode = bpp->mode;
114 spp->seq = bpp->_seq;
116 #endif
118 #ifdef SYSVSEM
119 static void
120 bsd_to_svr4_32_semid_ds(const struct semid_ds *bds, struct svr4_32_semid_ds *sds)
122 bsd_to_svr4_32_ipc_perm(&bds->sem_perm, &sds->sem_perm);
123 NETBSD32PTR32(sds->sem_base, bds->_sem_base);
124 sds->sem_nsems = bds->sem_nsems;
125 sds->sem_otime = bds->sem_otime;
126 sds->sem_ctime = bds->sem_ctime;
129 static void
130 svr4_32_to_bsd_semid_ds(const struct svr4_32_semid_ds *sds, struct semid_ds *bds)
132 svr4_32_to_bsd_ipc_perm(&sds->sem_perm, &bds->sem_perm);
133 bds->_sem_base = NETBSD32PTR64(sds->sem_base);
134 bds->sem_nsems = sds->sem_nsems;
135 bds->sem_otime = sds->sem_otime;
136 bds->sem_ctime = sds->sem_ctime;
139 struct svr4_32_sys_semctl_args {
140 syscallarg(int) what;
141 syscallarg(int) semid;
142 syscallarg(int) semnum;
143 syscallarg(int) cmd;
144 syscallarg(union netbsd32_semun) arg;
147 static int
148 svr4_32_semctl(struct lwp *l, const struct svr4_32_sys_semctl_args *uap, register_t *retval)
150 struct semid_ds sembuf;
151 struct svr4_32_semid_ds ssembuf;
152 int cmd, error;
153 void *pass_arg = NULL;
154 union netbsd32_semun arg;
156 cmd = SCARG(uap, cmd);
158 switch (cmd) {
159 case SVR4_IPC_SET:
160 pass_arg = &sembuf;
161 cmd = IPC_SET;
162 break;
164 case SVR4_IPC_STAT:
165 pass_arg = &sembuf;
166 cmd = IPC_STAT;
167 break;
169 case SVR4_IPC_RMID:
170 cmd = IPC_RMID;
171 break;
173 case SVR4_SEM_GETVAL:
174 cmd = GETVAL;
175 break;
177 case SVR4_SEM_GETPID:
178 cmd = GETPID;
179 break;
181 case SVR4_SEM_GETNCNT:
182 cmd = GETNCNT;
183 break;
185 case SVR4_SEM_GETZCNT:
186 cmd = GETZCNT;
187 break;
189 case SVR4_SEM_GETALL:
190 arg = SCARG(uap, arg);
191 pass_arg = &arg;
192 cmd = GETALL;
193 break;
195 case SVR4_SEM_SETVAL:
196 arg = SCARG(uap, arg);
197 pass_arg = &arg;
198 cmd = SETVAL;
199 break;
201 case SVR4_SEM_SETALL:
202 arg = SCARG(uap, arg);
203 pass_arg = &arg;
204 cmd = SETALL;
205 break;
207 default:
208 return (EINVAL);
211 if (cmd == IPC_SET) {
212 error = copyin(NETBSD32PTR64(SCARG(uap, arg).buf),
213 &ssembuf, sizeof(ssembuf));
214 if (error)
215 return (error);
216 svr4_32_to_bsd_semid_ds(&ssembuf, &sembuf);
219 error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
220 pass_arg, retval);
222 if (error == 0 && cmd == IPC_STAT) {
223 bsd_to_svr4_32_semid_ds(&sembuf, &ssembuf);
224 error = copyout(&ssembuf, NETBSD32PTR64(SCARG(uap, arg).buf),
225 sizeof(ssembuf));
228 return (error);
231 struct svr4_32_sys_semget_args {
232 syscallarg(int) what;
233 syscallarg(svr4_key_t) key;
234 syscallarg(int) nsems;
235 syscallarg(int) semflg;
238 static int
239 svr4_32_semget(struct lwp *l, const struct svr4_32_sys_semget_args *uap, register_t *retval)
241 struct sys_semget_args ap;
243 SCARG(&ap, key) = SCARG(uap, key);
244 SCARG(&ap, nsems) = SCARG(uap, nsems);
245 SCARG(&ap, semflg) = SCARG(uap, semflg);
247 return sys_semget(l, &ap, retval);
250 struct svr4_32_sys_semop_args {
251 syscallarg(int) what;
252 syscallarg(int) semid;
253 syscallarg(svr4_32_sembufp) sops;
254 syscallarg(u_int) nsops;
257 static int
258 svr4_32_semop(struct lwp *l, const struct svr4_32_sys_semop_args *uap, register_t *retval)
260 struct sys_semop_args ap;
262 SCARG(&ap, semid) = SCARG(uap, semid);
263 /* These are the same */
264 SCARG(&ap, sops) = SCARG_P32(uap, sops);
265 SCARG(&ap, nsops) = SCARG(uap, nsops);
267 return sys_semop(l, &ap, retval);
271 svr4_32_sys_semsys(struct lwp *l, const struct svr4_32_sys_semsys_args *uap, register_t *retval)
274 DPRINTF(("svr4_32_semsys(%d)\n", SCARG(uap, what)));
276 switch (SCARG(uap, what)) {
277 case SVR4_semctl:
278 return svr4_32_semctl(l, (const void *)uap, retval);
279 case SVR4_semget:
280 return svr4_32_semget(l, (const void *)uap, retval);
281 case SVR4_semop:
282 return svr4_32_semop(l, (const void *)uap, retval);
283 default:
284 return EINVAL;
287 #endif
289 #ifdef SYSVMSG
290 static void
291 bsd_to_svr4_32_msqid_ds(const struct msqid_ds *bds, struct svr4_32_msqid_ds *sds)
293 bsd_to_svr4_32_ipc_perm(&bds->msg_perm, &sds->msg_perm);
294 NETBSD32PTR32(sds->msg_first, bds->_msg_first);
295 NETBSD32PTR32(sds->msg_last, bds->_msg_last);
296 sds->msg_cbytes = bds->_msg_cbytes;
297 sds->msg_qnum = bds->msg_qnum;
298 sds->msg_qbytes = bds->msg_qbytes;
299 sds->msg_lspid = bds->msg_lspid;
300 sds->msg_lrpid = bds->msg_lrpid;
301 sds->msg_stime = bds->msg_stime;
302 sds->msg_rtime = bds->msg_rtime;
303 sds->msg_ctime = bds->msg_ctime;
305 #if 0
306 /* XXX What to put here? */
307 sds->msg_cv = 0;
308 sds->msg_qnum_cv = 0;
309 #endif
312 static void
313 svr4_32_to_bsd_msqid_ds(const struct svr4_32_msqid_ds *sds, struct msqid_ds *bds)
315 svr4_32_to_bsd_ipc_perm(&sds->msg_perm, &bds->msg_perm);
316 bds->_msg_first = NETBSD32PTR64(sds->msg_first);
317 bds->_msg_last = NETBSD32PTR64(sds->msg_last);
318 bds->_msg_cbytes = sds->msg_cbytes;
319 bds->msg_qnum = sds->msg_qnum;
320 bds->msg_qbytes = sds->msg_qbytes;
321 bds->msg_lspid = sds->msg_lspid;
322 bds->msg_lrpid = sds->msg_lrpid;
323 bds->msg_stime = sds->msg_stime;
324 bds->msg_rtime = sds->msg_rtime;
325 bds->msg_ctime = sds->msg_ctime;
327 #if 0
328 XXX sds->msg_cv
329 XXX sds->msg_qnum_cv
330 #endif
333 struct svr4_32_sys_msgsnd_args {
334 syscallarg(int) what;
335 syscallarg(int) msqid;
336 syscallarg(netbsd32_voidp) msgp;
337 syscallarg(netbsd32_size_t) msgsz;
338 syscallarg(int) msgflg;
341 static int
342 svr4_32_msgsnd(struct lwp *l, const struct svr4_32_sys_msgsnd_args *uap, register_t *retval)
344 struct sys_msgsnd_args ap;
346 SCARG(&ap, msqid) = SCARG(uap, msqid);
347 SCARG(&ap, msgp) = SCARG_P32(uap, msgp);
348 SCARG(&ap, msgsz) = SCARG(uap, msgsz);
349 SCARG(&ap, msgflg) = SCARG(uap, msgflg);
351 return sys_msgsnd(l, &ap, retval);
354 struct svr4_32_sys_msgrcv_args {
355 syscallarg(int) what;
356 syscallarg(int) msqid;
357 syscallarg(netbsd32_voidp) msgp;
358 syscallarg(netbsd32_size_t) msgsz;
359 syscallarg(netbsd32_long) msgtyp;
360 syscallarg(int) msgflg;
363 static int
364 svr4_32_msgrcv(struct lwp *l, const struct svr4_32_sys_msgrcv_args *uap, register_t *retval)
366 struct sys_msgrcv_args ap;
368 SCARG(&ap, msqid) = SCARG(uap, msqid);
369 SCARG(&ap, msgp) = SCARG_P32(uap, msgp);
370 SCARG(&ap, msgsz) = SCARG(uap, msgsz);
371 SCARG(&ap, msgtyp) = SCARG(uap, msgtyp);
372 SCARG(&ap, msgflg) = SCARG(uap, msgflg);
374 return sys_msgrcv(l, &ap, retval);
377 struct svr4_32_sys_msgget_args {
378 syscallarg(int) what;
379 syscallarg(svr4_key_t) key;
380 syscallarg(int) msgflg;
383 static int
384 svr4_32_msgget(struct lwp *l, const struct svr4_32_sys_msgget_args *uap, register_t *retval)
386 struct sys_msgget_args ap;
388 SCARG(&ap, key) = SCARG(uap, key);
389 SCARG(&ap, msgflg) = SCARG(uap, msgflg);
391 return sys_msgget(l, &ap, retval);
394 struct svr4_32_sys_msgctl_args {
395 syscallarg(int) what;
396 syscallarg(int) msqid;
397 syscallarg(int) cmd;
398 syscallarg(svr4_32_msqid_dsp) buf;
401 static int
402 svr4_32_msgctl(struct lwp *l, const struct svr4_32_sys_msgctl_args *uap, register_t *retval)
404 struct svr4_32_msqid_ds ss;
405 struct msqid_ds bs;
406 int error;
408 switch (SCARG(uap, cmd)) {
409 case SVR4_IPC_STAT:
410 error = msgctl1(l, SCARG(uap, msqid), IPC_STAT, &bs);
411 if (error == 0) {
412 bsd_to_svr4_32_msqid_ds(&bs, &ss);
413 error = copyout(&ss, SCARG_P32(uap, buf), sizeof ss);
415 return error;
417 case SVR4_IPC_SET:
418 error = copyin(SCARG_P32(uap, buf), &ss, sizeof ss);
419 if (error)
420 return error;
421 svr4_32_to_bsd_msqid_ds(&ss, &bs);
422 return msgctl1(l, SCARG(uap, msqid), IPC_SET, &bs);
424 case SVR4_IPC_RMID:
425 return msgctl1(l, SCARG(uap, msqid), IPC_RMID, NULL);
427 default:
428 return EINVAL;
433 svr4_32_sys_msgsys(struct lwp *l, const struct svr4_32_sys_msgsys_args *uap, register_t *retval)
436 DPRINTF(("svr4_32_msgsys(%d)\n", SCARG(uap, what)));
438 switch (SCARG(uap, what)) {
439 case SVR4_msgsnd:
440 return svr4_32_msgsnd(l, (const void *)uap, retval);
441 case SVR4_msgrcv:
442 return svr4_32_msgrcv(l, (const void *)uap, retval);
443 case SVR4_msgget:
444 return svr4_32_msgget(l, (const void *)uap, retval);
445 case SVR4_msgctl:
446 return svr4_32_msgctl(l, (const void *)uap, retval);
447 default:
448 return EINVAL;
451 #endif
453 #ifdef SYSVSHM
455 static void
456 bsd_to_svr4_32_shmid_ds(const struct shmid_ds *bds, struct svr4_32_shmid_ds *sds)
458 bsd_to_svr4_32_ipc_perm(&bds->shm_perm, &sds->shm_perm);
459 sds->shm_segsz = bds->shm_segsz;
460 sds->shm_lkcnt = 0;
461 sds->shm_lpid = bds->shm_lpid;
462 sds->shm_cpid = bds->shm_cpid;
463 NETBSD32PTR32(sds->shm_amp, bds->_shm_internal);
464 sds->shm_nattch = bds->shm_nattch;
465 sds->shm_cnattch = 0;
466 sds->shm_atime = bds->shm_atime;
467 sds->shm_pad1 = 0;
468 sds->shm_dtime = bds->shm_dtime;
469 sds->shm_pad2 = 0;
470 sds->shm_ctime = bds->shm_ctime;
471 sds->shm_pad3 = 0;
474 static void
475 svr4_32_to_bsd_shmid_ds(const struct svr4_32_shmid_ds *sds, struct shmid_ds *bds)
477 svr4_32_to_bsd_ipc_perm(&sds->shm_perm, &bds->shm_perm);
478 bds->shm_segsz = sds->shm_segsz;
479 bds->shm_lpid = sds->shm_lpid;
480 bds->shm_cpid = sds->shm_cpid;
481 bds->_shm_internal = NETBSD32PTR64(sds->shm_amp);
482 bds->shm_nattch = sds->shm_nattch;
483 bds->shm_atime = sds->shm_atime;
484 bds->shm_dtime = sds->shm_dtime;
485 bds->shm_ctime = sds->shm_ctime;
488 struct svr4_32_sys_shmat_args {
489 syscallarg(int) what;
490 syscallarg(int) shmid;
491 syscallarg(netbsd32_voidp) shmaddr;
492 syscallarg(int) shmflg;
495 static int
496 svr4_32_shmat(struct lwp *l, const struct svr4_32_sys_shmat_args *uap, register_t *retval)
498 struct sys_shmat_args ap;
500 SCARG(&ap, shmid) = SCARG(uap, shmid);
501 SCARG(&ap, shmaddr) = SCARG_P32(uap, shmaddr);
502 SCARG(&ap, shmflg) = SCARG(uap, shmflg);
504 return sys_shmat(l, &ap, retval);
507 struct svr4_32_sys_shmdt_args {
508 syscallarg(int) what;
509 syscallarg(netbsd32_voidp) shmaddr;
512 static int
513 svr4_32_shmdt(struct lwp *l, const struct svr4_32_sys_shmdt_args *uap, register_t *retval)
515 struct sys_shmdt_args ap;
517 SCARG(&ap, shmaddr) = SCARG_P32(uap, shmaddr);
519 return sys_shmdt(l, &ap, retval);
522 struct svr4_32_sys_shmget_args {
523 syscallarg(int) what;
524 syscallarg(netbsd32_key_t) key;
525 syscallarg(int) size;
526 syscallarg(int) shmflg;
529 static int
530 svr4_32_shmget(struct lwp *l, const struct svr4_32_sys_shmget_args *uap, register_t *retval)
532 struct sys_shmget_args ap;
534 SCARG(&ap, key) = SCARG(uap, key);
535 SCARG(&ap, size) = SCARG(uap, size);
536 SCARG(&ap, shmflg) = SCARG(uap, shmflg);
538 return sys_shmget(l, &ap, retval);
541 struct svr4_32_sys_shmctl_args {
542 syscallarg(int) what;
543 syscallarg(int) shmid;
544 syscallarg(int) cmd;
545 syscallarg(svr4_32_shmid_dsp) buf;
548 static int
549 svr4_32_shmctl(struct lwp *l, const struct svr4_32_sys_shmctl_args *uap, register_t *retval)
551 struct shmid_ds bs;
552 struct svr4_32_shmid_ds ss;
553 int error;
555 switch (SCARG(uap, cmd)) {
556 case SVR4_IPC_STAT:
557 error = shmctl1(l, SCARG(uap, shmid), IPC_STAT, &bs);
558 if (error == 0) {
559 bsd_to_svr4_32_shmid_ds(&bs, &ss);
560 error = copyout(&ss, SCARG_P32(uap, buf), sizeof ss);
562 return error;
564 case SVR4_IPC_SET:
565 error = copyin(SCARG_P32(uap, buf), &ss, sizeof ss);
566 if (error)
567 return error;
568 svr4_32_to_bsd_shmid_ds(&ss, &bs);
569 return shmctl1(l, SCARG(uap, shmid), IPC_SET, &bs);
571 case SVR4_IPC_RMID:
572 return shmctl1(l, SCARG(uap, shmid), IPC_RMID, NULL);
574 case SVR4_SHM_LOCK:
575 return shmctl1(l, SCARG(uap, shmid), SHM_LOCK, NULL);
577 case SVR4_SHM_UNLOCK:
578 return shmctl1(l, SCARG(uap, shmid), SHM_UNLOCK, NULL);
580 default:
581 return EINVAL;
586 svr4_32_sys_shmsys(struct lwp *l, const struct svr4_32_sys_shmsys_args *uap, register_t *retval)
589 DPRINTF(("svr4_32_shmsys(%d)\n", SCARG(uap, what)));
591 switch (SCARG(uap, what)) {
592 case SVR4_shmat:
593 return svr4_32_shmat(l, (const void *)uap, retval);
594 case SVR4_shmdt:
595 return svr4_32_shmdt(l, (const void *)uap, retval);
596 case SVR4_shmget:
597 return svr4_32_shmget(l, (const void *)uap, retval);
598 case SVR4_shmctl:
599 return svr4_32_shmctl(l, (const void *)uap, retval);
600 default:
601 return ENOSYS;
604 #endif /* SYSVSHM */