sysv_shm.c revision 1.28 1 1.28 christos /* $NetBSD: sysv_shm.c,v 1.28 1995/05/10 16:53:00 christos Exp $ */
2 1.22 cgd
3 1.11 hpeyerl /*
4 1.17 mycroft * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
5 1.11 hpeyerl *
6 1.11 hpeyerl * Redistribution and use in source and binary forms, with or without
7 1.11 hpeyerl * modification, are permitted provided that the following conditions
8 1.11 hpeyerl * are met:
9 1.11 hpeyerl * 1. Redistributions of source code must retain the above copyright
10 1.11 hpeyerl * notice, this list of conditions and the following disclaimer.
11 1.17 mycroft * 2. Redistributions in binary form must reproduce the above copyright
12 1.17 mycroft * notice, this list of conditions and the following disclaimer in the
13 1.17 mycroft * documentation and/or other materials provided with the distribution.
14 1.17 mycroft * 3. All advertising materials mentioning features or use of this software
15 1.17 mycroft * must display the following acknowledgement:
16 1.17 mycroft * This product includes software developed by Adam Glass and Charles
17 1.17 mycroft * Hannum.
18 1.17 mycroft * 4. The names of the authors may not be used to endorse or promote products
19 1.11 hpeyerl * derived from this software without specific prior written permission.
20 1.11 hpeyerl *
21 1.17 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22 1.17 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.17 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.17 mycroft * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.17 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.17 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.17 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.17 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.17 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.17 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.11 hpeyerl */
32 1.11 hpeyerl
33 1.11 hpeyerl #include <sys/types.h>
34 1.11 hpeyerl #include <sys/param.h>
35 1.11 hpeyerl #include <sys/kernel.h>
36 1.11 hpeyerl #include <sys/shm.h>
37 1.11 hpeyerl #include <sys/proc.h>
38 1.11 hpeyerl #include <sys/uio.h>
39 1.11 hpeyerl #include <sys/time.h>
40 1.11 hpeyerl #include <sys/malloc.h>
41 1.11 hpeyerl #include <sys/mman.h>
42 1.11 hpeyerl #include <sys/systm.h>
43 1.12 mycroft #include <sys/stat.h>
44 1.11 hpeyerl
45 1.26 cgd #include <sys/mount.h>
46 1.26 cgd #include <sys/syscallargs.h>
47 1.26 cgd
48 1.11 hpeyerl #include <vm/vm.h>
49 1.11 hpeyerl #include <vm/vm_map.h>
50 1.11 hpeyerl #include <vm/vm_map.h>
51 1.11 hpeyerl #include <vm/vm_kern.h>
52 1.11 hpeyerl
53 1.11 hpeyerl /*
54 1.11 hpeyerl * Provides the following externally accessible functions:
55 1.11 hpeyerl *
56 1.11 hpeyerl * shminit(void); initialization
57 1.11 hpeyerl * shmexit(struct proc *) cleanup
58 1.11 hpeyerl * shmfork(struct proc *, struct proc *, int) fork handling
59 1.11 hpeyerl * shmsys(arg1, arg2, arg3, arg4); shm{at,ctl,dt,get}(arg2, arg3, arg4)
60 1.11 hpeyerl *
61 1.11 hpeyerl * Structures:
62 1.11 hpeyerl * shmsegs (an array of 'struct shmid_ds')
63 1.11 hpeyerl * per proc array of 'struct shmmap_state'
64 1.11 hpeyerl */
65 1.12 mycroft
66 1.16 mycroft #define SHMSEG_FREE 0x0200
67 1.16 mycroft #define SHMSEG_REMOVED 0x0400
68 1.16 mycroft #define SHMSEG_ALLOCATED 0x0800
69 1.16 mycroft #define SHMSEG_WANTED 0x1000
70 1.11 hpeyerl
71 1.11 hpeyerl vm_map_t sysvshm_map;
72 1.11 hpeyerl int shm_last_free, shm_nused, shm_committed;
73 1.11 hpeyerl
74 1.11 hpeyerl struct shm_handle {
75 1.11 hpeyerl vm_offset_t kva;
76 1.11 hpeyerl };
77 1.11 hpeyerl
78 1.11 hpeyerl struct shmmap_state {
79 1.11 hpeyerl vm_offset_t va;
80 1.11 hpeyerl int shmid;
81 1.11 hpeyerl };
82 1.11 hpeyerl
83 1.12 mycroft static void shm_deallocate_segment __P((struct shmid_ds *));
84 1.12 mycroft static int shm_find_segment_by_key __P((key_t));
85 1.28 christos struct shmid_ds *shm_find_segment_by_shmid __P((int));
86 1.12 mycroft static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
87 1.11 hpeyerl
88 1.12 mycroft static int
89 1.12 mycroft shm_find_segment_by_key(key)
90 1.11 hpeyerl key_t key;
91 1.11 hpeyerl {
92 1.11 hpeyerl int i;
93 1.11 hpeyerl
94 1.12 mycroft for (i = 0; i < shminfo.shmmni; i++)
95 1.12 mycroft if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
96 1.12 mycroft shmsegs[i].shm_perm.key == key)
97 1.12 mycroft return i;
98 1.11 hpeyerl return -1;
99 1.11 hpeyerl }
100 1.11 hpeyerl
101 1.28 christos struct shmid_ds *
102 1.15 mycroft shm_find_segment_by_shmid(shmid)
103 1.11 hpeyerl int shmid;
104 1.11 hpeyerl {
105 1.11 hpeyerl int segnum;
106 1.11 hpeyerl struct shmid_ds *shmseg;
107 1.11 hpeyerl
108 1.11 hpeyerl segnum = IPCID_TO_IX(shmid);
109 1.12 mycroft if (segnum < 0 || segnum >= shminfo.shmmni)
110 1.11 hpeyerl return NULL;
111 1.11 hpeyerl shmseg = &shmsegs[segnum];
112 1.12 mycroft if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
113 1.12 mycroft != SHMSEG_ALLOCATED ||
114 1.12 mycroft shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
115 1.11 hpeyerl return NULL;
116 1.11 hpeyerl return shmseg;
117 1.11 hpeyerl }
118 1.11 hpeyerl
119 1.12 mycroft static void
120 1.12 mycroft shm_deallocate_segment(shmseg)
121 1.12 mycroft struct shmid_ds *shmseg;
122 1.12 mycroft {
123 1.12 mycroft struct shm_handle *shm_handle;
124 1.14 mycroft size_t size;
125 1.12 mycroft
126 1.12 mycroft shm_handle = shmseg->shm_internal;
127 1.14 mycroft size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
128 1.14 mycroft vm_deallocate(sysvshm_map, shm_handle->kva, size);
129 1.12 mycroft free((caddr_t)shm_handle, M_SHM);
130 1.12 mycroft shmseg->shm_internal = NULL;
131 1.14 mycroft shm_committed -= btoc(size);
132 1.12 mycroft shmseg->shm_perm.mode = SHMSEG_FREE;
133 1.25 mycroft shm_nused--;
134 1.12 mycroft }
135 1.12 mycroft
136 1.12 mycroft static int
137 1.12 mycroft shm_delete_mapping(p, shmmap_s)
138 1.11 hpeyerl struct proc *p;
139 1.11 hpeyerl struct shmmap_state *shmmap_s;
140 1.11 hpeyerl {
141 1.12 mycroft struct shmid_ds *shmseg;
142 1.12 mycroft int segnum, result;
143 1.14 mycroft size_t size;
144 1.11 hpeyerl
145 1.12 mycroft segnum = IPCID_TO_IX(shmmap_s->shmid);
146 1.12 mycroft shmseg = &shmsegs[segnum];
147 1.14 mycroft size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
148 1.14 mycroft result = vm_deallocate(&p->p_vmspace->vm_map, shmmap_s->va, size);
149 1.11 hpeyerl if (result != KERN_SUCCESS)
150 1.11 hpeyerl return EINVAL;
151 1.12 mycroft shmmap_s->shmid = -1;
152 1.11 hpeyerl shmseg->shm_dtime = time.tv_sec;
153 1.12 mycroft if ((--shmseg->shm_nattch <= 0) &&
154 1.11 hpeyerl (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
155 1.12 mycroft shm_deallocate_segment(shmseg);
156 1.12 mycroft shm_last_free = segnum;
157 1.11 hpeyerl }
158 1.11 hpeyerl return 0;
159 1.11 hpeyerl }
160 1.11 hpeyerl
161 1.12 mycroft int
162 1.12 mycroft shmdt(p, uap, retval)
163 1.11 hpeyerl struct proc *p;
164 1.26 cgd struct shmdt_args /* {
165 1.26 cgd syscallarg(void *) shmaddr;
166 1.26 cgd } */ *uap;
167 1.26 cgd register_t *retval;
168 1.11 hpeyerl {
169 1.12 mycroft struct shmmap_state *shmmap_s;
170 1.11 hpeyerl int i;
171 1.11 hpeyerl
172 1.12 mycroft shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
173 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
174 1.12 mycroft if (shmmap_s->shmid != -1 &&
175 1.26 cgd shmmap_s->va == (vm_offset_t)SCARG(uap, shmaddr))
176 1.12 mycroft break;
177 1.11 hpeyerl if (i == shminfo.shmseg)
178 1.11 hpeyerl return EINVAL;
179 1.12 mycroft return shm_delete_mapping(p, shmmap_s);
180 1.11 hpeyerl }
181 1.11 hpeyerl
182 1.12 mycroft int
183 1.12 mycroft shmat(p, uap, retval)
184 1.11 hpeyerl struct proc *p;
185 1.26 cgd struct shmat_args /* {
186 1.26 cgd syscallarg(int) shmid;
187 1.26 cgd syscallarg(void *) shmaddr;
188 1.26 cgd syscallarg(int) shmflag;
189 1.26 cgd } */ *uap;
190 1.26 cgd register_t *retval;
191 1.11 hpeyerl {
192 1.12 mycroft int error, i, flags;
193 1.12 mycroft struct ucred *cred = p->p_ucred;
194 1.11 hpeyerl struct shmid_ds *shmseg;
195 1.11 hpeyerl struct shmmap_state *shmmap_s = NULL;
196 1.11 hpeyerl vm_offset_t attach_va;
197 1.11 hpeyerl vm_prot_t prot;
198 1.11 hpeyerl vm_size_t size;
199 1.11 hpeyerl
200 1.12 mycroft shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
201 1.12 mycroft if (shmmap_s == NULL) {
202 1.12 mycroft size = shminfo.shmseg * sizeof(struct shmmap_state);
203 1.12 mycroft shmmap_s = malloc(size, M_SHM, M_WAITOK);
204 1.18 cgd for (i = 0; i < shminfo.shmseg; i++)
205 1.18 cgd shmmap_s[i].shmid = -1;
206 1.12 mycroft p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
207 1.12 mycroft }
208 1.26 cgd shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
209 1.11 hpeyerl if (shmseg == NULL)
210 1.11 hpeyerl return EINVAL;
211 1.12 mycroft if (error = ipcperm(cred, &shmseg->shm_perm,
212 1.26 cgd (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W))
213 1.12 mycroft return error;
214 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++) {
215 1.12 mycroft if (shmmap_s->shmid == -1)
216 1.12 mycroft break;
217 1.12 mycroft shmmap_s++;
218 1.11 hpeyerl }
219 1.12 mycroft if (i >= shminfo.shmseg)
220 1.12 mycroft return EMFILE;
221 1.14 mycroft size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
222 1.12 mycroft prot = VM_PROT_READ;
223 1.26 cgd if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
224 1.12 mycroft prot |= VM_PROT_WRITE;
225 1.12 mycroft flags = MAP_ANON | MAP_SHARED;
226 1.26 cgd if (SCARG(uap, shmaddr)) {
227 1.12 mycroft flags |= MAP_FIXED;
228 1.26 cgd if (SCARG(uap, shmflg) & SHM_RND)
229 1.26 cgd attach_va =
230 1.26 cgd (vm_offset_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
231 1.26 cgd else if (((vm_offset_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
232 1.26 cgd attach_va = (vm_offset_t)SCARG(uap, shmaddr);
233 1.11 hpeyerl else
234 1.14 mycroft return EINVAL;
235 1.12 mycroft } else {
236 1.20 mycroft /* This is just a hint to vm_mmap() about where to put it. */
237 1.20 mycroft attach_va = round_page(p->p_vmspace->vm_daddr + MAXDSIZ);
238 1.11 hpeyerl }
239 1.12 mycroft error = vm_mmap(&p->p_vmspace->vm_map, &attach_va, size, prot,
240 1.26 cgd VM_PROT_DEFAULT, flags, (caddr_t)(long)SCARG(uap, shmid), 0);
241 1.11 hpeyerl if (error)
242 1.11 hpeyerl return error;
243 1.12 mycroft shmmap_s->va = attach_va;
244 1.26 cgd shmmap_s->shmid = SCARG(uap, shmid);
245 1.11 hpeyerl shmseg->shm_lpid = p->p_pid;
246 1.11 hpeyerl shmseg->shm_atime = time.tv_sec;
247 1.11 hpeyerl shmseg->shm_nattch++;
248 1.12 mycroft *retval = attach_va;
249 1.11 hpeyerl return 0;
250 1.11 hpeyerl }
251 1.11 hpeyerl
252 1.12 mycroft int
253 1.12 mycroft shmctl(p, uap, retval)
254 1.11 hpeyerl struct proc *p;
255 1.26 cgd struct shmctl_args /* {
256 1.26 cgd syscallarg(int) shmid;
257 1.26 cgd syscallarg(int) cmd;
258 1.26 cgd syscallarg(struct shmid_ds *) buf;
259 1.26 cgd } */ *uap;
260 1.26 cgd register_t *retval;
261 1.11 hpeyerl {
262 1.11 hpeyerl int error, segnum;
263 1.12 mycroft struct ucred *cred = p->p_ucred;
264 1.11 hpeyerl struct shmid_ds inbuf;
265 1.11 hpeyerl struct shmid_ds *shmseg;
266 1.11 hpeyerl
267 1.26 cgd shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
268 1.11 hpeyerl if (shmseg == NULL)
269 1.11 hpeyerl return EINVAL;
270 1.26 cgd switch (SCARG(uap, cmd)) {
271 1.11 hpeyerl case IPC_STAT:
272 1.12 mycroft if (error = ipcperm(cred, &shmseg->shm_perm, IPC_R))
273 1.11 hpeyerl return error;
274 1.26 cgd if (error = copyout((caddr_t)shmseg, SCARG(uap, buf),
275 1.26 cgd sizeof(inbuf)))
276 1.11 hpeyerl return error;
277 1.11 hpeyerl break;
278 1.11 hpeyerl case IPC_SET:
279 1.13 mycroft if (error = ipcperm(cred, &shmseg->shm_perm, IPC_M))
280 1.13 mycroft return error;
281 1.26 cgd if (error = copyin(SCARG(uap, buf), (caddr_t)&inbuf,
282 1.26 cgd sizeof(inbuf)))
283 1.11 hpeyerl return error;
284 1.11 hpeyerl shmseg->shm_perm.uid = inbuf.shm_perm.uid;
285 1.11 hpeyerl shmseg->shm_perm.gid = inbuf.shm_perm.gid;
286 1.12 mycroft shmseg->shm_perm.mode =
287 1.12 mycroft (shmseg->shm_perm.mode & ~ACCESSPERMS) |
288 1.12 mycroft (inbuf.shm_perm.mode & ACCESSPERMS);
289 1.12 mycroft shmseg->shm_ctime = time.tv_sec;
290 1.11 hpeyerl break;
291 1.11 hpeyerl case IPC_RMID:
292 1.13 mycroft if (error = ipcperm(cred, &shmseg->shm_perm, IPC_M))
293 1.13 mycroft return error;
294 1.12 mycroft shmseg->shm_perm.key = IPC_PRIVATE;
295 1.12 mycroft shmseg->shm_perm.mode |= SHMSEG_REMOVED;
296 1.12 mycroft if (shmseg->shm_nattch <= 0) {
297 1.12 mycroft shm_deallocate_segment(shmseg);
298 1.26 cgd shm_last_free = IPCID_TO_IX(SCARG(uap, shmid));
299 1.11 hpeyerl }
300 1.11 hpeyerl break;
301 1.11 hpeyerl case SHM_LOCK:
302 1.11 hpeyerl case SHM_UNLOCK:
303 1.11 hpeyerl default:
304 1.11 hpeyerl return EINVAL;
305 1.11 hpeyerl }
306 1.11 hpeyerl return 0;
307 1.11 hpeyerl }
308 1.11 hpeyerl
309 1.12 mycroft static int
310 1.12 mycroft shmget_existing(p, uap, mode, segnum, retval)
311 1.11 hpeyerl struct proc *p;
312 1.26 cgd struct shmget_args /* {
313 1.26 cgd syscallarg(key_t) key;
314 1.26 cgd syscallarg(int) size;
315 1.26 cgd syscallarg(int) shmflag;
316 1.26 cgd } */ *uap;
317 1.11 hpeyerl int mode;
318 1.11 hpeyerl int segnum;
319 1.26 cgd register_t *retval;
320 1.11 hpeyerl {
321 1.12 mycroft struct shmid_ds *shmseg;
322 1.12 mycroft struct ucred *cred = p->p_ucred;
323 1.11 hpeyerl int error;
324 1.11 hpeyerl
325 1.11 hpeyerl shmseg = &shmsegs[segnum];
326 1.16 mycroft if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
327 1.16 mycroft /*
328 1.16 mycroft * This segment is in the process of being allocated. Wait
329 1.16 mycroft * until it's done, and look the key up again (in case the
330 1.16 mycroft * allocation failed or it was freed).
331 1.16 mycroft */
332 1.16 mycroft shmseg->shm_perm.mode |= SHMSEG_WANTED;
333 1.16 mycroft if (error =
334 1.16 mycroft tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0))
335 1.16 mycroft return error;
336 1.16 mycroft return EAGAIN;
337 1.16 mycroft }
338 1.12 mycroft if (error = ipcperm(cred, &shmseg->shm_perm, mode))
339 1.11 hpeyerl return error;
340 1.26 cgd if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
341 1.11 hpeyerl return EINVAL;
342 1.26 cgd if (SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL) ==
343 1.26 cgd (IPC_CREAT | IPC_EXCL))
344 1.12 mycroft return EEXIST;
345 1.11 hpeyerl *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
346 1.11 hpeyerl return 0;
347 1.11 hpeyerl }
348 1.11 hpeyerl
349 1.14 mycroft static int
350 1.14 mycroft shmget_allocate_segment(p, uap, mode, retval)
351 1.14 mycroft struct proc *p;
352 1.26 cgd struct shmget_args /* {
353 1.26 cgd syscallarg(key_t) key;
354 1.26 cgd syscallarg(int) size;
355 1.26 cgd syscallarg(int) shmflag;
356 1.26 cgd } */ *uap;
357 1.14 mycroft int mode;
358 1.26 cgd register_t *retval;
359 1.14 mycroft {
360 1.14 mycroft int i, segnum, result, shmid, size;
361 1.14 mycroft struct ucred *cred = p->p_ucred;
362 1.14 mycroft struct shmid_ds *shmseg;
363 1.14 mycroft struct shm_handle *shm_handle;
364 1.14 mycroft
365 1.26 cgd if (SCARG(uap, size) < shminfo.shmmin ||
366 1.26 cgd SCARG(uap, size) > shminfo.shmmax)
367 1.14 mycroft return EINVAL;
368 1.14 mycroft if (shm_nused >= shminfo.shmmni) /* any shmids left? */
369 1.14 mycroft return ENOSPC;
370 1.26 cgd size = (SCARG(uap, size) + CLOFSET) & ~CLOFSET;
371 1.14 mycroft if (shm_committed + btoc(size) > shminfo.shmall)
372 1.14 mycroft return ENOMEM;
373 1.14 mycroft if (shm_last_free < 0) {
374 1.14 mycroft for (i = 0; i < shminfo.shmmni; i++)
375 1.14 mycroft if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
376 1.14 mycroft break;
377 1.14 mycroft if (i == shminfo.shmmni)
378 1.14 mycroft panic("shmseg free count inconsistent");
379 1.14 mycroft segnum = i;
380 1.14 mycroft } else {
381 1.14 mycroft segnum = shm_last_free;
382 1.14 mycroft shm_last_free = -1;
383 1.14 mycroft }
384 1.14 mycroft shmseg = &shmsegs[segnum];
385 1.14 mycroft /*
386 1.14 mycroft * In case we sleep in malloc(), mark the segment present but deleted
387 1.14 mycroft * so that noone else tries to create the same key.
388 1.14 mycroft */
389 1.14 mycroft shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
390 1.26 cgd shmseg->shm_perm.key = SCARG(uap, key);
391 1.14 mycroft shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
392 1.14 mycroft shm_handle = (struct shm_handle *)
393 1.14 mycroft malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
394 1.14 mycroft shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
395 1.14 mycroft result = vm_mmap(sysvshm_map, &shm_handle->kva, size, VM_PROT_ALL,
396 1.26 cgd VM_PROT_DEFAULT, MAP_ANON, (caddr_t)(long)shmid, 0);
397 1.14 mycroft if (result != KERN_SUCCESS) {
398 1.14 mycroft shmseg->shm_perm.mode = SHMSEG_FREE;
399 1.15 mycroft shm_last_free = segnum;
400 1.14 mycroft free((caddr_t)shm_handle, M_SHM);
401 1.16 mycroft /* Just in case. */
402 1.16 mycroft wakeup((caddr_t)shmseg);
403 1.14 mycroft return ENOMEM;
404 1.14 mycroft }
405 1.14 mycroft shmseg->shm_internal = shm_handle;
406 1.14 mycroft shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
407 1.14 mycroft shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
408 1.16 mycroft shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
409 1.16 mycroft (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
410 1.26 cgd shmseg->shm_segsz = SCARG(uap, size);
411 1.14 mycroft shmseg->shm_cpid = p->p_pid;
412 1.14 mycroft shmseg->shm_lpid = shmseg->shm_nattch = 0;
413 1.14 mycroft shmseg->shm_atime = shmseg->shm_dtime = 0;
414 1.14 mycroft shmseg->shm_ctime = time.tv_sec;
415 1.14 mycroft shm_committed += btoc(size);
416 1.14 mycroft shm_nused++;
417 1.16 mycroft if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
418 1.16 mycroft /*
419 1.16 mycroft * Somebody else wanted this key while we were asleep. Wake
420 1.16 mycroft * them up now.
421 1.16 mycroft */
422 1.16 mycroft shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
423 1.16 mycroft wakeup((caddr_t)shmseg);
424 1.16 mycroft }
425 1.14 mycroft *retval = shmid;
426 1.14 mycroft return 0;
427 1.14 mycroft }
428 1.14 mycroft
429 1.12 mycroft int
430 1.12 mycroft shmget(p, uap, retval)
431 1.11 hpeyerl struct proc *p;
432 1.26 cgd struct shmget_args /* {
433 1.26 cgd syscallarg(key_t) key;
434 1.26 cgd syscallarg(int) size;
435 1.26 cgd syscallarg(int) shmflag;
436 1.26 cgd } */ *uap;
437 1.26 cgd register_t *retval;
438 1.11 hpeyerl {
439 1.11 hpeyerl int segnum, mode, error;
440 1.11 hpeyerl struct shmid_ds *shmseg;
441 1.11 hpeyerl
442 1.26 cgd mode = SCARG(uap, shmflg) & ACCESSPERMS;
443 1.26 cgd if (SCARG(uap, key) != IPC_PRIVATE) {
444 1.16 mycroft again:
445 1.26 cgd segnum = shm_find_segment_by_key(SCARG(uap, key));
446 1.16 mycroft if (segnum >= 0) {
447 1.16 mycroft error = shmget_existing(p, uap, mode, segnum, retval);
448 1.16 mycroft if (error == EAGAIN)
449 1.16 mycroft goto again;
450 1.16 mycroft return error;
451 1.16 mycroft }
452 1.26 cgd if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
453 1.14 mycroft return ENOENT;
454 1.11 hpeyerl }
455 1.14 mycroft return shmget_allocate_segment(p, uap, mode, retval);
456 1.11 hpeyerl }
457 1.11 hpeyerl
458 1.12 mycroft void
459 1.12 mycroft shmfork(p1, p2, isvfork)
460 1.12 mycroft struct proc *p1, *p2;
461 1.12 mycroft int isvfork;
462 1.11 hpeyerl {
463 1.11 hpeyerl struct shmmap_state *shmmap_s;
464 1.12 mycroft size_t size;
465 1.11 hpeyerl int i;
466 1.11 hpeyerl
467 1.12 mycroft size = shminfo.shmseg * sizeof(struct shmmap_state);
468 1.12 mycroft shmmap_s = malloc(size, M_SHM, M_WAITOK);
469 1.12 mycroft bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
470 1.12 mycroft p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
471 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
472 1.12 mycroft if (shmmap_s->shmid != -1)
473 1.11 hpeyerl shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
474 1.11 hpeyerl }
475 1.11 hpeyerl
476 1.12 mycroft void
477 1.12 mycroft shmexit(p)
478 1.11 hpeyerl struct proc *p;
479 1.11 hpeyerl {
480 1.12 mycroft struct shmmap_state *shmmap_s;
481 1.12 mycroft struct shmid_ds *shmseg;
482 1.11 hpeyerl int i;
483 1.11 hpeyerl
484 1.11 hpeyerl shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
485 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
486 1.12 mycroft if (shmmap_s->shmid != -1)
487 1.12 mycroft shm_delete_mapping(p, shmmap_s);
488 1.12 mycroft free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
489 1.11 hpeyerl p->p_vmspace->vm_shm = NULL;
490 1.11 hpeyerl }
491 1.11 hpeyerl
492 1.12 mycroft void
493 1.12 mycroft shminit()
494 1.11 hpeyerl {
495 1.11 hpeyerl int i;
496 1.11 hpeyerl vm_offset_t garbage1, garbage2;
497 1.24 deraadt
498 1.24 deraadt shminfo.shmmax *= NBPG;
499 1.11 hpeyerl
500 1.11 hpeyerl /* actually this *should* be pageable. SHM_{LOCK,UNLOCK} */
501 1.11 hpeyerl sysvshm_map = kmem_suballoc(kernel_map, &garbage1, &garbage2,
502 1.21 mycroft shminfo.shmall * NBPG, TRUE);
503 1.11 hpeyerl for (i = 0; i < shminfo.shmmni; i++) {
504 1.11 hpeyerl shmsegs[i].shm_perm.mode = SHMSEG_FREE;
505 1.11 hpeyerl shmsegs[i].shm_perm.seq = 0;
506 1.11 hpeyerl }
507 1.11 hpeyerl shm_last_free = 0;
508 1.11 hpeyerl shm_nused = 0;
509 1.11 hpeyerl shm_committed = 0;
510 1.11 hpeyerl }
511 1.26 cgd
512 1.26 cgd #if defined(COMPAT_10) && !defined(alpha)
513 1.26 cgd int
514 1.26 cgd compat_10_shmsys(p, uap, retval)
515 1.26 cgd struct proc *p;
516 1.26 cgd struct compat_10_shmsys_args /* {
517 1.26 cgd syscallarg(int) which;
518 1.26 cgd syscallarg(int) a2;
519 1.26 cgd syscallarg(int) a3;
520 1.26 cgd syscallarg(int) a4;
521 1.26 cgd } */ *uap;
522 1.26 cgd register_t *retval;
523 1.26 cgd {
524 1.26 cgd struct shmat_args /* {
525 1.26 cgd syscallarg(int) shmid;
526 1.26 cgd syscallarg(void *) shmaddr;
527 1.26 cgd syscallarg(int) shmflg;
528 1.26 cgd } */ shmat_args;
529 1.26 cgd struct shmctl_args /* {
530 1.26 cgd syscallarg(int) shmid;
531 1.26 cgd syscallarg(int) cmd;
532 1.26 cgd syscallarg(struct shmid_ds *) buf;
533 1.26 cgd } */ shmctl_args;
534 1.26 cgd struct shmdt_args /* {
535 1.26 cgd syscallarg(void *) shmaddr;
536 1.26 cgd } */ shmdt_args;
537 1.26 cgd struct shmget_args /* {
538 1.26 cgd syscallarg(key_t) key;
539 1.26 cgd syscallarg(int) size;
540 1.26 cgd syscallarg(int) shmflg;
541 1.26 cgd } */ shmget_args;
542 1.26 cgd
543 1.26 cgd switch (SCARG(uap, which)) {
544 1.26 cgd case 0: /* shmat() */
545 1.26 cgd SCARG(&shmat_args, shmid) = SCARG(uap, a2);
546 1.26 cgd SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a3);
547 1.26 cgd SCARG(&shmat_args, shmflg) = SCARG(uap, a4);
548 1.26 cgd return (shmat(p, &shmat_args, retval));
549 1.26 cgd
550 1.26 cgd case 1: /* shmctl() */
551 1.26 cgd SCARG(&shmctl_args, shmid) = SCARG(uap, a2);
552 1.26 cgd SCARG(&shmctl_args, cmd) = SCARG(uap, a3);
553 1.26 cgd SCARG(&shmctl_args, buf) = (struct shmid_ds *)SCARG(uap, a4);
554 1.26 cgd return (shmctl(p, &shmctl_args, retval));
555 1.26 cgd
556 1.26 cgd case 2: /* shmdt() */
557 1.26 cgd SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a2);
558 1.26 cgd return (shmdt(p, &shmdt_args, retval));
559 1.26 cgd
560 1.26 cgd case 3: /* shmget() */
561 1.26 cgd SCARG(&shmget_args, key) = SCARG(uap, a2);
562 1.26 cgd SCARG(&shmget_args, size) = SCARG(uap, a3);
563 1.26 cgd SCARG(&shmget_args, shmflg) = SCARG(uap, a4);
564 1.26 cgd return (shmget(p, &shmget_args, retval));
565 1.26 cgd
566 1.26 cgd default:
567 1.26 cgd return (EINVAL);
568 1.26 cgd }
569 1.26 cgd }
570 1.26 cgd #endif /* defined(COMPAT_10) && !defined(alpha) */
571