linux_ipc.c revision 1.51 1 1.51 njoly /* $NetBSD: linux_ipc.c,v 1.51 2008/05/21 11:15:57 njoly Exp $ */
2 1.14 erh
3 1.14 erh /*-
4 1.16 fvdl * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5 1.14 erh * All rights reserved.
6 1.14 erh *
7 1.14 erh * This code is derived from software contributed to The NetBSD Foundation
8 1.16 fvdl * by Frank van der Linden and Eric Haszlakiewicz.
9 1.14 erh *
10 1.14 erh * Redistribution and use in source and binary forms, with or without
11 1.14 erh * modification, are permitted provided that the following conditions
12 1.14 erh * are met:
13 1.14 erh * 1. Redistributions of source code must retain the above copyright
14 1.14 erh * notice, this list of conditions and the following disclaimer.
15 1.14 erh * 2. Redistributions in binary form must reproduce the above copyright
16 1.14 erh * notice, this list of conditions and the following disclaimer in the
17 1.14 erh * documentation and/or other materials provided with the distribution.
18 1.14 erh *
19 1.14 erh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.14 erh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.14 erh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.14 erh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.14 erh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.14 erh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.14 erh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.14 erh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.14 erh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.14 erh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.14 erh * POSSIBILITY OF SUCH DAMAGE.
30 1.1 fvdl */
31 1.24 lukem
32 1.24 lukem #include <sys/cdefs.h>
33 1.51 njoly __KERNEL_RCSID(0, "$NetBSD: linux_ipc.c,v 1.51 2008/05/21 11:15:57 njoly Exp $");
34 1.19 erh
35 1.23 mrg #if defined(_KERNEL_OPT)
36 1.19 erh #include "opt_sysv.h"
37 1.22 jdolecek #endif
38 1.1 fvdl
39 1.1 fvdl #include <sys/param.h>
40 1.1 fvdl #include <sys/shm.h>
41 1.6 fvdl #include <sys/sem.h>
42 1.1 fvdl #include <sys/msg.h>
43 1.1 fvdl #include <sys/proc.h>
44 1.1 fvdl #include <sys/systm.h>
45 1.43 njoly #include <sys/vnode.h>
46 1.1 fvdl
47 1.1 fvdl #include <sys/mount.h>
48 1.1 fvdl #include <sys/syscallargs.h>
49 1.1 fvdl
50 1.15 christos #include <compat/linux/common/linux_types.h>
51 1.15 christos #include <compat/linux/common/linux_signal.h>
52 1.15 christos #include <compat/linux/common/linux_util.h>
53 1.15 christos #include <compat/linux/common/linux_ipc.h>
54 1.15 christos #include <compat/linux/common/linux_msg.h>
55 1.15 christos #include <compat/linux/common/linux_shm.h>
56 1.15 christos #include <compat/linux/common/linux_sem.h>
57 1.1 fvdl
58 1.33 manu #include <compat/linux/linux_syscallargs.h>
59 1.33 manu #include <compat/linux/linux_syscall.h>
60 1.33 manu
61 1.34 dogcow #include <compat/linux/common/linux_ipccall.h>
62 1.44 njoly #include <compat/linux/common/linux_machdep.h>
63 1.34 dogcow
64 1.1 fvdl /*
65 1.14 erh * Note: Not all linux architechtures have explicit versions
66 1.14 erh * of the SYSV* syscalls. On the ones that don't
67 1.14 erh * we pretend that they are defined anyway. *_args and
68 1.14 erh * prototypes are defined in individual headers;
69 1.14 erh * syscalls.master lists those syscalls as NOARGS.
70 1.1 fvdl *
71 1.14 erh * The functions in multiarch are the ones that just need
72 1.14 erh * the arguments shuffled around and then use the
73 1.14 erh * normal NetBSD syscall.
74 1.14 erh *
75 1.14 erh * Function in multiarch:
76 1.14 erh * linux_sys_ipc : linux_ipccall.c
77 1.14 erh * liunx_semop : linux_ipccall.c
78 1.14 erh * linux_semget : linux_ipccall.c
79 1.14 erh * linux_msgsnd : linux_ipccall.c
80 1.14 erh * linux_msgrcv : linux_ipccall.c
81 1.14 erh * linux_msgget : linux_ipccall.c
82 1.14 erh * linux_shmdt : linux_ipccall.c
83 1.14 erh * linux_shmget : linux_ipccall.c
84 1.1 fvdl */
85 1.1 fvdl
86 1.11 fvdl #if defined (SYSVSEM) || defined(SYSVSHM) || defined(SYSVMSG)
87 1.1 fvdl /*
88 1.1 fvdl * Convert between Linux and NetBSD ipc_perm structures. Only the
89 1.1 fvdl * order of the fields is different.
90 1.1 fvdl */
91 1.14 erh void
92 1.41 dsl linux_to_bsd_ipc_perm(struct linux_ipc_perm *lpp, struct ipc_perm *bpp)
93 1.1 fvdl {
94 1.8 mycroft
95 1.21 thorpej bpp->_key = lpp->l_key;
96 1.1 fvdl bpp->uid = lpp->l_uid;
97 1.1 fvdl bpp->gid = lpp->l_gid;
98 1.1 fvdl bpp->cuid = lpp->l_cuid;
99 1.1 fvdl bpp->cgid = lpp->l_cgid;
100 1.1 fvdl bpp->mode = lpp->l_mode;
101 1.21 thorpej bpp->_seq = lpp->l_seq;
102 1.1 fvdl }
103 1.1 fvdl
104 1.14 erh void
105 1.41 dsl linux_to_bsd_ipc64_perm(struct linux_ipc64_perm *lpp, struct ipc_perm *bpp)
106 1.32 christos {
107 1.32 christos bpp->_key = lpp->l_key;
108 1.32 christos bpp->uid = lpp->l_uid;
109 1.32 christos bpp->gid = lpp->l_gid;
110 1.32 christos bpp->cuid = lpp->l_cuid;
111 1.32 christos bpp->cgid = lpp->l_cgid;
112 1.32 christos bpp->mode = lpp->l_mode;
113 1.32 christos bpp->_seq = lpp->l_seq;
114 1.32 christos }
115 1.32 christos
116 1.32 christos void
117 1.41 dsl bsd_to_linux_ipc_perm(struct ipc_perm *bpp, struct linux_ipc_perm *lpp)
118 1.1 fvdl {
119 1.8 mycroft
120 1.21 thorpej lpp->l_key = bpp->_key;
121 1.1 fvdl lpp->l_uid = bpp->uid;
122 1.1 fvdl lpp->l_gid = bpp->gid;
123 1.1 fvdl lpp->l_cuid = bpp->cuid;
124 1.1 fvdl lpp->l_cgid = bpp->cgid;
125 1.1 fvdl lpp->l_mode = bpp->mode;
126 1.21 thorpej lpp->l_seq = bpp->_seq;
127 1.1 fvdl }
128 1.32 christos
129 1.32 christos void
130 1.41 dsl bsd_to_linux_ipc64_perm(struct ipc_perm *bpp, struct linux_ipc64_perm *lpp)
131 1.32 christos {
132 1.32 christos lpp->l_key = bpp->_key;
133 1.32 christos lpp->l_uid = bpp->uid;
134 1.32 christos lpp->l_gid = bpp->gid;
135 1.32 christos lpp->l_cuid = bpp->cuid;
136 1.32 christos lpp->l_cgid = bpp->cgid;
137 1.32 christos lpp->l_mode = bpp->mode;
138 1.32 christos lpp->l_seq = bpp->_seq;
139 1.32 christos }
140 1.32 christos
141 1.11 fvdl #endif
142 1.1 fvdl
143 1.1 fvdl #ifdef SYSVSEM
144 1.1 fvdl /*
145 1.6 fvdl * Semaphore operations. Most constants and structures are the same on
146 1.6 fvdl * both systems. Only semctl() needs some extra work.
147 1.1 fvdl */
148 1.6 fvdl
149 1.6 fvdl /*
150 1.6 fvdl * Convert between Linux and NetBSD semid_ds structures.
151 1.6 fvdl */
152 1.14 erh void
153 1.41 dsl bsd_to_linux_semid_ds(struct semid_ds *bs, struct linux_semid_ds *ls)
154 1.6 fvdl {
155 1.6 fvdl bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
156 1.6 fvdl ls->l_sem_otime = bs->sem_otime;
157 1.6 fvdl ls->l_sem_ctime = bs->sem_ctime;
158 1.6 fvdl ls->l_sem_nsems = bs->sem_nsems;
159 1.21 thorpej ls->l_sem_base = bs->_sem_base;
160 1.6 fvdl }
161 1.6 fvdl
162 1.14 erh void
163 1.49 njoly bsd_to_linux_semid64_ds(struct semid_ds *bs, struct linux_semid64_ds *ls)
164 1.49 njoly {
165 1.49 njoly bsd_to_linux_ipc64_perm(&bs->sem_perm, &ls->l_sem_perm);
166 1.49 njoly ls->l_sem_otime = bs->sem_otime;
167 1.49 njoly ls->l_sem_ctime = bs->sem_ctime;
168 1.49 njoly ls->l_sem_nsems = bs->sem_nsems;
169 1.49 njoly }
170 1.49 njoly
171 1.49 njoly void
172 1.41 dsl linux_to_bsd_semid_ds(struct linux_semid_ds *ls, struct semid_ds *bs)
173 1.6 fvdl {
174 1.6 fvdl linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
175 1.6 fvdl bs->sem_otime = ls->l_sem_otime;
176 1.6 fvdl bs->sem_ctime = ls->l_sem_ctime;
177 1.6 fvdl bs->sem_nsems = ls->l_sem_nsems;
178 1.21 thorpej bs->_sem_base = ls->l_sem_base;
179 1.6 fvdl }
180 1.6 fvdl
181 1.49 njoly void
182 1.49 njoly linux_to_bsd_semid64_ds(struct linux_semid64_ds *ls, struct semid_ds *bs)
183 1.49 njoly {
184 1.49 njoly linux_to_bsd_ipc64_perm(&ls->l_sem_perm, &bs->sem_perm);
185 1.49 njoly bs->sem_otime = ls->l_sem_otime;
186 1.49 njoly bs->sem_ctime = ls->l_sem_ctime;
187 1.49 njoly bs->sem_nsems = ls->l_sem_nsems;
188 1.49 njoly }
189 1.49 njoly
190 1.6 fvdl /*
191 1.21 thorpej * Most of this can be handled by directly passing the arguments on; we
192 1.21 thorpej * just need to frob the `cmd' and convert the semid_ds and semun.
193 1.6 fvdl */
194 1.1 fvdl int
195 1.42 dsl linux_sys_semctl(struct lwp *l, const struct linux_sys_semctl_args *uap, register_t *retval)
196 1.1 fvdl {
197 1.42 dsl /* {
198 1.14 erh syscallarg(int) semid;
199 1.14 erh syscallarg(int) semnum;
200 1.14 erh syscallarg(int) cmd;
201 1.14 erh syscallarg(union linux_semun) arg;
202 1.42 dsl } */
203 1.21 thorpej struct semid_ds sembuf;
204 1.21 thorpej struct linux_semid_ds lsembuf;
205 1.49 njoly struct linux_semid64_ds lsembuf64;
206 1.21 thorpej union __semun semun;
207 1.49 njoly int cmd, lcmd, error;
208 1.21 thorpej void *pass_arg = NULL;
209 1.21 thorpej
210 1.49 njoly lcmd = SCARG(uap, cmd);
211 1.49 njoly #ifdef LINUX_IPC_FORCE64
212 1.49 njoly if (lcmd == LINUX_IPC_STAT || lcmd == LINUX_IPC_SET)
213 1.49 njoly lcmd |= LINUX_IPC_64;
214 1.49 njoly #endif
215 1.21 thorpej
216 1.49 njoly switch (lcmd) {
217 1.21 thorpej case LINUX_IPC_SET:
218 1.49 njoly error = copyin(SCARG(uap, arg).l_buf, &lsembuf,
219 1.49 njoly sizeof(lsembuf));
220 1.49 njoly if (error)
221 1.49 njoly return (error);
222 1.49 njoly linux_to_bsd_semid_ds(&lsembuf, &sembuf);
223 1.49 njoly pass_arg = &sembuf;
224 1.49 njoly cmd = IPC_SET;
225 1.49 njoly break;
226 1.49 njoly
227 1.49 njoly case LINUX_IPC_SET | LINUX_IPC_64:
228 1.49 njoly error = copyin(SCARG(uap, arg).l_buf, &lsembuf64,
229 1.49 njoly sizeof(lsembuf64));
230 1.49 njoly if (error)
231 1.49 njoly return (error);
232 1.49 njoly linux_to_bsd_semid64_ds(&lsembuf64, &sembuf);
233 1.21 thorpej pass_arg = &sembuf;
234 1.21 thorpej cmd = IPC_SET;
235 1.21 thorpej break;
236 1.6 fvdl
237 1.13 mycroft case LINUX_IPC_STAT:
238 1.49 njoly case LINUX_IPC_STAT | LINUX_IPC_64:
239 1.21 thorpej pass_arg = &sembuf;
240 1.21 thorpej cmd = IPC_STAT;
241 1.14 erh break;
242 1.21 thorpej
243 1.13 mycroft case LINUX_IPC_RMID:
244 1.21 thorpej cmd = IPC_RMID;
245 1.13 mycroft break;
246 1.21 thorpej
247 1.6 fvdl case LINUX_GETVAL:
248 1.21 thorpej cmd = GETVAL;
249 1.6 fvdl break;
250 1.21 thorpej
251 1.6 fvdl case LINUX_GETPID:
252 1.21 thorpej cmd = GETPID;
253 1.6 fvdl break;
254 1.21 thorpej
255 1.6 fvdl case LINUX_GETNCNT:
256 1.21 thorpej cmd = GETNCNT;
257 1.6 fvdl break;
258 1.21 thorpej
259 1.6 fvdl case LINUX_GETZCNT:
260 1.21 thorpej cmd = GETZCNT;
261 1.6 fvdl break;
262 1.21 thorpej
263 1.21 thorpej case LINUX_GETALL:
264 1.21 thorpej pass_arg = &semun;
265 1.21 thorpej semun.array = SCARG(uap, arg).l_array;
266 1.21 thorpej cmd = GETALL;
267 1.21 thorpej break;
268 1.21 thorpej
269 1.6 fvdl case LINUX_SETVAL:
270 1.21 thorpej pass_arg = &semun;
271 1.21 thorpej semun.val = SCARG(uap, arg).l_val;
272 1.21 thorpej cmd = SETVAL;
273 1.20 tron break;
274 1.21 thorpej
275 1.20 tron case LINUX_SETALL:
276 1.21 thorpej pass_arg = &semun;
277 1.21 thorpej semun.array = SCARG(uap, arg).l_array;
278 1.21 thorpej cmd = SETALL;
279 1.6 fvdl break;
280 1.21 thorpej
281 1.6 fvdl default:
282 1.21 thorpej return (EINVAL);
283 1.21 thorpej }
284 1.21 thorpej
285 1.35 ad error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
286 1.21 thorpej pass_arg, retval);
287 1.49 njoly if (error)
288 1.49 njoly return error;
289 1.21 thorpej
290 1.49 njoly switch (lcmd) {
291 1.49 njoly case LINUX_IPC_STAT:
292 1.21 thorpej bsd_to_linux_semid_ds(&sembuf, &lsembuf);
293 1.21 thorpej error = copyout(&lsembuf, SCARG(uap, arg).l_buf,
294 1.21 thorpej sizeof(lsembuf));
295 1.49 njoly break;
296 1.49 njoly case LINUX_IPC_STAT | LINUX_IPC_64:
297 1.49 njoly bsd_to_linux_semid64_ds(&sembuf, &lsembuf64);
298 1.49 njoly error = copyout(&lsembuf64, SCARG(uap, arg).l_buf,
299 1.49 njoly sizeof(lsembuf64));
300 1.49 njoly break;
301 1.49 njoly default:
302 1.49 njoly break;
303 1.6 fvdl }
304 1.21 thorpej
305 1.21 thorpej return (error);
306 1.1 fvdl }
307 1.1 fvdl #endif /* SYSVSEM */
308 1.1 fvdl
309 1.1 fvdl #ifdef SYSVMSG
310 1.6 fvdl
311 1.14 erh void
312 1.41 dsl linux_to_bsd_msqid_ds(struct linux_msqid_ds *lmp, struct msqid_ds *bmp)
313 1.6 fvdl {
314 1.8 mycroft
315 1.6 fvdl linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
316 1.21 thorpej bmp->_msg_first = lmp->l_msg_first;
317 1.21 thorpej bmp->_msg_last = lmp->l_msg_last;
318 1.21 thorpej bmp->_msg_cbytes = lmp->l_msg_cbytes;
319 1.6 fvdl bmp->msg_qnum = lmp->l_msg_qnum;
320 1.6 fvdl bmp->msg_qbytes = lmp->l_msg_qbytes;
321 1.6 fvdl bmp->msg_lspid = lmp->l_msg_lspid;
322 1.6 fvdl bmp->msg_lrpid = lmp->l_msg_lrpid;
323 1.6 fvdl bmp->msg_stime = lmp->l_msg_stime;
324 1.6 fvdl bmp->msg_rtime = lmp->l_msg_rtime;
325 1.6 fvdl bmp->msg_ctime = lmp->l_msg_ctime;
326 1.6 fvdl }
327 1.6 fvdl
328 1.14 erh void
329 1.51 njoly linux_to_bsd_msqid64_ds(struct linux_msqid64_ds *lmp, struct msqid_ds *bmp)
330 1.51 njoly {
331 1.51 njoly linux_to_bsd_ipc64_perm(&lmp->l_msg_perm, &bmp->msg_perm);
332 1.51 njoly bmp->msg_stime = lmp->l_msg_stime;
333 1.51 njoly bmp->msg_rtime = lmp->l_msg_rtime;
334 1.51 njoly bmp->msg_ctime = lmp->l_msg_ctime;
335 1.51 njoly bmp->_msg_cbytes = lmp->l_msg_cbytes;
336 1.51 njoly bmp->msg_qnum = lmp->l_msg_qnum;
337 1.51 njoly bmp->msg_qbytes = lmp->l_msg_qbytes;
338 1.51 njoly bmp->msg_lspid = lmp->l_msg_lspid;
339 1.51 njoly bmp->msg_lrpid = lmp->l_msg_lrpid;
340 1.51 njoly }
341 1.51 njoly
342 1.51 njoly void
343 1.41 dsl bsd_to_linux_msqid_ds(struct msqid_ds *bmp, struct linux_msqid_ds *lmp)
344 1.6 fvdl {
345 1.8 mycroft
346 1.6 fvdl bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
347 1.21 thorpej lmp->l_msg_first = bmp->_msg_first;
348 1.21 thorpej lmp->l_msg_last = bmp->_msg_last;
349 1.21 thorpej lmp->l_msg_cbytes = bmp->_msg_cbytes;
350 1.6 fvdl lmp->l_msg_qnum = bmp->msg_qnum;
351 1.6 fvdl lmp->l_msg_qbytes = bmp->msg_qbytes;
352 1.6 fvdl lmp->l_msg_lspid = bmp->msg_lspid;
353 1.6 fvdl lmp->l_msg_lrpid = bmp->msg_lrpid;
354 1.6 fvdl lmp->l_msg_stime = bmp->msg_stime;
355 1.6 fvdl lmp->l_msg_rtime = bmp->msg_rtime;
356 1.6 fvdl lmp->l_msg_ctime = bmp->msg_ctime;
357 1.6 fvdl }
358 1.6 fvdl
359 1.51 njoly void
360 1.51 njoly bsd_to_linux_msqid64_ds(struct msqid_ds *bmp, struct linux_msqid64_ds *lmp)
361 1.51 njoly {
362 1.51 njoly bsd_to_linux_ipc64_perm(&bmp->msg_perm, &lmp->l_msg_perm);
363 1.51 njoly lmp->l_msg_stime = bmp->msg_stime;
364 1.51 njoly lmp->l_msg_rtime = bmp->msg_rtime;
365 1.51 njoly lmp->l_msg_ctime = bmp->msg_ctime;
366 1.51 njoly lmp->l_msg_cbytes = bmp->_msg_cbytes;
367 1.51 njoly lmp->l_msg_qnum = bmp->msg_qnum;
368 1.51 njoly lmp->l_msg_qbytes = bmp->msg_qbytes;
369 1.51 njoly lmp->l_msg_lspid = bmp->msg_lspid;
370 1.51 njoly lmp->l_msg_lrpid = bmp->msg_lrpid;
371 1.51 njoly }
372 1.51 njoly
373 1.14 erh int
374 1.42 dsl linux_sys_msgctl(struct lwp *l, const struct linux_sys_msgctl_args *uap, register_t *retval)
375 1.1 fvdl {
376 1.42 dsl /* {
377 1.14 erh syscallarg(int) msqid;
378 1.14 erh syscallarg(int) cmd;
379 1.14 erh syscallarg(struct linux_msqid_ds *) buf;
380 1.42 dsl } */
381 1.51 njoly struct msqid_ds bm, *bmp = NULL;
382 1.6 fvdl struct linux_msqid_ds lm;
383 1.51 njoly struct linux_msqid64_ds lm64;
384 1.51 njoly int cmd, lcmd, error;
385 1.51 njoly
386 1.51 njoly lcmd = SCARG(uap, cmd);
387 1.51 njoly #ifdef LINUX_IPC_FORCE64
388 1.51 njoly if (lcmd == LINUX_IPC_STAT || lcmd == LINUX_IPC_SET)
389 1.51 njoly lcmd |= LINUX_IPC_64;
390 1.51 njoly #endif
391 1.6 fvdl
392 1.51 njoly switch (lcmd) {
393 1.13 mycroft case LINUX_IPC_STAT:
394 1.51 njoly case LINUX_IPC_STAT|LINUX_IPC_64:
395 1.51 njoly cmd = IPC_STAT;
396 1.51 njoly bmp = &bm;
397 1.51 njoly break;
398 1.6 fvdl case LINUX_IPC_SET:
399 1.14 erh if ((error = copyin(SCARG(uap, buf), &lm, sizeof lm)))
400 1.6 fvdl return error;
401 1.6 fvdl linux_to_bsd_msqid_ds(&lm, &bm);
402 1.51 njoly cmd = IPC_SET;
403 1.51 njoly bmp = &bm;
404 1.51 njoly break;
405 1.51 njoly case LINUX_IPC_SET|LINUX_IPC_64:
406 1.51 njoly if ((error = copyin(SCARG(uap, buf), &lm64, sizeof lm64)))
407 1.51 njoly return error;
408 1.51 njoly linux_to_bsd_msqid64_ds(&lm64, &bm);
409 1.51 njoly cmd = IPC_SET;
410 1.51 njoly bmp = &bm;
411 1.51 njoly break;
412 1.13 mycroft case LINUX_IPC_RMID:
413 1.51 njoly cmd = IPC_RMID;
414 1.13 mycroft break;
415 1.13 mycroft default:
416 1.13 mycroft return EINVAL;
417 1.6 fvdl }
418 1.51 njoly
419 1.51 njoly if ((error = msgctl1(l, SCARG(uap, msqid), cmd, bmp)))
420 1.51 njoly return error;
421 1.51 njoly
422 1.51 njoly switch (lcmd) {
423 1.51 njoly case LINUX_IPC_STAT:
424 1.51 njoly bsd_to_linux_msqid_ds(&bm, &lm);
425 1.51 njoly error = copyout(&lm, SCARG(uap, buf), sizeof lm);
426 1.51 njoly break;
427 1.51 njoly case LINUX_IPC_STAT|LINUX_IPC_64:
428 1.51 njoly bsd_to_linux_msqid64_ds(&bm, &lm64);
429 1.51 njoly error = copyout(&lm64, SCARG(uap, buf), sizeof lm64);
430 1.51 njoly break;
431 1.51 njoly default:
432 1.51 njoly break;
433 1.51 njoly }
434 1.51 njoly
435 1.51 njoly return error;
436 1.1 fvdl }
437 1.1 fvdl #endif /* SYSVMSG */
438 1.1 fvdl
439 1.1 fvdl #ifdef SYSVSHM
440 1.1 fvdl /*
441 1.30 jdolecek * shmget(2). Just make sure the Linux-compatible shmat() semantics
442 1.30 jdolecek * is enabled for the segment, so that shmat() succeeds even when
443 1.30 jdolecek * the segment would be removed.
444 1.30 jdolecek */
445 1.30 jdolecek int
446 1.42 dsl linux_sys_shmget(struct lwp *l, const struct linux_sys_shmget_args *uap, register_t *retval)
447 1.30 jdolecek {
448 1.42 dsl /* {
449 1.30 jdolecek syscallarg(key_t) key;
450 1.30 jdolecek syscallarg(size_t) size;
451 1.30 jdolecek syscallarg(int) shmflg;
452 1.42 dsl } */
453 1.42 dsl struct sys_shmget_args bsd_ua;
454 1.30 jdolecek
455 1.42 dsl SCARG(&bsd_ua, key) = SCARG(uap, key);
456 1.42 dsl SCARG(&bsd_ua, size) = SCARG(uap, size);
457 1.42 dsl SCARG(&bsd_ua, shmflg) = SCARG(uap, shmflg) | _SHM_RMLINGER;
458 1.42 dsl
459 1.42 dsl return sys_shmget(l, &bsd_ua, retval);
460 1.30 jdolecek }
461 1.30 jdolecek
462 1.30 jdolecek /*
463 1.1 fvdl * shmat(2). Very straightforward, except that Linux passes a pointer
464 1.1 fvdl * in which the return value is to be passed. This is subsequently
465 1.1 fvdl * handled by libc, apparently.
466 1.1 fvdl */
467 1.36 manu #ifndef __amd64__
468 1.14 erh int
469 1.42 dsl linux_sys_shmat(struct lwp *l, const struct linux_sys_shmat_args *uap, register_t *retval)
470 1.1 fvdl {
471 1.42 dsl /* {
472 1.14 erh syscallarg(int) shmid;
473 1.14 erh syscallarg(void *) shmaddr;
474 1.14 erh syscallarg(int) shmflg;
475 1.14 erh syscallarg(u_long *) raddr;
476 1.42 dsl } */
477 1.1 fvdl int error;
478 1.1 fvdl
479 1.42 dsl if ((error = sys_shmat(l, (const void *)uap, retval)))
480 1.1 fvdl return error;
481 1.1 fvdl
482 1.42 dsl if ((error = copyout(&retval[0], SCARG(uap, raddr), sizeof retval[0])))
483 1.1 fvdl return error;
484 1.31 perry
485 1.1 fvdl retval[0] = 0;
486 1.1 fvdl return 0;
487 1.1 fvdl }
488 1.36 manu #endif /* __amd64__ */
489 1.1 fvdl
490 1.1 fvdl /*
491 1.1 fvdl * Convert between Linux and NetBSD shmid_ds structures.
492 1.1 fvdl * The order of the fields is once again the difference, and
493 1.1 fvdl * we also need a place to store the internal data pointer
494 1.1 fvdl * in, which is unfortunately stored in this structure.
495 1.1 fvdl *
496 1.1 fvdl * We abuse a Linux internal field for that.
497 1.1 fvdl */
498 1.14 erh void
499 1.41 dsl linux_to_bsd_shmid_ds(struct linux_shmid_ds *lsp, struct shmid_ds *bsp)
500 1.1 fvdl {
501 1.8 mycroft
502 1.1 fvdl linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
503 1.1 fvdl bsp->shm_segsz = lsp->l_shm_segsz;
504 1.1 fvdl bsp->shm_lpid = lsp->l_shm_lpid;
505 1.1 fvdl bsp->shm_cpid = lsp->l_shm_cpid;
506 1.1 fvdl bsp->shm_nattch = lsp->l_shm_nattch;
507 1.1 fvdl bsp->shm_atime = lsp->l_shm_atime;
508 1.1 fvdl bsp->shm_dtime = lsp->l_shm_dtime;
509 1.1 fvdl bsp->shm_ctime = lsp->l_shm_ctime;
510 1.21 thorpej bsp->_shm_internal = lsp->l_private2; /* XXX Oh well. */
511 1.1 fvdl }
512 1.1 fvdl
513 1.14 erh void
514 1.41 dsl linux_to_bsd_shmid64_ds(struct linux_shmid64_ds *lsp, struct shmid_ds *bsp)
515 1.32 christos {
516 1.32 christos
517 1.32 christos linux_to_bsd_ipc64_perm(&lsp->l_shm_perm, &bsp->shm_perm);
518 1.32 christos bsp->shm_segsz = lsp->l_shm_segsz;
519 1.32 christos bsp->shm_lpid = lsp->l_shm_lpid;
520 1.32 christos bsp->shm_cpid = lsp->l_shm_cpid;
521 1.32 christos bsp->shm_nattch = lsp->l_shm_nattch;
522 1.32 christos bsp->shm_atime = lsp->l_shm_atime;
523 1.32 christos bsp->shm_dtime = lsp->l_shm_dtime;
524 1.32 christos bsp->shm_ctime = lsp->l_shm_ctime;
525 1.32 christos bsp->_shm_internal = (void*)lsp->l___unused5; /* XXX Oh well. */
526 1.32 christos }
527 1.32 christos
528 1.32 christos void
529 1.41 dsl bsd_to_linux_shmid_ds(struct shmid_ds *bsp, struct linux_shmid_ds *lsp)
530 1.1 fvdl {
531 1.8 mycroft
532 1.1 fvdl bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
533 1.1 fvdl lsp->l_shm_segsz = bsp->shm_segsz;
534 1.1 fvdl lsp->l_shm_lpid = bsp->shm_lpid;
535 1.1 fvdl lsp->l_shm_cpid = bsp->shm_cpid;
536 1.1 fvdl lsp->l_shm_nattch = bsp->shm_nattch;
537 1.1 fvdl lsp->l_shm_atime = bsp->shm_atime;
538 1.1 fvdl lsp->l_shm_dtime = bsp->shm_dtime;
539 1.1 fvdl lsp->l_shm_ctime = bsp->shm_ctime;
540 1.21 thorpej lsp->l_private2 = bsp->_shm_internal; /* XXX */
541 1.1 fvdl }
542 1.1 fvdl
543 1.32 christos void
544 1.41 dsl bsd_to_linux_shmid64_ds(struct shmid_ds *bsp, struct linux_shmid64_ds *lsp)
545 1.32 christos {
546 1.32 christos bsd_to_linux_ipc64_perm(&bsp->shm_perm, &lsp->l_shm_perm);
547 1.32 christos lsp->l_shm_segsz = bsp->shm_segsz;
548 1.32 christos lsp->l_shm_lpid = bsp->shm_lpid;
549 1.32 christos lsp->l_shm_cpid = bsp->shm_cpid;
550 1.32 christos lsp->l_shm_nattch = bsp->shm_nattch;
551 1.32 christos lsp->l_shm_atime = bsp->shm_atime;
552 1.32 christos lsp->l_shm_dtime = bsp->shm_dtime;
553 1.32 christos lsp->l_shm_ctime = bsp->shm_ctime;
554 1.32 christos lsp->l___unused5 = (u_long)bsp->_shm_internal; /* XXX */
555 1.32 christos }
556 1.32 christos
557 1.1 fvdl /*
558 1.46 njoly * shmctl.
559 1.1 fvdl *
560 1.1 fvdl * The usual structure conversion and massaging is done.
561 1.1 fvdl */
562 1.14 erh int
563 1.42 dsl linux_sys_shmctl(struct lwp *l, const struct linux_sys_shmctl_args *uap, register_t *retval)
564 1.1 fvdl {
565 1.42 dsl /* {
566 1.14 erh syscallarg(int) shmid;
567 1.14 erh syscallarg(int) cmd;
568 1.14 erh syscallarg(struct linux_shmid_ds *) buf;
569 1.42 dsl } */
570 1.40 dsl struct shmid_ds bs;
571 1.13 mycroft struct linux_shmid_ds ls;
572 1.32 christos struct linux_shmid64_ds ls64;
573 1.32 christos struct linux_shminfo64 lsi64;
574 1.32 christos struct linux_shm_info lsi;
575 1.43 njoly int error, i, cmd, shmid;
576 1.1 fvdl
577 1.43 njoly shmid = SCARG(uap, shmid);
578 1.32 christos cmd = SCARG(uap, cmd);
579 1.47 njoly #ifdef LINUX_IPC_FORCE64
580 1.48 njoly if (cmd == LINUX_IPC_STAT || cmd == LINUX_SHM_STAT ||
581 1.48 njoly cmd == LINUX_IPC_SET)
582 1.44 njoly cmd |= LINUX_IPC_64;
583 1.44 njoly #endif
584 1.43 njoly
585 1.32 christos switch (cmd) {
586 1.1 fvdl case LINUX_IPC_STAT:
587 1.32 christos case LINUX_SHM_STAT:
588 1.43 njoly if (cmd == LINUX_SHM_STAT) {
589 1.43 njoly shmid = IXSEQ_TO_IPCID(shmid, shmsegs[shmid].shm_perm);
590 1.43 njoly retval[0] = shmid;
591 1.43 njoly }
592 1.43 njoly error = shmctl1(l, shmid, IPC_STAT, &bs);
593 1.40 dsl if (error != 0)
594 1.1 fvdl return error;
595 1.13 mycroft bsd_to_linux_shmid_ds(&bs, &ls);
596 1.14 erh return copyout(&ls, SCARG(uap, buf), sizeof ls);
597 1.32 christos
598 1.40 dsl case LINUX_IPC_STAT | LINUX_IPC_64:
599 1.40 dsl case LINUX_SHM_STAT | LINUX_IPC_64:
600 1.43 njoly if (cmd == (LINUX_SHM_STAT | LINUX_IPC_64)) {
601 1.43 njoly shmid = IXSEQ_TO_IPCID(shmid, shmsegs[shmid].shm_perm);
602 1.43 njoly retval[0] = shmid;
603 1.43 njoly }
604 1.43 njoly error = shmctl1(l, shmid, IPC_STAT, &bs);
605 1.40 dsl if (error != 0)
606 1.32 christos return error;
607 1.32 christos bsd_to_linux_shmid64_ds(&bs, &ls64);
608 1.32 christos return copyout(&ls64, SCARG(uap, buf), sizeof ls64);
609 1.32 christos
610 1.1 fvdl case LINUX_IPC_SET:
611 1.14 erh if ((error = copyin(SCARG(uap, buf), &ls, sizeof ls)))
612 1.1 fvdl return error;
613 1.13 mycroft linux_to_bsd_shmid_ds(&ls, &bs);
614 1.43 njoly return shmctl1(l, shmid, IPC_SET, &bs);
615 1.32 christos
616 1.48 njoly case LINUX_IPC_SET | LINUX_IPC_64:
617 1.48 njoly if ((error = copyin(SCARG(uap, buf), &ls64, sizeof ls64)))
618 1.48 njoly return error;
619 1.48 njoly linux_to_bsd_shmid64_ds(&ls64, &bs);
620 1.48 njoly return shmctl1(l, shmid, IPC_SET, &bs);
621 1.48 njoly
622 1.1 fvdl case LINUX_IPC_RMID:
623 1.43 njoly return shmctl1(l, shmid, IPC_RMID, NULL);
624 1.32 christos
625 1.1 fvdl case LINUX_SHM_LOCK:
626 1.43 njoly return shmctl1(l, shmid, SHM_LOCK, NULL);
627 1.32 christos
628 1.1 fvdl case LINUX_SHM_UNLOCK:
629 1.43 njoly return shmctl1(l, shmid, SHM_UNLOCK, NULL);
630 1.32 christos
631 1.1 fvdl case LINUX_IPC_INFO:
632 1.32 christos memset(&lsi64, 0, sizeof lsi64);
633 1.32 christos lsi64.l_shmmax = shminfo.shmmax;
634 1.32 christos lsi64.l_shmmin = shminfo.shmmin;
635 1.32 christos lsi64.l_shmmni = shminfo.shmmni;
636 1.32 christos lsi64.l_shmseg = shminfo.shmseg;
637 1.32 christos lsi64.l_shmall = shminfo.shmall;
638 1.43 njoly for (i = shminfo.shmmni - 1; i > 0; i--)
639 1.43 njoly if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
640 1.43 njoly break;
641 1.43 njoly retval[0] = i;
642 1.32 christos return copyout(&lsi64, SCARG(uap, buf), sizeof lsi64);
643 1.32 christos
644 1.1 fvdl case LINUX_SHM_INFO:
645 1.32 christos (void)memset(&lsi, 0, sizeof lsi);
646 1.32 christos lsi.l_used_ids = shm_nused;
647 1.32 christos for (i = 0; i < shminfo.shmmni; i++)
648 1.32 christos if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
649 1.43 njoly lsi.l_shm_tot +=
650 1.43 njoly round_page(shmsegs[i].shm_segsz) /
651 1.43 njoly uvmexp.pagesize;
652 1.32 christos lsi.l_shm_rss = 0;
653 1.32 christos lsi.l_shm_swp = 0;
654 1.32 christos lsi.l_swap_attempts = 0;
655 1.32 christos lsi.l_swap_successes = 0;
656 1.43 njoly for (i = shminfo.shmmni - 1; i > 0; i--)
657 1.43 njoly if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
658 1.43 njoly break;
659 1.43 njoly retval[0] = i;
660 1.32 christos return copyout(&lsi, SCARG(uap, buf), sizeof lsi);
661 1.32 christos
662 1.1 fvdl default:
663 1.32 christos #ifdef DEBUG
664 1.32 christos printf("linux_sys_shmctl cmd %d\n", SCARG(uap, cmd));
665 1.32 christos #endif
666 1.1 fvdl return EINVAL;
667 1.1 fvdl }
668 1.1 fvdl }
669 1.1 fvdl #endif /* SYSVSHM */
670