xref: /xnu-12377.61.12/osfmk/mach/vm_types.h (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8) !
1 /*
2  * Copyright (c) 2000-2018 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  * @OSF_COPYRIGHT@
30  *
31  */
32 #ifndef _MACH_VM_TYPES_H_
33 #define _MACH_VM_TYPES_H_
34 
35 #include <mach/port.h>
36 #include <mach/machine/vm_types.h>
37 
38 #include <stdint.h>
39 #include <sys/cdefs.h>
40 
41 __BEGIN_DECLS
42 
43 typedef vm_offset_t             pointer_t __kernel_ptr_semantics;
44 typedef vm_offset_t             vm_address_t __kernel_ptr_semantics;
45 
46 /*
47  * We use addr64_t for 64-bit addresses that are used on both
48  * 32 and 64-bit machines.  On PPC, they are passed and returned as
49  * two adjacent 32-bit GPRs.  We use addr64_t in places where
50  * common code must be useable both on 32 and 64-bit machines.
51  */
52 typedef uint64_t addr64_t;              /* Basic effective address */
53 
54 /*
55  * We use reg64_t for addresses that are 32 bits on a 32-bit
56  * machine, and 64 bits on a 64-bit machine, but are always
57  * passed and returned in a single GPR on PPC.  This type
58  * cannot be used in generic 32-bit c, since on a 64-bit
59  * machine the upper half of the register will be ignored
60  * by the c compiler in 32-bit mode.  In c, we can only use the
61  * type in prototypes of functions that are written in and called
62  * from assembly language.  This type is basically a comment.
63  */
64 typedef uint32_t        reg64_t;
65 
66 /*
67  * To minimize the use of 64-bit fields, we keep some physical
68  * addresses (that are page aligned) as 32-bit page numbers.
69  * This limits the physical address space to 16TB of RAM.
70  */
71 typedef uint32_t ppnum_t __kernel_ptr_semantics; /* Physical page number */
72 #define PPNUM_MAX UINT32_MAX
73 
74 /*
75  * For use with mach_vm_update_pointers_with_remote_tags.
76  * This definition must be kept in sync with the definition in mach_types.defs.
77  */
78 #define VM_OFFSET_LIST_MAX  1024
79 
80 #ifdef  KERNEL_PRIVATE
81 
82 __options_decl(vm_map_create_options_t, uint32_t, {
83 	VM_MAP_CREATE_DEFAULT          = 0x00000000,
84 	VM_MAP_CREATE_PAGEABLE         = 0x00000001,
85 	VM_MAP_CREATE_CORPSE_FOOTPRINT = 0x00000002,
86 	VM_MAP_CREATE_DISABLE_HOLELIST = 0x00000004,
87 	VM_MAP_CREATE_NEVER_FAULTS     = 0x00000008,
88 	/* Denote that we're creating this map as part of a fork() */
89 	VM_MAP_CREATE_VIA_FORK             = 0x00000010,
90 });
91 
92 /*
93  * Use specifically typed null structures for these in
94  * other parts of the kernel to enable compiler warnings
95  * about type mismatches, etc...  Otherwise, these would
96  * be void*.
97  */
98 
99 typedef struct pmap             *pmap_t;
100 typedef struct _vm_map          *vm_map_t, *vm_map_read_t, *vm_map_inspect_t;
101 typedef struct vm_object        *vm_object_t;
102 typedef struct vm_object_fault_info     *vm_object_fault_info_t;
103 typedef struct upl              *upl_t;
104 typedef struct vm_map_copy      *vm_map_copy_t;
105 typedef struct vm_named_entry   *vm_named_entry_t;
106 typedef struct vm_page          *vm_page_t;
107 /*
108  * A generation ID for vm_maps, which increments monotonically.
109  * These IDs are not globally unique among VM maps, however. Instead,
110  *  IDs represent 'independent' VM map lineages: maps interrelated via
111  *  fork() identify with the same ID.
112  */
113 typedef const void                              *vm_map_serial_t;
114 
115 #define PMAP_NULL               ((pmap_t) NULL)
116 #define VM_OBJECT_NULL          ((vm_object_t) NULL)
117 #define VM_MAP_COPY_NULL        ((vm_map_copy_t) NULL)
118 
119 #define VM_MAP_SERIAL_NONE      ((vm_map_serial_t)-1)
120 /* Denotes 'special'/one-off kernel-managed objects that don't belong to a parent map */
121 #define VM_MAP_SERIAL_SPECIAL   ((vm_map_serial_t)-2)
122 
123 #else   /* KERNEL_PRIVATE */
124 
125 typedef mach_port_t             vm_map_t, vm_map_read_t, vm_map_inspect_t;
126 typedef mach_port_t             upl_t;
127 typedef mach_port_t             vm_named_entry_t;
128 
129 #endif  /* KERNEL_PRIVATE */
130 
131 typedef mach_vm_offset_t                *mach_vm_offset_list_t;
132 
133 #ifdef KERNEL
134 #define VM_MAP_NULL             ((vm_map_t) NULL)
135 #define VM_MAP_INSPECT_NULL     ((vm_map_inspect_t) NULL)
136 #define VM_MAP_READ_NULL        ((vm_map_read_t) NULL)
137 #define UPL_NULL                ((upl_t) NULL)
138 #define VM_NAMED_ENTRY_NULL     ((vm_named_entry_t) NULL)
139 #else
140 #define VM_MAP_NULL             ((vm_map_t) 0)
141 #define VM_MAP_INSPECT_NULL     ((vm_map_inspect_t) 0)
142 #define VM_MAP_READ_NULL        ((vm_map_read_t) 0)
143 #define UPL_NULL                ((upl_t) 0)
144 #define VM_NAMED_ENTRY_NULL     ((vm_named_entry_t) 0)
145 #endif
146 
147 /*
148  * Evolving definitions, likely to change.
149  */
150 
151 typedef uint64_t                vm_object_offset_t;
152 typedef uint64_t                vm_object_size_t;
153 
154 /*!
155  * @typedef mach_vm_range_t
156  *
157  * @brief
158  * Pair of a min/max address used to denote a memory region.
159  *
160  * @discussion
161  * @c min_address must be smaller or equal to @c max_address.
162  */
163 typedef struct mach_vm_range {
164 	mach_vm_offset_t        min_address;
165 	mach_vm_offset_t        max_address;
166 } *mach_vm_range_t;
167 
168 /*!
169  * @enum mach_vm_range_flavor_t
170  *
171  * @brief
172  * A flavor for the mach_vm_range_create() call.
173  *
174  * @const MACH_VM_RANGE_FLAVOR_V1
175  * The recipe is an array of @c mach_vm_range_recipe_v1_t.
176  */
177 __enum_decl(mach_vm_range_flavor_t, uint32_t, {
178 	MACH_VM_RANGE_FLAVOR_INVALID,
179 	MACH_VM_RANGE_FLAVOR_V1,
180 });
181 
182 
183 /*!
184  * @enum mach_vm_range_flags_t
185  *
186  * @brief
187  * Flags used to alter the behavior of a Mach VM Range.
188  */
189 __options_decl(mach_vm_range_flags_t, uint64_t, {
190 	MACH_VM_RANGE_NONE      = 0x000000000000,
191 });
192 
193 
194 /*!
195  * @enum mach_vm_range_tag_t
196  *
197  * @brief
198  * A tag to denote the semantics of a given Mach VM Range.
199  *
200  * @const MACH_VM_RANGE_DEFAULT
201  * The tag associated with the general VA space usable
202  * before the shared cache.
203  * Such a range can't be made by userspace.
204  *
205  * @const MACH_VM_RANGE_DATA
206  * The tag associated with the anonymous randomly slid
207  * range of data heap optionally made when a process is created.
208  * Such a range can't be made by userspace.
209  *
210  * @const MACH_VM_RANGE_FIXED
211  * The tag associated with ranges that are made available
212  * for @c VM_FLAGS_FIXED allocations, but that the VM will never
213  * autonomously serve from a @c VM_FLAGS_ANYWHERE kind of request.
214  * This really create a delegated piece of VA that can be carved out
215  * in the way userspace sees fit.
216  */
217 __enum_decl(mach_vm_range_tag_t, uint16_t, {
218 	MACH_VM_RANGE_DEFAULT,
219 	MACH_VM_RANGE_DATA,
220 	MACH_VM_RANGE_FIXED,
221 });
222 
223 #pragma pack(1)
224 
225 typedef struct {
226 	mach_vm_range_flags_t   flags: 48;
227 	mach_vm_range_tag_t     range_tag  : 8;
228 	uint8_t                 vm_tag : 8;
229 	struct mach_vm_range    range;
230 } mach_vm_range_recipe_v1_t;
231 
232 #pragma pack()
233 
234 #define MACH_VM_RANGE_FLAVOR_DEFAULT MACH_VM_RANGE_FLAVOR_V1
235 typedef mach_vm_range_recipe_v1_t    mach_vm_range_recipe_t;
236 
237 typedef uint8_t                *mach_vm_range_recipes_raw_t;
238 
239 #ifdef PRIVATE
240 
241 typedef struct {
242 	uint64_t rtfabstime; // mach_continuous_time at start of fault
243 	uint64_t rtfduration; // fault service duration
244 	uint64_t rtfaddr; // fault address
245 	uint64_t rtfpc; // userspace program counter of thread incurring the fault
246 	uint64_t rtftid; // thread ID
247 	uint64_t rtfupid; // process identifier
248 	uint64_t rtftype; // fault type
249 } vm_rtfault_record_t;
250 
251 #endif /* PRIVATE */
252 #ifdef XNU_KERNEL_PRIVATE
253 
254 #define VM_TAG_ACTIVE_UPDATE    1
255 
256 typedef uint16_t                vm_tag_t;
257 
258 #define VM_TAG_NAME_LEN_MAX     0x7F
259 #define VM_TAG_NAME_LEN_SHIFT   0
260 #define VM_TAG_UNLOAD           0x0100
261 #define VM_TAG_KMOD             0x0200
262 
263 #if !KASAN && (DEBUG || DEVELOPMENT)
264 /*
265  * To track the utilization of memory at every kalloc callsite, zone tagging
266  * allocates an array of stats (of size VM_TAG_SIZECLASSES), one for each
267  * size class exposed by kalloc.
268  *
269  * If VM_TAG_SIZECLASSES is modified ensure that z_tags_sizeclass
270  * has sufficient bits to represent all values (max value exclusive).
271  */
272 #define VM_TAG_SIZECLASSES      36
273 // must be multiple of 64
274 #define VM_MAX_TAG_VALUE        1536
275 #else
276 #define VM_TAG_SIZECLASSES      0
277 #define VM_MAX_TAG_VALUE        256
278 #endif
279 
280 #define ARRAY_COUNT(a)  (sizeof((a)) / sizeof((a)[0]))
281 
282 struct vm_allocation_total {
283 	vm_tag_t tag;
284 	uint64_t total;
285 };
286 
287 struct vm_allocation_zone_total {
288 	vm_size_t vazt_total;
289 	vm_size_t vazt_peak;
290 };
291 typedef struct vm_allocation_zone_total vm_allocation_zone_total_t;
292 
293 struct vm_allocation_site {
294 	uint64_t  total;
295 #if DEBUG || DEVELOPMENT
296 	uint64_t  peak;
297 #endif /* DEBUG || DEVELOPMENT */
298 	uint64_t  mapped;
299 	int16_t   refcount;
300 	vm_tag_t  tag;
301 	uint16_t  flags;
302 	uint16_t  subtotalscount;
303 	struct vm_allocation_total subtotals[0];
304 	/* char      name[0]; -- this is placed after subtotals, see KA_NAME() */
305 };
306 typedef struct vm_allocation_site vm_allocation_site_t;
307 
308 extern int vmrtf_extract(uint64_t, boolean_t, unsigned long, void *, unsigned long *);
309 extern unsigned int vmrtfaultinfo_bufsz(void);
310 
311 #endif /* XNU_KERNEL_PRIVATE */
312 
313 __END_DECLS
314 
315 #endif  /* _MACH_VM_TYPES_H_ */
316