kernel - Fix overflows and races in zalloc()
[dragonfly.git] / sys / vm / vm_mmap.c
CommitLineData
984263bc 1/*
fcc11a69
VS
2 * (MPSAFE)
3 *
984263bc
MD
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
984263bc
MD
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
37 *
38 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
39 * $FreeBSD: src/sys/vm/vm_mmap.c,v 1.108.2.6 2002/07/02 20:06:19 dillon Exp $
40 */
41
42/*
43 * Mapped file (mmap) interface to VM
44 */
45
984263bc
MD
46#include <sys/param.h>
47#include <sys/kernel.h>
48#include <sys/systm.h>
49#include <sys/sysproto.h>
50#include <sys/filedesc.h>
a0ff68c9 51#include <sys/kern_syscall.h>
984263bc 52#include <sys/proc.h>
895c1f85 53#include <sys/priv.h>
984263bc
MD
54#include <sys/resource.h>
55#include <sys/resourcevar.h>
56#include <sys/vnode.h>
57#include <sys/fcntl.h>
58#include <sys/file.h>
59#include <sys/mman.h>
60#include <sys/conf.h>
61#include <sys/stat.h>
62#include <sys/vmmeter.h>
63#include <sys/sysctl.h>
64
65#include <vm/vm.h>
66#include <vm/vm_param.h>
67#include <sys/lock.h>
68#include <vm/pmap.h>
69#include <vm/vm_map.h>
70#include <vm/vm_object.h>
71#include <vm/vm_page.h>
72#include <vm/vm_pager.h>
73#include <vm/vm_pageout.h>
74#include <vm/vm_extern.h>
75#include <vm/vm_page.h>
76#include <vm/vm_kern.h>
77
dadab5e9 78#include <sys/file2.h>
3de6dc48 79#include <sys/thread.h>
654a39f0 80#include <sys/thread2.h>
dadab5e9 81
984263bc
MD
82static int max_proc_mmap;
83SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
568e6804
MD
84int vkernel_enable;
85SYSCTL_INT(_vm, OID_AUTO, vkernel_enable, CTLFLAG_RW, &vkernel_enable, 0, "");
984263bc
MD
86
87/*
88 * Set the maximum number of vm_map_entry structures per process. Roughly
89 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
90 * of our KVM malloc space still results in generous limits. We want a
91 * default that is good enough to prevent the kernel running out of resources
92 * if attacked from compromised user account but generous enough such that
93 * multi-threaded processes are not unduly inconvenienced.
94 */
95
1388df65 96static void vmmapentry_rsrc_init (void *);
ba39e2e0 97SYSINIT(vmmersrc, SI_BOOT1_POST, SI_ORDER_ANY, vmmapentry_rsrc_init, NULL)
984263bc
MD
98
99static void
57e43348 100vmmapentry_rsrc_init(void *dummy)
984263bc 101{
c439ad8f 102 max_proc_mmap = KvaSize / sizeof(struct vm_map_entry);
984263bc
MD
103 max_proc_mmap /= 100;
104}
105
3919ced0
MD
106/*
107 * MPSAFE
108 */
984263bc 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)
3919ced0
MD
118 *
119 * MPSAFE
41c20dac 120 */
984263bc 121int
753fd850 122sys_sstk(struct sstk_args *uap)
984263bc 123{
984263bc
MD
124 /* Not yet implemented */
125 return (EOPNOTSUPP);
126}
127
984263bc 128/*
41c20dac
MD
129 * mmap_args(void *addr, size_t len, int prot, int flags, int fd,
130 * long pad, off_t pos)
131 *
984263bc
MD
132 * Memory Map (mmap) system call. Note that the file offset
133 * and address are allowed to be NOT page aligned, though if
134 * the MAP_FIXED flag it set, both must have the same remainder
135 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
136 * page-aligned, the actual mapping starts at trunc_page(addr)
137 * and the return value is adjusted up by the page offset.
138 *
139 * Generally speaking, only character devices which are themselves
140 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
141 * there would be no cache coherency between a descriptor and a VM mapping
142 * both to the same character device.
143 *
144 * Block devices can be mmap'd no matter what they represent. Cache coherency
145 * is maintained as long as you do not write directly to the underlying
146 * character device.
3de6dc48 147 *
b12defdc 148 * No requirements
984263bc 149 */
984263bc 150int
d3313941
MD
151kern_mmap(struct vmspace *vms, caddr_t uaddr, size_t ulen,
152 int uprot, int uflags, int fd, off_t upos, void **res)
984263bc 153{
dadab5e9
MD
154 struct thread *td = curthread;
155 struct proc *p = td->td_proc;
41c20dac 156 struct file *fp = NULL;
984263bc
MD
157 struct vnode *vp;
158 vm_offset_t addr;
e54488bb 159 vm_offset_t tmpaddr;
984263bc
MD
160 vm_size_t size, pageoff;
161 vm_prot_t prot, maxprot;
162 void *handle;
163 int flags, error;
984263bc 164 off_t pos;
984263bc
MD
165 vm_object_t obj;
166
dadab5e9
MD
167 KKASSERT(p);
168
a0ff68c9
DRJ
169 addr = (vm_offset_t) uaddr;
170 size = ulen;
171 prot = uprot & VM_PROT_ALL;
172 flags = uflags;
173 pos = upos;
984263bc 174
e54488bb
MD
175 /*
176 * Make sure mapping fits into numeric range etc.
177 *
178 * NOTE: We support the full unsigned range for size now.
179 */
749349ca 180 if (((flags & MAP_ANON) && (fd != -1 || pos != 0)))
984263bc
MD
181 return (EINVAL);
182
06cee6ba
VS
183 if (size == 0)
184 return (EINVAL);
185
984263bc 186 if (flags & MAP_STACK) {
a0ff68c9 187 if ((fd != -1) ||
984263bc
MD
188 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
189 return (EINVAL);
190 flags |= MAP_ANON;
191 pos = 0;
192 }
193
568e6804
MD
194 /*
195 * Virtual page tables cannot be used with MAP_STACK. Apart from
afeabdca 196 * it not making any sense, the aux union is used by both
568e6804
MD
197 * types.
198 *
199 * Because the virtual page table is stored in the backing object
200 * and might be updated by the kernel, the mapping must be R+W.
201 */
202 if (flags & MAP_VPAGETABLE) {
203 if (vkernel_enable == 0)
204 return (EOPNOTSUPP);
205 if (flags & MAP_STACK)
206 return (EINVAL);
207 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE))
208 return (EINVAL);
209 }
210
984263bc
MD
211 /*
212 * Align the file position to a page boundary,
213 * and save its page offset component.
214 */
215 pageoff = (pos & PAGE_MASK);
216 pos -= pageoff;
217
218 /* Adjust size for rounding (on both ends). */
219 size += pageoff; /* low end... */
220 size = (vm_size_t) round_page(size); /* hi end */
e54488bb
MD
221 if (size < ulen) /* wrap */
222 return(EINVAL);
984263bc
MD
223
224 /*
225 * Check for illegal addresses. Watch out for address wrap... Note
226 * that VM_*_ADDRESS are not constants due to casts (argh).
227 */
c809941b 228 if (flags & (MAP_FIXED | MAP_TRYFIXED)) {
984263bc
MD
229 /*
230 * The specified address must have the same remainder
231 * as the file offset taken modulo PAGE_SIZE, so it
232 * should be aligned after adjustment by pageoff.
233 */
234 addr -= pageoff;
235 if (addr & PAGE_MASK)
236 return (EINVAL);
e54488bb
MD
237
238 /*
239 * Address range must be all in user VM space and not wrap.
240 */
241 tmpaddr = addr + size;
242 if (tmpaddr < addr)
243 return (EINVAL);
244 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
984263bc 245 return (EINVAL);
88181b08 246 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
984263bc 247 return (EINVAL);
c809941b 248 } else {
459f5c1e 249 /*
911e30e2
AH
250 * Get a hint of where to map. It also provides mmap offset
251 * randomization if enabled.
459f5c1e 252 */
911e30e2 253 addr = vm_map_hint(p, addr, prot);
984263bc 254 }
984263bc
MD
255
256 if (flags & MAP_ANON) {
257 /*
258 * Mapping blank space is trivial.
259 */
260 handle = NULL;
261 maxprot = VM_PROT_ALL;
984263bc
MD
262 } else {
263 /*
264 * Mapping file, get fp for validation. Obtain vnode and make
265 * sure it is of appropriate type.
266 */
228b401d
MD
267 fp = holdfp(p->p_fd, fd, -1);
268 if (fp == NULL)
984263bc 269 return (EBADF);
228b401d
MD
270 if (fp->f_type != DTYPE_VNODE) {
271 error = EINVAL;
272 goto done;
273 }
984263bc
MD
274 /*
275 * POSIX shared-memory objects are defined to have
276 * kernel persistence, and are not defined to support
277 * read(2)/write(2) -- or even open(2). Thus, we can
278 * use MAP_ASYNC to trade on-disk coherence for speed.
279 * The shm_open(3) library routine turns on the FPOSIXSHM
280 * flag to request this behavior.
281 */
282 if (fp->f_flag & FPOSIXSHM)
283 flags |= MAP_NOSYNC;
284 vp = (struct vnode *) fp->f_data;
339fa1ed
MD
285
286 /*
287 * Validate the vnode for the operation.
288 */
289 switch(vp->v_type) {
290 case VREG:
984263bc
MD
291 /*
292 * Get the proper underlying object
293 */
228b401d
MD
294 if ((obj = vp->v_object) == NULL) {
295 error = EINVAL;
296 goto done;
297 }
339fa1ed
MD
298 KKASSERT((struct vnode *)obj->handle == vp);
299 break;
300 case VCHR:
301 /*
302 * Make sure a device has not been revoked.
303 * Mappability is handled by the device layer.
304 */
305 if (vp->v_rdev == NULL) {
306 error = EBADF;
307 goto done;
308 }
309 break;
310 default:
311 /*
312 * Nothing else is mappable.
313 */
314 error = EINVAL;
315 goto done;
984263bc
MD
316 }
317
984263bc
MD
318 /*
319 * XXX hack to handle use of /dev/zero to map anon memory (ala
320 * SunOS).
321 */
322 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
323 handle = NULL;
324 maxprot = VM_PROT_ALL;
325 flags |= MAP_ANON;
326 pos = 0;
327 } else {
328 /*
329 * cdevs does not provide private mappings of any kind.
330 */
8ef9b5bb 331 if (vp->v_type == VCHR &&
984263bc
MD
332 (flags & (MAP_PRIVATE|MAP_COPY))) {
333 error = EINVAL;
334 goto done;
335 }
336 /*
337 * Ensure that file and memory protections are
338 * compatible. Note that we only worry about
339 * writability if mapping is shared; in this case,
340 * current and max prot are dictated by the open file.
341 * XXX use the vnode instead? Problem is: what
342 * credentials do we use for determination? What if
343 * proc does a setuid?
344 */
345 maxprot = VM_PROT_EXECUTE; /* ??? */
346 if (fp->f_flag & FREAD) {
347 maxprot |= VM_PROT_READ;
348 } else if (prot & PROT_READ) {
349 error = EACCES;
350 goto done;
351 }
352 /*
353 * If we are sharing potential changes (either via
354 * MAP_SHARED or via the implicit sharing of character
355 * device mappings), and we are trying to get write
356 * permission although we opened it without asking
357 * for it, bail out. Check for superuser, only if
358 * we're at securelevel < 1, to allow the XIG X server
359 * to continue to work.
360 */
3e272077 361 if ((flags & MAP_SHARED) != 0 || vp->v_type == VCHR) {
984263bc
MD
362 if ((fp->f_flag & FWRITE) != 0) {
363 struct vattr va;
87de5057 364 if ((error = VOP_GETATTR(vp, &va))) {
984263bc
MD
365 goto done;
366 }
367 if ((va.va_flags &
368 (IMMUTABLE|APPEND)) == 0) {
369 maxprot |= VM_PROT_WRITE;
370 } else if (prot & PROT_WRITE) {
371 error = EPERM;
372 goto done;
373 }
374 } else if ((prot & PROT_WRITE) != 0) {
375 error = EACCES;
376 goto done;
377 }
378 } else {
379 maxprot |= VM_PROT_WRITE;
380 }
381 handle = (void *)vp;
382 }
383 }
384
b12defdc 385 lwkt_gettoken(&vms->vm_map.token);
8611a340 386
984263bc
MD
387 /*
388 * Do not allow more then a certain number of vm_map_entry structures
389 * per process. Scale with the number of rforks sharing the map
390 * to make the limit reasonable for threads.
391 */
392 if (max_proc_mmap &&
e3161323 393 vms->vm_map.nentries >= max_proc_mmap * vms->vm_sysref.refcnt) {
984263bc 394 error = ENOMEM;
b12defdc 395 lwkt_reltoken(&vms->vm_map.token);
984263bc
MD
396 goto done;
397 }
398
399 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
e54488bb 400 flags, handle, pos);
984263bc 401 if (error == 0)
a0ff68c9 402 *res = (void *)(addr + pageoff);
8611a340 403
b12defdc 404 lwkt_reltoken(&vms->vm_map.token);
984263bc
MD
405done:
406 if (fp)
9f87144f 407 fdrop(fp);
8611a340 408
984263bc
MD
409 return (error);
410}
411
3919ced0 412/*
3de6dc48
VS
413 * mmap system call handler
414 *
415 * No requirements.
3919ced0 416 */
984263bc 417int
753fd850 418sys_mmap(struct mmap_args *uap)
984263bc 419{
a0ff68c9 420 int error;
984263bc 421
d3313941
MD
422 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len,
423 uap->prot, uap->flags,
424 uap->fd, uap->pos, &uap->sysmsg_resultp);
984263bc 425
a0ff68c9 426 return (error);
984263bc 427}
984263bc 428
41c20dac 429/*
3de6dc48
VS
430 * msync system call handler
431 *
e54488bb 432 * msync_args(void *addr, size_t len, int flags)
3919ced0 433 *
3de6dc48 434 * No requirements
41c20dac 435 */
984263bc 436int
753fd850 437sys_msync(struct msync_args *uap)
984263bc 438{
41c20dac 439 struct proc *p = curproc;
984263bc 440 vm_offset_t addr;
e54488bb 441 vm_offset_t tmpaddr;
984263bc
MD
442 vm_size_t size, pageoff;
443 int flags;
444 vm_map_t map;
445 int rv;
446
447 addr = (vm_offset_t) uap->addr;
448 size = uap->len;
449 flags = uap->flags;
450
451 pageoff = (addr & PAGE_MASK);
452 addr -= pageoff;
453 size += pageoff;
454 size = (vm_size_t) round_page(size);
e54488bb
MD
455 if (size < uap->len) /* wrap */
456 return(EINVAL);
457 tmpaddr = addr + size; /* workaround gcc4 opt */
458 if (tmpaddr < addr) /* wrap */
984263bc
MD
459 return(EINVAL);
460
461 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
462 return (EINVAL);
463
464 map = &p->p_vmspace->vm_map;
465
8611a340 466 /*
b12defdc 467 * map->token serializes extracting the address range for size == 0
8611a340
VS
468 * msyncs with the vm_map_clean call; if the token were not held
469 * across the two calls, an intervening munmap/mmap pair, for example,
470 * could cause msync to occur on a wrong region.
471 */
b12defdc 472 lwkt_gettoken(&map->token);
8611a340 473
984263bc
MD
474 /*
475 * XXX Gak! If size is zero we are supposed to sync "all modified
476 * pages with the region containing addr". Unfortunately, we don't
477 * really keep track of individual mmaps so we approximate by flushing
478 * the range of the map entry containing addr. This can be incorrect
479 * if the region splits or is coalesced with a neighbor.
480 */
481 if (size == 0) {
482 vm_map_entry_t entry;
483
484 vm_map_lock_read(map);
485 rv = vm_map_lookup_entry(map, addr, &entry);
3919ced0
MD
486 if (rv == FALSE) {
487 vm_map_unlock_read(map);
488 rv = KERN_INVALID_ADDRESS;
489 goto done;
490 }
984263bc
MD
491 addr = entry->start;
492 size = entry->end - entry->start;
3919ced0 493 vm_map_unlock_read(map);
984263bc
MD
494 }
495
496 /*
497 * Clean the pages and interpret the return value.
498 */
499 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
3919ced0
MD
500 (flags & MS_INVALIDATE) != 0);
501done:
b12defdc 502 lwkt_reltoken(&map->token);
984263bc
MD
503
504 switch (rv) {
505 case KERN_SUCCESS:
506 break;
507 case KERN_INVALID_ADDRESS:
508 return (EINVAL); /* Sun returns ENOMEM? */
509 case KERN_FAILURE:
510 return (EIO);
511 default:
512 return (EINVAL);
513 }
514
515 return (0);
516}
517
41c20dac 518/*
3de6dc48
VS
519 * munmap system call handler
520 *
41c20dac 521 * munmap_args(void *addr, size_t len)
3919ced0 522 *
3de6dc48 523 * No requirements
41c20dac 524 */
984263bc 525int
753fd850 526sys_munmap(struct munmap_args *uap)
984263bc 527{
41c20dac 528 struct proc *p = curproc;
984263bc 529 vm_offset_t addr;
e54488bb 530 vm_offset_t tmpaddr;
984263bc
MD
531 vm_size_t size, pageoff;
532 vm_map_t map;
533
534 addr = (vm_offset_t) uap->addr;
535 size = uap->len;
536
537 pageoff = (addr & PAGE_MASK);
538 addr -= pageoff;
539 size += pageoff;
540 size = (vm_size_t) round_page(size);
e54488bb
MD
541 if (size < uap->len) /* wrap */
542 return(EINVAL);
543 tmpaddr = addr + size; /* workaround gcc4 opt */
544 if (tmpaddr < addr) /* wrap */
984263bc
MD
545 return(EINVAL);
546
547 if (size == 0)
548 return (0);
549
550 /*
551 * Check for illegal addresses. Watch out for address wrap... Note
552 * that VM_*_ADDRESS are not constants due to casts (argh).
553 */
e54488bb 554 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
984263bc 555 return (EINVAL);
88181b08 556 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
984263bc 557 return (EINVAL);
3919ced0 558
984263bc 559 map = &p->p_vmspace->vm_map;
8611a340 560
b12defdc
MD
561 /* map->token serializes between the map check and the actual unmap */
562 lwkt_gettoken(&map->token);
8611a340 563
984263bc
MD
564 /*
565 * Make sure entire range is allocated.
566 */
46754a20
MD
567 if (!vm_map_check_protection(map, addr, addr + size,
568 VM_PROT_NONE, FALSE)) {
b12defdc 569 lwkt_reltoken(&map->token);
984263bc 570 return (EINVAL);
3919ced0 571 }
984263bc 572 /* returns nothing but KERN_SUCCESS anyway */
418ff780 573 vm_map_remove(map, addr, addr + size);
b12defdc 574 lwkt_reltoken(&map->token);
984263bc
MD
575 return (0);
576}
577
41c20dac
MD
578/*
579 * mprotect_args(const void *addr, size_t len, int prot)
3919ced0 580 *
8611a340 581 * No requirements.
41c20dac 582 */
984263bc 583int
753fd850 584sys_mprotect(struct mprotect_args *uap)
984263bc 585{
41c20dac 586 struct proc *p = curproc;
984263bc 587 vm_offset_t addr;
e54488bb 588 vm_offset_t tmpaddr;
984263bc 589 vm_size_t size, pageoff;
5f910b2f 590 vm_prot_t prot;
3919ced0 591 int error;
984263bc
MD
592
593 addr = (vm_offset_t) uap->addr;
594 size = uap->len;
595 prot = uap->prot & VM_PROT_ALL;
596#if defined(VM_PROT_READ_IS_EXEC)
597 if (prot & VM_PROT_READ)
598 prot |= VM_PROT_EXECUTE;
599#endif
600
601 pageoff = (addr & PAGE_MASK);
602 addr -= pageoff;
603 size += pageoff;
604 size = (vm_size_t) round_page(size);
e54488bb
MD
605 if (size < uap->len) /* wrap */
606 return(EINVAL);
607 tmpaddr = addr + size; /* workaround gcc4 opt */
608 if (tmpaddr < addr) /* wrap */
984263bc
MD
609 return(EINVAL);
610
3919ced0
MD
611 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size,
612 prot, FALSE)) {
984263bc 613 case KERN_SUCCESS:
3919ced0
MD
614 error = 0;
615 break;
984263bc 616 case KERN_PROTECTION_FAILURE:
3919ced0
MD
617 error = EACCES;
618 break;
619 default:
620 error = EINVAL;
621 break;
984263bc 622 }
3919ced0 623 return (error);
984263bc
MD
624}
625
41c20dac 626/*
3de6dc48
VS
627 * minherit system call handler
628 *
41c20dac 629 * minherit_args(void *addr, size_t len, int inherit)
3919ced0 630 *
3de6dc48 631 * No requirements.
41c20dac 632 */
984263bc 633int
753fd850 634sys_minherit(struct minherit_args *uap)
984263bc 635{
41c20dac 636 struct proc *p = curproc;
984263bc 637 vm_offset_t addr;
e54488bb 638 vm_offset_t tmpaddr;
984263bc 639 vm_size_t size, pageoff;
5f910b2f 640 vm_inherit_t inherit;
3919ced0 641 int error;
984263bc
MD
642
643 addr = (vm_offset_t)uap->addr;
644 size = uap->len;
645 inherit = uap->inherit;
646
647 pageoff = (addr & PAGE_MASK);
648 addr -= pageoff;
649 size += pageoff;
650 size = (vm_size_t) round_page(size);
e54488bb
MD
651 if (size < uap->len) /* wrap */
652 return(EINVAL);
653 tmpaddr = addr + size; /* workaround gcc4 opt */
654 if (tmpaddr < addr) /* wrap */
984263bc
MD
655 return(EINVAL);
656
3919ced0
MD
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 669 return (error);
984263bc
MD
670}
671
41c20dac 672/*
3de6dc48
VS
673 * madvise system call handler
674 *
41c20dac 675 * madvise_args(void *addr, size_t len, int behav)
3919ced0 676 *
3de6dc48 677 * No requirements.
41c20dac 678 */
984263bc 679int
753fd850 680sys_madvise(struct madvise_args *uap)
984263bc 681{
41c20dac 682 struct proc *p = curproc;
984263bc 683 vm_offset_t start, end;
e54488bb 684 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
3919ced0 685 int error;
984263bc
MD
686
687 /*
688 * Check for illegal behavior
689 */
afeabdca 690 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END)
984263bc
MD
691 return (EINVAL);
692 /*
693 * Check for illegal addresses. Watch out for address wrap... Note
694 * that VM_*_ADDRESS are not constants due to casts (argh).
695 */
e54488bb 696 if (tmpaddr < (vm_offset_t)uap->addr)
984263bc 697 return (EINVAL);
e54488bb 698 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
984263bc 699 return (EINVAL);
e54488bb 700 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
984263bc
MD
701 return (EINVAL);
702
703 /*
704 * Since this routine is only advisory, we default to conservative
705 * behavior.
706 */
e54488bb
MD
707 start = trunc_page((vm_offset_t)uap->addr);
708 end = round_page(tmpaddr);
3919ced0 709
3919ced0
MD
710 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
711 uap->behav, 0);
3919ced0 712 return (error);
afeabdca
MD
713}
714
715/*
3de6dc48
VS
716 * mcontrol system call handler
717 *
afeabdca 718 * mcontrol_args(void *addr, size_t len, int behav, off_t value)
3919ced0 719 *
3de6dc48 720 * No requirements
afeabdca 721 */
afeabdca
MD
722int
723sys_mcontrol(struct mcontrol_args *uap)
724{
725 struct proc *p = curproc;
726 vm_offset_t start, end;
e54488bb 727 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
3919ced0 728 int error;
afeabdca
MD
729
730 /*
731 * Check for illegal behavior
732 */
733 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
984263bc 734 return (EINVAL);
afeabdca
MD
735 /*
736 * Check for illegal addresses. Watch out for address wrap... Note
737 * that VM_*_ADDRESS are not constants due to casts (argh).
738 */
e54488bb 739 if (tmpaddr < (vm_offset_t) uap->addr)
afeabdca 740 return (EINVAL);
e54488bb 741 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS)
afeabdca 742 return (EINVAL);
e54488bb 743 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
afeabdca
MD
744 return (EINVAL);
745
746 /*
747 * Since this routine is only advisory, we default to conservative
748 * behavior.
749 */
e54488bb
MD
750 start = trunc_page((vm_offset_t)uap->addr);
751 end = round_page(tmpaddr);
afeabdca 752
3919ced0
MD
753 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end,
754 uap->behav, uap->value);
3919ced0 755 return (error);
984263bc
MD
756}
757
afeabdca 758
41c20dac 759/*
3de6dc48
VS
760 * mincore system call handler
761 *
41c20dac 762 * mincore_args(const void *addr, size_t len, char *vec)
3919ced0 763 *
3de6dc48 764 * No requirements
41c20dac 765 */
984263bc 766int
753fd850 767sys_mincore(struct mincore_args *uap)
984263bc 768{
41c20dac 769 struct proc *p = curproc;
984263bc
MD
770 vm_offset_t addr, first_addr;
771 vm_offset_t end, cend;
772 pmap_t pmap;
773 vm_map_t map;
774 char *vec;
775 int error;
776 int vecindex, lastvecindex;
5f910b2f 777 vm_map_entry_t current;
984263bc
MD
778 vm_map_entry_t entry;
779 int mincoreinfo;
780 unsigned int timestamp;
781
782 /*
783 * Make sure that the addresses presented are valid for user
784 * mode.
785 */
786 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
787 end = addr + (vm_size_t)round_page(uap->len);
984263bc
MD
788 if (end < addr)
789 return (EINVAL);
e54488bb
MD
790 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS)
791 return (EINVAL);
984263bc
MD
792
793 /*
794 * Address of byte vector
795 */
796 vec = uap->vec;
797
798 map = &p->p_vmspace->vm_map;
799 pmap = vmspace_pmap(p->p_vmspace);
800
b12defdc 801 lwkt_gettoken(&map->token);
984263bc
MD
802 vm_map_lock_read(map);
803RestartScan:
804 timestamp = map->timestamp;
805
806 if (!vm_map_lookup_entry(map, addr, &entry))
807 entry = entry->next;
808
809 /*
810 * Do this on a map entry basis so that if the pages are not
811 * in the current processes address space, we can easily look
812 * up the pages elsewhere.
813 */
814 lastvecindex = -1;
815 for(current = entry;
816 (current != &map->header) && (current->start < end);
817 current = current->next) {
818
819 /*
820 * ignore submaps (for now) or null objects
821 */
1b874851
MD
822 if (current->maptype != VM_MAPTYPE_NORMAL &&
823 current->maptype != VM_MAPTYPE_VPAGETABLE) {
824 continue;
825 }
826 if (current->object.vm_object == NULL)
984263bc
MD
827 continue;
828
829 /*
830 * limit this scan to the current map entry and the
831 * limits for the mincore call
832 */
833 if (addr < current->start)
834 addr = current->start;
835 cend = current->end;
836 if (cend > end)
837 cend = end;
838
839 /*
840 * scan this entry one page at a time
841 */
06ecca5a 842 while (addr < cend) {
984263bc
MD
843 /*
844 * Check pmap first, it is likely faster, also
845 * it can provide info as to whether we are the
846 * one referencing or modifying the page.
1b874851
MD
847 *
848 * If we have to check the VM object, only mess
849 * around with normal maps. Do not mess around
850 * with virtual page tables (XXX).
984263bc
MD
851 */
852 mincoreinfo = pmap_mincore(pmap, addr);
1b874851
MD
853 if (mincoreinfo == 0 &&
854 current->maptype == VM_MAPTYPE_NORMAL) {
984263bc
MD
855 vm_pindex_t pindex;
856 vm_ooffset_t offset;
857 vm_page_t m;
06ecca5a 858
984263bc
MD
859 /*
860 * calculate the page index into the object
861 */
862 offset = current->offset + (addr - current->start);
863 pindex = OFF_TO_IDX(offset);
06ecca5a 864
984263bc 865 /*
06ecca5a
MD
866 * if the page is resident, then gather
867 * information about it. spl protection is
868 * required to maintain the object
869 * association. And XXX what if the page is
870 * busy? What's the deal with that?
b12defdc
MD
871 *
872 * XXX vm_token - legacy for pmap_ts_referenced
873 * in i386 and vkernel pmap code.
984263bc 874 */
b12defdc
MD
875 lwkt_gettoken(&vm_token);
876 vm_object_hold(current->object.vm_object);
06ecca5a
MD
877 m = vm_page_lookup(current->object.vm_object,
878 pindex);
2ff71562 879 if (m && m->valid) {
984263bc
MD
880 mincoreinfo = MINCORE_INCORE;
881 if (m->dirty ||
882 pmap_is_modified(m))
883 mincoreinfo |= MINCORE_MODIFIED_OTHER;
884 if ((m->flags & PG_REFERENCED) ||
885 pmap_ts_referenced(m)) {
886 vm_page_flag_set(m, PG_REFERENCED);
887 mincoreinfo |= MINCORE_REFERENCED_OTHER;
888 }
889 }
b12defdc
MD
890 vm_object_drop(current->object.vm_object);
891 lwkt_reltoken(&vm_token);
984263bc
MD
892 }
893
894 /*
895 * subyte may page fault. In case it needs to modify
896 * the map, we release the lock.
897 */
898 vm_map_unlock_read(map);
899
900 /*
901 * calculate index into user supplied byte vector
902 */
903 vecindex = OFF_TO_IDX(addr - first_addr);
904
905 /*
906 * If we have skipped map entries, we need to make sure that
907 * the byte vector is zeroed for those skipped entries.
908 */
909 while((lastvecindex + 1) < vecindex) {
910 error = subyte( vec + lastvecindex, 0);
911 if (error) {
3919ced0
MD
912 error = EFAULT;
913 goto done;
984263bc
MD
914 }
915 ++lastvecindex;
916 }
917
918 /*
919 * Pass the page information to the user
920 */
921 error = subyte( vec + vecindex, mincoreinfo);
922 if (error) {
3919ced0
MD
923 error = EFAULT;
924 goto done;
984263bc
MD
925 }
926
927 /*
928 * If the map has changed, due to the subyte, the previous
929 * output may be invalid.
930 */
931 vm_map_lock_read(map);
932 if (timestamp != map->timestamp)
933 goto RestartScan;
934
935 lastvecindex = vecindex;
936 addr += PAGE_SIZE;
937 }
938 }
939
940 /*
941 * subyte may page fault. In case it needs to modify
942 * the map, we release the lock.
943 */
944 vm_map_unlock_read(map);
945
946 /*
947 * Zero the last entries in the byte vector.
948 */
949 vecindex = OFF_TO_IDX(end - first_addr);
950 while((lastvecindex + 1) < vecindex) {
951 error = subyte( vec + lastvecindex, 0);
952 if (error) {
3919ced0
MD
953 error = EFAULT;
954 goto done;
984263bc
MD
955 }
956 ++lastvecindex;
957 }
958
959 /*
960 * If the map has changed, due to the subyte, the previous
961 * output may be invalid.
962 */
963 vm_map_lock_read(map);
964 if (timestamp != map->timestamp)
965 goto RestartScan;
966 vm_map_unlock_read(map);
967
3919ced0
MD
968 error = 0;
969done:
b12defdc 970 lwkt_reltoken(&map->token);
3919ced0 971 return (error);
984263bc
MD
972}
973
41c20dac 974/*
3de6dc48
VS
975 * mlock system call handler
976 *
41c20dac 977 * mlock_args(const void *addr, size_t len)
3919ced0 978 *
3de6dc48 979 * No requirements
41c20dac 980 */
984263bc 981int
753fd850 982sys_mlock(struct mlock_args *uap)
984263bc
MD
983{
984 vm_offset_t addr;
e54488bb 985 vm_offset_t tmpaddr;
984263bc 986 vm_size_t size, pageoff;
9910d07b
MD
987 struct thread *td = curthread;
988 struct proc *p = td->td_proc;
3919ced0 989 int error;
984263bc
MD
990
991 addr = (vm_offset_t) uap->addr;
992 size = uap->len;
993
994 pageoff = (addr & PAGE_MASK);
995 addr -= pageoff;
996 size += pageoff;
997 size = (vm_size_t) round_page(size);
e54488bb
MD
998 if (size < uap->len) /* wrap */
999 return(EINVAL);
1000 tmpaddr = addr + size; /* workaround gcc4 opt */
1001 if (tmpaddr < addr) /* wrap */
984263bc
MD
1002 return (EINVAL);
1003
12e4aaff 1004 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired)
984263bc
MD
1005 return (EAGAIN);
1006
8611a340
VS
1007 /*
1008 * We do not need to synchronize against other threads updating ucred;
1009 * they update p->ucred, which is synchronized into td_ucred ourselves.
1010 */
984263bc
MD
1011#ifdef pmap_wired_count
1012 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
3919ced0 1013 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) {
984263bc 1014 return (ENOMEM);
3919ced0 1015 }
984263bc 1016#else
9910d07b 1017 error = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
3919ced0 1018 if (error) {
984263bc 1019 return (error);
3919ced0 1020 }
984263bc 1021#endif
cde87949 1022 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
984263bc
MD
1023 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1024}
1025
41c20dac 1026/*
ed59f8ce 1027 * mlockall(int how)
3919ced0 1028 *
3de6dc48 1029 * No requirements
41c20dac 1030 */
984263bc 1031int
753fd850 1032sys_mlockall(struct mlockall_args *uap)
984263bc 1033{
d3fc9dd8 1034#ifdef _P1003_1B_VISIBLE
ed59f8ce
VS
1035 struct thread *td = curthread;
1036 struct proc *p = td->td_proc;
1037 vm_map_t map = &p->p_vmspace->vm_map;
1038 vm_map_entry_t entry;
1039 int how = uap->how;
1040 int rc = KERN_SUCCESS;
1041
ba197ca7
VS
1042 if (((how & MCL_CURRENT) == 0) && ((how & MCL_FUTURE) == 0))
1043 return (EINVAL);
1044
ed59f8ce
VS
1045 rc = priv_check_cred(td->td_ucred, PRIV_ROOT, 0);
1046 if (rc)
1047 return (rc);
1048
1049 vm_map_lock(map);
1050 do {
1051 if (how & MCL_CURRENT) {
1052 for(entry = map->header.next;
1053 entry != &map->header;
1054 entry = entry->next);
1055
1056 rc = ENOSYS;
1057 break;
1058 }
1059
1060 if (how & MCL_FUTURE)
1061 map->flags |= MAP_WIREFUTURE;
1062 } while(0);
1063 vm_map_unlock(map);
1064
1065 return (rc);
2a5dc8a8 1066#else /* !_P1003_1B_VISIBLE */
d3fc9dd8 1067 return (ENOSYS);
2a5dc8a8 1068#endif /* _P1003_1B_VISIBLE */
984263bc
MD
1069}
1070
41c20dac 1071/*
fcb8bc83 1072 * munlockall(void)
3919ced0 1073 *
fcb8bc83 1074 * Unwire all user-wired map entries, cancel MCL_FUTURE.
3919ced0 1075 *
3de6dc48 1076 * No requirements
41c20dac 1077 */
984263bc 1078int
753fd850 1079sys_munlockall(struct munlockall_args *uap)
984263bc 1080{
fcb8bc83
VS
1081 struct thread *td = curthread;
1082 struct proc *p = td->td_proc;
1083 vm_map_t map = &p->p_vmspace->vm_map;
1084 vm_map_entry_t entry;
1085 int rc = KERN_SUCCESS;
1086
1087 vm_map_lock(map);
1088
1089 /* Clear MAP_WIREFUTURE to cancel mlockall(MCL_FUTURE) */
1090 map->flags &= ~MAP_WIREFUTURE;
1091
1092retry:
1093 for (entry = map->header.next;
1094 entry != &map->header;
1095 entry = entry->next) {
1096 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
1097 continue;
1098
1099 /*
1100 * If we encounter an in-transition entry, we release the
1101 * map lock and retry the scan; we do not decrement any
1102 * wired_count more than once because we do not touch
1103 * any entries with MAP_ENTRY_USER_WIRED not set.
1104 *
1105 * There is a potential interleaving with concurrent
1106 * mlockall()s here -- if we abort a scan, an mlockall()
1107 * could start, wire a number of entries before our
1108 * current position in, and then stall itself on this
1109 * or any other in-transition entry. If that occurs, when
1110 * we resume, we will unwire those entries.
1111 */
1112 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1113 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1114 ++mycpu->gd_cnt.v_intrans_coll;
1115 ++mycpu->gd_cnt.v_intrans_wait;
1116 vm_map_transition_wait(map);
1117 goto retry;
1118 }
1119
1120 KASSERT(entry->wired_count > 0,
1121 ("wired_count was 0 with USER_WIRED set! %p", entry));
1122
1123 /* Drop wired count, if it hits zero, unwire the entry */
1124 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1125 entry->wired_count--;
1126 if (entry->wired_count == 0)
1127 vm_fault_unwire(map, entry);
1128 }
1129
1130 map->timestamp++;
1131 vm_map_unlock(map);
1132
1133 return (rc);
984263bc
MD
1134}
1135
41c20dac 1136/*
3de6dc48
VS
1137 * munlock system call handler
1138 *
41c20dac 1139 * munlock_args(const void *addr, size_t len)
3919ced0 1140 *
3de6dc48 1141 * No requirements
41c20dac 1142 */
984263bc 1143int
753fd850 1144sys_munlock(struct munlock_args *uap)
984263bc 1145{
dadab5e9
MD
1146 struct thread *td = curthread;
1147 struct proc *p = td->td_proc;
984263bc 1148 vm_offset_t addr;
e54488bb 1149 vm_offset_t tmpaddr;
984263bc
MD
1150 vm_size_t size, pageoff;
1151 int error;
1152
1153 addr = (vm_offset_t) uap->addr;
1154 size = uap->len;
1155
1156 pageoff = (addr & PAGE_MASK);
1157 addr -= pageoff;
1158 size += pageoff;
1159 size = (vm_size_t) round_page(size);
1160
e54488bb
MD
1161 tmpaddr = addr + size;
1162 if (tmpaddr < addr) /* wrap */
984263bc
MD
1163 return (EINVAL);
1164
1165#ifndef pmap_wired_count
895c1f85 1166 error = priv_check(td, PRIV_ROOT);
984263bc
MD
1167 if (error)
1168 return (error);
1169#endif
1170
cde87949 1171 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
984263bc
MD
1172 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1173}
1174
1175/*
1176 * Internal version of mmap.
1177 * Currently used by mmap, exec, and sys5 shared memory.
1178 * Handle is either a vnode pointer or NULL for MAP_ANON.
3de6dc48 1179 *
b12defdc 1180 * No requirements
984263bc
MD
1181 */
1182int
1183vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
c809941b 1184 vm_prot_t maxprot, int flags, void *handle, vm_ooffset_t foff)
984263bc
MD
1185{
1186 boolean_t fitit;
1187 vm_object_t object;
85d25bcf
MD
1188 vm_offset_t eaddr;
1189 vm_size_t esize;
7ca5320a 1190 vm_size_t align;
a6e41612 1191 struct vnode *vp;
349433c9 1192 struct thread *td = curthread;
d3313941 1193 struct proc *p;
984263bc 1194 int rv = KERN_SUCCESS;
57f7b636 1195 off_t objsize;
984263bc 1196 int docow;
984263bc
MD
1197
1198 if (size == 0)
1199 return (0);
1200
e54488bb
MD
1201 objsize = round_page(size);
1202 if (objsize < size)
1203 return (EINVAL);
1204 size = objsize;
984263bc 1205
b12defdc 1206 lwkt_gettoken(&map->token);
fcc11a69 1207
d3313941
MD
1208 /*
1209 * XXX messy code, fixme
85d25bcf
MD
1210 *
1211 * NOTE: Overflow checks require discrete statements or GCC4
1212 * will optimize it out.
d3313941
MD
1213 */
1214 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) {
e54488bb 1215 esize = map->size + size; /* workaround gcc4 opt */
85d25bcf
MD
1216 if (esize < map->size ||
1217 esize > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
b12defdc 1218 lwkt_reltoken(&map->token);
d3313941 1219 return(ENOMEM);
85d25bcf 1220 }
984263bc
MD
1221 }
1222
1223 /*
1224 * We currently can only deal with page aligned file offsets.
1225 * The check is here rather than in the syscall because the
1226 * kernel calls this function internally for other mmaping
1227 * operations (such as in exec) and non-aligned offsets will
1228 * cause pmap inconsistencies...so we want to be sure to
1229 * disallow this in all cases.
85d25bcf
MD
1230 *
1231 * NOTE: Overflow checks require discrete statements or GCC4
1232 * will optimize it out.
984263bc 1233 */
fcc11a69 1234 if (foff & PAGE_MASK) {
b12defdc 1235 lwkt_reltoken(&map->token);
984263bc 1236 return (EINVAL);
fcc11a69 1237 }
984263bc 1238
7ca5320a
MD
1239 if (flags & MAP_SIZEALIGN) {
1240 align = size;
1241 if ((align ^ (align - 1)) != (align << 1) - 1) {
1242 lwkt_reltoken(&map->token);
1243 return (EINVAL);
1244 }
1245 } else {
1246 align = PAGE_SIZE;
1247 }
1248
c809941b 1249 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) {
984263bc
MD
1250 fitit = TRUE;
1251 *addr = round_page(*addr);
1252 } else {
fcc11a69 1253 if (*addr != trunc_page(*addr)) {
b12defdc 1254 lwkt_reltoken(&map->token);
984263bc 1255 return (EINVAL);
fcc11a69 1256 }
85d25bcf 1257 eaddr = *addr + size;
fcc11a69 1258 if (eaddr < *addr) {
b12defdc 1259 lwkt_reltoken(&map->token);
85d25bcf 1260 return (EINVAL);
fcc11a69 1261 }
984263bc 1262 fitit = FALSE;
c809941b
MD
1263 if ((flags & MAP_TRYFIXED) == 0)
1264 vm_map_remove(map, *addr, *addr + size);
984263bc
MD
1265 }
1266
1267 /*
1268 * Lookup/allocate object.
1269 */
1270 if (flags & MAP_ANON) {
984263bc
MD
1271 /*
1272 * Unnamed anonymous regions always start at 0.
1273 */
5a648714
MD
1274 if (handle) {
1275 /*
1276 * Default memory object
1277 */
1278 object = default_pager_alloc(handle, objsize,
1279 prot, foff);
fcc11a69 1280 if (object == NULL) {
b12defdc 1281 lwkt_reltoken(&map->token);
5a648714 1282 return(ENOMEM);
fcc11a69 1283 }
5a648714
MD
1284 docow = MAP_PREFAULT_PARTIAL;
1285 } else {
1286 /*
1287 * Implicit single instance of a default memory
1288 * object, so we don't need a VM object yet.
1289 */
984263bc 1290 foff = 0;
5a648714
MD
1291 object = NULL;
1292 docow = 0;
1293 }
a6e41612 1294 vp = NULL;
984263bc 1295 } else {
a6e41612 1296 vp = (struct vnode *)handle;
984263bc 1297 if (vp->v_type == VCHR) {
5a648714
MD
1298 /*
1299 * Device mappings (device size unknown?).
1300 * Force them to be shared.
1301 */
984263bc 1302 handle = (void *)(intptr_t)vp->v_rdev;
5a648714 1303 object = dev_pager_alloc(handle, objsize, prot, foff);
fcc11a69 1304 if (object == NULL) {
b12defdc 1305 lwkt_reltoken(&map->token);
5a648714 1306 return(EINVAL);
fcc11a69 1307 }
5a648714
MD
1308 docow = MAP_PREFAULT_PARTIAL;
1309 flags &= ~(MAP_PRIVATE|MAP_COPY);
1310 flags |= MAP_SHARED;
984263bc 1311 } else {
5a648714
MD
1312 /*
1313 * Regular file mapping (typically). The attribute
1314 * check is for the link count test only. Mmapble
1315 * vnodes must already have a VM object assigned.
1316 */
f9cae863 1317 struct vattr vat;
984263bc
MD
1318 int error;
1319
87de5057 1320 error = VOP_GETATTR(vp, &vat);
fcc11a69 1321 if (error) {
b12defdc 1322 lwkt_reltoken(&map->token);
984263bc 1323 return (error);
fcc11a69 1324 }
5a648714
MD
1325 docow = MAP_PREFAULT_PARTIAL;
1326 object = vnode_pager_reference(vp);
1327 if (object == NULL && vp->v_type == VREG) {
b12defdc 1328 lwkt_reltoken(&map->token);
5a648714
MD
1329 kprintf("Warning: cannot mmap vnode %p, no "
1330 "object\n", vp);
1331 return(EINVAL);
1332 }
1333
984263bc 1334 /*
5a648714 1335 * If it is a regular file without any references
984263bc
MD
1336 * we do not need to sync it.
1337 */
1338 if (vp->v_type == VREG && vat.va_nlink == 0) {
1339 flags |= MAP_NOSYNC;
1340 }
1341 }
1342 }
1343
984263bc 1344 /*
5a648714 1345 * Deal with the adjusted flags
984263bc 1346 */
984263bc
MD
1347 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1348 docow |= MAP_COPY_ON_WRITE;
1349 if (flags & MAP_NOSYNC)
1350 docow |= MAP_DISABLE_SYNCER;
1351 if (flags & MAP_NOCORE)
1352 docow |= MAP_DISABLE_COREDUMP;
1353
1354#if defined(VM_PROT_READ_IS_EXEC)
1355 if (prot & VM_PROT_READ)
1356 prot |= VM_PROT_EXECUTE;
1357
1358 if (maxprot & VM_PROT_READ)
1359 maxprot |= VM_PROT_EXECUTE;
1360#endif
1361
c809941b
MD
1362 /*
1363 * This may place the area in its own page directory if (size) is
1364 * large enough, otherwise it typically returns its argument.
1365 */
984263bc
MD
1366 if (fitit) {
1367 *addr = pmap_addr_hint(object, *addr, size);
1368 }
1369
568e6804 1370 /*
c809941b
MD
1371 * Stack mappings need special attention.
1372 *
1373 * Mappings that use virtual page tables will default to storing
1374 * the page table at offset 0.
568e6804 1375 */
1b874851 1376 if (flags & MAP_STACK) {
c809941b 1377 rv = vm_map_stack(map, *addr, size, flags,
85d25bcf 1378 prot, maxprot, docow);
568e6804 1379 } else if (flags & MAP_VPAGETABLE) {
7ca5320a 1380 rv = vm_map_find(map, object, foff, addr, size, align,
9388fcaa
MD
1381 fitit, VM_MAPTYPE_VPAGETABLE,
1382 prot, maxprot, docow);
1b874851 1383 } else {
7ca5320a 1384 rv = vm_map_find(map, object, foff, addr, size, align,
9388fcaa
MD
1385 fitit, VM_MAPTYPE_NORMAL,
1386 prot, maxprot, docow);
1b874851 1387 }
984263bc
MD
1388
1389 if (rv != KERN_SUCCESS) {
1390 /*
1391 * Lose the object reference. Will destroy the
1392 * object if it's an unnamed anonymous mapping
1393 * or named anonymous without other references.
1394 */
1395 vm_object_deallocate(object);
1396 goto out;
1397 }
1398
1399 /*
1400 * Shared memory is also shared with children.
1401 */
1402 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1403 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1404 if (rv != KERN_SUCCESS) {
418ff780 1405 vm_map_remove(map, *addr, *addr + size);
984263bc
MD
1406 goto out;
1407 }
1408 }
349433c9 1409
7c553423
VS
1410 /* If a process has marked all future mappings for wiring, do so */
1411 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1412 vm_map_unwire(map, *addr, *addr + size, FALSE);
1413
349433c9
MD
1414 /*
1415 * Set the access time on the vnode
1416 */
a6e41612
MD
1417 if (vp != NULL)
1418 vn_mark_atime(vp, td);
984263bc 1419out:
b12defdc 1420 lwkt_reltoken(&map->token);
fcc11a69 1421
984263bc
MD
1422 switch (rv) {
1423 case KERN_SUCCESS:
1424 return (0);
1425 case KERN_INVALID_ADDRESS:
1426 case KERN_NO_SPACE:
1427 return (ENOMEM);
1428 case KERN_PROTECTION_FAILURE:
1429 return (EACCES);
1430 default:
1431 return (EINVAL);
1432 }
1433}