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
//! This module roughly corresponds to `mach/task_info.h`.

use core::mem::size_of;
use core::ffi::c_uint;

use crate::vm_types::{
    integer_t,
    natural_t,
    mach_vm_address_t,
    mach_vm_size_t,
};
use crate::message::mach_msg_type_number_t;

pub const TASK_INFO_MAX:       c_uint = 1024;
pub const TASK_BASIC_INFO_32:  c_uint = 4;
pub const TASK_BASIC2_INFO_32: c_uint = 6;

#[cfg(target_arch = "x86_64")]
pub const TASK_BASIC_INFO_64:  c_uint = 5;

#[cfg(target_arch = "aarch64")]
pub const TASK_BASIC_INFO_64:  c_uint = 18;

#[cfg(target_pointer_width = "32")]
pub const TASK_BASIC_INFO: c_uint = TASK_BASIC_INFO_32;

#[cfg(target_pointer_width = "64")]
pub const TASK_BASIC_INFO: c_uint = TASK_BASIC_INFO_64;

pub const TASK_EVENTS_INFO:               c_uint = 2;
pub const TASK_THREAD_TIMES_INFO:         c_uint = 3;
pub const TASK_ABSOLUTETIME_INFO:         c_uint = 1;
pub const TASK_KERNELMEMORY_INFO:         c_uint = 7;
pub const TASK_SECURITY_TOKEN:            c_uint = 13;
pub const TASK_AUDIT_TOKEN:               c_uint = 15;
pub const TASK_AFFINITY_TAG_INFO:         c_uint = 16;
pub const TASK_DYLD_INFO:                 c_uint = 17;
pub const TASK_DYLD_ALL_IMAGE_INFO_32:    c_uint = 0;
pub const TASK_DYLD_ALL_IMAGE_INFO_64:    c_uint = 1;
pub const TASK_EXTMOD_INFO:               c_uint = 19;
pub const MACH_TASK_BASIC_INFO:           c_uint = 20;
pub const TASK_POWER_INFO:                c_uint = 21;
pub const TASK_VM_INFO:                   c_uint = 22;
pub const TASK_VM_INFO_PURGEABLE:         c_uint = 23;
pub const TASK_TRACE_MEMORY_INFO:         c_uint = 24;
pub const TASK_WAIT_STATE_INFO:           c_uint = 25;
pub const TASK_POWER_INFO_V2:             c_uint = 26;
pub const TASK_VM_INFO_PURGEABLE_ACCOUNT: c_uint = 27;
pub const TASK_FLAGS_INFO:                c_uint = 28;
pub const TASK_DEBUG_INFO_INTERNAL:       c_uint = 29;

pub type task_flavor_t = natural_t;
pub type task_info_t   = *mut integer_t;

pub type task_vm_info_t = task_vm_info_rev5_t;
pub type task_vm_info   = task_vm_info_t;

pub const TASK_VM_INFO_COUNT:      mach_msg_type_number_t = TASK_VM_INFO_REV5_COUNT;
pub const TASK_VM_INFO_REV5_COUNT: mach_msg_type_number_t = (size_of::<task_vm_info_rev5_t>() / size_of::<natural_t>() ) as _;
pub const TASK_VM_INFO_REV4_COUNT: mach_msg_type_number_t = TASK_VM_INFO_REV5_COUNT - 1;
pub const TASK_VM_INFO_REV3_COUNT: mach_msg_type_number_t = TASK_VM_INFO_REV4_COUNT - 2;
pub const TASK_VM_INFO_REV2_COUNT: mach_msg_type_number_t = TASK_VM_INFO_REV3_COUNT - 42;
pub const TASK_VM_INFO_REV1_COUNT: mach_msg_type_number_t = TASK_VM_INFO_REV2_COUNT - 4;
pub const TASK_VM_INFO_REV0_COUNT: mach_msg_type_number_t = TASK_VM_INFO_REV1_COUNT - 2;

// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/osfmk/mach/task_info.h#L425
//
/*

#define TASK_VM_INFO_COUNT  ((mach_msg_type_number_t) \
	(sizeof (task_vm_info_data_t) / sizeof (natural_t)))

#define TASK_VM_INFO_REV5_COUNT TASK_VM_INFO_COUNT

// doesn't include decompressions
#define TASK_VM_INFO_REV4_COUNT \
((mach_msg_type_number_t) (TASK_VM_INFO_REV5_COUNT - 1))

// doesn't include limit bytes
#define TASK_VM_INFO_REV3_COUNT \
((mach_msg_type_number_t) (TASK_VM_INFO_REV4_COUNT - 2))

// doesn't include extra ledgers info
#define TASK_VM_INFO_REV2_COUNT \
((mach_msg_type_number_t) (TASK_VM_INFO_REV3_COUNT - 42))

// doesn't include min and max address
#define TASK_VM_INFO_REV1_COUNT \
((mach_msg_type_number_t) (TASK_VM_INFO_REV2_COUNT - 4))

// doesn't include phys_footprint
#define TASK_VM_INFO_REV0_COUNT \
((mach_msg_type_number_t) (TASK_VM_INFO_REV1_COUNT - 2))

*/

