xref: /xnu-8792.41.9/osfmk/vm/vm_map_internal.h (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions  * Copyright (c) 2021 Apple Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions  *
4*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions  *
6*5c2921b0SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5c2921b0SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5c2921b0SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5c2921b0SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5c2921b0SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5c2921b0SApple OSS Distributions  *
15*5c2921b0SApple OSS Distributions  * Please obtain a copy of the License at
16*5c2921b0SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5c2921b0SApple OSS Distributions  *
18*5c2921b0SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5c2921b0SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5c2921b0SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5c2921b0SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5c2921b0SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5c2921b0SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5c2921b0SApple OSS Distributions  * limitations under the License.
25*5c2921b0SApple OSS Distributions  *
26*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5c2921b0SApple OSS Distributions  */
28*5c2921b0SApple OSS Distributions /*
29*5c2921b0SApple OSS Distributions  * @OSF_COPYRIGHT@
30*5c2921b0SApple OSS Distributions  */
31*5c2921b0SApple OSS Distributions /*
32*5c2921b0SApple OSS Distributions  * Mach Operating System
33*5c2921b0SApple OSS Distributions  * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34*5c2921b0SApple OSS Distributions  * All Rights Reserved.
35*5c2921b0SApple OSS Distributions  *
36*5c2921b0SApple OSS Distributions  * Permission to use, copy, modify and distribute this software and its
37*5c2921b0SApple OSS Distributions  * documentation is hereby granted, provided that both the copyright
38*5c2921b0SApple OSS Distributions  * notice and this permission notice appear in all copies of the
39*5c2921b0SApple OSS Distributions  * software, derivative works or modified versions, and any portions
40*5c2921b0SApple OSS Distributions  * thereof, and that both notices appear in supporting documentation.
41*5c2921b0SApple OSS Distributions  *
42*5c2921b0SApple OSS Distributions  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43*5c2921b0SApple OSS Distributions  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44*5c2921b0SApple OSS Distributions  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45*5c2921b0SApple OSS Distributions  *
46*5c2921b0SApple OSS Distributions  * Carnegie Mellon requests users of this software to return to
47*5c2921b0SApple OSS Distributions  *
48*5c2921b0SApple OSS Distributions  *  Software Distribution Coordinator  or  [email protected]
49*5c2921b0SApple OSS Distributions  *  School of Computer Science
50*5c2921b0SApple OSS Distributions  *  Carnegie Mellon University
51*5c2921b0SApple OSS Distributions  *  Pittsburgh PA 15213-3890
52*5c2921b0SApple OSS Distributions  *
53*5c2921b0SApple OSS Distributions  * any improvements or extensions that they make and grant Carnegie Mellon
54*5c2921b0SApple OSS Distributions  * the rights to redistribute these changes.
55*5c2921b0SApple OSS Distributions  */
56*5c2921b0SApple OSS Distributions 
57*5c2921b0SApple OSS Distributions #ifndef _VM_VM_MAP_INTERNAL_H_
58*5c2921b0SApple OSS Distributions #define _VM_VM_MAP_INTERNAL_H_
59*5c2921b0SApple OSS Distributions 
60*5c2921b0SApple OSS Distributions #include <vm/vm_map.h>
61*5c2921b0SApple OSS Distributions #include <vm/vm_kern.h>
62*5c2921b0SApple OSS Distributions 
63*5c2921b0SApple OSS Distributions __BEGIN_DECLS
64*5c2921b0SApple OSS Distributions #pragma GCC visibility push(hidden)
65*5c2921b0SApple OSS Distributions 
66*5c2921b0SApple OSS Distributions /*
67*5c2921b0SApple OSS Distributions  * This file contains interfaces that are private to the VM
68*5c2921b0SApple OSS Distributions  */
69*5c2921b0SApple OSS Distributions 
70*5c2921b0SApple OSS Distributions #define KiB(x) (1024 * (x))
71*5c2921b0SApple OSS Distributions #define MeB(x) (1024 * 1024 * (x))
72*5c2921b0SApple OSS Distributions 
73*5c2921b0SApple OSS Distributions #if __LP64__
74*5c2921b0SApple OSS Distributions #define KMEM_SMALLMAP_THRESHOLD     (MeB(1))
75*5c2921b0SApple OSS Distributions #else
76*5c2921b0SApple OSS Distributions #define KMEM_SMALLMAP_THRESHOLD     (KiB(256))
77*5c2921b0SApple OSS Distributions #endif
78*5c2921b0SApple OSS Distributions 
79*5c2921b0SApple OSS Distributions #if CONFIG_MAP_RANGES
80*5c2921b0SApple OSS Distributions /*
81*5c2921b0SApple OSS Distributions  * This has been tuned for iOS only
82*5c2921b0SApple OSS Distributions  */
83*5c2921b0SApple OSS Distributions #define VM_MAP_USER_RANGE_MAX       (GiB(12))
84*5c2921b0SApple OSS Distributions #endif
85*5c2921b0SApple OSS Distributions 
86*5c2921b0SApple OSS Distributions /* Initialize the module */
87*5c2921b0SApple OSS Distributions extern void vm_map_init(void);
88*5c2921b0SApple OSS Distributions 
89*5c2921b0SApple OSS Distributions extern kern_return_t vm_map_locate_space(
90*5c2921b0SApple OSS Distributions 	vm_map_t                map,
91*5c2921b0SApple OSS Distributions 	vm_map_size_t           size,
92*5c2921b0SApple OSS Distributions 	vm_map_offset_t         mask,
93*5c2921b0SApple OSS Distributions 	vm_map_kernel_flags_t   vmk_flags,
94*5c2921b0SApple OSS Distributions 	vm_map_offset_t        *start_inout,
95*5c2921b0SApple OSS Distributions 	vm_map_entry_t         *entry_out);
96*5c2921b0SApple OSS Distributions 
97*5c2921b0SApple OSS Distributions /* Allocate a range in the specified virtual address map and
98*5c2921b0SApple OSS Distributions  * return the entry allocated for that range. */
99*5c2921b0SApple OSS Distributions extern kern_return_t vm_map_find_space(
100*5c2921b0SApple OSS Distributions 	vm_map_t                map,
101*5c2921b0SApple OSS Distributions 	vm_map_address_t        hint_addr,
102*5c2921b0SApple OSS Distributions 	vm_map_size_t           size,
103*5c2921b0SApple OSS Distributions 	vm_map_offset_t         mask,
104*5c2921b0SApple OSS Distributions 	vm_map_kernel_flags_t   vmk_flags,
105*5c2921b0SApple OSS Distributions 	vm_map_entry_t          *o_entry);                              /* OUT */
106*5c2921b0SApple OSS Distributions 
107*5c2921b0SApple OSS Distributions extern void vm_map_clip_start(
108*5c2921b0SApple OSS Distributions 	vm_map_t                map,
109*5c2921b0SApple OSS Distributions 	vm_map_entry_t          entry,
110*5c2921b0SApple OSS Distributions 	vm_map_offset_t         endaddr);
111*5c2921b0SApple OSS Distributions 
112*5c2921b0SApple OSS Distributions extern void vm_map_clip_end(
113*5c2921b0SApple OSS Distributions 	vm_map_t                map,
114*5c2921b0SApple OSS Distributions 	vm_map_entry_t          entry,
115*5c2921b0SApple OSS Distributions 	vm_map_offset_t         endaddr);
116*5c2921b0SApple OSS Distributions 
117*5c2921b0SApple OSS Distributions extern boolean_t vm_map_entry_should_cow_for_true_share(
118*5c2921b0SApple OSS Distributions 	vm_map_entry_t          entry);
119*5c2921b0SApple OSS Distributions 
120*5c2921b0SApple OSS Distributions /*!
121*5c2921b0SApple OSS Distributions  * @typedef vmr_flags_t
122*5c2921b0SApple OSS Distributions  *
123*5c2921b0SApple OSS Distributions  * @brief
124*5c2921b0SApple OSS Distributions  * Flags for vm_map_remove() and vm_map_delete()
125*5c2921b0SApple OSS Distributions  *
126*5c2921b0SApple OSS Distributions  * @const VM_MAP_REMOVE_NO_FLAGS
127*5c2921b0SApple OSS Distributions  * When no special flags is to be passed.
128*5c2921b0SApple OSS Distributions  *
129*5c2921b0SApple OSS Distributions  * @const VM_MAP_REMOVE_KUNWIRE
130*5c2921b0SApple OSS Distributions  * Unwire memory as a side effect.
131*5c2921b0SApple OSS Distributions  *
132*5c2921b0SApple OSS Distributions  * @const VM_MAP_REMOVE_INTERRUPTIBLE
133*5c2921b0SApple OSS Distributions  * Whether the call is interruptible if it needs to wait for a vm map
134*5c2921b0SApple OSS Distributions  * entry to quiesce (interruption leads to KERN_ABORTED).
135*5c2921b0SApple OSS Distributions  *
136*5c2921b0SApple OSS Distributions  * @const VM_MAP_REMOVE_IMMUTABLE
137*5c2921b0SApple OSS Distributions  * Allow permanent entries to be removed.
138*5c2921b0SApple OSS Distributions  *
139*5c2921b0SApple OSS Distributions  * @const VM_MAP_REMOVE_GAPS_FAIL
140*5c2921b0SApple OSS Distributions  * Return KERN_INVALID_VALUE when a gap is being removed instead of panicking.
141*5c2921b0SApple OSS Distributions  *
142*5c2921b0SApple OSS Distributions  * @const VM_MAP_REMOVE_NO_YIELD.
143*5c2921b0SApple OSS Distributions  * Try to avoid yielding during this call.
144*5c2921b0SApple OSS Distributions  *
145*5c2921b0SApple OSS Distributions  * @const VM_MAP_REMOVE_GUESS_SIZE
146*5c2921b0SApple OSS Distributions  * The caller doesn't know the precise size of the entry,
147*5c2921b0SApple OSS Distributions  * but the address must match an atomic entry.
148*5c2921b0SApple OSS Distributions  *
149*5c2921b0SApple OSS Distributions  * @const VM_MAP_REMOVE_IMMUTABLE_CODE
150*5c2921b0SApple OSS Distributions  * Allow executables entries to be removed (for VM_PROT_COPY),
151*5c2921b0SApple OSS Distributions  * which is used by debuggers.
152*5c2921b0SApple OSS Distributions  */
153*5c2921b0SApple OSS Distributions __options_decl(vmr_flags_t, uint32_t, {
154*5c2921b0SApple OSS Distributions 	VM_MAP_REMOVE_NO_FLAGS          = 0x000,
155*5c2921b0SApple OSS Distributions 	VM_MAP_REMOVE_KUNWIRE           = 0x001,
156*5c2921b0SApple OSS Distributions 	VM_MAP_REMOVE_INTERRUPTIBLE     = 0x002,
157*5c2921b0SApple OSS Distributions 	// unused                       = 0x004,
158*5c2921b0SApple OSS Distributions 	VM_MAP_REMOVE_NO_MAP_ALIGN      = 0x008,
159*5c2921b0SApple OSS Distributions 	VM_MAP_REMOVE_IMMUTABLE         = 0x010,
160*5c2921b0SApple OSS Distributions 	VM_MAP_REMOVE_GAPS_FAIL         = 0x020,
161*5c2921b0SApple OSS Distributions 	VM_MAP_REMOVE_NO_YIELD          = 0x040,
162*5c2921b0SApple OSS Distributions 	VM_MAP_REMOVE_GUESS_SIZE        = 0x080,
163*5c2921b0SApple OSS Distributions 	VM_MAP_REMOVE_IMMUTABLE_CODE    = 0x100,
164*5c2921b0SApple OSS Distributions });
165*5c2921b0SApple OSS Distributions 
166*5c2921b0SApple OSS Distributions /* Deallocate a region */
167*5c2921b0SApple OSS Distributions extern kmem_return_t vm_map_remove_guard(
168*5c2921b0SApple OSS Distributions 	vm_map_t                map,
169*5c2921b0SApple OSS Distributions 	vm_map_offset_t         start,
170*5c2921b0SApple OSS Distributions 	vm_map_offset_t         end,
171*5c2921b0SApple OSS Distributions 	vmr_flags_t             flags,
172*5c2921b0SApple OSS Distributions 	kmem_guard_t            guard) __result_use_check;
173*5c2921b0SApple OSS Distributions 
174*5c2921b0SApple OSS Distributions extern kmem_return_t vm_map_remove_and_unlock(
175*5c2921b0SApple OSS Distributions 	vm_map_t        map,
176*5c2921b0SApple OSS Distributions 	vm_map_offset_t start,
177*5c2921b0SApple OSS Distributions 	vm_map_offset_t end,
178*5c2921b0SApple OSS Distributions 	vmr_flags_t     flags,
179*5c2921b0SApple OSS Distributions 	kmem_guard_t    guard) __result_use_check;
180*5c2921b0SApple OSS Distributions 
181*5c2921b0SApple OSS Distributions /* Deallocate a region */
182*5c2921b0SApple OSS Distributions static inline void
vm_map_remove(vm_map_t map,vm_map_offset_t start,vm_map_offset_t end)183*5c2921b0SApple OSS Distributions vm_map_remove(
184*5c2921b0SApple OSS Distributions 	vm_map_t                map,
185*5c2921b0SApple OSS Distributions 	vm_map_offset_t         start,
186*5c2921b0SApple OSS Distributions 	vm_map_offset_t         end)
187*5c2921b0SApple OSS Distributions {
188*5c2921b0SApple OSS Distributions 	vmr_flags_t  flags = VM_MAP_REMOVE_NO_FLAGS;
189*5c2921b0SApple OSS Distributions 	kmem_guard_t guard = KMEM_GUARD_NONE;
190*5c2921b0SApple OSS Distributions 
191*5c2921b0SApple OSS Distributions 	(void)vm_map_remove_guard(map, start, end, flags, guard);
192*5c2921b0SApple OSS Distributions }
193*5c2921b0SApple OSS Distributions 
194*5c2921b0SApple OSS Distributions #pragma GCC visibility pop
195*5c2921b0SApple OSS Distributions __END_DECLS
196*5c2921b0SApple OSS Distributions 
197*5c2921b0SApple OSS Distributions #endif  /* _VM_VM_MAP_INTERNAL_H_ */
198