xref: /freebsd/sys/vm/vm_mmap.c (revision 580340dbdaaf372867e9ed3dd257430982753e5e)
160727d8bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1988 University of Utah.
5df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
6df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
9df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
10df8bae1dSRodney W. Grimes  * Science Department.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39df8bae1dSRodney W. Grimes /*
40df8bae1dSRodney W. Grimes  * Mapped file (mmap) interface to VM
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
43874651b1SDavid E. O'Brien #include <sys/cdefs.h>
4449874f6eSJoseph Koshy #include "opt_hwpmc_hooks.h"
453d653db0SAlan Cox #include "opt_vm.h"
46e9822d92SJoerg Wunsch 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
48df8bae1dSRodney W. Grimes #include <sys/systm.h>
494a144410SRobert Watson #include <sys/capsicum.h>
50a9d2f8d8SRobert Watson #include <sys/kernel.h>
51fb919e4dSMark Murray #include <sys/lock.h>
5223955314SAlfred Perlstein #include <sys/mutex.h>
53d2d3e875SBruce Evans #include <sys/sysproto.h>
545dc7e31aSKonstantin Belousov #include <sys/elf.h>
55df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
56acd3428bSRobert Watson #include <sys/priv.h>
57df8bae1dSRodney W. Grimes #include <sys/proc.h>
5855648840SJohn Baldwin #include <sys/procctl.h>
591ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
60070f64feSMatthew Dillon #include <sys/resource.h>
61070f64feSMatthew Dillon #include <sys/resourcevar.h>
6289f6b863SAttilio Rao #include <sys/rwlock.h>
637e19eda4SAndrey Zonov #include <sys/sysctl.h>
64df8bae1dSRodney W. Grimes #include <sys/vnode.h>
653ac4d1efSBruce Evans #include <sys/fcntl.h>
66df8bae1dSRodney W. Grimes #include <sys/file.h>
67df8bae1dSRodney W. Grimes #include <sys/mman.h>
68b483c7f6SGuido van Rooij #include <sys/mount.h>
69df8bae1dSRodney W. Grimes #include <sys/conf.h>
704183b6b6SPeter Wemm #include <sys/stat.h>
7155648840SJohn Baldwin #include <sys/syscallsubr.h>
72497a8238SKonstantin Belousov #include <sys/sysent.h>
73efeaf95aSDavid Greenman #include <sys/vmmeter.h>
74a7f67facSKonstantin Belousov #if defined(__amd64__) || defined(__i386__) /* for i386_read_exec */
75a7f67facSKonstantin Belousov #include <machine/md_var.h>
76a7f67facSKonstantin Belousov #endif
77df8bae1dSRodney W. Grimes 
7851d1f690SRobert Watson #include <security/audit/audit.h>
79aed55708SRobert Watson #include <security/mac/mac_framework.h>
80aed55708SRobert Watson 
81df8bae1dSRodney W. Grimes #include <vm/vm.h>
82efeaf95aSDavid Greenman #include <vm/vm_param.h>
83efeaf95aSDavid Greenman #include <vm/pmap.h>
84efeaf95aSDavid Greenman #include <vm/vm_map.h>
85efeaf95aSDavid Greenman #include <vm/vm_object.h>
861c7c3c6aSMatthew Dillon #include <vm/vm_page.h>
87df8bae1dSRodney W. Grimes #include <vm/vm_pager.h>
88b5e8ce9fSBruce Evans #include <vm/vm_pageout.h>
89efeaf95aSDavid Greenman #include <vm/vm_extern.h>
90867a482dSJohn Dyson #include <vm/vm_page.h>
9184110e7eSKonstantin Belousov #include <vm/vnode_pager.h>
92df8bae1dSRodney W. Grimes 
9349874f6eSJoseph Koshy #ifdef HWPMC_HOOKS
9449874f6eSJoseph Koshy #include <sys/pmckern.h>
9549874f6eSJoseph Koshy #endif
9649874f6eSJoseph Koshy 
977e19eda4SAndrey Zonov int old_mlock = 0;
98af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RWTUN, &old_mlock, 0,
997e19eda4SAndrey Zonov     "Do not apply RLIMIT_MEMLOCK on mlockall");
1003fbc2e00SKonstantin Belousov static int mincore_mapped = 1;
1013fbc2e00SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, mincore_mapped, CTLFLAG_RWTUN, &mincore_mapped, 0,
1023fbc2e00SKonstantin Belousov     "mincore reports mappings, not residency");
10374a1b66cSBrooks Davis static int imply_prot_max = 0;
10474a1b66cSBrooks Davis SYSCTL_INT(_vm, OID_AUTO, imply_prot_max, CTLFLAG_RWTUN, &imply_prot_max, 0,
1054d13f784SEd Maste     "Imply maximum page protections in mmap() when none are specified");
1067e19eda4SAndrey Zonov 
107847ab36bSMark Johnston _Static_assert(MAXPAGESIZES <= 4, "MINCORE_SUPER too narrow");
108847ab36bSMark Johnston 
1091930e303SPoul-Henning Kamp #if defined(COMPAT_43)
110df8bae1dSRodney W. Grimes int
ogetpagesize(struct thread * td,struct ogetpagesize_args * uap)111d48719bdSBrooks Davis ogetpagesize(struct thread *td, struct ogetpagesize_args *uap)
112df8bae1dSRodney W. Grimes {
11304e89ffbSKonstantin Belousov 
114b40ce416SJulian Elischer 	td->td_retval[0] = PAGE_SIZE;
115df8bae1dSRodney W. Grimes 	return (0);
116df8bae1dSRodney W. Grimes }
1171930e303SPoul-Henning Kamp #endif				/* COMPAT_43 */
118df8bae1dSRodney W. Grimes 
11954f42e4bSPeter Wemm /*
12054f42e4bSPeter Wemm  * Memory Map (mmap) system call.  Note that the file offset
12154f42e4bSPeter Wemm  * and address are allowed to be NOT page aligned, though if
12254f42e4bSPeter Wemm  * the MAP_FIXED flag it set, both must have the same remainder
12354f42e4bSPeter Wemm  * modulo the PAGE_SIZE (POSIX 1003.1b).  If the address is not
12454f42e4bSPeter Wemm  * page-aligned, the actual mapping starts at trunc_page(addr)
12554f42e4bSPeter Wemm  * and the return value is adjusted up by the page offset.
126b4309055SMatthew Dillon  *
127b4309055SMatthew Dillon  * Generally speaking, only character devices which are themselves
128b4309055SMatthew Dillon  * memory-based, such as a video framebuffer, can be mmap'd.  Otherwise
129b4309055SMatthew Dillon  * there would be no cache coherency between a descriptor and a VM mapping
130b4309055SMatthew Dillon  * both to the same character device.
13154f42e4bSPeter Wemm  */
132d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
133df8bae1dSRodney W. Grimes struct mmap_args {
134651bb817SAlexander Langer 	void *addr;
135df8bae1dSRodney W. Grimes 	size_t len;
136df8bae1dSRodney W. Grimes 	int prot;
137df8bae1dSRodney W. Grimes 	int flags;
138df8bae1dSRodney W. Grimes 	int fd;
139df8bae1dSRodney W. Grimes 	long pad;
140df8bae1dSRodney W. Grimes 	off_t pos;
141df8bae1dSRodney W. Grimes };
142d2d3e875SBruce Evans #endif
143df8bae1dSRodney W. Grimes 
144df8bae1dSRodney W. Grimes int
sys_mmap(struct thread * td,struct mmap_args * uap)14569cdfcefSEdward Tomasz Napierala sys_mmap(struct thread *td, struct mmap_args *uap)
14669cdfcefSEdward Tomasz Napierala {
14769cdfcefSEdward Tomasz Napierala 
1487a1591c1SBrooks Davis 	return (kern_mmap(td, &(struct mmap_req){
1497a1591c1SBrooks Davis 		.mr_hint = (uintptr_t)uap->addr,
1507a1591c1SBrooks Davis 		.mr_len = uap->len,
1517a1591c1SBrooks Davis 		.mr_prot = uap->prot,
1527a1591c1SBrooks Davis 		.mr_flags = uap->flags,
1537a1591c1SBrooks Davis 		.mr_fd = uap->fd,
1547a1591c1SBrooks Davis 		.mr_pos = uap->pos,
1557a1591c1SBrooks Davis 	    }));
15669cdfcefSEdward Tomasz Napierala }
15769cdfcefSEdward Tomasz Napierala 
15869cdfcefSEdward Tomasz Napierala int
kern_mmap_maxprot(struct proc * p,int prot)1595dc7e31aSKonstantin Belousov kern_mmap_maxprot(struct proc *p, int prot)
1605dc7e31aSKonstantin Belousov {
1615dc7e31aSKonstantin Belousov 
1625dc7e31aSKonstantin Belousov 	if ((p->p_flag2 & P2_PROTMAX_DISABLE) != 0 ||
1635dc7e31aSKonstantin Belousov 	    (p->p_fctl0 & NT_FREEBSD_FCTL_PROTMAX_DISABLE) != 0)
1645dc7e31aSKonstantin Belousov 		return (_PROT_ALL);
1655dc7e31aSKonstantin Belousov 	if (((p->p_flag2 & P2_PROTMAX_ENABLE) != 0 || imply_prot_max) &&
1665dc7e31aSKonstantin Belousov 	    prot != PROT_NONE)
1675dc7e31aSKonstantin Belousov 		 return (prot);
1685dc7e31aSKonstantin Belousov 	return (_PROT_ALL);
1695dc7e31aSKonstantin Belousov }
1705dc7e31aSKonstantin Belousov 
1715dc7e31aSKonstantin Belousov int
kern_mmap(struct thread * td,const struct mmap_req * mrp)1727a1591c1SBrooks Davis kern_mmap(struct thread *td, const struct mmap_req *mrp)
17318348a23SKyle Evans {
174496ab053SKonstantin Belousov 	struct vmspace *vms;
175c8daea13SAlexander Kabaev 	struct file *fp;
17637306951SKonstantin Belousov 	struct proc *p;
177d718de81SBrooks Davis 	off_t pos;
178d301b358SKonstantin Belousov 	vm_offset_t addr, orig_addr;
179d718de81SBrooks Davis 	vm_size_t len, pageoff, size;
1807077c426SJohn Baldwin 	vm_prot_t cap_maxprot;
181d718de81SBrooks Davis 	int align, error, fd, flags, max_prot, prot;
182a9d2f8d8SRobert Watson 	cap_rights_t rights;
183d718de81SBrooks Davis 	mmap_check_fp_fn check_fp_fn;
184d718de81SBrooks Davis 
185d301b358SKonstantin Belousov 	orig_addr = addr = mrp->mr_hint;
186d718de81SBrooks Davis 	len = mrp->mr_len;
187d718de81SBrooks Davis 	prot = mrp->mr_prot;
188d718de81SBrooks Davis 	flags = mrp->mr_flags;
189d718de81SBrooks Davis 	fd = mrp->mr_fd;
190d718de81SBrooks Davis 	pos = mrp->mr_pos;
191d718de81SBrooks Davis 	check_fp_fn = mrp->mr_check_fp_fn;
192df8bae1dSRodney W. Grimes 
19374a1b66cSBrooks Davis 	if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
19474a1b66cSBrooks Davis 		return (EINVAL);
19574a1b66cSBrooks Davis 	max_prot = PROT_MAX_EXTRACT(prot);
19674a1b66cSBrooks Davis 	prot = PROT_EXTRACT(prot);
19774a1b66cSBrooks Davis 	if (max_prot != 0 && (max_prot & prot) != prot)
198acb8858fSEd Maste 		return (ENOTSUP);
19937306951SKonstantin Belousov 
20037306951SKonstantin Belousov 	p = td->td_proc;
20137306951SKonstantin Belousov 
20274a1b66cSBrooks Davis 	/*
20374a1b66cSBrooks Davis 	 * Always honor PROT_MAX if set.  If not, default to all
20474a1b66cSBrooks Davis 	 * permissions unless we're implying maximum permissions.
20574a1b66cSBrooks Davis 	 */
20674a1b66cSBrooks Davis 	if (max_prot == 0)
2075dc7e31aSKonstantin Belousov 		max_prot = kern_mmap_maxprot(p, prot);
20874a1b66cSBrooks Davis 
20937306951SKonstantin Belousov 	vms = p->p_vmspace;
210426da3bcSAlfred Perlstein 	fp = NULL;
21169cdfcefSEdward Tomasz Napierala 	AUDIT_ARG_FD(fd);
21227bfa958SSimon L. B. Nielsen 
2137707ccabSKonstantin Belousov 	/*
2145817298fSJohn Baldwin 	 * Ignore old flags that used to be defined but did not do anything.
2155817298fSJohn Baldwin 	 */
2165817298fSJohn Baldwin 	flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
2175817298fSJohn Baldwin 
2185817298fSJohn Baldwin 	/*
2197707ccabSKonstantin Belousov 	 * Enforce the constraints.
2207707ccabSKonstantin Belousov 	 * Mapping of length 0 is only allowed for old binaries.
2217707ccabSKonstantin Belousov 	 * Anonymous mapping shall specify -1 as filedescriptor and
2227707ccabSKonstantin Belousov 	 * zero position for new code. Be nice to ancient a.out
2237707ccabSKonstantin Belousov 	 * binaries and correct pos for anonymous mapping, since old
2247707ccabSKonstantin Belousov 	 * ld.so sometimes issues anonymous map requests with non-zero
2257707ccabSKonstantin Belousov 	 * pos.
2267707ccabSKonstantin Belousov 	 */
2277707ccabSKonstantin Belousov 	if (!SV_CURPROC_FLAG(SV_AOUT)) {
22837306951SKonstantin Belousov 		if ((len == 0 && p->p_osrel >= P_OSREL_MAP_ANON) ||
22969cdfcefSEdward Tomasz Napierala 		    ((flags & MAP_ANON) != 0 && (fd != -1 || pos != 0)))
230df8bae1dSRodney W. Grimes 			return (EINVAL);
2317707ccabSKonstantin Belousov 	} else {
2327707ccabSKonstantin Belousov 		if ((flags & MAP_ANON) != 0)
2337707ccabSKonstantin Belousov 			pos = 0;
2347707ccabSKonstantin Belousov 	}
2359154ee6aSPeter Wemm 
2362267af78SJulian Elischer 	if (flags & MAP_STACK) {
23769cdfcefSEdward Tomasz Napierala 		if ((fd != -1) ||
2382267af78SJulian Elischer 		    ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
2392267af78SJulian Elischer 			return (EINVAL);
2402267af78SJulian Elischer 		flags |= MAP_ANON;
2412267af78SJulian Elischer 		pos = 0;
2422907af2aSJulian Elischer 	}
2435817298fSJohn Baldwin 	if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
2445817298fSJohn Baldwin 	    MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
245f3e11927SDmitry Chagin 	    MAP_PREFAULT_READ | MAP_GUARD | MAP_32BIT | MAP_ALIGNMENT_MASK)) != 0)
2465fd3f8b3SJohn Baldwin 		return (EINVAL);
24711c42bccSKonstantin Belousov 	if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
24811c42bccSKonstantin Belousov 		return (EINVAL);
24910204535SKonstantin Belousov 	if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
2505fd3f8b3SJohn Baldwin 		return (EINVAL);
2515fd3f8b3SJohn Baldwin 	if (prot != PROT_NONE &&
2525fd3f8b3SJohn Baldwin 	    (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
2535fd3f8b3SJohn Baldwin 		return (EINVAL);
25419bd0d9cSKonstantin Belousov 	if ((flags & MAP_GUARD) != 0 && (prot != PROT_NONE || fd != -1 ||
25560221a57SAlan Cox 	    pos != 0 || (flags & ~(MAP_FIXED | MAP_GUARD | MAP_EXCL |
256f3e11927SDmitry Chagin 	    MAP_32BIT | MAP_ALIGNMENT_MASK)) != 0))
25719bd0d9cSKonstantin Belousov 		return (EINVAL);
2582907af2aSJulian Elischer 
2599154ee6aSPeter Wemm 	/*
26054f42e4bSPeter Wemm 	 * Align the file position to a page boundary,
26154f42e4bSPeter Wemm 	 * and save its page offset component.
2629154ee6aSPeter Wemm 	 */
26354f42e4bSPeter Wemm 	pageoff = (pos & PAGE_MASK);
26454f42e4bSPeter Wemm 	pos -= pageoff;
26554f42e4bSPeter Wemm 
26677555b84SDoug Moore 	/* Compute size from len by rounding (on both ends). */
26777555b84SDoug Moore 	size = len + pageoff;			/* low end... */
26897220a27SDoug Moore 	size = round_page(size);		/* hi end */
26977555b84SDoug Moore 	/* Check for rounding up to zero. */
270f8c8b2e8SDoug Moore 	if (len > size)
27177555b84SDoug Moore 		return (ENOMEM);
2729154ee6aSPeter Wemm 
2735aa60b6fSJohn Baldwin 	/* Ensure alignment is at least a page and fits in a pointer. */
2745aa60b6fSJohn Baldwin 	align = flags & MAP_ALIGNMENT_MASK;
2755aa60b6fSJohn Baldwin 	if (align != 0 && align != MAP_ALIGNED_SUPER &&
2765aa60b6fSJohn Baldwin 	    (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
2775aa60b6fSJohn Baldwin 	    align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
2785aa60b6fSJohn Baldwin 		return (EINVAL);
2795aa60b6fSJohn Baldwin 
280df8bae1dSRodney W. Grimes 	/*
2810d94caffSDavid Greenman 	 * Check for illegal addresses.  Watch out for address wrap... Note
2820d94caffSDavid Greenman 	 * that VM_*_ADDRESS are not constants due to casts (argh).
283df8bae1dSRodney W. Grimes 	 */
284df8bae1dSRodney W. Grimes 	if (flags & MAP_FIXED) {
28554f42e4bSPeter Wemm 		/*
28654f42e4bSPeter Wemm 		 * The specified address must have the same remainder
28754f42e4bSPeter Wemm 		 * as the file offset taken modulo PAGE_SIZE, so it
28854f42e4bSPeter Wemm 		 * should be aligned after adjustment by pageoff.
28954f42e4bSPeter Wemm 		 */
29054f42e4bSPeter Wemm 		addr -= pageoff;
29154f42e4bSPeter Wemm 		if (addr & PAGE_MASK)
29254f42e4bSPeter Wemm 			return (EINVAL);
29327bfa958SSimon L. B. Nielsen 
29454f42e4bSPeter Wemm 		/* Address range must be all in user VM space. */
2950f1e6ec5SMark Johnston 		if (!vm_map_range_valid(&vms->vm_map, addr, addr + size))
296df8bae1dSRodney W. Grimes 			return (EINVAL);
297edb572a3SJohn Baldwin 		if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
298edb572a3SJohn Baldwin 			return (EINVAL);
299edb572a3SJohn Baldwin 	} else if (flags & MAP_32BIT) {
300edb572a3SJohn Baldwin 		/*
301edb572a3SJohn Baldwin 		 * For MAP_32BIT, override the hint if it is too high and
302edb572a3SJohn Baldwin 		 * do not bother moving the mapping past the heap (since
303edb572a3SJohn Baldwin 		 * the heap is usually above 2GB).
304edb572a3SJohn Baldwin 		 */
305edb572a3SJohn Baldwin 		if (addr + size > MAP_32BIT_MAX_ADDR)
306edb572a3SJohn Baldwin 			addr = 0;
30791d5354aSJohn Baldwin 	} else {
308df8bae1dSRodney W. Grimes 		/*
30954f42e4bSPeter Wemm 		 * XXX for non-fixed mappings where no hint is provided or
31054f42e4bSPeter Wemm 		 * the hint would fall in the potential heap space,
31154f42e4bSPeter Wemm 		 * place it after the end of the largest possible heap.
312df8bae1dSRodney W. Grimes 		 *
313d8e6f494SAlan Cox 		 * For anonymous mappings within the address space of the
314d8e6f494SAlan Cox 		 * calling process, the absence of a hint is handled at a
315d8e6f494SAlan Cox 		 * lower level in order to implement different clustering
316d8e6f494SAlan Cox 		 * strategies for ASLR.
317df8bae1dSRodney W. Grimes 		 */
318d8e6f494SAlan Cox 		if (((flags & MAP_ANON) == 0 && addr == 0) ||
3191f6889a1SMatthew Dillon 		    (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
320c460ac3aSPeter Wemm 		    addr < round_page((vm_offset_t)vms->vm_daddr +
321cd336badSMateusz Guzik 		    lim_max(td, RLIMIT_DATA))))
322c460ac3aSPeter Wemm 			addr = round_page((vm_offset_t)vms->vm_daddr +
323cd336badSMateusz Guzik 			    lim_max(td, RLIMIT_DATA));
32491d5354aSJohn Baldwin 	}
32577555b84SDoug Moore 	if (len == 0) {
3267077c426SJohn Baldwin 		/*
3277077c426SJohn Baldwin 		 * Return success without mapping anything for old
3287077c426SJohn Baldwin 		 * binaries that request a page-aligned mapping of
3297077c426SJohn Baldwin 		 * length 0.  For modern binaries, this function
3307077c426SJohn Baldwin 		 * returns an error earlier.
3317077c426SJohn Baldwin 		 */
3327077c426SJohn Baldwin 		error = 0;
33319bd0d9cSKonstantin Belousov 	} else if ((flags & MAP_GUARD) != 0) {
33419bd0d9cSKonstantin Belousov 		error = vm_mmap_object(&vms->vm_map, &addr, size, VM_PROT_NONE,
33519bd0d9cSKonstantin Belousov 		    VM_PROT_NONE, flags, NULL, pos, FALSE, td);
33619bd0d9cSKonstantin Belousov 	} else if ((flags & MAP_ANON) != 0) {
337df8bae1dSRodney W. Grimes 		/*
338df8bae1dSRodney W. Grimes 		 * Mapping blank space is trivial.
3397077c426SJohn Baldwin 		 *
3407077c426SJohn Baldwin 		 * This relies on VM_PROT_* matching PROT_*.
341df8bae1dSRodney W. Grimes 		 */
3427077c426SJohn Baldwin 		error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
34374a1b66cSBrooks Davis 		    max_prot, flags, NULL, pos, FALSE, td);
34430d4dd7eSAlexander Kabaev 	} else {
345df8bae1dSRodney W. Grimes 		/*
346a9d2f8d8SRobert Watson 		 * Mapping file, get fp for validation and don't let the
347a9d2f8d8SRobert Watson 		 * descriptor disappear on us if we block. Check capability
348a9d2f8d8SRobert Watson 		 * rights, but also return the maximum rights to be combined
349a9d2f8d8SRobert Watson 		 * with maxprot later.
350df8bae1dSRodney W. Grimes 		 */
3513379d2f9SMateusz Guzik 		cap_rights_init_one(&rights, CAP_MMAP);
352a9d2f8d8SRobert Watson 		if (prot & PROT_READ)
3533379d2f9SMateusz Guzik 			cap_rights_set_one(&rights, CAP_MMAP_R);
354a9d2f8d8SRobert Watson 		if ((flags & MAP_SHARED) != 0) {
355a9d2f8d8SRobert Watson 			if (prot & PROT_WRITE)
3563379d2f9SMateusz Guzik 				cap_rights_set_one(&rights, CAP_MMAP_W);
357a9d2f8d8SRobert Watson 		}
358a9d2f8d8SRobert Watson 		if (prot & PROT_EXEC)
3593379d2f9SMateusz Guzik 			cap_rights_set_one(&rights, CAP_MMAP_X);
36069cdfcefSEdward Tomasz Napierala 		error = fget_mmap(td, fd, &rights, &cap_maxprot, &fp);
3617008be5bSPawel Jakub Dawidek 		if (error != 0)
362426da3bcSAlfred Perlstein 			goto done;
36310204535SKonstantin Belousov 		if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
36437306951SKonstantin Belousov 		    p->p_osrel >= P_OSREL_MAP_FSTRICT) {
36510204535SKonstantin Belousov 			error = EINVAL;
36610204535SKonstantin Belousov 			goto done;
36710204535SKonstantin Belousov 		}
36818348a23SKyle Evans 		if (check_fp_fn != NULL) {
36918348a23SKyle Evans 			error = check_fp_fn(fp, prot, max_prot & cap_maxprot,
37018348a23SKyle Evans 			    flags);
37118348a23SKyle Evans 			if (error != 0)
37218348a23SKyle Evans 				goto done;
37318348a23SKyle Evans 		}
374d301b358SKonstantin Belousov 		if (fp->f_ops == &shm_ops && shm_largepage(fp->f_data))
375d301b358SKonstantin Belousov 			addr = orig_addr;
3765fd3f8b3SJohn Baldwin 		/* This relies on VM_PROT_* matching PROT_*. */
3777077c426SJohn Baldwin 		error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
37874a1b66cSBrooks Davis 		    max_prot & cap_maxprot, flags, pos, td);
37949874f6eSJoseph Koshy 	}
3807077c426SJohn Baldwin 
381df8bae1dSRodney W. Grimes 	if (error == 0)
382b1ad6a90SBrooks Davis 		td->td_retval[0] = addr + pageoff;
383279d7226SMatthew Dillon done:
384279d7226SMatthew Dillon 	if (fp)
385b40ce416SJulian Elischer 		fdrop(fp, td);
386f6b5b182SJeff Roberson 
387df8bae1dSRodney W. Grimes 	return (error);
388df8bae1dSRodney W. Grimes }
389df8bae1dSRodney W. Grimes 
3900538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6)
391c2815ad5SPeter Wemm int
freebsd6_mmap(struct thread * td,struct freebsd6_mmap_args * uap)392c2815ad5SPeter Wemm freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
393c2815ad5SPeter Wemm {
3947a1591c1SBrooks Davis 	return (kern_mmap(td, &(struct mmap_req){
3957a1591c1SBrooks Davis 		.mr_hint = (uintptr_t)uap->addr,
3967a1591c1SBrooks Davis 		.mr_len = uap->len,
3977a1591c1SBrooks Davis 		.mr_prot = uap->prot,
3987a1591c1SBrooks Davis 		.mr_flags = uap->flags,
3997a1591c1SBrooks Davis 		.mr_fd = uap->fd,
4007a1591c1SBrooks Davis 		.mr_pos = uap->pos,
4017a1591c1SBrooks Davis 	    }));
402c2815ad5SPeter Wemm }
4030538aafcSKonstantin Belousov #endif
404c2815ad5SPeter Wemm 
40505f0fdd2SPoul-Henning Kamp #ifdef COMPAT_43
406d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
40705f0fdd2SPoul-Henning Kamp struct ommap_args {
40805f0fdd2SPoul-Henning Kamp 	caddr_t addr;
40905f0fdd2SPoul-Henning Kamp 	int len;
41005f0fdd2SPoul-Henning Kamp 	int prot;
41105f0fdd2SPoul-Henning Kamp 	int flags;
41205f0fdd2SPoul-Henning Kamp 	int fd;
41305f0fdd2SPoul-Henning Kamp 	long pos;
41405f0fdd2SPoul-Henning Kamp };
415d2d3e875SBruce Evans #endif
41605f0fdd2SPoul-Henning Kamp int
ommap(struct thread * td,struct ommap_args * uap)41769cdfcefSEdward Tomasz Napierala ommap(struct thread *td, struct ommap_args *uap)
41805f0fdd2SPoul-Henning Kamp {
41901ce7fcaSBrooks Davis 	return (kern_ommap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
42001ce7fcaSBrooks Davis 	    uap->flags, uap->fd, uap->pos));
42101ce7fcaSBrooks Davis }
42201ce7fcaSBrooks Davis 
42301ce7fcaSBrooks Davis int
kern_ommap(struct thread * td,uintptr_t hint,int len,int oprot,int oflags,int fd,long pos)42401ce7fcaSBrooks Davis kern_ommap(struct thread *td, uintptr_t hint, int len, int oprot,
42501ce7fcaSBrooks Davis     int oflags, int fd, long pos)
42601ce7fcaSBrooks Davis {
42705f0fdd2SPoul-Henning Kamp 	static const char cvtbsdprot[8] = {
42805f0fdd2SPoul-Henning Kamp 		0,
42905f0fdd2SPoul-Henning Kamp 		PROT_EXEC,
43005f0fdd2SPoul-Henning Kamp 		PROT_WRITE,
43105f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE,
43205f0fdd2SPoul-Henning Kamp 		PROT_READ,
43305f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_READ,
43405f0fdd2SPoul-Henning Kamp 		PROT_WRITE | PROT_READ,
43505f0fdd2SPoul-Henning Kamp 		PROT_EXEC | PROT_WRITE | PROT_READ,
43605f0fdd2SPoul-Henning Kamp 	};
43769cdfcefSEdward Tomasz Napierala 	int flags, prot;
4380d94caffSDavid Greenman 
43901ce7fcaSBrooks Davis 	if (len < 0)
44001ce7fcaSBrooks Davis 		return (EINVAL);
44101ce7fcaSBrooks Davis 
44205f0fdd2SPoul-Henning Kamp #define	OMAP_ANON	0x0002
44305f0fdd2SPoul-Henning Kamp #define	OMAP_COPY	0x0020
44405f0fdd2SPoul-Henning Kamp #define	OMAP_SHARED	0x0010
44505f0fdd2SPoul-Henning Kamp #define	OMAP_FIXED	0x0100
44605f0fdd2SPoul-Henning Kamp 
44701ce7fcaSBrooks Davis 	prot = cvtbsdprot[oprot & 0x7];
4485dddee2dSKonstantin Belousov #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__)
449ee4116b8SKonstantin Belousov 	if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
45069cdfcefSEdward Tomasz Napierala 	    prot != 0)
45169cdfcefSEdward Tomasz Napierala 		prot |= PROT_EXEC;
452ee4116b8SKonstantin Belousov #endif
45369cdfcefSEdward Tomasz Napierala 	flags = 0;
45401ce7fcaSBrooks Davis 	if (oflags & OMAP_ANON)
45569cdfcefSEdward Tomasz Napierala 		flags |= MAP_ANON;
45601ce7fcaSBrooks Davis 	if (oflags & OMAP_COPY)
45769cdfcefSEdward Tomasz Napierala 		flags |= MAP_COPY;
45801ce7fcaSBrooks Davis 	if (oflags & OMAP_SHARED)
45969cdfcefSEdward Tomasz Napierala 		flags |= MAP_SHARED;
46005f0fdd2SPoul-Henning Kamp 	else
46169cdfcefSEdward Tomasz Napierala 		flags |= MAP_PRIVATE;
46201ce7fcaSBrooks Davis 	if (oflags & OMAP_FIXED)
46369cdfcefSEdward Tomasz Napierala 		flags |= MAP_FIXED;
4647a1591c1SBrooks Davis 	return (kern_mmap(td, &(struct mmap_req){
46501ce7fcaSBrooks Davis 		.mr_hint = hint,
46601ce7fcaSBrooks Davis 		.mr_len = len,
4677a1591c1SBrooks Davis 		.mr_prot = prot,
4687a1591c1SBrooks Davis 		.mr_flags = flags,
46901ce7fcaSBrooks Davis 		.mr_fd = fd,
47001ce7fcaSBrooks Davis 		.mr_pos = pos,
4717a1591c1SBrooks Davis 	    }));
47205f0fdd2SPoul-Henning Kamp }
47305f0fdd2SPoul-Henning Kamp #endif				/* COMPAT_43 */
47405f0fdd2SPoul-Henning Kamp 
475d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
476df8bae1dSRodney W. Grimes struct msync_args {
477651bb817SAlexander Langer 	void *addr;
478c899450bSPeter Wemm 	size_t len;
479e6c6af11SDavid Greenman 	int flags;
480df8bae1dSRodney W. Grimes };
481d2d3e875SBruce Evans #endif
482df8bae1dSRodney W. Grimes int
sys_msync(struct thread * td,struct msync_args * uap)48369cdfcefSEdward Tomasz Napierala sys_msync(struct thread *td, struct msync_args *uap)
484df8bae1dSRodney W. Grimes {
48569cdfcefSEdward Tomasz Napierala 
486496ab053SKonstantin Belousov 	return (kern_msync(td, (uintptr_t)uap->addr, uap->len, uap->flags));
48769cdfcefSEdward Tomasz Napierala }
48869cdfcefSEdward Tomasz Napierala 
48969cdfcefSEdward Tomasz Napierala int
kern_msync(struct thread * td,uintptr_t addr0,size_t size,int flags)490496ab053SKonstantin Belousov kern_msync(struct thread *td, uintptr_t addr0, size_t size, int flags)
49169cdfcefSEdward Tomasz Napierala {
492496ab053SKonstantin Belousov 	vm_offset_t addr;
49369cdfcefSEdward Tomasz Napierala 	vm_size_t pageoff;
494df8bae1dSRodney W. Grimes 	vm_map_t map;
495df8bae1dSRodney W. Grimes 	int rv;
496df8bae1dSRodney W. Grimes 
497496ab053SKonstantin Belousov 	addr = addr0;
498dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
499dabee6feSPeter Wemm 	addr -= pageoff;
500dabee6feSPeter Wemm 	size += pageoff;
501dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
5029154ee6aSPeter Wemm 	if (addr + size < addr)
503dabee6feSPeter Wemm 		return (EINVAL);
504dabee6feSPeter Wemm 
505dabee6feSPeter Wemm 	if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
5061e62bc63SDavid Greenman 		return (EINVAL);
5071e62bc63SDavid Greenman 
508b40ce416SJulian Elischer 	map = &td->td_proc->p_vmspace->vm_map;
5099154ee6aSPeter Wemm 
510df8bae1dSRodney W. Grimes 	/*
511df8bae1dSRodney W. Grimes 	 * Clean the pages and interpret the return value.
512df8bae1dSRodney W. Grimes 	 */
513950f8459SAlan Cox 	rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
514e6c6af11SDavid Greenman 	    (flags & MS_INVALIDATE) != 0);
515df8bae1dSRodney W. Grimes 	switch (rv) {
516df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
517d2c60af8SMatthew Dillon 		return (0);
518df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
519e103f5b1SPeter Holm 		return (ENOMEM);
520b7b7cd44SAlan Cox 	case KERN_INVALID_ARGUMENT:
521b7b7cd44SAlan Cox 		return (EBUSY);
522126d6082SKonstantin Belousov 	case KERN_FAILURE:
523126d6082SKonstantin Belousov 		return (EIO);
524df8bae1dSRodney W. Grimes 	default:
525df8bae1dSRodney W. Grimes 		return (EINVAL);
526df8bae1dSRodney W. Grimes 	}
527df8bae1dSRodney W. Grimes }
528df8bae1dSRodney W. Grimes 
529d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
530df8bae1dSRodney W. Grimes struct munmap_args {
531651bb817SAlexander Langer 	void *addr;
5329154ee6aSPeter Wemm 	size_t len;
533df8bae1dSRodney W. Grimes };
534d2d3e875SBruce Evans #endif
535df8bae1dSRodney W. Grimes int
sys_munmap(struct thread * td,struct munmap_args * uap)53669cdfcefSEdward Tomasz Napierala sys_munmap(struct thread *td, struct munmap_args *uap)
53769cdfcefSEdward Tomasz Napierala {
53869cdfcefSEdward Tomasz Napierala 
539496ab053SKonstantin Belousov 	return (kern_munmap(td, (uintptr_t)uap->addr, uap->len));
54069cdfcefSEdward Tomasz Napierala }
54169cdfcefSEdward Tomasz Napierala 
54269cdfcefSEdward Tomasz Napierala int
kern_munmap(struct thread * td,uintptr_t addr0,size_t size)543496ab053SKonstantin Belousov kern_munmap(struct thread *td, uintptr_t addr0, size_t size)
544df8bae1dSRodney W. Grimes {
54549874f6eSJoseph Koshy #ifdef HWPMC_HOOKS
54649874f6eSJoseph Koshy 	struct pmckern_map_out pkm;
54749874f6eSJoseph Koshy 	vm_map_entry_t entry;
548736ff8c3SMateusz Guzik 	bool pmc_handled;
54949874f6eSJoseph Koshy #endif
5500f1e6ec5SMark Johnston 	vm_offset_t addr, end;
55169cdfcefSEdward Tomasz Napierala 	vm_size_t pageoff;
552df8bae1dSRodney W. Grimes 	vm_map_t map;
553e8f77c20SKonstantin Belousov 	int rv;
554df8bae1dSRodney W. Grimes 
555d8834602SAlan Cox 	if (size == 0)
556d8834602SAlan Cox 		return (EINVAL);
557dabee6feSPeter Wemm 
558496ab053SKonstantin Belousov 	addr = addr0;
559dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
560dabee6feSPeter Wemm 	addr -= pageoff;
561dabee6feSPeter Wemm 	size += pageoff;
562dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
5630f1e6ec5SMark Johnston 	end = addr + size;
5640f1e6ec5SMark Johnston 	map = &td->td_proc->p_vmspace->vm_map;
5650f1e6ec5SMark Johnston 	if (!vm_map_range_valid(map, addr, end))
566df8bae1dSRodney W. Grimes 		return (EINVAL);
5679154ee6aSPeter Wemm 
568d8834602SAlan Cox 	vm_map_lock(map);
56949874f6eSJoseph Koshy #ifdef HWPMC_HOOKS
570736ff8c3SMateusz Guzik 	pmc_handled = false;
571736ff8c3SMateusz Guzik 	if (PMC_HOOK_INSTALLED(PMC_FN_MUNMAP)) {
572736ff8c3SMateusz Guzik 		pmc_handled = true;
57349874f6eSJoseph Koshy 		/*
57449874f6eSJoseph Koshy 		 * Inform hwpmc if the address range being unmapped contains
57549874f6eSJoseph Koshy 		 * an executable region.
57649874f6eSJoseph Koshy 		 */
5770d419640SRyan Stone 		pkm.pm_address = (uintptr_t) NULL;
57849874f6eSJoseph Koshy 		if (vm_map_lookup_entry(map, addr, &entry)) {
5790f1e6ec5SMark Johnston 			for (; entry->start < end;
5807cdcf863SDoug Moore 			    entry = vm_map_entry_succ(entry)) {
58149874f6eSJoseph Koshy 				if (vm_map_check_protection(map, entry->start,
58249874f6eSJoseph Koshy 					entry->end, VM_PROT_EXECUTE) == TRUE) {
58349874f6eSJoseph Koshy 					pkm.pm_address = (uintptr_t) addr;
58449874f6eSJoseph Koshy 					pkm.pm_size = (size_t) size;
58549874f6eSJoseph Koshy 					break;
58649874f6eSJoseph Koshy 				}
58749874f6eSJoseph Koshy 			}
58849874f6eSJoseph Koshy 		}
589736ff8c3SMateusz Guzik 	}
59049874f6eSJoseph Koshy #endif
591e8f77c20SKonstantin Belousov 	rv = vm_map_delete(map, addr, end);
5920d419640SRyan Stone 
5930d419640SRyan Stone #ifdef HWPMC_HOOKS
594e8f77c20SKonstantin Belousov 	if (rv == KERN_SUCCESS && __predict_false(pmc_handled)) {
5950d419640SRyan Stone 		/* downgrade the lock to prevent a LOR with the pmc-sx lock */
5960d419640SRyan Stone 		vm_map_lock_downgrade(map);
597d473d3a1SRyan Stone 		if (pkm.pm_address != (uintptr_t) NULL)
5980d419640SRyan Stone 			PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
5990d419640SRyan Stone 		vm_map_unlock_read(map);
600736ff8c3SMateusz Guzik 	} else
6010d419640SRyan Stone #endif
602736ff8c3SMateusz Guzik 		vm_map_unlock(map);
603736ff8c3SMateusz Guzik 
604e8f77c20SKonstantin Belousov 	return (vm_mmap_to_errno(rv));
605df8bae1dSRodney W. Grimes }
606df8bae1dSRodney W. Grimes 
607d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
608df8bae1dSRodney W. Grimes struct mprotect_args {
609651bb817SAlexander Langer 	const void *addr;
6109154ee6aSPeter Wemm 	size_t len;
611df8bae1dSRodney W. Grimes 	int prot;
612df8bae1dSRodney W. Grimes };
613d2d3e875SBruce Evans #endif
614df8bae1dSRodney W. Grimes int
sys_mprotect(struct thread * td,struct mprotect_args * uap)61569cdfcefSEdward Tomasz Napierala sys_mprotect(struct thread *td, struct mprotect_args *uap)
616df8bae1dSRodney W. Grimes {
617df8bae1dSRodney W. Grimes 
6189b65fa69SKonstantin Belousov 	return (kern_mprotect(td, (uintptr_t)uap->addr, uap->len,
6199b65fa69SKonstantin Belousov 	    uap->prot, 0));
62069cdfcefSEdward Tomasz Napierala }
621df8bae1dSRodney W. Grimes 
62269cdfcefSEdward Tomasz Napierala int
kern_mprotect(struct thread * td,uintptr_t addr0,size_t size,int prot,int flags)6239b65fa69SKonstantin Belousov kern_mprotect(struct thread *td, uintptr_t addr0, size_t size, int prot,
6249b65fa69SKonstantin Belousov     int flags)
62569cdfcefSEdward Tomasz Napierala {
626496ab053SKonstantin Belousov 	vm_offset_t addr;
62769cdfcefSEdward Tomasz Napierala 	vm_size_t pageoff;
62874a1b66cSBrooks Davis 	int vm_error, max_prot;
62969cdfcefSEdward Tomasz Napierala 
630496ab053SKonstantin Belousov 	addr = addr0;
63174a1b66cSBrooks Davis 	if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
63274a1b66cSBrooks Davis 		return (EINVAL);
63374a1b66cSBrooks Davis 	max_prot = PROT_MAX_EXTRACT(prot);
63474a1b66cSBrooks Davis 	prot = PROT_EXTRACT(prot);
635dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
636dabee6feSPeter Wemm 	addr -= pageoff;
637dabee6feSPeter Wemm 	size += pageoff;
638dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
6396e1d2cf6SKonstantin Belousov #ifdef COMPAT_FREEBSD32
6406e1d2cf6SKonstantin Belousov 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
6416e1d2cf6SKonstantin Belousov 		if (((addr + size) & 0xffffffff) < addr)
6426e1d2cf6SKonstantin Belousov 			return (EINVAL);
6436e1d2cf6SKonstantin Belousov 	} else
6446e1d2cf6SKonstantin Belousov #endif
6459154ee6aSPeter Wemm 	if (addr + size < addr)
646dabee6feSPeter Wemm 		return (EINVAL);
647dabee6feSPeter Wemm 
6489b65fa69SKonstantin Belousov 	flags |= VM_MAP_PROTECT_SET_PROT;
6490659df6fSKonstantin Belousov 	if (max_prot != 0)
6500659df6fSKonstantin Belousov 		flags |= VM_MAP_PROTECT_SET_MAXPROT;
65174a1b66cSBrooks Davis 	vm_error = vm_map_protect(&td->td_proc->p_vmspace->vm_map,
6520659df6fSKonstantin Belousov 	    addr, addr + size, prot, max_prot, flags);
65374a1b66cSBrooks Davis 
65474a1b66cSBrooks Davis 	switch (vm_error) {
655df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
656df8bae1dSRodney W. Grimes 		return (0);
657df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
658df8bae1dSRodney W. Grimes 		return (EACCES);
6593364c323SKonstantin Belousov 	case KERN_RESOURCE_SHORTAGE:
6603364c323SKonstantin Belousov 		return (ENOMEM);
6610659df6fSKonstantin Belousov 	case KERN_OUT_OF_BOUNDS:
6620659df6fSKonstantin Belousov 		return (ENOTSUP);
663df8bae1dSRodney W. Grimes 	}
664df8bae1dSRodney W. Grimes 	return (EINVAL);
665df8bae1dSRodney W. Grimes }
666df8bae1dSRodney W. Grimes 
667d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
668dabee6feSPeter Wemm struct minherit_args {
669651bb817SAlexander Langer 	void *addr;
6709154ee6aSPeter Wemm 	size_t len;
671dabee6feSPeter Wemm 	int inherit;
672dabee6feSPeter Wemm };
673dabee6feSPeter Wemm #endif
674dabee6feSPeter Wemm int
sys_minherit(struct thread * td,struct minherit_args * uap)67504e89ffbSKonstantin Belousov sys_minherit(struct thread *td, struct minherit_args *uap)
676dabee6feSPeter Wemm {
67752c81be1SEdward Tomasz Napierala 
67852c81be1SEdward Tomasz Napierala 	return (kern_minherit(td, (uintptr_t)uap->addr, uap->len,
67952c81be1SEdward Tomasz Napierala 	    uap->inherit));
68052c81be1SEdward Tomasz Napierala }
68152c81be1SEdward Tomasz Napierala 
68252c81be1SEdward Tomasz Napierala int
kern_minherit(struct thread * td,uintptr_t addr0,size_t len,int inherit0)68352c81be1SEdward Tomasz Napierala kern_minherit(struct thread *td, uintptr_t addr0, size_t len, int inherit0)
68452c81be1SEdward Tomasz Napierala {
685dabee6feSPeter Wemm 	vm_offset_t addr;
686dabee6feSPeter Wemm 	vm_size_t size, pageoff;
68754d92145SMatthew Dillon 	vm_inherit_t inherit;
688dabee6feSPeter Wemm 
68952c81be1SEdward Tomasz Napierala 	addr = (vm_offset_t)addr0;
69052c81be1SEdward Tomasz Napierala 	size = len;
69152c81be1SEdward Tomasz Napierala 	inherit = inherit0;
692dabee6feSPeter Wemm 
693dabee6feSPeter Wemm 	pageoff = (addr & PAGE_MASK);
694dabee6feSPeter Wemm 	addr -= pageoff;
695dabee6feSPeter Wemm 	size += pageoff;
696dabee6feSPeter Wemm 	size = (vm_size_t) round_page(size);
6979154ee6aSPeter Wemm 	if (addr + size < addr)
698dabee6feSPeter Wemm 		return (EINVAL);
699dabee6feSPeter Wemm 
700e0be79afSAlan Cox 	switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
701e0be79afSAlan Cox 	    addr + size, inherit)) {
702dabee6feSPeter Wemm 	case KERN_SUCCESS:
703dabee6feSPeter Wemm 		return (0);
704dabee6feSPeter Wemm 	case KERN_PROTECTION_FAILURE:
705dabee6feSPeter Wemm 		return (EACCES);
706dabee6feSPeter Wemm 	}
707dabee6feSPeter Wemm 	return (EINVAL);
708dabee6feSPeter Wemm }
709dabee6feSPeter Wemm 
710dabee6feSPeter Wemm #ifndef _SYS_SYSPROTO_H_
711df8bae1dSRodney W. Grimes struct madvise_args {
712651bb817SAlexander Langer 	void *addr;
7139154ee6aSPeter Wemm 	size_t len;
714df8bae1dSRodney W. Grimes 	int behav;
715df8bae1dSRodney W. Grimes };
716d2d3e875SBruce Evans #endif
7170d94caffSDavid Greenman 
718df8bae1dSRodney W. Grimes int
sys_madvise(struct thread * td,struct madvise_args * uap)71904e89ffbSKonstantin Belousov sys_madvise(struct thread *td, struct madvise_args *uap)
720df8bae1dSRodney W. Grimes {
72169cdfcefSEdward Tomasz Napierala 
722496ab053SKonstantin Belousov 	return (kern_madvise(td, (uintptr_t)uap->addr, uap->len, uap->behav));
72369cdfcefSEdward Tomasz Napierala }
72469cdfcefSEdward Tomasz Napierala 
72569cdfcefSEdward Tomasz Napierala int
kern_madvise(struct thread * td,uintptr_t addr0,size_t len,int behav)726496ab053SKonstantin Belousov kern_madvise(struct thread *td, uintptr_t addr0, size_t len, int behav)
72769cdfcefSEdward Tomasz Napierala {
72805ba50f5SJake Burkholder 	vm_map_t map;
729496ab053SKonstantin Belousov 	vm_offset_t addr, end, start;
73055648840SJohn Baldwin 	int flags;
731b4309055SMatthew Dillon 
732b4309055SMatthew Dillon 	/*
733f4cf2141SWes Peters 	 * Check for our special case, advising the swap pager we are
734f4cf2141SWes Peters 	 * "immortal."
735f4cf2141SWes Peters 	 */
73669cdfcefSEdward Tomasz Napierala 	if (behav == MADV_PROTECT) {
73755648840SJohn Baldwin 		flags = PPROT_SET;
73855648840SJohn Baldwin 		return (kern_procctl(td, P_PID, td->td_proc->p_pid,
73955648840SJohn Baldwin 		    PROC_SPROTECT, &flags));
74069297bf8SJohn Baldwin 	}
74155648840SJohn Baldwin 
742f4cf2141SWes Peters 	/*
743867a482dSJohn Dyson 	 * Check for illegal addresses.  Watch out for address wrap... Note
744867a482dSJohn Dyson 	 * that VM_*_ADDRESS are not constants due to casts (argh).
745867a482dSJohn Dyson 	 */
74605ba50f5SJake Burkholder 	map = &td->td_proc->p_vmspace->vm_map;
747496ab053SKonstantin Belousov 	addr = addr0;
7480f1e6ec5SMark Johnston 	if (!vm_map_range_valid(map, addr, addr + len))
749867a482dSJohn Dyson 		return (EINVAL);
750867a482dSJohn Dyson 
751867a482dSJohn Dyson 	/*
752867a482dSJohn Dyson 	 * Since this routine is only advisory, we default to conservative
753867a482dSJohn Dyson 	 * behavior.
754867a482dSJohn Dyson 	 */
75569cdfcefSEdward Tomasz Napierala 	start = trunc_page(addr);
75669cdfcefSEdward Tomasz Napierala 	end = round_page(addr + len);
757867a482dSJohn Dyson 
7583e7cb27cSAlan Cox 	/*
7593e7cb27cSAlan Cox 	 * vm_map_madvise() checks for illegal values of behav.
7603e7cb27cSAlan Cox 	 */
7613e7cb27cSAlan Cox 	return (vm_map_madvise(map, start, end, behav));
762df8bae1dSRodney W. Grimes }
763df8bae1dSRodney W. Grimes 
764d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
765df8bae1dSRodney W. Grimes struct mincore_args {
766651bb817SAlexander Langer 	const void *addr;
7679154ee6aSPeter Wemm 	size_t len;
768df8bae1dSRodney W. Grimes 	char *vec;
769df8bae1dSRodney W. Grimes };
770d2d3e875SBruce Evans #endif
7710d94caffSDavid Greenman 
772df8bae1dSRodney W. Grimes int
sys_mincore(struct thread * td,struct mincore_args * uap)77304e89ffbSKonstantin Belousov sys_mincore(struct thread *td, struct mincore_args *uap)
774df8bae1dSRodney W. Grimes {
77546dc8e9dSDmitry Chagin 
77646dc8e9dSDmitry Chagin 	return (kern_mincore(td, (uintptr_t)uap->addr, uap->len, uap->vec));
77746dc8e9dSDmitry Chagin }
77846dc8e9dSDmitry Chagin 
77946dc8e9dSDmitry Chagin int
kern_mincore(struct thread * td,uintptr_t addr0,size_t len,char * vec)78046dc8e9dSDmitry Chagin kern_mincore(struct thread *td, uintptr_t addr0, size_t len, char *vec)
78146dc8e9dSDmitry Chagin {
782867a482dSJohn Dyson 	pmap_t pmap;
783867a482dSJohn Dyson 	vm_map_t map;
78401cef4caSMark Johnston 	vm_map_entry_t current, entry;
785567e51e1SAlan Cox 	vm_object_t object;
78601cef4caSMark Johnston 	vm_offset_t addr, cend, end, first_addr;
78701cef4caSMark Johnston 	vm_paddr_t pa;
788567e51e1SAlan Cox 	vm_page_t m;
789567e51e1SAlan Cox 	vm_pindex_t pindex;
79001cef4caSMark Johnston 	int error, lastvecindex, mincoreinfo, vecindex;
791dd2622a8SAlan Cox 	unsigned int timestamp;
792df8bae1dSRodney W. Grimes 
793867a482dSJohn Dyson 	/*
794867a482dSJohn Dyson 	 * Make sure that the addresses presented are valid for user
795867a482dSJohn Dyson 	 * mode.
796867a482dSJohn Dyson 	 */
79746dc8e9dSDmitry Chagin 	first_addr = addr = trunc_page(addr0);
798d0c9294bSMark Johnston 	end = round_page(addr0 + len);
79905ba50f5SJake Burkholder 	map = &td->td_proc->p_vmspace->vm_map;
80005ba50f5SJake Burkholder 	if (end > vm_map_max(map) || end < addr)
801455dd7d4SKonstantin Belousov 		return (ENOMEM);
80202c04a2fSJohn Dyson 
803b40ce416SJulian Elischer 	pmap = vmspace_pmap(td->td_proc->p_vmspace);
804867a482dSJohn Dyson 
805eff50fcdSAlan Cox 	vm_map_lock_read(map);
806dd2622a8SAlan Cox RestartScan:
807dd2622a8SAlan Cox 	timestamp = map->timestamp;
808867a482dSJohn Dyson 
809455dd7d4SKonstantin Belousov 	if (!vm_map_lookup_entry(map, addr, &entry)) {
810455dd7d4SKonstantin Belousov 		vm_map_unlock_read(map);
811455dd7d4SKonstantin Belousov 		return (ENOMEM);
812455dd7d4SKonstantin Belousov 	}
813867a482dSJohn Dyson 
814867a482dSJohn Dyson 	/*
815867a482dSJohn Dyson 	 * Do this on a map entry basis so that if the pages are not
816867a482dSJohn Dyson 	 * in the current processes address space, we can easily look
817867a482dSJohn Dyson 	 * up the pages elsewhere.
818867a482dSJohn Dyson 	 */
819867a482dSJohn Dyson 	lastvecindex = -1;
8207cdcf863SDoug Moore 	while (entry->start < end) {
821867a482dSJohn Dyson 		/*
822455dd7d4SKonstantin Belousov 		 * check for contiguity
823455dd7d4SKonstantin Belousov 		 */
8247cdcf863SDoug Moore 		current = entry;
8257cdcf863SDoug Moore 		entry = vm_map_entry_succ(current);
8267cdcf863SDoug Moore 		if (current->end < end &&
8277cdcf863SDoug Moore 		    entry->start > current->end) {
828455dd7d4SKonstantin Belousov 			vm_map_unlock_read(map);
829455dd7d4SKonstantin Belousov 			return (ENOMEM);
830455dd7d4SKonstantin Belousov 		}
831455dd7d4SKonstantin Belousov 
832455dd7d4SKonstantin Belousov 		/*
833867a482dSJohn Dyson 		 * ignore submaps (for now) or null objects
834867a482dSJohn Dyson 		 */
8359fdfe602SMatthew Dillon 		if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
836867a482dSJohn Dyson 		    current->object.vm_object == NULL)
837867a482dSJohn Dyson 			continue;
838867a482dSJohn Dyson 
839867a482dSJohn Dyson 		/*
840867a482dSJohn Dyson 		 * limit this scan to the current map entry and the
841867a482dSJohn Dyson 		 * limits for the mincore call
842867a482dSJohn Dyson 		 */
843867a482dSJohn Dyson 		if (addr < current->start)
844867a482dSJohn Dyson 			addr = current->start;
845867a482dSJohn Dyson 		cend = current->end;
846867a482dSJohn Dyson 		if (cend > end)
847867a482dSJohn Dyson 			cend = end;
848867a482dSJohn Dyson 
84901cef4caSMark Johnston 		for (; addr < cend; addr += PAGE_SIZE) {
850867a482dSJohn Dyson 			/*
851867a482dSJohn Dyson 			 * Check pmap first, it is likely faster, also
852867a482dSJohn Dyson 			 * it can provide info as to whether we are the
853867a482dSJohn Dyson 			 * one referencing or modifying the page.
854867a482dSJohn Dyson 			 */
855567e51e1SAlan Cox 			m = NULL;
85601cef4caSMark Johnston 			object = NULL;
85701cef4caSMark Johnston retry:
85801cef4caSMark Johnston 			pa = 0;
85901cef4caSMark Johnston 			mincoreinfo = pmap_mincore(pmap, addr, &pa);
8603fbc2e00SKonstantin Belousov 			if (mincore_mapped) {
8613fbc2e00SKonstantin Belousov 				/*
8623fbc2e00SKonstantin Belousov 				 * We only care about this pmap's
8633fbc2e00SKonstantin Belousov 				 * mapping of the page, if any.
8643fbc2e00SKonstantin Belousov 				 */
86501cef4caSMark Johnston 				;
86601cef4caSMark Johnston 			} else if (pa != 0) {
867867a482dSJohn Dyson 				/*
868567e51e1SAlan Cox 				 * The page is mapped by this process but not
869567e51e1SAlan Cox 				 * both accessed and modified.  It is also
870567e51e1SAlan Cox 				 * managed.  Acquire the object lock so that
87101cef4caSMark Johnston 				 * other mappings might be examined.  The page's
87201cef4caSMark Johnston 				 * identity may change at any point before its
87301cef4caSMark Johnston 				 * object lock is acquired, so re-validate if
87401cef4caSMark Johnston 				 * necessary.
875867a482dSJohn Dyson 				 */
87601cef4caSMark Johnston 				m = PHYS_TO_VM_PAGE(pa);
87701cef4caSMark Johnston 				while (object == NULL || m->object != object) {
878567e51e1SAlan Cox 					if (object != NULL)
87989f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
88023ed568cSMateusz Guzik 					object = atomic_load_ptr(&m->object);
88101cef4caSMark Johnston 					if (object == NULL)
882567e51e1SAlan Cox 						goto retry;
88301cef4caSMark Johnston 					VM_OBJECT_WLOCK(object);
884567e51e1SAlan Cox 				}
88501cef4caSMark Johnston 				if (pa != pmap_extract(pmap, addr))
88601cef4caSMark Johnston 					goto retry;
8870012f373SJeff Roberson 				KASSERT(vm_page_all_valid(m),
888567e51e1SAlan Cox 				    ("mincore: page %p is mapped but invalid",
889567e51e1SAlan Cox 				    m));
890567e51e1SAlan Cox 			} else if (mincoreinfo == 0) {
891567e51e1SAlan Cox 				/*
892567e51e1SAlan Cox 				 * The page is not mapped by this process.  If
893567e51e1SAlan Cox 				 * the object implements managed pages, then
894567e51e1SAlan Cox 				 * determine if the page is resident so that
895567e51e1SAlan Cox 				 * the mappings might be examined.
896567e51e1SAlan Cox 				 */
897567e51e1SAlan Cox 				if (current->object.vm_object != object) {
898567e51e1SAlan Cox 					if (object != NULL)
89989f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
900567e51e1SAlan Cox 					object = current->object.vm_object;
90189f6b863SAttilio Rao 					VM_OBJECT_WLOCK(object);
902567e51e1SAlan Cox 				}
9030cb2610eSMark Johnston 				if ((object->flags & OBJ_SWAP) != 0 ||
904567e51e1SAlan Cox 				    object->type == OBJT_VNODE) {
905567e51e1SAlan Cox 					pindex = OFF_TO_IDX(current->offset +
906567e51e1SAlan Cox 					    (addr - current->start));
907567e51e1SAlan Cox 					m = vm_page_lookup(object, pindex);
9080012f373SJeff Roberson 					if (m != NULL && vm_page_none_valid(m))
909567e51e1SAlan Cox 						m = NULL;
910567e51e1SAlan Cox 					if (m != NULL)
911567e51e1SAlan Cox 						mincoreinfo = MINCORE_INCORE;
912567e51e1SAlan Cox 				}
913567e51e1SAlan Cox 			}
914567e51e1SAlan Cox 			if (m != NULL) {
91501cef4caSMark Johnston 				VM_OBJECT_ASSERT_WLOCKED(m->object);
91601cef4caSMark Johnston 
91701cef4caSMark Johnston 				/* Examine other mappings of the page. */
918567e51e1SAlan Cox 				if (m->dirty == 0 && pmap_is_modified(m))
919567e51e1SAlan Cox 					vm_page_dirty(m);
920567e51e1SAlan Cox 				if (m->dirty != 0)
921867a482dSJohn Dyson 					mincoreinfo |= MINCORE_MODIFIED_OTHER;
92201cef4caSMark Johnston 
923c46b90e9SAlan Cox 				/*
9243407fefeSKonstantin Belousov 				 * The first test for PGA_REFERENCED is an
925c46b90e9SAlan Cox 				 * optimization.  The second test is
926c46b90e9SAlan Cox 				 * required because a concurrent pmap
927c46b90e9SAlan Cox 				 * operation could clear the last reference
9283407fefeSKonstantin Belousov 				 * and set PGA_REFERENCED before the call to
929c46b90e9SAlan Cox 				 * pmap_is_referenced().
930c46b90e9SAlan Cox 				 */
9315cff1f4dSMark Johnston 				if ((m->a.flags & PGA_REFERENCED) != 0 ||
932c46b90e9SAlan Cox 				    pmap_is_referenced(m) ||
9335cff1f4dSMark Johnston 				    (m->a.flags & PGA_REFERENCED) != 0)
934867a482dSJohn Dyson 					mincoreinfo |= MINCORE_REFERENCED_OTHER;
9359b5a5d81SJohn Dyson 			}
936567e51e1SAlan Cox 			if (object != NULL)
93789f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(object);
938867a482dSJohn Dyson 
939867a482dSJohn Dyson 			/*
940dd2622a8SAlan Cox 			 * subyte may page fault.  In case it needs to modify
941dd2622a8SAlan Cox 			 * the map, we release the lock.
942dd2622a8SAlan Cox 			 */
943dd2622a8SAlan Cox 			vm_map_unlock_read(map);
944dd2622a8SAlan Cox 
945dd2622a8SAlan Cox 			/*
946867a482dSJohn Dyson 			 * calculate index into user supplied byte vector
947867a482dSJohn Dyson 			 */
948d1780e8dSKonstantin Belousov 			vecindex = atop(addr - first_addr);
949867a482dSJohn Dyson 
950867a482dSJohn Dyson 			/*
951867a482dSJohn Dyson 			 * If we have skipped map entries, we need to make sure that
952867a482dSJohn Dyson 			 * the byte vector is zeroed for those skipped entries.
953867a482dSJohn Dyson 			 */
954867a482dSJohn Dyson 			while ((lastvecindex + 1) < vecindex) {
9556a87d217SJohn Baldwin 				++lastvecindex;
956867a482dSJohn Dyson 				error = subyte(vec + lastvecindex, 0);
957867a482dSJohn Dyson 				if (error) {
958d2c60af8SMatthew Dillon 					error = EFAULT;
959d2c60af8SMatthew Dillon 					goto done2;
960867a482dSJohn Dyson 				}
961867a482dSJohn Dyson 			}
962867a482dSJohn Dyson 
963867a482dSJohn Dyson 			/*
964867a482dSJohn Dyson 			 * Pass the page information to the user
965867a482dSJohn Dyson 			 */
966867a482dSJohn Dyson 			error = subyte(vec + vecindex, mincoreinfo);
967867a482dSJohn Dyson 			if (error) {
968d2c60af8SMatthew Dillon 				error = EFAULT;
969d2c60af8SMatthew Dillon 				goto done2;
970867a482dSJohn Dyson 			}
971dd2622a8SAlan Cox 
972dd2622a8SAlan Cox 			/*
973dd2622a8SAlan Cox 			 * If the map has changed, due to the subyte, the previous
974dd2622a8SAlan Cox 			 * output may be invalid.
975dd2622a8SAlan Cox 			 */
976dd2622a8SAlan Cox 			vm_map_lock_read(map);
977dd2622a8SAlan Cox 			if (timestamp != map->timestamp)
978dd2622a8SAlan Cox 				goto RestartScan;
979dd2622a8SAlan Cox 
980867a482dSJohn Dyson 			lastvecindex = vecindex;
98102c04a2fSJohn Dyson 		}
982867a482dSJohn Dyson 	}
983867a482dSJohn Dyson 
984867a482dSJohn Dyson 	/*
985dd2622a8SAlan Cox 	 * subyte may page fault.  In case it needs to modify
986dd2622a8SAlan Cox 	 * the map, we release the lock.
987dd2622a8SAlan Cox 	 */
988dd2622a8SAlan Cox 	vm_map_unlock_read(map);
989dd2622a8SAlan Cox 
990dd2622a8SAlan Cox 	/*
991867a482dSJohn Dyson 	 * Zero the last entries in the byte vector.
992867a482dSJohn Dyson 	 */
993d1780e8dSKonstantin Belousov 	vecindex = atop(end - first_addr);
994867a482dSJohn Dyson 	while ((lastvecindex + 1) < vecindex) {
9956a87d217SJohn Baldwin 		++lastvecindex;
996867a482dSJohn Dyson 		error = subyte(vec + lastvecindex, 0);
997867a482dSJohn Dyson 		if (error) {
998d2c60af8SMatthew Dillon 			error = EFAULT;
999d2c60af8SMatthew Dillon 			goto done2;
1000867a482dSJohn Dyson 		}
1001867a482dSJohn Dyson 	}
1002867a482dSJohn Dyson 
1003dd2622a8SAlan Cox 	/*
1004dd2622a8SAlan Cox 	 * If the map has changed, due to the subyte, the previous
1005dd2622a8SAlan Cox 	 * output may be invalid.
1006dd2622a8SAlan Cox 	 */
1007dd2622a8SAlan Cox 	vm_map_lock_read(map);
1008dd2622a8SAlan Cox 	if (timestamp != map->timestamp)
1009dd2622a8SAlan Cox 		goto RestartScan;
1010eff50fcdSAlan Cox 	vm_map_unlock_read(map);
1011d2c60af8SMatthew Dillon done2:
1012d2c60af8SMatthew Dillon 	return (error);
1013df8bae1dSRodney W. Grimes }
1014df8bae1dSRodney W. Grimes 
1015d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1016df8bae1dSRodney W. Grimes struct mlock_args {
1017651bb817SAlexander Langer 	const void *addr;
1018df8bae1dSRodney W. Grimes 	size_t len;
1019df8bae1dSRodney W. Grimes };
1020d2d3e875SBruce Evans #endif
1021df8bae1dSRodney W. Grimes int
sys_mlock(struct thread * td,struct mlock_args * uap)102204e89ffbSKonstantin Belousov sys_mlock(struct thread *td, struct mlock_args *uap)
1023df8bae1dSRodney W. Grimes {
1024995d7069SGleb Smirnoff 
1025496ab053SKonstantin Belousov 	return (kern_mlock(td->td_proc, td->td_ucred,
1026496ab053SKonstantin Belousov 	    __DECONST(uintptr_t, uap->addr), uap->len));
1027995d7069SGleb Smirnoff }
1028995d7069SGleb Smirnoff 
1029995d7069SGleb Smirnoff int
kern_mlock(struct proc * proc,struct ucred * cred,uintptr_t addr0,size_t len)1030496ab053SKonstantin Belousov kern_mlock(struct proc *proc, struct ucred *cred, uintptr_t addr0, size_t len)
1031995d7069SGleb Smirnoff {
1032bb734798SDon Lewis 	vm_offset_t addr, end, last, start;
1033bb734798SDon Lewis 	vm_size_t npages, size;
10343ac7d297SAndrey Zonov 	vm_map_t map;
10351ba5ad42SEdward Tomasz Napierala 	unsigned long nsize;
1036bb734798SDon Lewis 	int error;
1037df8bae1dSRodney W. Grimes 
1038cc426dd3SMateusz Guzik 	error = priv_check_cred(cred, PRIV_VM_MLOCK);
103947934cefSDon Lewis 	if (error)
104047934cefSDon Lewis 		return (error);
1041496ab053SKonstantin Belousov 	addr = addr0;
1042995d7069SGleb Smirnoff 	size = len;
1043bb734798SDon Lewis 	last = addr + size;
104416929939SDon Lewis 	start = trunc_page(addr);
1045bb734798SDon Lewis 	end = round_page(last);
1046bb734798SDon Lewis 	if (last < addr || end < addr)
1047df8bae1dSRodney W. Grimes 		return (EINVAL);
104816929939SDon Lewis 	npages = atop(end - start);
104954a3a114SMark Johnston 	if (npages > vm_page_max_user_wired)
105016929939SDon Lewis 		return (ENOMEM);
10513ac7d297SAndrey Zonov 	map = &proc->p_vmspace->vm_map;
105247934cefSDon Lewis 	PROC_LOCK(proc);
10533ac7d297SAndrey Zonov 	nsize = ptoa(npages + pmap_wired_count(map->pmap));
1054f6f6d240SMateusz Guzik 	if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
105547934cefSDon Lewis 		PROC_UNLOCK(proc);
10564a40e3d4SJohn Dyson 		return (ENOMEM);
105791d5354aSJohn Baldwin 	}
105847934cefSDon Lewis 	PROC_UNLOCK(proc);
1059afcc55f3SEdward Tomasz Napierala #ifdef RACCT
10604b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
10611ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(proc);
10621ba5ad42SEdward Tomasz Napierala 		error = racct_set(proc, RACCT_MEMLOCK, nsize);
10631ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(proc);
10641ba5ad42SEdward Tomasz Napierala 		if (error != 0)
10651ba5ad42SEdward Tomasz Napierala 			return (ENOMEM);
10664b5c9cf6SEdward Tomasz Napierala 	}
1067afcc55f3SEdward Tomasz Napierala #endif
10683ac7d297SAndrey Zonov 	error = vm_map_wire(map, start, end,
106916929939SDon Lewis 	    VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1070afcc55f3SEdward Tomasz Napierala #ifdef RACCT
10714b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && error != KERN_SUCCESS) {
10721ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(proc);
10731ba5ad42SEdward Tomasz Napierala 		racct_set(proc, RACCT_MEMLOCK,
10743ac7d297SAndrey Zonov 		    ptoa(pmap_wired_count(map->pmap)));
10751ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(proc);
10761ba5ad42SEdward Tomasz Napierala 	}
1077afcc55f3SEdward Tomasz Napierala #endif
1078d301b358SKonstantin Belousov 	switch (error) {
1079d301b358SKonstantin Belousov 	case KERN_SUCCESS:
1080d301b358SKonstantin Belousov 		return (0);
1081d301b358SKonstantin Belousov 	case KERN_INVALID_ARGUMENT:
1082d301b358SKonstantin Belousov 		return (EINVAL);
1083d301b358SKonstantin Belousov 	default:
1084d301b358SKonstantin Belousov 		return (ENOMEM);
1085d301b358SKonstantin Belousov 	}
1086df8bae1dSRodney W. Grimes }
1087df8bae1dSRodney W. Grimes 
1088d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
10894a40e3d4SJohn Dyson struct mlockall_args {
10904a40e3d4SJohn Dyson 	int	how;
10914a40e3d4SJohn Dyson };
10924a40e3d4SJohn Dyson #endif
10934a40e3d4SJohn Dyson 
10944a40e3d4SJohn Dyson int
sys_mlockall(struct thread * td,struct mlockall_args * uap)109504e89ffbSKonstantin Belousov sys_mlockall(struct thread *td, struct mlockall_args *uap)
10964a40e3d4SJohn Dyson {
1097abd498aaSBruce M Simpson 	vm_map_t map;
1098abd498aaSBruce M Simpson 	int error;
1099abd498aaSBruce M Simpson 
1100abd498aaSBruce M Simpson 	map = &td->td_proc->p_vmspace->vm_map;
11017e19eda4SAndrey Zonov 	error = priv_check(td, PRIV_VM_MLOCK);
11027e19eda4SAndrey Zonov 	if (error)
11037e19eda4SAndrey Zonov 		return (error);
1104abd498aaSBruce M Simpson 
1105abd498aaSBruce M Simpson 	if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1106abd498aaSBruce M Simpson 		return (EINVAL);
1107abd498aaSBruce M Simpson 
1108abd498aaSBruce M Simpson 	/*
1109abd498aaSBruce M Simpson 	 * If wiring all pages in the process would cause it to exceed
1110abd498aaSBruce M Simpson 	 * a hard resource limit, return ENOMEM.
1111abd498aaSBruce M Simpson 	 */
11127e19eda4SAndrey Zonov 	if (!old_mlock && uap->how & MCL_CURRENT) {
11132554f86aSMateusz Guzik 		if (map->size > lim_cur(td, RLIMIT_MEMLOCK))
1114abd498aaSBruce M Simpson 			return (ENOMEM);
111591d5354aSJohn Baldwin 	}
1116afcc55f3SEdward Tomasz Napierala #ifdef RACCT
11174b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
11181ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
11191ba5ad42SEdward Tomasz Napierala 		error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
11201ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
11211ba5ad42SEdward Tomasz Napierala 		if (error != 0)
11221ba5ad42SEdward Tomasz Napierala 			return (ENOMEM);
11234b5c9cf6SEdward Tomasz Napierala 	}
1124afcc55f3SEdward Tomasz Napierala #endif
1125abd498aaSBruce M Simpson 
1126abd498aaSBruce M Simpson 	if (uap->how & MCL_FUTURE) {
1127abd498aaSBruce M Simpson 		vm_map_lock(map);
1128abd498aaSBruce M Simpson 		vm_map_modflags(map, MAP_WIREFUTURE, 0);
1129abd498aaSBruce M Simpson 		vm_map_unlock(map);
1130abd498aaSBruce M Simpson 		error = 0;
1131abd498aaSBruce M Simpson 	}
1132abd498aaSBruce M Simpson 
1133abd498aaSBruce M Simpson 	if (uap->how & MCL_CURRENT) {
1134abd498aaSBruce M Simpson 		/*
1135abd498aaSBruce M Simpson 		 * P1003.1-2001 mandates that all currently mapped pages
1136abd498aaSBruce M Simpson 		 * will be memory resident and locked (wired) upon return
1137abd498aaSBruce M Simpson 		 * from mlockall(). vm_map_wire() will wire pages, by
1138abd498aaSBruce M Simpson 		 * calling vm_fault_wire() for each page in the region.
1139abd498aaSBruce M Simpson 		 */
1140abd498aaSBruce M Simpson 		error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1141abd498aaSBruce M Simpson 		    VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
114254a3a114SMark Johnston 		if (error == KERN_SUCCESS)
114354a3a114SMark Johnston 			error = 0;
114454a3a114SMark Johnston 		else if (error == KERN_RESOURCE_SHORTAGE)
114554a3a114SMark Johnston 			error = ENOMEM;
114654a3a114SMark Johnston 		else
114754a3a114SMark Johnston 			error = EAGAIN;
1148abd498aaSBruce M Simpson 	}
1149afcc55f3SEdward Tomasz Napierala #ifdef RACCT
11504b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && error != KERN_SUCCESS) {
11511ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
11521ba5ad42SEdward Tomasz Napierala 		racct_set(td->td_proc, RACCT_MEMLOCK,
11533ac7d297SAndrey Zonov 		    ptoa(pmap_wired_count(map->pmap)));
11541ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
11551ba5ad42SEdward Tomasz Napierala 	}
1156afcc55f3SEdward Tomasz Napierala #endif
1157abd498aaSBruce M Simpson 
1158abd498aaSBruce M Simpson 	return (error);
11594a40e3d4SJohn Dyson }
11604a40e3d4SJohn Dyson 
11614a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_
1162fa721254SAlfred Perlstein struct munlockall_args {
1163abd498aaSBruce M Simpson 	register_t dummy;
11644a40e3d4SJohn Dyson };
11654a40e3d4SJohn Dyson #endif
11664a40e3d4SJohn Dyson 
11674a40e3d4SJohn Dyson int
sys_munlockall(struct thread * td,struct munlockall_args * uap)116804e89ffbSKonstantin Belousov sys_munlockall(struct thread *td, struct munlockall_args *uap)
11694a40e3d4SJohn Dyson {
1170abd498aaSBruce M Simpson 	vm_map_t map;
1171abd498aaSBruce M Simpson 	int error;
1172abd498aaSBruce M Simpson 
1173abd498aaSBruce M Simpson 	map = &td->td_proc->p_vmspace->vm_map;
1174acd3428bSRobert Watson 	error = priv_check(td, PRIV_VM_MUNLOCK);
1175abd498aaSBruce M Simpson 	if (error)
1176abd498aaSBruce M Simpson 		return (error);
1177abd498aaSBruce M Simpson 
1178abd498aaSBruce M Simpson 	/* Clear the MAP_WIREFUTURE flag from this vm_map. */
1179abd498aaSBruce M Simpson 	vm_map_lock(map);
1180abd498aaSBruce M Simpson 	vm_map_modflags(map, 0, MAP_WIREFUTURE);
1181abd498aaSBruce M Simpson 	vm_map_unlock(map);
1182abd498aaSBruce M Simpson 
1183abd498aaSBruce M Simpson 	/* Forcibly unwire all pages. */
1184abd498aaSBruce M Simpson 	error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1185abd498aaSBruce M Simpson 	    VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1186afcc55f3SEdward Tomasz Napierala #ifdef RACCT
11874b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && error == KERN_SUCCESS) {
11881ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
11891ba5ad42SEdward Tomasz Napierala 		racct_set(td->td_proc, RACCT_MEMLOCK, 0);
11901ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
11911ba5ad42SEdward Tomasz Napierala 	}
1192afcc55f3SEdward Tomasz Napierala #endif
1193abd498aaSBruce M Simpson 
1194abd498aaSBruce M Simpson 	return (error);
11954a40e3d4SJohn Dyson }
11964a40e3d4SJohn Dyson 
11974a40e3d4SJohn Dyson #ifndef _SYS_SYSPROTO_H_
1198df8bae1dSRodney W. Grimes struct munlock_args {
1199651bb817SAlexander Langer 	const void *addr;
1200df8bae1dSRodney W. Grimes 	size_t len;
1201df8bae1dSRodney W. Grimes };
1202d2d3e875SBruce Evans #endif
1203df8bae1dSRodney W. Grimes int
sys_munlock(struct thread * td,struct munlock_args * uap)120469cdfcefSEdward Tomasz Napierala sys_munlock(struct thread *td, struct munlock_args *uap)
1205df8bae1dSRodney W. Grimes {
120669cdfcefSEdward Tomasz Napierala 
1207496ab053SKonstantin Belousov 	return (kern_munlock(td, (uintptr_t)uap->addr, uap->len));
120869cdfcefSEdward Tomasz Napierala }
120969cdfcefSEdward Tomasz Napierala 
121069cdfcefSEdward Tomasz Napierala int
kern_munlock(struct thread * td,uintptr_t addr0,size_t size)1211496ab053SKonstantin Belousov kern_munlock(struct thread *td, uintptr_t addr0, size_t size)
121269cdfcefSEdward Tomasz Napierala {
1213496ab053SKonstantin Belousov 	vm_offset_t addr, end, last, start;
1214fc2b1679SJeremie Le Hen #ifdef RACCT
1215c92b5069SJeremie Le Hen 	vm_map_t map;
1216fc2b1679SJeremie Le Hen #endif
1217df8bae1dSRodney W. Grimes 	int error;
1218df8bae1dSRodney W. Grimes 
1219acd3428bSRobert Watson 	error = priv_check(td, PRIV_VM_MUNLOCK);
122047934cefSDon Lewis 	if (error)
122147934cefSDon Lewis 		return (error);
1222496ab053SKonstantin Belousov 	addr = addr0;
1223bb734798SDon Lewis 	last = addr + size;
122416929939SDon Lewis 	start = trunc_page(addr);
1225bb734798SDon Lewis 	end = round_page(last);
1226bb734798SDon Lewis 	if (last < addr || end < addr)
1227df8bae1dSRodney W. Grimes 		return (EINVAL);
122816929939SDon Lewis 	error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
122916929939SDon Lewis 	    VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1230afcc55f3SEdward Tomasz Napierala #ifdef RACCT
12314b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && error == KERN_SUCCESS) {
12321ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
1233c92b5069SJeremie Le Hen 		map = &td->td_proc->p_vmspace->vm_map;
1234c92b5069SJeremie Le Hen 		racct_set(td->td_proc, RACCT_MEMLOCK,
1235c92b5069SJeremie Le Hen 		    ptoa(pmap_wired_count(map->pmap)));
12361ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
12371ba5ad42SEdward Tomasz Napierala 	}
1238afcc55f3SEdward Tomasz Napierala #endif
1239df8bae1dSRodney W. Grimes 	return (error == KERN_SUCCESS ? 0 : ENOMEM);
1240df8bae1dSRodney W. Grimes }
1241df8bae1dSRodney W. Grimes 
1242df8bae1dSRodney W. Grimes /*
1243c8daea13SAlexander Kabaev  * vm_mmap_vnode()
1244c8daea13SAlexander Kabaev  *
1245c8daea13SAlexander Kabaev  * Helper function for vm_mmap.  Perform sanity check specific for mmap
1246c8daea13SAlexander Kabaev  * operations on vnodes.
1247c8daea13SAlexander Kabaev  */
1248c8daea13SAlexander Kabaev 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)1249c8daea13SAlexander Kabaev vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1250c8daea13SAlexander Kabaev     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
125184110e7eSKonstantin Belousov     struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
125284110e7eSKonstantin Belousov     boolean_t *writecounted)
1253c8daea13SAlexander Kabaev {
1254c8daea13SAlexander Kabaev 	struct vattr va;
1255c8daea13SAlexander Kabaev 	vm_object_t obj;
1256bd0e1bebSMark Johnston 	vm_ooffset_t foff;
12570359a12eSAttilio Rao 	struct ucred *cred;
125878022527SKonstantin Belousov 	int error, flags;
125978022527SKonstantin Belousov 	bool writex;
1260c8daea13SAlexander Kabaev 
12610359a12eSAttilio Rao 	cred = td->td_ucred;
126278022527SKonstantin Belousov 	writex = (*maxprotp & VM_PROT_WRITE) != 0 &&
126378022527SKonstantin Belousov 	    (*flagsp & MAP_SHARED) != 0;
1264a92a971bSMateusz Guzik 	if ((error = vget(vp, LK_SHARED)) != 0)
1265c8daea13SAlexander Kabaev 		return (error);
12660df42647SRobert Watson 	AUDIT_ARG_VNODE1(vp);
126764345f0bSJohn Baldwin 	foff = *foffp;
1268c8daea13SAlexander Kabaev 	flags = *flagsp;
12698516dd18SPoul-Henning Kamp 	obj = vp->v_object;
1270c8daea13SAlexander Kabaev 	if (vp->v_type == VREG) {
1271c8daea13SAlexander Kabaev 		/*
1272c8daea13SAlexander Kabaev 		 * Get the proper underlying object
1273c8daea13SAlexander Kabaev 		 */
12748516dd18SPoul-Henning Kamp 		if (obj == NULL) {
1275c8daea13SAlexander Kabaev 			error = EINVAL;
1276c8daea13SAlexander Kabaev 			goto done;
1277c8daea13SAlexander Kabaev 		}
1278e5f299ffSKonstantin Belousov 		if (obj->type == OBJT_VNODE && obj->handle != vp) {
1279c8daea13SAlexander Kabaev 			vput(vp);
1280c8daea13SAlexander Kabaev 			vp = (struct vnode *)obj->handle;
128184110e7eSKonstantin Belousov 			/*
128284110e7eSKonstantin Belousov 			 * Bypass filesystems obey the mpsafety of the
128353f5f8a0SKonstantin Belousov 			 * underlying fs.  Tmpfs never bypasses.
128484110e7eSKonstantin Belousov 			 */
1285a92a971bSMateusz Guzik 			error = vget(vp, LK_SHARED);
12865050aa86SKonstantin Belousov 			if (error != 0)
128784110e7eSKonstantin Belousov 				return (error);
128884110e7eSKonstantin Belousov 		}
128978022527SKonstantin Belousov 		if (writex) {
129084110e7eSKonstantin Belousov 			*writecounted = TRUE;
1291fe7bcbafSKyle Evans 			vm_pager_update_writecount(obj, 0, objsize);
129284110e7eSKonstantin Belousov 		}
1293c8daea13SAlexander Kabaev 	} else {
1294c8daea13SAlexander Kabaev 		error = EINVAL;
1295c8daea13SAlexander Kabaev 		goto done;
1296c8daea13SAlexander Kabaev 	}
12970359a12eSAttilio Rao 	if ((error = VOP_GETATTR(vp, &va, cred)))
1298c8daea13SAlexander Kabaev 		goto done;
1299c92163dcSChristian S.J. Peron #ifdef MAC
13007077c426SJohn Baldwin 	/* This relies on VM_PROT_* matching PROT_*. */
13017077c426SJohn Baldwin 	error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1302c92163dcSChristian S.J. Peron 	if (error != 0)
1303c92163dcSChristian S.J. Peron 		goto done;
1304c92163dcSChristian S.J. Peron #endif
1305c8daea13SAlexander Kabaev 	if ((flags & MAP_SHARED) != 0) {
1306c8daea13SAlexander Kabaev 		if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
13077077c426SJohn Baldwin 			if (prot & VM_PROT_WRITE) {
1308c8daea13SAlexander Kabaev 				error = EPERM;
1309c8daea13SAlexander Kabaev 				goto done;
1310c8daea13SAlexander Kabaev 			}
1311c8daea13SAlexander Kabaev 			*maxprotp &= ~VM_PROT_WRITE;
1312c8daea13SAlexander Kabaev 		}
1313c8daea13SAlexander Kabaev 	}
1314c8daea13SAlexander Kabaev 	/*
1315c8daea13SAlexander Kabaev 	 * If it is a regular file without any references
1316c8daea13SAlexander Kabaev 	 * we do not need to sync it.
1317c8daea13SAlexander Kabaev 	 * Adjust object size to be the size of actual file.
1318c8daea13SAlexander Kabaev 	 */
1319c8daea13SAlexander Kabaev 	objsize = round_page(va.va_size);
1320c8daea13SAlexander Kabaev 	if (va.va_nlink == 0)
1321c8daea13SAlexander Kabaev 		flags |= MAP_NOSYNC;
13223d653db0SAlan Cox 	if (obj->type == OBJT_VNODE) {
1323e5f299ffSKonstantin Belousov 		obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1324e5f299ffSKonstantin Belousov 		    cred);
1325c8daea13SAlexander Kabaev 		if (obj == NULL) {
132664345f0bSJohn Baldwin 			error = ENOMEM;
1327c8daea13SAlexander Kabaev 			goto done;
1328c8daea13SAlexander Kabaev 		}
13293d653db0SAlan Cox 	} else {
13300cb2610eSMark Johnston 		KASSERT((obj->flags & OBJ_SWAP) != 0, ("wrong object type"));
1331f2410510SJeff Roberson 		vm_object_reference(obj);
13323d653db0SAlan Cox #if VM_NRESERVLEVEL > 0
1333f2410510SJeff Roberson 		if ((obj->flags & OBJ_COLORED) == 0) {
1334f2410510SJeff Roberson 			VM_OBJECT_WLOCK(obj);
13353d653db0SAlan Cox 			vm_object_color(obj, 0);
13363d653db0SAlan Cox 			VM_OBJECT_WUNLOCK(obj);
13373d653db0SAlan Cox 		}
1338f2410510SJeff Roberson #endif
1339f2410510SJeff Roberson 	}
1340c8daea13SAlexander Kabaev 	*objp = obj;
1341c8daea13SAlexander Kabaev 	*flagsp = flags;
134264345f0bSJohn Baldwin 
1343643656cfSMateusz Guzik 	VOP_MMAPPED(vp);
13441e309003SDiomidis Spinellis 
1345c8daea13SAlexander Kabaev done:
1346bafa6cfcSKonstantin Belousov 	if (error != 0 && *writecounted) {
1347bafa6cfcSKonstantin Belousov 		*writecounted = FALSE;
1348fe7bcbafSKyle Evans 		vm_pager_update_writecount(obj, objsize, 0);
1349bafa6cfcSKonstantin Belousov 	}
1350c8daea13SAlexander Kabaev 	vput(vp);
1351c8daea13SAlexander Kabaev 	return (error);
1352c8daea13SAlexander Kabaev }
1353c8daea13SAlexander Kabaev 
1354c8daea13SAlexander Kabaev /*
135598df9218SJohn Baldwin  * vm_mmap_cdev()
135698df9218SJohn Baldwin  *
135798df9218SJohn Baldwin  * Helper function for vm_mmap.  Perform sanity check specific for mmap
135898df9218SJohn Baldwin  * operations on cdevs.
135998df9218SJohn Baldwin  */
136098df9218SJohn Baldwin 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)13617077c426SJohn Baldwin vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
13627077c426SJohn Baldwin     vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
13637077c426SJohn Baldwin     vm_ooffset_t *foff, vm_object_t *objp)
136498df9218SJohn Baldwin {
136598df9218SJohn Baldwin 	vm_object_t obj;
13667077c426SJohn Baldwin 	int error, flags;
136798df9218SJohn Baldwin 
136898df9218SJohn Baldwin 	flags = *flagsp;
136998df9218SJohn Baldwin 
137091a35e78SKonstantin Belousov 	if (dsw->d_flags & D_MMAP_ANON) {
13717077c426SJohn Baldwin 		*objp = NULL;
13727077c426SJohn Baldwin 		*foff = 0;
137398df9218SJohn Baldwin 		*maxprotp = VM_PROT_ALL;
137498df9218SJohn Baldwin 		*flagsp |= MAP_ANON;
137598df9218SJohn Baldwin 		return (0);
137698df9218SJohn Baldwin 	}
137798df9218SJohn Baldwin 	/*
137864345f0bSJohn Baldwin 	 * cdevs do not provide private mappings of any kind.
137998df9218SJohn Baldwin 	 */
138098df9218SJohn Baldwin 	if ((*maxprotp & VM_PROT_WRITE) == 0 &&
13817077c426SJohn Baldwin 	    (prot & VM_PROT_WRITE) != 0)
138298df9218SJohn Baldwin 		return (EACCES);
13837077c426SJohn Baldwin 	if (flags & (MAP_PRIVATE|MAP_COPY))
138498df9218SJohn Baldwin 		return (EINVAL);
138598df9218SJohn Baldwin 	/*
138698df9218SJohn Baldwin 	 * Force device mappings to be shared.
138798df9218SJohn Baldwin 	 */
138898df9218SJohn Baldwin 	flags |= MAP_SHARED;
138998df9218SJohn Baldwin #ifdef MAC_XXX
13907077c426SJohn Baldwin 	error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
13917077c426SJohn Baldwin 	if (error != 0)
139298df9218SJohn Baldwin 		return (error);
139398df9218SJohn Baldwin #endif
139464345f0bSJohn Baldwin 	/*
139564345f0bSJohn Baldwin 	 * First, try d_mmap_single().  If that is not implemented
139664345f0bSJohn Baldwin 	 * (returns ENODEV), fall back to using the device pager.
139764345f0bSJohn Baldwin 	 * Note that d_mmap_single() must return a reference to the
139864345f0bSJohn Baldwin 	 * object (it needs to bump the reference count of the object
139964345f0bSJohn Baldwin 	 * it returns somehow).
140064345f0bSJohn Baldwin 	 *
140164345f0bSJohn Baldwin 	 * XXX assumes VM_PROT_* == PROT_*
140264345f0bSJohn Baldwin 	 */
140364345f0bSJohn Baldwin 	error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
140464345f0bSJohn Baldwin 	if (error != ENODEV)
140564345f0bSJohn Baldwin 		return (error);
14063364c323SKonstantin Belousov 	obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
14073364c323SKonstantin Belousov 	    td->td_ucred);
140898df9218SJohn Baldwin 	if (obj == NULL)
140998df9218SJohn Baldwin 		return (EINVAL);
1410*580340dbSKonstantin Belousov 	VM_OBJECT_WLOCK(obj);
1411*580340dbSKonstantin Belousov 	vm_object_set_flag(obj, OBJ_CDEVH);
1412*580340dbSKonstantin Belousov 	VM_OBJECT_WUNLOCK(obj);
141398df9218SJohn Baldwin 	*objp = obj;
141498df9218SJohn Baldwin 	*flagsp = flags;
141598df9218SJohn Baldwin 	return (0);
141698df9218SJohn Baldwin }
141798df9218SJohn Baldwin 
1418df8bae1dSRodney W. Grimes 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)1419b9dcd593SBruce Evans vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1420b9dcd593SBruce Evans 	vm_prot_t maxprot, int flags,
142198df9218SJohn Baldwin 	objtype_t handle_type, void *handle,
1422b9dcd593SBruce Evans 	vm_ooffset_t foff)
1423df8bae1dSRodney W. Grimes {
14247077c426SJohn Baldwin 	vm_object_t object;
1425b40ce416SJulian Elischer 	struct thread *td = curthread;
14267077c426SJohn Baldwin 	int error;
142784110e7eSKonstantin Belousov 	boolean_t writecounted;
1428df8bae1dSRodney W. Grimes 
1429df8bae1dSRodney W. Grimes 	if (size == 0)
14307077c426SJohn Baldwin 		return (EINVAL);
1431df8bae1dSRodney W. Grimes 
1432749474f2SPeter Wemm 	size = round_page(size);
1433010ba384SMark Johnston 	object = NULL;
14347077c426SJohn Baldwin 	writecounted = FALSE;
14357077c426SJohn Baldwin 
14367077c426SJohn Baldwin 	switch (handle_type) {
14377077c426SJohn Baldwin 	case OBJT_DEVICE: {
14387077c426SJohn Baldwin 		struct cdevsw *dsw;
14397077c426SJohn Baldwin 		struct cdev *cdev;
14407077c426SJohn Baldwin 		int ref;
14417077c426SJohn Baldwin 
14427077c426SJohn Baldwin 		cdev = handle;
14437077c426SJohn Baldwin 		dsw = dev_refthread(cdev, &ref);
14447077c426SJohn Baldwin 		if (dsw == NULL)
14457077c426SJohn Baldwin 			return (ENXIO);
14467077c426SJohn Baldwin 		error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
14477077c426SJohn Baldwin 		    dsw, &foff, &object);
14487077c426SJohn Baldwin 		dev_relthread(cdev, ref);
14497077c426SJohn Baldwin 		break;
14507077c426SJohn Baldwin 	}
14517077c426SJohn Baldwin 	case OBJT_VNODE:
14527077c426SJohn Baldwin 		error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
14537077c426SJohn Baldwin 		    handle, &foff, &object, &writecounted);
14547077c426SJohn Baldwin 		break;
14557077c426SJohn Baldwin 	default:
14567077c426SJohn Baldwin 		error = EINVAL;
14577077c426SJohn Baldwin 		break;
14587077c426SJohn Baldwin 	}
14597077c426SJohn Baldwin 	if (error)
14607077c426SJohn Baldwin 		return (error);
14617077c426SJohn Baldwin 
14627077c426SJohn Baldwin 	error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
14637077c426SJohn Baldwin 	    foff, writecounted, td);
14647077c426SJohn Baldwin 	if (error != 0 && object != NULL) {
14657077c426SJohn Baldwin 		/*
14667077c426SJohn Baldwin 		 * If this mapping was accounted for in the vnode's
14677077c426SJohn Baldwin 		 * writecount, then undo that now.
14687077c426SJohn Baldwin 		 */
14697077c426SJohn Baldwin 		if (writecounted)
1470fe7bcbafSKyle Evans 			vm_pager_release_writecount(object, 0, size);
14717077c426SJohn Baldwin 		vm_object_deallocate(object);
14727077c426SJohn Baldwin 	}
14737077c426SJohn Baldwin 	return (error);
14747077c426SJohn Baldwin }
14757077c426SJohn Baldwin 
14767077c426SJohn Baldwin int
kern_mmap_racct_check(struct thread * td,vm_map_t map,vm_size_t size)147767a659d2SKonstantin Belousov kern_mmap_racct_check(struct thread *td, vm_map_t map, vm_size_t size)
14787077c426SJohn Baldwin {
147967a659d2SKonstantin Belousov 	int error;
1480df8bae1dSRodney W. Grimes 
14812554f86aSMateusz Guzik 	RACCT_PROC_LOCK(td->td_proc);
14822554f86aSMateusz Guzik 	if (map->size + size > lim_cur(td, RLIMIT_VMEM)) {
14832554f86aSMateusz Guzik 		RACCT_PROC_UNLOCK(td->td_proc);
1484070f64feSMatthew Dillon 		return (ENOMEM);
1485070f64feSMatthew Dillon 	}
1486a6492969SAlan Cox 	if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
14872554f86aSMateusz Guzik 		RACCT_PROC_UNLOCK(td->td_proc);
14881ba5ad42SEdward Tomasz Napierala 		return (ENOMEM);
14891ba5ad42SEdward Tomasz Napierala 	}
14907e19eda4SAndrey Zonov 	if (!old_mlock && map->flags & MAP_WIREFUTURE) {
14913ac7d297SAndrey Zonov 		if (ptoa(pmap_wired_count(map->pmap)) + size >
14922554f86aSMateusz Guzik 		    lim_cur(td, RLIMIT_MEMLOCK)) {
149367a659d2SKonstantin Belousov 			racct_set_force(td->td_proc, RACCT_VMEM, map->size);
14942554f86aSMateusz Guzik 			RACCT_PROC_UNLOCK(td->td_proc);
14957e19eda4SAndrey Zonov 			return (ENOMEM);
14967e19eda4SAndrey Zonov 		}
14977e19eda4SAndrey Zonov 		error = racct_set(td->td_proc, RACCT_MEMLOCK,
14983ac7d297SAndrey Zonov 		    ptoa(pmap_wired_count(map->pmap)) + size);
14997e19eda4SAndrey Zonov 		if (error != 0) {
150067a659d2SKonstantin Belousov 			racct_set_force(td->td_proc, RACCT_VMEM, map->size);
15012554f86aSMateusz Guzik 			RACCT_PROC_UNLOCK(td->td_proc);
15027e19eda4SAndrey Zonov 			return (error);
15037e19eda4SAndrey Zonov 		}
15047e19eda4SAndrey Zonov 	}
15052554f86aSMateusz Guzik 	RACCT_PROC_UNLOCK(td->td_proc);
150667a659d2SKonstantin Belousov 	return (0);
150767a659d2SKonstantin Belousov }
150867a659d2SKonstantin Belousov 
150967a659d2SKonstantin Belousov /*
151067a659d2SKonstantin Belousov  * Internal version of mmap that maps a specific VM object into an
151167a659d2SKonstantin Belousov  * map.  Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
151267a659d2SKonstantin Belousov  */
151367a659d2SKonstantin Belousov 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)151467a659d2SKonstantin Belousov vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
151567a659d2SKonstantin Belousov     vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
151667a659d2SKonstantin Belousov     boolean_t writecounted, struct thread *td)
151767a659d2SKonstantin Belousov {
151837e5d49eSAlan Cox 	vm_offset_t default_addr, max_addr;
151967a659d2SKonstantin Belousov 	int docow, error, findspace, rv;
152067a659d2SKonstantin Belousov 	bool curmap, fitit;
152167a659d2SKonstantin Belousov 
152267a659d2SKonstantin Belousov 	curmap = map == &td->td_proc->p_vmspace->vm_map;
152367a659d2SKonstantin Belousov 	if (curmap) {
152467a659d2SKonstantin Belousov 		error = kern_mmap_racct_check(td, map, size);
152567a659d2SKonstantin Belousov 		if (error != 0)
152667a659d2SKonstantin Belousov 			return (error);
1527a6492969SAlan Cox 	}
1528070f64feSMatthew Dillon 
1529df8bae1dSRodney W. Grimes 	/*
1530bc9ad247SDavid Greenman 	 * We currently can only deal with page aligned file offsets.
15317077c426SJohn Baldwin 	 * The mmap() system call already enforces this by subtracting
15327077c426SJohn Baldwin 	 * the page offset from the file offset, but checking here
15337077c426SJohn Baldwin 	 * catches errors in device drivers (e.g. d_single_mmap()
15347077c426SJohn Baldwin 	 * callbacks) and other internal mapping requests (such as in
15357077c426SJohn Baldwin 	 * exec).
1536bc9ad247SDavid Greenman 	 */
1537bc9ad247SDavid Greenman 	if (foff & PAGE_MASK)
1538bc9ad247SDavid Greenman 		return (EINVAL);
1539bc9ad247SDavid Greenman 
154006cb7259SDavid Greenman 	if ((flags & MAP_FIXED) == 0) {
15415ec2d94aSAlan Cox 		fitit = true;
154206cb7259SDavid Greenman 		*addr = round_page(*addr);
154306cb7259SDavid Greenman 	} else {
154406cb7259SDavid Greenman 		if (*addr != trunc_page(*addr))
154506cb7259SDavid Greenman 			return (EINVAL);
15465ec2d94aSAlan Cox 		fitit = false;
154706cb7259SDavid Greenman 	}
154884110e7eSKonstantin Belousov 
15495f55e841SDavid Greenman 	if (flags & MAP_ANON) {
15507077c426SJohn Baldwin 		if (object != NULL || foff != 0)
15517077c426SJohn Baldwin 			return (EINVAL);
1552c8daea13SAlexander Kabaev 		docow = 0;
155374ffb9afSAlan Cox 	} else if (flags & MAP_PREFAULT_READ)
155474ffb9afSAlan Cox 		docow = MAP_PREFAULT;
155574ffb9afSAlan Cox 	else
15564738fa09SAlan Cox 		docow = MAP_PREFAULT_PARTIAL;
1557df8bae1dSRodney W. Grimes 
15584f79d873SMatthew Dillon 	if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
15594738fa09SAlan Cox 		docow |= MAP_COPY_ON_WRITE;
15604f79d873SMatthew Dillon 	if (flags & MAP_NOSYNC)
15614f79d873SMatthew Dillon 		docow |= MAP_DISABLE_SYNCER;
15629730a5daSPaul Saab 	if (flags & MAP_NOCORE)
15639730a5daSPaul Saab 		docow |= MAP_DISABLE_COREDUMP;
15648211bd45SKonstantin Belousov 	/* Shared memory is also shared with children. */
15658211bd45SKonstantin Belousov 	if (flags & MAP_SHARED)
15668211bd45SKonstantin Belousov 		docow |= MAP_INHERIT_SHARE;
156784110e7eSKonstantin Belousov 	if (writecounted)
1568fe7bcbafSKyle Evans 		docow |= MAP_WRITECOUNT;
15694648ba0aSKonstantin Belousov 	if (flags & MAP_STACK) {
15704648ba0aSKonstantin Belousov 		if (object != NULL)
15714648ba0aSKonstantin Belousov 			return (EINVAL);
15724648ba0aSKonstantin Belousov 		docow |= MAP_STACK_GROWS_DOWN;
15734648ba0aSKonstantin Belousov 	}
157411c42bccSKonstantin Belousov 	if ((flags & MAP_EXCL) != 0)
157511c42bccSKonstantin Belousov 		docow |= MAP_CHECK_EXCL;
157619bd0d9cSKonstantin Belousov 	if ((flags & MAP_GUARD) != 0)
157719bd0d9cSKonstantin Belousov 		docow |= MAP_CREATE_GUARD;
15785850152dSJohn Dyson 
15794648ba0aSKonstantin Belousov 	if (fitit) {
15805aa60b6fSJohn Baldwin 		if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
15815aa60b6fSJohn Baldwin 			findspace = VMFS_SUPER_SPACE;
15825aa60b6fSJohn Baldwin 		else if ((flags & MAP_ALIGNMENT_MASK) != 0)
15835aa60b6fSJohn Baldwin 			findspace = VMFS_ALIGNED_SPACE(flags >>
15845aa60b6fSJohn Baldwin 			    MAP_ALIGNMENT_SHIFT);
15852267af78SJulian Elischer 		else
15865aa60b6fSJohn Baldwin 			findspace = VMFS_OPTIMAL_SPACE;
15876a97a3f7SKonstantin Belousov 		max_addr = 0;
15886a97a3f7SKonstantin Belousov 		if ((flags & MAP_32BIT) != 0)
15896a97a3f7SKonstantin Belousov 			max_addr = MAP_32BIT_MAX_ADDR;
15906a97a3f7SKonstantin Belousov 		if (curmap) {
159137e5d49eSAlan Cox 			default_addr =
15926a97a3f7SKonstantin Belousov 			    round_page((vm_offset_t)td->td_proc->p_vmspace->
159337e5d49eSAlan Cox 			    vm_daddr + lim_max(td, RLIMIT_DATA));
159437e5d49eSAlan Cox 			if ((flags & MAP_32BIT) != 0)
159537e5d49eSAlan Cox 				default_addr = 0;
159637e5d49eSAlan Cox 			rv = vm_map_find_min(map, object, foff, addr, size,
159737e5d49eSAlan Cox 			    default_addr, max_addr, findspace, prot, maxprot,
159837e5d49eSAlan Cox 			    docow);
15996a97a3f7SKonstantin Belousov 		} else {
16006a97a3f7SKonstantin Belousov 			rv = vm_map_find(map, object, foff, addr, size,
16016a97a3f7SKonstantin Belousov 			    max_addr, findspace, prot, maxprot, docow);
16026a97a3f7SKonstantin Belousov 		}
16034648ba0aSKonstantin Belousov 	} else {
1604b8ca4ef2SAlan Cox 		rv = vm_map_fixed(map, object, foff, *addr, size,
1605bd7e5f99SJohn Dyson 		    prot, maxprot, docow);
16064648ba0aSKonstantin Belousov 	}
1607bd7e5f99SJohn Dyson 
1608f9230ad6SAlan Cox 	if (rv == KERN_SUCCESS) {
16097fb0c17eSDavid Greenman 		/*
1610f9230ad6SAlan Cox 		 * If the process has requested that all future mappings
1611f9230ad6SAlan Cox 		 * be wired, then heed this.
1612f9230ad6SAlan Cox 		 */
161354a3a114SMark Johnston 		if ((map->flags & MAP_WIREFUTURE) != 0) {
161454a3a114SMark Johnston 			vm_map_lock(map);
161554a3a114SMark Johnston 			if ((map->flags & MAP_WIREFUTURE) != 0)
16168cd6a80dSMark Johnston 				(void)vm_map_wire_locked(map, *addr,
161754a3a114SMark Johnston 				    *addr + size, VM_MAP_WIRE_USER |
161854a3a114SMark Johnston 				    ((flags & MAP_STACK) ? VM_MAP_WIRE_HOLESOK :
161954a3a114SMark Johnston 				    VM_MAP_WIRE_NOHOLES));
162054a3a114SMark Johnston 			vm_map_unlock(map);
16211472f4f4SKonstantin Belousov 		}
1622df8bae1dSRodney W. Grimes 	}
16232e32165cSKonstantin Belousov 	return (vm_mmap_to_errno(rv));
16242e32165cSKonstantin Belousov }
16252e32165cSKonstantin Belousov 
1626f9230ad6SAlan Cox /*
1627f9230ad6SAlan Cox  * Translate a Mach VM return code to zero on success or the appropriate errno
1628f9230ad6SAlan Cox  * on failure.
1629f9230ad6SAlan Cox  */
16302e32165cSKonstantin Belousov int
vm_mmap_to_errno(int rv)16312e32165cSKonstantin Belousov vm_mmap_to_errno(int rv)
16322e32165cSKonstantin Belousov {
16332e32165cSKonstantin Belousov 
1634df8bae1dSRodney W. Grimes 	switch (rv) {
1635df8bae1dSRodney W. Grimes 	case KERN_SUCCESS:
1636df8bae1dSRodney W. Grimes 		return (0);
1637df8bae1dSRodney W. Grimes 	case KERN_INVALID_ADDRESS:
1638df8bae1dSRodney W. Grimes 	case KERN_NO_SPACE:
1639df8bae1dSRodney W. Grimes 		return (ENOMEM);
1640df8bae1dSRodney W. Grimes 	case KERN_PROTECTION_FAILURE:
1641df8bae1dSRodney W. Grimes 		return (EACCES);
1642df8bae1dSRodney W. Grimes 	default:
1643df8bae1dSRodney W. Grimes 		return (EINVAL);
1644df8bae1dSRodney W. Grimes 	}
1645df8bae1dSRodney W. Grimes }
1646