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
/*
 * Copyright (c) 2000-2006 Apple Computer, 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_kern.h
 *	Author:	Avadis Tevanian, Jr., Michael Wayne Young
 *	Date:	1985
 *
 *	Kernel memory management definitions.
 */

#ifndef _VM_VM_KERN_H_
#define _VM_VM_KERN_H_

#include <mach/mach_types.h>
#include <mach/boolean.h>
#include <mach/kern_return.h>
#include <mach/vm_types.h>
#ifdef XNU_KERNEL_PRIVATE
#include <kern/locks.h>
#endif /* XNU_KERNEL_PRIVATE */


__BEGIN_DECLS

#ifdef KERNEL_PRIVATE

/*!
 * @brief
 * The VM map for the kernel.
 *
 * @discussion
 * This represents the VM-managed portion of the address space.
 *
 * The actual address space of the kernel is larger but is managed
 * by the pmap directly and the VM is oblivious to it. Unmanaged regions
 * of that kind include the physical aperture or the KASAN shadow map.
 */
extern vm_map_t kernel_map;


/*!
 * @brief
 * The IPC VM submap.
 *
 * @discussion
 * The IPC submap is used by the Mach IPC subsystem in order to stage
 * allocations for IPC kmsgs or to throttle debugging interfaces.
 *
 * This submap doesn't zero on page fault, and clients must properly
 * erase memory or risk memory disclosures.
 */
extern vm_map_t ipc_kernel_map;

#if XNU_KERNEL_PRIVATE

/*!
 * @brief
 * The Kext VM submap
 *
 * @discussion
 * This submap is used to support unloading and paging out kexts.
 */
extern vm_map_t g_kext_map __XNU_PRIVATE_EXTERN;

#else

#pragma mark - the kmem subsystem

/*!
 * @function kmem_alloc()
 *
 * @brief
 * Allocate anonymous wired memory from the kernel map or a kernel submap.
 *
 * @discussion
 * The memory allocated is wired and must be deallocated with @c kmem_free()
 * or @c mach_vm_deallocate().
 *
 * Kernel extensions are discouraged from using this function:
 * consider @c IOMallocType() instead.
 *
 * Per kernel allocation security policies (see doc/allocators/api-basics.md),
 * this allocation cannot be used to store pure data, @c IOMallocData()
 * must be used instead.
 *
 * @param [in]  map     the map to allocate from, this must be the kernel map
 *                      or one of its submaps.
 * @param [out] addrp   a non-NULL pointer used to return the newly allocated
 *                      memory.
 * @param [in]  size    the size of the memory to allocate.
 *
 * @returns
 * KERN_SUCCESS         the allocation succeeded,
 *                      the returned address will be non-zero.
 * KERN_INVALID_ARGUMENT
 *                      the allocation failed because @c size was 0.
 * KERN_NO_SPACE        the allocation failed because the specified map
 *                      is out of address space.
 * KERN_RESOURCE_SHORTAGE
 *                      the allocation failed because the kernel
 *                      was out of pages and couldn't satisfy the demand.
 */
extern kern_return_t kmem_alloc(
	vm_map_t                map,
	vm_offset_t            *addrp,
	vm_size_t               size);


/*!
 * @function kmem_alloc_pageable()
 *
 * @brief
 * Allocate anonymous pageable memory from the kernel map or a kernel submap.
 *
 * @discussion
 * This call is equivalent to @c mach_vm_allocate(map, addr, size, VM_FLAGS_ANYWHERE)
 * which should be preferred to this legacy call.
 *
 * The memory allocated is wired and must be deallocated with @c kmem_free()
 * or @c mach_vm_deallocate().
 *
 * Per kernel allocation security policies (see doc/allocators/api-basics.md),
 * this allocation must not be used to store kernel pointers.
 *
 * @param [in]  map     the map to allocate from, this must be the kernel map
 *                      or one of its submaps.
 * @param [out] addrp   a non-NULL pointer used to return the newly allocated
 *                      memory.
 * @param [in]  size    the size of the memory to allocate.
 *
 * @returns
 * KERN_SUCCESS         the allocation succeeded,
 *                      the returned address will be non-zero.
 * KERN_NO_SPACE        the allocation failed because the specified map
 *                      is out of address space.
 */