#[repr(C, packed(4))]
#[derive(
    Copy, Clone,
    Debug, Default,
    Hash,
    PartialOrd, Ord,
    PartialEq, Eq,
)]
pub struct task_vm_info_rev0_t {
    /// virtual memory size (bytes)
    pub virtual_size:                mach_vm_size_t,

    /// number of memory regions
    pub region_count:                integer_t,

    /// page size
    pub page_size:                   integer_t,

    /// resident memory size (bytes)
    pub resident_size:               mach_vm_size_t,

    /// peak resident size (bytes)
    pub resident_size_peak:          mach_vm_size_t,

    pub device:                      mach_vm_size_t,
    pub device_peak:                 mach_vm_size_t,
    pub internal:                    mach_vm_size_t,
    pub internal_peak:               mach_vm_size_t,
    pub external:                    mach_vm_size_t,
    pub external_peak:               mach_vm_size_t,
    pub reusable:                    mach_vm_size_t,
    pub reusable_peak:               mach_vm_size_t,
    pub purgeable_volatile_pmap:     mach_vm_size_t,
    pub purgeable_volatile_resident: mach_vm_size_t,
    pub purgeable_volatile_virtual:  mach_vm_size_t,
    pub compressed:                  mach_vm_size_t,
    pub compressed_peak:             mach_vm_size_t,
    pub compressed_lifetime:         mach_vm_size_t,
}

#[repr(C, packed(4))]
#[derive(
    Copy, Clone,
    Debug, Default,
    Hash,
    PartialOrd, Ord,
    PartialEq, Eq,
)]
pub struct task_vm_info_rev1_t {
    /// virtual memory size (bytes)
    pub virtual_size:                mach_vm_size_t,

    /// number of memory regions
    pub region_count:                integer_t,

    /// page size
    pub page_size:                   integer_t,

    /// resident memory size (bytes)
    pub resident_size:               mach_vm_size_t,

    /// peak resident size (bytes)
    pub resident_size_peak:          mach_vm_size_t,

    pub device:                      mach_vm_size_t,
    pub device_peak:                 mach_vm_size_t,
    pub internal:                    mach_vm_size_t,
    pub internal_peak:               mach_vm_size_t,
    pub external:                    mach_vm_size_t,
    pub external_peak:               mach_vm_size_t,
    pub reusable:                    mach_vm_size_t,
    pub reusable_peak:               mach_vm_size_t,
    pub purgeable_volatile_pmap:     mach_vm_size_t,
    pub purgeable_volatile_resident: mach_vm_size_t,
    pub purgeable_volatile_virtual:  mach_vm_size_t,
    pub compressed:                  mach_vm_size_t,
    pub compressed_peak:             mach_vm_size_t,
    pub compressed_lifetime:         mach_vm_size_t,

    /// added for rev1
    pub phys_footprint: mach_vm_size_t,
}

#[repr(C, packed(4))]
#[derive(
    Copy, Clone,
    Debug, Default,
    Hash,
    PartialOrd, Ord,
    PartialEq, Eq,
)]
pub struct task_vm_info_rev2_t {
    /// virtual memory size (bytes)
    pub virtual_size:                mach_vm_size_t,

    /// number of memory regions
    pub region_count:                integer_t,

    /// page size
    pub page_size:                   integer_t,

    /// resident memory size (bytes)
    pub resident_size:               mach_vm_size_t,

    /// peak resident size (bytes)
    pub resident_size_peak:          mach_vm_size_t,

    pub device:                      mach_vm_size_t,
    pub device_peak:                 mach_vm_size_t,
    pub internal:                    mach_vm_size_t,
    pub internal_peak:               mach_vm_size_t,
    pub external:                    mach_vm_size_t,
    pub external_peak:               mach_vm_size_t,
    pub reusable:                    mach_vm_size_t,
    pub reusable_peak:               mach_vm_size_t,
    pub purgeable_volatile_pmap:     mach_vm_size_t,
    pub purgeable_volatile_resident: mach_vm_size_t,
    pub purgeable_volatile_virtual:  mach_vm_size_t,
    pub compressed:                  mach_vm_size_t,
    pub compressed_peak:             mach_vm_size_t,
    pub compressed_lifetime:         mach_vm_size_t,

    // added for rev1
    pub phys_footprint: mach_vm_size_t,

