uvm_mmap.c revision 1.49 1 1.49 chs /* $NetBSD: uvm_mmap.c,v 1.49 2001/02/18 21:19:08 chs Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.1 mrg * Copyright (c) 1991, 1993 The Regents of the University of California.
6 1.1 mrg * Copyright (c) 1988 University of Utah.
7 1.1 mrg *
8 1.1 mrg * All rights reserved.
9 1.1 mrg *
10 1.1 mrg * This code is derived from software contributed to Berkeley by
11 1.1 mrg * the Systems Programming Group of the University of Utah Computer
12 1.1 mrg * Science Department.
13 1.1 mrg *
14 1.1 mrg * Redistribution and use in source and binary forms, with or without
15 1.1 mrg * modification, are permitted provided that the following conditions
16 1.1 mrg * are met:
17 1.1 mrg * 1. Redistributions of source code must retain the above copyright
18 1.1 mrg * notice, this list of conditions and the following disclaimer.
19 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 mrg * notice, this list of conditions and the following disclaimer in the
21 1.1 mrg * documentation and/or other materials provided with the distribution.
22 1.1 mrg * 3. All advertising materials mentioning features or use of this software
23 1.1 mrg * must display the following acknowledgement:
24 1.1 mrg * This product includes software developed by the Charles D. Cranor,
25 1.1 mrg * Washington University, University of California, Berkeley and
26 1.1 mrg * its contributors.
27 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
28 1.1 mrg * may be used to endorse or promote products derived from this software
29 1.1 mrg * without specific prior written permission.
30 1.1 mrg *
31 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 1.1 mrg * SUCH DAMAGE.
42 1.1 mrg *
43 1.1 mrg * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
44 1.1 mrg * @(#)vm_mmap.c 8.5 (Berkeley) 5/19/94
45 1.3 mrg * from: Id: uvm_mmap.c,v 1.1.2.14 1998/01/05 21:04:26 chuck Exp
46 1.1 mrg */
47 1.1 mrg
48 1.1 mrg /*
49 1.1 mrg * uvm_mmap.c: system call interface into VM system, plus kernel vm_mmap
50 1.1 mrg * function.
51 1.1 mrg */
52 1.1 mrg #include <sys/param.h>
53 1.1 mrg #include <sys/systm.h>
54 1.1 mrg #include <sys/file.h>
55 1.1 mrg #include <sys/filedesc.h>
56 1.1 mrg #include <sys/resourcevar.h>
57 1.1 mrg #include <sys/mman.h>
58 1.1 mrg #include <sys/mount.h>
59 1.1 mrg #include <sys/proc.h>
60 1.1 mrg #include <sys/malloc.h>
61 1.1 mrg #include <sys/vnode.h>
62 1.1 mrg #include <sys/conf.h>
63 1.9 mrg #include <sys/stat.h>
64 1.1 mrg
65 1.1 mrg #include <miscfs/specfs/specdev.h>
66 1.1 mrg
67 1.1 mrg #include <sys/syscallargs.h>
68 1.1 mrg
69 1.1 mrg #include <uvm/uvm.h>
70 1.1 mrg #include <uvm/uvm_device.h>
71 1.1 mrg #include <uvm/uvm_vnode.h>
72 1.1 mrg
73 1.1 mrg
74 1.1 mrg /*
75 1.1 mrg * unimplemented VM system calls:
76 1.1 mrg */
77 1.1 mrg
78 1.1 mrg /*
79 1.1 mrg * sys_sbrk: sbrk system call.
80 1.1 mrg */
81 1.1 mrg
82 1.1 mrg /* ARGSUSED */
83 1.6 mrg int
84 1.6 mrg sys_sbrk(p, v, retval)
85 1.6 mrg struct proc *p;
86 1.6 mrg void *v;
87 1.6 mrg register_t *retval;
88 1.1 mrg {
89 1.1 mrg #if 0
90 1.6 mrg struct sys_sbrk_args /* {
91 1.33 kleink syscallarg(intptr_t) incr;
92 1.20 mrg } */ *uap = v;
93 1.1 mrg #endif
94 1.6 mrg
95 1.17 kleink return (ENOSYS);
96 1.1 mrg }
97 1.1 mrg
98 1.1 mrg /*
99 1.1 mrg * sys_sstk: sstk system call.
100 1.1 mrg */
101 1.1 mrg
102 1.1 mrg /* ARGSUSED */
103 1.6 mrg int
104 1.6 mrg sys_sstk(p, v, retval)
105 1.6 mrg struct proc *p;
106 1.6 mrg void *v;
107 1.6 mrg register_t *retval;
108 1.1 mrg {
109 1.1 mrg #if 0
110 1.6 mrg struct sys_sstk_args /* {
111 1.20 mrg syscallarg(int) incr;
112 1.20 mrg } */ *uap = v;
113 1.1 mrg #endif
114 1.6 mrg
115 1.17 kleink return (ENOSYS);
116 1.1 mrg }
117 1.1 mrg
118 1.1 mrg /*
119 1.1 mrg * sys_mincore: determine if pages are in core or not.
120 1.1 mrg */
121 1.1 mrg
122 1.1 mrg /* ARGSUSED */
123 1.6 mrg int
124 1.6 mrg sys_mincore(p, v, retval)
125 1.6 mrg struct proc *p;
126 1.6 mrg void *v;
127 1.6 mrg register_t *retval;
128 1.1 mrg {
129 1.6 mrg struct sys_mincore_args /* {
130 1.22 thorpej syscallarg(void *) addr;
131 1.20 mrg syscallarg(size_t) len;
132 1.20 mrg syscallarg(char *) vec;
133 1.20 mrg } */ *uap = v;
134 1.22 thorpej vm_page_t m;
135 1.22 thorpej char *vec, pgi;
136 1.22 thorpej struct uvm_object *uobj;
137 1.22 thorpej struct vm_amap *amap;
138 1.22 thorpej struct vm_anon *anon;
139 1.22 thorpej vm_map_entry_t entry;
140 1.22 thorpej vaddr_t start, end, lim;
141 1.22 thorpej vm_map_t map;
142 1.22 thorpej vsize_t len;
143 1.22 thorpej int error = 0, npgs;
144 1.22 thorpej
145 1.22 thorpej map = &p->p_vmspace->vm_map;
146 1.22 thorpej
147 1.22 thorpej start = (vaddr_t)SCARG(uap, addr);
148 1.22 thorpej len = SCARG(uap, len);
149 1.22 thorpej vec = SCARG(uap, vec);
150 1.22 thorpej
151 1.22 thorpej if (start & PAGE_MASK)
152 1.22 thorpej return (EINVAL);
153 1.22 thorpej len = round_page(len);
154 1.22 thorpej end = start + len;
155 1.22 thorpej if (end <= start)
156 1.22 thorpej return (EINVAL);
157 1.22 thorpej
158 1.22 thorpej npgs = len >> PAGE_SHIFT;
159 1.22 thorpej
160 1.22 thorpej if (uvm_useracc(vec, npgs, B_WRITE) == FALSE)
161 1.22 thorpej return (EFAULT);
162 1.22 thorpej
163 1.22 thorpej /*
164 1.22 thorpej * Lock down vec, so our returned status isn't outdated by
165 1.22 thorpej * storing the status byte for a page.
166 1.22 thorpej */
167 1.22 thorpej uvm_vslock(p, vec, npgs, VM_PROT_WRITE);
168 1.22 thorpej
169 1.22 thorpej vm_map_lock_read(map);
170 1.22 thorpej
171 1.22 thorpej if (uvm_map_lookup_entry(map, start, &entry) == FALSE) {
172 1.22 thorpej error = ENOMEM;
173 1.22 thorpej goto out;
174 1.22 thorpej }
175 1.22 thorpej
176 1.22 thorpej for (/* nothing */;
177 1.22 thorpej entry != &map->header && entry->start < end;
178 1.22 thorpej entry = entry->next) {
179 1.49 chs KASSERT(!UVM_ET_ISSUBMAP(entry));
180 1.49 chs KASSERT(start >= entry->start);
181 1.49 chs
182 1.22 thorpej /* Make sure there are no holes. */
183 1.22 thorpej if (entry->end < end &&
184 1.22 thorpej (entry->next == &map->header ||
185 1.22 thorpej entry->next->start > entry->end)) {
186 1.22 thorpej error = ENOMEM;
187 1.22 thorpej goto out;
188 1.22 thorpej }
189 1.6 mrg
190 1.22 thorpej lim = end < entry->end ? end : entry->end;
191 1.22 thorpej
192 1.22 thorpej /*
193 1.31 thorpej * Special case for objects with no "real" pages. Those
194 1.31 thorpej * are always considered resident (mapped devices).
195 1.22 thorpej */
196 1.22 thorpej if (UVM_ET_ISOBJ(entry)) {
197 1.49 chs KASSERT(!UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj));
198 1.31 thorpej if (entry->object.uvm_obj->pgops->pgo_releasepg
199 1.31 thorpej == NULL) {
200 1.22 thorpej for (/* nothing */; start < lim;
201 1.22 thorpej start += PAGE_SIZE, vec++)
202 1.22 thorpej subyte(vec, 1);
203 1.22 thorpej continue;
204 1.22 thorpej }
205 1.22 thorpej }
206 1.22 thorpej
207 1.32 thorpej amap = entry->aref.ar_amap; /* top layer */
208 1.32 thorpej uobj = entry->object.uvm_obj; /* bottom layer */
209 1.22 thorpej
210 1.22 thorpej if (amap != NULL)
211 1.22 thorpej amap_lock(amap);
212 1.22 thorpej if (uobj != NULL)
213 1.22 thorpej simple_lock(&uobj->vmobjlock);
214 1.22 thorpej
215 1.22 thorpej for (/* nothing */; start < lim; start += PAGE_SIZE, vec++) {
216 1.22 thorpej pgi = 0;
217 1.22 thorpej if (amap != NULL) {
218 1.22 thorpej /* Check the top layer first. */
219 1.22 thorpej anon = amap_lookup(&entry->aref,
220 1.22 thorpej start - entry->start);
221 1.22 thorpej /* Don't need to lock anon here. */
222 1.22 thorpej if (anon != NULL && anon->u.an_page != NULL) {
223 1.22 thorpej /*
224 1.22 thorpej * Anon has the page for this entry
225 1.22 thorpej * offset.
226 1.22 thorpej */
227 1.22 thorpej pgi = 1;
228 1.22 thorpej }
229 1.22 thorpej }
230 1.22 thorpej
231 1.22 thorpej if (uobj != NULL && pgi == 0) {
232 1.22 thorpej /* Check the bottom layer. */
233 1.22 thorpej m = uvm_pagelookup(uobj,
234 1.22 thorpej entry->offset + (start - entry->start));
235 1.22 thorpej if (m != NULL) {
236 1.22 thorpej /*
237 1.22 thorpej * Object has the page for this entry
238 1.22 thorpej * offset.
239 1.22 thorpej */
240 1.22 thorpej pgi = 1;
241 1.22 thorpej }
242 1.22 thorpej }
243 1.22 thorpej
244 1.22 thorpej (void) subyte(vec, pgi);
245 1.22 thorpej }
246 1.22 thorpej
247 1.22 thorpej if (uobj != NULL)
248 1.27 thorpej simple_unlock(&uobj->vmobjlock);
249 1.22 thorpej if (amap != NULL)
250 1.22 thorpej amap_unlock(amap);
251 1.22 thorpej }
252 1.22 thorpej
253 1.22 thorpej out:
254 1.22 thorpej vm_map_unlock_read(map);
255 1.22 thorpej uvm_vsunlock(p, SCARG(uap, vec), npgs);
256 1.22 thorpej return (error);
257 1.1 mrg }
258 1.1 mrg
259 1.1 mrg /*
260 1.1 mrg * sys_mmap: mmap system call.
261 1.1 mrg *
262 1.1 mrg * => file offest and address may not be page aligned
263 1.1 mrg * - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE
264 1.1 mrg * - if address isn't page aligned the mapping starts at trunc_page(addr)
265 1.1 mrg * and the return value is adjusted up by the page offset.
266 1.1 mrg */
267 1.1 mrg
268 1.6 mrg int
269 1.6 mrg sys_mmap(p, v, retval)
270 1.6 mrg struct proc *p;
271 1.6 mrg void *v;
272 1.6 mrg register_t *retval;
273 1.6 mrg {
274 1.40 augustss struct sys_mmap_args /* {
275 1.6 mrg syscallarg(caddr_t) addr;
276 1.6 mrg syscallarg(size_t) len;
277 1.6 mrg syscallarg(int) prot;
278 1.6 mrg syscallarg(int) flags;
279 1.6 mrg syscallarg(int) fd;
280 1.6 mrg syscallarg(long) pad;
281 1.6 mrg syscallarg(off_t) pos;
282 1.6 mrg } */ *uap = v;
283 1.12 eeh vaddr_t addr;
284 1.9 mrg struct vattr va;
285 1.6 mrg off_t pos;
286 1.12 eeh vsize_t size, pageoff;
287 1.6 mrg vm_prot_t prot, maxprot;
288 1.6 mrg int flags, fd;
289 1.12 eeh vaddr_t vm_min_address = VM_MIN_ADDRESS;
290 1.40 augustss struct filedesc *fdp = p->p_fd;
291 1.40 augustss struct file *fp;
292 1.6 mrg struct vnode *vp;
293 1.6 mrg caddr_t handle;
294 1.6 mrg int error;
295 1.6 mrg
296 1.6 mrg /*
297 1.6 mrg * first, extract syscall args from the uap.
298 1.6 mrg */
299 1.6 mrg
300 1.12 eeh addr = (vaddr_t) SCARG(uap, addr);
301 1.12 eeh size = (vsize_t) SCARG(uap, len);
302 1.6 mrg prot = SCARG(uap, prot) & VM_PROT_ALL;
303 1.6 mrg flags = SCARG(uap, flags);
304 1.6 mrg fd = SCARG(uap, fd);
305 1.6 mrg pos = SCARG(uap, pos);
306 1.6 mrg
307 1.6 mrg /*
308 1.24 thorpej * Fixup the old deprecated MAP_COPY into MAP_PRIVATE, and
309 1.24 thorpej * validate the flags.
310 1.24 thorpej */
311 1.24 thorpej if (flags & MAP_COPY)
312 1.24 thorpej flags = (flags & ~MAP_COPY) | MAP_PRIVATE;
313 1.24 thorpej if ((flags & (MAP_SHARED|MAP_PRIVATE)) == (MAP_SHARED|MAP_PRIVATE))
314 1.24 thorpej return (EINVAL);
315 1.24 thorpej
316 1.24 thorpej /*
317 1.6 mrg * align file position and save offset. adjust size.
318 1.6 mrg */
319 1.6 mrg
320 1.6 mrg pageoff = (pos & PAGE_MASK);
321 1.6 mrg pos -= pageoff;
322 1.6 mrg size += pageoff; /* add offset */
323 1.12 eeh size = (vsize_t) round_page(size); /* round up */
324 1.6 mrg if ((ssize_t) size < 0)
325 1.6 mrg return (EINVAL); /* don't allow wrap */
326 1.6 mrg
327 1.6 mrg /*
328 1.6 mrg * now check (MAP_FIXED) or get (!MAP_FIXED) the "addr"
329 1.6 mrg */
330 1.6 mrg
331 1.6 mrg if (flags & MAP_FIXED) {
332 1.6 mrg
333 1.6 mrg /* ensure address and file offset are aligned properly */
334 1.6 mrg addr -= pageoff;
335 1.6 mrg if (addr & PAGE_MASK)
336 1.6 mrg return (EINVAL);
337 1.6 mrg
338 1.6 mrg if (VM_MAXUSER_ADDRESS > 0 &&
339 1.6 mrg (addr + size) > VM_MAXUSER_ADDRESS)
340 1.6 mrg return (EINVAL);
341 1.6 mrg if (vm_min_address > 0 && addr < vm_min_address)
342 1.6 mrg return (EINVAL);
343 1.6 mrg if (addr > addr + size)
344 1.6 mrg return (EINVAL); /* no wrapping! */
345 1.6 mrg
346 1.6 mrg } else {
347 1.6 mrg
348 1.6 mrg /*
349 1.6 mrg * not fixed: make sure we skip over the largest possible heap.
350 1.6 mrg * we will refine our guess later (e.g. to account for VAC, etc)
351 1.6 mrg */
352 1.46 chs
353 1.46 chs if (addr < round_page((vaddr_t)p->p_vmspace->vm_daddr +
354 1.46 chs MAXDSIZ))
355 1.38 kleink addr = round_page((vaddr_t)p->p_vmspace->vm_daddr +
356 1.38 kleink MAXDSIZ);
357 1.6 mrg }
358 1.6 mrg
359 1.6 mrg /*
360 1.6 mrg * check for file mappings (i.e. not anonymous) and verify file.
361 1.6 mrg */
362 1.6 mrg
363 1.6 mrg if ((flags & MAP_ANON) == 0) {
364 1.6 mrg
365 1.6 mrg if (fd < 0 || fd >= fdp->fd_nfiles)
366 1.6 mrg return(EBADF); /* failed range check? */
367 1.6 mrg fp = fdp->fd_ofiles[fd]; /* convert to file pointer */
368 1.6 mrg if (fp == NULL)
369 1.6 mrg return(EBADF);
370 1.6 mrg
371 1.6 mrg if (fp->f_type != DTYPE_VNODE)
372 1.7 kleink return (ENODEV); /* only mmap vnodes! */
373 1.6 mrg vp = (struct vnode *)fp->f_data; /* convert to vnode */
374 1.6 mrg
375 1.11 thorpej if (vp->v_type != VREG && vp->v_type != VCHR &&
376 1.11 thorpej vp->v_type != VBLK)
377 1.11 thorpej return (ENODEV); /* only REG/CHR/BLK support mmap */
378 1.39 kleink
379 1.39 kleink if (vp->v_type == VREG && (pos + size) < pos)
380 1.39 kleink return (EOVERFLOW); /* no offset wrapping */
381 1.6 mrg
382 1.6 mrg /* special case: catch SunOS style /dev/zero */
383 1.6 mrg if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
384 1.6 mrg flags |= MAP_ANON;
385 1.6 mrg goto is_anon;
386 1.6 mrg }
387 1.6 mrg
388 1.6 mrg /*
389 1.6 mrg * Old programs may not select a specific sharing type, so
390 1.6 mrg * default to an appropriate one.
391 1.6 mrg *
392 1.6 mrg * XXX: how does MAP_ANON fit in the picture?
393 1.6 mrg */
394 1.24 thorpej if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
395 1.8 tv #if defined(DEBUG)
396 1.6 mrg printf("WARNING: defaulted mmap() share type to "
397 1.6 mrg "%s (pid %d comm %s)\n", vp->v_type == VCHR ?
398 1.6 mrg "MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
399 1.6 mrg p->p_comm);
400 1.1 mrg #endif
401 1.6 mrg if (vp->v_type == VCHR)
402 1.6 mrg flags |= MAP_SHARED; /* for a device */
403 1.6 mrg else
404 1.6 mrg flags |= MAP_PRIVATE; /* for a file */
405 1.6 mrg }
406 1.6 mrg
407 1.6 mrg /*
408 1.6 mrg * MAP_PRIVATE device mappings don't make sense (and aren't
409 1.6 mrg * supported anyway). However, some programs rely on this,
410 1.6 mrg * so just change it to MAP_SHARED.
411 1.6 mrg */
412 1.6 mrg if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
413 1.6 mrg flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
414 1.6 mrg }
415 1.1 mrg
416 1.6 mrg /*
417 1.6 mrg * now check protection
418 1.6 mrg */
419 1.6 mrg
420 1.48 thorpej maxprot = VM_PROT_EXECUTE;
421 1.6 mrg
422 1.6 mrg /* check read access */
423 1.6 mrg if (fp->f_flag & FREAD)
424 1.6 mrg maxprot |= VM_PROT_READ;
425 1.6 mrg else if (prot & PROT_READ)
426 1.6 mrg return (EACCES);
427 1.6 mrg
428 1.9 mrg /* check write access, shared case first */
429 1.6 mrg if (flags & MAP_SHARED) {
430 1.9 mrg /*
431 1.9 mrg * if the file is writable, only add PROT_WRITE to
432 1.9 mrg * maxprot if the file is not immutable, append-only.
433 1.9 mrg * otherwise, if we have asked for PROT_WRITE, return
434 1.9 mrg * EPERM.
435 1.9 mrg */
436 1.9 mrg if (fp->f_flag & FWRITE) {
437 1.9 mrg if ((error =
438 1.9 mrg VOP_GETATTR(vp, &va, p->p_ucred, p)))
439 1.9 mrg return (error);
440 1.9 mrg if ((va.va_flags & (IMMUTABLE|APPEND)) == 0)
441 1.9 mrg maxprot |= VM_PROT_WRITE;
442 1.9 mrg else if (prot & PROT_WRITE)
443 1.9 mrg return (EPERM);
444 1.9 mrg }
445 1.6 mrg else if (prot & PROT_WRITE)
446 1.6 mrg return (EACCES);
447 1.6 mrg } else {
448 1.6 mrg /* MAP_PRIVATE mappings can always write to */
449 1.6 mrg maxprot |= VM_PROT_WRITE;
450 1.6 mrg }
451 1.6 mrg
452 1.6 mrg /*
453 1.6 mrg * set handle to vnode
454 1.6 mrg */
455 1.1 mrg
456 1.6 mrg handle = (caddr_t)vp;
457 1.1 mrg
458 1.6 mrg } else { /* MAP_ANON case */
459 1.24 thorpej /*
460 1.24 thorpej * XXX What do we do about (MAP_SHARED|MAP_PRIVATE) == 0?
461 1.24 thorpej */
462 1.6 mrg if (fd != -1)
463 1.6 mrg return (EINVAL);
464 1.1 mrg
465 1.24 thorpej is_anon: /* label for SunOS style /dev/zero */
466 1.6 mrg handle = NULL;
467 1.6 mrg maxprot = VM_PROT_ALL;
468 1.6 mrg pos = 0;
469 1.28 cgd }
470 1.28 cgd
471 1.28 cgd /*
472 1.28 cgd * XXX (in)sanity check. We don't do proper datasize checking
473 1.28 cgd * XXX for anonymous (or private writable) mmap(). However,
474 1.28 cgd * XXX know that if we're trying to allocate more than the amount
475 1.28 cgd * XXX remaining under our current data size limit, _that_ should
476 1.28 cgd * XXX be disallowed.
477 1.28 cgd */
478 1.28 cgd if ((flags & MAP_ANON) != 0 ||
479 1.28 cgd ((flags & MAP_PRIVATE) != 0 && (prot & PROT_WRITE) != 0)) {
480 1.28 cgd if (size >
481 1.28 cgd (p->p_rlimit[RLIMIT_DATA].rlim_cur - ctob(p->p_vmspace->vm_dsize))) {
482 1.28 cgd return (ENOMEM);
483 1.28 cgd }
484 1.6 mrg }
485 1.6 mrg
486 1.6 mrg /*
487 1.6 mrg * now let kernel internal function uvm_mmap do the work.
488 1.6 mrg */
489 1.6 mrg
490 1.6 mrg error = uvm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
491 1.25 thorpej flags, handle, pos, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
492 1.6 mrg
493 1.6 mrg if (error == 0)
494 1.6 mrg /* remember to add offset */
495 1.6 mrg *retval = (register_t)(addr + pageoff);
496 1.1 mrg
497 1.6 mrg return (error);
498 1.1 mrg }
499 1.1 mrg
500 1.1 mrg /*
501 1.1 mrg * sys___msync13: the msync system call (a front-end for flush)
502 1.1 mrg */
503 1.1 mrg
504 1.6 mrg int
505 1.6 mrg sys___msync13(p, v, retval)
506 1.6 mrg struct proc *p;
507 1.6 mrg void *v;
508 1.6 mrg register_t *retval;
509 1.6 mrg {
510 1.6 mrg struct sys___msync13_args /* {
511 1.6 mrg syscallarg(caddr_t) addr;
512 1.6 mrg syscallarg(size_t) len;
513 1.6 mrg syscallarg(int) flags;
514 1.6 mrg } */ *uap = v;
515 1.12 eeh vaddr_t addr;
516 1.12 eeh vsize_t size, pageoff;
517 1.6 mrg vm_map_t map;
518 1.6 mrg int rv, flags, uvmflags;
519 1.6 mrg
520 1.6 mrg /*
521 1.6 mrg * extract syscall args from the uap
522 1.6 mrg */
523 1.6 mrg
524 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
525 1.12 eeh size = (vsize_t)SCARG(uap, len);
526 1.6 mrg flags = SCARG(uap, flags);
527 1.6 mrg
528 1.6 mrg /* sanity check flags */
529 1.6 mrg if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 ||
530 1.6 mrg (flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 ||
531 1.6 mrg (flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC))
532 1.1 mrg return (EINVAL);
533 1.6 mrg if ((flags & (MS_ASYNC | MS_SYNC)) == 0)
534 1.1 mrg flags |= MS_SYNC;
535 1.1 mrg
536 1.6 mrg /*
537 1.6 mrg * align the address to a page boundary, and adjust the size accordingly
538 1.6 mrg */
539 1.6 mrg
540 1.6 mrg pageoff = (addr & PAGE_MASK);
541 1.6 mrg addr -= pageoff;
542 1.6 mrg size += pageoff;
543 1.12 eeh size = (vsize_t) round_page(size);
544 1.6 mrg
545 1.6 mrg /* disallow wrap-around. */
546 1.6 mrg if (addr + size < addr)
547 1.6 mrg return (EINVAL);
548 1.6 mrg
549 1.6 mrg /*
550 1.6 mrg * get map
551 1.6 mrg */
552 1.6 mrg
553 1.6 mrg map = &p->p_vmspace->vm_map;
554 1.6 mrg
555 1.6 mrg /*
556 1.6 mrg * XXXCDC: do we really need this semantic?
557 1.6 mrg *
558 1.6 mrg * XXX Gak! If size is zero we are supposed to sync "all modified
559 1.6 mrg * pages with the region containing addr". Unfortunately, we
560 1.6 mrg * don't really keep track of individual mmaps so we approximate
561 1.6 mrg * by flushing the range of the map entry containing addr.
562 1.6 mrg * This can be incorrect if the region splits or is coalesced
563 1.6 mrg * with a neighbor.
564 1.6 mrg */
565 1.6 mrg if (size == 0) {
566 1.6 mrg vm_map_entry_t entry;
567 1.6 mrg
568 1.6 mrg vm_map_lock_read(map);
569 1.6 mrg rv = uvm_map_lookup_entry(map, addr, &entry);
570 1.6 mrg if (rv == TRUE) {
571 1.6 mrg addr = entry->start;
572 1.6 mrg size = entry->end - entry->start;
573 1.6 mrg }
574 1.6 mrg vm_map_unlock_read(map);
575 1.6 mrg if (rv == FALSE)
576 1.6 mrg return (EINVAL);
577 1.6 mrg }
578 1.6 mrg
579 1.6 mrg /*
580 1.6 mrg * translate MS_ flags into PGO_ flags
581 1.6 mrg */
582 1.34 thorpej uvmflags = PGO_CLEANIT;
583 1.34 thorpej if (flags & MS_INVALIDATE)
584 1.34 thorpej uvmflags |= PGO_FREE;
585 1.6 mrg if (flags & MS_SYNC)
586 1.6 mrg uvmflags |= PGO_SYNCIO;
587 1.6 mrg else
588 1.6 mrg uvmflags |= PGO_SYNCIO; /* XXXCDC: force sync for now! */
589 1.6 mrg
590 1.6 mrg /*
591 1.6 mrg * doit!
592 1.6 mrg */
593 1.6 mrg rv = uvm_map_clean(map, addr, addr+size, uvmflags);
594 1.6 mrg
595 1.6 mrg /*
596 1.6 mrg * and return...
597 1.6 mrg */
598 1.6 mrg switch (rv) {
599 1.6 mrg case KERN_SUCCESS:
600 1.6 mrg return(0);
601 1.6 mrg case KERN_INVALID_ADDRESS:
602 1.6 mrg return (ENOMEM);
603 1.6 mrg case KERN_FAILURE:
604 1.6 mrg return (EIO);
605 1.6 mrg case KERN_PAGES_LOCKED: /* XXXCDC: uvm doesn't return this */
606 1.6 mrg return (EBUSY);
607 1.6 mrg default:
608 1.6 mrg return (EINVAL);
609 1.6 mrg }
610 1.6 mrg /*NOTREACHED*/
611 1.1 mrg }
612 1.1 mrg
613 1.1 mrg /*
614 1.1 mrg * sys_munmap: unmap a users memory
615 1.1 mrg */
616 1.1 mrg
617 1.6 mrg int
618 1.6 mrg sys_munmap(p, v, retval)
619 1.40 augustss struct proc *p;
620 1.6 mrg void *v;
621 1.6 mrg register_t *retval;
622 1.6 mrg {
623 1.40 augustss struct sys_munmap_args /* {
624 1.6 mrg syscallarg(caddr_t) addr;
625 1.6 mrg syscallarg(size_t) len;
626 1.6 mrg } */ *uap = v;
627 1.12 eeh vaddr_t addr;
628 1.12 eeh vsize_t size, pageoff;
629 1.6 mrg vm_map_t map;
630 1.12 eeh vaddr_t vm_min_address = VM_MIN_ADDRESS;
631 1.6 mrg struct vm_map_entry *dead_entries;
632 1.6 mrg
633 1.6 mrg /*
634 1.6 mrg * get syscall args...
635 1.6 mrg */
636 1.6 mrg
637 1.12 eeh addr = (vaddr_t) SCARG(uap, addr);
638 1.12 eeh size = (vsize_t) SCARG(uap, len);
639 1.6 mrg
640 1.6 mrg /*
641 1.6 mrg * align the address to a page boundary, and adjust the size accordingly
642 1.6 mrg */
643 1.6 mrg
644 1.6 mrg pageoff = (addr & PAGE_MASK);
645 1.6 mrg addr -= pageoff;
646 1.6 mrg size += pageoff;
647 1.12 eeh size = (vsize_t) round_page(size);
648 1.6 mrg
649 1.6 mrg if ((int)size < 0)
650 1.6 mrg return (EINVAL);
651 1.6 mrg if (size == 0)
652 1.6 mrg return (0);
653 1.6 mrg
654 1.6 mrg /*
655 1.6 mrg * Check for illegal addresses. Watch out for address wrap...
656 1.6 mrg * Note that VM_*_ADDRESS are not constants due to casts (argh).
657 1.6 mrg */
658 1.6 mrg if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
659 1.6 mrg return (EINVAL);
660 1.6 mrg if (vm_min_address > 0 && addr < vm_min_address)
661 1.6 mrg return (EINVAL);
662 1.6 mrg if (addr > addr + size)
663 1.6 mrg return (EINVAL);
664 1.6 mrg map = &p->p_vmspace->vm_map;
665 1.6 mrg
666 1.6 mrg
667 1.6 mrg vm_map_lock(map); /* lock map so we can checkprot */
668 1.6 mrg
669 1.6 mrg /*
670 1.6 mrg * interesting system call semantic: make sure entire range is
671 1.6 mrg * allocated before allowing an unmap.
672 1.6 mrg */
673 1.6 mrg
674 1.6 mrg if (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) {
675 1.6 mrg vm_map_unlock(map);
676 1.6 mrg return (EINVAL);
677 1.6 mrg }
678 1.6 mrg
679 1.6 mrg /*
680 1.6 mrg * doit!
681 1.6 mrg */
682 1.15 chuck (void) uvm_unmap_remove(map, addr, addr + size, &dead_entries);
683 1.1 mrg
684 1.6 mrg vm_map_unlock(map); /* and unlock */
685 1.1 mrg
686 1.6 mrg if (dead_entries != NULL)
687 1.6 mrg uvm_unmap_detach(dead_entries, 0);
688 1.1 mrg
689 1.6 mrg return (0);
690 1.1 mrg }
691 1.1 mrg
692 1.1 mrg /*
693 1.1 mrg * sys_mprotect: the mprotect system call
694 1.1 mrg */
695 1.1 mrg
696 1.6 mrg int
697 1.6 mrg sys_mprotect(p, v, retval)
698 1.6 mrg struct proc *p;
699 1.6 mrg void *v;
700 1.6 mrg register_t *retval;
701 1.6 mrg {
702 1.6 mrg struct sys_mprotect_args /* {
703 1.6 mrg syscallarg(caddr_t) addr;
704 1.6 mrg syscallarg(int) len;
705 1.6 mrg syscallarg(int) prot;
706 1.6 mrg } */ *uap = v;
707 1.12 eeh vaddr_t addr;
708 1.12 eeh vsize_t size, pageoff;
709 1.6 mrg vm_prot_t prot;
710 1.6 mrg int rv;
711 1.6 mrg
712 1.6 mrg /*
713 1.6 mrg * extract syscall args from uap
714 1.6 mrg */
715 1.6 mrg
716 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
717 1.12 eeh size = (vsize_t)SCARG(uap, len);
718 1.6 mrg prot = SCARG(uap, prot) & VM_PROT_ALL;
719 1.6 mrg
720 1.6 mrg /*
721 1.6 mrg * align the address to a page boundary, and adjust the size accordingly
722 1.6 mrg */
723 1.6 mrg pageoff = (addr & PAGE_MASK);
724 1.6 mrg addr -= pageoff;
725 1.6 mrg size += pageoff;
726 1.12 eeh size = (vsize_t) round_page(size);
727 1.6 mrg if ((int)size < 0)
728 1.6 mrg return (EINVAL);
729 1.6 mrg
730 1.6 mrg /*
731 1.6 mrg * doit
732 1.6 mrg */
733 1.1 mrg
734 1.6 mrg rv = uvm_map_protect(&p->p_vmspace->vm_map,
735 1.1 mrg addr, addr+size, prot, FALSE);
736 1.1 mrg
737 1.6 mrg if (rv == KERN_SUCCESS)
738 1.6 mrg return (0);
739 1.6 mrg if (rv == KERN_PROTECTION_FAILURE)
740 1.6 mrg return (EACCES);
741 1.6 mrg return (EINVAL);
742 1.1 mrg }
743 1.1 mrg
744 1.1 mrg /*
745 1.1 mrg * sys_minherit: the minherit system call
746 1.1 mrg */
747 1.1 mrg
748 1.6 mrg int
749 1.6 mrg sys_minherit(p, v, retval)
750 1.6 mrg struct proc *p;
751 1.6 mrg void *v;
752 1.6 mrg register_t *retval;
753 1.6 mrg {
754 1.6 mrg struct sys_minherit_args /* {
755 1.6 mrg syscallarg(caddr_t) addr;
756 1.6 mrg syscallarg(int) len;
757 1.6 mrg syscallarg(int) inherit;
758 1.6 mrg } */ *uap = v;
759 1.12 eeh vaddr_t addr;
760 1.12 eeh vsize_t size, pageoff;
761 1.40 augustss vm_inherit_t inherit;
762 1.6 mrg
763 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
764 1.12 eeh size = (vsize_t)SCARG(uap, len);
765 1.6 mrg inherit = SCARG(uap, inherit);
766 1.6 mrg /*
767 1.6 mrg * align the address to a page boundary, and adjust the size accordingly
768 1.6 mrg */
769 1.6 mrg
770 1.6 mrg pageoff = (addr & PAGE_MASK);
771 1.6 mrg addr -= pageoff;
772 1.6 mrg size += pageoff;
773 1.12 eeh size = (vsize_t) round_page(size);
774 1.6 mrg
775 1.6 mrg if ((int)size < 0)
776 1.6 mrg return (EINVAL);
777 1.6 mrg
778 1.6 mrg switch (uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size,
779 1.1 mrg inherit)) {
780 1.21 mrg case KERN_SUCCESS:
781 1.21 mrg return (0);
782 1.21 mrg case KERN_PROTECTION_FAILURE:
783 1.21 mrg return (EACCES);
784 1.21 mrg }
785 1.21 mrg return (EINVAL);
786 1.21 mrg }
787 1.21 mrg
788 1.21 mrg /*
789 1.21 mrg * sys_madvise: give advice about memory usage.
790 1.21 mrg */
791 1.21 mrg
792 1.21 mrg /* ARGSUSED */
793 1.21 mrg int
794 1.21 mrg sys_madvise(p, v, retval)
795 1.21 mrg struct proc *p;
796 1.21 mrg void *v;
797 1.21 mrg register_t *retval;
798 1.21 mrg {
799 1.21 mrg struct sys_madvise_args /* {
800 1.21 mrg syscallarg(caddr_t) addr;
801 1.21 mrg syscallarg(size_t) len;
802 1.21 mrg syscallarg(int) behav;
803 1.21 mrg } */ *uap = v;
804 1.21 mrg vaddr_t addr;
805 1.21 mrg vsize_t size, pageoff;
806 1.29 thorpej int advice, rv;;
807 1.21 mrg
808 1.21 mrg addr = (vaddr_t)SCARG(uap, addr);
809 1.21 mrg size = (vsize_t)SCARG(uap, len);
810 1.21 mrg advice = SCARG(uap, behav);
811 1.21 mrg
812 1.21 mrg /*
813 1.21 mrg * align the address to a page boundary, and adjust the size accordingly
814 1.21 mrg */
815 1.21 mrg pageoff = (addr & PAGE_MASK);
816 1.21 mrg addr -= pageoff;
817 1.21 mrg size += pageoff;
818 1.21 mrg size = (vsize_t) round_page(size);
819 1.21 mrg
820 1.29 thorpej if ((ssize_t)size <= 0)
821 1.29 thorpej return (EINVAL);
822 1.29 thorpej
823 1.29 thorpej switch (advice) {
824 1.29 thorpej case MADV_NORMAL:
825 1.29 thorpej case MADV_RANDOM:
826 1.29 thorpej case MADV_SEQUENTIAL:
827 1.29 thorpej rv = uvm_map_advice(&p->p_vmspace->vm_map, addr, addr + size,
828 1.29 thorpej advice);
829 1.29 thorpej break;
830 1.29 thorpej
831 1.29 thorpej case MADV_WILLNEED:
832 1.29 thorpej /*
833 1.29 thorpej * Activate all these pages, pre-faulting them in if
834 1.29 thorpej * necessary.
835 1.29 thorpej */
836 1.29 thorpej /*
837 1.29 thorpej * XXX IMPLEMENT ME.
838 1.29 thorpej * Should invent a "weak" mode for uvm_fault()
839 1.29 thorpej * which would only do the PGO_LOCKED pgo_get().
840 1.29 thorpej */
841 1.29 thorpej return (0);
842 1.29 thorpej
843 1.29 thorpej case MADV_DONTNEED:
844 1.29 thorpej /*
845 1.29 thorpej * Deactivate all these pages. We don't need them
846 1.29 thorpej * any more. We don't, however, toss the data in
847 1.29 thorpej * the pages.
848 1.29 thorpej */
849 1.29 thorpej rv = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
850 1.29 thorpej PGO_DEACTIVATE);
851 1.29 thorpej break;
852 1.29 thorpej
853 1.29 thorpej case MADV_FREE:
854 1.29 thorpej /*
855 1.29 thorpej * These pages contain no valid data, and may be
856 1.45 soren * garbage-collected. Toss all resources, including
857 1.30 thorpej * any swap space in use.
858 1.29 thorpej */
859 1.29 thorpej rv = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
860 1.29 thorpej PGO_FREE);
861 1.29 thorpej break;
862 1.29 thorpej
863 1.29 thorpej case MADV_SPACEAVAIL:
864 1.29 thorpej /*
865 1.29 thorpej * XXXMRG What is this? I think it's:
866 1.29 thorpej *
867 1.29 thorpej * Ensure that we have allocated backing-store
868 1.29 thorpej * for these pages.
869 1.29 thorpej *
870 1.29 thorpej * This is going to require changes to the page daemon,
871 1.29 thorpej * as it will free swap space allocated to pages in core.
872 1.29 thorpej * There's also what to do for device/file/anonymous memory.
873 1.29 thorpej */
874 1.29 thorpej return (EINVAL);
875 1.29 thorpej
876 1.29 thorpej default:
877 1.21 mrg return (EINVAL);
878 1.29 thorpej }
879 1.29 thorpej
880 1.29 thorpej switch (rv) {
881 1.6 mrg case KERN_SUCCESS:
882 1.6 mrg return (0);
883 1.29 thorpej case KERN_NO_SPACE:
884 1.29 thorpej return (EAGAIN);
885 1.29 thorpej case KERN_INVALID_ADDRESS:
886 1.29 thorpej return (ENOMEM);
887 1.29 thorpej case KERN_FAILURE:
888 1.29 thorpej return (EIO);
889 1.6 mrg }
890 1.29 thorpej
891 1.6 mrg return (EINVAL);
892 1.1 mrg }
893 1.1 mrg
894 1.1 mrg /*
895 1.1 mrg * sys_mlock: memory lock
896 1.1 mrg */
897 1.1 mrg
898 1.6 mrg int
899 1.6 mrg sys_mlock(p, v, retval)
900 1.6 mrg struct proc *p;
901 1.6 mrg void *v;
902 1.6 mrg register_t *retval;
903 1.6 mrg {
904 1.6 mrg struct sys_mlock_args /* {
905 1.10 kleink syscallarg(const void *) addr;
906 1.6 mrg syscallarg(size_t) len;
907 1.6 mrg } */ *uap = v;
908 1.12 eeh vaddr_t addr;
909 1.12 eeh vsize_t size, pageoff;
910 1.6 mrg int error;
911 1.6 mrg
912 1.6 mrg /*
913 1.6 mrg * extract syscall args from uap
914 1.6 mrg */
915 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
916 1.12 eeh size = (vsize_t)SCARG(uap, len);
917 1.6 mrg
918 1.6 mrg /*
919 1.6 mrg * align the address to a page boundary and adjust the size accordingly
920 1.6 mrg */
921 1.6 mrg pageoff = (addr & PAGE_MASK);
922 1.6 mrg addr -= pageoff;
923 1.6 mrg size += pageoff;
924 1.12 eeh size = (vsize_t) round_page(size);
925 1.6 mrg
926 1.6 mrg /* disallow wrap-around. */
927 1.6 mrg if (addr + (int)size < addr)
928 1.6 mrg return (EINVAL);
929 1.1 mrg
930 1.6 mrg if (atop(size) + uvmexp.wired > uvmexp.wiredmax)
931 1.6 mrg return (EAGAIN);
932 1.1 mrg
933 1.1 mrg #ifdef pmap_wired_count
934 1.6 mrg if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
935 1.6 mrg p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
936 1.6 mrg return (EAGAIN);
937 1.1 mrg #else
938 1.6 mrg if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
939 1.6 mrg return (error);
940 1.1 mrg #endif
941 1.1 mrg
942 1.25 thorpej error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, FALSE,
943 1.35 thorpej 0);
944 1.6 mrg return (error == KERN_SUCCESS ? 0 : ENOMEM);
945 1.1 mrg }
946 1.1 mrg
947 1.1 mrg /*
948 1.1 mrg * sys_munlock: unlock wired pages
949 1.1 mrg */
950 1.1 mrg
951 1.6 mrg int
952 1.6 mrg sys_munlock(p, v, retval)
953 1.6 mrg struct proc *p;
954 1.6 mrg void *v;
955 1.6 mrg register_t *retval;
956 1.6 mrg {
957 1.6 mrg struct sys_munlock_args /* {
958 1.10 kleink syscallarg(const void *) addr;
959 1.6 mrg syscallarg(size_t) len;
960 1.6 mrg } */ *uap = v;
961 1.12 eeh vaddr_t addr;
962 1.12 eeh vsize_t size, pageoff;
963 1.6 mrg int error;
964 1.6 mrg
965 1.6 mrg /*
966 1.6 mrg * extract syscall args from uap
967 1.6 mrg */
968 1.6 mrg
969 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
970 1.12 eeh size = (vsize_t)SCARG(uap, len);
971 1.6 mrg
972 1.6 mrg /*
973 1.6 mrg * align the address to a page boundary, and adjust the size accordingly
974 1.6 mrg */
975 1.6 mrg pageoff = (addr & PAGE_MASK);
976 1.6 mrg addr -= pageoff;
977 1.6 mrg size += pageoff;
978 1.12 eeh size = (vsize_t) round_page(size);
979 1.6 mrg
980 1.6 mrg /* disallow wrap-around. */
981 1.6 mrg if (addr + (int)size < addr)
982 1.6 mrg return (EINVAL);
983 1.1 mrg
984 1.1 mrg #ifndef pmap_wired_count
985 1.6 mrg if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
986 1.6 mrg return (error);
987 1.1 mrg #endif
988 1.1 mrg
989 1.25 thorpej error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, TRUE,
990 1.35 thorpej 0);
991 1.6 mrg return (error == KERN_SUCCESS ? 0 : ENOMEM);
992 1.22 thorpej }
993 1.22 thorpej
994 1.22 thorpej /*
995 1.22 thorpej * sys_mlockall: lock all pages mapped into an address space.
996 1.22 thorpej */
997 1.22 thorpej
998 1.22 thorpej int
999 1.22 thorpej sys_mlockall(p, v, retval)
1000 1.22 thorpej struct proc *p;
1001 1.22 thorpej void *v;
1002 1.22 thorpej register_t *retval;
1003 1.22 thorpej {
1004 1.22 thorpej struct sys_mlockall_args /* {
1005 1.22 thorpej syscallarg(int) flags;
1006 1.22 thorpej } */ *uap = v;
1007 1.22 thorpej int error, flags;
1008 1.22 thorpej
1009 1.22 thorpej flags = SCARG(uap, flags);
1010 1.22 thorpej
1011 1.22 thorpej if (flags == 0 ||
1012 1.22 thorpej (flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0)
1013 1.22 thorpej return (EINVAL);
1014 1.22 thorpej
1015 1.25 thorpej #ifndef pmap_wired_count
1016 1.22 thorpej if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1017 1.22 thorpej return (error);
1018 1.22 thorpej #endif
1019 1.22 thorpej
1020 1.25 thorpej error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags,
1021 1.25 thorpej p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
1022 1.22 thorpej switch (error) {
1023 1.22 thorpej case KERN_SUCCESS:
1024 1.22 thorpej error = 0;
1025 1.22 thorpej break;
1026 1.22 thorpej
1027 1.22 thorpej case KERN_NO_SPACE: /* XXX overloaded */
1028 1.22 thorpej error = ENOMEM;
1029 1.22 thorpej break;
1030 1.22 thorpej
1031 1.22 thorpej default:
1032 1.22 thorpej /*
1033 1.22 thorpej * "Some or all of the memory could not be locked when
1034 1.22 thorpej * the call was made."
1035 1.22 thorpej */
1036 1.22 thorpej error = EAGAIN;
1037 1.22 thorpej }
1038 1.22 thorpej
1039 1.22 thorpej return (error);
1040 1.22 thorpej }
1041 1.22 thorpej
1042 1.22 thorpej /*
1043 1.22 thorpej * sys_munlockall: unlock all pages mapped into an address space.
1044 1.22 thorpej */
1045 1.22 thorpej
1046 1.22 thorpej int
1047 1.22 thorpej sys_munlockall(p, v, retval)
1048 1.22 thorpej struct proc *p;
1049 1.22 thorpej void *v;
1050 1.22 thorpej register_t *retval;
1051 1.22 thorpej {
1052 1.22 thorpej
1053 1.22 thorpej (void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0);
1054 1.22 thorpej return (0);
1055 1.1 mrg }
1056 1.1 mrg
1057 1.1 mrg /*
1058 1.1 mrg * uvm_mmap: internal version of mmap
1059 1.1 mrg *
1060 1.1 mrg * - used by sys_mmap, exec, and sysv shm
1061 1.1 mrg * - handle is a vnode pointer or NULL for MAP_ANON (XXX: not true,
1062 1.1 mrg * sysv shm uses "named anonymous memory")
1063 1.1 mrg * - caller must page-align the file offset
1064 1.1 mrg */
1065 1.1 mrg
1066 1.6 mrg int
1067 1.25 thorpej uvm_mmap(map, addr, size, prot, maxprot, flags, handle, foff, locklimit)
1068 1.6 mrg vm_map_t map;
1069 1.12 eeh vaddr_t *addr;
1070 1.12 eeh vsize_t size;
1071 1.6 mrg vm_prot_t prot, maxprot;
1072 1.6 mrg int flags;
1073 1.6 mrg caddr_t handle; /* XXX: VNODE? */
1074 1.38 kleink voff_t foff;
1075 1.25 thorpej vsize_t locklimit;
1076 1.6 mrg {
1077 1.6 mrg struct uvm_object *uobj;
1078 1.6 mrg struct vnode *vp;
1079 1.6 mrg int retval;
1080 1.6 mrg int advice = UVM_ADV_NORMAL;
1081 1.6 mrg uvm_flag_t uvmflag = 0;
1082 1.6 mrg
1083 1.6 mrg /*
1084 1.6 mrg * check params
1085 1.6 mrg */
1086 1.6 mrg
1087 1.6 mrg if (size == 0)
1088 1.6 mrg return(0);
1089 1.6 mrg if (foff & PAGE_MASK)
1090 1.6 mrg return(EINVAL);
1091 1.6 mrg if ((prot & maxprot) != prot)
1092 1.6 mrg return(EINVAL);
1093 1.6 mrg
1094 1.6 mrg /*
1095 1.6 mrg * for non-fixed mappings, round off the suggested address.
1096 1.6 mrg * for fixed mappings, check alignment and zap old mappings.
1097 1.6 mrg */
1098 1.6 mrg
1099 1.6 mrg if ((flags & MAP_FIXED) == 0) {
1100 1.6 mrg *addr = round_page(*addr); /* round */
1101 1.6 mrg } else {
1102 1.6 mrg
1103 1.6 mrg if (*addr & PAGE_MASK)
1104 1.6 mrg return(EINVAL);
1105 1.6 mrg uvmflag |= UVM_FLAG_FIXED;
1106 1.15 chuck (void) uvm_unmap(map, *addr, *addr + size); /* zap! */
1107 1.6 mrg }
1108 1.6 mrg
1109 1.6 mrg /*
1110 1.6 mrg * handle anon vs. non-anon mappings. for non-anon mappings attach
1111 1.6 mrg * to underlying vm object.
1112 1.6 mrg */
1113 1.6 mrg
1114 1.6 mrg if (flags & MAP_ANON) {
1115 1.36 thorpej foff = UVM_UNKNOWN_OFFSET;
1116 1.6 mrg uobj = NULL;
1117 1.6 mrg if ((flags & MAP_SHARED) == 0)
1118 1.6 mrg /* XXX: defer amap create */
1119 1.6 mrg uvmflag |= UVM_FLAG_COPYONW;
1120 1.6 mrg else
1121 1.6 mrg /* shared: create amap now */
1122 1.6 mrg uvmflag |= UVM_FLAG_OVERLAY;
1123 1.6 mrg
1124 1.6 mrg } else {
1125 1.6 mrg
1126 1.6 mrg vp = (struct vnode *) handle; /* get vnode */
1127 1.6 mrg if (vp->v_type != VCHR) {
1128 1.6 mrg uobj = uvn_attach((void *) vp, (flags & MAP_SHARED) ?
1129 1.6 mrg maxprot : (maxprot & ~VM_PROT_WRITE));
1130 1.6 mrg
1131 1.46 chs /* XXX for now, attach doesn't gain a ref */
1132 1.46 chs VREF(vp);
1133 1.6 mrg } else {
1134 1.6 mrg uobj = udv_attach((void *) &vp->v_rdev,
1135 1.48 thorpej (flags & MAP_SHARED) ? maxprot :
1136 1.48 thorpej (maxprot & ~VM_PROT_WRITE), foff, size);
1137 1.48 thorpej /*
1138 1.48 thorpej * XXX Some devices don't like to be mapped with
1139 1.48 thorpej * XXX PROT_EXEC, but we don't really have a
1140 1.48 thorpej * XXX better way of handling this, right now
1141 1.48 thorpej */
1142 1.48 thorpej if (uobj == NULL && (prot & PROT_EXEC) == 0) {
1143 1.48 thorpej maxprot &= ~VM_PROT_EXECUTE;
1144 1.48 thorpej uobj = udv_attach((void *) &vp->v_rdev,
1145 1.48 thorpej (flags & MAP_SHARED) ? maxprot :
1146 1.48 thorpej (maxprot & ~VM_PROT_WRITE), foff, size);
1147 1.48 thorpej }
1148 1.6 mrg advice = UVM_ADV_RANDOM;
1149 1.6 mrg }
1150 1.6 mrg
1151 1.6 mrg if (uobj == NULL)
1152 1.11 thorpej return((vp->v_type == VREG) ? ENOMEM : EINVAL);
1153 1.6 mrg
1154 1.6 mrg if ((flags & MAP_SHARED) == 0)
1155 1.6 mrg uvmflag |= UVM_FLAG_COPYONW;
1156 1.6 mrg }
1157 1.6 mrg
1158 1.6 mrg /*
1159 1.6 mrg * set up mapping flags
1160 1.6 mrg */
1161 1.1 mrg
1162 1.6 mrg uvmflag = UVM_MAPFLAG(prot, maxprot,
1163 1.1 mrg (flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY,
1164 1.1 mrg advice, uvmflag);
1165 1.1 mrg
1166 1.6 mrg /*
1167 1.6 mrg * do it!
1168 1.6 mrg */
1169 1.6 mrg
1170 1.44 thorpej retval = uvm_map(map, addr, size, uobj, foff, 0, uvmflag);
1171 1.6 mrg
1172 1.25 thorpej if (retval == KERN_SUCCESS) {
1173 1.25 thorpej /*
1174 1.25 thorpej * POSIX 1003.1b -- if our address space was configured
1175 1.25 thorpej * to lock all future mappings, wire the one we just made.
1176 1.25 thorpej */
1177 1.25 thorpej if (prot == VM_PROT_NONE) {
1178 1.25 thorpej /*
1179 1.25 thorpej * No more work to do in this case.
1180 1.25 thorpej */
1181 1.25 thorpej return (0);
1182 1.25 thorpej }
1183 1.25 thorpej
1184 1.25 thorpej vm_map_lock(map);
1185 1.25 thorpej
1186 1.25 thorpej if (map->flags & VM_MAP_WIREFUTURE) {
1187 1.25 thorpej if ((atop(size) + uvmexp.wired) > uvmexp.wiredmax
1188 1.25 thorpej #ifdef pmap_wired_count
1189 1.26 thorpej || (locklimit != 0 && (size +
1190 1.25 thorpej ptoa(pmap_wired_count(vm_map_pmap(map)))) >
1191 1.26 thorpej locklimit)
1192 1.25 thorpej #endif
1193 1.25 thorpej ) {
1194 1.25 thorpej retval = KERN_RESOURCE_SHORTAGE;
1195 1.41 enami vm_map_unlock(map);
1196 1.25 thorpej /* unmap the region! */
1197 1.25 thorpej (void) uvm_unmap(map, *addr, *addr + size);
1198 1.25 thorpej goto bad;
1199 1.25 thorpej }
1200 1.41 enami /*
1201 1.41 enami * uvm_map_pageable() always returns the map
1202 1.41 enami * unlocked.
1203 1.41 enami */
1204 1.25 thorpej retval = uvm_map_pageable(map, *addr, *addr + size,
1205 1.35 thorpej FALSE, UVM_LK_ENTER);
1206 1.25 thorpej if (retval != KERN_SUCCESS) {
1207 1.25 thorpej /* unmap the region! */
1208 1.25 thorpej (void) uvm_unmap(map, *addr, *addr + size);
1209 1.25 thorpej goto bad;
1210 1.25 thorpej }
1211 1.25 thorpej return (0);
1212 1.25 thorpej }
1213 1.25 thorpej
1214 1.25 thorpej vm_map_unlock(map);
1215 1.25 thorpej
1216 1.25 thorpej return (0);
1217 1.25 thorpej }
1218 1.6 mrg
1219 1.6 mrg /*
1220 1.6 mrg * errors: first detach from the uobj, if any.
1221 1.6 mrg */
1222 1.6 mrg
1223 1.6 mrg if (uobj)
1224 1.6 mrg uobj->pgops->pgo_detach(uobj);
1225 1.6 mrg
1226 1.25 thorpej bad:
1227 1.6 mrg switch (retval) {
1228 1.6 mrg case KERN_INVALID_ADDRESS:
1229 1.6 mrg case KERN_NO_SPACE:
1230 1.6 mrg return(ENOMEM);
1231 1.25 thorpej case KERN_RESOURCE_SHORTAGE:
1232 1.25 thorpej return (EAGAIN);
1233 1.6 mrg case KERN_PROTECTION_FAILURE:
1234 1.6 mrg return(EACCES);
1235 1.6 mrg }
1236 1.6 mrg return(EINVAL);
1237 1.1 mrg }
1238