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
673 extern kern_return_t vm_map_query_volatile(
674 vm_map_t map,
675 mach_vm_size_t *volatile_virtual_size_p,
676 mach_vm_size_t *volatile_resident_size_p,
677 mach_vm_size_t *volatile_compressed_size_p,
678 mach_vm_size_t *volatile_pmap_size_p,
679 mach_vm_size_t *volatile_compressed_pmap_size_p);
680
681
682 extern kern_return_t vm_map_set_cache_attr(
683 vm_map_t map,
684 vm_map_offset_t va);
685
686
687 extern void vm_map_copy_footprint_ledgers(
688 task_t old_task,
689 task_t new_task);
690
691
692 /**
693 * Represents a single region of virtual address space that should be reserved
694 * (pre-mapped) in a user address space.
695 */
696 struct vm_reserved_region {
697 const char *vmrr_name;
698 vm_map_offset_t vmrr_addr;
699 vm_map_size_t vmrr_size;
700 };
701
702 /**
703 * Return back a machine-dependent array of address space regions that should be
704 * reserved by the VM. This function is defined in the machine-dependent
705 * machine_routines.c files.
706 */
707 extern size_t ml_get_vm_reserved_regions(
708 bool vm_is64bit,
709 const struct vm_reserved_region **regions);
710
711 /**
712 * Explicitly preallocates a floating point save area. This function is defined
713 * in the machine-dependent machine_routines.c files.
714 */
715 extern void ml_fp_save_area_prealloc(void);
716
717 #endif /* MACH_KERNEL_PRIVATE */
718
719 /*
720 * Read and write from a kernel buffer to a specified map.
721 */
722 extern kern_return_t vm_map_write_user(
723 vm_map_t map,
724 void *src_p,
725 vm_map_offset_ut dst_addr_u,
726 vm_size_ut size_u);
727
728 extern kern_return_t vm_map_read_user(
729 vm_map_t map,
730 vm_map_offset_ut src_addr_u,
731 void *dst_p,
732 vm_size_ut size_u);
733
734 extern vm_map_size_t vm_map_adjusted_size(vm_map_t map);
735
736 extern vm_map_t vm_map_switch(
737 vm_map_t map);
738
739 extern boolean_t vm_map_cs_enforcement(
740 vm_map_t map);
741 extern void vm_map_cs_enforcement_set(
742 vm_map_t map,
743 boolean_t val);
744
745 extern void vm_map_cs_debugged_set(
746 vm_map_t map,
747 boolean_t val);
748
749 extern kern_return_t vm_map_cs_wx_enable(vm_map_t map);
750 extern kern_return_t vm_map_csm_allow_jit(vm_map_t map);
751
752
753 extern void vm_map_will_allocate_early_map(
754 vm_map_t *map_owner);
755
756 extern void vm_map_relocate_early_maps(
757 vm_offset_t delta);
758
759 extern void vm_map_relocate_early_elem(
760 uint32_t zone_id,
761 vm_offset_t new_addr,
762 vm_offset_t delta);
763
764 /* wire down a region */
765
766 /* never fails */
767 extern vm_map_t vm_map_create_options(
768 pmap_t pmap,
769 vm_map_offset_t min_off,
770 vm_map_offset_t max_off,
771 vm_map_create_options_t options);
772
773 extern boolean_t vm_kernel_map_is_kernel(vm_map_t map);
774
775 /*!
776 * @function vm_map_enter_mem_object_control()
777 *
778 * @brief
779 * Enters a mapping of @c initial_size bytes at @c *address (subject to
780 * fixed/anywhere semantics, see @c VM_FLAGS_FIXED/VM_FLAGS_ANYWHERE ).
781 * The pages will come from a memory object paged in by the @c control pager,
782 * and the caller may specify an @c offset into the object.
783 *
784 * @param target_map The map into which to enter the mapping.
785 * @param address [in] Pointer to the address at which to enter the
786 * mapping (or use as a hint for anywhere
787 * mappings).
788 * No alignment is required, the function will
789 * round this down to a page boundary in the
790 * @c target_map.
791 * [out] On success, it will be filled with the address
792 * at which the object data is made available, and
793 * will have the same misalignment into
794 * @c target_map as @c offset.
795 * On failure, it remains unmodified.
796 * @param initial_size Size of the mapping to enter.
797 * Must be non-zero.
798 * No alignment is required.
799 * @param mask An alignment mask the mapping must respect.
800 * @param vmk_flags The vm map kernel flags to influence this call.
801 * @param control The pager-managed memory object which is the source
802 * of the pages.
803 * @param offset The offset into the memory object to use when
804 * paging.
805 * @c vm_map_enter, which is called into by
806 * @c vm_map_enter_mem_object_control, requires that
807 * @c offset be page-aligned for either @c target_map
808 * pages or kernel pages.
809 * @param needs_copy Boolean which can be set to request that the mapped
810 * pages be a copy of the memory object's pages.
811 * @param cur_protection Effective protection that should be set for the
812 * mapping.
813 * @param max_protection Max protection that should be allowed for the
814 * mapping. Should at least cover @c cur_protection.
815 * @param inheritance Inheritance policy for the mapping.
816 *
817 * @returns @c KERN_SUCCESS if the mapping was successfully entered, an error
818 * code otherwise.
819 */
820 extern kern_return_t vm_map_enter_mem_object_control(
821 vm_map_t target_map,
822 vm_map_offset_ut *address,
823 vm_map_size_ut initial_size,
824 vm_map_offset_ut mask,
825 vm_map_kernel_flags_t vmk_flags,
826 memory_object_control_t control,
827 vm_object_offset_ut offset,
828 boolean_t needs_copy,
829 vm_prot_ut cur_protection,
830 vm_prot_ut max_protection,
831 vm_inherit_ut inheritance);
832
833 extern kern_return_t vm_map_terminate(
834 vm_map_t map);
835
836 /* Overwrite existing memory with a copy */
837 extern kern_return_t vm_map_copy_overwrite(
838 vm_map_t dst_map,
839 vm_map_address_ut dst_addr_u,
840 vm_map_copy_t copy,
841 vm_map_size_ut copy_size_u,
842 boolean_t interruptible);
843
844 /* returns TRUE if size of vm_map_copy == *size, FALSE otherwise */
845 extern boolean_t vm_map_copy_validate_size(
846 vm_map_t dst_map,
847 vm_map_copy_t copy,
848 vm_map_size_t *size);
849
850 extern kern_return_t vm_map_copyout_size(
851 vm_map_t dst_map,
852 vm_map_address_t *dst_addr, /* OUT */
853 vm_map_copy_t copy,
854 vm_map_size_ut copy_size);
855
856 extern void vm_map_disable_NX(
857 vm_map_t map);
858
859 extern void vm_map_disallow_data_exec(
860 vm_map_t map);
861
862 extern void vm_map_set_64bit(
863 vm_map_t map);
864
865 extern void vm_map_set_32bit(
866 vm_map_t map);
867
868 extern void vm_map_set_jumbo(
869 vm_map_t map);
870
871 #if XNU_PLATFORM_iPhoneOS && EXTENDED_USER_VA_SUPPORT
872 extern void vm_map_set_extra_jumbo(
873 vm_map_t map);
874 #endif /* XNU_PLATFORM_iPhoneOS && EXTENDED_USER_VA_SUPPORT */
875
876 extern void vm_map_set_jit_entitled(
877 vm_map_t map);
878
879 extern void vm_map_set_max_addr(
880 vm_map_t map,
881 vm_map_offset_t new_max_offset,
882 bool extra_jumbo);
883
884 extern boolean_t vm_map_has_hard_pagezero(
885 vm_map_t map,
886 vm_map_offset_t pagezero_size);
887
888 extern void vm_commit_pagezero_status(vm_map_t tmap);
889
890 extern boolean_t vm_map_tpro(
891 vm_map_t map);
892
893 extern void vm_map_set_tpro(
894 vm_map_t map);
895
896 extern void vm_map_set_tpro_enforcement(
897 vm_map_t map);
898
899 extern boolean_t vm_map_set_tpro_range(
900 vm_map_t map,
901 vm_map_address_t start,
902 vm_map_address_t end);
903
904 extern boolean_t vm_map_is_64bit(
905 vm_map_t map);
906
907 extern kern_return_t vm_map_raise_max_offset(
908 vm_map_t map,
909 vm_map_offset_t new_max_offset);
910
911 extern kern_return_t vm_map_raise_min_offset(
912 vm_map_t map,
913 vm_map_offset_t new_min_offset);
914
915 #if XNU_TARGET_OS_OSX
916 extern void vm_map_set_high_start(
917 vm_map_t map,
918 vm_map_offset_t high_start);
919 #endif /* XNU_TARGET_OS_OSX */
920
921
922 extern vm_map_offset_t vm_compute_max_offset(
923 boolean_t is64);
924
925 extern void vm_map_get_max_aslr_slide_section(
926 vm_map_t map,
927 int64_t *max_sections,
928 int64_t *section_size);
929
930 extern uint64_t vm_map_get_max_aslr_slide_pages(
931 vm_map_t map);
932
933 extern uint64_t vm_map_get_max_loader_aslr_slide_pages(
934 vm_map_t map);
935
936 extern kern_return_t vm_map_set_size_limit(
937 vm_map_t map,
938 uint64_t limit);
939
940 extern kern_return_t vm_map_set_data_limit(
941 vm_map_t map,
942 uint64_t limit);
943
944 extern void vm_map_set_user_wire_limit(
945 vm_map_t map,
946 vm_size_t limit);
947
948 extern void vm_map_switch_protect(
949 vm_map_t map,
950 boolean_t val);
951
952 extern boolean_t vm_map_page_aligned(
953 vm_map_offset_t offset,
954 vm_map_offset_t mask);
955
956 extern bool vm_map_range_overflows(
957 vm_map_t map,
958 vm_map_offset_t addr,
959 vm_map_size_t size);
960
961 /* Support for vm_map ranges */
962 extern kern_return_t vm_map_range_configure(
963 vm_map_t map,
964 bool needs_extra_jumbo_va);
965
966
967
968 /*!
969 * @function vm_map_kernel_flags_update_range_id()
970 *
971 * @brief
972 * Updates the @c vmkf_range_id field with the adequate value
973 * according to the policy for specified map and tag set in @c vmk_flags.
974 *
975 * @discussion
976 * This function is meant to be called by Mach VM entry points,
977 * which matters for the kernel: allocations with pointers _MUST_
978 * be allocated with @c kmem_*() functions.
979 *
980 * If the range ID is already set, it is preserved.
981 */
982 extern void vm_map_kernel_flags_update_range_id(
983 vm_map_kernel_flags_t *flags,
984 vm_map_t map,
985 vm_map_size_t size);
986
987 #if XNU_TARGET_OS_OSX
988 extern void vm_map_mark_alien(vm_map_t map);
989 extern void vm_map_single_jit(vm_map_t map);
990 #endif /* XNU_TARGET_OS_OSX */
991
992 extern kern_return_t vm_map_page_info(
993 vm_map_t map,
994 vm_map_offset_ut offset,
995 vm_page_info_flavor_t flavor,
996 vm_page_info_t info,
997 mach_msg_type_number_t *count);
998
999 extern kern_return_t vm_map_page_range_info_internal(
1000 vm_map_t map,
1001 vm_map_offset_ut start_offset,
1002 vm_map_offset_ut end_offset,
1003 int effective_page_shift,
1004 vm_page_info_flavor_t flavor,
1005 vm_page_info_t info,
1006 mach_msg_type_number_t *count);
1007
1008 #ifdef MACH_KERNEL_PRIVATE
1009
1010 /*
1011 * Internal macros for rounding and truncation of vm_map offsets and sizes
1012 */
1013 #define VM_MAP_ROUND_PAGE(x, pgmask) (((vm_map_offset_t)(x) + (pgmask)) & ~((signed)(pgmask)))
1014 #define VM_MAP_TRUNC_PAGE(x, pgmask) ((vm_map_offset_t)(x) & ~((signed)(pgmask)))
1015
1016 /*
1017 * Macros for rounding and truncation of vm_map offsets and sizes
1018 */
1019 static inline int
VM_MAP_PAGE_SHIFT(vm_map_t map)1020 VM_MAP_PAGE_SHIFT(
1021 vm_map_t map)
1022 {
1023 int shift = map ? map->hdr.page_shift : PAGE_SHIFT;
1024 /*
1025 * help ubsan and codegen in general,
1026 * cannot use PAGE_{MIN,MAX}_SHIFT
1027 * because of testing code which
1028 * tests 16k aligned maps on 4k only systems.
1029 */
1030 __builtin_assume(shift >= 12 && shift <= 14);
1031 return shift;
1032 }
1033
1034 #define VM_MAP_PAGE_SIZE(map) (1 << VM_MAP_PAGE_SHIFT((map)))
1035 #define VM_MAP_PAGE_MASK(map) (VM_MAP_PAGE_SIZE((map)) - 1)
1036 #define VM_MAP_PAGE_ALIGNED(x, pgmask) (((x) & (pgmask)) == 0)
1037
1038 #endif /* MACH_KERNEL_PRIVATE */
1039
1040
1041 extern kern_return_t vm_map_set_page_shift(vm_map_t map, int pageshift);
1042 extern bool vm_map_is_exotic(vm_map_t map);
1043 extern bool vm_map_is_alien(vm_map_t map);
1044 extern pmap_t vm_map_get_pmap(vm_map_t map);
1045
1046 extern bool vm_map_is_corpse_source(vm_map_t map);
1047 extern void vm_map_set_corpse_source(vm_map_t map);
1048 extern void vm_map_unset_corpse_source(vm_map_t map);
1049
1050
1051 #if CONFIG_DYNAMIC_CODE_SIGNING
1052
1053 extern kern_return_t vm_map_sign(vm_map_t map,
1054 vm_map_offset_t start,
1055 vm_map_offset_t end);
1056
1057 #endif /* CONFIG_DYNAMIC_CODE_SIGNING */
1058 #if CONFIG_FREEZE
1059
1060 extern kern_return_t vm_map_freeze(
1061 task_t task,
1062 unsigned int *purgeable_count,
1063 unsigned int *wired_count,
1064 unsigned int *clean_count,
1065 unsigned int *dirty_count,
1066 unsigned int dirty_budget,
1067 unsigned int *shared_count,
1068 int *freezer_error_code,
1069 boolean_t eval_only);
1070
1071 __enum_decl(freezer_error_code_t, int, {
1072 FREEZER_ERROR_GENERIC = -1,
1073 FREEZER_ERROR_EXCESS_SHARED_MEMORY = -2,
1074 FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO = -3,
1075 FREEZER_ERROR_NO_COMPRESSOR_SPACE = -4,
1076 FREEZER_ERROR_NO_SWAP_SPACE = -5,
1077 FREEZER_ERROR_NO_SLOTS = -6,
1078 });
1079
1080 #endif /* CONFIG_FREEZE */
1081
1082 extern kern_return_t vm_map_partial_reap(
1083 vm_map_t map,
1084 unsigned int *reclaimed_resident,
1085 unsigned int *reclaimed_compressed);
1086
1087 /*
1088 * In some cases, we don't have a real VM object but still want to return a
1089 * unique ID (to avoid a memory region looking like shared memory), so build
1090 * a fake pointer based on the map's ledger and the index of the ledger being
1091 * reported.
1092 */
1093 #define VM_OBJECT_ID_FAKE(map, ledger_id) ((uint32_t)(uintptr_t)VM_KERNEL_ADDRHASH((int*)((map)->pmap->ledger)+(ledger_id)))
1094
1095 #if DEVELOPMENT || DEBUG
1096
1097 extern int vm_map_disconnect_page_mappings(
1098 vm_map_t map,
1099 boolean_t);
1100
1101 extern kern_return_t vm_map_inject_error(vm_map_t map, vm_map_offset_t vaddr);
1102
1103 extern kern_return_t vm_map_entries_foreach(vm_map_t map, kern_return_t (^count_handler)(int nentries),
1104 kern_return_t (^entry_handler)(void* entry));
1105 extern kern_return_t vm_map_dump_entry_and_compressor_pager(void* entry, char *buf, size_t *count);
1106
1107 #endif /* DEVELOPMENT || DEBUG */
1108
1109 boolean_t kdp_vm_map_is_acquired_exclusive(vm_map_t map);
1110
1111 boolean_t vm_map_entry_has_device_pager(vm_map_t, vm_map_offset_t vaddr);
1112
1113
1114 #ifdef VM_SCAN_FOR_SHADOW_CHAIN
1115 int vm_map_shadow_max(vm_map_t map);
1116 #endif
1117 __END_DECLS
1118
1119 #endif /* XNU_KERNEL_PRIVATE */
1120 #endif /* _VM_VM_MAP_XNU_H_ */
1121