sysv_shm.c revision 1.44 1 1.44 kleink /* $NetBSD: sysv_shm.c,v 1.44 1998/05/07 18:00:49 kleink 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.43 mrg
33 1.43 mrg #include "opt_uvm.h"
34 1.11 hpeyerl
35 1.11 hpeyerl #include <sys/types.h>
36 1.11 hpeyerl #include <sys/param.h>
37 1.11 hpeyerl #include <sys/kernel.h>
38 1.11 hpeyerl #include <sys/shm.h>
39 1.11 hpeyerl #include <sys/proc.h>
40 1.11 hpeyerl #include <sys/uio.h>
41 1.11 hpeyerl #include <sys/time.h>
42 1.11 hpeyerl #include <sys/malloc.h>
43 1.11 hpeyerl #include <sys/mman.h>
44 1.11 hpeyerl #include <sys/systm.h>
45 1.12 mycroft #include <sys/stat.h>
46 1.11 hpeyerl
47 1.26 cgd #include <sys/mount.h>
48 1.26 cgd #include <sys/syscallargs.h>
49 1.26 cgd
50 1.11 hpeyerl #include <vm/vm.h>
51 1.42 mrg #ifdef UVM
52 1.42 mrg #include <uvm/uvm_extern.h>
53 1.42 mrg #else
54 1.11 hpeyerl #include <vm/vm_map.h>
55 1.11 hpeyerl #include <vm/vm_kern.h>
56 1.42 mrg #endif
57 1.35 christos
58 1.35 christos struct shmid_ds *shm_find_segment_by_shmid __P((int));
59 1.35 christos
60 1.11 hpeyerl /*
61 1.11 hpeyerl * Provides the following externally accessible functions:
62 1.11 hpeyerl *
63 1.41 thorpej * shminit(void); initialization
64 1.41 thorpej * shmexit(struct vmspace *) cleanup
65 1.41 thorpej * shmfork(struct vmspace *, struct vmspace *) fork handling
66 1.11 hpeyerl * shmsys(arg1, arg2, arg3, arg4); shm{at,ctl,dt,get}(arg2, arg3, arg4)
67 1.11 hpeyerl *
68 1.11 hpeyerl * Structures:
69 1.11 hpeyerl * shmsegs (an array of 'struct shmid_ds')
70 1.11 hpeyerl * per proc array of 'struct shmmap_state'
71 1.11 hpeyerl */
72 1.12 mycroft
73 1.16 mycroft #define SHMSEG_FREE 0x0200
74 1.16 mycroft #define SHMSEG_REMOVED 0x0400
75 1.16 mycroft #define SHMSEG_ALLOCATED 0x0800
76 1.16 mycroft #define SHMSEG_WANTED 0x1000
77 1.11 hpeyerl
78 1.11 hpeyerl int shm_last_free, shm_nused, shm_committed;
79 1.11 hpeyerl
80 1.11 hpeyerl struct shm_handle {
81 1.42 mrg #ifdef UVM
82 1.42 mrg struct uvm_object *shm_object;
83 1.42 mrg #else
84 1.39 drochner vm_object_t shm_object;
85 1.42 mrg #endif
86 1.11 hpeyerl };
87 1.11 hpeyerl
88 1.11 hpeyerl struct shmmap_state {
89 1.11 hpeyerl vm_offset_t va;
90 1.11 hpeyerl int shmid;
91 1.11 hpeyerl };
92 1.11 hpeyerl
93 1.35 christos static int shm_find_segment_by_key __P((key_t));
94 1.12 mycroft static void shm_deallocate_segment __P((struct shmid_ds *));
95 1.41 thorpej static int shm_delete_mapping __P((struct vmspace *, struct shmmap_state *));
96 1.35 christos static int shmget_existing __P((struct proc *, struct sys_shmget_args *,
97 1.35 christos int, int, register_t *));
98 1.35 christos static int shmget_allocate_segment __P((struct proc *, struct sys_shmget_args *,
99 1.35 christos int, register_t *));
100 1.11 hpeyerl
101 1.12 mycroft static int
102 1.12 mycroft shm_find_segment_by_key(key)
103 1.11 hpeyerl key_t key;
104 1.11 hpeyerl {
105 1.11 hpeyerl int i;
106 1.11 hpeyerl
107 1.12 mycroft for (i = 0; i < shminfo.shmmni; i++)
108 1.12 mycroft if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
109 1.12 mycroft shmsegs[i].shm_perm.key == key)
110 1.12 mycroft return i;
111 1.11 hpeyerl return -1;
112 1.11 hpeyerl }
113 1.11 hpeyerl
114 1.28 christos struct shmid_ds *
115 1.15 mycroft shm_find_segment_by_shmid(shmid)
116 1.11 hpeyerl int shmid;
117 1.11 hpeyerl {
118 1.11 hpeyerl int segnum;
119 1.11 hpeyerl struct shmid_ds *shmseg;
120 1.11 hpeyerl
121 1.11 hpeyerl segnum = IPCID_TO_IX(shmid);
122 1.12 mycroft if (segnum < 0 || segnum >= shminfo.shmmni)
123 1.11 hpeyerl return NULL;
124 1.11 hpeyerl shmseg = &shmsegs[segnum];
125 1.12 mycroft if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
126 1.12 mycroft != SHMSEG_ALLOCATED ||
127 1.12 mycroft shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
128 1.11 hpeyerl return NULL;
129 1.11 hpeyerl return shmseg;
130 1.11 hpeyerl }
131 1.11 hpeyerl
132 1.12 mycroft static void
133 1.12 mycroft shm_deallocate_segment(shmseg)
134 1.12 mycroft struct shmid_ds *shmseg;
135 1.12 mycroft {
136 1.12 mycroft struct shm_handle *shm_handle;
137 1.14 mycroft size_t size;
138 1.12 mycroft
139 1.12 mycroft shm_handle = shmseg->shm_internal;
140 1.14 mycroft size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
141 1.42 mrg #ifdef UVM
142 1.42 mrg uao_detach(shm_handle->shm_object);
143 1.42 mrg #else
144 1.39 drochner vm_object_deallocate(shm_handle->shm_object);
145 1.42 mrg #endif
146 1.12 mycroft free((caddr_t)shm_handle, M_SHM);
147 1.12 mycroft shmseg->shm_internal = NULL;
148 1.14 mycroft shm_committed -= btoc(size);
149 1.12 mycroft shmseg->shm_perm.mode = SHMSEG_FREE;
150 1.25 mycroft shm_nused--;
151 1.12 mycroft }
152 1.12 mycroft
153 1.12 mycroft static int
154 1.41 thorpej shm_delete_mapping(vm, shmmap_s)
155 1.41 thorpej struct vmspace *vm;
156 1.11 hpeyerl struct shmmap_state *shmmap_s;
157 1.11 hpeyerl {
158 1.12 mycroft struct shmid_ds *shmseg;
159 1.12 mycroft int segnum, result;
160 1.14 mycroft size_t size;
161 1.11 hpeyerl
162 1.12 mycroft segnum = IPCID_TO_IX(shmmap_s->shmid);
163 1.12 mycroft shmseg = &shmsegs[segnum];
164 1.14 mycroft size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
165 1.42 mrg #ifdef UVM
166 1.42 mrg result = uvm_deallocate(&vm->vm_map,
167 1.42 mrg shmmap_s->va, shmmap_s->va + size);
168 1.42 mrg #else
169 1.41 thorpej result = vm_map_remove(&vm->vm_map,
170 1.40 drochner shmmap_s->va, shmmap_s->va + size);
171 1.42 mrg #endif
172 1.11 hpeyerl if (result != KERN_SUCCESS)
173 1.11 hpeyerl return EINVAL;
174 1.12 mycroft shmmap_s->shmid = -1;
175 1.11 hpeyerl shmseg->shm_dtime = time.tv_sec;
176 1.12 mycroft if ((--shmseg->shm_nattch <= 0) &&
177 1.11 hpeyerl (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
178 1.12 mycroft shm_deallocate_segment(shmseg);
179 1.12 mycroft shm_last_free = segnum;
180 1.11 hpeyerl }
181 1.11 hpeyerl return 0;
182 1.11 hpeyerl }
183 1.11 hpeyerl
184 1.12 mycroft int
185 1.33 mycroft sys_shmdt(p, v, retval)
186 1.11 hpeyerl struct proc *p;
187 1.32 thorpej void *v;
188 1.32 thorpej register_t *retval;
189 1.32 thorpej {
190 1.33 mycroft struct sys_shmdt_args /* {
191 1.44 kleink syscallarg(const void *) shmaddr;
192 1.32 thorpej } */ *uap = v;
193 1.12 mycroft struct shmmap_state *shmmap_s;
194 1.11 hpeyerl int i;
195 1.11 hpeyerl
196 1.12 mycroft shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
197 1.38 christos if (shmmap_s == NULL)
198 1.38 christos return EINVAL;
199 1.38 christos
200 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
201 1.12 mycroft if (shmmap_s->shmid != -1 &&
202 1.26 cgd shmmap_s->va == (vm_offset_t)SCARG(uap, shmaddr))
203 1.12 mycroft break;
204 1.11 hpeyerl if (i == shminfo.shmseg)
205 1.11 hpeyerl return EINVAL;
206 1.41 thorpej return shm_delete_mapping(p->p_vmspace, shmmap_s);
207 1.11 hpeyerl }
208 1.11 hpeyerl
209 1.12 mycroft int
210 1.33 mycroft sys_shmat(p, v, retval)
211 1.11 hpeyerl struct proc *p;
212 1.32 thorpej void *v;
213 1.32 thorpej register_t *retval;
214 1.32 thorpej {
215 1.33 mycroft struct sys_shmat_args /* {
216 1.26 cgd syscallarg(int) shmid;
217 1.44 kleink syscallarg(const void *) shmaddr;
218 1.35 christos syscallarg(int) shmflg;
219 1.32 thorpej } */ *uap = v;
220 1.12 mycroft int error, i, flags;
221 1.12 mycroft struct ucred *cred = p->p_ucred;
222 1.11 hpeyerl struct shmid_ds *shmseg;
223 1.11 hpeyerl struct shmmap_state *shmmap_s = NULL;
224 1.39 drochner struct shm_handle *shm_handle;
225 1.11 hpeyerl vm_offset_t attach_va;
226 1.11 hpeyerl vm_prot_t prot;
227 1.11 hpeyerl vm_size_t size;
228 1.39 drochner int rv;
229 1.11 hpeyerl
230 1.12 mycroft shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
231 1.12 mycroft if (shmmap_s == NULL) {
232 1.12 mycroft size = shminfo.shmseg * sizeof(struct shmmap_state);
233 1.12 mycroft shmmap_s = malloc(size, M_SHM, M_WAITOK);
234 1.18 cgd for (i = 0; i < shminfo.shmseg; i++)
235 1.18 cgd shmmap_s[i].shmid = -1;
236 1.12 mycroft p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
237 1.12 mycroft }
238 1.26 cgd shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
239 1.11 hpeyerl if (shmseg == NULL)
240 1.11 hpeyerl return EINVAL;
241 1.35 christos error = ipcperm(cred, &shmseg->shm_perm,
242 1.35 christos (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
243 1.35 christos if (error)
244 1.12 mycroft return error;
245 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++) {
246 1.12 mycroft if (shmmap_s->shmid == -1)
247 1.12 mycroft break;
248 1.12 mycroft shmmap_s++;
249 1.11 hpeyerl }
250 1.12 mycroft if (i >= shminfo.shmseg)
251 1.12 mycroft return EMFILE;
252 1.14 mycroft size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
253 1.12 mycroft prot = VM_PROT_READ;
254 1.26 cgd if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
255 1.12 mycroft prot |= VM_PROT_WRITE;
256 1.12 mycroft flags = MAP_ANON | MAP_SHARED;
257 1.26 cgd if (SCARG(uap, shmaddr)) {
258 1.12 mycroft flags |= MAP_FIXED;
259 1.26 cgd if (SCARG(uap, shmflg) & SHM_RND)
260 1.26 cgd attach_va =
261 1.26 cgd (vm_offset_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
262 1.26 cgd else if (((vm_offset_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
263 1.26 cgd attach_va = (vm_offset_t)SCARG(uap, shmaddr);
264 1.11 hpeyerl else
265 1.14 mycroft return EINVAL;
266 1.12 mycroft } else {
267 1.20 mycroft /* This is just a hint to vm_mmap() about where to put it. */
268 1.31 cgd attach_va =
269 1.31 cgd round_page(p->p_vmspace->vm_taddr + MAXTSIZ + MAXDSIZ);
270 1.11 hpeyerl }
271 1.39 drochner shm_handle = shmseg->shm_internal;
272 1.42 mrg #ifdef UVM
273 1.42 mrg uao_reference(shm_handle->shm_object);
274 1.42 mrg rv = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
275 1.42 mrg shm_handle->shm_object, 0,
276 1.42 mrg UVM_MAPFLAG(prot, prot, UVM_INH_SHARE,
277 1.42 mrg UVM_ADV_RANDOM, 0));
278 1.42 mrg if (rv != KERN_SUCCESS) {
279 1.42 mrg return ENOMEM;
280 1.42 mrg }
281 1.42 mrg #else
282 1.39 drochner vm_object_reference(shm_handle->shm_object);
283 1.39 drochner rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
284 1.39 drochner 0, &attach_va, size, (flags & MAP_FIXED)?0:1);
285 1.39 drochner if (rv != KERN_SUCCESS) {
286 1.39 drochner return ENOMEM;
287 1.39 drochner }
288 1.39 drochner vm_map_protect(&p->p_vmspace->vm_map, attach_va, attach_va + size,
289 1.39 drochner prot, 0);
290 1.39 drochner vm_map_inherit(&p->p_vmspace->vm_map,
291 1.39 drochner attach_va, attach_va + size, VM_INHERIT_SHARE);
292 1.42 mrg #endif
293 1.39 drochner
294 1.12 mycroft shmmap_s->va = attach_va;
295 1.26 cgd shmmap_s->shmid = SCARG(uap, shmid);
296 1.11 hpeyerl shmseg->shm_lpid = p->p_pid;
297 1.11 hpeyerl shmseg->shm_atime = time.tv_sec;
298 1.11 hpeyerl shmseg->shm_nattch++;
299 1.12 mycroft *retval = attach_va;
300 1.11 hpeyerl return 0;
301 1.11 hpeyerl }
302 1.11 hpeyerl
303 1.12 mycroft int
304 1.33 mycroft sys_shmctl(p, v, retval)
305 1.11 hpeyerl struct proc *p;
306 1.32 thorpej void *v;
307 1.32 thorpej register_t *retval;
308 1.32 thorpej {
309 1.33 mycroft struct sys_shmctl_args /* {
310 1.26 cgd syscallarg(int) shmid;
311 1.26 cgd syscallarg(int) cmd;
312 1.26 cgd syscallarg(struct shmid_ds *) buf;
313 1.32 thorpej } */ *uap = v;
314 1.35 christos int error;
315 1.12 mycroft struct ucred *cred = p->p_ucred;
316 1.11 hpeyerl struct shmid_ds inbuf;
317 1.11 hpeyerl struct shmid_ds *shmseg;
318 1.11 hpeyerl
319 1.26 cgd shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
320 1.11 hpeyerl if (shmseg == NULL)
321 1.11 hpeyerl return EINVAL;
322 1.26 cgd switch (SCARG(uap, cmd)) {
323 1.11 hpeyerl case IPC_STAT:
324 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
325 1.11 hpeyerl return error;
326 1.35 christos error = copyout((caddr_t)shmseg, SCARG(uap, buf),
327 1.35 christos sizeof(inbuf));
328 1.35 christos if (error)
329 1.11 hpeyerl return error;
330 1.11 hpeyerl break;
331 1.11 hpeyerl case IPC_SET:
332 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
333 1.13 mycroft return error;
334 1.35 christos error = copyin(SCARG(uap, buf), (caddr_t)&inbuf,
335 1.35 christos sizeof(inbuf));
336 1.35 christos if (error)
337 1.11 hpeyerl return error;
338 1.11 hpeyerl shmseg->shm_perm.uid = inbuf.shm_perm.uid;
339 1.11 hpeyerl shmseg->shm_perm.gid = inbuf.shm_perm.gid;
340 1.12 mycroft shmseg->shm_perm.mode =
341 1.12 mycroft (shmseg->shm_perm.mode & ~ACCESSPERMS) |
342 1.12 mycroft (inbuf.shm_perm.mode & ACCESSPERMS);
343 1.12 mycroft shmseg->shm_ctime = time.tv_sec;
344 1.11 hpeyerl break;
345 1.11 hpeyerl case IPC_RMID:
346 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
347 1.13 mycroft return error;
348 1.12 mycroft shmseg->shm_perm.key = IPC_PRIVATE;
349 1.12 mycroft shmseg->shm_perm.mode |= SHMSEG_REMOVED;
350 1.12 mycroft if (shmseg->shm_nattch <= 0) {
351 1.12 mycroft shm_deallocate_segment(shmseg);
352 1.26 cgd shm_last_free = IPCID_TO_IX(SCARG(uap, shmid));
353 1.11 hpeyerl }
354 1.11 hpeyerl break;
355 1.11 hpeyerl case SHM_LOCK:
356 1.11 hpeyerl case SHM_UNLOCK:
357 1.11 hpeyerl default:
358 1.11 hpeyerl return EINVAL;
359 1.11 hpeyerl }
360 1.11 hpeyerl return 0;
361 1.11 hpeyerl }
362 1.11 hpeyerl
363 1.12 mycroft static int
364 1.12 mycroft shmget_existing(p, uap, mode, segnum, retval)
365 1.11 hpeyerl struct proc *p;
366 1.33 mycroft struct sys_shmget_args /* {
367 1.26 cgd syscallarg(key_t) key;
368 1.44 kleink syscallarg(size_t) size;
369 1.35 christos syscallarg(int) shmflg;
370 1.26 cgd } */ *uap;
371 1.11 hpeyerl int mode;
372 1.11 hpeyerl int segnum;
373 1.26 cgd register_t *retval;
374 1.11 hpeyerl {
375 1.12 mycroft struct shmid_ds *shmseg;
376 1.12 mycroft struct ucred *cred = p->p_ucred;
377 1.11 hpeyerl int error;
378 1.11 hpeyerl
379 1.11 hpeyerl shmseg = &shmsegs[segnum];
380 1.16 mycroft if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
381 1.16 mycroft /*
382 1.16 mycroft * This segment is in the process of being allocated. Wait
383 1.16 mycroft * until it's done, and look the key up again (in case the
384 1.16 mycroft * allocation failed or it was freed).
385 1.16 mycroft */
386 1.16 mycroft shmseg->shm_perm.mode |= SHMSEG_WANTED;
387 1.35 christos error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
388 1.35 christos if (error)
389 1.16 mycroft return error;
390 1.16 mycroft return EAGAIN;
391 1.16 mycroft }
392 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
393 1.11 hpeyerl return error;
394 1.26 cgd if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
395 1.11 hpeyerl return EINVAL;
396 1.37 christos if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
397 1.26 cgd (IPC_CREAT | IPC_EXCL))
398 1.12 mycroft return EEXIST;
399 1.11 hpeyerl *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
400 1.11 hpeyerl return 0;
401 1.11 hpeyerl }
402 1.11 hpeyerl
403 1.14 mycroft static int
404 1.14 mycroft shmget_allocate_segment(p, uap, mode, retval)
405 1.14 mycroft struct proc *p;
406 1.33 mycroft struct sys_shmget_args /* {
407 1.26 cgd syscallarg(key_t) key;
408 1.44 kleink syscallarg(size_t) size;
409 1.35 christos syscallarg(int) shmflg;
410 1.26 cgd } */ *uap;
411 1.14 mycroft int mode;
412 1.26 cgd register_t *retval;
413 1.14 mycroft {
414 1.39 drochner int i, segnum, shmid, size;
415 1.14 mycroft struct ucred *cred = p->p_ucred;
416 1.14 mycroft struct shmid_ds *shmseg;
417 1.14 mycroft struct shm_handle *shm_handle;
418 1.42 mrg #ifndef UVM
419 1.39 drochner vm_pager_t pager;
420 1.42 mrg #endif
421 1.40 drochner int error = 0;
422 1.14 mycroft
423 1.26 cgd if (SCARG(uap, size) < shminfo.shmmin ||
424 1.26 cgd SCARG(uap, size) > shminfo.shmmax)
425 1.14 mycroft return EINVAL;
426 1.14 mycroft if (shm_nused >= shminfo.shmmni) /* any shmids left? */
427 1.14 mycroft return ENOSPC;
428 1.26 cgd size = (SCARG(uap, size) + CLOFSET) & ~CLOFSET;
429 1.14 mycroft if (shm_committed + btoc(size) > shminfo.shmall)
430 1.14 mycroft return ENOMEM;
431 1.14 mycroft if (shm_last_free < 0) {
432 1.14 mycroft for (i = 0; i < shminfo.shmmni; i++)
433 1.14 mycroft if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
434 1.14 mycroft break;
435 1.14 mycroft if (i == shminfo.shmmni)
436 1.14 mycroft panic("shmseg free count inconsistent");
437 1.14 mycroft segnum = i;
438 1.14 mycroft } else {
439 1.14 mycroft segnum = shm_last_free;
440 1.14 mycroft shm_last_free = -1;
441 1.14 mycroft }
442 1.14 mycroft shmseg = &shmsegs[segnum];
443 1.14 mycroft /*
444 1.14 mycroft * In case we sleep in malloc(), mark the segment present but deleted
445 1.14 mycroft * so that noone else tries to create the same key.
446 1.14 mycroft */
447 1.14 mycroft shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
448 1.26 cgd shmseg->shm_perm.key = SCARG(uap, key);
449 1.14 mycroft shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
450 1.14 mycroft shm_handle = (struct shm_handle *)
451 1.14 mycroft malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
452 1.14 mycroft shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
453 1.39 drochner
454 1.42 mrg
455 1.42 mrg #ifdef UVM
456 1.42 mrg shm_handle->shm_object = uao_create(size, 0);
457 1.42 mrg #else
458 1.39 drochner shm_handle->shm_object = vm_object_allocate(size);
459 1.39 drochner if (shm_handle->shm_object == NULL) {
460 1.40 drochner /* XXX cannot happen */
461 1.40 drochner error = ENOMEM;
462 1.40 drochner goto out;
463 1.39 drochner }
464 1.39 drochner /*
465 1.39 drochner * We make sure that we have allocated a pager before we need
466 1.39 drochner * to.
467 1.39 drochner */
468 1.39 drochner pager = vm_pager_allocate(PG_DFLT, 0, size, VM_PROT_DEFAULT, 0);
469 1.39 drochner if (pager == NULL) {
470 1.40 drochner error = ENOMEM;
471 1.40 drochner goto out;
472 1.14 mycroft }
473 1.39 drochner vm_object_setpager(shm_handle->shm_object, pager, 0, 0);
474 1.42 mrg #endif
475 1.42 mrg
476 1.14 mycroft shmseg->shm_internal = shm_handle;
477 1.14 mycroft shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
478 1.14 mycroft shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
479 1.16 mycroft shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
480 1.16 mycroft (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
481 1.26 cgd shmseg->shm_segsz = SCARG(uap, size);
482 1.14 mycroft shmseg->shm_cpid = p->p_pid;
483 1.14 mycroft shmseg->shm_lpid = shmseg->shm_nattch = 0;
484 1.14 mycroft shmseg->shm_atime = shmseg->shm_dtime = 0;
485 1.14 mycroft shmseg->shm_ctime = time.tv_sec;
486 1.14 mycroft shm_committed += btoc(size);
487 1.14 mycroft shm_nused++;
488 1.40 drochner
489 1.42 mrg #ifndef UVM
490 1.40 drochner out:
491 1.40 drochner if (error) {
492 1.40 drochner if (shm_handle->shm_object != NULL)
493 1.40 drochner vm_object_deallocate(shm_handle->shm_object);
494 1.40 drochner free(shm_handle, M_SHM);
495 1.40 drochner shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED)
496 1.40 drochner | SHMSEG_FREE;
497 1.40 drochner } else
498 1.42 mrg #endif
499 1.40 drochner *retval = shmid;
500 1.16 mycroft if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
501 1.16 mycroft /*
502 1.16 mycroft * Somebody else wanted this key while we were asleep. Wake
503 1.16 mycroft * them up now.
504 1.16 mycroft */
505 1.16 mycroft shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
506 1.16 mycroft wakeup((caddr_t)shmseg);
507 1.16 mycroft }
508 1.40 drochner return error;
509 1.14 mycroft }
510 1.14 mycroft
511 1.12 mycroft int
512 1.33 mycroft sys_shmget(p, v, retval)
513 1.11 hpeyerl struct proc *p;
514 1.32 thorpej void *v;
515 1.32 thorpej register_t *retval;
516 1.32 thorpej {
517 1.33 mycroft struct sys_shmget_args /* {
518 1.26 cgd syscallarg(key_t) key;
519 1.26 cgd syscallarg(int) size;
520 1.35 christos syscallarg(int) shmflg;
521 1.32 thorpej } */ *uap = v;
522 1.11 hpeyerl int segnum, mode, error;
523 1.11 hpeyerl
524 1.26 cgd mode = SCARG(uap, shmflg) & ACCESSPERMS;
525 1.26 cgd if (SCARG(uap, key) != IPC_PRIVATE) {
526 1.16 mycroft again:
527 1.26 cgd segnum = shm_find_segment_by_key(SCARG(uap, key));
528 1.16 mycroft if (segnum >= 0) {
529 1.16 mycroft error = shmget_existing(p, uap, mode, segnum, retval);
530 1.16 mycroft if (error == EAGAIN)
531 1.16 mycroft goto again;
532 1.16 mycroft return error;
533 1.16 mycroft }
534 1.26 cgd if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
535 1.14 mycroft return ENOENT;
536 1.11 hpeyerl }
537 1.14 mycroft return shmget_allocate_segment(p, uap, mode, retval);
538 1.11 hpeyerl }
539 1.11 hpeyerl
540 1.12 mycroft void
541 1.41 thorpej shmfork(vm1, vm2)
542 1.41 thorpej struct vmspace *vm1, *vm2;
543 1.11 hpeyerl {
544 1.11 hpeyerl struct shmmap_state *shmmap_s;
545 1.12 mycroft size_t size;
546 1.11 hpeyerl int i;
547 1.11 hpeyerl
548 1.41 thorpej if (vm1->vm_shm == NULL) {
549 1.41 thorpej vm2->vm_shm = NULL;
550 1.38 christos return;
551 1.38 christos }
552 1.41 thorpej
553 1.12 mycroft size = shminfo.shmseg * sizeof(struct shmmap_state);
554 1.12 mycroft shmmap_s = malloc(size, M_SHM, M_WAITOK);
555 1.41 thorpej bcopy(vm1->vm_shm, shmmap_s, size);
556 1.41 thorpej vm2->vm_shm = (caddr_t)shmmap_s;
557 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
558 1.12 mycroft if (shmmap_s->shmid != -1)
559 1.11 hpeyerl shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
560 1.11 hpeyerl }
561 1.11 hpeyerl
562 1.12 mycroft void
563 1.41 thorpej shmexit(vm)
564 1.41 thorpej struct vmspace *vm;
565 1.11 hpeyerl {
566 1.12 mycroft struct shmmap_state *shmmap_s;
567 1.11 hpeyerl int i;
568 1.11 hpeyerl
569 1.41 thorpej shmmap_s = (struct shmmap_state *)vm->vm_shm;
570 1.38 christos if (shmmap_s == NULL)
571 1.38 christos return;
572 1.12 mycroft for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
573 1.12 mycroft if (shmmap_s->shmid != -1)
574 1.41 thorpej shm_delete_mapping(vm, shmmap_s);
575 1.41 thorpej free(vm->vm_shm, M_SHM);
576 1.41 thorpej vm->vm_shm = NULL;
577 1.11 hpeyerl }
578 1.11 hpeyerl
579 1.12 mycroft void
580 1.12 mycroft shminit()
581 1.11 hpeyerl {
582 1.11 hpeyerl int i;
583 1.24 deraadt
584 1.24 deraadt shminfo.shmmax *= NBPG;
585 1.11 hpeyerl
586 1.11 hpeyerl for (i = 0; i < shminfo.shmmni; i++) {
587 1.11 hpeyerl shmsegs[i].shm_perm.mode = SHMSEG_FREE;
588 1.11 hpeyerl shmsegs[i].shm_perm.seq = 0;
589 1.11 hpeyerl }
590 1.11 hpeyerl shm_last_free = 0;
591 1.11 hpeyerl shm_nused = 0;
592 1.11 hpeyerl shm_committed = 0;
593 1.11 hpeyerl }
594