1 /*
2 * Copyright (c) 2023 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 #ifndef _VM_VM_MAP_XNU_H_
30 #define _VM_VM_MAP_XNU_H_
31
32 #ifdef XNU_KERNEL_PRIVATE
33
34 #include <sys/cdefs.h>
35 #include <vm/vm_map.h>
36
37
38 __BEGIN_DECLS
39
40 extern void vm_map_reference(vm_map_t map);
41 extern vm_map_t current_map(void);
42
43 /* Setup reserved areas in a new VM map */
44 extern kern_return_t vm_map_exec(
45 vm_map_t new_map,
46 task_t task,
47 boolean_t is64bit,
48 void *fsroot,
49 cpu_type_t cpu,
50 cpu_subtype_t cpu_subtype,
51 boolean_t reslide,
52 boolean_t is_driverkit,
53 uint32_t rsr_version);
54
55
56
57 #ifdef MACH_KERNEL_PRIVATE
58
59 #define current_map_fast() (current_thread()->map)
60 #define current_map() (current_map_fast())
61
62 /*
63 * Types defined:
64 *
65 * vm_map_t the high-level address map data structure.
66 * vm_map_entry_t an entry in an address map.
67 * vm_map_version_t a timestamp of a map, for use with vm_map_lookup
68 * vm_map_copy_t represents memory copied from an address map,
69 * used for inter-map copy operations
70 */
71 typedef struct vm_map_entry *vm_map_entry_t;
72 #define VM_MAP_ENTRY_NULL ((vm_map_entry_t) NULL)
73
74
75 #define named_entry_lock_init(object) lck_mtx_init(&(object)->Lock, &vm_object_lck_grp, &vm_object_lck_attr)
76 #define named_entry_lock_destroy(object) lck_mtx_destroy(&(object)->Lock, &vm_object_lck_grp)
77 #define named_entry_lock(object) lck_mtx_lock(&(object)->Lock)
78 #define named_entry_unlock(object) lck_mtx_unlock(&(object)->Lock)
79
80 /*
81 * Type: vm_named_entry_t [internal use only]
82 *
83 * Description:
84 * Description of a mapping to a memory cache object.
85 *
86 * Implementation:
87 * While the handle to this object is used as a means to map
88 * and pass around the right to map regions backed by pagers
89 * of all sorts, the named_entry itself is only manipulated
90 * by the kernel. Named entries hold information on the
91 * right to map a region of a cached object. Namely,
92 * the target cache object, the beginning and ending of the
93 * region to be mapped, and the permissions, (read, write)
94 * with which it can be mapped.
95 *
96 */
97
98 struct vm_named_entry {
99 decl_lck_mtx_data(, Lock); /* Synchronization */
100 union {
101 vm_map_t map; /* map backing submap */
102 vm_map_copy_t copy; /* a VM map copy */
103 } backing;
104 vm_object_offset_t offset; /* offset into object */
105 vm_object_size_t size; /* size of region */
106 vm_object_offset_t data_offset; /* offset to first byte of data */
107 unsigned int /* Is backing.xxx : */
108 /* unsigned */ access:8, /* MAP_MEM_* */
109 /* vm_prot_t */ protection:4, /* access permissions */
110 /* boolean_t */ is_object:1, /* ... a VM object (wrapped in a VM map copy) */
111 /* boolean_t */ internal:1, /* ... an internal object */
112 /* boolean_t */ is_sub_map:1, /* ... a submap? */
113 /* boolean_t */ is_copy:1, /* ... a VM map copy */
114 /* boolean_t */ is_fully_owned:1; /* ... all objects are owned */
115 #if VM_NAMED_ENTRY_DEBUG
116 uint32_t named_entry_bt; /* btref_t */
117 #endif /* VM_NAMED_ENTRY_DEBUG */
118 };
119
120 /*
121 * Bit 3 of the protection and max_protection bitfields in a vm_map_entry
122 * does not correspond to bit 3 of a vm_prot_t, so these macros provide a means
123 * to convert between the "packed" representation in the vm_map_entry's fields
124 * and the equivalent bits defined in vm_prot_t.
125 */
126 #if defined(__x86_64__)
127 #define VM_VALID_VMPROTECT_FLAGS (VM_PROT_ALL | VM_PROT_COPY | VM_PROT_UEXEC)
128 #else
129 #define VM_VALID_VMPROTECT_FLAGS (VM_PROT_ALL | VM_PROT_COPY)
130 #endif
131
132 /*
133 * FOOTPRINT ACCOUNTING:
134 * The "memory footprint" is better described in the pmap layer.
135 *
136 * At the VM level, these 2 vm_map_entry_t fields are relevant:
137 * iokit_mapped:
138 * For an "iokit_mapped" entry, we add the size of the entry to the
139 * footprint when the entry is entered into the map and we subtract that
140 * size when the entry is removed. No other accounting should take place.
141 * "use_pmap" should be FALSE but is not taken into account.
142 * use_pmap: (only when is_sub_map is FALSE)
143 * This indicates if we should ask the pmap layer to account for pages
144 * in this mapping. If FALSE, we expect that another form of accounting
145 * is being used (e.g. "iokit_mapped" or the explicit accounting of
146 * non-volatile purgable memory).
147 *
148 * So the logic is mostly:
149 * if entry->is_sub_map == TRUE
150 * anything in a submap does not count for the footprint
151 * else if entry->iokit_mapped == TRUE
152 * footprint includes the entire virtual size of this entry
153 * else if entry->use_pmap == FALSE
154 * tell pmap NOT to account for pages being pmap_enter()'d from this
155 * mapping (i.e. use "alternate accounting")
156 * else
157 * pmap will account for pages being pmap_enter()'d from this mapping
158 * as it sees fit (only if anonymous, etc...)
159 */
160
161 #define VME_ALIAS_BITS 12
162 #define VME_ALIAS_MASK ((1u << VME_ALIAS_BITS) - 1)
163 #define VME_OFFSET_SHIFT VME_ALIAS_BITS
164 #define VME_OFFSET_BITS (64 - VME_ALIAS_BITS)
165 #define VME_SUBMAP_SHIFT 2
166 #define VME_SUBMAP_BITS (sizeof(vm_offset_t) * 8 - VME_SUBMAP_SHIFT)
167
168 struct vm_map_entry {
169 struct vm_map_links links; /* links to other entries */
170 #define vme_prev links.prev
171 #define vme_next links.next
172 #define vme_start links.start
173 #define vme_end links.end
174
175 struct vm_map_store store;
176
177 union {
178 vm_offset_t vme_object_value;
179 struct {
180 vm_offset_t vme_atomic:1; /* entry cannot be split/coalesced */
181 vm_offset_t is_sub_map:1; /* Is "object" a submap? */
182 vm_offset_t vme_submap:VME_SUBMAP_BITS;
183 };
184 struct {
185 uint32_t vme_ctx_atomic : 1;
186 uint32_t vme_ctx_is_sub_map : 1;
187 uint32_t vme_context : 30;
188
189 /**
190 * If vme_kernel_object==1 && KASAN,
191 * vme_object_or_delta holds the delta.
192 *
193 * If vme_kernel_object==1 && !KASAN,
194 * vme_tag_btref holds a btref when vme_alias is equal to the "vmtaglog"
195 * boot-arg.
196 *
197 * If vme_kernel_object==0,
198 * vme_object_or_delta holds the packed vm object.
199 */
200 union {
201 vm_page_object_t vme_object_or_delta;
202 btref_t vme_tag_btref;
203 };
204 };
205 };
206
207 unsigned long long
208 /* vm_tag_t */ vme_alias:VME_ALIAS_BITS, /* entry VM tag */
209 /* vm_object_offset_t*/ vme_offset:VME_OFFSET_BITS, /* offset into object */
210
211 /* boolean_t */ is_shared:1, /* region is shared */
212 /* boolean_t */__unused1:1,
213 /* boolean_t */in_transition:1, /* Entry being changed */
214 /* boolean_t */ needs_wakeup:1, /* Waiters on in_transition */
215 /* behavior is not defined for submap type */
216 /* vm_behavior_t */ behavior:2, /* user paging behavior hint */
217 /* boolean_t */ needs_copy:1, /* object need to be copied? */
218
219 /* Only in task maps: */
220 #if defined(__arm64e__)
221 /*
222 * On ARM, the fourth protection bit is unused (UEXEC is x86_64 only).
223 * We reuse it here to keep track of mappings that have hardware support
224 * for read-only/read-write trusted paths.
225 */
226 /* vm_prot_t-like */ protection:3, /* protection code */
227 /* boolean_t */ used_for_tpro:1,
228 #else /* __arm64e__ */
229 /* vm_prot_t-like */protection:4, /* protection code, bit3=UEXEC */
230 #endif /* __arm64e__ */
231
232 /* vm_prot_t-like */ max_protection:4, /* maximum protection, bit3=UEXEC */
233 /* vm_inherit_t */ inheritance:2, /* inheritance */
234
235 /*
236 * use_pmap is overloaded:
237 * if "is_sub_map":
238 * use a nested pmap?
239 * else (i.e. if object):
240 * use pmap accounting
241 * for footprint?
242 */
243 /* boolean_t */ use_pmap:1,
244 /* boolean_t */ no_cache:1, /* should new pages be cached? */
245 /* boolean_t */ vme_permanent:1, /* mapping can not be removed */
246 /* boolean_t */ superpage_size:1, /* use superpages of a certain size */
247 /* boolean_t */ map_aligned:1, /* align to map's page size */
248 /*
249 * zero out the wired pages of this entry
250 * if is being deleted without unwiring them
251 */
252 /* boolean_t */ zero_wired_pages:1,
253 /* boolean_t */ used_for_jit:1,
254 /* boolean_t */ csm_associated:1, /* code signing monitor will validate */
255
256 /* iokit accounting: use the virtual size rather than resident size: */
257 /* boolean_t */ iokit_acct:1,
258 /* boolean_t */ vme_resilient_codesign:1,
259 /* boolean_t */ vme_resilient_media:1,
260 /* boolean_t */ vme_xnu_user_debug:1,
261 /* boolean_t */ vme_no_copy_on_read:1,
262 /* boolean_t */ translated_allow_execute:1, /* execute in translated processes */
263 /* boolean_t */ vme_kernel_object:1; /* vme_object is a kernel_object */
264
265 unsigned short wired_count; /* can be paged if = 0 */
266 unsigned short user_wired_count; /* for vm_wire */
267
268 #if DEBUG
269 #define MAP_ENTRY_CREATION_DEBUG (1)
270 #define MAP_ENTRY_INSERTION_DEBUG (1)
271 #endif /* DEBUG */
272 #if MAP_ENTRY_CREATION_DEBUG
273 struct vm_map_header *vme_creation_maphdr;
274 uint32_t vme_creation_bt; /* btref_t */
275 #endif /* MAP_ENTRY_CREATION_DEBUG */
276 #if MAP_ENTRY_INSERTION_DEBUG
277 uint32_t vme_insertion_bt; /* btref_t */
278 vm_map_offset_t vme_start_original;
279 vm_map_offset_t vme_end_original;
280 #endif /* MAP_ENTRY_INSERTION_DEBUG */
281 };
282
283 #define VME_ALIAS(entry) \
284 ((entry)->vme_alias)
285
286 static inline vm_map_t
_VME_SUBMAP(vm_map_entry_t entry)287 _VME_SUBMAP(
288 vm_map_entry_t entry)
289 {
290 __builtin_assume(entry->vme_submap);
291 return (vm_map_t)(entry->vme_submap << VME_SUBMAP_SHIFT);
292 }
293 #define VME_SUBMAP(entry) ({ assert((entry)->is_sub_map); _VME_SUBMAP(entry); })
294
295 static inline void
VME_SUBMAP_SET(vm_map_entry_t entry,vm_map_t submap)296 VME_SUBMAP_SET(
297 vm_map_entry_t entry,
298 vm_map_t submap)
299 {
300 __builtin_assume(((vm_offset_t)submap & 3) == 0);
301
302 entry->is_sub_map = true;
303 entry->vme_submap = (vm_offset_t)submap >> VME_SUBMAP_SHIFT;
304 }
305
306 static inline vm_object_t
_VME_OBJECT(vm_map_entry_t entry)307 _VME_OBJECT(
308 vm_map_entry_t entry)
309 {
310 vm_object_t object;
311
312 if (!entry->vme_kernel_object) {
313 object = VM_OBJECT_UNPACK(entry->vme_object_or_delta);
314 __builtin_assume(!is_kernel_object(object));
315 } else {
316 object = kernel_object_default;
317 }
318 return object;
319 }
320 #define VME_OBJECT(entry) ({ assert(!(entry)->is_sub_map); _VME_OBJECT(entry); })
321
322
323 static inline vm_object_offset_t
VME_OFFSET(vm_map_entry_t entry)324 VME_OFFSET(
325 vm_map_entry_t entry)
326 {
327 return entry->vme_offset << VME_OFFSET_SHIFT;
328 }
329
330
331 #if (DEBUG || DEVELOPMENT) && !KASAN
332 #define VM_BTLOG_TAGS 1
333 #else
334 #define VM_BTLOG_TAGS 0
335 #endif
336
337
338 /*
339 * Convenience macros for dealing with superpages
340 * SUPERPAGE_NBASEPAGES is architecture dependent and defined in pmap.h
341 */
342 #define SUPERPAGE_SIZE (PAGE_SIZE*SUPERPAGE_NBASEPAGES)
343 #define SUPERPAGE_MASK (-SUPERPAGE_SIZE)
344 #define SUPERPAGE_ROUND_DOWN(a) (a & SUPERPAGE_MASK)
345 #define SUPERPAGE_ROUND_UP(a) ((a + SUPERPAGE_SIZE-1) & SUPERPAGE_MASK)
346
347 /*
348 * wired_counts are unsigned short. This value is used to safeguard
349 * against any mishaps due to runaway user programs.
350 */
351 #define MAX_WIRE_COUNT 65535
352
353 typedef struct vm_map_user_range {
354 vm_map_address_t vmur_min_address __kernel_data_semantics;
355
356 vm_map_address_t vmur_max_address : 56 __kernel_data_semantics;
357 vm_map_range_id_t vmur_range_id : 8;
358 } *vm_map_user_range_t;
359
360 /*
361 * Type: vm_map_t [exported; contents invisible]
362 *
363 * Description:
364 * An address map -- a directory relating valid
365 * regions of a task's address space to the corresponding
366 * virtual memory objects.
367 *
368 * Implementation:
369 * Maps are doubly-linked lists of map entries, sorted
370 * by address. One hint is used to start
371 * searches again from the last successful search,
372 * insertion, or removal. Another hint is used to
373 * quickly find free space.
374 *
375 * Note:
376 * vm_map_relocate_early_elem() knows about this layout,
377 * and needs to be kept in sync.
378 */
379 struct _vm_map {
380 lck_rw_t lock; /* map lock */
381 struct vm_map_header hdr; /* Map entry header */
382 #define min_offset hdr.links.start /* start of range */
383 #define max_offset hdr.links.end /* end of range */
384 pmap_t XNU_PTRAUTH_SIGNED_PTR("_vm_map.pmap") pmap; /* Physical map */
385 vm_map_size_t size; /* virtual size */
386 uint64_t size_limit; /* rlimit on address space size */
387 uint64_t data_limit; /* rlimit on data size */
388 vm_map_size_t user_wire_limit;/* rlimit on user locked memory */
389 vm_map_size_t user_wire_size; /* current size of user locked memory in this map */
390 #if __x86_64__
391 vm_map_offset_t vmmap_high_start;
392 #endif /* __x86_64__ */
393
394 os_ref_atomic_t map_refcnt; /* Reference count */
395
396 #if CONFIG_MAP_RANGES
397 #define VM_MAP_EXTRA_RANGES_MAX 1024
398 struct mach_vm_range default_range;
399 struct mach_vm_range data_range;
400 struct mach_vm_range large_file_range;
401
402 uint16_t extra_ranges_count;
403 vm_map_user_range_t extra_ranges;
404 #endif /* CONFIG_MAP_RANGES */
405
406 union {
407 /*
408 * If map->disable_vmentry_reuse == TRUE:
409 * the end address of the highest allocated vm_map_entry_t.
410 */
411 vm_map_offset_t vmu1_highest_entry_end;
412 /*
413 * For a nested VM map:
414 * the lowest address in this nested VM map that we would
415 * expect to be unnested under normal operation (i.e. for
416 * regular copy-on-write on DATA section).
417 */
418 vm_map_offset_t vmu1_lowest_unnestable_start;
419 } vmu1;
420 #define highest_entry_end vmu1.vmu1_highest_entry_end
421 #define lowest_unnestable_start vmu1.vmu1_lowest_unnestable_start
422 vm_map_entry_t hint; /* hint for quick lookups */
423 union {
424 struct vm_map_links* vmmap_hole_hint; /* hint for quick hole lookups */
425 struct vm_map_corpse_footprint_header *vmmap_corpse_footprint;
426 } vmmap_u_1;
427 #define hole_hint vmmap_u_1.vmmap_hole_hint
428 #define vmmap_corpse_footprint vmmap_u_1.vmmap_corpse_footprint
429 union {
430 vm_map_entry_t _first_free; /* First free space hint */
431 struct vm_map_links* _holes; /* links all holes between entries */
432 } f_s; /* Union for free space data structures being used */
433
434 #define first_free f_s._first_free
435 #define holes_list f_s._holes
436
437 unsigned int
438 /* boolean_t */ wait_for_space:1, /* Should callers wait for space? */
439 /* boolean_t */ wiring_required:1, /* All memory wired? */
440 /* boolean_t */ no_zero_fill:1, /* No zero fill absent pages */
441 /* boolean_t */ mapped_in_other_pmaps:1, /* has this submap been mapped in maps that use a different pmap */
442 /* boolean_t */ switch_protect:1, /* Protect map from write faults while switched */
443 /* boolean_t */ disable_vmentry_reuse:1, /* All vm entries should keep using newer and higher addresses in the map */
444 /* boolean_t */ map_disallow_data_exec:1, /* Disallow execution from data pages on exec-permissive architectures */
445 /* boolean_t */ holelistenabled:1,
446 /* boolean_t */ is_nested_map:1,
447 /* boolean_t */ map_disallow_new_exec:1, /* Disallow new executable code */
448 /* boolean_t */ jit_entry_exists:1,
449 /* boolean_t */ has_corpse_footprint:1,
450 /* boolean_t */ terminated:1,
451 /* boolean_t */ is_alien:1, /* for platform simulation, i.e. PLATFORM_IOS on OSX */
452 /* boolean_t */ cs_enforcement:1, /* code-signing enforcement */
453 /* boolean_t */ cs_debugged:1, /* code-signed but debugged */
454 /* boolean_t */ reserved_regions:1, /* has reserved regions. The map size that userspace sees should ignore these. */
455 /* boolean_t */ single_jit:1, /* only allow one JIT mapping */
456 /* boolean_t */ never_faults:1, /* this map should never cause faults */
457 /* boolean_t */ uses_user_ranges:1, /* has the map been configured to use user VM ranges */
458 /* boolean_t */ tpro_enforcement:1, /* enforce TPRO propagation */
459 /* boolean_t */ corpse_source:1, /* map is being used to create a corpse for diagnostics.*/
460 /* reserved */ res0:1,
461 /* reserved */pad:9;
462 unsigned int timestamp; /* Version number */
463 };
464
465 #define CAST_TO_VM_MAP_ENTRY(x) ((struct vm_map_entry *)(uintptr_t)(x))
466 #define vm_map_to_entry(map) CAST_TO_VM_MAP_ENTRY(&(map)->hdr.links)
467 #define vm_map_first_entry(map) ((map)->hdr.links.next)
468 #define vm_map_last_entry(map) ((map)->hdr.links.prev)
469
470 /*
471 * Type: vm_map_version_t [exported; contents invisible]
472 *
473 * Description:
474 * Map versions may be used to quickly validate a previous
475 * lookup operation.
476 *
477 * Usage note:
478 * Because they are bulky objects, map versions are usually
479 * passed by reference.
480 *
481 * Implementation:
482 * Just a timestamp for the main map.
483 */
484 typedef struct vm_map_version {
485 unsigned int main_timestamp;
486 } vm_map_version_t;
487
488 /*
489 * Type: vm_map_copy_t [exported; contents invisible]
490 *
491 * Description:
492 * A map copy object represents a region of virtual memory
493 * that has been copied from an address map but is still
494 * in transit.
495 *
496 * A map copy object may only be used by a single thread
497 * at a time.
498 *
499 * Implementation:
500 * There are two formats for map copy objects.
501 * The first is very similar to the main
502 * address map in structure, and as a result, some
503 * of the internal maintenance functions/macros can
504 * be used with either address maps or map copy objects.
505 *
506 * The map copy object contains a header links
507 * entry onto which the other entries that represent
508 * the region are chained.
509 *
510 * The second format is a kernel buffer copy object - for data
511 * small enough that physical copies were the most efficient
512 * method. This method uses a zero-sized array unioned with
513 * other format-specific data in the 'c_u' member. This unsized
514 * array overlaps the other elements and allows us to use this
515 * extra structure space for physical memory copies. On 64-bit
516 * systems this saves ~64 bytes per vm_map_copy.
517 */
518
519 struct vm_map_copy {
520 #define VM_MAP_COPY_ENTRY_LIST 1
521 #define VM_MAP_COPY_KERNEL_BUFFER 2
522 uint16_t type;
523 bool is_kernel_range;
524 bool is_user_range;
525 vm_map_range_id_t orig_range;
526 vm_object_offset_t offset;
527 vm_map_size_t size;
528 union {
529 struct vm_map_header hdr; /* ENTRY_LIST */
530 void *XNU_PTRAUTH_SIGNED_PTR("vm_map_copy.kdata") kdata; /* KERNEL_BUFFER */
531 } c_u;
532 };
533
534
535 ZONE_DECLARE_ID(ZONE_ID_VM_MAP_ENTRY, struct vm_map_entry);
536 #define vm_map_entry_zone (&zone_array[ZONE_ID_VM_MAP_ENTRY])
537
538 ZONE_DECLARE_ID(ZONE_ID_VM_MAP_HOLES, struct vm_map_links);
539 #define vm_map_holes_zone (&zone_array[ZONE_ID_VM_MAP_HOLES])
540
541 ZONE_DECLARE_ID(ZONE_ID_VM_MAP, struct _vm_map);
542 #define vm_map_zone (&zone_array[ZONE_ID_VM_MAP])
543
544
545 #define cpy_hdr c_u.hdr
546 #define cpy_kdata c_u.kdata
547
548 #define VM_MAP_COPY_PAGE_SHIFT(copy) ((copy)->cpy_hdr.page_shift)
549 #define VM_MAP_COPY_PAGE_SIZE(copy) (1 << VM_MAP_COPY_PAGE_SHIFT((copy)))
550 #define VM_MAP_COPY_PAGE_MASK(copy) (VM_MAP_COPY_PAGE_SIZE((copy)) - 1)
551
552 /*
553 * Useful macros for entry list copy objects
554 */
555
556 #define vm_map_copy_to_entry(copy) CAST_TO_VM_MAP_ENTRY(&(copy)->cpy_hdr.links)
557 #define vm_map_copy_first_entry(copy) \
558 ((copy)->cpy_hdr.links.next)
559 #define vm_map_copy_last_entry(copy) \
560 ((copy)->cpy_hdr.links.prev)
561
562
563 /*
564 * Macros: vm_map_lock, etc. [internal use only]
565 * Description:
566 * Perform locking on the data portion of a map.
567 * When multiple maps are to be locked, order by map address.
568 * (See vm_map.c::vm_remap())
569 */
570
571 #define vm_map_lock_init(map) \
572 ((map)->timestamp = 0 , \
573 lck_rw_init(&(map)->lock, &vm_map_lck_grp, &vm_map_lck_rw_attr))
574
575 #define vm_map_lock(map) \
576 MACRO_BEGIN \
577 DTRACE_VM(vm_map_lock_w); \
578 lck_rw_lock_exclusive(&(map)->lock); \
579 MACRO_END
580
581 #define vm_map_unlock(map) \
582 MACRO_BEGIN \
583 DTRACE_VM(vm_map_unlock_w); \
584 (map)->timestamp++; \
585 lck_rw_done(&(map)->lock); \
586 MACRO_END
587
588 #define vm_map_lock_read(map) \
589 MACRO_BEGIN \
590 DTRACE_VM(vm_map_lock_r); \
591 lck_rw_lock_shared(&(map)->lock); \
592 MACRO_END
593
594 #define vm_map_unlock_read(map) \
595 MACRO_BEGIN \
596 DTRACE_VM(vm_map_unlock_r); \
597 lck_rw_done(&(map)->lock); \
598 MACRO_END
599
600 #define vm_map_lock_write_to_read(map) \
601 MACRO_BEGIN \
602 DTRACE_VM(vm_map_lock_downgrade); \
603 (map)->timestamp++; \
604 lck_rw_lock_exclusive_to_shared(&(map)->lock); \
605 MACRO_END
606
607 #define vm_map_lock_assert_held(map) \
608 LCK_RW_ASSERT(&(map)->lock, LCK_RW_ASSERT_HELD)
609 #define vm_map_lock_assert_shared(map) \
610 LCK_RW_ASSERT(&(map)->lock, LCK_RW_ASSERT_SHARED)
611 #define vm_map_lock_assert_exclusive(map) \
612 LCK_RW_ASSERT(&(map)->lock, LCK_RW_ASSERT_EXCLUSIVE)
613 #define vm_map_lock_assert_notheld(map) \
614 LCK_RW_ASSERT(&(map)->lock, LCK_RW_ASSERT_NOTHELD)
615
616 /*
617 * Exported procedures that operate on vm_map_t.
618 */
619
620 /* Lookup map entry containing or the specified address in the given map */
621 extern boolean_t vm_map_lookup_entry(
622 vm_map_t map,
623 vm_map_address_t address,
624 vm_map_entry_t *entry); /* OUT */
625
626
627 /*
628 * Functions implemented as macros
629 */
630 #define vm_map_min(map) ((map)->min_offset)
631 /* Lowest valid address in
632 * a map */
633
634 #define vm_map_max(map) ((map)->max_offset)
635 /* Highest valid address */
636
637 #define vm_map_pmap(map) ((map)->pmap)
638 /* Physical map associated
639 * with this address map */
640
641 /* Gain a reference to an existing map */
642 extern void vm_map_reference(
643 vm_map_t map);
644
645 /*
646 * Wait and wakeup macros for in_transition map entries.
647 */
648 #define vm_map_entry_wait(map, interruptible) \
649 ((map)->timestamp++ , \
650 lck_rw_sleep(&(map)->lock, LCK_SLEEP_EXCLUSIVE|LCK_SLEEP_PROMOTED_PRI, \
651 (event_t)&(map)->hdr, interruptible))
652
653
654 #define vm_map_entry_wakeup(map) \
655 thread_wakeup((event_t)(&(map)->hdr))
656
657
658 extern void vm_map_inherit_limits(
659 vm_map_t new_map,
660 const struct _vm_map *old_map);
661
662 /* Create a new task map using an existing task map as a template. */
663 extern vm_map_t vm_map_fork(
664 ledger_t ledger,
665 vm_map_t old_map,
666 int options);
667 #define VM_MAP_FORK_SHARE_IF_INHERIT_NONE 0x00000001
668 #define VM_MAP_FORK_PRESERVE_PURGEABLE 0x00000002
669 #define VM_MAP_FORK_CORPSE_FOOTPRINT 0x00000004
670 #define VM_MAP_FORK_SHARE_IF_OWNED 0x00000008
671
672 /* Change inheritance */
673 extern kern_return_t vm_map_inherit(
674 vm_map_t map,
675 vm_map_offset_t start,
676 vm_map_offset_t end,
677 vm_inherit_t new_inheritance);
678
679
680 extern kern_return_t vm_map_query_volatile(
681 vm_map_t map,
682 mach_vm_size_t *volatile_virtual_size_p,
683 mach_vm_size_t *volatile_resident_size_p,
684 mach_vm_size_t *volatile_compressed_size_p,
685 mach_vm_size_t *volatile_pmap_size_p,
686 mach_vm_size_t *volatile_compressed_pmap_size_p);
687
688
689 extern kern_return_t vm_map_set_cache_attr(
690 vm_map_t map,
691 vm_map_offset_t va);
692
693
694 extern void vm_map_copy_footprint_ledgers(
695 task_t old_task,
696 task_t new_task);
697
698
699 /**
700 * Represents a single region of virtual address space that should be reserved
701 * (pre-mapped) in a user address space.
702 */
703 struct vm_reserved_region {
704 const char *vmrr_name;
705 vm_map_offset_t vmrr_addr;
706 vm_map_size_t vmrr_size;
707 };
708
709 /**
710 * Return back a machine-dependent array of address space regions that should be
711 * reserved by the VM. This function is defined in the machine-dependent
712 * machine_routines.c files.
713 */
714 extern size_t ml_get_vm_reserved_regions(
715 bool vm_is64bit,
716 const struct vm_reserved_region **regions);
717
718 /**
719 * Explicitly preallocates a floating point save area. This function is defined
720 * in the machine-dependent machine_routines.c files.
721 */
722 extern void ml_fp_save_area_prealloc(void);
723
724 #endif /* MACH_KERNEL_PRIVATE */
725
726 /*
727 * Read and write from a kernel buffer to a specified map.
728 */
729 extern kern_return_t vm_map_write_user(
730 vm_map_t map,
731 void *src_p,
732 vm_map_offset_ut dst_addr_u,
733 vm_size_ut size_u);
734
735 extern kern_return_t vm_map_read_user(
736 vm_map_t map,
737 vm_map_offset_ut src_addr_u,
738 void *dst_p,
739 vm_size_ut size_u);
740
741 extern vm_map_size_t vm_map_adjusted_size(vm_map_t map);
742
743 extern vm_map_t vm_map_switch(
744 vm_map_t map);
745
746 /* Change protection */
747 extern kern_return_t vm_map_protect(
748 vm_map_t map,
749 vm_map_offset_t start,
750 vm_map_offset_t end,
751 vm_prot_t new_prot,
752 boolean_t set_max);
753
754 extern boolean_t vm_map_cs_enforcement(
755 vm_map_t map);
756 extern void vm_map_cs_enforcement_set(
757 vm_map_t map,
758 boolean_t val);
759
760 extern void vm_map_cs_debugged_set(
761 vm_map_t map,
762 boolean_t val);
763
764 extern kern_return_t vm_map_cs_wx_enable(vm_map_t map);
765 extern kern_return_t vm_map_csm_allow_jit(vm_map_t map);
766
767
768 extern void vm_map_will_allocate_early_map(
769 vm_map_t *map_owner);
770
771 extern void vm_map_relocate_early_maps(
772 vm_offset_t delta);
773
774 extern void vm_map_relocate_early_elem(
775 uint32_t zone_id,
776 vm_offset_t new_addr,
777 vm_offset_t delta);
778
779 /* wire down a region */
780
781 /* never fails */
782 extern vm_map_t vm_map_create_options(
783 pmap_t pmap,
784 vm_map_offset_t min_off,
785 vm_map_offset_t max_off,
786 vm_map_create_options_t options);
787
788 extern boolean_t vm_kernel_map_is_kernel(vm_map_t map);
789
790 /*!
791 * @function vm_map_enter_mem_object_control()
792 *
793 * @brief
794 * Enters a mapping of @c initial_size bytes at @c *address (subject to
795 * fixed/anywhere semantics, see @c VM_FLAGS_FIXED/VM_FLAGS_ANYWHERE ).
796 * The pages will come from a memory object paged in by the @c control pager,
797 * and the caller may specify an @c offset into the object.
798 *
799 * @param target_map The map into which to enter the mapping.
800 * @param address [in] Pointer to the address at which to enter the
801 * mapping (or use as a hint for anywhere
802 * mappings).
803 * No alignment is required, the function will
804 * round this down to a page boundary in the
805 * @c target_map.
806 * [out] On success, it will be filled with the address
807 * at which the object data is made available, and
808 * will have the same misalignment into
809 * @c target_map as @c offset.
810 * On failure, it remains unmodified.
811 * @param initial_size Size of the mapping to enter.
812 * Must be non-zero.
813 * No alignment is required.
814 * @param mask An alignment mask the mapping must respect.
815 * @param vmk_flags The vm map kernel flags to influence this call.
816 * @param control The pager-managed memory object which is the source
817 * of the pages.
818 * @param offset The offset into the memory object to use when
819 * paging.
820 * @c vm_map_enter, which is called into by
821 * @c vm_map_enter_mem_object_control, requires that
822 * @c offset be page-aligned for either @c target_map
823 * pages or kernel pages.
824 * @param needs_copy Boolean which can be set to request that the mapped
825 * pages be a copy of the memory object's pages.
826 * @param cur_protection Effective protection that should be set for the
827 * mapping.
828 * @param max_protection Max protection that should be allowed for the
829 * mapping. Should at least cover @c cur_protection.
830 * @param inheritance Inheritance policy for the mapping.
831 *
832 * @returns @c KERN_SUCCESS if the mapping was successfully entered, an error
833 * code otherwise.
834 */
835 extern kern_return_t vm_map_enter_mem_object_control(
836 vm_map_t target_map,
837 vm_map_offset_ut *address,
838 vm_map_size_ut initial_size,
839 vm_map_offset_ut mask,
840 vm_map_kernel_flags_t vmk_flags,
841 memory_object_control_t control,
842 vm_object_offset_ut offset,
843 boolean_t needs_copy,
844 vm_prot_ut cur_protection,
845 vm_prot_ut max_protection,
846 vm_inherit_ut inheritance);
847
848 extern kern_return_t vm_map_terminate(
849 vm_map_t map);
850
851 /* Overwrite existing memory with a copy */
852 extern kern_return_t vm_map_copy_overwrite(
853 vm_map_t dst_map,
854 vm_map_address_ut dst_addr_u,
855 vm_map_copy_t copy,
856 vm_map_size_ut copy_size_u,
857 boolean_t interruptible);
858
859 /* returns TRUE if size of vm_map_copy == *size, FALSE otherwise */
860 extern boolean_t vm_map_copy_validate_size(
861 vm_map_t dst_map,
862 vm_map_copy_t copy,
863 vm_map_size_t *size);
864
865 extern kern_return_t vm_map_copyout_size(
866 vm_map_t dst_map,
867 vm_map_address_t *dst_addr, /* OUT */
868 vm_map_copy_t copy,
869 vm_map_size_ut copy_size);
870
871 extern void vm_map_disable_NX(
872 vm_map_t map);
873
874 extern void vm_map_disallow_data_exec(
875 vm_map_t map);
876
877 extern void vm_map_set_64bit(
878 vm_map_t map);
879
880 extern void vm_map_set_32bit(
881 vm_map_t map);
882
883 extern void vm_map_set_jumbo(
884 vm_map_t map);
885
886 #if XNU_PLATFORM_iPhoneOS && EXTENDED_USER_VA_SUPPORT
887 extern void vm_map_set_extra_jumbo(
888 vm_map_t map);
889 #endif /* XNU_PLATFORM_iPhoneOS && EXTENDED_USER_VA_SUPPORT */
890
891 extern void vm_map_set_jit_entitled(
892 vm_map_t map);
893
894 extern void vm_map_set_max_addr(
895 vm_map_t map,
896 vm_map_offset_t new_max_offset,
897 bool extra_jumbo);
898
899 extern boolean_t vm_map_has_hard_pagezero(
900 vm_map_t map,
901 vm_map_offset_t pagezero_size);
902
903 extern void vm_commit_pagezero_status(vm_map_t tmap);
904
905 extern boolean_t vm_map_tpro(
906 vm_map_t map);
907
908 extern void vm_map_set_tpro(
909 vm_map_t map);
910
911 extern void vm_map_set_tpro_enforcement(
912 vm_map_t map);
913
914 extern boolean_t vm_map_set_tpro_range(
915 vm_map_t map,
916 vm_map_address_t start,
917 vm_map_address_t end);
918
919 extern boolean_t vm_map_is_64bit(
920 vm_map_t map);
921
922 extern kern_return_t vm_map_raise_max_offset(
923 vm_map_t map,
924 vm_map_offset_t new_max_offset);
925
926 extern kern_return_t vm_map_raise_min_offset(
927 vm_map_t map,
928 vm_map_offset_t new_min_offset);
929
930 #if XNU_TARGET_OS_OSX
931 extern void vm_map_set_high_start(
932 vm_map_t map,
933 vm_map_offset_t high_start);
934 #endif /* XNU_TARGET_OS_OSX */
935
936
937 extern vm_map_offset_t vm_compute_max_offset(
938 boolean_t is64);
939
940 extern void vm_map_get_max_aslr_slide_section(
941 vm_map_t map,
942 int64_t *max_sections,
943 int64_t *section_size);
944
945 extern uint64_t vm_map_get_max_aslr_slide_pages(
946 vm_map_t map);
947
948 extern uint64_t vm_map_get_max_loader_aslr_slide_pages(
949 vm_map_t map);
950
951 extern kern_return_t vm_map_set_size_limit(
952 vm_map_t map,
953 uint64_t limit);
954
955 extern kern_return_t vm_map_set_data_limit(
956 vm_map_t map,
957 uint64_t limit);
958
959 extern void vm_map_set_user_wire_limit(
960 vm_map_t map,
961 vm_size_t limit);
962
963 extern void vm_map_switch_protect(
964 vm_map_t map,
965 boolean_t val);
966
967 extern boolean_t vm_map_page_aligned(
968 vm_map_offset_t offset,
969 vm_map_offset_t mask);
970
971 extern bool vm_map_range_overflows(
972 vm_map_t map,
973 vm_map_offset_t addr,
974 vm_map_size_t size);
975
976 /* Support for vm_map ranges */
977 extern kern_return_t vm_map_range_configure(
978 vm_map_t map,
979 bool needs_extra_jumbo_va);
980
981
982
983 /*!
984 * @function vm_map_kernel_flags_update_range_id()
985 *
986 * @brief
987 * Updates the @c vmkf_range_id field with the adequate value
988 * according to the policy for specified map and tag set in @c vmk_flags.
989 *
990 * @discussion
991 * This function is meant to be called by Mach VM entry points,
992 * which matters for the kernel: allocations with pointers _MUST_
993 * be allocated with @c kmem_*() functions.
994 *
995 * If the range ID is already set, it is preserved.
996 */
997 extern void vm_map_kernel_flags_update_range_id(
998 vm_map_kernel_flags_t *flags,
999 vm_map_t map,
1000 vm_map_size_t size);
1001
1002 #if XNU_TARGET_OS_OSX
1003 extern void vm_map_mark_alien(vm_map_t map);
1004 extern void vm_map_single_jit(vm_map_t map);
1005 #endif /* XNU_TARGET_OS_OSX */
1006
1007 extern kern_return_t vm_map_page_info(
1008 vm_map_t map,
1009 vm_map_offset_t offset,
1010 vm_page_info_flavor_t flavor,
1011 vm_page_info_t info,
1012 mach_msg_type_number_t *count);
1013 extern kern_return_t vm_map_page_range_info_internal(
1014 vm_map_t map,
1015 vm_map_offset_t start_offset,
1016 vm_map_offset_t end_offset,
1017 int effective_page_shift,
1018 vm_page_info_flavor_t flavor,
1019 vm_page_info_t info,
1020 mach_msg_type_number_t *count);
1021
1022 #ifdef MACH_KERNEL_PRIVATE
1023
1024 /*
1025 * Internal macros for rounding and truncation of vm_map offsets and sizes
1026 */
1027 #define VM_MAP_ROUND_PAGE(x, pgmask) (((vm_map_offset_t)(x) + (pgmask)) & ~((signed)(pgmask)))
1028 #define VM_MAP_TRUNC_PAGE(x, pgmask) ((vm_map_offset_t)(x) & ~((signed)(pgmask)))
1029
1030 /*
1031 * Macros for rounding and truncation of vm_map offsets and sizes
1032 */
1033 static inline int
VM_MAP_PAGE_SHIFT(vm_map_t map)1034 VM_MAP_PAGE_SHIFT(
1035 vm_map_t map)
1036 {
1037 int shift = map ? map->hdr.page_shift : PAGE_SHIFT;
1038 /*
1039 * help ubsan and codegen in general,
1040 * cannot use PAGE_{MIN,MAX}_SHIFT
1041 * because of testing code which
1042 * tests 16k aligned maps on 4k only systems.
1043 */
1044 __builtin_assume(shift >= 12 && shift <= 14);
1045 return shift;
1046 }
1047
1048 #define VM_MAP_PAGE_SIZE(map) (1 << VM_MAP_PAGE_SHIFT((map)))
1049 #define VM_MAP_PAGE_MASK(map) (VM_MAP_PAGE_SIZE((map)) - 1)
1050 #define VM_MAP_PAGE_ALIGNED(x, pgmask) (((x) & (pgmask)) == 0)
1051
1052 #endif /* MACH_KERNEL_PRIVATE */
1053
1054
1055 extern kern_return_t vm_map_set_page_shift(vm_map_t map, int pageshift);
1056 extern bool vm_map_is_exotic(vm_map_t map);
1057 extern bool vm_map_is_alien(vm_map_t map);
1058 extern pmap_t vm_map_get_pmap(vm_map_t map);
1059
1060 extern bool vm_map_is_corpse_source(vm_map_t map);
1061 extern void vm_map_set_corpse_source(vm_map_t map);
1062 extern void vm_map_unset_corpse_source(vm_map_t map);
1063
1064
1065 #if CONFIG_DYNAMIC_CODE_SIGNING
1066
1067 extern kern_return_t vm_map_sign(vm_map_t map,
1068 vm_map_offset_t start,
1069 vm_map_offset_t end);
1070
1071 #endif /* CONFIG_DYNAMIC_CODE_SIGNING */
1072 #if CONFIG_FREEZE
1073
1074 extern kern_return_t vm_map_freeze(
1075 task_t task,
1076 unsigned int *purgeable_count,
1077 unsigned int *wired_count,
1078 unsigned int *clean_count,
1079 unsigned int *dirty_count,
1080 unsigned int dirty_budget,
1081 unsigned int *shared_count,
1082 int *freezer_error_code,
1083 boolean_t eval_only);
1084
1085 __enum_decl(freezer_error_code_t, int, {
1086 FREEZER_ERROR_GENERIC = -1,
1087 FREEZER_ERROR_EXCESS_SHARED_MEMORY = -2,
1088 FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO = -3,
1089 FREEZER_ERROR_NO_COMPRESSOR_SPACE = -4,
1090 FREEZER_ERROR_NO_SWAP_SPACE = -5,
1091 FREEZER_ERROR_NO_SLOTS = -6,
1092 });
1093
1094 #endif /* CONFIG_FREEZE */
1095
1096 extern kern_return_t vm_map_partial_reap(
1097 vm_map_t map,
1098 unsigned int *reclaimed_resident,
1099 unsigned int *reclaimed_compressed);
1100
1101 /*
1102 * In some cases, we don't have a real VM object but still want to return a
1103 * unique ID (to avoid a memory region looking like shared memory), so build
1104 * a fake pointer based on the map's ledger and the index of the ledger being
1105 * reported.
1106 */
1107 #define VM_OBJECT_ID_FAKE(map, ledger_id) ((uint32_t)(uintptr_t)VM_KERNEL_ADDRPERM((int*)((map)->pmap->ledger)+(ledger_id)))
1108
1109 #if DEVELOPMENT || DEBUG
1110
1111 extern int vm_map_disconnect_page_mappings(
1112 vm_map_t map,
1113 boolean_t);
1114
1115 extern kern_return_t vm_map_inject_error(vm_map_t map, vm_map_offset_t vaddr);
1116
1117 extern kern_return_t vm_map_entries_foreach(vm_map_t map, kern_return_t (^count_handler)(int nentries),
1118 kern_return_t (^entry_handler)(void* entry));
1119 extern kern_return_t vm_map_dump_entry_and_compressor_pager(void* entry, char *buf, size_t *count);
1120
1121 #endif /* DEVELOPMENT || DEBUG */
1122
1123 boolean_t kdp_vm_map_is_acquired_exclusive(vm_map_t map);
1124
1125 boolean_t vm_map_entry_has_device_pager(vm_map_t, vm_map_offset_t vaddr);
1126
1127
1128 #ifdef VM_SCAN_FOR_SHADOW_CHAIN
1129 int vm_map_shadow_max(vm_map_t map);
1130 #endif
1131 __END_DECLS
1132
1133 #endif /* XNU_KERNEL_PRIVATE */
1134 #endif /* _VM_VM_MAP_XNU_H_ */
1135