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