    // added for rev2
    pub min_address: mach_vm_address_t,
    pub max_address: mach_vm_address_t,
}

#[repr(C, packed(4))]
#[derive(
    Copy, Clone,
    Debug, Default,
    Hash,
    PartialOrd, Ord,
    PartialEq, Eq,
)]
pub struct task_vm_info_rev3_t {
    /// virtual memory size (bytes)
    pub virtual_size:                mach_vm_size_t,

    /// number of memory regions
    pub region_count:                integer_t,

    /// page size
    pub page_size:                   integer_t,

    /// resident memory size (bytes)
    pub resident_size:               mach_vm_size_t,

    /// peak resident size (bytes)
    pub resident_size_peak:          mach_vm_size_t,

    pub device:                      mach_vm_size_t,
    pub device_peak:                 mach_vm_size_t,
    pub internal:                    mach_vm_size_t,
    pub internal_peak:               mach_vm_size_t,
    pub external:                    mach_vm_size_t,
    pub external_peak:               mach_vm_size_t,
    pub reusable:                    mach_vm_size_t,
    pub reusable_peak:               mach_vm_size_t,
    pub purgeable_volatile_pmap:     mach_vm_size_t,
    pub purgeable_volatile_resident: mach_vm_size_t,
    pub purgeable_volatile_virtual:  mach_vm_size_t,
    pub compressed:                  mach_vm_size_t,
    pub compressed_peak:             mach_vm_size_t,
    pub compressed_lifetime:         mach_vm_size_t,

    // added for rev1
    pub phys_footprint:              mach_vm_size_t,

    // added for rev2
    pub min_address:                 mach_vm_address_t,
    pub max_address:                 mach_vm_address_t,

    // added for rev3
    pub ledger_phys_footprint_peak:                 i64,
    pub ledger_purgeable_nonvolatile:               i64,
    pub ledger_purgeable_novolatile_compressed:     i64,
    pub ledger_purgeable_volatile:                  i64,
    pub ledger_purgeable_volatile_compressed:       i64,
    pub ledger_tag_network_nonvolatile:             i64,
    pub ledger_tag_network_nonvolatile_compressed:  i64,
    pub ledger_tag_network_volatile:                i64,
    pub ledger_tag_network_volatile_compressed:     i64,
    pub ledger_tag_media_footprint:                 i64,
    pub ledger_tag_media_footprint_compressed:      i64,
    pub ledger_tag_media_nofootprint:               i64,
    pub ledger_tag_media_nofootprint_compressed:    i64,
    pub ledger_tag_graphics_footprint:              i64,
    pub ledger_tag_graphics_footprint_compressed:   i64,
    pub ledger_tag_graphics_nofootprint:            i64,
    pub ledger_tag_graphics_nofootprint_compressed: i64,
    pub ledger_tag_neural_footprint:                i64,
    pub ledger_tag_neural_footprint_compressed:     i64,
    pub ledger_tag_neural_nofootprint:              i64,
    pub ledger_tag_neural_nofootprint_compressed:   i64,
}

#[repr(C, packed(4))]
#[derive(
    Copy, Clone,
    Debug, Default,
    Hash,
    PartialOrd, Ord,
    PartialEq, Eq,
)]
pub struct task_vm_info_rev4_t {
    /// virtual memory size (bytes)
    pub virtual_size:                mach_vm_size_t,

    /// number of memory regions
    pub region_count:                integer_t,

    /// page size
    pub page_size:                   integer_t,

    /// resident memory size (bytes)
    pub resident_size:               mach_vm_size_t,

    /// peak resident size (bytes)
    pub resident_size_peak:          mach_vm_size_t,

    pub device:                      mach_vm_size_t,
    pub device_peak:                 mach_vm_size_t,
    pub internal:                    mach_vm_size_t,
    pub internal_peak:               mach_vm_size_t,
    pub external:                    mach_vm_size_t,
    pub external_peak:               mach_vm_size_t,
    pub reusable:                    mach_vm_size_t,
    pub reusable_peak:               mach_vm_size_t,
    pub purgeable_volatile_pmap:     mach_vm_size_t,
    pub purgeable_volatile_resident: mach_vm_size_t,
    pub purgeable_volatile_virtual:  mach_vm_size_t,
    pub compressed:                  mach_vm_size_t,
    pub compressed_peak:             mach_vm_size_t,
    pub compressed_lifetime:         mach_vm_size_t,

    // added for rev1
    pub phys_footprint:              mach_vm_size_t,

    // added for rev2
    pub min_address:                 mach_vm_address_t,
    pub max_address:                 mach_vm_address_t,

