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 #if CONFIG_MAP_RANGES
80 /*
81 * This has been tuned for iOS only
82 */
83 #define VM_MAP_USER_RANGE_MAX (GiB(12))
84 #endif
85
86 /* Initialize the module */
87 extern void vm_map_init(void);
88
89 extern kern_return_t vm_map_locate_space(
90 vm_map_t map,
91 vm_map_size_t size,
92 vm_map_offset_t mask,
93 vm_map_kernel_flags_t vmk_flags,
94 vm_map_offset_t *start_inout,
95 vm_map_entry_t *entry_out);
96
97 /* Allocate a range in the specified virtual address map and
98 * return the entry allocated for that range. */
99 extern kern_return_t vm_map_find_space(
100 vm_map_t map,
101 vm_map_address_t hint_addr,
102 vm_map_size_t size,
103 vm_map_offset_t mask,
104 vm_map_kernel_flags_t vmk_flags,
105 vm_map_entry_t *o_entry); /* OUT */
106
107 extern void vm_map_clip_start(
108 vm_map_t map,
109 vm_map_entry_t entry,
110 vm_map_offset_t endaddr);
111
112 extern void vm_map_clip_end(
113 vm_map_t map,
114 vm_map_entry_t entry,
115 vm_map_offset_t endaddr);
116
117 extern boolean_t vm_map_entry_should_cow_for_true_share(
118 vm_map_entry_t entry);
119
120 /*!
121 * @typedef vmr_flags_t
122 *
123 * @brief
124 * Flags for vm_map_remove() and vm_map_delete()
125 *
126 * @const VM_MAP_REMOVE_NO_FLAGS
127 * When no special flags is to be passed.
128 *
129 * @const VM_MAP_REMOVE_KUNWIRE
130 * Unwire memory as a side effect.
131 *
132 * @const VM_MAP_REMOVE_INTERRUPTIBLE
133 * Whether the call is interruptible if it needs to wait for a vm map
134 * entry to quiesce (interruption leads to KERN_ABORTED).
135 *
136 * @const VM_MAP_REMOVE_IMMUTABLE
137 * Allow permanent entries to be removed.
138 *
139 * @const VM_MAP_REMOVE_GAPS_FAIL
140 * Return KERN_INVALID_VALUE when a gap is being removed instead of panicking.
141 *
142 * @const VM_MAP_REMOVE_NO_YIELD.
143 * Try to avoid yielding during this call.
144 *
145 * @const VM_MAP_REMOVE_GUESS_SIZE
146 * The caller doesn't know the precise size of the entry,
147 * but the address must match an atomic entry.
148 *
149 * @const VM_MAP_REMOVE_IMMUTABLE_CODE
150 * Allow executables entries to be removed (for VM_PROT_COPY),
151 * which is used by debuggers.
152 */
153 __options_decl(vmr_flags_t, uint32_t, {
154 VM_MAP_REMOVE_NO_FLAGS = 0x000,
155 VM_MAP_REMOVE_KUNWIRE = 0x001,
156 VM_MAP_REMOVE_INTERRUPTIBLE = 0x002,
157 // unused = 0x004,
158 VM_MAP_REMOVE_NO_MAP_ALIGN = 0x008,
159 VM_MAP_REMOVE_IMMUTABLE = 0x010,
160 VM_MAP_REMOVE_GAPS_FAIL = 0x020,
161 VM_MAP_REMOVE_NO_YIELD = 0x040,
162 VM_MAP_REMOVE_GUESS_SIZE = 0x080,
163 VM_MAP_REMOVE_IMMUTABLE_CODE = 0x100,
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 #pragma GCC visibility pop
195 __END_DECLS
196
197 #endif /* _VM_VM_MAP_INTERNAL_H_ */
198