Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
 * Copyright (c) 2000-2021 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */
/*
 * @OSF_COPYRIGHT@
 */
/*
 * Mach Operating System
 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */
/*
 */

/*
 *	File:	vm/vm_map.h
 *	Author:	Avadis Tevanian, Jr., Michael Wayne Young
 *	Date:	1985
 *
 *	Virtual memory map module definitions.
 *
 * Contributors:
 *	avie, dlb, mwyoung
 */

#ifndef _VM_VM_MAP_H_
#define _VM_VM_MAP_H_

#include <sys/cdefs.h>

#include <mach/mach_types.h>
#include <mach/kern_return.h>
#include <mach/boolean.h>
#include <mach/vm_types.h>
#include <mach/vm_prot.h>
#include <mach/vm_inherit.h>
#include <mach/vm_behavior.h>
#include <mach/vm_param.h>
#include <mach/sdt.h>
#include <vm/pmap.h>
#include <os/overflow.h>
#ifdef XNU_KERNEL_PRIVATE
#include <vm/vm_protos.h>
#endif /* XNU_KERNEL_PRIVATE */
#ifdef  MACH_KERNEL_PRIVATE
#include <mach_assert.h>
#include <vm/vm_map_store_internal.h>
#include <vm/vm_object_xnu.h>
#include <vm/vm_page.h>
#include <kern/locks.h>
#include <kern/zalloc.h>
#include <kern/macro_help.h>

#include <kern/thread.h>
#include <os/refcnt.h>
#endif /* MACH_KERNEL_PRIVATE */


__BEGIN_DECLS

#ifdef KERNEL_PRIVATE

#pragma mark - VM map basics

#ifndef XNU_KERNEL_PRIVATE
/*!
 * @function vm_map_create()
 *
 * @brief
 * Creates an empty VM map.
 *
 * @discussion
 * A VM map represents an address space or sub-address space that is managed
 * by the Mach VM.
 *
 * It consists of a complex data structure which represents all the VM regions
 * as configured by the address space client, which supports fast lookup
 * by address or range.
 *
 * In the Mach VM, the VM map is the source of truth for all the configuration
 * of regions, and the machine dependent physical map layer is used as a cache
 * of that information.
 *
 * Most of the kernel clients never have to make VM maps themselves
 * and will instead interact with:
 * - the @c current_map() (or a given task map),
 * - the @c kernel_map (which is the kernel's own map) or one of its submaps.
 *
 * @param pmap          the physical map to associated with this map
 * @param min_off       the lower address bound of this map
 * @param max_off       the upper address bound of this map
 * @param pageable      unused
 */
extern vm_map_t         vm_map_create(
	pmap_t                  pmap,
	vm_map_offset_t         min_off,
	vm_map_offset_t         max_off,
	boolean_t               pageable);
#endif /* !defined(XNU_KERNEL_PRIVATE) */


/*!
 * @function vm_map_deallocate()
 *
 * @brief
 * Deallocates a VM map.
 *
 * @discussion
 * VM maps are refcounted objects, however most clients will instead
 * hold references to the owning task, or manipulate them via Mach ports.
 *
 * @param map           the map to deallocate.
 */
extern void             vm_map_deallocate(
	vm_map_t                map);


/*!
 * @function vm_map_page_shift()
 *
 * @brief
 * Returns the page shift for a given map.
 *
 * @param map           the specified map
 * @returns             the page shift for this map
 */
extern int              vm_map_page_shift(
	vm_map_t                map) __pure2;


/*!
 * @function vm_map_page_mask()
 *
 * @brief
 * Returns the page mask for a given map.
 *
 * @discussion
 * This is equivalent to @c ((1ull << vm_page_shift(mask)) - 1).
 *
 * @param map           the specified map
 * @returns             the page mask for this map
 */
extern vm_map_offset_t  vm_map_page_mask(
	vm_map_t                map) __pure2;


/*!
 * @function vm_map_page_size()
 *
 * @brief
 * Returns the page size for a given map.
 *
 * @discussion
 * This is equivalent to @c ((1 << vm_page_shift(mask)))
 *
 * @param map           the specified map
 * @returns             the page size for this map
 */