extern kern_return_t kmem_alloc_pageable(
	vm_map_t                map,
	vm_offset_t            *addrp,
	vm_size_t               size);


/*!
 * @function kmem_alloc_kobject()
 *
 * @brief
 * Allocate kobject wired memory from the kernel map or a kernel submap.
 *
 * @discussion
 * The memory allocated is wired and must be deallocated with @c kmem_free()
 * or @c mach_vm_deallocate().
 *
 * Memory allocated by this function is added to the VM kernel object rather
 * than a new VM object. This makes it possible to avoid the cost of that extra
 * VM object, but forgoes any advanced VM features such as unwiring memory, or
 * sharing it (whether it be to an IOMMU or another address space).
 *
 * Kernel extensions are discouraged from using this function:
 * consider @c IOMallocType() instead.
 *
 * Per kernel allocation security policies (see doc/allocators/api-basics.md),
 * this allocation cannot be used to store pure data, @c IOMallocData()
 * must be used instead.
 *
 * @param [in]  map     the map to allocate from, this must be the kernel map
 *                      or one of its submaps.
 * @param [out] addrp   a non-NULL pointer used to return the newly allocated
 *                      memory.
 * @param [in]  size    the size of the memory to allocate.
 *
 * @returns
 * KERN_SUCCESS         the allocation succeeded,
 *                      the returned address will be non-zero.
 * KERN_INVALID_ARGUMENT
 *                      the allocation failed because @c size was 0.
 * KERN_NO_SPACE        the allocation failed because the specified map
 *                      is out of address space.
 * KERN_RESOURCE_SHORTAGE
 *                      the allocation failed because the kernel
 *                      was out of pages and couldn't satisfy the demand.
 */
extern kern_return_t kmem_alloc_kobject(
	vm_map_t                map,
	vm_offset_t            *addrp,
	vm_size_t               size);

/*!
 * @function kmem_free()
 *
 * @brief
 * Deallocates a range of memory.
 *
 * @discussion
 * This call is roughly equivalent to @c mach_vm_deallocate(map, addr, size).
 *
 * It is possible to deallocate an allocation in several steps provided that
 * the deallocations form a partition of the range allocated with one of
 * the functions from the @c kmem_alloc*() family.
 *
 * Unlike @c mach_vm_deallocate(), this function will panic for invalid
 * arguments, in particular for invalid sizes or a @c map argument
 * not matching the one used for allocating.
 *
 * @param map           the map to allocate from, this must be the kernel map
 *                      or one of its submaps.
 * @param addr          the address to deallocate.
 * @param size          the size of the address to deallocate.
 */
extern void kmem_free(
	vm_map_t                map,
	vm_offset_t             addr,
	vm_size_t               size);

#endif /* !XNU_KERNEL_PRIVATE */
#endif /* KERNEL_PRIVATE */

#pragma mark - kernel address obfuscation / hashing for logging

/*!
 * @function vm_kernel_addrhide()
 *
 * @brief
 * Unslides a kernel pointer.
 *
 * @discussion
 * This is exporting the VM_KERNEL_ADDRHIDE() functionality to kernel
 * extensions.
 *
 * @param addr          the kernel address to unslide
 * @param hide_addr     the unslid value of @c addr if it was part of a slid
 *                      region of the kernel.
 *
 *                      0 on release kernels if @c addr is not part of a slid
 *                      region of the kernel.
 *
 *                      @c addr on development kernels if @c addr is not part of
 *                      a slid region of the kernel.
 */
extern void vm_kernel_addrhide(
	vm_offset_t             addr,
	vm_offset_t            *hide_addr);


/*!
 * @function vm_kernel_addrperm_external()
 *
 * @brief
 * Unslides or "permutate" a kernel pointer.
 *
 * @discussion
 * This is exporting the VM_KERNEL_ADDRPERM() functionality to kernel
 * extensions.
 *
 * The level of "hiding" of heap kernel pointers done by this function is
 * insufficient. Using @c vm_kernel_addrhash() is preferred when possible.
 *
 * Note that this function might cause lazy allocation to preserve the floating
 * point register state on Intel and is generally unsafe to call under lock.
 *
 * @param addr          the kernel address to unslide
 * @param perm_addr     the unslid value of @c addr if it was part of a slid
 *                      region of the kernel.
 */
