1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
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.
20 * 3. 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 */
40
41 /*
42 * Mapped file (mmap) interface to VM
43 */
44
45 #include <sys/cdefs.h>
46 #include "opt_hwpmc_hooks.h"
47 #include "opt_vm.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/capsicum.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/elf.h>
57 #include <sys/filedesc.h>
58 #include <sys/priv.h>
59 #include <sys/proc.h>
60 #include <sys/procctl.h>
61 #include <sys/racct.h>
62 #include <sys/resource.h>
63 #include <sys/resourcevar.h>
64 #include <sys/rwlock.h>
65 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
67 #include <sys/fcntl.h>
68 #include <sys/file.h>
69 #include <sys/mman.h>
70 #include <sys/mount.h>
71 #include <sys/conf.h>
72 #include <sys/stat.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysent.h>
75 #include <sys/vmmeter.h>
76 #if defined(__amd64__) || defined(__i386__) /* for i386_read_exec */
77 #include <machine/md_var.h>
78 #endif
79
80 #include <security/audit/audit.h>
81 #include <security/mac/mac_framework.h>
82
83 #include <vm/vm.h>
84 #include <vm/vm_param.h>
85 #include <vm/pmap.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_pager.h>
90 #include <vm/vm_pageout.h>
91 #include <vm/vm_extern.h>
92 #include <vm/vm_page.h>
93 #include <vm/vnode_pager.h>
94
95 #ifdef HWPMC_HOOKS
96 #include <sys/pmckern.h>
97 #endif
98
99 int old_mlock = 0;
100 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RWTUN, &old_mlock, 0,
101 "Do not apply RLIMIT_MEMLOCK on mlockall");
102 static int mincore_mapped = 1;
103 SYSCTL_INT(_vm, OID_AUTO, mincore_mapped, CTLFLAG_RWTUN, &mincore_mapped, 0,
104 "mincore reports mappings, not residency");
105 static int imply_prot_max = 0;
106 SYSCTL_INT(_vm, OID_AUTO, imply_prot_max, CTLFLAG_RWTUN, &imply_prot_max, 0,
107 "Imply maximum page protections in mmap() when none are specified");
108
109 #ifdef MAP_32BIT
110 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
111 #endif
112
113 _Static_assert(MAXPAGESIZES <= 4, "MINCORE_SUPER too narrow");
114
115 #ifndef _SYS_SYSPROTO_H_
116 struct sbrk_args {
117 int incr;
118 };
119 #endif
120
121 int
sys_sbrk(struct thread * td,struct sbrk_args * uap)122 sys_sbrk(struct thread *td, struct sbrk_args *uap)
123 {
124 /* Not yet implemented */
125 return (EOPNOTSUPP);
126 }
127
128 #ifndef _SYS_SYSPROTO_H_
129 struct sstk_args {
130 int incr;
131 };
132 #endif
133
134 int
sys_sstk(struct thread * td,struct sstk_args * uap)135 sys_sstk(struct thread *td, struct sstk_args *uap)
136 {
137 /* Not yet implemented */
138 return (EOPNOTSUPP);
139 }
140
141 #if defined(COMPAT_43)
142 int
ogetpagesize(struct thread * td,struct ogetpagesize_args * uap)143 ogetpagesize(struct thread *td, struct ogetpagesize_args *uap)
144 {
145
146 td->td_retval[0] = PAGE_SIZE;
147 return (0);
148 }
149 #endif /* COMPAT_43 */
150
151 /*
152 * Memory Map (mmap) system call. Note that the file offset
153 * and address are allowed to be NOT page aligned, though if
154 * the MAP_FIXED flag it set, both must have the same remainder
155 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
156 * page-aligned, the actual mapping starts at trunc_page(addr)
157 * and the return value is adjusted up by the page offset.
158 *
159 * Generally speaking, only character devices which are themselves
160 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
161 * there would be no cache coherency between a descriptor and a VM mapping
162 * both to the same character device.
163 */
164 #ifndef _SYS_SYSPROTO_H_
165 struct mmap_args {
166 void *addr;
167 size_t len;
168 int prot;
169 int flags;
170 int fd;
171 long pad;
172 off_t pos;
173 };
174 #endif
175
176 int
sys_mmap(struct thread * td,struct mmap_args * uap)177 sys_mmap(struct thread *td, struct mmap_args *uap)
178 {
179
180 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
181 uap->flags, uap->fd, uap->pos));
182 }
183
184 int
kern_mmap_maxprot(struct proc * p,int prot)185 kern_mmap_maxprot(struct proc *p, int prot)
186 {
187
188 if ((p->p_flag2 & P2_PROTMAX_DISABLE) != 0 ||
189 (p->p_fctl0 & NT_FREEBSD_FCTL_PROTMAX_DISABLE) != 0)
190 return (_PROT_ALL);
191 if (((p->p_flag2 & P2_PROTMAX_ENABLE) != 0 || imply_prot_max) &&
192 prot != PROT_NONE)
193 return (prot);
194 return (_PROT_ALL);
195 }
196
197 int
kern_mmap(struct thread * td,uintptr_t addr0,size_t len,int prot,int flags,int fd,off_t pos)198 kern_mmap(struct thread *td, uintptr_t addr0, size_t len, int prot, int flags,
199 int fd, off_t pos)
200 {
201 struct mmap_req mr = {
202 .mr_hint = addr0,
203 .mr_len = len,
204 .mr_prot = prot,
205 .mr_flags = flags,
206 .mr_fd = fd,
207 .mr_pos = pos
208 };
209
210 return (kern_mmap_req(td, &mr));
211 }
212
213 int
kern_mmap_req(struct thread * td,const struct mmap_req * mrp)214 kern_mmap_req(struct thread *td, const struct mmap_req *mrp)
215 {
216 struct vmspace *vms;
217 struct file *fp;
218 struct proc *p;
219 off_t pos;
220 vm_offset_t addr, orig_addr;
221 vm_size_t len, pageoff, size;
222 vm_prot_t cap_maxprot;
223 int align, error, fd, flags, max_prot, prot;
224 cap_rights_t rights;
225 mmap_check_fp_fn check_fp_fn;
226
227 orig_addr = addr = mrp->mr_hint;
228 len = mrp->mr_len;
229 prot = mrp->mr_prot;
230 flags = mrp->mr_flags;
231 fd = mrp->mr_fd;
232 pos = mrp->mr_pos;
233 check_fp_fn = mrp->mr_check_fp_fn;
234
235 if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
236 return (EINVAL);
237 max_prot = PROT_MAX_EXTRACT(prot);
238 prot = PROT_EXTRACT(prot);
239 if (max_prot != 0 && (max_prot & prot) != prot)
240 return (ENOTSUP);
241
242 p = td->td_proc;
243
244 /*
245 * Always honor PROT_MAX if set. If not, default to all
246 * permissions unless we're implying maximum permissions.
247 */
248 if (max_prot == 0)
249 max_prot = kern_mmap_maxprot(p, prot);
250
251 vms = p->p_vmspace;
252 fp = NULL;
253 AUDIT_ARG_FD(fd);
254
255 /*
256 * Ignore old flags that used to be defined but did not do anything.
257 */
258 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
259
260 /*
261 * Enforce the constraints.
262 * Mapping of length 0 is only allowed for old binaries.
263 * Anonymous mapping shall specify -1 as filedescriptor and
264 * zero position for new code. Be nice to ancient a.out
265 * binaries and correct pos for anonymous mapping, since old
266 * ld.so sometimes issues anonymous map requests with non-zero
267 * pos.
268 */
269 if (!SV_CURPROC_FLAG(SV_AOUT)) {
270 if ((len == 0 && p->p_osrel >= P_OSREL_MAP_ANON) ||
271 ((flags & MAP_ANON) != 0 && (fd != -1 || pos != 0)))
272 return (EINVAL);
273 } else {
274 if ((flags & MAP_ANON) != 0)
275 pos = 0;
276 }
277
278 if (flags & MAP_STACK) {
279 if ((fd != -1) ||
280 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
281 return (EINVAL);
282 flags |= MAP_ANON;
283 pos = 0;
284 }
285 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
286 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
287 MAP_PREFAULT_READ | MAP_GUARD |
288 #ifdef MAP_32BIT
289 MAP_32BIT |
290 #endif
291 MAP_ALIGNMENT_MASK)) != 0)
292 return (EINVAL);
293 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
294 return (EINVAL);
295 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
296 return (EINVAL);
297 if (prot != PROT_NONE &&
298 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
299 return (EINVAL);
300 if ((flags & MAP_GUARD) != 0 && (prot != PROT_NONE || fd != -1 ||
301 pos != 0 || (flags & ~(MAP_FIXED | MAP_GUARD | MAP_EXCL |
302 #ifdef MAP_32BIT
303 MAP_32BIT |
304 #endif
305 MAP_ALIGNMENT_MASK)) != 0))
306 return (EINVAL);
307
308 /*
309 * Align the file position to a page boundary,
310 * and save its page offset component.
311 */
312 pageoff = (pos & PAGE_MASK);
313 pos -= pageoff;
314
315 /* Compute size from len by rounding (on both ends). */
316 size = len + pageoff; /* low end... */
317 size = round_page(size); /* hi end */
318 /* Check for rounding up to zero. */
319 if (len > size)
320 return (ENOMEM);
321
322 /* Ensure alignment is at least a page and fits in a pointer. */
323 align = flags & MAP_ALIGNMENT_MASK;
324 if (align != 0 && align != MAP_ALIGNED_SUPER &&
325 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
326 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
327 return (EINVAL);
328
329 /*
330 * Check for illegal addresses. Watch out for address wrap... Note
331 * that VM_*_ADDRESS are not constants due to casts (argh).
332 */
333 if (flags & MAP_FIXED) {
334 /*
335 * The specified address must have the same remainder
336 * as the file offset taken modulo PAGE_SIZE, so it
337 * should be aligned after adjustment by pageoff.
338 */
339 addr -= pageoff;
340 if (addr & PAGE_MASK)
341 return (EINVAL);
342
343 /* Address range must be all in user VM space. */
344 if (!vm_map_range_valid(&vms->vm_map, addr, addr + size))
345 return (EINVAL);
346 #ifdef MAP_32BIT
347 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
348 return (EINVAL);
349 } else if (flags & MAP_32BIT) {
350 /*
351 * For MAP_32BIT, override the hint if it is too high and
352 * do not bother moving the mapping past the heap (since
353 * the heap is usually above 2GB).
354 */
355 if (addr + size > MAP_32BIT_MAX_ADDR)
356 addr = 0;
357 #endif
358 } else {
359 /*
360 * XXX for non-fixed mappings where no hint is provided or
361 * the hint would fall in the potential heap space,
362 * place it after the end of the largest possible heap.
363 *
364 * There should really be a pmap call to determine a reasonable
365 * location.
366 */
367 if (addr == 0 ||
368 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
369 addr < round_page((vm_offset_t)vms->vm_daddr +
370 lim_max(td, RLIMIT_DATA))))
371 addr = round_page((vm_offset_t)vms->vm_daddr +
372 lim_max(td, RLIMIT_DATA));
373 }
374 if (len == 0) {
375 /*
376 * Return success without mapping anything for old
377 * binaries that request a page-aligned mapping of
378 * length 0. For modern binaries, this function
379 * returns an error earlier.
380 */
381 error = 0;
382 } else if ((flags & MAP_GUARD) != 0) {
383 error = vm_mmap_object(&vms->vm_map, &addr, size, VM_PROT_NONE,
384 VM_PROT_NONE, flags, NULL, pos, FALSE, td);
385 } else if ((flags & MAP_ANON) != 0) {
386 /*
387 * Mapping blank space is trivial.
388 *
389 * This relies on VM_PROT_* matching PROT_*.
390 */
391 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
392 max_prot, flags, NULL, pos, FALSE, td);
393 } else {
394 /*
395 * Mapping file, get fp for validation and don't let the
396 * descriptor disappear on us if we block. Check capability
397 * rights, but also return the maximum rights to be combined
398 * with maxprot later.
399 */
400 cap_rights_init_one(&rights, CAP_MMAP);
401 if (prot & PROT_READ)
402 cap_rights_set_one(&rights, CAP_MMAP_R);
403 if ((flags & MAP_SHARED) != 0) {
404 if (prot & PROT_WRITE)
405 cap_rights_set_one(&rights, CAP_MMAP_W);
406 }
407 if (prot & PROT_EXEC)
408 cap_rights_set_one(&rights, CAP_MMAP_X);
409 error = fget_mmap(td, fd, &rights, &cap_maxprot, &fp);
410 if (error != 0)
411 goto done;
412 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
413 p->p_osrel >= P_OSREL_MAP_FSTRICT) {
414 error = EINVAL;
415 goto done;
416 }
417 if (check_fp_fn != NULL) {
418 error = check_fp_fn(fp, prot, max_prot & cap_maxprot,
419 flags);
420 if (error != 0)
421 goto done;
422 }
423 if (fp->f_ops == &shm_ops && shm_largepage(fp->f_data))
424 addr = orig_addr;
425 /* This relies on VM_PROT_* matching PROT_*. */
426 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
427 max_prot & cap_maxprot, flags, pos, td);
428 }
429
430 if (error == 0)
431 td->td_retval[0] = (register_t) (addr + pageoff);
432 done:
433 if (fp)
434 fdrop(fp, td);
435
436 return (error);
437 }
438
439 #if defined(COMPAT_FREEBSD6)
440 int
freebsd6_mmap(struct thread * td,struct freebsd6_mmap_args * uap)441 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
442 {
443
444 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
445 uap->flags, uap->fd, uap->pos));
446 }
447 #endif
448
449 #ifdef COMPAT_43
450 #ifndef _SYS_SYSPROTO_H_
451 struct ommap_args {
452 caddr_t addr;
453 int len;
454 int prot;
455 int flags;
456 int fd;
457 long pos;
458 };
459 #endif
460 int
ommap(struct thread * td,struct ommap_args * uap)461 ommap(struct thread *td, struct ommap_args *uap)
462 {
463 static const char cvtbsdprot[8] = {
464 0,
465 PROT_EXEC,
466 PROT_WRITE,
467 PROT_EXEC | PROT_WRITE,
468 PROT_READ,
469 PROT_EXEC | PROT_READ,
470 PROT_WRITE | PROT_READ,
471 PROT_EXEC | PROT_WRITE | PROT_READ,
472 };
473 int flags, prot;
474
475 #define OMAP_ANON 0x0002
476 #define OMAP_COPY 0x0020
477 #define OMAP_SHARED 0x0010
478 #define OMAP_FIXED 0x0100
479
480 prot = cvtbsdprot[uap->prot & 0x7];
481 #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__)
482 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
483 prot != 0)
484 prot |= PROT_EXEC;
485 #endif
486 flags = 0;
487 if (uap->flags & OMAP_ANON)
488 flags |= MAP_ANON;
489 if (uap->flags & OMAP_COPY)
490 flags |= MAP_COPY;
491 if (uap->flags & OMAP_SHARED)
492 flags |= MAP_SHARED;
493 else
494 flags |= MAP_PRIVATE;
495 if (uap->flags & OMAP_FIXED)
496 flags |= MAP_FIXED;
497 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot, flags,
498 uap->fd, uap->pos));
499 }
500 #endif /* COMPAT_43 */
501
502 #ifndef _SYS_SYSPROTO_H_
503 struct msync_args {
504 void *addr;
505 size_t len;
506 int flags;
507 };
508 #endif
509 int
sys_msync(struct thread * td,struct msync_args * uap)510 sys_msync(struct thread *td, struct msync_args *uap)
511 {
512
513 return (kern_msync(td, (uintptr_t)uap->addr, uap->len, uap->flags));
514 }
515
516 int
kern_msync(struct thread * td,uintptr_t addr0,size_t size,int flags)517 kern_msync(struct thread *td, uintptr_t addr0, size_t size, int flags)
518 {
519 vm_offset_t addr;
520 vm_size_t pageoff;
521 vm_map_t map;
522 int rv;
523
524 addr = addr0;
525 pageoff = (addr & PAGE_MASK);
526 addr -= pageoff;
527 size += pageoff;
528 size = (vm_size_t) round_page(size);
529 if (addr + size < addr)
530 return (EINVAL);
531
532 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
533 return (EINVAL);
534
535 map = &td->td_proc->p_vmspace->vm_map;
536
537 /*
538 * Clean the pages and interpret the return value.
539 */
540 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
541 (flags & MS_INVALIDATE) != 0);
542 switch (rv) {
543 case KERN_SUCCESS:
544 return (0);
545 case KERN_INVALID_ADDRESS:
546 return (ENOMEM);
547 case KERN_INVALID_ARGUMENT:
548 return (EBUSY);
549 case KERN_FAILURE:
550 return (EIO);
551 default:
552 return (EINVAL);
553 }
554 }
555
556 #ifndef _SYS_SYSPROTO_H_
557 struct munmap_args {
558 void *addr;
559 size_t len;
560 };
561 #endif
562 int
sys_munmap(struct thread * td,struct munmap_args * uap)563 sys_munmap(struct thread *td, struct munmap_args *uap)
564 {
565
566 return (kern_munmap(td, (uintptr_t)uap->addr, uap->len));
567 }
568
569 int
kern_munmap(struct thread * td,uintptr_t addr0,size_t size)570 kern_munmap(struct thread *td, uintptr_t addr0, size_t size)
571 {
572 #ifdef HWPMC_HOOKS
573 struct pmckern_map_out pkm;
574 vm_map_entry_t entry;
575 bool pmc_handled;
576 #endif
577 vm_offset_t addr, end;
578 vm_size_t pageoff;
579 vm_map_t map;
580 int rv;
581
582 if (size == 0)
583 return (EINVAL);
584
585 addr = addr0;
586 pageoff = (addr & PAGE_MASK);
587 addr -= pageoff;
588 size += pageoff;
589 size = (vm_size_t) round_page(size);
590 end = addr + size;
591 map = &td->td_proc->p_vmspace->vm_map;
592 if (!vm_map_range_valid(map, addr, end))
593 return (EINVAL);
594
595 vm_map_lock(map);
596 #ifdef HWPMC_HOOKS
597 pmc_handled = false;
598 if (PMC_HOOK_INSTALLED(PMC_FN_MUNMAP)) {
599 pmc_handled = true;
600 /*
601 * Inform hwpmc if the address range being unmapped contains
602 * an executable region.
603 */
604 pkm.pm_address = (uintptr_t) NULL;
605 if (vm_map_lookup_entry(map, addr, &entry)) {
606 for (; entry->start < end;
607 entry = vm_map_entry_succ(entry)) {
608 if (vm_map_check_protection(map, entry->start,
609 entry->end, VM_PROT_EXECUTE) == TRUE) {
610 pkm.pm_address = (uintptr_t) addr;
611 pkm.pm_size = (size_t) size;
612 break;
613 }
614 }
615 }
616 }
617 #endif
618 rv = vm_map_delete(map, addr, end);
619
620 #ifdef HWPMC_HOOKS
621 if (rv == KERN_SUCCESS && __predict_false(pmc_handled)) {
622 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
623 vm_map_lock_downgrade(map);
624 if (pkm.pm_address != (uintptr_t) NULL)
625 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
626 vm_map_unlock_read(map);
627 } else
628 #endif
629 vm_map_unlock(map);
630
631 return (vm_mmap_to_errno(rv));
632 }
633
634 #ifndef _SYS_SYSPROTO_H_
635 struct mprotect_args {
636 const void *addr;
637 size_t len;
638 int prot;
639 };
640 #endif
641 int
sys_mprotect(struct thread * td,struct mprotect_args * uap)642 sys_mprotect(struct thread *td, struct mprotect_args *uap)
643 {
644
645 return (kern_mprotect(td, (uintptr_t)uap->addr, uap->len,
646 uap->prot, 0));
647 }
648
649 int
kern_mprotect(struct thread * td,uintptr_t addr0,size_t size,int prot,int flags)650 kern_mprotect(struct thread *td, uintptr_t addr0, size_t size, int prot,
651 int flags)
652 {
653 vm_offset_t addr;
654 vm_size_t pageoff;
655 int vm_error, max_prot;
656
657 addr = addr0;
658 if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
659 return (EINVAL);
660 max_prot = PROT_MAX_EXTRACT(prot);
661 prot = PROT_EXTRACT(prot);
662 pageoff = (addr & PAGE_MASK);
663 addr -= pageoff;
664 size += pageoff;
665 size = (vm_size_t) round_page(size);
666 #ifdef COMPAT_FREEBSD32
667 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
668 if (((addr + size) & 0xffffffff) < addr)
669 return (EINVAL);
670 } else
671 #endif
672 if (addr + size < addr)
673 return (EINVAL);
674
675 flags |= VM_MAP_PROTECT_SET_PROT;
676 if (max_prot != 0)
677 flags |= VM_MAP_PROTECT_SET_MAXPROT;
678 vm_error = vm_map_protect(&td->td_proc->p_vmspace->vm_map,
679 addr, addr + size, prot, max_prot, flags);
680
681 switch (vm_error) {
682 case KERN_SUCCESS:
683 return (0);
684 case KERN_PROTECTION_FAILURE:
685 return (EACCES);
686 case KERN_RESOURCE_SHORTAGE:
687 return (ENOMEM);
688 case KERN_OUT_OF_BOUNDS:
689 return (ENOTSUP);
690 }
691 return (EINVAL);
692 }
693
694 #ifndef _SYS_SYSPROTO_H_
695 struct minherit_args {
696 void *addr;
697 size_t len;
698 int inherit;
699 };
700 #endif
701 int
sys_minherit(struct thread * td,struct minherit_args * uap)702 sys_minherit(struct thread *td, struct minherit_args *uap)
703 {
704
705 return (kern_minherit(td, (uintptr_t)uap->addr, uap->len,
706 uap->inherit));
707 }
708
709 int
kern_minherit(struct thread * td,uintptr_t addr0,size_t len,int inherit0)710 kern_minherit(struct thread *td, uintptr_t addr0, size_t len, int inherit0)
711 {
712 vm_offset_t addr;
713 vm_size_t size, pageoff;
714 vm_inherit_t inherit;
715
716 addr = (vm_offset_t)addr0;
717 size = len;
718 inherit = inherit0;
719
720 pageoff = (addr & PAGE_MASK);
721 addr -= pageoff;
722 size += pageoff;
723 size = (vm_size_t) round_page(size);
724 if (addr + size < addr)
725 return (EINVAL);
726
727 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
728 addr + size, inherit)) {
729 case KERN_SUCCESS:
730 return (0);
731 case KERN_PROTECTION_FAILURE:
732 return (EACCES);
733 }
734 return (EINVAL);
735 }
736
737 #ifndef _SYS_SYSPROTO_H_
738 struct madvise_args {
739 void *addr;
740 size_t len;
741 int behav;
742 };
743 #endif
744
745 int
sys_madvise(struct thread * td,struct madvise_args * uap)746 sys_madvise(struct thread *td, struct madvise_args *uap)
747 {
748
749 return (kern_madvise(td, (uintptr_t)uap->addr, uap->len, uap->behav));
750 }
751
752 int
kern_madvise(struct thread * td,uintptr_t addr0,size_t len,int behav)753 kern_madvise(struct thread *td, uintptr_t addr0, size_t len, int behav)
754 {
755 vm_map_t map;
756 vm_offset_t addr, end, start;
757 int flags;
758
759 /*
760 * Check for our special case, advising the swap pager we are
761 * "immortal."
762 */
763 if (behav == MADV_PROTECT) {
764 flags = PPROT_SET;
765 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
766 PROC_SPROTECT, &flags));
767 }
768
769 /*
770 * Check for illegal addresses. Watch out for address wrap... Note
771 * that VM_*_ADDRESS are not constants due to casts (argh).
772 */
773 map = &td->td_proc->p_vmspace->vm_map;
774 addr = addr0;
775 if (!vm_map_range_valid(map, addr, addr + len))
776 return (EINVAL);
777
778 /*
779 * Since this routine is only advisory, we default to conservative
780 * behavior.
781 */
782 start = trunc_page(addr);
783 end = round_page(addr + len);
784
785 /*
786 * vm_map_madvise() checks for illegal values of behav.
787 */
788 return (vm_map_madvise(map, start, end, behav));
789 }
790
791 #ifndef _SYS_SYSPROTO_H_
792 struct mincore_args {
793 const void *addr;
794 size_t len;
795 char *vec;
796 };
797 #endif
798
799 int
sys_mincore(struct thread * td,struct mincore_args * uap)800 sys_mincore(struct thread *td, struct mincore_args *uap)
801 {
802
803 return (kern_mincore(td, (uintptr_t)uap->addr, uap->len, uap->vec));
804 }
805
806 int
kern_mincore(struct thread * td,uintptr_t addr0,size_t len,char * vec)807 kern_mincore(struct thread *td, uintptr_t addr0, size_t len, char *vec)
808 {
809 pmap_t pmap;
810 vm_map_t map;
811 vm_map_entry_t current, entry;
812 vm_object_t object;
813 vm_offset_t addr, cend, end, first_addr;
814 vm_paddr_t pa;
815 vm_page_t m;
816 vm_pindex_t pindex;
817 int error, lastvecindex, mincoreinfo, vecindex;
818 unsigned int timestamp;
819
820 /*
821 * Make sure that the addresses presented are valid for user
822 * mode.
823 */
824 first_addr = addr = trunc_page(addr0);
825 end = round_page(addr0 + len);
826 map = &td->td_proc->p_vmspace->vm_map;
827 if (end > vm_map_max(map) || end < addr)
828 return (ENOMEM);
829
830 pmap = vmspace_pmap(td->td_proc->p_vmspace);
831
832 vm_map_lock_read(map);
833 RestartScan:
834 timestamp = map->timestamp;
835
836 if (!vm_map_lookup_entry(map, addr, &entry)) {
837 vm_map_unlock_read(map);
838 return (ENOMEM);
839 }
840
841 /*
842 * Do this on a map entry basis so that if the pages are not
843 * in the current processes address space, we can easily look
844 * up the pages elsewhere.
845 */
846 lastvecindex = -1;
847 while (entry->start < end) {
848 /*
849 * check for contiguity
850 */
851 current = entry;
852 entry = vm_map_entry_succ(current);
853 if (current->end < end &&
854 entry->start > current->end) {
855 vm_map_unlock_read(map);
856 return (ENOMEM);
857 }
858
859 /*
860 * ignore submaps (for now) or null objects
861 */
862 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
863 current->object.vm_object == NULL)
864 continue;
865
866 /*
867 * limit this scan to the current map entry and the
868 * limits for the mincore call
869 */
870 if (addr < current->start)
871 addr = current->start;
872 cend = current->end;
873 if (cend > end)
874 cend = end;
875
876 for (; addr < cend; addr += PAGE_SIZE) {
877 /*
878 * Check pmap first, it is likely faster, also
879 * it can provide info as to whether we are the
880 * one referencing or modifying the page.
881 */
882 m = NULL;
883 object = NULL;
884 retry:
885 pa = 0;
886 mincoreinfo = pmap_mincore(pmap, addr, &pa);
887 if (mincore_mapped) {
888 /*
889 * We only care about this pmap's
890 * mapping of the page, if any.
891 */
892 ;
893 } else if (pa != 0) {
894 /*
895 * The page is mapped by this process but not
896 * both accessed and modified. It is also
897 * managed. Acquire the object lock so that
898 * other mappings might be examined. The page's
899 * identity may change at any point before its
900 * object lock is acquired, so re-validate if
901 * necessary.
902 */
903 m = PHYS_TO_VM_PAGE(pa);
904 while (object == NULL || m->object != object) {
905 if (object != NULL)
906 VM_OBJECT_WUNLOCK(object);
907 object = atomic_load_ptr(&m->object);
908 if (object == NULL)
909 goto retry;
910 VM_OBJECT_WLOCK(object);
911 }
912 if (pa != pmap_extract(pmap, addr))
913 goto retry;
914 KASSERT(vm_page_all_valid(m),
915 ("mincore: page %p is mapped but invalid",
916 m));
917 } else if (mincoreinfo == 0) {
918 /*
919 * The page is not mapped by this process. If
920 * the object implements managed pages, then
921 * determine if the page is resident so that
922 * the mappings might be examined.
923 */
924 if (current->object.vm_object != object) {
925 if (object != NULL)
926 VM_OBJECT_WUNLOCK(object);
927 object = current->object.vm_object;
928 VM_OBJECT_WLOCK(object);
929 }
930 if (object->type == OBJT_DEFAULT ||
931 (object->flags & OBJ_SWAP) != 0 ||
932 object->type == OBJT_VNODE) {
933 pindex = OFF_TO_IDX(current->offset +
934 (addr - current->start));
935 m = vm_page_lookup(object, pindex);
936 if (m != NULL && vm_page_none_valid(m))
937 m = NULL;
938 if (m != NULL)
939 mincoreinfo = MINCORE_INCORE;
940 }
941 }
942 if (m != NULL) {
943 VM_OBJECT_ASSERT_WLOCKED(m->object);
944
945 /* Examine other mappings of the page. */
946 if (m->dirty == 0 && pmap_is_modified(m))
947 vm_page_dirty(m);
948 if (m->dirty != 0)
949 mincoreinfo |= MINCORE_MODIFIED_OTHER;
950
951 /*
952 * The first test for PGA_REFERENCED is an
953 * optimization. The second test is
954 * required because a concurrent pmap
955 * operation could clear the last reference
956 * and set PGA_REFERENCED before the call to
957 * pmap_is_referenced().
958 */
959 if ((m->a.flags & PGA_REFERENCED) != 0 ||
960 pmap_is_referenced(m) ||
961 (m->a.flags & PGA_REFERENCED) != 0)
962 mincoreinfo |= MINCORE_REFERENCED_OTHER;
963 }
964 if (object != NULL)
965 VM_OBJECT_WUNLOCK(object);
966
967 /*
968 * subyte may page fault. In case it needs to modify
969 * the map, we release the lock.
970 */
971 vm_map_unlock_read(map);
972
973 /*
974 * calculate index into user supplied byte vector
975 */
976 vecindex = atop(addr - first_addr);
977
978 /*
979 * If we have skipped map entries, we need to make sure that
980 * the byte vector is zeroed for those skipped entries.
981 */
982 while ((lastvecindex + 1) < vecindex) {
983 ++lastvecindex;
984 error = subyte(vec + lastvecindex, 0);
985 if (error) {
986 error = EFAULT;
987 goto done2;
988 }
989 }
990
991 /*
992 * Pass the page information to the user
993 */
994 error = subyte(vec + vecindex, mincoreinfo);
995 if (error) {
996 error = EFAULT;
997 goto done2;
998 }
999
1000 /*
1001 * If the map has changed, due to the subyte, the previous
1002 * output may be invalid.
1003 */
1004 vm_map_lock_read(map);
1005 if (timestamp != map->timestamp)
1006 goto RestartScan;
1007
1008 lastvecindex = vecindex;
1009 }
1010 }
1011
1012 /*
1013 * subyte may page fault. In case it needs to modify
1014 * the map, we release the lock.
1015 */
1016 vm_map_unlock_read(map);
1017
1018 /*
1019 * Zero the last entries in the byte vector.
1020 */
1021 vecindex = atop(end - first_addr);
1022 while ((lastvecindex + 1) < vecindex) {
1023 ++lastvecindex;
1024 error = subyte(vec + lastvecindex, 0);
1025 if (error) {
1026 error = EFAULT;
1027 goto done2;
1028 }
1029 }
1030
1031 /*
1032 * If the map has changed, due to the subyte, the previous
1033 * output may be invalid.
1034 */
1035 vm_map_lock_read(map);
1036 if (timestamp != map->timestamp)
1037 goto RestartScan;
1038 vm_map_unlock_read(map);
1039 done2:
1040 return (error);
1041 }
1042
1043 #ifndef _SYS_SYSPROTO_H_
1044 struct mlock_args {
1045 const void *addr;
1046 size_t len;
1047 };
1048 #endif
1049 int
sys_mlock(struct thread * td,struct mlock_args * uap)1050 sys_mlock(struct thread *td, struct mlock_args *uap)
1051 {
1052
1053 return (kern_mlock(td->td_proc, td->td_ucred,
1054 __DECONST(uintptr_t, uap->addr), uap->len));
1055 }
1056
1057 int
kern_mlock(struct proc * proc,struct ucred * cred,uintptr_t addr0,size_t len)1058 kern_mlock(struct proc *proc, struct ucred *cred, uintptr_t addr0, size_t len)
1059 {
1060 vm_offset_t addr, end, last, start;
1061 vm_size_t npages, size;
1062 vm_map_t map;
1063 unsigned long nsize;
1064 int error;
1065
1066 error = priv_check_cred(cred, PRIV_VM_MLOCK);
1067 if (error)
1068 return (error);
1069 addr = addr0;
1070 size = len;
1071 last = addr + size;
1072 start = trunc_page(addr);
1073 end = round_page(last);
1074 if (last < addr || end < addr)
1075 return (EINVAL);
1076 npages = atop(end - start);
1077 if (npages > vm_page_max_user_wired)
1078 return (ENOMEM);
1079 map = &proc->p_vmspace->vm_map;
1080 PROC_LOCK(proc);
1081 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1082 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
1083 PROC_UNLOCK(proc);
1084 return (ENOMEM);
1085 }
1086 PROC_UNLOCK(proc);
1087 #ifdef RACCT
1088 if (racct_enable) {
1089 PROC_LOCK(proc);
1090 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1091 PROC_UNLOCK(proc);
1092 if (error != 0)
1093 return (ENOMEM);
1094 }
1095 #endif
1096 error = vm_map_wire(map, start, end,
1097 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1098 #ifdef RACCT
1099 if (racct_enable && error != KERN_SUCCESS) {
1100 PROC_LOCK(proc);
1101 racct_set(proc, RACCT_MEMLOCK,
1102 ptoa(pmap_wired_count(map->pmap)));
1103 PROC_UNLOCK(proc);
1104 }
1105 #endif
1106 switch (error) {
1107 case KERN_SUCCESS:
1108 return (0);
1109 case KERN_INVALID_ARGUMENT:
1110 return (EINVAL);
1111 default:
1112 return (ENOMEM);
1113 }
1114 }
1115
1116 #ifndef _SYS_SYSPROTO_H_
1117 struct mlockall_args {
1118 int how;
1119 };
1120 #endif
1121
1122 int
sys_mlockall(struct thread * td,struct mlockall_args * uap)1123 sys_mlockall(struct thread *td, struct mlockall_args *uap)
1124 {
1125 vm_map_t map;
1126 int error;
1127
1128 map = &td->td_proc->p_vmspace->vm_map;
1129 error = priv_check(td, PRIV_VM_MLOCK);
1130 if (error)
1131 return (error);
1132
1133 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1134 return (EINVAL);
1135
1136 /*
1137 * If wiring all pages in the process would cause it to exceed
1138 * a hard resource limit, return ENOMEM.
1139 */
1140 if (!old_mlock && uap->how & MCL_CURRENT) {
1141 if (map->size > lim_cur(td, RLIMIT_MEMLOCK))
1142 return (ENOMEM);
1143 }
1144 #ifdef RACCT
1145 if (racct_enable) {
1146 PROC_LOCK(td->td_proc);
1147 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1148 PROC_UNLOCK(td->td_proc);
1149 if (error != 0)
1150 return (ENOMEM);
1151 }
1152 #endif
1153
1154 if (uap->how & MCL_FUTURE) {
1155 vm_map_lock(map);
1156 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1157 vm_map_unlock(map);
1158 error = 0;
1159 }
1160
1161 if (uap->how & MCL_CURRENT) {
1162 /*
1163 * P1003.1-2001 mandates that all currently mapped pages
1164 * will be memory resident and locked (wired) upon return
1165 * from mlockall(). vm_map_wire() will wire pages, by
1166 * calling vm_fault_wire() for each page in the region.
1167 */
1168 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1169 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1170 if (error == KERN_SUCCESS)
1171 error = 0;
1172 else if (error == KERN_RESOURCE_SHORTAGE)
1173 error = ENOMEM;
1174 else
1175 error = EAGAIN;
1176 }
1177 #ifdef RACCT
1178 if (racct_enable && error != KERN_SUCCESS) {
1179 PROC_LOCK(td->td_proc);
1180 racct_set(td->td_proc, RACCT_MEMLOCK,
1181 ptoa(pmap_wired_count(map->pmap)));
1182 PROC_UNLOCK(td->td_proc);
1183 }
1184 #endif
1185
1186 return (error);
1187 }
1188
1189 #ifndef _SYS_SYSPROTO_H_
1190 struct munlockall_args {
1191 register_t dummy;
1192 };
1193 #endif
1194
1195 int
sys_munlockall(struct thread * td,struct munlockall_args * uap)1196 sys_munlockall(struct thread *td, struct munlockall_args *uap)
1197 {
1198 vm_map_t map;
1199 int error;
1200
1201 map = &td->td_proc->p_vmspace->vm_map;
1202 error = priv_check(td, PRIV_VM_MUNLOCK);
1203 if (error)
1204 return (error);
1205
1206 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1207 vm_map_lock(map);
1208 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1209 vm_map_unlock(map);
1210
1211 /* Forcibly unwire all pages. */
1212 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1213 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1214 #ifdef RACCT
1215 if (racct_enable && error == KERN_SUCCESS) {
1216 PROC_LOCK(td->td_proc);
1217 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1218 PROC_UNLOCK(td->td_proc);
1219 }
1220 #endif
1221
1222 return (error);
1223 }
1224
1225 #ifndef _SYS_SYSPROTO_H_
1226 struct munlock_args {
1227 const void *addr;
1228 size_t len;
1229 };
1230 #endif
1231 int
sys_munlock(struct thread * td,struct munlock_args * uap)1232 sys_munlock(struct thread *td, struct munlock_args *uap)
1233 {
1234
1235 return (kern_munlock(td, (uintptr_t)uap->addr, uap->len));
1236 }
1237
1238 int
kern_munlock(struct thread * td,uintptr_t addr0,size_t size)1239 kern_munlock(struct thread *td, uintptr_t addr0, size_t size)
1240 {
1241 vm_offset_t addr, end, last, start;
1242 #ifdef RACCT
1243 vm_map_t map;
1244 #endif
1245 int error;
1246
1247 error = priv_check(td, PRIV_VM_MUNLOCK);
1248 if (error)
1249 return (error);
1250 addr = addr0;
1251 last = addr + size;
1252 start = trunc_page(addr);
1253 end = round_page(last);
1254 if (last < addr || end < addr)
1255 return (EINVAL);
1256 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1257 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1258 #ifdef RACCT
1259 if (racct_enable && error == KERN_SUCCESS) {
1260 PROC_LOCK(td->td_proc);
1261 map = &td->td_proc->p_vmspace->vm_map;
1262 racct_set(td->td_proc, RACCT_MEMLOCK,
1263 ptoa(pmap_wired_count(map->pmap)));
1264 PROC_UNLOCK(td->td_proc);
1265 }
1266 #endif
1267 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1268 }
1269
1270 /*
1271 * vm_mmap_vnode()
1272 *
1273 * Helper function for vm_mmap. Perform sanity check specific for mmap
1274 * operations on vnodes.
1275 */
1276 int
vm_mmap_vnode(struct thread * td,vm_size_t objsize,vm_prot_t prot,vm_prot_t * maxprotp,int * flagsp,struct vnode * vp,vm_ooffset_t * foffp,vm_object_t * objp,boolean_t * writecounted)1277 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1278 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1279 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1280 boolean_t *writecounted)
1281 {
1282 struct vattr va;
1283 vm_object_t obj;
1284 vm_ooffset_t foff;
1285 struct ucred *cred;
1286 int error, flags;
1287 bool writex;
1288
1289 cred = td->td_ucred;
1290 writex = (*maxprotp & VM_PROT_WRITE) != 0 &&
1291 (*flagsp & MAP_SHARED) != 0;
1292 if ((error = vget(vp, LK_SHARED)) != 0)
1293 return (error);
1294 AUDIT_ARG_VNODE1(vp);
1295 foff = *foffp;
1296 flags = *flagsp;
1297 obj = vp->v_object;
1298 if (vp->v_type == VREG) {
1299 /*
1300 * Get the proper underlying object
1301 */
1302 if (obj == NULL) {
1303 error = EINVAL;
1304 goto done;
1305 }
1306 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1307 vput(vp);
1308 vp = (struct vnode *)obj->handle;
1309 /*
1310 * Bypass filesystems obey the mpsafety of the
1311 * underlying fs. Tmpfs never bypasses.
1312 */
1313 error = vget(vp, LK_SHARED);
1314 if (error != 0)
1315 return (error);
1316 }
1317 if (writex) {
1318 *writecounted = TRUE;
1319 vm_pager_update_writecount(obj, 0, objsize);
1320 }
1321 } else {
1322 error = EINVAL;
1323 goto done;
1324 }
1325 if ((error = VOP_GETATTR(vp, &va, cred)))
1326 goto done;
1327 #ifdef MAC
1328 /* This relies on VM_PROT_* matching PROT_*. */
1329 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1330 if (error != 0)
1331 goto done;
1332 #endif
1333 if ((flags & MAP_SHARED) != 0) {
1334 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1335 if (prot & VM_PROT_WRITE) {
1336 error = EPERM;
1337 goto done;
1338 }
1339 *maxprotp &= ~VM_PROT_WRITE;
1340 }
1341 }
1342 /*
1343 * If it is a regular file without any references
1344 * we do not need to sync it.
1345 * Adjust object size to be the size of actual file.
1346 */
1347 objsize = round_page(va.va_size);
1348 if (va.va_nlink == 0)
1349 flags |= MAP_NOSYNC;
1350 if (obj->type == OBJT_VNODE) {
1351 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1352 cred);
1353 if (obj == NULL) {
1354 error = ENOMEM;
1355 goto done;
1356 }
1357 } else {
1358 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP ||
1359 (obj->flags & OBJ_SWAP) != 0, ("wrong object type"));
1360 vm_object_reference(obj);
1361 #if VM_NRESERVLEVEL > 0
1362 if ((obj->flags & OBJ_COLORED) == 0) {
1363 VM_OBJECT_WLOCK(obj);
1364 vm_object_color(obj, 0);
1365 VM_OBJECT_WUNLOCK(obj);
1366 }
1367 #endif
1368 }
1369 *objp = obj;
1370 *flagsp = flags;
1371
1372 VOP_MMAPPED(vp);
1373
1374 done:
1375 if (error != 0 && *writecounted) {
1376 *writecounted = FALSE;
1377 vm_pager_update_writecount(obj, objsize, 0);
1378 }
1379 vput(vp);
1380 return (error);
1381 }
1382
1383 /*
1384 * vm_mmap_cdev()
1385 *
1386 * Helper function for vm_mmap. Perform sanity check specific for mmap
1387 * operations on cdevs.
1388 */
1389 int
vm_mmap_cdev(struct thread * td,vm_size_t objsize,vm_prot_t prot,vm_prot_t * maxprotp,int * flagsp,struct cdev * cdev,struct cdevsw * dsw,vm_ooffset_t * foff,vm_object_t * objp)1390 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1391 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1392 vm_ooffset_t *foff, vm_object_t *objp)
1393 {
1394 vm_object_t obj;
1395 int error, flags;
1396
1397 flags = *flagsp;
1398
1399 if (dsw->d_flags & D_MMAP_ANON) {
1400 *objp = NULL;
1401 *foff = 0;
1402 *maxprotp = VM_PROT_ALL;
1403 *flagsp |= MAP_ANON;
1404 return (0);
1405 }
1406 /*
1407 * cdevs do not provide private mappings of any kind.
1408 */
1409 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1410 (prot & VM_PROT_WRITE) != 0)
1411 return (EACCES);
1412 if (flags & (MAP_PRIVATE|MAP_COPY))
1413 return (EINVAL);
1414 /*
1415 * Force device mappings to be shared.
1416 */
1417 flags |= MAP_SHARED;
1418 #ifdef MAC_XXX
1419 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1420 if (error != 0)
1421 return (error);
1422 #endif
1423 /*
1424 * First, try d_mmap_single(). If that is not implemented
1425 * (returns ENODEV), fall back to using the device pager.
1426 * Note that d_mmap_single() must return a reference to the
1427 * object (it needs to bump the reference count of the object
1428 * it returns somehow).
1429 *
1430 * XXX assumes VM_PROT_* == PROT_*
1431 */
1432 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1433 if (error != ENODEV)
1434 return (error);
1435 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1436 td->td_ucred);
1437 if (obj == NULL)
1438 return (EINVAL);
1439 *objp = obj;
1440 *flagsp = flags;
1441 return (0);
1442 }
1443
1444 int
vm_mmap(vm_map_t map,vm_offset_t * addr,vm_size_t size,vm_prot_t prot,vm_prot_t maxprot,int flags,objtype_t handle_type,void * handle,vm_ooffset_t foff)1445 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1446 vm_prot_t maxprot, int flags,
1447 objtype_t handle_type, void *handle,
1448 vm_ooffset_t foff)
1449 {
1450 vm_object_t object;
1451 struct thread *td = curthread;
1452 int error;
1453 boolean_t writecounted;
1454
1455 if (size == 0)
1456 return (EINVAL);
1457
1458 size = round_page(size);
1459 object = NULL;
1460 writecounted = FALSE;
1461
1462 switch (handle_type) {
1463 case OBJT_DEVICE: {
1464 struct cdevsw *dsw;
1465 struct cdev *cdev;
1466 int ref;
1467
1468 cdev = handle;
1469 dsw = dev_refthread(cdev, &ref);
1470 if (dsw == NULL)
1471 return (ENXIO);
1472 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1473 dsw, &foff, &object);
1474 dev_relthread(cdev, ref);
1475 break;
1476 }
1477 case OBJT_VNODE:
1478 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1479 handle, &foff, &object, &writecounted);
1480 break;
1481 default:
1482 error = EINVAL;
1483 break;
1484 }
1485 if (error)
1486 return (error);
1487
1488 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1489 foff, writecounted, td);
1490 if (error != 0 && object != NULL) {
1491 /*
1492 * If this mapping was accounted for in the vnode's
1493 * writecount, then undo that now.
1494 */
1495 if (writecounted)
1496 vm_pager_release_writecount(object, 0, size);
1497 vm_object_deallocate(object);
1498 }
1499 return (error);
1500 }
1501
1502 int
kern_mmap_racct_check(struct thread * td,vm_map_t map,vm_size_t size)1503 kern_mmap_racct_check(struct thread *td, vm_map_t map, vm_size_t size)
1504 {
1505 int error;
1506
1507 RACCT_PROC_LOCK(td->td_proc);
1508 if (map->size + size > lim_cur(td, RLIMIT_VMEM)) {
1509 RACCT_PROC_UNLOCK(td->td_proc);
1510 return (ENOMEM);
1511 }
1512 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1513 RACCT_PROC_UNLOCK(td->td_proc);
1514 return (ENOMEM);
1515 }
1516 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1517 if (ptoa(pmap_wired_count(map->pmap)) + size >
1518 lim_cur(td, RLIMIT_MEMLOCK)) {
1519 racct_set_force(td->td_proc, RACCT_VMEM, map->size);
1520 RACCT_PROC_UNLOCK(td->td_proc);
1521 return (ENOMEM);
1522 }
1523 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1524 ptoa(pmap_wired_count(map->pmap)) + size);
1525 if (error != 0) {
1526 racct_set_force(td->td_proc, RACCT_VMEM, map->size);
1527 RACCT_PROC_UNLOCK(td->td_proc);
1528 return (error);
1529 }
1530 }
1531 RACCT_PROC_UNLOCK(td->td_proc);
1532 return (0);
1533 }
1534
1535 /*
1536 * Internal version of mmap that maps a specific VM object into an
1537 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1538 */
1539 int
vm_mmap_object(vm_map_t map,vm_offset_t * addr,vm_size_t size,vm_prot_t prot,vm_prot_t maxprot,int flags,vm_object_t object,vm_ooffset_t foff,boolean_t writecounted,struct thread * td)1540 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1541 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1542 boolean_t writecounted, struct thread *td)
1543 {
1544 vm_offset_t max_addr;
1545 int docow, error, findspace, rv;
1546 bool curmap, fitit;
1547
1548 curmap = map == &td->td_proc->p_vmspace->vm_map;
1549 if (curmap) {
1550 error = kern_mmap_racct_check(td, map, size);
1551 if (error != 0)
1552 return (error);
1553 }
1554
1555 /*
1556 * We currently can only deal with page aligned file offsets.
1557 * The mmap() system call already enforces this by subtracting
1558 * the page offset from the file offset, but checking here
1559 * catches errors in device drivers (e.g. d_single_mmap()
1560 * callbacks) and other internal mapping requests (such as in
1561 * exec).
1562 */
1563 if (foff & PAGE_MASK)
1564 return (EINVAL);
1565
1566 if ((flags & MAP_FIXED) == 0) {
1567 fitit = TRUE;
1568 *addr = round_page(*addr);
1569 } else {
1570 if (*addr != trunc_page(*addr))
1571 return (EINVAL);
1572 fitit = FALSE;
1573 }
1574
1575 if (flags & MAP_ANON) {
1576 if (object != NULL || foff != 0)
1577 return (EINVAL);
1578 docow = 0;
1579 } else if (flags & MAP_PREFAULT_READ)
1580 docow = MAP_PREFAULT;
1581 else
1582 docow = MAP_PREFAULT_PARTIAL;
1583
1584 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1585 docow |= MAP_COPY_ON_WRITE;
1586 if (flags & MAP_NOSYNC)
1587 docow |= MAP_DISABLE_SYNCER;
1588 if (flags & MAP_NOCORE)
1589 docow |= MAP_DISABLE_COREDUMP;
1590 /* Shared memory is also shared with children. */
1591 if (flags & MAP_SHARED)
1592 docow |= MAP_INHERIT_SHARE;
1593 if (writecounted)
1594 docow |= MAP_WRITECOUNT;
1595 if (flags & MAP_STACK) {
1596 if (object != NULL)
1597 return (EINVAL);
1598 docow |= MAP_STACK_GROWS_DOWN;
1599 }
1600 if ((flags & MAP_EXCL) != 0)
1601 docow |= MAP_CHECK_EXCL;
1602 if ((flags & MAP_GUARD) != 0)
1603 docow |= MAP_CREATE_GUARD;
1604
1605 if (fitit) {
1606 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1607 findspace = VMFS_SUPER_SPACE;
1608 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1609 findspace = VMFS_ALIGNED_SPACE(flags >>
1610 MAP_ALIGNMENT_SHIFT);
1611 else
1612 findspace = VMFS_OPTIMAL_SPACE;
1613 max_addr = 0;
1614 #ifdef MAP_32BIT
1615 if ((flags & MAP_32BIT) != 0)
1616 max_addr = MAP_32BIT_MAX_ADDR;
1617 #endif
1618 if (curmap) {
1619 rv = vm_map_find_min(map, object, foff, addr, size,
1620 round_page((vm_offset_t)td->td_proc->p_vmspace->
1621 vm_daddr + lim_max(td, RLIMIT_DATA)), max_addr,
1622 findspace, prot, maxprot, docow);
1623 } else {
1624 rv = vm_map_find(map, object, foff, addr, size,
1625 max_addr, findspace, prot, maxprot, docow);
1626 }
1627 } else {
1628 rv = vm_map_fixed(map, object, foff, *addr, size,
1629 prot, maxprot, docow);
1630 }
1631
1632 if (rv == KERN_SUCCESS) {
1633 /*
1634 * If the process has requested that all future mappings
1635 * be wired, then heed this.
1636 */
1637 if ((map->flags & MAP_WIREFUTURE) != 0) {
1638 vm_map_lock(map);
1639 if ((map->flags & MAP_WIREFUTURE) != 0)
1640 (void)vm_map_wire_locked(map, *addr,
1641 *addr + size, VM_MAP_WIRE_USER |
1642 ((flags & MAP_STACK) ? VM_MAP_WIRE_HOLESOK :
1643 VM_MAP_WIRE_NOHOLES));
1644 vm_map_unlock(map);
1645 }
1646 }
1647 return (vm_mmap_to_errno(rv));
1648 }
1649
1650 /*
1651 * Translate a Mach VM return code to zero on success or the appropriate errno
1652 * on failure.
1653 */
1654 int
vm_mmap_to_errno(int rv)1655 vm_mmap_to_errno(int rv)
1656 {
1657
1658 switch (rv) {
1659 case KERN_SUCCESS:
1660 return (0);
1661 case KERN_INVALID_ADDRESS:
1662 case KERN_NO_SPACE:
1663 return (ENOMEM);
1664 case KERN_PROTECTION_FAILURE:
1665 return (EACCES);
1666 default:
1667 return (EINVAL);
1668 }
1669 }
1670