kernel - use new td_ucred in numerous places
[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
3919ced0
MD
108/*
109 * MPSAFE
110 */
984263bc 111int
753fd850 112sys_sbrk(struct sbrk_args *uap)
984263bc 113{
984263bc
MD
114 /* Not yet implemented */
115 return (EOPNOTSUPP);
116}
117
41c20dac
MD
118/*
119 * sstk_args(int incr)
3919ced0
MD
120 *
121 * MPSAFE
41c20dac 122 */
984263bc 123int
753fd850 124sys_sstk(struct sstk_args *uap)
984263bc 125{
984263bc
MD
126 /* Not yet implemented */
127 return (EOPNOTSUPP);
128}
129
984263bc 130/*
41c20dac
MD
131 * mmap_args(void *addr, size_t len, int prot, int flags, int fd,
132 * long pad, off_t pos)
133 *
984263bc
MD
134 * Memory Map (mmap) system call. Note that the file offset
135 * and address are allowed to be NOT page aligned, though if
136 * the MAP_FIXED flag it set, both must have the same remainder
137 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
138 * page-aligned, the actual mapping starts at trunc_page(addr)
139 * and the return value is adjusted up by the page offset.
140 *
141 * Generally speaking, only character devices which are themselves
142 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
143 * there would be no cache coherency between a descriptor and a VM mapping
144 * both to the same character device.
145 *
146 * Block devices can be mmap'd no matter what they represent. Cache coherency
147 * is maintained as long as you do not write directly to the underlying
148 * character device.
149 */
984263bc
MD
150
151int
d3313941
MD
152kern_mmap(struct vmspace *vms, caddr_t uaddr, size_t ulen,
153 int uprot, int uflags, int fd, off_t upos, void **res)
984263bc 154{
dadab5e9
MD
155 struct thread *td = curthread;
156 struct proc *p = td->td_proc;
41c20dac 157 struct file *fp = NULL;
984263bc
MD
158 struct vnode *vp;
159 vm_offset_t addr;
e54488bb 160 vm_offset_t tmpaddr;
984263bc
MD
161 vm_size_t size, pageoff;
162 vm_prot_t prot, maxprot;
163 void *handle;
164 int flags, error;
165 int disablexworkaround;
166 off_t pos;
984263bc
MD
167 vm_object_t obj;
168
dadab5e9
MD
169 KKASSERT(p);
170
a0ff68c9
DRJ
171 addr = (vm_offset_t) uaddr;
172 size = ulen;
173 prot = uprot & VM_PROT_ALL;
174 flags = uflags;
175 pos = upos;
984263bc 176
e54488bb
MD
177 /*
178 * Make sure mapping fits into numeric range etc.
179 *
180 * NOTE: We support the full unsigned range for size now.
181 */
182 if (((flags & MAP_ANON) && fd != -1))
984263bc
MD
183 return (EINVAL);
184
185 if (flags & MAP_STACK) {
a0ff68c9 186 if ((fd != -1) ||
984263bc
MD
187 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
188 return (EINVAL);
189 flags |= MAP_ANON;
190 pos = 0;
191 }
192
193 /*
568e6804 194 * Virtual page tables cannot be used with MAP_STACK. Apart from
afeabdca 195 * it not making any sense, the aux union is used by both
568e6804
MD
196 * types.
197 *
198 * Because the virtual page table is stored in the backing object
199 * and might be updated by the kernel, the mapping must be R+W.
200 */
201 if (flags & MAP_VPAGETABLE) {
202 if (vkernel_enable == 0)
203 return (EOPNOTSUPP);
204 if (flags & MAP_STACK)
205 return (EINVAL);
206 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE))
207 return (EINVAL);
208 }
209
210 /*
984263bc
MD
211 * Align the file position to a page boundary,
212 * and save its page offset component.
213 */
214 pageoff = (pos & PAGE_MASK);
215 pos -= pageoff;
216
217 /* Adjust size for rounding (on both ends). */
218 size += pageoff; /* low end... */
219 size = (vm_size_t) round_page(size); /* hi end */
e54488bb
MD
220 if (size < ulen) /* wrap */
221 return(EINVAL);
984263bc
MD
222
223 /*
224 * Check for illegal addresses. Watch out for address wrap... Note
225 * that VM_*_ADDRESS are not constants due to casts (argh).
226 */
c809941b 227 if (flags & (MAP_FIXED | MAP_TRYFIXED)) {
984263bc
MD
228 /*
229 * The specified address must have the same remainder
230 * as the file offset taken modulo PAGE_SIZE, so it
231 * should be aligned after adjustment by pageoff.
232 */
233 addr -= pageoff;
234 if (addr & PAGE_MASK)
235 return (EINVAL);
e54488bb
MD
236
237 /*
238 * Address range must be all in user VM space and not wrap.
239 */
240 tmpaddr = addr + size;
241 if (tmpaddr < addr)
242 return (EINVAL);
243 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
984263bc 244 return (EINVAL);
88181b08 245 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
984263bc 246 return (EINVAL);
c809941b 247 } else {
459f5c1e 248 /*
c809941b
MD
249 * Set a reasonable start point for the hint if it was
250 * not specified or if it falls within the heap space.
251 * Hinted mmap()s do not allocate out of the heap space.
459f5c1e
SS
252 */
253 if (addr == 0 ||
254 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
255 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz)))
256 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
984263bc 257 }
984263bc
MD
258
259 if (flags & MAP_ANON) {
260 /*
261 * Mapping blank space is trivial.
262 */
263 handle = NULL;
264 maxprot = VM_PROT_ALL;
265 pos = 0;
266 } else {
267 /*
268 * Mapping file, get fp for validation. Obtain vnode and make
269 * sure it is of appropriate type.
270 */
228b401d
MD
271 fp = holdfp(p->p_fd, fd, -1);
272 if (fp == NULL)
984263bc 273 return (EBADF);
228b401d
MD
274 if (fp->f_type != DTYPE_VNODE) {
275 error = EINVAL;
276 goto done;
277 }
984263bc
MD
278 /*
279 * POSIX shared-memory objects are defined to have
280 * kernel persistence, and are not defined to support
281 * read(2)/write(2) -- or even open(2). Thus, we can
282 * use MAP_ASYNC to trade on-disk coherence for speed.
283 * The shm_open(3) library routine turns on the FPOSIXSHM
284 * flag to request this behavior.
285 */
286 if (fp->f_flag & FPOSIXSHM)
287 flags |= MAP_NOSYNC;
288 vp = (struct vnode *) fp->f_data;
339fa1ed
MD
289
290 /*
291 * Validate the vnode for the operation.
292 */
293 switch(vp->v_type) {
294 case VREG:
984263bc
MD
295 /*
296 * Get the proper underlying object
297 */
228b401d
MD
298 if ((obj = vp->v_object) == NULL) {
299 error = EINVAL;
300 goto done;
301 }
339fa1ed
MD
302 KKASSERT((struct vnode *)obj->handle == vp);
303 break;
304 case VCHR:
305 /*
306 * Make sure a device has not been revoked.
307 * Mappability is handled by the device layer.
308 */
309 if (vp->v_rdev == NULL) {
310 error = EBADF;
311 goto done;
312 }
313 break;
314 default:
315 /*
316 * Nothing else is mappable.
317 */
318 error = EINVAL;
319 goto done;
984263bc
MD
320 }
321
322 /*
984263bc
MD
323 * XXX hack to handle use of /dev/zero to map anon memory (ala
324 * SunOS).
325 */
326 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
327 handle = NULL;
328 maxprot = VM_PROT_ALL;
329 flags |= MAP_ANON;
330 pos = 0;
331 } else {
332 /*
333 * cdevs does not provide private mappings of any kind.
334 */
335 /*
336 * However, for XIG X server to continue to work,
337 * we should allow the superuser to do it anyway.
338 * We only allow it at securelevel < 1.
339 * (Because the XIG X server writes directly to video
340 * memory via /dev/mem, it should never work at any
341 * other securelevel.
342 * XXX this will have to go
343 */
344 if (securelevel >= 1)
345 disablexworkaround = 1;
346 else
895c1f85 347 disablexworkaround = priv_check(td, PRIV_ROOT);
984263bc
MD
348 if (vp->v_type == VCHR && disablexworkaround &&
349 (flags & (MAP_PRIVATE|MAP_COPY))) {
350 error = EINVAL;
351 goto done;
352 }
353 /*
354 * Ensure that file and memory protections are
355 * compatible. Note that we only worry about
356 * writability if mapping is shared; in this case,
357 * current and max prot are dictated by the open file.
358 * XXX use the vnode instead? Problem is: what
359 * credentials do we use for determination? What if
360 * proc does a setuid?
361 */
362 maxprot = VM_PROT_EXECUTE; /* ??? */
363 if (fp->f_flag & FREAD) {
364 maxprot |= VM_PROT_READ;
365 } else if (prot & PROT_READ) {
366 error = EACCES;
367 goto done;
368 }
369 /*
370 * If we are sharing potential changes (either via
371 * MAP_SHARED or via the implicit sharing of character
372 * device mappings), and we are trying to get write
373 * permission although we opened it without asking
374 * for it, bail out. Check for superuser, only if
375 * we're at securelevel < 1, to allow the XIG X server
376 * to continue to work.
377 */
378
379 if ((flags & MAP_SHARED) != 0 ||
380 (vp->v_type == VCHR && disablexworkaround)) {
381 if ((fp->f_flag & FWRITE) != 0) {
382 struct vattr va;
87de5057 383 if ((error = VOP_GETATTR(vp, &va))) {
984263bc
MD
384 goto done;
385 }
386 if ((va.va_flags &
387 (IMMUTABLE|APPEND)) == 0) {
388 maxprot |= VM_PROT_WRITE;
389 } else if (prot & PROT_WRITE) {
390 error = EPERM;
391 goto done;
392 }
393 } else if ((prot & PROT_WRITE) != 0) {
394 error = EACCES;
395 goto done;
396 }
397 } else {
398 maxprot |= VM_PROT_WRITE;
399 }
400 handle = (void *)vp;
401 }
402 }
403
404 /*
405 * Do not allow more then a certain number of vm_map_entry structures
406 * per process. Scale with the number of rforks sharing the map
407 * to make the limit reasonable for threads.
408 */
409 if (max_proc_mmap &&
e3161323 410 vms->vm_map.nentries >= max_proc_mmap * vms->vm_sysref.refcnt) {
984263bc
MD
411 error = ENOMEM;
412 goto done;
413 }
414
415 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
e54488bb 416 flags, handle, pos);
984263bc 417 if (error == 0)
a0ff68c9 418 *res = (void *)(addr + pageoff);
984263bc
MD
419done:
420 if (fp)
9f87144f 421 fdrop(fp);
984263bc
MD
422 return (error);
423}
424
3919ced0
MD
425/*
426 * MPALMOSTSAFE
427 */
984263bc 428int
753fd850 429sys_mmap(struct mmap_args *uap)
984263bc 430{
a0ff68c9 431 int error;
984263bc 432
3919ced0 433 get_mplock();
d3313941
MD
434 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len,
435 uap->prot, uap->flags,
436 uap->fd, uap->pos, &uap->sysmsg_resultp);
3919ced0 437 rel_mplock();
984263bc 438
a0ff68c9 439 return (error);
984263bc 440}
984263bc 441
41c20dac 442/*
e54488bb 443 * msync_args(void *addr, size_t len, int flags)
3919ced0
MD
444 *
445 * MPALMOSTSAFE
41c20dac 446 */
984263bc 447int
753fd850 448sys_msync(struct msync_args *uap)
984263bc 449{
41c20dac 450 struct proc *p = curproc;
984263bc 451 vm_offset_t addr;
e54488bb 452 vm_offset_t tmpaddr;
984263bc
MD
453 vm_size_t size, pageoff;
454 int flags;
455 vm_map_t map;
456 int rv;
457
458 addr = (vm_offset_t) uap->addr;
459 size = uap->len;
460 flags = uap->flags;
461
462 pageoff = (addr & PAGE_MASK);
463 addr -= pageoff;
464 size += pageoff;
465 size = (vm_size_t) round_page(size);
e54488bb
MD
466 if (size < uap->len) /* wrap */
467 return(EINVAL);
468 tmpaddr = addr + size; /* workaround gcc4 opt */
469 if (tmpaddr < addr) /* wrap */
984263bc
MD
470 return(EINVAL);
471
472 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
473 return (EINVAL);
474
3919ced0 475 get_mplock();
984263bc
MD
476 map = &p->p_vmspace->vm_map;
477
478 /*
479 * XXX Gak! If size is zero we are supposed to sync "all modified
480 * pages with the region containing addr". Unfortunately, we don't
481 * really keep track of individual mmaps so we approximate by flushing
482 * the range of the map entry containing addr. This can be incorrect
483 * if the region splits or is coalesced with a neighbor.
484 */
485 if (size == 0) {
486 vm_map_entry_t entry;
487
488 vm_map_lock_read(map);
489 rv = vm_map_lookup_entry(map, addr, &entry);
3919ced0
MD
490 if (rv == FALSE) {
491 vm_map_unlock_read(map);
492 rv = KERN_INVALID_ADDRESS;
493 goto done;
494 }
984263bc
MD
495 addr = entry->start;
496 size = entry->end - entry->start;
3919ced0 497 vm_map_unlock_read(map);
984263bc
MD
498 }
499
500 /*
501 * Clean the pages and interpret the return value.
502 */
503 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
3919ced0
MD
504 (flags & MS_INVALIDATE) != 0);
505done:
506 rel_mplock();
984263bc
MD
507
508 switch (rv) {
509 case KERN_SUCCESS:
510 break;
511 case KERN_INVALID_ADDRESS:
512 return (EINVAL); /* Sun returns ENOMEM? */
513 case KERN_FAILURE:
514 return (EIO);
515 default:
516 return (EINVAL);
517 }
518
519 return (0);
520}
521
41c20dac
MD
522/*
523 * munmap_args(void *addr, size_t len)
3919ced0
MD
524 *
525 * MPALMOSTSAFE
41c20dac 526 */
984263bc 527int
753fd850 528sys_munmap(struct munmap_args *uap)
984263bc 529{
41c20dac 530 struct proc *p = curproc;
984263bc 531 vm_offset_t addr;
e54488bb 532 vm_offset_t tmpaddr;
984263bc
MD
533 vm_size_t size, pageoff;
534 vm_map_t map;
535
536 addr = (vm_offset_t) uap->addr;
537 size = uap->len;
538
539 pageoff = (addr & PAGE_MASK);
540 addr -= pageoff;
541 size += pageoff;
542 size = (vm_size_t) round_page(size);
e54488bb
MD
543 if (size < uap->len) /* wrap */
544 return(EINVAL);
545 tmpaddr = addr + size; /* workaround gcc4 opt */
546 if (tmpaddr < addr) /* wrap */
984263bc
MD
547 return(EINVAL);
548
549 if (size == 0)
550 return (0);
551
552 /*
553 * Check for illegal addresses. Watch out for address wrap... Note
554 * that VM_*_ADDRESS are not constants due to casts (argh).
555 */
e54488bb 556 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
984263bc 557 return (EINVAL);
88181b08 558 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
984263bc 559 return (EINVAL);
3919ced0
MD
560
561 get_mplock();
984263bc
MD
562 map = &p->p_vmspace->vm_map;
563 /*
564 * Make sure entire range is allocated.
565 */
3919ced0
MD
566 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) {
567 rel_mplock();
984263bc 568 return (EINVAL);
3919ced0 569 }
984263bc 570 /* returns nothing but KERN_SUCCESS anyway */
418ff780 571 vm_map_remove(map, addr, addr + size);
3919ced0 572 rel_mplock();
984263bc
MD
573 return (0);
574}
575
41c20dac
MD
576/*
577 * mprotect_args(const void *addr, size_t len, int prot)
3919ced0
MD
578 *
579 * MPALMOSTSAFE
41c20dac 580 */
984263bc 581int
753fd850 582sys_mprotect(struct mprotect_args *uap)
984263bc 583{
41c20dac 584 struct proc *p = curproc;
984263bc 585 vm_offset_t addr;
e54488bb 586 vm_offset_t tmpaddr;
984263bc 587 vm_size_t size, pageoff;
5f910b2f 588 vm_prot_t prot;
3919ced0 589 int error;
984263bc
MD
590
591 addr = (vm_offset_t) uap->addr;
592 size = uap->len;
593 prot = uap->prot & VM_PROT_ALL;
594#if defined(VM_PROT_READ_IS_EXEC)
595 if (prot & VM_PROT_READ)
596 prot |= VM_PROT_EXECUTE;
597#endif
598
599 pageoff = (addr & PAGE_MASK);
600 addr -= pageoff;
601 size += pageoff;
602 size = (vm_size_t) round_page(size);
e54488bb
MD
603 if (size < uap->len) /* wrap */
604 return(EINVAL);
605 tmpaddr = addr + size; /* workaround gcc4 opt */
606 if (tmpaddr < addr) /* wrap */
984263bc
MD
607 return(EINVAL);
608
3919ced0
MD
609 get_mplock();
610 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size,
611 prot, FALSE)) {
984263bc 612 case KERN_SUCCESS:
3919ced0
MD
613 error = 0;
614 break;
984263bc 615 case KERN_PROTECTION_FAILURE:
3919ced0
MD
616 error = EACCES;
617 break;
618 default:
619 error = EINVAL;
620 break;
984263bc 621 }
3919ced0
MD
622 rel_mplock();
623 return (error);
984263bc
MD
624}
625
41c20dac
MD
626/*
627 * minherit_args(void *addr, size_t len, int inherit)
3919ced0
MD
628 *
629 * MPALMOSTSAFE
41c20dac 630 */
984263bc 631int
753fd850 632sys_minherit(struct minherit_args *uap)
984263bc 633{
41c20dac 634 struct proc *p = curproc;
984263bc 635 vm_offset_t addr;
e54488bb 636 vm_offset_t tmpaddr;
984263bc 637 vm_size_t size, pageoff;
5f910b2f 638 vm_inherit_t inherit;
3919ced0 639 int error;
984263bc
MD
640
641 addr = (vm_offset_t)uap->addr;
642 size = uap->len;
643 inherit = uap->inherit;
644
645 pageoff = (addr & PAGE_MASK);
646 addr -= pageoff;
647 size += pageoff;
648 size = (vm_size_t) round_page(size);
e54488bb
MD
649 if (size < uap->len) /* wrap */
650 return(EINVAL);
651 tmpaddr = addr + size; /* workaround gcc4 opt */
652 if (tmpaddr < addr) /* wrap */
984263bc
MD
653 return(EINVAL);
654
3919ced0
MD
655 get_mplock();
656
657 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr,
658 addr + size, inherit)) {
984263bc 659 case KERN_SUCCESS:
3919ced0
MD
660 error = 0;
661 break;
984263bc 662 case KERN_PROTECTION_FAILURE:
3919ced0
MD
663 error = EACCES;
664 break;
665 default:
666 error = EINVAL;
667 break;
984263bc 668 }
3919ced0
MD
669 rel_mplock();
670 return (error);
984263bc
MD
671}
672
41c20dac
MD
673/*
674 * madvise_args(void *addr, size_t len, int behav)
3919ced0
MD
675 *
676 * MPALMOSTSAFE
41c20dac 677 */
984263bc 678int
753fd850 679sys_madvise(struct madvise_args *uap)
984263bc 680{
41c20dac 681 struct proc *p = curproc;
984263bc 682 vm_offset_t start, end;
e54488bb 683 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
3919ced0 684 int error;
984263bc
MD
685
686 /*
687 * Check for illegal behavior
688 */
afeabdca 689 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END)
984263bc
MD
690 return (EINVAL);
691 /*
692 * Check for illegal addresses. Watch out for address wrap... Note
693 * that VM_*_ADDRESS are not constants due to casts (argh).
694 */
e54488bb 695 if (tmpaddr < (vm_offset_t)uap->addr)
984263bc 696 return (EINVAL);
e54488bb 697 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
984263bc 698 return (EINVAL);
e54488bb 699 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
984263bc
MD
700 return (EINVAL);
701
702 /*
703 * Since this routine is only advisory, we default to conservative
704 * behavior.
705 */
e54488bb
MD
706 start = trunc_page((vm_offset_t)uap->addr);
707 end = round_page(tmpaddr);
3919ced0
MD
708
709 get_mplock();
710 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
711 uap->behav, 0);
712 rel_mplock();
713 return (error);
afeabdca
MD
714}
715
716/*
717 * mcontrol_args(void *addr, size_t len, int behav, off_t value)
3919ced0
MD
718 *
719 * MPALMOSTSAFE
afeabdca 720 */
afeabdca
MD
721int
722sys_mcontrol(struct mcontrol_args *uap)
723{
724 struct proc *p = curproc;
725 vm_offset_t start, end;
e54488bb 726 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
3919ced0 727 int error;
afeabdca
MD
728
729 /*
730 * Check for illegal behavior
731 */
732 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
984263bc 733 return (EINVAL);
afeabdca
MD
734 /*
735 * Check for illegal addresses. Watch out for address wrap... Note
736 * that VM_*_ADDRESS are not constants due to casts (argh).
737 */
e54488bb 738 if (tmpaddr < (vm_offset_t) uap->addr)
afeabdca 739 return (EINVAL);
e54488bb 740 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
afeabdca 741 return (EINVAL);
e54488bb 742 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
afeabdca
MD
743 return (EINVAL);
744
745 /*
746 * Since this routine is only advisory, we default to conservative
747 * behavior.
748 */
e54488bb
MD
749 start = trunc_page((vm_offset_t)uap->addr);
750 end = round_page(tmpaddr);
afeabdca 751
3919ced0
MD
752 get_mplock();
753 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
754 uap->behav, uap->value);
755 rel_mplock();
756 return (error);
984263bc
MD
757}
758
afeabdca 759
41c20dac
MD
760/*
761 * mincore_args(const void *addr, size_t len, char *vec)
3919ced0
MD
762 *
763 * MPALMOSTSAFE
41c20dac 764 */
984263bc 765int
753fd850 766sys_mincore(struct mincore_args *uap)
984263bc 767{
41c20dac 768 struct proc *p = curproc;
984263bc
MD
769 vm_offset_t addr, first_addr;
770 vm_offset_t end, cend;
771 pmap_t pmap;
772 vm_map_t map;
773 char *vec;
774 int error;
775 int vecindex, lastvecindex;
5f910b2f 776 vm_map_entry_t current;
984263bc
MD
777 vm_map_entry_t entry;
778 int mincoreinfo;
779 unsigned int timestamp;
780
781 /*
782 * Make sure that the addresses presented are valid for user
783 * mode.
784 */
785 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
786 end = addr + (vm_size_t)round_page(uap->len);
984263bc
MD
787 if (end < addr)
788 return (EINVAL);
e54488bb
MD
789 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS)
790 return (EINVAL);
984263bc
MD
791
792 /*
793 * Address of byte vector
794 */
795 vec = uap->vec;
796
797 map = &p->p_vmspace->vm_map;
798 pmap = vmspace_pmap(p->p_vmspace);
799
3919ced0 800 get_mplock();
984263bc
MD
801 vm_map_lock_read(map);
802RestartScan:
803 timestamp = map->timestamp;
804
805 if (!vm_map_lookup_entry(map, addr, &entry))
806 entry = entry->next;
807
808 /*
809 * Do this on a map entry basis so that if the pages are not
810 * in the current processes address space, we can easily look
811 * up the pages elsewhere.
812 */
813 lastvecindex = -1;
814 for(current = entry;
815 (current != &map->header) && (current->start < end);
816 current = current->next) {
817
818 /*
819 * ignore submaps (for now) or null objects
820 */
1b874851
MD
821 if (current->maptype != VM_MAPTYPE_NORMAL &&
822 current->maptype != VM_MAPTYPE_VPAGETABLE) {
823 continue;
824 }
825 if (current->object.vm_object == NULL)
984263bc
MD
826 continue;
827
828 /*
829 * limit this scan to the current map entry and the
830 * limits for the mincore call
831 */
832 if (addr < current->start)
833 addr = current->start;
834 cend = current->end;
835 if (cend > end)
836 cend = end;
837
838 /*
839 * scan this entry one page at a time
840 */
06ecca5a 841 while (addr < cend) {
984263bc
MD
842 /*
843 * Check pmap first, it is likely faster, also
844 * it can provide info as to whether we are the
845 * one referencing or modifying the page.
1b874851
MD
846 *
847 * If we have to check the VM object, only mess
848 * around with normal maps. Do not mess around
849 * with virtual page tables (XXX).
984263bc
MD
850 */
851 mincoreinfo = pmap_mincore(pmap, addr);
1b874851
MD
852 if (mincoreinfo == 0 &&
853 current->maptype == VM_MAPTYPE_NORMAL) {
984263bc
MD
854 vm_pindex_t pindex;
855 vm_ooffset_t offset;
856 vm_page_t m;
06ecca5a 857
984263bc
MD
858 /*
859 * calculate the page index into the object
860 */
861 offset = current->offset + (addr - current->start);
862 pindex = OFF_TO_IDX(offset);
06ecca5a 863
984263bc 864 /*
06ecca5a
MD
865 * if the page is resident, then gather
866 * information about it. spl protection is
867 * required to maintain the object
868 * association. And XXX what if the page is
869 * busy? What's the deal with that?
984263bc 870 */
654a39f0 871 crit_enter();
06ecca5a
MD
872 m = vm_page_lookup(current->object.vm_object,
873 pindex);
2ff71562 874 if (m && m->valid) {
984263bc
MD
875 mincoreinfo = MINCORE_INCORE;
876 if (m->dirty ||
877 pmap_is_modified(m))
878 mincoreinfo |= MINCORE_MODIFIED_OTHER;
879 if ((m->flags & PG_REFERENCED) ||
880 pmap_ts_referenced(m)) {
881 vm_page_flag_set(m, PG_REFERENCED);
882 mincoreinfo |= MINCORE_REFERENCED_OTHER;
883 }
884 }
654a39f0 885 crit_exit();
984263bc
MD
886 }
887
888 /*
889 * subyte may page fault. In case it needs to modify
890 * the map, we release the lock.
891 */
892 vm_map_unlock_read(map);
893
894 /*
895 * calculate index into user supplied byte vector
896 */
897 vecindex = OFF_TO_IDX(addr - first_addr);
898
899 /*
900 * If we have skipped map entries, we need to make sure that
901 * the byte vector is zeroed for those skipped entries.
902 */
903 while((lastvecindex + 1) < vecindex) {
904 error = subyte( vec + lastvecindex, 0);
905 if (error) {
3919ced0
MD
906 error = EFAULT;
907 goto done;
984263bc
MD
908 }
909 ++lastvecindex;
910 }
911
912 /*
913 * Pass the page information to the user
914 */
915 error = subyte( vec + vecindex, mincoreinfo);
916 if (error) {
3919ced0
MD
917 error = EFAULT;
918 goto done;
984263bc
MD
919 }
920
921 /*
922 * If the map has changed, due to the subyte, the previous
923 * output may be invalid.
924 */
925 vm_map_lock_read(map);
926 if (timestamp != map->timestamp)
927 goto RestartScan;
928
929 lastvecindex = vecindex;
930 addr += PAGE_SIZE;
931 }
932 }
933
934 /*
935 * subyte may page fault. In case it needs to modify
936 * the map, we release the lock.
937 */
938 vm_map_unlock_read(map);
939
940 /*
941 * Zero the last entries in the byte vector.
942 */
943 vecindex = OFF_TO_IDX(end - first_addr);
944 while((lastvecindex + 1) < vecindex) {
945 error = subyte( vec + lastvecindex, 0);
946 if (error) {
3919ced0
MD
947 error = EFAULT;
948 goto done;
984263bc
MD
949 }
950 ++lastvecindex;
951 }
952
953 /*
954 * If the map has changed, due to the subyte, the previous
955 * output may be invalid.
956 */
957 vm_map_lock_read(map);
958 if (timestamp != map->timestamp)
959 goto RestartScan;
960 vm_map_unlock_read(map);
961
3919ced0
MD
962 error = 0;
963done:
964 rel_mplock();
965 return (error);
984263bc
MD
966}
967
41c20dac
MD
968/*
969 * mlock_args(const void *addr, size_t len)
3919ced0
MD
970 *
971 * MPALMOSTSAFE
41c20dac 972 */
984263bc 973int
753fd850 974sys_mlock(struct mlock_args *uap)
984263bc
MD
975{
976 vm_offset_t addr;
e54488bb 977 vm_offset_t tmpaddr;
984263bc 978 vm_size_t size, pageoff;
9910d07b
MD
979 struct thread *td = curthread;
980 struct proc *p = td->td_proc;
3919ced0 981 int error;
984263bc
MD
982
983 addr = (vm_offset_t) uap->addr;
984 size = uap->len;
985
986 pageoff = (addr & PAGE_MASK);
987 addr -= pageoff;
988 size += pageoff;
989 size = (vm_size_t) round_page(size);
e54488bb
MD
990 if (size < uap->len) /* wrap */
991 return(EINVAL);
992 tmpaddr = addr + size; /* workaround gcc4 opt */
993 if (tmpaddr < addr) /* wrap */
984263bc
MD
994 return (EINVAL);
995
12e4aaff 996 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired)
984263bc
MD
997 return (EAGAIN);
998
3919ced0 999 get_mplock();
984263bc
MD
1000#ifdef pmap_wired_count
1001 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
3919ced0
MD
1002 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) {
1003 rel_mplock();
984263bc 1004 return (ENOMEM);
3919ced0 1005 }
984263bc 1006#else
9910d07b 1007 error = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
3919ced0
MD
1008 if (error) {
1009 rel_mplock();
984263bc 1010 return (error);
3919ced0 1011 }
984263bc 1012#endif
cde87949 1013 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
3919ced0 1014 rel_mplock();
984263bc
MD
1015 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1016}
1017
41c20dac
MD
1018/*
1019 * mlockall_args(int how)
3919ced0
MD
1020 *
1021 * Dummy routine, doesn't actually do anything.
1022 *
1023 * MPSAFE
41c20dac 1024 */
984263bc 1025int
753fd850 1026sys_mlockall(struct mlockall_args *uap)
984263bc
MD
1027{
1028 return 0;
1029}
1030
41c20dac 1031/*
efbaff5d 1032 * munlockall_args(void)
3919ced0
MD
1033 *
1034 * Dummy routine, doesn't actually do anything.
1035 *
1036 * MPSAFE
41c20dac 1037 */
984263bc 1038int
753fd850 1039sys_munlockall(struct munlockall_args *uap)
984263bc
MD
1040{
1041 return 0;
1042}
1043
41c20dac
MD
1044/*
1045 * munlock_args(const void *addr, size_t len)
3919ced0
MD
1046 *
1047 * MPALMOSTSAFE
41c20dac 1048 */
984263bc 1049int
753fd850 1050sys_munlock(struct munlock_args *uap)
984263bc 1051{
dadab5e9
MD
1052 struct thread *td = curthread;
1053 struct proc *p = td->td_proc;
984263bc 1054 vm_offset_t addr;
e54488bb 1055 vm_offset_t tmpaddr;
984263bc
MD
1056 vm_size_t size, pageoff;
1057 int error;
1058
1059 addr = (vm_offset_t) uap->addr;
1060 size = uap->len;
1061
1062 pageoff = (addr & PAGE_MASK);
1063 addr -= pageoff;
1064 size += pageoff;
1065 size = (vm_size_t) round_page(size);
1066
e54488bb
MD
1067 tmpaddr = addr + size;
1068 if (tmpaddr < addr) /* wrap */
984263bc
MD
1069 return (EINVAL);
1070
1071#ifndef pmap_wired_count
895c1f85 1072 error = priv_check(td, PRIV_ROOT);
984263bc
MD
1073 if (error)
1074 return (error);
1075#endif
1076
3919ced0 1077 get_mplock();
cde87949 1078 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
3919ced0 1079 rel_mplock();
984263bc
MD
1080 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1081}
1082
1083/*
1084 * Internal version of mmap.
1085 * Currently used by mmap, exec, and sys5 shared memory.
1086 * Handle is either a vnode pointer or NULL for MAP_ANON.
1087 */
1088int
1089vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
c809941b 1090 vm_prot_t maxprot, int flags, void *handle, vm_ooffset_t foff)
984263bc
MD
1091{
1092 boolean_t fitit;
1093 vm_object_t object;
85d25bcf
MD
1094 vm_offset_t eaddr;
1095 vm_size_t esize;
a6e41612 1096 struct vnode *vp;
349433c9 1097 struct thread *td = curthread;
d3313941 1098 struct proc *p;
984263bc
MD
1099 objtype_t type;
1100 int rv = KERN_SUCCESS;
57f7b636 1101 off_t objsize;
984263bc 1102 int docow;
984263bc
MD
1103
1104 if (size == 0)
1105 return (0);
1106
e54488bb
MD
1107 objsize = round_page(size);
1108 if (objsize < size)
1109 return (EINVAL);
1110 size = objsize;
984263bc 1111
d3313941
MD
1112 /*
1113 * XXX messy code, fixme
85d25bcf
MD
1114 *
1115 * NOTE: Overflow checks require discrete statements or GCC4
1116 * will optimize it out.
d3313941
MD
1117 */
1118 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) {
e54488bb 1119 esize = map->size + size; /* workaround gcc4 opt */
85d25bcf
MD
1120 if (esize < map->size ||
1121 esize > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
d3313941 1122 return(ENOMEM);
85d25bcf 1123 }
984263bc
MD
1124 }
1125
1126 /*
1127 * We currently can only deal with page aligned file offsets.
1128 * The check is here rather than in the syscall because the
1129 * kernel calls this function internally for other mmaping
1130 * operations (such as in exec) and non-aligned offsets will
1131 * cause pmap inconsistencies...so we want to be sure to
1132 * disallow this in all cases.
85d25bcf
MD
1133 *
1134 * NOTE: Overflow checks require discrete statements or GCC4
1135 * will optimize it out.
984263bc
MD
1136 */
1137 if (foff & PAGE_MASK)
1138 return (EINVAL);
1139
c809941b 1140 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
984263bc
MD
1141 fitit = TRUE;
1142 *addr = round_page(*addr);
1143 } else {
1144 if (*addr != trunc_page(*addr))
1145 return (EINVAL);
85d25bcf
MD
1146 eaddr = *addr + size;
1147 if (eaddr < *addr)
1148 return (EINVAL);
984263bc 1149 fitit = FALSE;
c809941b
MD
1150 if ((flags & MAP_TRYFIXED) == 0)
1151 vm_map_remove(map, *addr, *addr + size);
984263bc
MD
1152 }
1153
1154 /*
1155 * Lookup/allocate object.
1156 */
1157 if (flags & MAP_ANON) {
1158 type = OBJT_DEFAULT;
1159 /*
1160 * Unnamed anonymous regions always start at 0.
1161 */
a6e41612 1162 if (handle == NULL)
984263bc 1163 foff = 0;
a6e41612 1164 vp = NULL;
984263bc 1165 } else {
a6e41612 1166 vp = (struct vnode *)handle;
984263bc
MD
1167 if (vp->v_type == VCHR) {
1168 type = OBJT_DEVICE;
1169 handle = (void *)(intptr_t)vp->v_rdev;
1170 } else {
f9cae863 1171 struct vattr vat;
984263bc
MD
1172 int error;
1173
87de5057 1174 error = VOP_GETATTR(vp, &vat);
984263bc
MD
1175 if (error)
1176 return (error);
57f7b636 1177 objsize = vat.va_size;
984263bc
MD
1178 type = OBJT_VNODE;
1179 /*
1180 * if it is a regular file without any references
1181 * we do not need to sync it.
1182 */
1183 if (vp->v_type == VREG && vat.va_nlink == 0) {
1184 flags |= MAP_NOSYNC;
1185 }
1186 }
1187 }
1188
1189 if (handle == NULL) {
1190 object = NULL;
1191 docow = 0;
1192 } else {
57f7b636 1193 object = vm_pager_allocate(type, handle, objsize, prot, foff);
984263bc
MD
1194 if (object == NULL)
1195 return (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1196 docow = MAP_PREFAULT_PARTIAL;
1197 }
1198
1199 /*
1200 * Force device mappings to be shared.
1201 */
1202 if (type == OBJT_DEVICE || type == OBJT_PHYS) {
1203 flags &= ~(MAP_PRIVATE|MAP_COPY);
1204 flags |= MAP_SHARED;
1205 }
1206
1207 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1208 docow |= MAP_COPY_ON_WRITE;
1209 if (flags & MAP_NOSYNC)
1210 docow |= MAP_DISABLE_SYNCER;
1211 if (flags & MAP_NOCORE)
1212 docow |= MAP_DISABLE_COREDUMP;
1213
1214#if defined(VM_PROT_READ_IS_EXEC)
1215 if (prot & VM_PROT_READ)
1216 prot |= VM_PROT_EXECUTE;
1217
1218 if (maxprot & VM_PROT_READ)
1219 maxprot |= VM_PROT_EXECUTE;
1220#endif
1221
c809941b
MD
1222 /*
1223 * This may place the area in its own page directory if (size) is
1224 * large enough, otherwise it typically returns its argument.
1225 */
984263bc
MD
1226 if (fitit) {
1227 *addr = pmap_addr_hint(object, *addr, size);
1228 }
1229
568e6804 1230 /*
c809941b
MD
1231 * Stack mappings need special attention.
1232 *
1233 * Mappings that use virtual page tables will default to storing
1234 * the page table at offset 0.
568e6804 1235 */
1b874851 1236 if (flags & MAP_STACK) {
c809941b 1237 rv = vm_map_stack(map, *addr, size, flags,
85d25bcf 1238 prot, maxprot, docow);
568e6804
MD
1239 } else if (flags & MAP_VPAGETABLE) {
1240 rv = vm_map_find(map, object, foff, addr, size, fitit,
1241 VM_MAPTYPE_VPAGETABLE, prot, maxprot, docow);
1b874851 1242 } else {
984263bc 1243 rv = vm_map_find(map, object, foff, addr, size, fitit,
1b874851
MD
1244 VM_MAPTYPE_NORMAL, prot, maxprot, docow);
1245 }
984263bc
MD
1246
1247 if (rv != KERN_SUCCESS) {
1248 /*
1249 * Lose the object reference. Will destroy the
1250 * object if it's an unnamed anonymous mapping
1251 * or named anonymous without other references.
1252 */
1253 vm_object_deallocate(object);
1254 goto out;
1255 }
1256
1257 /*
1258 * Shared memory is also shared with children.
1259 */
1260 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1261 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1262 if (rv != KERN_SUCCESS) {
418ff780 1263 vm_map_remove(map, *addr, *addr + size);
984263bc
MD
1264 goto out;
1265 }
1266 }
349433c9
MD
1267
1268 /*
1269 * Set the access time on the vnode
1270 */
a6e41612
MD
1271 if (vp != NULL)
1272 vn_mark_atime(vp, td);
984263bc
MD
1273out:
1274 switch (rv) {
1275 case KERN_SUCCESS:
1276 return (0);
1277 case KERN_INVALID_ADDRESS:
1278 case KERN_NO_SPACE:
1279 return (ENOMEM);
1280 case KERN_PROTECTION_FAILURE:
1281 return (EACCES);
1282 default:
1283 return (EINVAL);
1284 }
1285}