-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathdarwin.zig
1555 lines (1424 loc) · 47.8 KB
/
darwin.zig
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const builtin = @import("builtin");
const native_arch = builtin.target.cpu.arch;
const assert = std.debug.assert;
const AF = std.c.AF;
const PROT = std.c.PROT;
const fd_t = std.c.fd_t;
const iovec_const = std.posix.iovec_const;
const mode_t = std.c.mode_t;
const off_t = std.c.off_t;
const pid_t = std.c.pid_t;
const pthread_attr_t = std.c.pthread_attr_t;
const timespec = std.c.timespec;
const sf_hdtr = std.c.sf_hdtr;
comptime {
assert(builtin.os.tag.isDarwin()); // Prevent access of std.c symbols on wrong OS.
}
pub const mach_port_t = c_uint;
pub const THREAD_STATE_NONE = switch (native_arch) {
.aarch64 => 5,
.x86_64 => 13,
else => @compileError("unsupported arch"),
};
pub const EXC = enum(exception_type_t) {
NULL = 0,
/// Could not access memory
BAD_ACCESS = 1,
/// Instruction failed
BAD_INSTRUCTION = 2,
/// Arithmetic exception
ARITHMETIC = 3,
/// Emulation instruction
EMULATION = 4,
/// Software generated exception
SOFTWARE = 5,
/// Trace, breakpoint, etc.
BREAKPOINT = 6,
/// System calls.
SYSCALL = 7,
/// Mach system calls.
MACH_SYSCALL = 8,
/// RPC alert
RPC_ALERT = 9,
/// Abnormal process exit
CRASH = 10,
/// Hit resource consumption limit
RESOURCE = 11,
/// Violated guarded resource protections
GUARD = 12,
/// Abnormal process exited to corpse state
CORPSE_NOTIFY = 13,
pub const TYPES_COUNT = @typeInfo(EXC).@"enum".fields.len;
pub const SOFT_SIGNAL = 0x10003;
pub const MASK = packed struct(u32) {
_0: u1 = 0,
BAD_ACCESS: bool = false,
BAD_INSTRUCTION: bool = false,
ARITHMETIC: bool = false,
EMULATION: bool = false,
SOFTWARE: bool = false,
BREAKPOINT: bool = false,
SYSCALL: bool = false,
MACH_SYSCALL: bool = false,
RPC_ALERT: bool = false,
CRASH: bool = false,
RESOURCE: bool = false,
GUARD: bool = false,
CORPSE_NOTIFY: bool = false,
_14: u18 = 0,
pub const MACHINE: MASK = @bitCast(@as(u32, 0));
pub const ALL: MASK = .{
.BAD_ACCESS = true,
.BAD_INSTRUCTION = true,
.ARITHMETIC = true,
.EMULATION = true,
.SOFTWARE = true,
.BREAKPOINT = true,
.SYSCALL = true,
.MACH_SYSCALL = true,
.RPC_ALERT = true,
.CRASH = true,
.RESOURCE = true,
.GUARD = true,
.CORPSE_NOTIFY = true,
};
};
};
pub const EXCEPTION = enum(u32) {
/// Send a catch_exception_raise message including the identity.
DEFAULT = 1,
/// Send a catch_exception_raise_state message including the
/// thread state.
STATE = 2,
/// Send a catch_exception_raise_state_identity message including
/// the thread identity and state.
STATE_IDENTITY = 3,
/// Send a catch_exception_raise_identity_protected message including protected task
/// and thread identity.
IDENTITY_PROTECTED = 4,
_,
};
/// Prefer sending a catch_exception_raice_backtrace message, if applicable.
pub const MACH_EXCEPTION_BACKTRACE_PREFERRED = 0x20000000;
/// include additional exception specific errors, not used yet.
pub const MACH_EXCEPTION_ERRORS = 0x40000000;
/// Send 64-bit code and subcode in the exception header */
pub const MACH_EXCEPTION_CODES = 0x80000000;
pub const MACH_EXCEPTION_MASK = MACH_EXCEPTION_CODES |
MACH_EXCEPTION_ERRORS |
MACH_EXCEPTION_BACKTRACE_PREFERRED;
pub const TASK_NULL: task_t = 0;
pub const THREAD_NULL: thread_t = 0;
pub const MACH_PORT_NULL: mach_port_t = 0;
pub const MACH_MSG_TIMEOUT_NONE: mach_msg_timeout_t = 0;
pub const MACH_MSG_OPTION_NONE = 0x00000000;
pub const MACH_SEND_MSG = 0x00000001;
pub const MACH_RCV_MSG = 0x00000002;
pub const MACH_RCV_LARGE = 0x00000004;
pub const MACH_RCV_LARGE_IDENTITY = 0x00000008;
pub const MACH_SEND_TIMEOUT = 0x00000010;
pub const MACH_SEND_OVERRIDE = 0x00000020;
pub const MACH_SEND_INTERRUPT = 0x00000040;
pub const MACH_SEND_NOTIFY = 0x00000080;
pub const MACH_SEND_ALWAYS = 0x00010000;
pub const MACH_SEND_FILTER_NONFATAL = 0x00010000;
pub const MACH_SEND_TRAILER = 0x00020000;
pub const MACH_SEND_NOIMPORTANCE = 0x00040000;
pub const MACH_SEND_NODENAP = MACH_SEND_NOIMPORTANCE;
pub const MACH_SEND_IMPORTANCE = 0x00080000;
pub const MACH_SEND_SYNC_OVERRIDE = 0x00100000;
pub const MACH_SEND_PROPAGATE_QOS = 0x00200000;
pub const MACH_SEND_SYNC_USE_THRPRI = MACH_SEND_PROPAGATE_QOS;
pub const MACH_SEND_KERNEL = 0x00400000;
pub const MACH_SEND_SYNC_BOOTSTRAP_CHECKIN = 0x00800000;
pub const MACH_RCV_TIMEOUT = 0x00000100;
pub const MACH_RCV_NOTIFY = 0x00000000;
pub const MACH_RCV_INTERRUPT = 0x00000400;
pub const MACH_RCV_VOUCHER = 0x00000800;
pub const MACH_RCV_OVERWRITE = 0x00000000;
pub const MACH_RCV_GUARDED_DESC = 0x00001000;
pub const MACH_RCV_SYNC_WAIT = 0x00004000;
pub const MACH_RCV_SYNC_PEEK = 0x00008000;
pub const MACH_MSG_STRICT_REPLY = 0x00000200;
pub const exception_type_t = c_int;
pub const mcontext_t = switch (native_arch) {
.aarch64 => extern struct {
es: exception_state,
ss: thread_state,
ns: neon_state,
},
.x86_64 => extern struct {
es: exception_state,
ss: thread_state,
fs: float_state,
},
else => @compileError("unsupported arch"),
};
pub const exception_state = switch (native_arch) {
.aarch64 => extern struct {
far: u64, // Virtual Fault Address
esr: u32, // Exception syndrome
exception: u32, // Number of arm exception taken
},
.x86_64 => extern struct {
trapno: u16,
cpu: u16,
err: u32,
faultvaddr: u64,
},
else => @compileError("unsupported arch"),
};
pub const thread_state = switch (native_arch) {
.aarch64 => extern struct {
/// General purpose registers
regs: [29]u64,
/// Frame pointer x29
fp: u64,
/// Link register x30
lr: u64,
/// Stack pointer x31
sp: u64,
/// Program counter
pc: u64,
/// Current program status register
cpsr: u32,
__pad: u32,
},
.x86_64 => extern struct {
rax: u64,
rbx: u64,
rcx: u64,
rdx: u64,
rdi: u64,
rsi: u64,
rbp: u64,
rsp: u64,
r8: u64,
r9: u64,
r10: u64,
r11: u64,
r12: u64,
r13: u64,
r14: u64,
r15: u64,
rip: u64,
rflags: u64,
cs: u64,
fs: u64,
gs: u64,
},
else => @compileError("unsupported arch"),
};
pub const neon_state = extern struct {
q: [32]u128,
fpsr: u32,
fpcr: u32,
};
pub const float_state = extern struct {
reserved: [2]c_int,
fcw: u16,
fsw: u16,
ftw: u8,
rsrv1: u8,
fop: u16,
ip: u32,
cs: u16,
rsrv2: u16,
dp: u32,
ds: u16,
rsrv3: u16,
mxcsr: u32,
mxcsrmask: u32,
stmm: [8]stmm_reg,
xmm: [16]xmm_reg,
rsrv4: [96]u8,
reserved1: c_int,
};
pub const stmm_reg = [16]u8;
pub const xmm_reg = [16]u8;
pub extern "c" fn NSVersionOfRunTimeLibrary(library_name: [*:0]const u8) u32;
pub extern "c" fn _NSGetExecutablePath(buf: [*:0]u8, bufsize: *u32) c_int;
pub extern "c" fn _dyld_image_count() u32;
pub extern "c" fn _dyld_get_image_header(image_index: u32) ?*mach_header;
pub extern "c" fn _dyld_get_image_vmaddr_slide(image_index: u32) usize;
pub extern "c" fn _dyld_get_image_name(image_index: u32) [*:0]const u8;
pub const COPYFILE = packed struct(u32) {
ACL: bool = false,
STAT: bool = false,
XATTR: bool = false,
DATA: bool = false,
_: u28 = 0,
};
pub const copyfile_state_t = *opaque {};
pub extern "c" fn fcopyfile(from: fd_t, to: fd_t, state: ?copyfile_state_t, flags: COPYFILE) c_int;
pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: [*]u8, buf_len: usize, basep: *i64) isize;
pub extern "c" fn mach_absolute_time() u64;
pub extern "c" fn mach_continuous_time() u64;
pub extern "c" fn mach_timebase_info(tinfo: ?*mach_timebase_info_data) kern_return_t;
pub extern "c" fn kevent64(
kq: c_int,
changelist: [*]const kevent64_s,
nchanges: c_int,
eventlist: [*]kevent64_s,
nevents: c_int,
flags: c_uint,
timeout: ?*const timespec,
) c_int;
pub const mach_hdr = if (@sizeOf(usize) == 8) mach_header_64 else mach_header;
pub const mach_header_64 = std.macho.mach_header_64;
pub const mach_header = std.macho.mach_header;
pub extern "c" fn @"close$NOCANCEL"(fd: fd_t) c_int;
pub extern "c" fn mach_host_self() mach_port_t;
pub extern "c" fn clock_get_time(clock_serv: clock_serv_t, cur_time: *mach_timespec_t) kern_return_t;
pub const exception_data_type_t = integer_t;
pub const exception_data_t = ?*mach_exception_data_type_t;
pub const mach_exception_data_type_t = i64;
pub const mach_exception_data_t = ?*mach_exception_data_type_t;
pub const vm_map_t = mach_port_t;
pub const vm_map_read_t = mach_port_t;
pub const vm_region_flavor_t = c_int;
pub const vm_region_info_t = *c_int;
pub const vm_region_recurse_info_t = *c_int;
pub const mach_vm_address_t = usize;
pub const vm_offset_t = usize;
pub const mach_vm_size_t = u64;
pub const mach_msg_bits_t = c_uint;
pub const mach_msg_id_t = integer_t;
pub const mach_msg_type_number_t = natural_t;
pub const mach_msg_type_name_t = c_uint;
pub const mach_msg_option_t = integer_t;
pub const mach_msg_size_t = natural_t;
pub const mach_msg_timeout_t = natural_t;
pub const mach_port_right_t = natural_t;
pub const task_t = mach_port_t;
pub const thread_port_t = task_t;
pub const thread_t = thread_port_t;
pub const exception_mask_t = c_uint;
pub const exception_mask_array_t = [*]exception_mask_t;
pub const exception_handler_t = mach_port_t;
pub const exception_handler_array_t = [*]exception_handler_t;
pub const exception_port_t = exception_handler_t;
pub const exception_port_array_t = exception_handler_array_t;
pub const exception_flavor_array_t = [*]thread_state_flavor_t;
pub const exception_behavior_t = c_uint;
pub const exception_behavior_array_t = [*]exception_behavior_t;
pub const thread_state_flavor_t = c_int;
pub const ipc_space_t = mach_port_t;
pub const ipc_space_port_t = ipc_space_t;
pub const MACH_PORT_RIGHT = enum(mach_port_right_t) {
SEND = 0,
RECEIVE = 1,
SEND_ONCE = 2,
PORT_SET = 3,
DEAD_NAME = 4,
/// Obsolete right
LABELH = 5,
/// Right not implemented
NUMBER = 6,
};
pub const MACH_MSG_TYPE = enum(mach_msg_type_name_t) {
/// Must hold receive right
MOVE_RECEIVE = 16,
/// Must hold send right(s)
MOVE_SEND = 17,
/// Must hold sendonce right
MOVE_SEND_ONCE = 18,
/// Must hold send right(s)
COPY_SEND = 19,
/// Must hold receive right
MAKE_SEND = 20,
/// Must hold receive right
MAKE_SEND_ONCE = 21,
/// NOT VALID
COPY_RECEIVE = 22,
/// Must hold receive right
DISPOSE_RECEIVE = 24,
/// Must hold send right(s)
DISPOSE_SEND = 25,
/// Must hold sendonce right
DISPOSE_SEND_ONCE = 26,
};
extern "c" var mach_task_self_: mach_port_t;
pub fn mach_task_self() callconv(.c) mach_port_t {
return mach_task_self_;
}
pub extern "c" fn mach_msg(
msg: ?*mach_msg_header_t,
option: mach_msg_option_t,
send_size: mach_msg_size_t,
rcv_size: mach_msg_size_t,
rcv_name: mach_port_name_t,
timeout: mach_msg_timeout_t,
notify: mach_port_name_t,
) kern_return_t;
pub const mach_msg_header_t = extern struct {
msgh_bits: mach_msg_bits_t,
msgh_size: mach_msg_size_t,
msgh_remote_port: mach_port_t,
msgh_local_port: mach_port_t,
msgh_voucher_port: mach_port_name_t,
msgh_id: mach_msg_id_t,
};
pub extern "c" fn task_get_exception_ports(
task: task_t,
exception_mask: exception_mask_t,
masks: exception_mask_array_t,
masks_cnt: *mach_msg_type_number_t,
old_handlers: exception_handler_array_t,
old_behaviors: exception_behavior_array_t,
old_flavors: exception_flavor_array_t,
) kern_return_t;
pub extern "c" fn task_set_exception_ports(
task: task_t,
exception_mask: exception_mask_t,
new_port: mach_port_t,
behavior: exception_behavior_t,
new_flavor: thread_state_flavor_t,
) kern_return_t;
pub const task_read_t = mach_port_t;
pub extern "c" fn task_resume(target_task: task_read_t) kern_return_t;
pub extern "c" fn task_suspend(target_task: task_read_t) kern_return_t;
pub extern "c" fn task_for_pid(target_tport: mach_port_name_t, pid: pid_t, t: *mach_port_name_t) kern_return_t;
pub extern "c" fn pid_for_task(target_tport: mach_port_name_t, pid: *pid_t) kern_return_t;
pub extern "c" fn mach_vm_read(
target_task: vm_map_read_t,
address: mach_vm_address_t,
size: mach_vm_size_t,
data: *vm_offset_t,
data_cnt: *mach_msg_type_number_t,
) kern_return_t;
pub extern "c" fn mach_vm_write(
target_task: vm_map_t,
address: mach_vm_address_t,
data: vm_offset_t,
data_cnt: mach_msg_type_number_t,
) kern_return_t;
pub extern "c" fn mach_vm_region(
target_task: vm_map_t,
address: *mach_vm_address_t,
size: *mach_vm_size_t,
flavor: vm_region_flavor_t,
info: vm_region_info_t,
info_cnt: *mach_msg_type_number_t,
object_name: *mach_port_t,
) kern_return_t;
pub extern "c" fn mach_vm_region_recurse(
target_task: vm_map_t,
address: *mach_vm_address_t,
size: *mach_vm_size_t,
nesting_depth: *natural_t,
info: vm_region_recurse_info_t,
info_cnt: *mach_msg_type_number_t,
) kern_return_t;
pub const vm_inherit_t = u32;
pub const memory_object_offset_t = u64;
pub const vm_behavior_t = i32;
pub const vm32_object_id_t = u32;
pub const vm_object_id_t = u64;
pub const VM = struct {
pub const INHERIT = struct {
pub const SHARE: vm_inherit_t = 0;
pub const COPY: vm_inherit_t = 1;
pub const NONE: vm_inherit_t = 2;
pub const DONATE_COPY: vm_inherit_t = 3;
pub const DEFAULT = COPY;
};
pub const BEHAVIOR = struct {
pub const DEFAULT: vm_behavior_t = 0;
pub const RANDOM: vm_behavior_t = 1;
pub const SEQUENTIAL: vm_behavior_t = 2;
pub const RSEQNTL: vm_behavior_t = 3;
pub const WILLNEED: vm_behavior_t = 4;
pub const DONTNEED: vm_behavior_t = 5;
pub const FREE: vm_behavior_t = 6;
pub const ZERO_WIRED_PAGES: vm_behavior_t = 7;
pub const REUSABLE: vm_behavior_t = 8;
pub const REUSE: vm_behavior_t = 9;
pub const CAN_REUSE: vm_behavior_t = 10;
pub const PAGEOUT: vm_behavior_t = 11;
};
pub const REGION = struct {
pub const BASIC_INFO_64 = 9;
pub const EXTENDED_INFO = 13;
pub const TOP_INFO = 12;
pub const SUBMAP_INFO_COUNT_64: mach_msg_type_number_t = @sizeOf(vm_region_submap_info_64) / @sizeOf(natural_t);
pub const SUBMAP_SHORT_INFO_COUNT_64: mach_msg_type_number_t = @sizeOf(vm_region_submap_short_info_64) / @sizeOf(natural_t);
pub const BASIC_INFO_COUNT: mach_msg_type_number_t = @sizeOf(vm_region_basic_info_64) / @sizeOf(c_int);
pub const EXTENDED_INFO_COUNT: mach_msg_type_number_t = @sizeOf(vm_region_extended_info) / @sizeOf(natural_t);
pub const TOP_INFO_COUNT: mach_msg_type_number_t = @sizeOf(vm_region_top_info) / @sizeOf(natural_t);
};
pub fn MAKE_TAG(tag: u8) u32 {
return @as(u32, tag) << 24;
}
};
pub const vm_region_basic_info_64 = extern struct {
protection: vm_prot_t,
max_protection: vm_prot_t,
inheritance: vm_inherit_t,
shared: boolean_t,
reserved: boolean_t,
offset: memory_object_offset_t,
behavior: vm_behavior_t,
user_wired_count: u16,
};
pub const vm_region_extended_info = extern struct {
protection: vm_prot_t,
user_tag: u32,
pages_resident: u32,
pages_shared_now_private: u32,
pages_swapped_out: u32,
pages_dirtied: u32,
ref_count: u32,
shadow_depth: u16,
external_pager: u8,
share_mode: u8,
pages_reusable: u32,
};
pub const vm_region_top_info = extern struct {
obj_id: u32,
ref_count: u32,
private_pages_resident: u32,
shared_pages_resident: u32,
share_mode: u8,
};
pub const vm_region_submap_info_64 = extern struct {
// present across protection
protection: vm_prot_t,
// max avail through vm_prot
max_protection: vm_prot_t,
// behavior of map/obj on fork
inheritance: vm_inherit_t,
// offset into object/map
offset: memory_object_offset_t,
// user tag on map entry
user_tag: u32,
// only valid for objects
pages_resident: u32,
// only for objects
pages_shared_now_private: u32,
// only for objects
pages_swapped_out: u32,
// only for objects
pages_dirtied: u32,
// obj/map mappers, etc.
ref_count: u32,
// only for obj
shadow_depth: u16,
// only for obj
external_pager: u8,
// see enumeration
share_mode: u8,
// submap vs obj
is_submap: boolean_t,
// access behavior hint
behavior: vm_behavior_t,
// obj/map name, not a handle
object_id: vm32_object_id_t,
user_wired_count: u16,
pages_reusable: u32,
object_id_full: vm_object_id_t,
};
pub const vm_region_submap_short_info_64 = extern struct {
// present access protection
protection: vm_prot_t,
// max avail through vm_prot
max_protection: vm_prot_t,
// behavior of map/obj on fork
inheritance: vm_inherit_t,
// offset into object/map
offset: memory_object_offset_t,
// user tag on map entry
user_tag: u32,
// obj/map mappers, etc
ref_count: u32,
// only for obj
shadow_depth: u16,
// only for obj
external_pager: u8,
// see enumeration
share_mode: u8,
// submap vs obj
is_submap: boolean_t,
// access behavior hint
behavior: vm_behavior_t,
// obj/map name, not a handle
object_id: vm32_object_id_t,
user_wired_count: u16,
};
pub const thread_act_t = mach_port_t;
pub const thread_state_t = *natural_t;
pub const mach_port_array_t = [*]mach_port_t;
pub extern "c" fn task_threads(
target_task: mach_port_t,
init_port_set: *mach_port_array_t,
init_port_count: *mach_msg_type_number_t,
) kern_return_t;
pub extern "c" fn thread_get_state(
thread: thread_act_t,
flavor: thread_flavor_t,
state: thread_state_t,
count: *mach_msg_type_number_t,
) kern_return_t;
pub extern "c" fn thread_set_state(
thread: thread_act_t,
flavor: thread_flavor_t,
new_state: thread_state_t,
count: mach_msg_type_number_t,
) kern_return_t;
pub extern "c" fn thread_info(
thread: thread_act_t,
flavor: thread_flavor_t,
info: thread_info_t,
count: *mach_msg_type_number_t,
) kern_return_t;
pub extern "c" fn thread_resume(thread: thread_act_t) kern_return_t;
pub const THREAD_BASIC_INFO = 3;
pub const THREAD_BASIC_INFO_COUNT: mach_msg_type_number_t = @sizeOf(thread_basic_info) / @sizeOf(natural_t);
pub const THREAD_IDENTIFIER_INFO = 4;
pub const THREAD_IDENTIFIER_INFO_COUNT: mach_msg_type_number_t = @sizeOf(thread_identifier_info) / @sizeOf(natural_t);
pub const thread_flavor_t = natural_t;
pub const thread_info_t = *integer_t;
pub const time_value_t = time_value;
pub const task_policy_flavor_t = natural_t;
pub const task_policy_t = *integer_t;
pub const policy_t = c_int;
pub const time_value = extern struct {
seconds: integer_t,
microseconds: integer_t,
};
pub const thread_basic_info = extern struct {
// user run time
user_time: time_value_t,
// system run time
system_time: time_value_t,
// scaled cpu usage percentage
cpu_usage: integer_t,
// scheduling policy in effect
policy: policy_t,
// run state
run_state: integer_t,
// various flags
flags: integer_t,
// suspend count for thread
suspend_count: integer_t,
// number of seconds that thread has been sleeping
sleep_time: integer_t,
};
pub const thread_identifier_info = extern struct {
/// System-wide unique 64-bit thread id
thread_id: u64,
/// Handle to be used by libproc
thread_handle: u64,
/// libdispatch queue address
dispatch_qaddr: u64,
};
pub const MATTR = struct {
/// Cachability
pub const CACHE = 1;
/// Migrability
pub const MIGRATE = 2;
/// Replicability
pub const REPLICATE = 4;
/// (Generic) turn attribute off
pub const VAL_OFF = 0;
/// (Generic) turn attribute on
pub const VAL_ON = 1;
/// (Generic) return current value
pub const VAL_GET = 2;
/// Flush from all caches
pub const VAL_CACHE_FLUSH = 6;
/// Flush from data caches
pub const VAL_DCACHE_FLUSH = 7;
/// Flush from instruction caches
pub const VAL_ICACHE_FLUSH = 8;
/// Sync I+D caches
pub const VAL_CACHE_SYNC = 9;
/// Get page info (stats)
pub const VAL_GET_INFO = 10;
};
pub const TASK_VM_INFO = 22;
pub const TASK_VM_INFO_COUNT: mach_msg_type_number_t = @sizeOf(task_vm_info_data_t) / @sizeOf(natural_t);
pub const task_vm_info = extern struct {
// virtual memory size (bytes)
virtual_size: mach_vm_size_t,
// number of memory regions
region_count: integer_t,
page_size: integer_t,
// resident memory size (bytes)
resident_size: mach_vm_size_t,
// peak resident size (bytes)
resident_size_peak: mach_vm_size_t,
device: mach_vm_size_t,
device_peak: mach_vm_size_t,
internal: mach_vm_size_t,
internal_peak: mach_vm_size_t,
external: mach_vm_size_t,
external_peak: mach_vm_size_t,
reusable: mach_vm_size_t,
reusable_peak: mach_vm_size_t,
purgeable_volatile_pmap: mach_vm_size_t,
purgeable_volatile_resident: mach_vm_size_t,
purgeable_volatile_virtual: mach_vm_size_t,
compressed: mach_vm_size_t,
compressed_peak: mach_vm_size_t,
compressed_lifetime: mach_vm_size_t,
// added for rev1
phys_footprint: mach_vm_size_t,
// added for rev2
min_address: mach_vm_address_t,
max_address: mach_vm_address_t,
// added for rev3
ledger_phys_footprint_peak: i64,
ledger_purgeable_nonvolatile: i64,
ledger_purgeable_novolatile_compressed: i64,
ledger_purgeable_volatile: i64,
ledger_purgeable_volatile_compressed: i64,
ledger_tag_network_nonvolatile: i64,
ledger_tag_network_nonvolatile_compressed: i64,
ledger_tag_network_volatile: i64,
ledger_tag_network_volatile_compressed: i64,
ledger_tag_media_footprint: i64,
ledger_tag_media_footprint_compressed: i64,
ledger_tag_media_nofootprint: i64,
ledger_tag_media_nofootprint_compressed: i64,
ledger_tag_graphics_footprint: i64,
ledger_tag_graphics_footprint_compressed: i64,
ledger_tag_graphics_nofootprint: i64,
ledger_tag_graphics_nofootprint_compressed: i64,
ledger_tag_neural_footprint: i64,
ledger_tag_neural_footprint_compressed: i64,
ledger_tag_neural_nofootprint: i64,
ledger_tag_neural_nofootprint_compressed: i64,
// added for rev4
limit_bytes_remaining: u64,
// added for rev5
decompressions: integer_t,
};
pub const task_vm_info_data_t = task_vm_info;
pub const vm_prot_t = c_int;
pub const boolean_t = c_int;
pub extern "c" fn mach_vm_protect(
target_task: vm_map_t,
address: mach_vm_address_t,
size: mach_vm_size_t,
set_maximum: boolean_t,
new_protection: vm_prot_t,
) kern_return_t;
pub extern "c" fn mach_port_allocate(
task: ipc_space_t,
right: mach_port_right_t,
name: *mach_port_name_t,
) kern_return_t;
pub extern "c" fn mach_port_deallocate(task: ipc_space_t, name: mach_port_name_t) kern_return_t;
pub extern "c" fn mach_port_insert_right(
task: ipc_space_t,
name: mach_port_name_t,
poly: mach_port_t,
poly_poly: mach_msg_type_name_t,
) kern_return_t;
pub extern "c" fn task_info(
target_task: task_name_t,
flavor: task_flavor_t,
task_info_out: task_info_t,
task_info_outCnt: *mach_msg_type_number_t,
) kern_return_t;
pub const mach_task_basic_info = extern struct {
/// Virtual memory size (bytes)
virtual_size: mach_vm_size_t,
/// Resident memory size (bytes)
resident_size: mach_vm_size_t,
/// Total user run time for terminated threads
user_time: time_value_t,
/// Total system run time for terminated threads
system_time: time_value_t,
/// Default policy for new threads
policy: policy_t,
/// Suspend count for task
suspend_count: mach_vm_size_t,
};
pub const MACH_TASK_BASIC_INFO = 20;
pub const MACH_TASK_BASIC_INFO_COUNT: mach_msg_type_number_t = @sizeOf(mach_task_basic_info) / @sizeOf(natural_t);
pub extern "c" fn _host_page_size(task: mach_port_t, size: *vm_size_t) kern_return_t;
pub extern "c" fn vm_deallocate(target_task: vm_map_t, address: vm_address_t, size: vm_size_t) kern_return_t;
pub extern "c" fn vm_machine_attribute(
target_task: vm_map_t,
address: vm_address_t,
size: vm_size_t,
attribute: vm_machine_attribute_t,
value: *vm_machine_attribute_val_t,
) kern_return_t;
pub extern "c" fn sendfile(
in_fd: fd_t,
out_fd: fd_t,
offset: off_t,
len: *off_t,
sf_hdtr: ?*sf_hdtr,
flags: u32,
) c_int;
// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/_types.h#L74
pub const sigset_t = u32;
// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/signal.h#L76
pub const NSIG = 32;
pub const qos_class_t = enum(c_uint) {
/// highest priority QOS class for critical tasks
QOS_CLASS_USER_INTERACTIVE = 0x21,
/// slightly more moderate priority QOS class
QOS_CLASS_USER_INITIATED = 0x19,
/// default QOS class when none is set
QOS_CLASS_DEFAULT = 0x15,
/// more energy efficient QOS class than default
QOS_CLASS_UTILITY = 0x11,
/// QOS class more appropriate for background tasks
QOS_CLASS_BACKGROUND = 0x09,
/// QOS class as a return value
QOS_CLASS_UNSPECIFIED = 0x00,
};
// Grand Central Dispatch is exposed by libSystem.
pub extern "c" fn dispatch_release(object: *anyopaque) void;
pub const dispatch_semaphore_t = *opaque {};
pub extern "c" fn dispatch_semaphore_create(value: isize) ?dispatch_semaphore_t;
pub extern "c" fn dispatch_semaphore_wait(dsema: dispatch_semaphore_t, timeout: dispatch_time_t) isize;
pub extern "c" fn dispatch_semaphore_signal(dsema: dispatch_semaphore_t) isize;
pub const dispatch_time_t = u64;
pub const DISPATCH_TIME_NOW = @as(dispatch_time_t, 0);
pub const DISPATCH_TIME_FOREVER = ~@as(dispatch_time_t, 0);
pub extern "c" fn dispatch_time(when: dispatch_time_t, delta: i64) dispatch_time_t;
const dispatch_once_t = usize;
const dispatch_function_t = fn (?*anyopaque) callconv(.c) void;
pub extern fn dispatch_once_f(
predicate: *dispatch_once_t,
context: ?*anyopaque,
function: dispatch_function_t,
) void;
/// Undocumented futex-like API available on darwin 16+
/// (macOS 10.12+, iOS 10.0+, tvOS 10.0+, watchOS 3.0+, catalyst 13.0+).
///
/// [ulock.h]: https://github.com/apple/darwin-xnu/blob/master/bsd/sys/ulock.h
/// [sys_ulock.c]: https://github.com/apple/darwin-xnu/blob/master/bsd/kern/sys_ulock.c
pub const UL = packed struct(u32) {
op: Op,
WAKE_ALL: bool = false,
WAKE_THREAD: bool = false,
_10: u6 = 0,
WAIT_WORKQ_DATA_CONTENTION: bool = false,
WAIT_CANCEL_POINT: bool = false,
WAIT_ADAPTIVE_SPIN: bool = false,
_19: u5 = 0,
NO_ERRNO: bool = false,
_: u7 = 0,
pub const Op = enum(u8) {
COMPARE_AND_WAIT = 1,
UNFAIR_LOCK = 2,
COMPARE_AND_WAIT_SHARED = 3,
UNFAIR_LOCK64_SHARED = 4,
COMPARE_AND_WAIT64 = 5,
COMPARE_AND_WAIT64_SHARED = 6,
};
};
pub extern "c" fn __ulock_wait2(op: UL, addr: ?*const anyopaque, val: u64, timeout_ns: u64, val2: u64) c_int;
pub extern "c" fn __ulock_wait(op: UL, addr: ?*const anyopaque, val: u64, timeout_us: u32) c_int;
pub extern "c" fn __ulock_wake(op: UL, addr: ?*const anyopaque, val: u64) c_int;
pub const os_unfair_lock_t = *os_unfair_lock;
pub const os_unfair_lock = extern struct {
_os_unfair_lock_opaque: u32 = 0,
};
pub extern "c" fn os_unfair_lock_lock(o: os_unfair_lock_t) void;
pub extern "c" fn os_unfair_lock_unlock(o: os_unfair_lock_t) void;
pub extern "c" fn os_unfair_lock_trylock(o: os_unfair_lock_t) bool;
pub extern "c" fn os_unfair_lock_assert_owner(o: os_unfair_lock_t) void;
pub extern "c" fn os_unfair_lock_assert_not_owner(o: os_unfair_lock_t) void;
pub const os_signpost_id_t = u64;
pub const OS_SIGNPOST_ID_NULL: os_signpost_id_t = 0;
pub const OS_SIGNPOST_ID_INVALID: os_signpost_id_t = !0;
pub const OS_SIGNPOST_ID_EXCLUSIVE: os_signpost_id_t = 0xeeeeb0b5b2b2eeee;
pub const os_log_t = *opaque {};
pub const os_log_type_t = enum(u8) {
/// default messages always captures
OS_LOG_TYPE_DEFAULT = 0x00,
/// messages with additional infos
OS_LOG_TYPE_INFO = 0x01,
/// debug messages
OS_LOG_TYPE_DEBUG = 0x02,
/// error messages
OS_LOG_TYPE_ERROR = 0x10,
/// unexpected conditions messages
OS_LOG_TYPE_FAULT = 0x11,
};
pub const OS_LOG_CATEGORY_POINTS_OF_INTEREST: *const u8 = "PointsOfInterest";
pub const OS_LOG_CATEGORY_DYNAMIC_TRACING: *const u8 = "DynamicTracing";
pub const OS_LOG_CATEGORY_DYNAMIC_STACK_TRACING: *const u8 = "DynamicStackTracing";
pub extern "c" fn os_log_create(subsystem: [*]const u8, category: [*]const u8) os_log_t;
pub extern "c" fn os_log_type_enabled(log: os_log_t, tpe: os_log_type_t) bool;
pub extern "c" fn os_signpost_id_generate(log: os_log_t) os_signpost_id_t;
pub extern "c" fn os_signpost_interval_begin(log: os_log_t, signpos: os_signpost_id_t, func: [*]const u8, ...) void;
pub extern "c" fn os_signpost_interval_end(log: os_log_t, signpos: os_signpost_id_t, func: [*]const u8, ...) void;
pub extern "c" fn os_signpost_id_make_with_pointer(log: os_log_t, ptr: ?*anyopaque) os_signpost_id_t;
pub extern "c" fn os_signpost_enabled(log: os_log_t) bool;
pub extern "c" fn pthread_setname_np(name: [*:0]const u8) c_int;
pub extern "c" fn pthread_attr_set_qos_class_np(attr: *pthread_attr_t, qos_class: qos_class_t, relative_priority: c_int) c_int;
pub extern "c" fn pthread_attr_get_qos_class_np(attr: *pthread_attr_t, qos_class: *qos_class_t, relative_priority: *c_int) c_int;
pub extern "c" fn pthread_set_qos_class_self_np(qos_class: qos_class_t, relative_priority: c_int) c_int;
pub extern "c" fn pthread_get_qos_class_np(pthread: std.c.pthread_t, qos_class: *qos_class_t, relative_priority: *c_int) c_int;
pub const mach_timebase_info_data = extern struct {
numer: u32,
denom: u32,
};
pub const kevent64_s = extern struct {
ident: u64,
filter: i16,
flags: u16,
fflags: u32,
data: i64,
udata: u64,
ext: [2]u64,
};
// sys/types.h on macos uses #pragma pack() so these checks are
// to make sure the struct is laid out the same. These values were
// produced from C code using the offsetof macro.
comptime {
if (builtin.target.os.tag.isDarwin()) {
assert(@offsetOf(kevent64_s, "ident") == 0);
assert(@offsetOf(kevent64_s, "filter") == 8);
assert(@offsetOf(kevent64_s, "flags") == 10);
assert(@offsetOf(kevent64_s, "fflags") == 12);
assert(@offsetOf(kevent64_s, "data") == 16);
assert(@offsetOf(kevent64_s, "udata") == 24);
assert(@offsetOf(kevent64_s, "ext") == 32);
}
}
pub const clock_serv_t = mach_port_t;
pub const clock_res_t = c_int;
pub const mach_port_name_t = natural_t;
pub const natural_t = c_uint;
pub const mach_timespec_t = extern struct {
sec: c_uint,
nsec: clock_res_t,