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