extern int              vm_map_page_size(
	vm_map_t                map) __pure2;


/*!
 * @function vm_map_round_page()
 *
 * @brief
 * Rounds up a given address to the next page boundary for a given page mask.
 *
 * @discussion
 * @warning
 * This function doesn't check for overflow,
 * clients are expected to verify the returned value wasn't 0.
 *
 * @param offset        the address to round to a page boundary
 * @param mask          the page mask to use for the operation
 * @returns             @c offset rounded up to the next page boundary
 */
#define vm_map_round_page(x, pgmask) \
	(((vm_map_offset_t)(x) + (pgmask)) & ~((signed)(pgmask)))

/*!
 * @function vm_map_round_page_mask()
 *
 * @brief
 * Rounds up a given address to the next page boundary for a given page mask.
 *
 * @discussion
 * This is equivalent to @c vm_map_round_page(offset, mask)
 *
 * @warning
 * This function doesn't check for overflow,
 * clients are expected to verify the returned value wasn't 0.
 *
 * @param offset        the address to round to a page boundary
 * @param mask          the page mask to use for the operation
 * @returns             @c offset rounded up to the next page boundary
 */
extern vm_map_offset_t  vm_map_round_page_mask(
	vm_map_offset_t         offset,
	vm_map_offset_t         mask) __pure2;


/*!
 * @function vm_map_trunc_page()
 *
 * @brief
 * Truncates a given address to the previous page boundary for a given page mask.
 *
 * @discussion
 * This is equivalent to @c vm_map_trunc_page(offset, mask)
 *
 * @param offset        the address to truncate to a page boundary
 * @param mask          the page mask to use for the operation
 * @returns             @c offset truncated to the previous page boundary
 */
#define vm_map_trunc_page(offset, pgmask) \
	((vm_map_offset_t)(offset) & ~((signed)(pgmask)))

/*!
 * @function vm_map_trunc_page_mask()
 *
 * @brief
 * Truncates a given address to the previous page boundary for a given page mask.
 *
 * @discussion
 * This is equivalent to @c vm_map_trunc_page(offset, mask)
 *
 * @param offset        the address to truncate to a page boundary
 * @param mask          the page mask to use for the operation
 * @returns             @c offset truncated to the previous page boundary
 */
extern vm_map_offset_t  vm_map_trunc_page_mask(
	vm_map_offset_t         offset,
	vm_map_offset_t         mask) __pure2;


/*!
 * Obsolete
 * @param map           the map to disable hole list for.
 */
extern void vm_map_disable_hole_optimization(
	vm_map_t                map);

#ifdef MACH_KERNEL_PRIVATE

#pragma mark - MIG helpers
#pragma GCC visibility push(hidden)

/*!
 * @function convert_port_entry_to_map()
 *
 * @brief
 * MIG intran for the @c vm_task_entry_t type, do not use directly.
 */
extern vm_map_t         convert_port_entry_to_map(
	ipc_port_t              port) __exported;

/*!
 * @function vm_map_inspect_deallocate()
 *
 * @brief
 * MIG destructor function for the @c vm_map_inspect_t type,
 * do not use directly.
 */
extern void             vm_map_inspect_deallocate(
	vm_map_inspect_t        map);


/*!
 * @function vm_map_read_deallocate()
 *
 * @brief
 * MIG destructor function for the @c vm_map_read_t type,
 * do not use directly.
 */
extern void             vm_map_read_deallocate(
	vm_map_read_t           map);

#pragma GCC visibility pop
#endif /* MACH_KERNEL_PRIVATE */

#pragma mark - vm map wiring
#if !XNU_KERNEL_PRIVATE

