1 /*
2 * Copyright (c) 2000-2020 Apple Computer, 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 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or [email protected]
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: mach/vm_param.h
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
61 * Date: 1985
62 *
63 * Machine independent virtual memory parameters.
64 *
65 */
66
67 #ifndef _MACH_VM_PARAM_H_
68 #define _MACH_VM_PARAM_H_
69
70 #include <mach/machine/vm_param.h>
71
72 #ifdef KERNEL
73
74 #ifndef ASSEMBLER
75 #include <mach/vm_types.h>
76 #endif /* ASSEMBLER */
77
78 #include <os/base.h>
79 #include <os/overflow.h>
80
81 /*
82 * The machine independent pages are refered to as PAGES. A page
83 * is some number of hardware pages, depending on the target machine.
84 */
85
86 #ifndef ASSEMBLER
87
88 #define PAGE_SIZE_64 (unsigned long long)PAGE_SIZE /* pagesize in addr units */
89 #define PAGE_MASK_64 (unsigned long long)PAGE_MASK /* mask for off in page */
90
91 /*
92 * Convert addresses to pages and vice versa. No rounding is used.
93 * The atop_32 and ptoa_32 macros should not be use on 64 bit types.
94 * The round_page_64 and trunc_page_64 macros should be used instead.
95 */
96
97 #define atop_32(x) ((uint32_t)(x) >> PAGE_SHIFT)
98 #define ptoa_32(x) ((uint32_t)(x) << PAGE_SHIFT)
99 #define atop_64(x) ((uint64_t)(x) >> PAGE_SHIFT)
100 #define ptoa_64(x) ((uint64_t)(x) << PAGE_SHIFT)
101
102 #define atop_kernel(x) ((vm_address_t)(x) >> PAGE_SHIFT)
103 #define ptoa_kernel(x) ((vm_address_t)(x) << PAGE_SHIFT)
104
105 /*
106 * While the following block is enabled, the legacy atop and ptoa
107 * macros will behave correctly. If not, they will generate
108 * invalid lvalue errors.
109 */
110
111 #if 1
112 #define atop(x) ((vm_address_t)(x) >> PAGE_SHIFT)
113 #define ptoa(x) ((vm_address_t)(x) << PAGE_SHIFT)
114 #else
115 #define atop(x) (0UL = 0)
116 #define ptoa(x) (0UL = 0)
117 #endif
118
119 /*
120 * Page-size rounding macros for the Public fixed-width VM types.
121 */
122 #define mach_vm_round_page(x) (((mach_vm_offset_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
123 #define mach_vm_trunc_page(x) ((mach_vm_offset_t)(x) & ~((signed)PAGE_MASK))
124
125 #define round_page_overflow(in, out) __os_warn_unused(({ \
126 bool __ovr = os_add_overflow(in, (__typeof__(*out))PAGE_MASK, out); \
127 *out &= ~((__typeof__(*out))PAGE_MASK); \
128 __ovr; \
129 }))
130
131 static inline int OS_WARN_RESULT
mach_vm_round_page_overflow(mach_vm_offset_t in,mach_vm_offset_t * out)132 mach_vm_round_page_overflow(mach_vm_offset_t in, mach_vm_offset_t *out)
133 {
134 return round_page_overflow(in, out);
135 }
136
137 #define memory_object_round_page(x) (((memory_object_offset_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
138 #define memory_object_trunc_page(x) ((memory_object_offset_t)(x) & ~((signed)PAGE_MASK))
139
140 /*
141 * Rounding macros for the legacy (scalable with the current task's
142 * address space size) VM types.
143 */
144
145 #define round_page(x) (((vm_offset_t)(x) + PAGE_MASK) & ~((vm_offset_t)PAGE_MASK))
146 #define trunc_page(x) ((vm_offset_t)(x) & ~((vm_offset_t)PAGE_MASK))
147
148 /*
149 * Round off or truncate to the nearest page. These will work
150 * for either addresses or counts. (i.e. 1 byte rounds to 1 page
151 * bytes. The round_page_32 and trunc_page_32 macros should not be
152 * use on 64 bit types. The round_page_64 and trunc_page_64 macros
153 * should be used instead.
154 *
155 * These should only be used in the rare case the size of the address
156 * or length is hard-coded as 32 or 64 bit. Otherwise, the macros
157 * associated with the specific VM type should be used.
158 */
159
160 #define round_page_32(x) (((uint32_t)(x) + PAGE_MASK) & ~((uint32_t)PAGE_MASK))
161 #define trunc_page_32(x) ((uint32_t)(x) & ~((uint32_t)PAGE_MASK))
162 #define round_page_64(x) (((uint64_t)(x) + PAGE_MASK_64) & ~((uint64_t)PAGE_MASK_64))
163 #define trunc_page_64(x) ((uint64_t)(x) & ~((uint64_t)PAGE_MASK_64))
164
165 #define round_page_mask_32(x, mask) (((uint32_t)(x) + (mask)) & ~((uint32_t)(mask)))
166 #define trunc_page_mask_32(x, mask) ((uint32_t)(x) & ~((uint32_t)(mask)))
167 #define round_page_mask_64(x, mask) (((uint64_t)(x) + (mask)) & ~((uint64_t)(mask)))
168 #define trunc_page_mask_64(x, mask) ((uint64_t)(x) & ~((uint64_t)(mask)))
169
170 /*
171 * Enable the following block to find uses of xxx_32 macros that should
172 * be xxx_64. These macros only work in C code, not C++. The resulting
173 * binaries are not functional. Look for invalid lvalue errors in
174 * the compiler output.
175 *
176 * Enabling the following block will also find use of the xxx_64 macros
177 * that have been passed pointers. The parameters should be case to an
178 * unsigned long type first. Look for invalid operands to binary + error
179 * in the compiler output.
180 */
181
182 #if 0
183 #undef atop_32
184 #undef ptoa_32
185 #undef round_page_32
186 #undef trunc_page_32
187 #undef atop_64
188 #undef ptoa_64
189 #undef round_page_64
190 #undef trunc_page_64
191
192 #ifndef __cplusplus
193
194 #define atop_32(x) \
195 (__builtin_choose_expr (sizeof(x) != sizeof(uint64_t), \
196 (*(long *)0), \
197 (0UL)) = 0)
198
199 #define ptoa_32(x) \
200 (__builtin_choose_expr (sizeof(x) != sizeof(uint64_t), \
201 (*(long *)0), \
202 (0UL)) = 0)
203
204 #define round_page_32(x) \
205 (__builtin_choose_expr (sizeof(x) != sizeof(uint64_t), \
206 (*(long *)0), \
207 (0UL)) = 0)
208
209 #define trunc_page_32(x) \
210 (__builtin_choose_expr (sizeof(x) != sizeof(uint64_t), \
211 (*(long *)0), \
212 (0UL)) = 0)
213 #else
214
215 #define atop_32(x) (0)
216 #define ptoa_32(x) (0)
217 #define round_page_32(x) (0)
218 #define trunc_page_32(x) (0)
219
220 #endif /* ! __cplusplus */
221
222 #define atop_64(x) ((uint64_t)((x) + (uint8_t *)0))
223 #define ptoa_64(x) ((uint64_t)((x) + (uint8_t *)0))
224 #define round_page_64(x) ((uint64_t)((x) + (uint8_t *)0))
225 #define trunc_page_64(x) ((uint64_t)((x) + (uint8_t *)0))
226
227 #endif
228
229 /*
230 * Determine whether an address is page-aligned, or a count is
231 * an exact page multiple.
232 */
233
234 #define page_aligned(x) (((x) & PAGE_MASK) == 0)
235
236 extern vm_size_t mem_size; /* 32-bit size of memory - limited by maxmem - deprecated */
237 extern uint64_t max_mem; /* 64-bit size of memory - limited by maxmem */
238
239 /*
240 * The VM compressor pager uses 32-bit page numbers, so this limits the size
241 * of anonymous memory objects to 0xffffffff pages.
242 * When we need to allocate a chunk of anonymous memory over that size,
243 * we have to allocate more than one chunk.
244 */
245 #define ANON_MAX_PAGES 0xFFFFFFFFULL
246 #define ANON_MAX_SIZE (ANON_MAX_PAGES << PAGE_SHIFT)
247 /*
248 * Work-around for <rdar://problem/6626493>
249 * Break large anonymous memory areas into 128MB chunks to alleviate
250 * the cost of copying when copy-on-write is not possible because a small
251 * portion of it being wired.
252 */
253 #define ANON_CHUNK_SIZE (128ULL * 1024 * 1024) /* 128MB */
254
255 /*
256 * The 'medium' malloc allocator would like its regions
257 * to be chunked up into MALLOC_MEDIUM_CHUNK_SIZE chunks
258 * and backed by different objects. This avoids contention
259 * on a single large object and showed solid improvements on high
260 * core machines with workloads involving video and graphics processing.
261 */
262 #define MALLOC_MEDIUM_CHUNK_SIZE (8ULL * 1024 * 1024) /* 8 MB */
263
264 #ifdef KERNEL_PRIVATE
265 extern uint64_t sane_size; /* Memory size to use for defaults calculations */
266 #endif /* KERNEL_PRIVATE */
267
268 #ifdef XNU_KERNEL_PRIVATE
269
270 #include <kern/debug.h>
271
272 extern uint64_t mem_actual; /* 64-bit size of memory - not limited by maxmem */
273 extern uint64_t max_mem_actual; /* Size of physical memory adjusted by maxmem */
274 extern addr64_t vm_last_addr; /* Highest kernel virtual address known to the VM system */
275
276 extern const vm_offset_t vm_min_kernel_address;
277 extern const vm_offset_t vm_max_kernel_address;
278
279 extern vm_offset_t vm_kernel_stext;
280 extern vm_offset_t vm_kernel_etext;
281 extern vm_offset_t vm_kernel_slid_base;
282 extern vm_offset_t vm_kernel_slid_top;
283 extern vm_offset_t vm_kernel_slide;
284
285
286 extern vm_offset_t vm_kernel_addrperm;
287 extern vm_offset_t vm_kext_base;
288 extern vm_offset_t vm_kext_top;
289 extern vm_offset_t vm_kernel_base;
290 extern vm_offset_t vm_kernel_top;
291 extern vm_offset_t vm_hib_base;
292
293 extern vm_offset_t vm_kernel_builtinkmod_text;
294 extern vm_offset_t vm_kernel_builtinkmod_text_end;
295
296 /**
297 * While these function's implementations are machine specific, due to the need
298 * to prevent header file circular dependencies, they need to be externed here
299 * for usage in the sliding/unsliding macros.
300 */
301 __BEGIN_DECLS
302 vm_offset_t ml_static_slide(vm_offset_t vaddr);
303 vm_offset_t ml_static_unslide(vm_offset_t vaddr);
304 __END_DECLS
305
306 /**
307 * Determine whether a given address is an address within a static region (i.e.,
308 * coming from TEXT or DATA) that was slid during boot. Addresses of this type
309 * should have the slide removed before exposing them to userspace so as to not
310 * leak the slide itself to userspace.
311 *
312 * @param addr The virtual address to check.
313 *
314 * @return True if the address is a static/slid kernel address, false otherwise.
315 */
316 static inline bool
vm_is_addr_slid(vm_offset_t addr)317 vm_is_addr_slid(vm_offset_t addr)
318 {
319 const vm_offset_t stripped_addr = (vm_offset_t)VM_KERNEL_STRIP_PTR(addr);
320 const bool is_slid_kern_addr =
321 (stripped_addr >= vm_kernel_slid_base) && (stripped_addr < vm_kernel_slid_top);
322
323 return is_slid_kern_addr;
324 }
325
326 #define VM_KERNEL_IS_SLID(_o) (vm_is_addr_slid((vm_offset_t)(_o)))
327
328 #define VM_KERNEL_SLIDE(_u) (ml_static_slide((vm_offset_t)(_u)))
329
330 /*
331 * The following macros are to be used when exposing kernel addresses to
332 * userspace via any of the various debug or info facilities that might exist
333 * (e.g. stackshot, proc_info syscall, etc.). It is important to understand
334 * the goal of each macro and choose the right one depending on what you are
335 * trying to do. Misuse of these macros can result in critical data leaks
336 * which in turn lead to all sorts of system vulnerabilities. It is invalid to
337 * call these macros on a non-kernel address (NULL is allowed).
338 *
339 * VM_KERNEL_UNSLIDE:
340 * Use this macro when you are exposing an address to userspace which is
341 * *guaranteed* to be a "static" kernel or kext address (i.e. coming from text
342 * or data sections). These are the addresses which get "slid" via ASLR on
343 * kernel or kext load, and it's precisely the slide value we are trying to
344 * protect from userspace.
345 *
346 * VM_KERNEL_ADDRHIDE:
347 * Use when exposing an address for internal purposes: debugging, tracing,
348 * etc. The address will be unslid if necessary. Other addresses will be
349 * hidden on customer builds, and unmodified on internal builds.
350 *
351 * VM_KERNEL_ADDRHASH:
352 * Use this macro when exposing a kernel address to userspace on customer
353 * builds. The address can be from the static kernel or kext regions, or the
354 * kernel heap. The address will be unslid or hashed as appropriate.
355 *
356 *
357 * ** SECURITY WARNING: The following macros can leak kernel secrets.
358 * Use *only* in performance *critical* code.
359 *
360 * VM_KERNEL_ADDRPERM:
361 * VM_KERNEL_UNSLIDE_OR_PERM:
362 * Use these macros when exposing a kernel address to userspace on customer
363 * builds. The address can be from the static kernel or kext regions, or the
364 * kernel heap. The address will be unslid or permuted as appropriate.
365 *
366 * Nesting of these macros should be considered invalid.
367 */
368
369 #define __DO_UNSLIDE(_v) (ml_static_unslide((vm_offset_t)VM_KERNEL_STRIP_PTR(_v)))
370
371 #if DEBUG || DEVELOPMENT
372 #define VM_KERNEL_ADDRHIDE(_v) (VM_KERNEL_IS_SLID(_v) ? __DO_UNSLIDE(_v) : (vm_address_t)VM_KERNEL_STRIP_PTR(_v))
373 #else
374 #define VM_KERNEL_ADDRHIDE(_v) (VM_KERNEL_IS_SLID(_v) ? __DO_UNSLIDE(_v) : (vm_address_t)0)
375 #endif /* DEBUG || DEVELOPMENT */
376
377 #define VM_KERNEL_ADDRHASH(_v) vm_kernel_addrhash((vm_offset_t)(_v))
378
379 #define VM_KERNEL_UNSLIDE_OR_PERM(_v) ({ \
380 VM_KERNEL_IS_SLID(_v) ? __DO_UNSLIDE(_v) : \
381 VM_KERNEL_ADDRESS(_v) ? ((vm_offset_t)VM_KERNEL_STRIP_PTR(_v) + vm_kernel_addrperm) : \
382 (vm_offset_t)VM_KERNEL_STRIP_PTR(_v); \
383 })
384
385 #define VM_KERNEL_UNSLIDE(_v) ({ \
386 VM_KERNEL_IS_SLID(_v) ? __DO_UNSLIDE(_v) : (vm_offset_t)0; \
387 })
388
389 #define VM_KERNEL_ADDRPERM(_v) VM_KERNEL_UNSLIDE_OR_PERM(_v)
390
391 #undef mach_vm_round_page
392 #undef round_page
393 #undef round_page_32
394 #undef round_page_64
395
396 static inline int
mach_vm_size_unit(mach_vm_size_t size)397 mach_vm_size_unit(mach_vm_size_t size)
398 {
399 uint32_t bits = 64u - (uint32_t)__builtin_clzll((size / 10) | 1);
400
401 return "BKMGTPE"[bits / 10];
402 }
403
404 static inline uint32_t
mach_vm_size_pretty(mach_vm_size_t size)405 mach_vm_size_pretty(mach_vm_size_t size)
406 {
407 uint32_t bits = 64u - (uint32_t)__builtin_clzll((size / 10) | 1);
408
409 return (uint32_t)(size >> (bits - bits % 10));
410 }
411
412 static inline mach_vm_offset_t
mach_vm_round_page(mach_vm_offset_t x)413 mach_vm_round_page(mach_vm_offset_t x)
414 {
415 if (round_page_overflow(x, &x)) {
416 panic("overflow detected");
417 }
418 return x;
419 }
420
421 static inline vm_offset_t
round_page(vm_offset_t x)422 round_page(vm_offset_t x)
423 {
424 if (round_page_overflow(x, &x)) {
425 panic("overflow detected");
426 }
427 return x;
428 }
429
430 static inline mach_vm_offset_t
round_page_64(mach_vm_offset_t x)431 round_page_64(mach_vm_offset_t x)
432 {
433 if (round_page_overflow(x, &x)) {
434 panic("overflow detected");
435 }
436 return x;
437 }
438
439 static inline uint32_t
round_page_32(uint32_t x)440 round_page_32(uint32_t x)
441 {
442 if (round_page_overflow(x, &x)) {
443 panic("overflow detected");
444 }
445 return x;
446 }
447
448
449 /*!
450 * @typedef vm_packing_params_t
451 *
452 * @brief
453 * Data structure representing the packing parameters for a given packed pointer
454 * encoding.
455 *
456 * @discussion
457 * Several data structures wish to pack their pointers on less than 64bits
458 * on LP64 in order to save memory.
459 *
460 * Adopters are supposed to define 3 macros:
461 * - @c *_BITS: number of storage bits used for the packing,
462 * - @c *_SHIFT: number of non significant low bits (expected to be 0),
463 * - @c *_BASE: the base against which to encode.
464 *
465 * The encoding is a no-op when @c *_BITS is equal to @c __WORDSIZE and
466 * @c *_SHIFT is 0.
467 *
468 *
469 * The convenience macro @c VM_PACKING_PARAMS can be used to create
470 * a @c vm_packing_params_t structure out of those definitions.
471 *
472 * It is customary to declare a constant global per scheme for the sake
473 * of debuggers to be able to dynamically decide how to unpack various schemes.
474 *
475 *
476 * This uses 2 possible schemes (who both preserve @c NULL):
477 *
478 * 1. When the storage bits and shift are sufficiently large (strictly more than
479 * VM_KERNEL_POINTER_SIGNIFICANT_BITS), a sign-extension scheme can be used.
480 *
481 * This allows to represent any kernel pointer.
482 *
483 * 2. Else, a base-relative scheme can be used, typical bases are:
484 *
485 * - @c KERNEL_PMAP_HEAP_RANGE_START when only pointers to heap (zone)
486 * allocated objects need to be packed,
487 *
488 * - @c VM_MIN_KERNEL_AND_KEXT_ADDRESS when pointers to kernel globals also
489 * need this.
490 *
491 * When such an ecoding is used, @c zone_restricted_va_max() must be taught
492 * about it.
493 */
494 typedef struct vm_packing_params {
495 vm_offset_t vmpp_base;
496 uint8_t vmpp_bits;
497 uint8_t vmpp_shift;
498 bool vmpp_base_relative;
499 } vm_packing_params_t;
500
501
502 /*!
503 * @macro VM_PACKING_IS_BASE_RELATIVE
504 *
505 * @brief
506 * Whether the packing scheme with those parameters will be base-relative.
507 */
508 #define VM_PACKING_IS_BASE_RELATIVE(ns) \
509 (ns##_BITS + ns##_SHIFT <= VM_KERNEL_POINTER_SIGNIFICANT_BITS)
510
511
512 /*!
513 * @macro VM_PACKING_PARAMS
514 *
515 * @brief
516 * Constructs a @c vm_packing_params_t structure based on the convention that
517 * macros with the @c _BASE, @c _BITS and @c _SHIFT suffixes have been defined
518 * to the proper values.
519 */
520 #define VM_PACKING_PARAMS(ns) \
521 (vm_packing_params_t){ \
522 .vmpp_base = ns##_BASE, \
523 .vmpp_bits = ns##_BITS, \
524 .vmpp_shift = ns##_SHIFT, \
525 .vmpp_base_relative = VM_PACKING_IS_BASE_RELATIVE(ns), \
526 }
527
528 /**
529 * @function vm_pack_pointer
530 *
531 * @brief
532 * Packs a pointer according to the specified parameters.
533 *
534 * @discussion
535 * The convenience @c VM_PACK_POINTER macro allows to synthesize
536 * the @c params argument.
537 *
538 * @param ptr The pointer to pack.
539 * @param params The encoding parameters.
540 * @returns The packed pointer.
541 */
542 static inline vm_offset_t
vm_pack_pointer(vm_offset_t ptr,vm_packing_params_t params)543 vm_pack_pointer(vm_offset_t ptr, vm_packing_params_t params)
544 {
545 if (!params.vmpp_base_relative) {
546 return ptr >> params.vmpp_shift;
547 }
548 if (ptr) {
549 return (ptr - params.vmpp_base) >> params.vmpp_shift;
550 }
551 return (vm_offset_t)0;
552 }
553 #define VM_PACK_POINTER(ptr, ns) \
554 vm_pack_pointer(ptr, VM_PACKING_PARAMS(ns))
555
556 /**
557 * @function vm_unpack_pointer
558 *
559 * @brief
560 * Unpacks a pointer packed with @c vm_pack_pointer().
561 *
562 * @discussion
563 * The convenience @c VM_UNPACK_POINTER macro allows to synthesize
564 * the @c params argument.
565 *
566 * @param packed The packed value to decode.
567 * @param params The encoding parameters.
568 * @returns The unpacked pointer.
569 */
570 static inline vm_offset_t
vm_unpack_pointer(vm_offset_t packed,vm_packing_params_t params)571 vm_unpack_pointer(vm_offset_t packed, vm_packing_params_t params)
572 {
573 if (!params.vmpp_base_relative) {
574 intptr_t addr = (intptr_t)packed;
575 addr <<= __WORDSIZE - params.vmpp_bits;
576 addr >>= __WORDSIZE - params.vmpp_bits - params.vmpp_shift;
577 return (vm_offset_t)addr;
578 }
579 if (packed) {
580 return (packed << params.vmpp_shift) + params.vmpp_base;
581 }
582 return (vm_offset_t)0;
583 }
584 #define VM_UNPACK_POINTER(packed, ns) \
585 vm_unpack_pointer(packed, VM_PACKING_PARAMS(ns))
586
587 /**
588 * @function vm_packing_max_packable
589 *
590 * @brief
591 * Returns the largest packable address for the given parameters.
592 *
593 * @discussion
594 * The convenience @c VM_PACKING_MAX_PACKABLE macro allows to synthesize
595 * the @c params argument.
596 *
597 * @param params The encoding parameters.
598 * @returns The largest packable pointer.
599 */
600 static inline vm_offset_t
vm_packing_max_packable(vm_packing_params_t params)601 vm_packing_max_packable(vm_packing_params_t params)
602 {
603 if (!params.vmpp_base_relative) {
604 return VM_MAX_KERNEL_ADDRESS;
605 }
606
607 vm_offset_t ptr = params.vmpp_base +
608 (((1ul << params.vmpp_bits) - 1) << params.vmpp_shift);
609
610 return ptr >= params.vmpp_base ? ptr : VM_MAX_KERNEL_ADDRESS;
611 }
612 #define VM_PACKING_MAX_PACKABLE(ns) \
613 vm_packing_max_packable(VM_PACKING_PARAMS(ns))
614
615
616 __abortlike
617 extern void
618 vm_packing_pointer_invalid(vm_offset_t ptr, vm_packing_params_t params);
619
620 /**
621 * @function vm_verify_pointer_packable
622 *
623 * @brief
624 * Panics if the specified pointer cannot be packed with the specified
625 * parameters.
626 *
627 * @discussion
628 * The convenience @c VM_VERIFY_POINTER_PACKABLE macro allows to synthesize
629 * the @c params argument.
630 *
631 * The convenience @c VM_ASSERT_POINTER_PACKABLE macro allows to synthesize
632 * the @c params argument, and is erased when assertions are disabled.
633 *
634 * @param ptr The packed value to decode.
635 * @param params The encoding parameters.
636 */
637 static inline void
vm_verify_pointer_packable(vm_offset_t ptr,vm_packing_params_t params)638 vm_verify_pointer_packable(vm_offset_t ptr, vm_packing_params_t params)
639 {
640 if (ptr & ((1ul << params.vmpp_shift) - 1)) {
641 vm_packing_pointer_invalid(ptr, params);
642 }
643 if (!params.vmpp_base_relative || ptr == 0) {
644 return;
645 }
646 if (ptr <= params.vmpp_base || ptr > vm_packing_max_packable(params)) {
647 vm_packing_pointer_invalid(ptr, params);
648 }
649 }
650 #define VM_VERIFY_POINTER_PACKABLE(ptr, ns) \
651 vm_verify_pointer_packable(ptr, VM_PACKING_PARAMS(ns))
652
653 #if DEBUG || DEVELOPMENT
654 #define VM_ASSERT_POINTER_PACKABLE(ptr, ns) \
655 VM_VERIFY_POINTER_PACKABLE(ptr, ns)
656 #else
657 #define VM_ASSERT_POINTER_PACKABLE(ptr, ns) ((void)(ptr))
658 #endif
659
660 /**
661 * @function vm_verify_pointer_range
662 *
663 * @brief
664 * Panics if some pointers in the specified range can't be packed with the
665 * specified parameters.
666 *
667 * @param subsystem The subsystem requiring the packing.
668 * @param min_address The smallest address of the range.
669 * @param max_address The largest address of the range.
670 * @param params The encoding parameters.
671 */
672 extern void
673 vm_packing_verify_range(
674 const char *subsystem,
675 vm_offset_t min_address,
676 vm_offset_t max_address,
677 vm_packing_params_t params);
678
679 #endif /* XNU_KERNEL_PRIVATE */
680
681 extern vm_size_t page_size;
682 extern vm_size_t page_mask;
683 extern int page_shift;
684
685 /* We need a way to get rid of compiler warnings when we cast from */
686 /* a 64 bit value to an address (which may be 32 bits or 64-bits). */
687 /* An intptr_t is used convert the value to the right precision, and */
688 /* then to an address. This macro is also used to convert addresses */
689 /* to 32-bit integers, which is a hard failure for a 64-bit kernel */
690 #include <stdint.h>
691 #ifndef __CAST_DOWN_CHECK
692 #define __CAST_DOWN_CHECK
693
694 #define CAST_DOWN( type, addr ) \
695 ( ((type)((uintptr_t) (addr)/(sizeof(type) < sizeof(uintptr_t) ? 0 : 1))) )
696
697 #define CAST_DOWN_EXPLICIT( type, addr ) ( ((type)((uintptr_t) (addr))) )
698
699 #endif /* __CAST_DOWN_CHECK */
700
701 #endif /* ASSEMBLER */
702
703 #endif /* KERNEL */
704
705 #endif /* _MACH_VM_PARAM_H_ */
706