2 * Copyright (c) 1994-1995 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software withough specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/compat/linux/linux_ipc.c,v 1.17.2.3 2001/11/05 19:08:22 marcel Exp $
29 * $DragonFly: src/sys/emulation/linux/linux_ipc.c,v 1.3 2003/06/23 17:55:26 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysproto.h>
39 #include <machine/../linux/linux.h>
40 #include <machine/../linux/linux_proto.h>
41 #include <compat/linux/linux_ipc.h>
42 #include <compat/linux/linux_util.h>
67 l_ulong shm_tot; /* total allocated shm */
68 l_ulong shm_rss; /* total resident shm */
69 l_ulong shm_swp; /* total swapped shm */
70 l_ulong swap_attempts;
71 l_ulong swap_successes;
85 linux_to_bsd_ipc_perm(struct l_ipc_perm *lpp, struct ipc_perm *bpp)
90 bpp->cuid = lpp->cuid;
91 bpp->cgid = lpp->cgid;
92 bpp->mode = lpp->mode;
98 bsd_to_linux_ipc_perm(struct ipc_perm *bpp, struct l_ipc_perm *lpp)
103 lpp->cuid = bpp->cuid;
104 lpp->cgid = bpp->cgid;
105 lpp->mode = bpp->mode;
110 struct l_ipc_perm sem_perm;
115 void *sem_pending_last;
121 struct l_ipc_perm shm_perm;
135 linux_to_bsd_semid_ds(struct l_semid_ds *lsp, struct semid_ds *bsp)
137 linux_to_bsd_ipc_perm(&lsp->sem_perm, &bsp->sem_perm);
138 bsp->sem_otime = lsp->sem_otime;
139 bsp->sem_ctime = lsp->sem_ctime;
140 bsp->sem_nsems = lsp->sem_nsems;
141 bsp->sem_base = lsp->sem_base;
145 bsd_to_linux_semid_ds(struct semid_ds *bsp, struct l_semid_ds *lsp)
147 bsd_to_linux_ipc_perm(&bsp->sem_perm, &lsp->sem_perm);
148 lsp->sem_otime = bsp->sem_otime;
149 lsp->sem_ctime = bsp->sem_ctime;
150 lsp->sem_nsems = bsp->sem_nsems;
151 lsp->sem_base = bsp->sem_base;
155 linux_to_bsd_shmid_ds(struct l_shmid_ds *lsp, struct shmid_ds *bsp)
157 linux_to_bsd_ipc_perm(&lsp->shm_perm, &bsp->shm_perm);
158 bsp->shm_segsz = lsp->shm_segsz;
159 bsp->shm_lpid = lsp->shm_lpid;
160 bsp->shm_cpid = lsp->shm_cpid;
161 bsp->shm_nattch = lsp->shm_nattch;
162 bsp->shm_atime = lsp->shm_atime;
163 bsp->shm_dtime = lsp->shm_dtime;
164 bsp->shm_ctime = lsp->shm_ctime;
165 bsp->shm_internal = lsp->private3; /* this goes (yet) SOS */
169 bsd_to_linux_shmid_ds(struct shmid_ds *bsp, struct l_shmid_ds *lsp)
171 bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->shm_perm);
172 lsp->shm_segsz = bsp->shm_segsz;
173 lsp->shm_lpid = bsp->shm_lpid;
174 lsp->shm_cpid = bsp->shm_cpid;
175 lsp->shm_nattch = bsp->shm_nattch;
176 lsp->shm_atime = bsp->shm_atime;
177 lsp->shm_dtime = bsp->shm_dtime;
178 lsp->shm_ctime = bsp->shm_ctime;
179 lsp->private3 = bsp->shm_internal; /* this goes (yet) SOS */
183 linux_semop(struct linux_semop_args *args)
185 struct semop_args /* {
191 bsd_args.semid = args->semid;
192 bsd_args.sops = (struct sembuf *)args->tsops;
193 bsd_args.nsops = args->nsops;
194 return semop(&bsd_args);
198 linux_semget(struct linux_semget_args *args)
200 struct semget_args /* {
206 bsd_args.key = args->key;
207 bsd_args.nsems = args->nsems;
208 bsd_args.semflg = args->semflg;
209 return semget(&bsd_args);
213 linux_semctl(struct linux_semctl_args *args)
215 struct proc *p = curproc;
216 struct l_semid_ds linux_semid;
217 struct __semctl_args /* {
223 struct l_seminfo linux_seminfo;
228 sg = stackgap_init();
230 /* Make sure the arg parameter can be copied in. */
231 unptr = stackgap_alloc(&sg, sizeof(union semun));
232 bcopy(&args->arg, unptr, sizeof(union semun));
234 bsd_args.semid = args->semid;
235 bsd_args.semnum = args->semnum;
236 bsd_args.arg = unptr;
240 bsd_args.cmd = IPC_RMID;
243 bsd_args.cmd = GETNCNT;
246 bsd_args.cmd = GETPID;
249 bsd_args.cmd = GETVAL;
252 bsd_args.cmd = GETZCNT;
255 bsd_args.cmd = SETVAL;
258 bsd_args.cmd = IPC_SET;
259 error = copyin((caddr_t)args->arg.buf, &linux_semid,
260 sizeof(linux_semid));
263 unptr->buf = stackgap_alloc(&sg, sizeof(struct semid_ds));
264 linux_to_bsd_semid_ds(&linux_semid, unptr->buf);
265 return __semctl(&bsd_args);
267 bsd_args.cmd = IPC_STAT;
268 unptr->buf = stackgap_alloc(&sg, sizeof(struct semid_ds));
269 error = __semctl(&bsd_args);
272 p->p_retval[0] = IXSEQ_TO_IPCID(bsd_args.semid,
273 unptr->buf->sem_perm);
274 bsd_to_linux_semid_ds(unptr->buf, &linux_semid);
275 return copyout(&linux_semid, (caddr_t)args->arg.buf,
276 sizeof(linux_semid));
279 error = copyin((caddr_t)args->arg.buf, &linux_seminfo,
280 sizeof(linux_seminfo) );
283 bcopy(&seminfo, &linux_seminfo, sizeof(linux_seminfo) );
284 /* XXX BSD equivalent?
285 #define used_semids 10
287 linux_seminfo.semusz = used_semids;
288 linux_seminfo.semaem = used_sems;
290 error = copyout((caddr_t)&linux_seminfo, (caddr_t)args->arg.buf,
291 sizeof(linux_seminfo) );
294 p->p_retval[0] = seminfo.semmni;
295 return 0; /* No need for __semctl call */
301 uprintf("linux: 'ipc' typ=%d not implemented\n", args->cmd);
304 return __semctl(&bsd_args);
308 linux_msgsnd(struct linux_msgsnd_args *args)
310 struct msgsnd_args /* {
317 bsd_args.msqid = args->msqid;
318 bsd_args.msgp = args->msgp;
319 bsd_args.msgsz = args->msgsz;
320 bsd_args.msgflg = args->msgflg;
321 return msgsnd(&bsd_args);
325 linux_msgrcv(struct linux_msgrcv_args *args)
327 struct msgrcv_args /* {
335 bsd_args.msqid = args->msqid;
336 bsd_args.msgp = args->msgp;
337 bsd_args.msgsz = args->msgsz;
338 bsd_args.msgtyp = 0; /* XXX - args->msgtyp; */
339 bsd_args.msgflg = args->msgflg;
340 return msgrcv(&bsd_args);
344 linux_msgget(struct linux_msgget_args *args)
346 struct msgget_args /* {
351 bsd_args.key = args->key;
352 bsd_args.msgflg = args->msgflg;
353 return msgget(&bsd_args);
357 linux_msgctl(struct linux_msgctl_args *args)
359 struct msgctl_args /* {
362 struct msqid_ds *buf;
366 bsd_args.msqid = args->msqid;
367 bsd_args.cmd = args->cmd;
368 bsd_args.buf = (struct msqid_ds *)args->buf;
369 error = msgctl(&bsd_args);
370 return ((args->cmd == LINUX_IPC_RMID && error == EINVAL) ? 0 : error);
374 linux_shmat(struct linux_shmat_args *args)
376 struct proc *p = curproc;
377 struct shmat_args /* {
384 bsd_args.shmid = args->shmid;
385 bsd_args.shmaddr = args->shmaddr;
386 bsd_args.shmflg = args->shmflg;
387 if ((error = shmat(&bsd_args)))
390 if ((error = copyout(p->p_retval, (caddr_t)args->raddr, sizeof(l_ulong))))
398 linux_shmdt(struct linux_shmdt_args *args)
400 struct shmdt_args /* {
404 bsd_args.shmaddr = args->shmaddr;
405 return shmdt(&bsd_args);
409 linux_shmget(struct linux_shmget_args *args)
411 struct shmget_args /* {
417 bsd_args.key = args->key;
418 bsd_args.size = args->size;
419 bsd_args.shmflg = args->shmflg;
420 return shmget(&bsd_args);
424 linux_shmctl(struct linux_shmctl_args *args)
426 struct l_shmid_ds linux_shmid;
427 struct shmctl_args /* {
430 struct shmid_ds *buf;
433 caddr_t sg = stackgap_init();
437 bsd_args.shmid = args->shmid;
438 bsd_args.cmd = IPC_STAT;
439 bsd_args.buf = (struct shmid_ds*)stackgap_alloc(&sg, sizeof(struct shmid_ds));
440 if ((error = shmctl(&bsd_args)))
442 bsd_to_linux_shmid_ds(bsd_args.buf, &linux_shmid);
443 return copyout(&linux_shmid, (caddr_t)args->buf, sizeof(linux_shmid));
446 if ((error = copyin((caddr_t)args->buf, &linux_shmid,
447 sizeof(linux_shmid))))
449 bsd_args.buf = (struct shmid_ds*)stackgap_alloc(&sg, sizeof(struct shmid_ds));
450 linux_to_bsd_shmid_ds(&linux_shmid, bsd_args.buf);
451 bsd_args.shmid = args->shmid;
452 bsd_args.cmd = IPC_SET;
453 return shmctl(&bsd_args);
456 bsd_args.shmid = args->shmid;
457 bsd_args.cmd = IPC_RMID;
458 if (args->buf == NULL)
461 if ((error = copyin((caddr_t)args->buf, &linux_shmid,
462 sizeof(linux_shmid))))
464 bsd_args.buf = (struct shmid_ds*)stackgap_alloc(&sg, sizeof(struct shmid_ds));
465 linux_to_bsd_shmid_ds(&linux_shmid, bsd_args.buf);
467 return shmctl(&bsd_args);
473 case LINUX_SHM_UNLOCK:
475 uprintf("linux: 'ipc' typ=%d not implemented\n", args->cmd);