/*!
 * @function vm_map_wire()
 *
 * @brief
 * Sets the pageability of the specified address range in the
 * target map as wired.
 *
 * @discussion
 * Regions specified as not pageable require locked-down physical memory
 * and physical page maps.
 *
 * The prot_u variable indicates types of accesses that must not
 * generate page faults. This is checked against protection of memory
 * being locked-down.
 *
 * The map must not be locked, but a reference must remain
 * to the map throughout the call.
 *
 *
 * @param map           the target VM map (the call will recurse in submaps).
 * @param start_u       the lower bound of the address range to wire
 * @param end_u         the upper bound of the address range to wire
 * @param prot_u        the access for which to perform the wiring
 * @param user_wire     whether the wiring is on behalf of userspace.
 *                      userspace wiring is equivalent to an mlock() call from
 *                      userspace and will be undone at process death unlike
 *                      kernel wiring which must always be undone explicitly.
 *
 * @returns
 * - KERN_SUCCESS       the operation was successful
 * - KERN_INVALID_ARGUMENT
 *                      @c [start_u, end_u) didn't form a valid region
 * - KERN_RESOURCE_SHORTAGE
 *                      the kernel was out of physical memory to perform
 *                      the operation.
 * - KERN_INVALID_ADDRESS
 *                      some address in the range wasn't mapped.
 * - KERN_PROTECTION_FAILURE
 *                      the region doesn't support wiring for this access.
 * - KERN_MEMORY_ERROR  faulting failed.
 * - MACH_SEND_INTERRUPTED
 *                      a signal was received during the wiring
 *
 * User wirings:
 * - KERN_FAILURE       the process was terminated during the wiring.
 * - KERN_FAILURE       the user wire counts would overflow @c MAX_WIRE_COUNT
 *                      for this region.
 * - KERN_RESOURCE_SHORTAGE
 *                      the process would overflow its user wiring limits.
 */
extern kern_return_t    vm_map_wire(
	vm_map_t                map,
	vm_map_offset_ut        start_u,
	vm_map_offset_ut        end_u,
	vm_prot_ut              prot_u,
	boolean_t               user_wire);

#endif /* !XNU_KERNEL_PRIVATE */

/*!
 * @function vm_map_unwire()
 *
 * @brief
 * Sets the pageability of the specified address range in the target
 * as pageable.
 *
 * @discussion
 * Regions specified must have been wired previously.
 *
 * The map must not be locked, but a reference must remain to the map
 * throughout the call.
 *
 * User unwire ignores holes and unwired and intransition entries to avoid
 * losing memory by leaving it unwired. Kernel unwires will panic on failures.
 *
 *
 * @param map           the target VM map (the call will recurse in submaps).
 * @param start_u       the lower bound of the address range to wire
 * @param end_u         the upper bound of the address range to wire
 * @param user_wire     whether the wiring is on behalf of userspace.
 */
extern kern_return_t    vm_map_unwire(
	vm_map_t                map,
	vm_map_offset_ut        start_u,
	vm_map_offset_ut        end_u,
	boolean_t               user_wire);


#if XNU_PLATFORM_MacOSX

/*!
 * @function vm_map_wire_and_extract()
 *
 * @brief
 * Sets the pageability of the specified page in the target map,
 * and returns the resulting physical page number for it.
 *
 * @discussion
 * This function should not be called by kernel extensions and is only here
 * for backward compatibility of macOS kernels.
 *
 *
 * @param map           the target VM map (the call will recurse in submaps).
 * @param address       the address of the page to wire
 * @param access_type   the access for which to perform the wiring
 * @param user_wire     whether the wiring is on behalf of userspace.
 *                      userspace wiring is equivalent to an mlock() call from
 *                      userspace and will be undone at process death unlike
 *                      kernel wiring which must always be undone explicitly.
 * @param physpage_p    a pointer filled with the page number for the wired down
 *                      physical page, or 0 in case of failure.
 *
 * @returns             @c KERN_SUCCESS or an error denoting the reason for
 *                      failure.
 */
extern kern_return_t    vm_map_wire_and_extract(
	vm_map_t                map,
	vm_map_offset_ut        address,
	vm_prot_ut              access_type,
	boolean_t               user_wire,
	ppnum_t                *physpage_p);

#endif /* XNU_PLATFORM_MacOSX */

#pragma mark - vm map copy

/*!
 * @const VM_MAP_COPY_OVERWRITE_OPTIMIZATION_THRESHOLD_PAGES
 * Number of pages under which the VM will copy by content rather
 * than trying to do a copy-on-write mapping to form a vm_map_copy_t.
 *
 * Note: this constant has unfortunately been exposed historically
 *       but should not be considered ABI.
 */
