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