extern void vm_kernel_addrperm_external(
	vm_offset_t             addr,
	vm_offset_t            *perm_addr);


/*!
 * @function vm_kernel_unslide_or_perm_external()
 *
 * @brief
 * Equivalent to vm_kernel_addrperm_external().
 */
extern void vm_kernel_unslide_or_perm_external(
	vm_offset_t             addr,
	vm_offset_t            *perm_addr);

#if !XNU_KERNEL_PRIVATE

/*!
 * @function vm_kernel_addrhash()
 *
 * @brief
 * Unslides or hashes a kernel pointer.
 *
 * @discussion
 * This is exporting the VM_KERNEL_ADDRHASH() functionality to kernel
 * extensions.
 *
 * @param addr          the kernel address to unslide
 * @returns             the unslid value of @c addr if it was part of a slid
 *                      region of the kernel.
 *
 *                      a hashed value of @c addr otherwise.
 */
extern vm_offset_t vm_kernel_addrhash(
	vm_offset_t             addr);

#else /* XNU_KERNEL_PRIVATE */
__exported_push_hidden

/*!
 * @brief
 * The quantity @c vm_kernel_addrhide() uses to slide heap pointers.
 */
extern vm_offset_t vm_kernel_addrperm_ext;


/*!
 * @brief
 * The quantity @c vm_kernel_addrhash() uses to hash heap pointers inside XNU.
 */
extern uint64_t vm_kernel_addrhash_salt;


/*!
 * @brief
 * The quantity @c vm_kernel_addrhash() uses to hash heap pointers for kernel
 * extensions.
 */
extern uint64_t vm_kernel_addrhash_salt_ext;


/*!
 * @function vm_kernel_addrhash_internal()
 *
 * @brief
 * Internal function used to implement the @c vm_kernel_addrhash*() functions.
 */
extern vm_offset_t vm_kernel_addrhash_internal(
	vm_offset_t             addr,
	uint64_t                salt);


/*!
 * @function vm_kernel_addrhash()
 *
 * @brief
 * Unslides or hashes a kernel pointer.
 *
 * @discussion
 * This is exporting the VM_KERNEL_ADDRHASH() functionality to kernel
 * extensions.
 *
 * @param addr          the kernel address to unslide
 * @returns             the unslid value of @c addr if it was part of a slid
 *                      region of the kernel.
 *
 *                      a hashed value of @c addr otherwise.
 */
static inline vm_offset_t
vm_kernel_addrhash(vm_offset_t addr)
{
	return vm_kernel_addrhash_internal(addr, vm_kernel_addrhash_salt);
}

__exported_pop
#endif /* XNU_KERNEL_PRIVATE */
#ifdef KERNEL_PRIVATE

#pragma mark - kern allocation names

/*!
 * @typedef kern_allocation_name_t
 *
 * @brief
 * This type is used to perform different kinds of accounting
 * in the Mach VM subsystem.
 */
#ifdef XNU_KERNEL_PRIVATE
typedef struct vm_allocation_site       kern_allocation_name;
typedef kern_allocation_name           *kern_allocation_name_t;
#else
typedef struct kern_allocation_name    *kern_allocation_name_t;
#endif


/*!
 * @brief
 * Allocate a kernel allocation accounting structure.
 *
 * @param name          a symbolic name for this accounting group.
 * @param suballocs     how many subtotals will be used for accounting.
 *                      see @c kern_allocation_update_subtotal().
 * @returns             the new allocated accounting structure,
 *                      this function never fails.
 */
extern kern_allocation_name_t kern_allocation_name_allocate(
	const char             *name,
	uint16_t                suballocs);

/*!
 * @brief
 * Frees a kernel allocation accounting structure.
 *
 * @param allocation    a structure made with @c kern_allocation_name_allocate().
 */
extern void kern_allocation_name_release(
	kern_allocation_name_t  allocation);


/*!
 * @brief
 * Returns the name associated with an allocation accounting structure.
 *
 * @returns             the name associated with that accounting structure,
 *                      when made with @c kern_allocation_name_allocate().
 */
extern const char *kern_allocation_get_name(
	kern_allocation_name_t  allocation);

#endif  /* KERNEL_PRIVATE */

__END_DECLS

#endif  /* _VM_VM_KERN_H_ */