    // added for rev3
    pub ledger_phys_footprint_peak:                 i64,
    pub ledger_purgeable_nonvolatile:               i64,
    pub ledger_purgeable_novolatile_compressed:     i64,
    pub ledger_purgeable_volatile:                  i64,
    pub ledger_purgeable_volatile_compressed:       i64,
    pub ledger_tag_network_nonvolatile:             i64,
    pub ledger_tag_network_nonvolatile_compressed:  i64,
    pub ledger_tag_network_volatile:                i64,
    pub ledger_tag_network_volatile_compressed:     i64,
    pub ledger_tag_media_footprint:                 i64,
    pub ledger_tag_media_footprint_compressed:      i64,
    pub ledger_tag_media_nofootprint:               i64,
    pub ledger_tag_media_nofootprint_compressed:    i64,
    pub ledger_tag_graphics_footprint:              i64,
    pub ledger_tag_graphics_footprint_compressed:   i64,
    pub ledger_tag_graphics_nofootprint:            i64,
    pub ledger_tag_graphics_nofootprint_compressed: i64,
    pub ledger_tag_neural_footprint:                i64,
    pub ledger_tag_neural_footprint_compressed:     i64,
    pub ledger_tag_neural_nofootprint:              i64,
    pub ledger_tag_neural_nofootprint_compressed:   i64,

    // added for rev4
    pub limit_bytes_remaining: u64,
}

#[repr(C, packed(4))]
#[derive(
    Copy, Clone,
    Debug, Default,
    Hash,
    PartialOrd, Ord,
    PartialEq, Eq,
)]
pub struct task_vm_info_rev5_t {
    /// virtual memory size (bytes)
    pub virtual_size:                mach_vm_size_t,

    /// number of memory regions
    pub region_count:                integer_t,

    /// page size
    pub page_size:                   integer_t,

    /// resident memory size (bytes)
    pub resident_size:               mach_vm_size_t,

    /// peak resident size (bytes)
    pub resident_size_peak:          mach_vm_size_t,

    pub device:                      mach_vm_size_t,
    pub device_peak:                 mach_vm_size_t,
    pub internal:                    mach_vm_size_t,
    pub internal_peak:               mach_vm_size_t,
    pub external:                    mach_vm_size_t,
    pub external_peak:               mach_vm_size_t,
    pub reusable:                    mach_vm_size_t,
    pub reusable_peak:               mach_vm_size_t,
    pub purgeable_volatile_pmap:     mach_vm_size_t,
    pub purgeable_volatile_resident: mach_vm_size_t,
    pub purgeable_volatile_virtual:  mach_vm_size_t,
    pub compressed:                  mach_vm_size_t,
    pub compressed_peak:             mach_vm_size_t,
    pub compressed_lifetime:         mach_vm_size_t,

    // added for rev1
    pub phys_footprint:              mach_vm_size_t,

    // added for rev2
    pub min_address:                 mach_vm_address_t,
    pub max_address:                 mach_vm_address_t,

    // added for rev3
    pub ledger_phys_footprint_peak:                 i64,
    pub ledger_purgeable_nonvolatile:               i64,
    pub ledger_purgeable_novolatile_compressed:     i64,
    pub ledger_purgeable_volatile:                  i64,
    pub ledger_purgeable_volatile_compressed:       i64,
    pub ledger_tag_network_nonvolatile:             i64,
    pub ledger_tag_network_nonvolatile_compressed:  i64,
    pub ledger_tag_network_volatile:                i64,
    pub ledger_tag_network_volatile_compressed:     i64,
    pub ledger_tag_media_footprint:                 i64,
    pub ledger_tag_media_footprint_compressed:      i64,
    pub ledger_tag_media_nofootprint:               i64,
    pub ledger_tag_media_nofootprint_compressed:    i64,
    pub ledger_tag_graphics_footprint:              i64,
    pub ledger_tag_graphics_footprint_compressed:   i64,
    pub ledger_tag_graphics_nofootprint:            i64,
    pub ledger_tag_graphics_nofootprint_compressed: i64,
    pub ledger_tag_neural_footprint:                i64,
    pub ledger_tag_neural_footprint_compressed:     i64,
    pub ledger_tag_neural_nofootprint:              i64,
    pub ledger_tag_neural_nofootprint_compressed:   i64,

    // added for rev4
    pub limit_bytes_remaining: u64,

    // added for rev5
    pub decompressions: integer_t,
}

#[repr(C, packed(4))]
#[derive(
    Copy, Clone,
    Debug, Default,
    Hash,
    PartialOrd, Ord,
    PartialEq, Eq,
)]
pub struct task_dyld_info {
    pub all_image_info_addr:   mach_vm_address_t,
    pub all_image_info_size:   mach_vm_size_t,
    pub all_image_info_format: integer_t,
}