authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-12 20:04:46+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-12 20:04:46+01:00
log6f5eb9927d5f7713ebc0d104c0590fb037afe275
treedad91384a447c4214c969d0b697ad939c00ee484
parent612b9f4da1a682eeb4947e473199c5231ae10be0

darwin: add more defs and wrappers


2 files changed, 279 insertions(+), 46 deletions(-)

lib/std/c/darwin.zig+242-46
......@@ -18,46 +18,51 @@ const arch_bits = switch (native_arch) {
1818pub const EXC_TYPES_COUNT = arch_bits.EXC_TYPES_COUNT;
1919pub const THREAD_STATE_NONE = arch_bits.THREAD_STATE_NONE;
2020
21/// Could not access memory
22pub const EXC_BAD_ACCESS = 1;
23/// Instruction failed
24pub const EXC_BAD_INSTRUCTION = 2;
25/// Arithmetic exception
26pub const EXC_ARITHMETIC = 3;
27/// Emulation instruction
28pub const EXC_EMULATION = 4;
29/// Software generated exception
30pub const EXC_SOFTWARE = 5;
31/// Trace, breakpoint, etc.
32pub const EXC_BREAKPOINT = 6;
33/// System calls.
34pub const EXC_SYSCALL = 7;
35/// Mach system calls.
36pub const EXC_MACH_SYSCALL = 8;
37/// RPC alert
38pub const EXC_RPC_ALERT = 9;
39/// Abnormal process exit
40pub const EXC_CRASH = 10;
41/// Hit resource consumption limit
42pub const EXC_RESOURCE = 11;
43/// Violated guarded resource protections
44pub const EXC_GUARD = 12;
45/// Abnormal process exited to corpse state
46pub const EXC_CORPSE_NOTIFY = 13;
47
48pub const EXC_MASK_BAD_ACCESS = 1 << EXC_BAD_ACCESS;
49pub const EXC_MASK_BAD_INSTRUCTION = 1 << EXC_BAD_INSTRUCTION;
50pub const EXC_MASK_ARITHMETIC = 1 << EXC_ARITHMETIC;
51pub const EXC_MASK_EMULATION = 1 << EXC_EMULATION;
52pub const EXC_MASK_SOFTWARE = 1 << EXC_SOFTWARE;
53pub const EXC_MASK_BREAKPOINT = 1 << EXC_BREAKPOINT;
54pub const EXC_MASK_SYSCALL = 1 << EXC_SYSCALL;
55pub const EXC_MASK_MACH_SYSCALL = 1 << EXC_MACH_SYSCALL;
56pub const EXC_MASK_RPC_ALERT = 1 << EXC_RPC_ALERT;
57pub const EXC_MASK_CRASH = 1 << EXC_CRASH;
58pub const EXC_MASK_RESOURCE = 1 << EXC_RESOURCE;
59pub const EXC_MASK_GUARD = 1 << EXC_GUARD;
60pub const EXC_MASK_CORPSE_NOTIFY = 1 << EXC_CORPSE_NOTIFY;
21pub const EXC = enum(exception_type_t) {
22 NULL = 0,
23 /// Could not access memory
24 BAD_ACCESS = 1,
25 /// Instruction failed
26 BAD_INSTRUCTION = 2,
27 /// Arithmetic exception
28 ARITHMETIC = 3,
29 /// Emulation instruction
30 EMULATION = 4,
31 /// Software generated exception
32 SOFTWARE = 5,
33 /// Trace, breakpoint, etc.
34 BREAKPOINT = 6,
35 /// System calls.
36 SYSCALL = 7,
37 /// Mach system calls.
38 MACH_SYSCALL = 8,
39 /// RPC alert
40 RPC_ALERT = 9,
41 /// Abnormal process exit
42 CRASH = 10,
43 /// Hit resource consumption limit
44 RESOURCE = 11,
45 /// Violated guarded resource protections
46 GUARD = 12,
47 /// Abnormal process exited to corpse state
48 CORPSE_NOTIFY = 13,
49};
50
51pub const EXC_SOFT_SIGNAL = 0x10003;
52
53pub const EXC_MASK_BAD_ACCESS = 1 << @enumToInt(EXC.BAD_ACCESS);
54pub const EXC_MASK_BAD_INSTRUCTION = 1 << @enumToInt(EXC.BAD_INSTRUCTION);
55pub const EXC_MASK_ARITHMETIC = 1 << @enumToInt(EXC.ARITHMETIC);
56pub const EXC_MASK_EMULATION = 1 << @enumToInt(EXC.EMULATION);
57pub const EXC_MASK_SOFTWARE = 1 << @enumToInt(EXC.SOFTWARE);
58pub const EXC_MASK_BREAKPOINT = 1 << @enumToInt(EXC.BREAKPOINT);
59pub const EXC_MASK_SYSCALL = 1 << @enumToInt(EXC.SYSCALL);
60pub const EXC_MASK_MACH_SYSCALL = 1 << @enumToInt(EXC.MACH_SYSCALL);
61pub const EXC_MASK_RPC_ALERT = 1 << @enumToInt(EXC.RPC_ALERT);
62pub const EXC_MASK_CRASH = 1 << @enumToInt(EXC.CRASH);
63pub const EXC_MASK_RESOURCE = 1 << @enumToInt(EXC.RESOURCE);
64pub const EXC_MASK_GUARD = 1 << @enumToInt(EXC.GUARD);
65pub const EXC_MASK_CORPSE_NOTIFY = 1 << @enumToInt(EXC.CORPSE_NOTIFY);
6166pub const EXC_MASK_MACHINE = arch_bits.EXC_MASK_MACHINE;
6267
6368pub const EXC_MASK_ALL = EXC_MASK_BAD_ACCESS |
......@@ -95,6 +100,44 @@ pub const MACH_EXCEPTION_MASK = MACH_EXCEPTION_CODES |
95100 MACH_EXCEPTION_ERRORS |
96101 MACH_EXCEPTION_BACKTRACE_PREFERRED;
97102
103pub const TASK_NULL: task_t = 0;
104pub const THREAD_NULL: thread_t = 0;
105
106pub const MACH_MSG_OPTION_NONE = 0x00000000;
107
108pub const MACH_SEND_MSG = 0x00000001;
109pub const MACH_RCV_MSG = 0x00000002;
110
111pub const MACH_RCV_LARGE = 0x00000004;
112pub const MACH_RCV_LARGE_IDENTITY = 0x00000008;
113
114pub const MACH_SEND_TIMEOUT = 0x00000010;
115pub const MACH_SEND_OVERRIDE = 0x00000020;
116pub const MACH_SEND_INTERRUPT = 0x00000040;
117pub const MACH_SEND_NOTIFY = 0x00000080;
118pub const MACH_SEND_ALWAYS = 0x00010000;
119pub const MACH_SEND_FILTER_NONFATAL = 0x00010000;
120pub const MACH_SEND_TRAILER = 0x00020000;
121pub const MACH_SEND_NOIMPORTANCE = 0x00040000;
122pub const MACH_SEND_NODENAP = MACH_SEND_NOIMPORTANCE;
123pub const MACH_SEND_IMPORTANCE = 0x00080000;
124pub const MACH_SEND_SYNC_OVERRIDE = 0x00100000;
125pub const MACH_SEND_PROPAGATE_QOS = 0x00200000;
126pub const MACH_SEND_SYNC_USE_THRPRI = MACH_SEND_PROPAGATE_QOS;
127pub const MACH_SEND_KERNEL = 0x00400000;
128pub const MACH_SEND_SYNC_BOOTSTRAP_CHECKIN = 0x00800000;
129
130pub const MACH_RCV_TIMEOUT = 0x00000100;
131pub const MACH_RCV_NOTIFY = 0x00000000;
132pub const MACH_RCV_INTERRUPT = 0x00000400;
133pub const MACH_RCV_VOUCHER = 0x00000800;
134pub const MACH_RCV_OVERWRITE = 0x00000000;
135pub const MACH_RCV_GUARDED_DESC = 0x00001000;
136pub const MACH_RCV_SYNC_WAIT = 0x00004000;
137pub const MACH_RCV_SYNC_PEEK = 0x00008000;
138
139pub const MACH_MSG_STRICT_REPLY = 0x00000200;
140
98141pub const ucontext_t = extern struct {
99142 onstack: c_int,
100143 sigmask: sigset_t,
......@@ -235,6 +278,11 @@ pub extern "c" fn @"close$NOCANCEL"(fd: fd_t) c_int;
235278pub extern "c" fn mach_host_self() mach_port_t;
236279pub extern "c" fn clock_get_time(clock_serv: clock_serv_t, cur_time: *mach_timespec_t) kern_return_t;
237280
281pub const exception_type_t = c_int;
282pub const exception_data_type_t = integer_t;
283pub const exception_data_t = ?*mach_exception_data_type_t;
284pub const mach_exception_data_type_t = i64;
285pub const mach_exception_data_t = ?*mach_exception_data_type_t;
238286pub const vm_map_t = mach_port_t;
239287pub const vm_map_read_t = mach_port_t;
240288pub const vm_region_flavor_t = c_int;
......@@ -243,20 +291,27 @@ pub const vm_region_recurse_info_t = *c_int;
243291pub const mach_vm_address_t = usize;
244292pub const vm_offset_t = usize;
245293pub const mach_vm_size_t = u64;
294pub const mach_msg_bits_t = c_uint;
295pub const mach_msg_id_t = integer_t;
246296pub const mach_msg_type_number_t = natural_t;
247pub const mach_msg_type_name_t = u32;
297pub const mach_msg_type_name_t = c_uint;
298pub const mach_msg_option_t = integer_t;
299pub const mach_msg_size_t = natural_t;
300pub const mach_msg_timeout_t = natural_t;
248301pub const mach_port_right_t = natural_t;
249302pub const task_t = mach_port_t;
250pub const exception_mask_t = u32;
303pub const thread_port_t = task_t;
304pub const thread_t = thread_port_t;
305pub const exception_mask_t = c_uint;
251306pub const exception_mask_array_t = [*]exception_mask_t;
252307pub const exception_handler_t = mach_port_t;
253308pub const exception_handler_array_t = [*]exception_handler_t;
254309pub const exception_port_t = exception_handler_t;
255310pub const exception_port_array_t = exception_handler_array_t;
256311pub const exception_flavor_array_t = [*]thread_state_flavor_t;
257pub const exception_behavior_t = i32;
312pub const exception_behavior_t = c_uint;
258313pub const exception_behavior_array_t = [*]exception_behavior_t;
259pub const thread_state_flavor_t = i32;
314pub const thread_state_flavor_t = c_int;
260315pub const ipc_space_t = mach_port_t;
261316pub const ipc_space_port_t = ipc_space_t;
262317
......@@ -309,6 +364,25 @@ pub fn mach_task_self() callconv(.C) mach_port_t {
309364 return mach_task_self_;
310365}
311366
367pub extern "c" fn mach_msg(
368 msg: ?*mach_msg_header_t,
369 option: mach_msg_option_t,
370 send_size: mach_msg_size_t,
371 rcv_size: mach_msg_size_t,
372 rcv_name: mach_port_name_t,
373 timeout: mach_msg_timeout_t,
374 notify: mach_port_name_t,
375) kern_return_t;
376
377pub const mach_msg_header_t = extern struct {
378 msgh_bits: mach_msg_bits_t,
379 msgh_size: mach_msg_size_t,
380 msgh_remote_port: mach_port_t,
381 msgh_local_port: mach_port_t,
382 msgh_voucher_port: mach_port_name_t,
383 msgh_id: mach_msg_id_t,
384};
385
312386pub extern "c" fn task_get_exception_ports(
313387 task: task_t,
314388 exception_mask: exception_mask_t,
......@@ -681,6 +755,30 @@ pub extern "c" fn task_info(
681755 task_info_out: task_info_t,
682756 task_info_outCnt: *mach_msg_type_number_t,
683757) kern_return_t;
758
759pub const mach_task_basic_info = extern struct {
760 /// Virtual memory size (bytes)
761 virtual_size: mach_vm_size_t,
762
763 /// Resident memory size (bytes)
764 resident_size: mach_vm_size_t,
765
766 /// Total user run time for terminated threads
767 user_time: time_value_t,
768
769 /// Total system run time for terminated threads
770 system_time: time_value_t,
771
772 /// Default policy for new threads
773 policy: policy_t,
774
775 /// Suspend count for task
776 suspend_count: mach_vm_size_t,
777};
778
779pub const MACH_TASK_BASIC_INFO = 20;
780pub const MACH_TASK_BASIC_INFO_COUNT: mach_msg_type_number_t = @sizeOf(mach_task_basic_info) / @sizeOf(natural_t);
781
684782pub extern "c" fn _host_page_size(task: mach_port_t, size: *vm_size_t) kern_return_t;
685783pub extern "c" fn vm_deallocate(target_task: vm_map_t, address: vm_address_t, size: vm_size_t) kern_return_t;
686784pub extern "c" fn vm_machine_attribute(
......@@ -2052,11 +2150,11 @@ pub const E = enum(u16) {
20522150};
20532151
20542152pub fn getKernError(err: kern_return_t) KernE {
2055 return @intToEnum(KernE, @truncate(u8, @intCast(usize, err)));
2153 return @intToEnum(KernE, @truncate(u32, @intCast(usize, err)));
20562154}
20572155
20582156/// Kernel return values
2059pub const KernE = enum(u8) {
2157pub const KernE = enum(u32) {
20602158 SUCCESS = 0,
20612159
20622160 /// Specified address is not currently valid
......@@ -2275,6 +2373,104 @@ pub const KernE = enum(u8) {
22752373 _,
22762374};
22772375
2376pub const mach_msg_return_t = kern_return_t;
2377
2378pub fn getMachMsgError(err: mach_msg_return_t) MachMsgE {
2379 return @intToEnum(MachMsgE, @truncate(u32, @intCast(usize, err)));
2380}
2381
2382/// All special error code bits defined below.
2383pub const MACH_MSG_MASK: u32 = 0x3e00;
2384/// No room in IPC name space for another capability name.
2385pub const MACH_MSG_IPC_SPACE: u32 = 0x2000;
2386/// No room in VM address space for out-of-line memory.
2387pub const MACH_MSG_VM_SPACE: u32 = 0x1000;
2388/// Kernel resource shortage handling out-of-line memory.
2389pub const MACH_MSG_IPC_KERNEL: u32 = 0x800;
2390/// Kernel resource shortage handling an IPC capability.
2391pub const MACH_MSG_VM_KERNEL: u32 = 0x400;
2392
2393/// Mach msg return values
2394pub const MachMsgE = enum(u32) {
2395 SUCCESS = 0x00000000,
2396
2397 /// Thread is waiting to send. (Internal use only.)
2398 SEND_IN_PROGRESS = 0x10000001,
2399 /// Bogus in-line data.
2400 SEND_INVALID_DATA = 0x10000002,
2401 /// Bogus destination port.
2402 SEND_INVALID_DEST = 0x10000003,
2403 /// Message not sent before timeout expired.
2404 SEND_TIMED_OUT = 0x10000004,
2405 /// Bogus voucher port.
2406 SEND_INVALID_VOUCHER = 0x10000005,
2407 /// Software interrupt.
2408 SEND_INTERRUPTED = 0x10000007,
2409 /// Data doesn't contain a complete message.
2410 SEND_MSG_TOO_SMALL = 0x10000008,
2411 /// Bogus reply port.
2412 SEND_INVALID_REPLY = 0x10000009,
2413 /// Bogus port rights in the message body.
2414 SEND_INVALID_RIGHT = 0x1000000a,
2415 /// Bogus notify port argument.
2416 SEND_INVALID_NOTIFY = 0x1000000b,
2417 /// Invalid out-of-line memory pointer.
2418 SEND_INVALID_MEMORY = 0x1000000c,
2419 /// No message buffer is available.
2420 SEND_NO_BUFFER = 0x1000000d,
2421 /// Send is too large for port
2422 SEND_TOO_LARGE = 0x1000000e,
2423 /// Invalid msg-type specification.
2424 SEND_INVALID_TYPE = 0x1000000f,
2425 /// A field in the header had a bad value.
2426 SEND_INVALID_HEADER = 0x10000010,
2427 /// The trailer to be sent does not match kernel format.
2428 SEND_INVALID_TRAILER = 0x10000011,
2429 /// The sending thread context did not match the context on the dest port
2430 SEND_INVALID_CONTEXT = 0x10000012,
2431 /// compatibility: no longer a returned error
2432 SEND_INVALID_RT_OOL_SIZE = 0x10000015,
2433 /// The destination port doesn't accept ports in body
2434 SEND_NO_GRANT_DEST = 0x10000016,
2435 /// Message send was rejected by message filter
2436 SEND_MSG_FILTERED = 0x10000017,
2437
2438 /// Thread is waiting for receive. (Internal use only.)
2439 RCV_IN_PROGRESS = 0x10004001,
2440 /// Bogus name for receive port/port-set.
2441 RCV_INVALID_NAME = 0x10004002,
2442 /// Didn't get a message within the timeout value.
2443 RCV_TIMED_OUT = 0x10004003,
2444 /// Message buffer is not large enough for inline data.
2445 RCV_TOO_LARGE = 0x10004004,
2446 /// Software interrupt.
2447 RCV_INTERRUPTED = 0x10004005,
2448 /// compatibility: no longer a returned error
2449 RCV_PORT_CHANGED = 0x10004006,
2450 /// Bogus notify port argument.
2451 RCV_INVALID_NOTIFY = 0x10004007,
2452 /// Bogus message buffer for inline data.
2453 RCV_INVALID_DATA = 0x10004008,
2454 /// Port/set was sent away/died during receive.
2455 RCV_PORT_DIED = 0x10004009,
2456 /// compatibility: no longer a returned error
2457 RCV_IN_SET = 0x1000400a,
2458 /// Error receiving message header. See special bits.
2459 RCV_HEADER_ERROR = 0x1000400b,
2460 /// Error receiving message body. See special bits.
2461 RCV_BODY_ERROR = 0x1000400c,
2462 /// Invalid msg-type specification in scatter list.
2463 RCV_INVALID_TYPE = 0x1000400d,
2464 /// Out-of-line overwrite region is not large enough
2465 RCV_SCATTER_SMALL = 0x1000400e,
2466 /// trailer type or number of trailer elements not supported
2467 RCV_INVALID_TRAILER = 0x1000400f,
2468 /// Waiting for receive with timeout. (Internal use only.)
2469 RCV_IN_PROGRESS_TIMED = 0x10004011,
2470 /// invalid reply port used in a STRICT_REPLY message
2471 RCV_INVALID_REPLY = 0x10004012,
2472};
2473
22782474pub const SIGSTKSZ = 131072;
22792475pub const MINSIGSTKSZ = 32768;
22802476
lib/std/os/darwin.zig+37
......@@ -401,6 +401,43 @@ const mach_task = if (builtin.target.isDarwin()) struct {
401401 },
402402 }
403403 }
404
405 pub fn basicTaskInfo(task: MachTask) MachError!std.c.mach_task_basic_info {
406 var info: std.c.mach_task_basic_info = undefined;
407 var count = std.c.MACH_TASK_BASIC_INFO_COUNT;
408 switch (std.c.getKernError(std.c.task_info(
409 task.port,
410 std.c.MACH_TASK_BASIC_INFO,
411 @ptrCast(std.c.task_info_t, &info),
412 &count,
413 ))) {
414 .SUCCESS => return info,
415 else => |err| {
416 log.err("task_info kernel call failed with error code: {s}", .{@tagName(err)});
417 return error.Unexpected;
418 },
419 }
420 }
421
422 pub fn @"resume"(task: MachTask) MachError!void {
423 switch (std.c.getKernError(std.c.task_resume(task.port))) {
424 .SUCCESS => {},
425 else => |err| {
426 log.err("task_resume kernel call failed with error code: {s}", .{@tagName(err)});
427 return error.Unexpected;
428 },
429 }
430 }
431
432 pub fn @"suspend"(task: MachTask) MachError!void {
433 switch (std.c.getKernError(std.c.task_suspend(task.port))) {
434 .SUCCESS => {},
435 else => |err| {
436 log.err("task_suspend kernel call failed with error code: {s}", .{@tagName(err)});
437 return error.Unexpected;
438 },
439 }
440 }
404441 };
405442
406443 pub fn machTaskForPid(pid: std.os.pid_t) MachError!MachTask {