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