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