xref: /freebsd-src/sys/vm/vm_extern.h (revision 235750ee5130aa71cf4151457435e806dd58f2ec)
1df8bae1dSRodney W. Grimes /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1992, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
321365ed99SBruce Evans #ifndef _VM_EXTERN_H_
331365ed99SBruce Evans #define	_VM_EXTERN_H_
343301cc3cSPaul Richards 
3574d1d2b7SNeel Natu struct pmap;
36df8bae1dSRodney W. Grimes struct proc;
37df8bae1dSRodney W. Grimes struct vmspace;
38df8bae1dSRodney W. Grimes struct vnode;
395df87b21SJeff Roberson struct vmem;
40df8bae1dSRodney W. Grimes 
41c4473420SPeter Wemm #ifdef _KERNEL
42d950c589SKonstantin Belousov #include <sys/kassert.h>
4329d481aeSJohn Baldwin 
447077c426SJohn Baldwin struct cdev;
457077c426SJohn Baldwin struct cdevsw;
469978bd99SMark Johnston struct domainset;
47b5e8ce9fSBruce Evans 
485df87b21SJeff Roberson /* These operate on kernel virtual addresses only. */
495df87b21SJeff Roberson vm_offset_t kva_alloc(vm_size_t);
50839999e7SAndrew Turner vm_offset_t kva_alloc_aligned(vm_size_t, vm_size_t);
515df87b21SJeff Roberson void kva_free(vm_offset_t, vm_size_t);
525df87b21SJeff Roberson 
535df87b21SJeff Roberson /* These operate on pageable virtual addresses. */
545df87b21SJeff Roberson vm_offset_t kmap_alloc_wait(vm_map_t, vm_size_t);
555df87b21SJeff Roberson void kmap_free_wakeup(vm_map_t, vm_offset_t, vm_size_t);
565df87b21SJeff Roberson 
575df87b21SJeff Roberson /* These operate on virtual addresses backed by memory. */
58f49fd63aSJohn Baldwin void *kmem_alloc_attr(vm_size_t size, int flags,
598ef9d880SAlan Cox     vm_paddr_t low, vm_paddr_t high, vm_memattr_t memattr);
60f49fd63aSJohn Baldwin void *kmem_alloc_attr_domainset(struct domainset *ds, vm_size_t size,
619978bd99SMark Johnston     int flags, vm_paddr_t low, vm_paddr_t high, vm_memattr_t memattr);
62f49fd63aSJohn Baldwin void *kmem_alloc_contig(vm_size_t size, int flags,
635c1f2cc4SAlan Cox     vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
641933a67cSAlan Cox     vm_memattr_t memattr);
65f49fd63aSJohn Baldwin void *kmem_alloc_contig_domainset(struct domainset *ds, vm_size_t size,
669978bd99SMark Johnston     int flags, vm_paddr_t low, vm_paddr_t high, u_long alignment,
679978bd99SMark Johnston     vm_paddr_t boundary, vm_memattr_t memattr);
68f49fd63aSJohn Baldwin void *kmem_malloc(vm_size_t size, int flags);
69f49fd63aSJohn Baldwin void *kmem_malloc_domainset(struct domainset *ds, vm_size_t size,
709978bd99SMark Johnston     int flags);
71f49fd63aSJohn Baldwin void kmem_free(void *addr, vm_size_t size);
725df87b21SJeff Roberson 
735df87b21SJeff Roberson /* This provides memory for previously allocated address space. */
745df87b21SJeff Roberson int kmem_back(vm_object_t, vm_offset_t, vm_size_t, int);
757a469c8eSJeff Roberson int kmem_back_domain(int, vm_object_t, vm_offset_t, vm_size_t, int);
765df87b21SJeff Roberson void kmem_unback(vm_object_t, vm_offset_t, vm_size_t);
775df87b21SJeff Roberson 
785df87b21SJeff Roberson /* Bootstrapping. */
79483f692eSMark Johnston void kmem_bootstrap_free(vm_offset_t, vm_size_t);
807dd979dfSMark Johnston void kmem_subinit(vm_map_t, vm_map_t, vm_offset_t *, vm_offset_t *, vm_size_t,
817dd979dfSMark Johnston     bool);
825df87b21SJeff Roberson void kmem_init(vm_offset_t, vm_offset_t);
835df87b21SJeff Roberson void kmem_init_zero_region(void);
845df87b21SJeff Roberson void kmeminit(void);
855df87b21SJeff Roberson 
86*9e016408SJohn Baldwin bool kernacc(void *, int, int);
87*9e016408SJohn Baldwin bool useracc(void *, int, int);
88df08823dSKonstantin Belousov int vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
89df08823dSKonstantin Belousov     int fault_flags, vm_page_t *m_hold);
90121fd461SKonstantin Belousov void vm_fault_copy_entry(vm_map_t, vm_map_t, vm_map_entry_t, vm_map_entry_t,
91121fd461SKonstantin Belousov     vm_ooffset_t *);
922801687dSKonstantin Belousov int vm_fault_disable_pagefaults(void);
932801687dSKonstantin Belousov void vm_fault_enable_pagefaults(int save);
9482de724fSAlan Cox int vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
9582de724fSAlan Cox     vm_prot_t prot, vm_page_t *ma, int max_count);
96df08823dSKonstantin Belousov int vm_fault_trap(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
97df08823dSKonstantin Belousov     int fault_flags, int *signo, int *ucode);
987077c426SJohn Baldwin int vm_forkproc(struct thread *, struct proc *, struct thread *,
997077c426SJohn Baldwin     struct vmspace *, int);
10011caded3SAlfred Perlstein void vm_waitproc(struct proc *);
1017077c426SJohn Baldwin int vm_mmap(vm_map_t, vm_offset_t *, vm_size_t, vm_prot_t, vm_prot_t, int,
1027077c426SJohn Baldwin     objtype_t, void *, vm_ooffset_t);
1037077c426SJohn Baldwin int vm_mmap_object(vm_map_t, vm_offset_t *, vm_size_t, vm_prot_t,
1047077c426SJohn Baldwin     vm_prot_t, int, vm_object_t, vm_ooffset_t, boolean_t, struct thread *);
1052e32165cSKonstantin Belousov int vm_mmap_to_errno(int rv);
1067077c426SJohn Baldwin int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
1077077c426SJohn Baldwin     int *, struct cdev *, struct cdevsw *, vm_ooffset_t *, vm_object_t *);
1087077c426SJohn Baldwin int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *, int *,
1097077c426SJohn Baldwin     struct vnode *, vm_ooffset_t *, vm_object_t *, boolean_t *);
11011caded3SAlfred Perlstein void vm_set_page_size(void);
1111a4fcaebSMarcel Moolenaar void vm_sync_icache(vm_map_t, vm_offset_t, vm_size_t);
11274d1d2b7SNeel Natu typedef int (*pmap_pinit_t)(struct pmap *pmap);
11374d1d2b7SNeel Natu struct vmspace *vmspace_alloc(vm_offset_t, vm_offset_t, pmap_pinit_t);
1143364c323SKonstantin Belousov struct vmspace *vmspace_fork(struct vmspace *, vm_ooffset_t *);
11589b57fcfSKonstantin Belousov int vmspace_exec(struct proc *, vm_offset_t, vm_offset_t);
11689b57fcfSKonstantin Belousov int vmspace_unshare(struct proc *);
11757051fdcSTor Egge void vmspace_exit(struct thread *);
11857051fdcSTor Egge struct vmspace *vmspace_acquire_ref(struct proc *);
11911caded3SAlfred Perlstein void vmspace_free(struct vmspace *);
12011caded3SAlfred Perlstein void vmspace_exitfree(struct proc *);
1218a4dc40fSJohn Baldwin void vmspace_switch_aio(struct vmspace *);
12211caded3SAlfred Perlstein void vnode_pager_setsize(struct vnode *, vm_ooffset_t);
123de2e1529SKa Ho Ng void vnode_pager_purge_range(struct vnode *, vm_ooffset_t, vm_ooffset_t);
124ce8660e3SDon Lewis int vslock(void *, size_t);
125ce8660e3SDon Lewis void vsunlock(void *, size_t);
126da61b9a6SAlan Cox struct sf_buf *vm_imgact_map_page(vm_object_t object, vm_ooffset_t offset);
127be996836SAttilio Rao void vm_imgact_unmap_page(struct sf_buf *sf);
12849a2507bSAlan Cox void vm_thread_dispose(struct thread *td);
12989b57fcfSKonstantin Belousov int vm_thread_new(struct thread *td, int pages);
130e2068d0bSJeff Roberson u_int vm_active_count(void);
131e2068d0bSJeff Roberson u_int vm_inactive_count(void);
132e2068d0bSJeff Roberson u_int vm_laundry_count(void);
133e2068d0bSJeff Roberson u_int vm_wait_count(void);
134c606ab59SDoug Moore 
135c606ab59SDoug Moore /*
136c606ab59SDoug Moore  * Is pa a multiple of alignment, which is a power-of-two?
137c606ab59SDoug Moore  */
138c606ab59SDoug Moore static inline bool
139c606ab59SDoug Moore vm_addr_align_ok(vm_paddr_t pa, u_long alignment)
140c606ab59SDoug Moore {
14129d481aeSJohn Baldwin 	KASSERT(powerof2(alignment), ("%s: alignment is not a power of 2: %#lx",
14229d481aeSJohn Baldwin 	    __func__, alignment));
143c606ab59SDoug Moore 	return ((pa & (alignment - 1)) == 0);
144c606ab59SDoug Moore }
145c606ab59SDoug Moore 
146c606ab59SDoug Moore /*
147c606ab59SDoug Moore  * Do the first and last addresses of a range match in all bits except the ones
148c606ab59SDoug Moore  * in -boundary (a power-of-two)?  For boundary == 0, all addresses match.
149c606ab59SDoug Moore  */
150c606ab59SDoug Moore static inline bool
151c606ab59SDoug Moore vm_addr_bound_ok(vm_paddr_t pa, vm_paddr_t size, vm_paddr_t boundary)
152c606ab59SDoug Moore {
15329d481aeSJohn Baldwin 	KASSERT(powerof2(boundary), ("%s: boundary is not a power of 2: %#jx",
15429d481aeSJohn Baldwin 	    __func__, (uintmax_t)boundary));
155c606ab59SDoug Moore 	return (((pa ^ (pa + size - 1)) & -boundary) == 0);
156c606ab59SDoug Moore }
157c606ab59SDoug Moore 
158c606ab59SDoug Moore static inline bool
159c606ab59SDoug Moore vm_addr_ok(vm_paddr_t pa, vm_paddr_t size, u_long alignment,
160c606ab59SDoug Moore     vm_paddr_t boundary)
161c606ab59SDoug Moore {
162c606ab59SDoug Moore 	return (vm_addr_align_ok(pa, alignment) &&
163c606ab59SDoug Moore 	    vm_addr_bound_ok(pa, size, boundary));
164c606ab59SDoug Moore }
165c4473420SPeter Wemm #endif				/* _KERNEL */
1661365ed99SBruce Evans #endif				/* !_VM_EXTERN_H_ */
167