Revert "Update the atime when mmap'ing a file"
[dragonfly.git] / sys / vm / vm_mmap.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
39 *
40 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
41 * $FreeBSD: src/sys/vm/vm_mmap.c,v 1.108.2.6 2002/07/02 20:06:19 dillon Exp $
ba39e2e0 42 * $DragonFly: src/sys/vm/vm_mmap.c,v 1.39 2007/04/30 07:18:57 dillon Exp $
984263bc
MD
43 */
44
45/*
46 * Mapped file (mmap) interface to VM
47 */
48
984263bc
MD
49#include <sys/param.h>
50#include <sys/kernel.h>
51#include <sys/systm.h>
52#include <sys/sysproto.h>
53#include <sys/filedesc.h>
a0ff68c9 54#include <sys/kern_syscall.h>
984263bc 55#include <sys/proc.h>
895c1f85 56#include <sys/priv.h>
984263bc
MD
57#include <sys/resource.h>
58#include <sys/resourcevar.h>
59#include <sys/vnode.h>
60#include <sys/fcntl.h>
61#include <sys/file.h>
62#include <sys/mman.h>
63#include <sys/conf.h>
64#include <sys/stat.h>
65#include <sys/vmmeter.h>
66#include <sys/sysctl.h>
67
68#include <vm/vm.h>
69#include <vm/vm_param.h>
70#include <sys/lock.h>
71#include <vm/pmap.h>
72#include <vm/vm_map.h>
73#include <vm/vm_object.h>
74#include <vm/vm_page.h>
75#include <vm/vm_pager.h>
76#include <vm/vm_pageout.h>
77#include <vm/vm_extern.h>
78#include <vm/vm_page.h>
79#include <vm/vm_kern.h>
80
dadab5e9 81#include <sys/file2.h>
654a39f0 82#include <sys/thread2.h>
dadab5e9 83
984263bc
MD
84static int max_proc_mmap;
85SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
568e6804
MD
86int vkernel_enable;
87SYSCTL_INT(_vm, OID_AUTO, vkernel_enable, CTLFLAG_RW, &vkernel_enable, 0, "");
984263bc
MD
88
89/*
90 * Set the maximum number of vm_map_entry structures per process. Roughly
91 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
92 * of our KVM malloc space still results in generous limits. We want a
93 * default that is good enough to prevent the kernel running out of resources
94 * if attacked from compromised user account but generous enough such that
95 * multi-threaded processes are not unduly inconvenienced.
96 */
97
1388df65 98static void vmmapentry_rsrc_init (void *);
ba39e2e0 99SYSINIT(vmmersrc, SI_BOOT1_POST, SI_ORDER_ANY, vmmapentry_rsrc_init, NULL)
984263bc
MD
100
101static void
57e43348 102vmmapentry_rsrc_init(void *dummy)
984263bc 103{
c439ad8f 104 max_proc_mmap = KvaSize / sizeof(struct vm_map_entry);
984263bc
MD
105 max_proc_mmap /= 100;
106}
107
108/* ARGSUSED */
109int
753fd850 110sys_sbrk(struct sbrk_args *uap)
984263bc 111{
984263bc
MD
112 /* Not yet implemented */
113 return (EOPNOTSUPP);
114}
115
41c20dac
MD
116/*
117 * sstk_args(int incr)
118 */
984263bc
MD
119/* ARGSUSED */
120int
753fd850 121sys_sstk(struct sstk_args *uap)
984263bc 122{
984263bc
MD
123 /* Not yet implemented */
124 return (EOPNOTSUPP);
125}
126
984263bc 127/*
41c20dac
MD
128 * mmap_args(void *addr, size_t len, int prot, int flags, int fd,
129 * long pad, off_t pos)
130 *
984263bc
MD
131 * Memory Map (mmap) system call. Note that the file offset
132 * and address are allowed to be NOT page aligned, though if
133 * the MAP_FIXED flag it set, both must have the same remainder
134 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
135 * page-aligned, the actual mapping starts at trunc_page(addr)
136 * and the return value is adjusted up by the page offset.
137 *
138 * Generally speaking, only character devices which are themselves
139 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
140 * there would be no cache coherency between a descriptor and a VM mapping
141 * both to the same character device.
142 *
143 * Block devices can be mmap'd no matter what they represent. Cache coherency
144 * is maintained as long as you do not write directly to the underlying
145 * character device.
146 */
984263bc
MD
147
148int
d3313941
MD
149kern_mmap(struct vmspace *vms, caddr_t uaddr, size_t ulen,
150 int uprot, int uflags, int fd, off_t upos, void **res)
984263bc 151{
dadab5e9
MD
152 struct thread *td = curthread;
153 struct proc *p = td->td_proc;
41c20dac 154 struct file *fp = NULL;
984263bc
MD
155 struct vnode *vp;
156 vm_offset_t addr;
157 vm_size_t size, pageoff;
158 vm_prot_t prot, maxprot;
159 void *handle;
160 int flags, error;
161 int disablexworkaround;
162 off_t pos;
984263bc
MD
163 vm_object_t obj;
164
dadab5e9
MD
165 KKASSERT(p);
166
a0ff68c9
DRJ
167 addr = (vm_offset_t) uaddr;
168 size = ulen;
169 prot = uprot & VM_PROT_ALL;
170 flags = uflags;
171 pos = upos;
984263bc
MD
172
173 /* make sure mapping fits into numeric range etc */
568e6804 174 if ((ssize_t) ulen < 0 || ((flags & MAP_ANON) && fd != -1))
984263bc
MD
175 return (EINVAL);
176
177 if (flags & MAP_STACK) {
a0ff68c9 178 if ((fd != -1) ||
984263bc
MD
179 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
180 return (EINVAL);
181 flags |= MAP_ANON;
182 pos = 0;
183 }
184
568e6804
MD
185 /*
186 * Virtual page tables cannot be used with MAP_STACK. Apart from
afeabdca 187 * it not making any sense, the aux union is used by both
568e6804
MD
188 * types.
189 *
190 * Because the virtual page table is stored in the backing object
191 * and might be updated by the kernel, the mapping must be R+W.
192 */
193 if (flags & MAP_VPAGETABLE) {
194 if (vkernel_enable == 0)
195 return (EOPNOTSUPP);
196 if (flags & MAP_STACK)
197 return (EINVAL);
198 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE))
199 return (EINVAL);
200 }
201
984263bc
MD
202 /*
203 * Align the file position to a page boundary,
204 * and save its page offset component.
205 */
206 pageoff = (pos & PAGE_MASK);
207 pos -= pageoff;
208
209 /* Adjust size for rounding (on both ends). */
210 size += pageoff; /* low end... */
211 size = (vm_size_t) round_page(size); /* hi end */
212
213 /*
214 * Check for illegal addresses. Watch out for address wrap... Note
215 * that VM_*_ADDRESS are not constants due to casts (argh).
216 */
217 if (flags & MAP_FIXED) {
218 /*
219 * The specified address must have the same remainder
220 * as the file offset taken modulo PAGE_SIZE, so it
221 * should be aligned after adjustment by pageoff.
222 */
223 addr -= pageoff;
224 if (addr & PAGE_MASK)
225 return (EINVAL);
226 /* Address range must be all in user VM space. */
88181b08 227 if (VM_MAX_USER_ADDRESS > 0 && addr + size > VM_MAX_USER_ADDRESS)
984263bc 228 return (EINVAL);
88181b08 229 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
984263bc 230 return (EINVAL);
984263bc
MD
231 if (addr + size < addr)
232 return (EINVAL);
459f5c1e
SS
233 } else if ((flags & MAP_TRYFIXED) == 0) {
234 /*
235 * XXX for non-fixed mappings where no hint is provided or
236 * the hint would fall in the potential heap space,
237 * place it after the end of the largest possible heap.
238 *
239 * There should really be a pmap call to determine a reasonable
240 * location.
241 */
242 if (addr == 0 ||
243 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
244 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz)))
245 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
984263bc 246 }
984263bc
MD
247
248 if (flags & MAP_ANON) {
249 /*
250 * Mapping blank space is trivial.
251 */
252 handle = NULL;
253 maxprot = VM_PROT_ALL;
254 pos = 0;
255 } else {
256 /*
257 * Mapping file, get fp for validation. Obtain vnode and make
258 * sure it is of appropriate type.
259 */
228b401d
MD
260 fp = holdfp(p->p_fd, fd, -1);
261 if (fp == NULL)
984263bc 262 return (EBADF);
228b401d
MD
263 if (fp->f_type != DTYPE_VNODE) {
264 error = EINVAL;
265 goto done;
266 }
984263bc
MD
267 /*
268 * POSIX shared-memory objects are defined to have
269 * kernel persistence, and are not defined to support
270 * read(2)/write(2) -- or even open(2). Thus, we can
271 * use MAP_ASYNC to trade on-disk coherence for speed.
272 * The shm_open(3) library routine turns on the FPOSIXSHM
273 * flag to request this behavior.
274 */
275 if (fp->f_flag & FPOSIXSHM)
276 flags |= MAP_NOSYNC;
277 vp = (struct vnode *) fp->f_data;
339fa1ed
MD
278
279 /*
280 * Validate the vnode for the operation.
281 */
282 switch(vp->v_type) {
283 case VREG:
984263bc
MD
284 /*
285 * Get the proper underlying object
286 */
228b401d
MD
287 if ((obj = vp->v_object) == NULL) {
288 error = EINVAL;
289 goto done;
290 }
339fa1ed
MD
291 KKASSERT((struct vnode *)obj->handle == vp);
292 break;
293 case VCHR:
294 /*
295 * Make sure a device has not been revoked.
296 * Mappability is handled by the device layer.
297 */
298 if (vp->v_rdev == NULL) {
299 error = EBADF;
300 goto done;
301 }
302 break;
303 default:
304 /*
305 * Nothing else is mappable.
306 */
307 error = EINVAL;
308 goto done;
984263bc
MD
309 }
310
984263bc
MD
311 /*
312 * XXX hack to handle use of /dev/zero to map anon memory (ala
313 * SunOS).
314 */
315 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
316 handle = NULL;
317 maxprot = VM_PROT_ALL;
318 flags |= MAP_ANON;
319 pos = 0;
320 } else {
321 /*
322 * cdevs does not provide private mappings of any kind.
323 */
324 /*
325 * However, for XIG X server to continue to work,
326 * we should allow the superuser to do it anyway.
327 * We only allow it at securelevel < 1.
328 * (Because the XIG X server writes directly to video
329 * memory via /dev/mem, it should never work at any
330 * other securelevel.
331 * XXX this will have to go
332 */
333 if (securelevel >= 1)
334 disablexworkaround = 1;
335 else
895c1f85 336 disablexworkaround = priv_check(td, PRIV_ROOT);
984263bc
MD
337 if (vp->v_type == VCHR && disablexworkaround &&
338 (flags & (MAP_PRIVATE|MAP_COPY))) {
339 error = EINVAL;
340 goto done;
341 }
342 /*
343 * Ensure that file and memory protections are
344 * compatible. Note that we only worry about
345 * writability if mapping is shared; in this case,
346 * current and max prot are dictated by the open file.
347 * XXX use the vnode instead? Problem is: what
348 * credentials do we use for determination? What if
349 * proc does a setuid?
350 */
351 maxprot = VM_PROT_EXECUTE; /* ??? */
352 if (fp->f_flag & FREAD) {
353 maxprot |= VM_PROT_READ;
354 } else if (prot & PROT_READ) {
355 error = EACCES;
356 goto done;
357 }
358 /*
359 * If we are sharing potential changes (either via
360 * MAP_SHARED or via the implicit sharing of character
361 * device mappings), and we are trying to get write
362 * permission although we opened it without asking
363 * for it, bail out. Check for superuser, only if
364 * we're at securelevel < 1, to allow the XIG X server
365 * to continue to work.
366 */
367
368 if ((flags & MAP_SHARED) != 0 ||
369 (vp->v_type == VCHR && disablexworkaround)) {
370 if ((fp->f_flag & FWRITE) != 0) {
371 struct vattr va;
87de5057 372 if ((error = VOP_GETATTR(vp, &va))) {
984263bc
MD
373 goto done;
374 }
375 if ((va.va_flags &
376 (IMMUTABLE|APPEND)) == 0) {
377 maxprot |= VM_PROT_WRITE;
378 } else if (prot & PROT_WRITE) {
379 error = EPERM;
380 goto done;
381 }
382 } else if ((prot & PROT_WRITE) != 0) {
383 error = EACCES;
384 goto done;
385 }
386 } else {
387 maxprot |= VM_PROT_WRITE;
388 }
389 handle = (void *)vp;
390 }
391 }
392
393 /*
394 * Do not allow more then a certain number of vm_map_entry structures
395 * per process. Scale with the number of rforks sharing the map
396 * to make the limit reasonable for threads.
397 */
398 if (max_proc_mmap &&
e3161323 399 vms->vm_map.nentries >= max_proc_mmap * vms->vm_sysref.refcnt) {
984263bc
MD
400 error = ENOMEM;
401 goto done;
402 }
403
404 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
405 flags, handle, pos);
406 if (error == 0)
a0ff68c9 407 *res = (void *)(addr + pageoff);
984263bc
MD
408done:
409 if (fp)
9f87144f 410 fdrop(fp);
984263bc
MD
411 return (error);
412}
413
984263bc 414int
753fd850 415sys_mmap(struct mmap_args *uap)
984263bc 416{
a0ff68c9 417 int error;
984263bc 418
d3313941
MD
419 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len,
420 uap->prot, uap->flags,
421 uap->fd, uap->pos, &uap->sysmsg_resultp);
984263bc 422
a0ff68c9 423 return (error);
984263bc 424}
984263bc 425
41c20dac
MD
426/*
427 * msync_args(void *addr, int len, int flags)
428 */
984263bc 429int
753fd850 430sys_msync(struct msync_args *uap)
984263bc 431{
41c20dac 432 struct proc *p = curproc;
984263bc
MD
433 vm_offset_t addr;
434 vm_size_t size, pageoff;
435 int flags;
436 vm_map_t map;
437 int rv;
438
439 addr = (vm_offset_t) uap->addr;
440 size = uap->len;
441 flags = uap->flags;
442
443 pageoff = (addr & PAGE_MASK);
444 addr -= pageoff;
445 size += pageoff;
446 size = (vm_size_t) round_page(size);
447 if (addr + size < addr)
448 return(EINVAL);
449
450 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
451 return (EINVAL);
452
453 map = &p->p_vmspace->vm_map;
454
455 /*
456 * XXX Gak! If size is zero we are supposed to sync "all modified
457 * pages with the region containing addr". Unfortunately, we don't
458 * really keep track of individual mmaps so we approximate by flushing
459 * the range of the map entry containing addr. This can be incorrect
460 * if the region splits or is coalesced with a neighbor.
461 */
462 if (size == 0) {
463 vm_map_entry_t entry;
464
465 vm_map_lock_read(map);
466 rv = vm_map_lookup_entry(map, addr, &entry);
467 vm_map_unlock_read(map);
468 if (rv == FALSE)
469 return (EINVAL);
470 addr = entry->start;
471 size = entry->end - entry->start;
472 }
473
474 /*
475 * Clean the pages and interpret the return value.
476 */
477 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
478 (flags & MS_INVALIDATE) != 0);
479
480 switch (rv) {
481 case KERN_SUCCESS:
482 break;
483 case KERN_INVALID_ADDRESS:
484 return (EINVAL); /* Sun returns ENOMEM? */
485 case KERN_FAILURE:
486 return (EIO);
487 default:
488 return (EINVAL);
489 }
490
491 return (0);
492}
493
41c20dac
MD
494/*
495 * munmap_args(void *addr, size_t len)
496 */
984263bc 497int
753fd850 498sys_munmap(struct munmap_args *uap)
984263bc 499{
41c20dac 500 struct proc *p = curproc;
984263bc
MD
501 vm_offset_t addr;
502 vm_size_t size, pageoff;
503 vm_map_t map;
504
505 addr = (vm_offset_t) uap->addr;
506 size = uap->len;
507
508 pageoff = (addr & PAGE_MASK);
509 addr -= pageoff;
510 size += pageoff;
511 size = (vm_size_t) round_page(size);
512 if (addr + size < addr)
513 return(EINVAL);
514
515 if (size == 0)
516 return (0);
517
518 /*
519 * Check for illegal addresses. Watch out for address wrap... Note
520 * that VM_*_ADDRESS are not constants due to casts (argh).
521 */
88181b08 522 if (VM_MAX_USER_ADDRESS > 0 && addr + size > VM_MAX_USER_ADDRESS)
984263bc 523 return (EINVAL);
88181b08 524 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
984263bc 525 return (EINVAL);
984263bc
MD
526 map = &p->p_vmspace->vm_map;
527 /*
528 * Make sure entire range is allocated.
529 */
530 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
531 return (EINVAL);
532 /* returns nothing but KERN_SUCCESS anyway */
418ff780 533 vm_map_remove(map, addr, addr + size);
984263bc
MD
534 return (0);
535}
536
41c20dac
MD
537/*
538 * mprotect_args(const void *addr, size_t len, int prot)
539 */
984263bc 540int
753fd850 541sys_mprotect(struct mprotect_args *uap)
984263bc 542{
41c20dac 543 struct proc *p = curproc;
984263bc
MD
544 vm_offset_t addr;
545 vm_size_t size, pageoff;
5f910b2f 546 vm_prot_t prot;
984263bc
MD
547
548 addr = (vm_offset_t) uap->addr;
549 size = uap->len;
550 prot = uap->prot & VM_PROT_ALL;
551#if defined(VM_PROT_READ_IS_EXEC)
552 if (prot & VM_PROT_READ)
553 prot |= VM_PROT_EXECUTE;
554#endif
555
556 pageoff = (addr & PAGE_MASK);
557 addr -= pageoff;
558 size += pageoff;
559 size = (vm_size_t) round_page(size);
560 if (addr + size < addr)
561 return(EINVAL);
562
563 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
564 FALSE)) {
565 case KERN_SUCCESS:
566 return (0);
567 case KERN_PROTECTION_FAILURE:
568 return (EACCES);
569 }
570 return (EINVAL);
571}
572
41c20dac
MD
573/*
574 * minherit_args(void *addr, size_t len, int inherit)
575 */
984263bc 576int
753fd850 577sys_minherit(struct minherit_args *uap)
984263bc 578{
41c20dac 579 struct proc *p = curproc;
984263bc
MD
580 vm_offset_t addr;
581 vm_size_t size, pageoff;
5f910b2f 582 vm_inherit_t inherit;
984263bc
MD
583
584 addr = (vm_offset_t)uap->addr;
585 size = uap->len;
586 inherit = uap->inherit;
587
588 pageoff = (addr & PAGE_MASK);
589 addr -= pageoff;
590 size += pageoff;
591 size = (vm_size_t) round_page(size);
592 if (addr + size < addr)
593 return(EINVAL);
594
595 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size,
596 inherit)) {
597 case KERN_SUCCESS:
598 return (0);
599 case KERN_PROTECTION_FAILURE:
600 return (EACCES);
601 }
602 return (EINVAL);
603}
604
41c20dac
MD
605/*
606 * madvise_args(void *addr, size_t len, int behav)
607 */
984263bc
MD
608/* ARGSUSED */
609int
753fd850 610sys_madvise(struct madvise_args *uap)
984263bc 611{
41c20dac 612 struct proc *p = curproc;
984263bc
MD
613 vm_offset_t start, end;
614
615 /*
616 * Check for illegal behavior
617 */
afeabdca 618 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END)
984263bc
MD
619 return (EINVAL);
620 /*
621 * Check for illegal addresses. Watch out for address wrap... Note
622 * that VM_*_ADDRESS are not constants due to casts (argh).
623 */
88181b08
MD
624 if (VM_MAX_USER_ADDRESS > 0 &&
625 ((vm_offset_t) uap->addr + uap->len) > VM_MAX_USER_ADDRESS)
984263bc 626 return (EINVAL);
88181b08 627 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
984263bc 628 return (EINVAL);
984263bc
MD
629 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
630 return (EINVAL);
631
632 /*
633 * Since this routine is only advisory, we default to conservative
634 * behavior.
635 */
636 start = trunc_page((vm_offset_t) uap->addr);
637 end = round_page((vm_offset_t) uap->addr + uap->len);
638
afeabdca
MD
639 return (vm_map_madvise(&p->p_vmspace->vm_map, start, end,
640 uap->behav, 0));
641}
642
643/*
644 * mcontrol_args(void *addr, size_t len, int behav, off_t value)
645 */
646/* ARGSUSED */
647int
648sys_mcontrol(struct mcontrol_args *uap)
649{
650 struct proc *p = curproc;
651 vm_offset_t start, end;
652
653 /*
654 * Check for illegal behavior
655 */
656 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
984263bc 657 return (EINVAL);
afeabdca
MD
658 /*
659 * Check for illegal addresses. Watch out for address wrap... Note
660 * that VM_*_ADDRESS are not constants due to casts (argh).
661 */
88181b08
MD
662 if (VM_MAX_USER_ADDRESS > 0 &&
663 ((vm_offset_t) uap->addr + uap->len) > VM_MAX_USER_ADDRESS)
afeabdca 664 return (EINVAL);
88181b08 665 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
afeabdca 666 return (EINVAL);
afeabdca
MD
667 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
668 return (EINVAL);
669
670 /*
671 * Since this routine is only advisory, we default to conservative
672 * behavior.
673 */
674 start = trunc_page((vm_offset_t) uap->addr);
675 end = round_page((vm_offset_t) uap->addr + uap->len);
676
677 return (vm_map_madvise(&p->p_vmspace->vm_map, start, end,
678 uap->behav, uap->value));
984263bc
MD
679}
680
afeabdca 681
41c20dac
MD
682/*
683 * mincore_args(const void *addr, size_t len, char *vec)
684 */
984263bc
MD
685/* ARGSUSED */
686int
753fd850 687sys_mincore(struct mincore_args *uap)
984263bc 688{
41c20dac 689 struct proc *p = curproc;
984263bc
MD
690 vm_offset_t addr, first_addr;
691 vm_offset_t end, cend;
692 pmap_t pmap;
693 vm_map_t map;
694 char *vec;
695 int error;
696 int vecindex, lastvecindex;
5f910b2f 697 vm_map_entry_t current;
984263bc
MD
698 vm_map_entry_t entry;
699 int mincoreinfo;
700 unsigned int timestamp;
701
702 /*
703 * Make sure that the addresses presented are valid for user
704 * mode.
705 */
706 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
707 end = addr + (vm_size_t)round_page(uap->len);
88181b08 708 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS)
984263bc
MD
709 return (EINVAL);
710 if (end < addr)
711 return (EINVAL);
712
713 /*
714 * Address of byte vector
715 */
716 vec = uap->vec;
717
718 map = &p->p_vmspace->vm_map;
719 pmap = vmspace_pmap(p->p_vmspace);
720
721 vm_map_lock_read(map);
722RestartScan:
723 timestamp = map->timestamp;
724
725 if (!vm_map_lookup_entry(map, addr, &entry))
726 entry = entry->next;
727
728 /*
729 * Do this on a map entry basis so that if the pages are not
730 * in the current processes address space, we can easily look
731 * up the pages elsewhere.
732 */
733 lastvecindex = -1;
734 for(current = entry;
735 (current != &map->header) && (current->start < end);
736 current = current->next) {
737
738 /*
739 * ignore submaps (for now) or null objects
740 */
1b874851
MD
741 if (current->maptype != VM_MAPTYPE_NORMAL &&
742 current->maptype != VM_MAPTYPE_VPAGETABLE) {
743 continue;
744 }
745 if (current->object.vm_object == NULL)
984263bc
MD
746 continue;
747
748 /*
749 * limit this scan to the current map entry and the
750 * limits for the mincore call
751 */
752 if (addr < current->start)
753 addr = current->start;
754 cend = current->end;
755 if (cend > end)
756 cend = end;
757
758 /*
759 * scan this entry one page at a time
760 */
06ecca5a 761 while (addr < cend) {
984263bc
MD
762 /*
763 * Check pmap first, it is likely faster, also
764 * it can provide info as to whether we are the
765 * one referencing or modifying the page.
1b874851
MD
766 *
767 * If we have to check the VM object, only mess
768 * around with normal maps. Do not mess around
769 * with virtual page tables (XXX).
984263bc
MD
770 */
771 mincoreinfo = pmap_mincore(pmap, addr);
1b874851
MD
772 if (mincoreinfo == 0 &&
773 current->maptype == VM_MAPTYPE_NORMAL) {
984263bc
MD
774 vm_pindex_t pindex;
775 vm_ooffset_t offset;
776 vm_page_t m;
06ecca5a 777
984263bc
MD
778 /*
779 * calculate the page index into the object
780 */
781 offset = current->offset + (addr - current->start);
782 pindex = OFF_TO_IDX(offset);
06ecca5a 783
984263bc 784 /*
06ecca5a
MD
785 * if the page is resident, then gather
786 * information about it. spl protection is
787 * required to maintain the object
788 * association. And XXX what if the page is
789 * busy? What's the deal with that?
984263bc 790 */
654a39f0 791 crit_enter();
06ecca5a
MD
792 m = vm_page_lookup(current->object.vm_object,
793 pindex);
2ff71562 794 if (m && m->valid) {
984263bc
MD
795 mincoreinfo = MINCORE_INCORE;
796 if (m->dirty ||
797 pmap_is_modified(m))
798 mincoreinfo |= MINCORE_MODIFIED_OTHER;
799 if ((m->flags & PG_REFERENCED) ||
800 pmap_ts_referenced(m)) {
801 vm_page_flag_set(m, PG_REFERENCED);
802 mincoreinfo |= MINCORE_REFERENCED_OTHER;
803 }
804 }
654a39f0 805 crit_exit();
984263bc
MD
806 }
807
808 /*
809 * subyte may page fault. In case it needs to modify
810 * the map, we release the lock.
811 */
812 vm_map_unlock_read(map);
813
814 /*
815 * calculate index into user supplied byte vector
816 */
817 vecindex = OFF_TO_IDX(addr - first_addr);
818
819 /*
820 * If we have skipped map entries, we need to make sure that
821 * the byte vector is zeroed for those skipped entries.
822 */
823 while((lastvecindex + 1) < vecindex) {
824 error = subyte( vec + lastvecindex, 0);
825 if (error) {
826 return (EFAULT);
827 }
828 ++lastvecindex;
829 }
830
831 /*
832 * Pass the page information to the user
833 */
834 error = subyte( vec + vecindex, mincoreinfo);
835 if (error) {
836 return (EFAULT);
837 }
838
839 /*
840 * If the map has changed, due to the subyte, the previous
841 * output may be invalid.
842 */
843 vm_map_lock_read(map);
844 if (timestamp != map->timestamp)
845 goto RestartScan;
846
847 lastvecindex = vecindex;
848 addr += PAGE_SIZE;
849 }
850 }
851
852 /*
853 * subyte may page fault. In case it needs to modify
854 * the map, we release the lock.
855 */
856 vm_map_unlock_read(map);
857
858 /*
859 * Zero the last entries in the byte vector.
860 */
861 vecindex = OFF_TO_IDX(end - first_addr);
862 while((lastvecindex + 1) < vecindex) {
863 error = subyte( vec + lastvecindex, 0);
864 if (error) {
865 return (EFAULT);
866 }
867 ++lastvecindex;
868 }
869
870 /*
871 * If the map has changed, due to the subyte, the previous
872 * output may be invalid.
873 */
874 vm_map_lock_read(map);
875 if (timestamp != map->timestamp)
876 goto RestartScan;
877 vm_map_unlock_read(map);
878
879 return (0);
880}
881
41c20dac
MD
882/*
883 * mlock_args(const void *addr, size_t len)
884 */
984263bc 885int
753fd850 886sys_mlock(struct mlock_args *uap)
984263bc
MD
887{
888 vm_offset_t addr;
889 vm_size_t size, pageoff;
890 int error;
41c20dac 891 struct proc *p = curproc;
984263bc
MD
892
893 addr = (vm_offset_t) uap->addr;
894 size = uap->len;
895
896 pageoff = (addr & PAGE_MASK);
897 addr -= pageoff;
898 size += pageoff;
899 size = (vm_size_t) round_page(size);
900
901 /* disable wrap around */
902 if (addr + size < addr)
903 return (EINVAL);
904
12e4aaff 905 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired)
984263bc
MD
906 return (EAGAIN);
907
908#ifdef pmap_wired_count
909 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
910 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
911 return (ENOMEM);
912#else
895c1f85 913 error = priv_check_cred(p->p_ucred, PRIV_ROOT, 0);
984263bc
MD
914 if (error)
915 return (error);
916#endif
917
cde87949 918 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
984263bc
MD
919 return (error == KERN_SUCCESS ? 0 : ENOMEM);
920}
921
41c20dac
MD
922/*
923 * mlockall_args(int how)
924 */
984263bc 925int
753fd850 926sys_mlockall(struct mlockall_args *uap)
984263bc
MD
927{
928 return 0;
929}
930
41c20dac 931/*
efbaff5d 932 * munlockall_args(void)
41c20dac 933 */
984263bc 934int
753fd850 935sys_munlockall(struct munlockall_args *uap)
984263bc
MD
936{
937 return 0;
938}
939
41c20dac
MD
940/*
941 * munlock_args(const void *addr, size_t len)
942 */
984263bc 943int
753fd850 944sys_munlock(struct munlock_args *uap)
984263bc 945{
dadab5e9
MD
946 struct thread *td = curthread;
947 struct proc *p = td->td_proc;
984263bc
MD
948 vm_offset_t addr;
949 vm_size_t size, pageoff;
950 int error;
951
952 addr = (vm_offset_t) uap->addr;
953 size = uap->len;
954
955 pageoff = (addr & PAGE_MASK);
956 addr -= pageoff;
957 size += pageoff;
958 size = (vm_size_t) round_page(size);
959
960 /* disable wrap around */
961 if (addr + size < addr)
962 return (EINVAL);
963
964#ifndef pmap_wired_count
895c1f85 965 error = priv_check(td, PRIV_ROOT);
984263bc
MD
966 if (error)
967 return (error);
968#endif
969
cde87949 970 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
984263bc
MD
971 return (error == KERN_SUCCESS ? 0 : ENOMEM);
972}
973
974/*
975 * Internal version of mmap.
976 * Currently used by mmap, exec, and sys5 shared memory.
977 * Handle is either a vnode pointer or NULL for MAP_ANON.
978 */
979int
980vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
981 vm_prot_t maxprot, int flags,
982 void *handle,
983 vm_ooffset_t foff)
984{
985 boolean_t fitit;
986 vm_object_t object;
a6e41612 987 struct vnode *vp;
349433c9 988 struct thread *td = curthread;
d3313941 989 struct proc *p;
984263bc
MD
990 objtype_t type;
991 int rv = KERN_SUCCESS;
57f7b636 992 off_t objsize;
984263bc 993 int docow;
984263bc
MD
994
995 if (size == 0)
996 return (0);
997
998 objsize = size = round_page(size);
999
d3313941
MD
1000 /*
1001 * XXX messy code, fixme
1002 */
1003 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) {
1004 if (map->size + size > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
1005 return(ENOMEM);
984263bc
MD
1006 }
1007
1008 /*
1009 * We currently can only deal with page aligned file offsets.
1010 * The check is here rather than in the syscall because the
1011 * kernel calls this function internally for other mmaping
1012 * operations (such as in exec) and non-aligned offsets will
1013 * cause pmap inconsistencies...so we want to be sure to
1014 * disallow this in all cases.
1015 */
1016 if (foff & PAGE_MASK)
1017 return (EINVAL);
1018
1019 if ((flags & MAP_FIXED) == 0) {
1020 fitit = TRUE;
1021 *addr = round_page(*addr);
1022 } else {
1023 if (*addr != trunc_page(*addr))
1024 return (EINVAL);
1025 fitit = FALSE;
418ff780 1026 vm_map_remove(map, *addr, *addr + size);
984263bc
MD
1027 }
1028
1029 /*
1030 * Lookup/allocate object.
1031 */
1032 if (flags & MAP_ANON) {
1033 type = OBJT_DEFAULT;
1034 /*
1035 * Unnamed anonymous regions always start at 0.
1036 */
a6e41612 1037 if (handle == NULL)
984263bc 1038 foff = 0;
a6e41612 1039 vp = NULL;
984263bc 1040 } else {
a6e41612 1041 vp = (struct vnode *)handle;
984263bc
MD
1042 if (vp->v_type == VCHR) {
1043 type = OBJT_DEVICE;
1044 handle = (void *)(intptr_t)vp->v_rdev;
1045 } else {
f9cae863 1046 struct vattr vat;
984263bc
MD
1047 int error;
1048
87de5057 1049 error = VOP_GETATTR(vp, &vat);
984263bc
MD
1050 if (error)
1051 return (error);
57f7b636 1052 objsize = vat.va_size;
984263bc
MD
1053 type = OBJT_VNODE;
1054 /*
1055 * if it is a regular file without any references
1056 * we do not need to sync it.
1057 */
1058 if (vp->v_type == VREG && vat.va_nlink == 0) {
1059 flags |= MAP_NOSYNC;
1060 }
1061 }
1062 }
1063
1064 if (handle == NULL) {
1065 object = NULL;
1066 docow = 0;
1067 } else {
57f7b636 1068 object = vm_pager_allocate(type, handle, objsize, prot, foff);
984263bc
MD
1069 if (object == NULL)
1070 return (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1071 docow = MAP_PREFAULT_PARTIAL;
1072 }
1073
1074 /*
1075 * Force device mappings to be shared.
1076 */
1077 if (type == OBJT_DEVICE || type == OBJT_PHYS) {
1078 flags &= ~(MAP_PRIVATE|MAP_COPY);
1079 flags |= MAP_SHARED;
1080 }
1081
1082 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1083 docow |= MAP_COPY_ON_WRITE;
1084 if (flags & MAP_NOSYNC)
1085 docow |= MAP_DISABLE_SYNCER;
1086 if (flags & MAP_NOCORE)
1087 docow |= MAP_DISABLE_COREDUMP;
1088
1089#if defined(VM_PROT_READ_IS_EXEC)
1090 if (prot & VM_PROT_READ)
1091 prot |= VM_PROT_EXECUTE;
1092
1093 if (maxprot & VM_PROT_READ)
1094 maxprot |= VM_PROT_EXECUTE;
1095#endif
1096
1097 if (fitit) {
1098 *addr = pmap_addr_hint(object, *addr, size);
1099 }
1100
568e6804
MD
1101 /*
1102 * Stack mappings need special attention. Mappings that use virtual
1103 * page tables will default to storing the page table at offset 0.
1104 */
1b874851 1105 if (flags & MAP_STACK) {
568e6804
MD
1106 rv = vm_map_stack (map, *addr, size, prot, maxprot, docow);
1107 } else if (flags & MAP_VPAGETABLE) {
1108 rv = vm_map_find(map, object, foff, addr, size, fitit,
1109 VM_MAPTYPE_VPAGETABLE, prot, maxprot, docow);
1b874851 1110 } else {
984263bc 1111 rv = vm_map_find(map, object, foff, addr, size, fitit,
1b874851
MD
1112 VM_MAPTYPE_NORMAL, prot, maxprot, docow);
1113 }
984263bc
MD
1114
1115 if (rv != KERN_SUCCESS) {
1116 /*
1117 * Lose the object reference. Will destroy the
1118 * object if it's an unnamed anonymous mapping
1119 * or named anonymous without other references.
1120 */
1121 vm_object_deallocate(object);
1122 goto out;
1123 }
1124
1125 /*
1126 * Shared memory is also shared with children.
1127 */
1128 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1129 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1130 if (rv != KERN_SUCCESS) {
418ff780 1131 vm_map_remove(map, *addr, *addr + size);
984263bc
MD
1132 goto out;
1133 }
1134 }
349433c9
MD
1135
1136 /*
1137 * Set the access time on the vnode
1138 */
a6e41612
MD
1139 if (vp != NULL)
1140 vn_mark_atime(vp, td);
984263bc
MD
1141out:
1142 switch (rv) {
1143 case KERN_SUCCESS:
1144 return (0);
1145 case KERN_INVALID_ADDRESS:
1146 case KERN_NO_SPACE:
1147 return (ENOMEM);
1148 case KERN_PROTECTION_FAILURE:
1149 return (EACCES);
1150 default:
1151 return (EINVAL);
1152 }
1153}