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 __arm64__
80 // <rdar://problem/48304934> arm64 doesn't use ldp when I'd expect it to
81 #define kmem_range_load(r, rmin, rmax) \
82 asm("ldp %[rmin], %[rmax], [%[range]]" \
83 : [rmin] "=r"(rmin), [rmax] "=r"(rmax) \
84 : [range] "r"(r), "m"((r)->min_address), "m"((r)->max_address))
85 #else
86 #define kmem_range_load(r, rmin, rmax) \
87 ({ rmin = (r)->min_address; rmax = (r)->max_address; })
88 #endif
89
90 /* Initialize the module */
91 extern void vm_map_init(void);
92
93 extern kern_return_t vm_map_locate_space(
94 vm_map_t map,
95 vm_map_size_t size,
96 vm_map_offset_t mask,
97 vm_map_kernel_flags_t vmk_flags,
98 vm_map_offset_t *start_inout,
99 vm_map_entry_t *entry_out);
100
101 /* Allocate a range in the specified virtual address map and
102 * return the entry allocated for that range. */
103 extern kern_return_t vm_map_find_space(
104 vm_map_t map,
105 vm_map_address_t hint_addr,
106 vm_map_size_t size,
107 vm_map_offset_t mask,
108 vm_map_kernel_flags_t vmk_flags,
109 vm_map_entry_t *o_entry); /* OUT */
110
111 extern void vm_map_clip_start(
112 vm_map_t map,
113 vm_map_entry_t entry,
114 vm_map_offset_t endaddr);
115
116 extern void vm_map_clip_end(
117 vm_map_t map,
118 vm_map_entry_t entry,
119 vm_map_offset_t endaddr);
120
121 extern boolean_t vm_map_entry_should_cow_for_true_share(
122 vm_map_entry_t entry);
123
124 /*!
125 * @typedef vmr_flags_t
126 *
127 * @brief
128 * Flags for vm_map_remove() and vm_map_delete()
129 *
130 * @const VM_MAP_REMOVE_NO_FLAGS
131 * When no special flags is to be passed.
132 *
133 * @const VM_MAP_REMOVE_KUNWIRE
134 * Unwire memory as a side effect.
135 *
136 * @const VM_MAP_REMOVE_INTERRUPTIBLE
137 * Whether the call is interruptible if it needs to wait for a vm map
138 * entry to quiesce (interruption leads to KERN_ABORTED).
139 *
140 * @const VM_MAP_REMOVE_RETURN_ERRORS
141 * The caller will handle errors that aren't explicitly requested
142 * by the caller (@c VM_MAP_REMOVE_INTERRUPTIBLE
143 * and @c VM_MAP_REMOVE_GAPS_FAIL).
144 *
145 * @const VM_MAP_REMOVE_IMMUTABLE
146 * Allow permanent entries to be removed.
147 *
148 * @const VM_MAP_REMOVE_GAPS_FAIL
149 * Return KERN_INVALID_VALUE when a gap is being removed instead of panicking.
150 *
151 * @const VM_MAP_REMOVE_NO_YIELD.
152 * Try to avoid yielding during this call.
153 */
154 __options_decl(vmr_flags_t, uint32_t, {
155 VM_MAP_REMOVE_NO_FLAGS = 0x00,
156 VM_MAP_REMOVE_KUNWIRE = 0x01,
157 VM_MAP_REMOVE_INTERRUPTIBLE = 0x02,
158 VM_MAP_REMOVE_RETURN_ERRORS = 0x04,
159 VM_MAP_REMOVE_NO_MAP_ALIGN = 0x08,
160 VM_MAP_REMOVE_IMMUTABLE = 0x10,
161 VM_MAP_REMOVE_GAPS_FAIL = 0x20,
162 VM_MAP_REMOVE_NO_YIELD = 0x40,
163 });
164
165 /* Deallocate a region */
166 extern kern_return_t vm_map_remove_flags(
167 vm_map_t map,
168 vm_map_offset_t start,
169 vm_map_offset_t end,
170 vmr_flags_t flags) __result_use_check;
171
172 static inline void
vm_map_remove(vm_map_t map,vm_map_offset_t start,vm_map_offset_t end)173 vm_map_remove(
174 vm_map_t map,
175 vm_map_offset_t start,
176 vm_map_offset_t end)
177 {
178 (void)vm_map_remove_flags(map, start, end, VM_MAP_REMOVE_NO_FLAGS);
179 }
180
181 /* Deallocate a region when the map is already locked */
182 extern kern_return_t vm_map_remove_and_unlock(
183 vm_map_t map,
184 vm_map_offset_t start,
185 vm_map_offset_t end,
186 vmr_flags_t flags);
187
188 #pragma GCC visibility pop
189 __END_DECLS
190
191 #endif /* _VM_VM_MAP_INTERNAL_H_ */
192