FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_mmap.c
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$
42 */
43
44 /*
45 * Mapped file (mmap) interface to VM
46 */
47
48 #include "opt_compat.h"
49 #include "opt_rlimit.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/sysproto.h>
54 #include <sys/filedesc.h>
55 #include <sys/proc.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
64 #include <miscfs/specfs/specdev.h>
65
66 #include <vm/vm.h>
67 #include <vm/vm_param.h>
68 #include <vm/vm_prot.h>
69 #include <vm/vm_inherit.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_pager.h>
75 #include <vm/vm_pageout.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_page.h>
78
79 #ifndef _SYS_SYSPROTO_H_
80 struct sbrk_args {
81 int incr;
82 };
83 #endif
84
85 /* ARGSUSED */
86 int
87 sbrk(p, uap)
88 struct proc *p;
89 struct sbrk_args *uap;
90 {
91
92 /* Not yet implemented */
93 return (EOPNOTSUPP);
94 }
95
96 #ifndef _SYS_SYSPROTO_H_
97 struct sstk_args {
98 int incr;
99 };
100 #endif
101
102 /* ARGSUSED */
103 int
104 sstk(p, uap)
105 struct proc *p;
106 struct sstk_args *uap;
107 {
108
109 /* Not yet implemented */
110 return (EOPNOTSUPP);
111 }
112
113 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
114 #ifndef _SYS_SYSPROTO_H_
115 struct getpagesize_args {
116 int dummy;
117 };
118 #endif
119
120 /* ARGSUSED */
121 int
122 ogetpagesize(p, uap)
123 struct proc *p;
124 struct getpagesize_args *uap;
125 {
126
127 p->p_retval[0] = PAGE_SIZE;
128 return (0);
129 }
130 #endif /* COMPAT_43 || COMPAT_SUNOS */
131
132
133 /*
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 #ifndef _SYS_SYSPROTO_H_
142 struct mmap_args {
143 void *addr;
144 size_t len;
145 int prot;
146 int flags;
147 int fd;
148 long pad;
149 off_t pos;
150 };
151 #endif
152
153 int
154 mmap(p, uap)
155 struct proc *p;
156 register struct mmap_args *uap;
157 {
158 register struct filedesc *fdp = p->p_fd;
159 register struct file *fp;
160 struct vnode *vp;
161 vm_offset_t addr;
162 vm_size_t size, pageoff;
163 vm_prot_t prot, maxprot;
164 void *handle;
165 int flags, error;
166 int disablexworkaround;
167 off_t pos;
168
169 addr = (vm_offset_t) uap->addr;
170 size = uap->len;
171 prot = uap->prot & VM_PROT_ALL;
172 flags = uap->flags;
173 pos = uap->pos;
174
175 /* make sure mapping fits into numeric range etc */
176 if ((ssize_t) uap->len < 0 ||
177 ((flags & MAP_ANON) && uap->fd != -1))
178 return (EINVAL);
179
180 if (flags & MAP_STACK) {
181 #ifdef VM_STACK
182 if ((uap->fd != -1) ||
183 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
184 return (EINVAL);
185 flags |= MAP_ANON;
186 pos = 0;
187 #else
188 return (EINVAL);
189 #endif
190 }
191
192 /*
193 * Align the file position to a page boundary,
194 * and save its page offset component.
195 */
196 pageoff = (pos & PAGE_MASK);
197 pos -= pageoff;
198
199 /* Adjust size for rounding (on both ends). */
200 size += pageoff; /* low end... */
201 size = (vm_size_t) round_page(size); /* hi end */
202
203 /*
204 * Check for illegal addresses. Watch out for address wrap... Note
205 * that VM_*_ADDRESS are not constants due to casts (argh).
206 */
207 if (flags & MAP_FIXED) {
208 /*
209 * The specified address must have the same remainder
210 * as the file offset taken modulo PAGE_SIZE, so it
211 * should be aligned after adjustment by pageoff.
212 */
213 addr -= pageoff;
214 if (addr & PAGE_MASK)
215 return (EINVAL);
216 /* Address range must be all in user VM space. */
217 if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
218 return (EINVAL);
219 #ifndef i386
220 if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
221 return (EINVAL);
222 #endif
223 if (addr + size < addr)
224 return (EINVAL);
225 }
226 /*
227 * XXX for non-fixed mappings where no hint is provided or
228 * the hint would fall in the potential heap space,
229 * place it after the end of the largest possible heap.
230 *
231 * There should really be a pmap call to determine a reasonable
232 * location.
233 */
234 else if (addr == 0 ||
235 addr >= round_page((vm_offset_t)p->p_vmspace->vm_taddr) &&
236 addr < round_page((vm_offset_t)p->p_vmspace->vm_daddr + MAXDSIZ))
237 addr = round_page((vm_offset_t)p->p_vmspace->vm_daddr + MAXDSIZ);
238
239 if (flags & MAP_ANON) {
240 /*
241 * Mapping blank space is trivial.
242 */
243 handle = NULL;
244 maxprot = VM_PROT_ALL;
245 pos = 0;
246 } else {
247 /*
248 * Mapping file, get fp for validation. Obtain vnode and make
249 * sure it is of appropriate type.
250 */
251 if (((unsigned) uap->fd) >= fdp->fd_nfiles ||
252 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
253 return (EBADF);
254 if (fp->f_type != DTYPE_VNODE)
255 return (EINVAL);
256 vp = (struct vnode *) fp->f_data;
257 if (vp->v_type != VREG && vp->v_type != VCHR)
258 return (EINVAL);
259 /*
260 * XXX hack to handle use of /dev/zero to map anon memory (ala
261 * SunOS).
262 */
263 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
264 handle = NULL;
265 maxprot = VM_PROT_ALL;
266 flags |= MAP_ANON;
267 pos = 0;
268 } else {
269 /*
270 * cdevs does not provide private mappings of any kind.
271 */
272 /*
273 * However, for XIG X server to continue to work,
274 * we should allow the superuser to do it anyway.
275 * We only allow it at securelevel < 1.
276 * (Because the XIG X server writes directly to video
277 * memory via /dev/mem, it should never work at any
278 * other securelevel.
279 * XXX this will have to go
280 */
281 if (securelevel >= 1)
282 disablexworkaround = 1;
283 else
284 disablexworkaround = suser(p->p_ucred,
285 &p->p_acflag);
286 if (vp->v_type == VCHR && disablexworkaround &&
287 (flags & (MAP_PRIVATE|MAP_COPY)))
288 return (EINVAL);
289 /*
290 * Ensure that file and memory protections are
291 * compatible. Note that we only worry about
292 * writability if mapping is shared; in this case,
293 * current and max prot are dictated by the open file.
294 * XXX use the vnode instead? Problem is: what
295 * credentials do we use for determination? What if
296 * proc does a setuid?
297 */
298 maxprot = VM_PROT_EXECUTE; /* ??? */
299 if (fp->f_flag & FREAD)
300 maxprot |= VM_PROT_READ;
301 else if (prot & PROT_READ)
302 return (EACCES);
303 /*
304 * If we are sharing potential changes (either via
305 * MAP_SHARED or via the implicit sharing of character
306 * device mappings), and we are trying to get write
307 * permission although we opened it without asking
308 * for it, bail out. Check for superuser, only if
309 * we're at securelevel < 1, to allow the XIG X server
310 * to continue to work.
311 */
312
313 if ((flags & MAP_SHARED) != 0 ||
314 (vp->v_type == VCHR && disablexworkaround)) {
315 if ((fp->f_flag & FWRITE) != 0) {
316 struct vattr va;
317 if ((error =
318 VOP_GETATTR(vp, &va,
319 p->p_ucred, p)))
320 return (error);
321 if ((va.va_flags &
322 (IMMUTABLE|APPEND)) == 0)
323 maxprot |= VM_PROT_WRITE;
324 else if (prot & PROT_WRITE)
325 return (EPERM);
326 } else if ((prot & PROT_WRITE) != 0)
327 return (EACCES);
328 } else
329 maxprot |= VM_PROT_WRITE;
330
331 handle = (void *)vp;
332 }
333 }
334 error = vm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
335 flags, handle, pos);
336 if (error == 0)
337 p->p_retval[0] = (register_t) (addr + pageoff);
338 return (error);
339 }
340
341 #ifdef COMPAT_43
342 #ifndef _SYS_SYSPROTO_H_
343 struct ommap_args {
344 caddr_t addr;
345 int len;
346 int prot;
347 int flags;
348 int fd;
349 long pos;
350 };
351 #endif
352 int
353 ommap(p, uap)
354 struct proc *p;
355 register struct ommap_args *uap;
356 {
357 struct mmap_args nargs;
358 static const char cvtbsdprot[8] = {
359 0,
360 PROT_EXEC,
361 PROT_WRITE,
362 PROT_EXEC | PROT_WRITE,
363 PROT_READ,
364 PROT_EXEC | PROT_READ,
365 PROT_WRITE | PROT_READ,
366 PROT_EXEC | PROT_WRITE | PROT_READ,
367 };
368
369 #define OMAP_ANON 0x0002
370 #define OMAP_COPY 0x0020
371 #define OMAP_SHARED 0x0010
372 #define OMAP_FIXED 0x0100
373 #define OMAP_INHERIT 0x0800
374
375 nargs.addr = uap->addr;
376 nargs.len = uap->len;
377 nargs.prot = cvtbsdprot[uap->prot & 0x7];
378 nargs.flags = 0;
379 if (uap->flags & OMAP_ANON)
380 nargs.flags |= MAP_ANON;
381 if (uap->flags & OMAP_COPY)
382 nargs.flags |= MAP_COPY;
383 if (uap->flags & OMAP_SHARED)
384 nargs.flags |= MAP_SHARED;
385 else
386 nargs.flags |= MAP_PRIVATE;
387 if (uap->flags & OMAP_FIXED)
388 nargs.flags |= MAP_FIXED;
389 if (uap->flags & OMAP_INHERIT)
390 nargs.flags |= MAP_INHERIT;
391 nargs.fd = uap->fd;
392 nargs.pos = uap->pos;
393 return (mmap(p, &nargs));
394 }
395 #endif /* COMPAT_43 */
396
397
398 #ifndef _SYS_SYSPROTO_H_
399 struct msync_args {
400 void *addr;
401 int len;
402 int flags;
403 };
404 #endif
405 int
406 msync(p, uap)
407 struct proc *p;
408 struct msync_args *uap;
409 {
410 vm_offset_t addr;
411 vm_size_t size, pageoff;
412 int flags;
413 vm_map_t map;
414 int rv;
415
416 addr = (vm_offset_t) uap->addr;
417 size = uap->len;
418 flags = uap->flags;
419
420 pageoff = (addr & PAGE_MASK);
421 addr -= pageoff;
422 size += pageoff;
423 size = (vm_size_t) round_page(size);
424 if (addr + size < addr)
425 return(EINVAL);
426
427 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
428 return (EINVAL);
429
430 map = &p->p_vmspace->vm_map;
431
432 /*
433 * XXX Gak! If size is zero we are supposed to sync "all modified
434 * pages with the region containing addr". Unfortunately, we don't
435 * really keep track of individual mmaps so we approximate by flushing
436 * the range of the map entry containing addr. This can be incorrect
437 * if the region splits or is coalesced with a neighbor.
438 */
439 if (size == 0) {
440 vm_map_entry_t entry;
441
442 vm_map_lock_read(map);
443 rv = vm_map_lookup_entry(map, addr, &entry);
444 vm_map_unlock_read(map);
445 if (rv == FALSE)
446 return (EINVAL);
447 addr = entry->start;
448 size = entry->end - entry->start;
449 }
450
451 /*
452 * Clean the pages and interpret the return value.
453 */
454 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
455 (flags & MS_INVALIDATE) != 0);
456
457 switch (rv) {
458 case KERN_SUCCESS:
459 break;
460 case KERN_INVALID_ADDRESS:
461 return (EINVAL); /* Sun returns ENOMEM? */
462 case KERN_FAILURE:
463 return (EIO);
464 default:
465 return (EINVAL);
466 }
467
468 return (0);
469 }
470
471 #ifndef _SYS_SYSPROTO_H_
472 struct munmap_args {
473 void *addr;
474 size_t len;
475 };
476 #endif
477 int
478 munmap(p, uap)
479 register struct proc *p;
480 register struct munmap_args *uap;
481 {
482 vm_offset_t addr;
483 vm_size_t size, pageoff;
484 vm_map_t map;
485
486 addr = (vm_offset_t) uap->addr;
487 size = uap->len;
488
489 pageoff = (addr & PAGE_MASK);
490 addr -= pageoff;
491 size += pageoff;
492 size = (vm_size_t) round_page(size);
493 if (addr + size < addr)
494 return(EINVAL);
495
496 if (size == 0)
497 return (0);
498
499 /*
500 * Check for illegal addresses. Watch out for address wrap... Note
501 * that VM_*_ADDRESS are not constants due to casts (argh).
502 */
503 if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
504 return (EINVAL);
505 #ifndef i386
506 if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
507 return (EINVAL);
508 #endif
509 map = &p->p_vmspace->vm_map;
510 /*
511 * Make sure entire range is allocated.
512 */
513 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
514 return (EINVAL);
515 /* returns nothing but KERN_SUCCESS anyway */
516 (void) vm_map_remove(map, addr, addr + size);
517 return (0);
518 }
519
520 void
521 munmapfd(p, fd)
522 struct proc *p;
523 int fd;
524 {
525 /*
526 * XXX should unmap any regions mapped to this file
527 */
528 p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
529 }
530
531 #ifndef _SYS_SYSPROTO_H_
532 struct mprotect_args {
533 const void *addr;
534 size_t len;
535 int prot;
536 };
537 #endif
538 int
539 mprotect(p, uap)
540 struct proc *p;
541 struct mprotect_args *uap;
542 {
543 vm_offset_t addr;
544 vm_size_t size, pageoff;
545 register vm_prot_t prot;
546
547 addr = (vm_offset_t) uap->addr;
548 size = uap->len;
549 prot = uap->prot & VM_PROT_ALL;
550 #if defined(VM_PROT_READ_IS_EXEC)
551 if (prot & VM_PROT_READ)
552 prot |= VM_PROT_EXECUTE;
553 #endif
554
555 pageoff = (addr & PAGE_MASK);
556 addr -= pageoff;
557 size += pageoff;
558 size = (vm_size_t) round_page(size);
559 if (addr + size < addr)
560 return(EINVAL);
561
562 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
563 FALSE)) {
564 case KERN_SUCCESS:
565 return (0);
566 case KERN_PROTECTION_FAILURE:
567 return (EACCES);
568 }
569 return (EINVAL);
570 }
571
572 #ifndef _SYS_SYSPROTO_H_
573 struct minherit_args {
574 void *addr;
575 size_t len;
576 int inherit;
577 };
578 #endif
579 int
580 minherit(p, uap)
581 struct proc *p;
582 struct minherit_args *uap;
583 {
584 vm_offset_t addr;
585 vm_size_t size, pageoff;
586 register vm_inherit_t inherit;
587
588 addr = (vm_offset_t)uap->addr;
589 size = uap->len;
590 inherit = uap->inherit;
591
592 pageoff = (addr & PAGE_MASK);
593 addr -= pageoff;
594 size += pageoff;
595 size = (vm_size_t) round_page(size);
596 if (addr + size < addr)
597 return(EINVAL);
598
599 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size,
600 inherit)) {
601 case KERN_SUCCESS:
602 return (0);
603 case KERN_PROTECTION_FAILURE:
604 return (EACCES);
605 }
606 return (EINVAL);
607 }
608
609 #ifndef _SYS_SYSPROTO_H_
610 struct madvise_args {
611 void *addr;
612 size_t len;
613 int behav;
614 };
615 #endif
616
617 /* ARGSUSED */
618 int
619 madvise(p, uap)
620 struct proc *p;
621 struct madvise_args *uap;
622 {
623 vm_map_t map;
624 pmap_t pmap;
625 vm_offset_t start, end;
626 /*
627 * Check for illegal addresses. Watch out for address wrap... Note
628 * that VM_*_ADDRESS are not constants due to casts (argh).
629 */
630 if (VM_MAXUSER_ADDRESS > 0 &&
631 ((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS)
632 return (EINVAL);
633 #ifndef i386
634 if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS)
635 return (EINVAL);
636 #endif
637 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
638 return (EINVAL);
639
640 /*
641 * Since this routine is only advisory, we default to conservative
642 * behavior.
643 */
644 start = trunc_page((vm_offset_t) uap->addr);
645 end = round_page((vm_offset_t) uap->addr + uap->len);
646
647 map = &p->p_vmspace->vm_map;
648 pmap = &p->p_vmspace->vm_pmap;
649
650 vm_map_madvise(map, pmap, start, end, uap->behav);
651
652 return (0);
653 }
654
655 #ifndef _SYS_SYSPROTO_H_
656 struct mincore_args {
657 const void *addr;
658 size_t len;
659 char *vec;
660 };
661 #endif
662
663 /* ARGSUSED */
664 int
665 mincore(p, uap)
666 struct proc *p;
667 struct mincore_args *uap;
668 {
669 vm_offset_t addr, first_addr;
670 vm_offset_t end, cend;
671 pmap_t pmap;
672 vm_map_t map;
673 char *vec;
674 int error;
675 int vecindex, lastvecindex;
676 register vm_map_entry_t current;
677 vm_map_entry_t entry;
678 int mincoreinfo;
679
680 /*
681 * Make sure that the addresses presented are valid for user
682 * mode.
683 */
684 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
685 end = addr + (vm_size_t)round_page(uap->len);
686 if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS)
687 return (EINVAL);
688 if (end < addr)
689 return (EINVAL);
690
691 /*
692 * Address of byte vector
693 */
694 vec = uap->vec;
695
696 map = &p->p_vmspace->vm_map;
697 pmap = &p->p_vmspace->vm_pmap;
698
699 vm_map_lock(map);
700
701 if (!vm_map_lookup_entry(map, addr, &entry))
702 entry = entry->next;
703
704 /*
705 * Do this on a map entry basis so that if the pages are not
706 * in the current processes address space, we can easily look
707 * up the pages elsewhere.
708 */
709 lastvecindex = -1;
710 for(current = entry;
711 (current != &map->header) && (current->start < end);
712 current = current->next) {
713
714 /*
715 * ignore submaps (for now) or null objects
716 */
717 if ((current->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) ||
718 current->object.vm_object == NULL)
719 continue;
720
721 /*
722 * limit this scan to the current map entry and the
723 * limits for the mincore call
724 */
725 if (addr < current->start)
726 addr = current->start;
727 cend = current->end;
728 if (cend > end)
729 cend = end;
730
731 /*
732 * scan this entry one page at a time
733 */
734 while(addr < cend) {
735 /*
736 * Check pmap first, it is likely faster, also
737 * it can provide info as to whether we are the
738 * one referencing or modifying the page.
739 */
740 mincoreinfo = pmap_mincore(pmap, addr);
741 if (!mincoreinfo) {
742 vm_pindex_t pindex;
743 vm_ooffset_t offset;
744 vm_page_t m;
745 /*
746 * calculate the page index into the object
747 */
748 offset = current->offset + (addr - current->start);
749 pindex = OFF_TO_IDX(offset);
750 m = vm_page_lookup(current->object.vm_object,
751 pindex);
752 /*
753 * if the page is resident, then gather information about
754 * it.
755 */
756 if (m) {
757 mincoreinfo = MINCORE_INCORE;
758 if (m->dirty ||
759 pmap_is_modified(VM_PAGE_TO_PHYS(m)))
760 mincoreinfo |= MINCORE_MODIFIED_OTHER;
761 if ((m->flags & PG_REFERENCED) ||
762 pmap_ts_referenced(VM_PAGE_TO_PHYS(m))) {
763 vm_page_flag_set(m, PG_REFERENCED);
764 mincoreinfo |= MINCORE_REFERENCED_OTHER;
765 }
766 }
767 }
768
769 /*
770 * calculate index into user supplied byte vector
771 */
772 vecindex = OFF_TO_IDX(addr - first_addr);
773
774 /*
775 * If we have skipped map entries, we need to make sure that
776 * the byte vector is zeroed for those skipped entries.
777 */
778 while((lastvecindex + 1) < vecindex) {
779 error = subyte( vec + lastvecindex, 0);
780 if (error) {
781 vm_map_unlock(map);
782 return (EFAULT);
783 }
784 ++lastvecindex;
785 }
786
787 /*
788 * Pass the page information to the user
789 */
790 error = subyte( vec + vecindex, mincoreinfo);
791 if (error) {
792 vm_map_unlock(map);
793 return (EFAULT);
794 }
795 lastvecindex = vecindex;
796 addr += PAGE_SIZE;
797 }
798 }
799
800 /*
801 * Zero the last entries in the byte vector.
802 */
803 vecindex = OFF_TO_IDX(end - first_addr);
804 while((lastvecindex + 1) < vecindex) {
805 error = subyte( vec + lastvecindex, 0);
806 if (error) {
807 vm_map_unlock(map);
808 return (EFAULT);
809 }
810 ++lastvecindex;
811 }
812
813 vm_map_unlock(map);
814 return (0);
815 }
816
817 #ifndef _SYS_SYSPROTO_H_
818 struct mlock_args {
819 const void *addr;
820 size_t len;
821 };
822 #endif
823 int
824 mlock(p, uap)
825 struct proc *p;
826 struct mlock_args *uap;
827 {
828 vm_offset_t addr;
829 vm_size_t size, pageoff;
830 int error;
831
832 addr = (vm_offset_t) uap->addr;
833 size = uap->len;
834
835 pageoff = (addr & PAGE_MASK);
836 addr -= pageoff;
837 size += pageoff;
838 size = (vm_size_t) round_page(size);
839
840 /* disable wrap around */
841 if (addr + size < addr)
842 return (EINVAL);
843
844 if (atop(size) + cnt.v_wire_count > vm_page_max_wired)
845 return (EAGAIN);
846
847 #ifdef pmap_wired_count
848 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
849 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
850 return (ENOMEM);
851 #else
852 error = suser(p->p_ucred, &p->p_acflag);
853 if (error)
854 return (error);
855 #endif
856
857 error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
858 return (error == KERN_SUCCESS ? 0 : ENOMEM);
859 }
860
861 #ifndef _SYS_SYSPROTO_H_
862 struct mlockall_args {
863 int how;
864 };
865 #endif
866
867 int
868 mlockall(p, uap)
869 struct proc *p;
870 struct mlockall_args *uap;
871 {
872 return 0;
873 }
874
875 #ifndef _SYS_SYSPROTO_H_
876 struct mlockall_args {
877 int how;
878 };
879 #endif
880
881 int
882 munlockall(p, uap)
883 struct proc *p;
884 struct munlockall_args *uap;
885 {
886 return 0;
887 }
888
889 #ifndef _SYS_SYSPROTO_H_
890 struct munlock_args {
891 const void *addr;
892 size_t len;
893 };
894 #endif
895 int
896 munlock(p, uap)
897 struct proc *p;
898 struct munlock_args *uap;
899 {
900 vm_offset_t addr;
901 vm_size_t size, pageoff;
902 int error;
903
904 addr = (vm_offset_t) uap->addr;
905 size = uap->len;
906
907 pageoff = (addr & PAGE_MASK);
908 addr -= pageoff;
909 size += pageoff;
910 size = (vm_size_t) round_page(size);
911
912 /* disable wrap around */
913 if (addr + size < addr)
914 return (EINVAL);
915
916 #ifndef pmap_wired_count
917 error = suser(p->p_ucred, &p->p_acflag);
918 if (error)
919 return (error);
920 #endif
921
922 error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
923 return (error == KERN_SUCCESS ? 0 : ENOMEM);
924 }
925
926 /*
927 * Internal version of mmap.
928 * Currently used by mmap, exec, and sys5 shared memory.
929 * Handle is either a vnode pointer or NULL for MAP_ANON.
930 */
931 int
932 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
933 vm_prot_t maxprot, int flags,
934 void *handle,
935 vm_ooffset_t foff)
936 {
937 boolean_t fitit;
938 vm_object_t object;
939 struct vnode *vp = NULL;
940 objtype_t type;
941 int rv = KERN_SUCCESS;
942 vm_ooffset_t objsize;
943 int docow;
944 struct proc *p = curproc;
945
946 if (size == 0)
947 return (0);
948
949 objsize = size = round_page(size);
950
951 /*
952 * We currently can only deal with page aligned file offsets.
953 * The check is here rather than in the syscall because the
954 * kernel calls this function internally for other mmaping
955 * operations (such as in exec) and non-aligned offsets will
956 * cause pmap inconsistencies...so we want to be sure to
957 * disallow this in all cases.
958 */
959 if (foff & PAGE_MASK)
960 return (EINVAL);
961
962 if ((flags & MAP_FIXED) == 0) {
963 fitit = TRUE;
964 *addr = round_page(*addr);
965 } else {
966 if (*addr != trunc_page(*addr))
967 return (EINVAL);
968 fitit = FALSE;
969 (void) vm_map_remove(map, *addr, *addr + size);
970 }
971
972 /*
973 * Lookup/allocate object.
974 */
975 if (flags & MAP_ANON) {
976 type = OBJT_DEFAULT;
977 /*
978 * Unnamed anonymous regions always start at 0.
979 */
980 if (handle == 0)
981 foff = 0;
982 } else {
983 vp = (struct vnode *) handle;
984 if (vp->v_type == VCHR) {
985 type = OBJT_DEVICE;
986 handle = (void *)(intptr_t)vp->v_rdev;
987 } else {
988 struct vattr vat;
989 int error;
990
991 error = VOP_GETATTR(vp, &vat, p->p_ucred, p);
992 if (error)
993 return (error);
994 objsize = round_page(vat.va_size);
995 type = OBJT_VNODE;
996 }
997 }
998
999 if (handle == NULL) {
1000 object = NULL;
1001 } else {
1002 object = vm_pager_allocate(type,
1003 handle, objsize, prot, foff);
1004 if (object == NULL)
1005 return (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1006 }
1007
1008 /*
1009 * Force device mappings to be shared.
1010 */
1011 if (type == OBJT_DEVICE) {
1012 flags &= ~(MAP_PRIVATE|MAP_COPY);
1013 flags |= MAP_SHARED;
1014 }
1015
1016 docow = 0;
1017 if ((flags & (MAP_ANON|MAP_SHARED)) == 0) {
1018 docow = MAP_COPY_ON_WRITE | MAP_COPY_NEEDED;
1019 }
1020
1021 #if defined(VM_PROT_READ_IS_EXEC)
1022 if (prot & VM_PROT_READ)
1023 prot |= VM_PROT_EXECUTE;
1024
1025 if (maxprot & VM_PROT_READ)
1026 maxprot |= VM_PROT_EXECUTE;
1027 #endif
1028
1029 if (fitit) {
1030 *addr = pmap_addr_hint(object, *addr, size);
1031 }
1032
1033 #ifdef VM_STACK
1034 if (flags & MAP_STACK)
1035 rv = vm_map_stack (map, *addr, size, prot,
1036 maxprot, docow);
1037 else
1038 #endif
1039 rv = vm_map_find(map, object, foff, addr, size, fitit,
1040 prot, maxprot, docow);
1041
1042 if (rv != KERN_SUCCESS) {
1043 /*
1044 * Lose the object reference. Will destroy the
1045 * object if it's an unnamed anonymous mapping
1046 * or named anonymous without other references.
1047 */
1048 vm_object_deallocate(object);
1049 goto out;
1050 }
1051
1052 /*
1053 * "Pre-fault" resident pages.
1054 */
1055 if ((map->pmap != NULL) && (object != NULL)) {
1056 pmap_object_init_pt(map->pmap, *addr,
1057 object, (vm_pindex_t) OFF_TO_IDX(foff), size, 1);
1058 }
1059
1060 /*
1061 * Shared memory is also shared with children.
1062 */
1063 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1064 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1065 if (rv != KERN_SUCCESS) {
1066 (void) vm_map_remove(map, *addr, *addr + size);
1067 goto out;
1068 }
1069 }
1070 out:
1071 switch (rv) {
1072 case KERN_SUCCESS:
1073 return (0);
1074 case KERN_INVALID_ADDRESS:
1075 case KERN_NO_SPACE:
1076 return (ENOMEM);
1077 case KERN_PROTECTION_FAILURE:
1078 return (EACCES);
1079 default:
1080 return (EINVAL);
1081 }
1082 }
Cache object: 99d32cda2558a612df5aa3af91fcedb5
|