#define VM_MAP_COPY_OVERWRITE_OPTIMIZATION_THRESHOLD_PAGES      (3)


/*!
 * @function vm_map_copyin()
 *
 * @brief
 * Copy the specified region from the source address space.
 *
 * @description
 * The source map should not be locked on entry.
 *
 *
 * @param [in] src_map       the source address space to copy from.
 * @param [in] src_addr      the address at which to start copying memory.
 * @param [in] len           the size of the region to copy.
 * @param [in] src_destroy   whether the copy also removes the region
 *                           from the source address space.
 * @param [out] copy_result  the out parameter, to be filled with the created
 *                           vm map copy on success.
 * @returns
 * - KERN_SUCCESS       the operation was successful
 * - KERN_INVALID_ARGUMENT
 *                      @c (src_addr, len) didn't form a valid region
 * - KERN_RESOURCE_SHORTAGE
 *                      the kernel was out of physical memory to perform
 *                      the operation.
 * - KERN_INVALID_ADDRESS
 *                      some address in the range wasn't mapped
 * - KERN_PROTECTION_FAILURE
 *                      the region isn't readable (it doesn't have
 *                      @c VM_PROT_READ set).
 * - KERN_PROTECTION_FAILURE
 *                      the memory range contains a physically contiguous
 *                      object
 * - KERN_MEMORY_ERROR  faulting failed.
 * - MACH_SEND_INTERRUPTED
 *                      a signal was received during the copy
 *
 */
extern kern_return_t    vm_map_copyin(
	vm_map_t                src_map,
	vm_map_address_ut       src_addr,
	vm_map_size_ut          len,
	boolean_t               src_destroy,
	vm_map_copy_t          *copy_result); /* OUT */


/*!
 * @function vm_map_copyout()
 *
 * @brief
 * Place a VM map copy made with @c vm_map_copyin() into a destination map.
 *
 * @description
 * The specified VM map copy is consumed on success,
 * otherwise the caller is responsible for it.
 * @see @c vm_map_copy_discard below.
 *
 * @param [in]  dst_map the destination address space to insert into.
 * @param [out] addr    the address at which the data was inserted.
 * @param [in]  copy    the VM map copy to place.
 * @returns
 * - KERN_SUCCESS       the operation succeeded, @c copy has been consumed.
 * - KERN_NO_SPACE      the destination map was out of address space.
 * - KERN_NOT_SUPPORTED the vm map copy can't be mapped in this address space.
 *                      This can for example happen for certain cases of a VM
 *                      map copy using a 4k page size into a space that is 16k
 *                      aligned, requiring different physical pages within the
 *                      same 16k page boundary.
 */
extern kern_return_t    vm_map_copyout(
	vm_map_t                dst_map,
	vm_map_address_t       *addr, /* OUT */
	vm_map_copy_t           copy);

/*!
 * @function vm_map_copy_discard()
 *
 * @brief
 * Dispose of a @c vm_map_copy_t object made by @c vm_map_copyin().
 *
 * @description
 * VM map copies are typically placed in an address space using
 * @c vm_map_copyout(), but when that has not happened, this function must be
 * used to dispose of it.
 *
 * @param copy          the VM map copy object to dispose of.
 */
extern void             vm_map_copy_discard(
	vm_map_copy_t           copy);


/**
 * @function vm_map_kernel_max_simple_mappable_size()
 *
 * @brief
 * Get the size of the largest contiguous mapping which can be used in the
 * kernel without special accessors/attributes. When using accessors/attributes
 * in XNU proper, this limit can be overridden when making allocations/mappings
 * through various APIs by setting the "no soft limit" option. Such
 * functionalities, however, are not available outside of XNU.
 *
 * Note that this function does not guarantee that the returned size will
 * actually be mappable. Rather, this function specifies that simple kernel
 * mappings larger than this will fail.
 */
extern vm_size_t vm_map_kernel_max_simple_mappable_size(void);

#endif  /* KERNEL_PRIVATE */

__END_DECLS

#endif  /* _VM_VM_MAP_H_ */