zx_types/
lib.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#![allow(non_camel_case_types)]
6
7use std::fmt::{self, Debug};
8use std::hash::{Hash, Hasher};
9use std::sync::atomic::AtomicI32;
10#[cfg(feature = "zerocopy")]
11use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout};
12
13pub type zx_addr_t = usize;
14pub type zx_stream_seek_origin_t = u32;
15pub type zx_clock_t = u32;
16pub type zx_duration_t = i64;
17pub type zx_duration_mono_t = i64;
18pub type zx_duration_mono_ticks_t = i64;
19pub type zx_duration_boot_t = i64;
20pub type zx_duration_boot_ticks_t = i64;
21pub type zx_futex_t = AtomicI32;
22pub type zx_gpaddr_t = usize;
23pub type zx_vcpu_option_t = u32;
24pub type zx_guest_trap_t = u32;
25pub type zx_handle_t = u32;
26pub type zx_handle_op_t = u32;
27pub type zx_koid_t = u64;
28pub type zx_obj_type_t = u32;
29pub type zx_object_info_topic_t = u32;
30pub type zx_info_maps_type_t = u32;
31pub type zx_instant_boot_t = i64;
32pub type zx_instant_boot_ticks_t = i64;
33pub type zx_instant_mono_t = i64;
34pub type zx_instant_mono_ticks_t = i64;
35pub type zx_iob_access_t = u32;
36pub type zx_iob_allocate_id_options_t = u32;
37pub type zx_iob_discipline_type_t = u64;
38pub type zx_iob_region_type_t = u32;
39pub type zx_iob_write_options_t = u64;
40pub type zx_off_t = u64;
41pub type zx_paddr_t = usize;
42pub type zx_rights_t = u32;
43pub type zx_rsrc_flags_t = u32;
44pub type zx_rsrc_kind_t = u32;
45pub type zx_signals_t = u32;
46pub type zx_ssize_t = isize;
47pub type zx_status_t = i32;
48pub type zx_rsrc_system_base_t = u64;
49pub type zx_ticks_t = i64;
50pub type zx_time_t = i64;
51pub type zx_txid_t = u32;
52pub type zx_vaddr_t = usize;
53pub type zx_vm_option_t = u32;
54pub type zx_thread_state_topic_t = u32;
55pub type zx_vcpu_state_topic_t = u32;
56pub type zx_restricted_reason_t = u64;
57pub type zx_processor_power_level_options_t = u64;
58pub type zx_processor_power_control_t = u64;
59pub type zx_system_memory_stall_type_t = u32;
60pub type zx_system_suspend_option_t = u64;
61pub type zx_system_wake_report_entry_flag_t = u32;
62
63macro_rules! const_assert {
64    ($e:expr $(,)?) => {
65        const _: [(); 1 - { const ASSERT: bool = $e; ASSERT as usize }] = [];
66    };
67}
68macro_rules! const_assert_eq {
69    ($lhs:expr, $rhs:expr $(,)?) => {
70        const_assert!($lhs == $rhs);
71    };
72}
73
74// TODO: magically coerce this to &`static str somehow?
75#[repr(C)]
76#[derive(Debug, Copy, Clone, Eq, PartialEq)]
77pub struct zx_string_view_t {
78    pub c_str: *const u8, // Guaranteed NUL-terminated valid UTF-8.
79    pub length: usize,
80}
81
82pub const ZX_MAX_NAME_LEN: usize = 32;
83
84// TODO: combine these macros with the bitflags and assoc consts macros below
85// so that we only have to do one macro invocation.
86// The result would look something like:
87// multiconst!(bitflags, zx_rights_t, Rights, [RIGHT_NONE => ZX_RIGHT_NONE = 0; ...]);
88// multiconst!(assoc_consts, zx_status_t, Status, [OK => ZX_OK = 0; ...]);
89// Note that the actual name of the inner macro (e.g. `bitflags`) can't be a variable.
90// It'll just have to be matched on manually
91macro_rules! multiconst {
92    ($typename:ident, [$($(#[$attr:meta])* $rawname:ident = $value:expr;)*]) => {
93        $(
94            $(#[$attr])*
95            pub const $rawname: $typename = $value;
96        )*
97    }
98}
99
100multiconst!(zx_handle_t, [
101    ZX_HANDLE_INVALID = 0;
102]);
103
104multiconst!(zx_handle_op_t, [
105    ZX_HANDLE_OP_MOVE = 0;
106    ZX_HANDLE_OP_DUPLICATE = 1;
107]);
108
109multiconst!(zx_koid_t, [
110    ZX_KOID_INVALID = 0;
111    ZX_KOID_KERNEL = 1;
112    ZX_KOID_FIRST = 1024;
113]);
114
115multiconst!(zx_time_t, [
116    ZX_TIME_INFINITE = i64::MAX;
117    ZX_TIME_INFINITE_PAST = ::std::i64::MIN;
118]);
119
120multiconst!(zx_rights_t, [
121    ZX_RIGHT_NONE           = 0;
122    ZX_RIGHT_DUPLICATE      = 1 << 0;
123    ZX_RIGHT_TRANSFER       = 1 << 1;
124    ZX_RIGHT_READ           = 1 << 2;
125    ZX_RIGHT_WRITE          = 1 << 3;
126    ZX_RIGHT_EXECUTE        = 1 << 4;
127    ZX_RIGHT_MAP            = 1 << 5;
128    ZX_RIGHT_GET_PROPERTY   = 1 << 6;
129    ZX_RIGHT_SET_PROPERTY   = 1 << 7;
130    ZX_RIGHT_ENUMERATE      = 1 << 8;
131    ZX_RIGHT_DESTROY        = 1 << 9;
132    ZX_RIGHT_SET_POLICY     = 1 << 10;
133    ZX_RIGHT_GET_POLICY     = 1 << 11;
134    ZX_RIGHT_SIGNAL         = 1 << 12;
135    ZX_RIGHT_SIGNAL_PEER    = 1 << 13;
136    ZX_RIGHT_WAIT           = 1 << 14;
137    ZX_RIGHT_INSPECT        = 1 << 15;
138    ZX_RIGHT_MANAGE_JOB     = 1 << 16;
139    ZX_RIGHT_MANAGE_PROCESS = 1 << 17;
140    ZX_RIGHT_MANAGE_THREAD  = 1 << 18;
141    ZX_RIGHT_APPLY_PROFILE  = 1 << 19;
142    ZX_RIGHT_MANAGE_SOCKET  = 1 << 20;
143    ZX_RIGHT_OP_CHILDREN    = 1 << 21;
144    ZX_RIGHT_RESIZE         = 1 << 22;
145    ZX_RIGHT_ATTACH_VMO     = 1 << 23;
146    ZX_RIGHT_MANAGE_VMO     = 1 << 24;
147    ZX_RIGHT_SAME_RIGHTS    = 1 << 31;
148]);
149
150multiconst!(u32, [
151    ZX_VMO_RESIZABLE = 1 << 1;
152    ZX_VMO_DISCARDABLE = 1 << 2;
153    ZX_VMO_TRAP_DIRTY = 1 << 3;
154    ZX_VMO_UNBOUNDED = 1 << 4;
155]);
156
157multiconst!(u64, [
158    ZX_VMO_DIRTY_RANGE_IS_ZERO = 1;
159]);
160
161multiconst!(u32, [
162    ZX_INFO_VMO_TYPE_PAGED = 1 << 0;
163    ZX_INFO_VMO_RESIZABLE = 1 << 1;
164    ZX_INFO_VMO_IS_COW_CLONE = 1 << 2;
165    ZX_INFO_VMO_VIA_HANDLE = 1 << 3;
166    ZX_INFO_VMO_VIA_MAPPING = 1 << 4;
167    ZX_INFO_VMO_PAGER_BACKED = 1 << 5;
168    ZX_INFO_VMO_CONTIGUOUS = 1 << 6;
169    ZX_INFO_VMO_DISCARDABLE = 1 << 7;
170    ZX_INFO_VMO_IMMUTABLE = 1 << 8;
171    ZX_INFO_VMO_VIA_IOB_HANDLE = 1 << 9;
172]);
173
174multiconst!(u32, [
175    ZX_VMO_OP_COMMIT = 1;
176    ZX_VMO_OP_DECOMMIT = 2;
177    ZX_VMO_OP_LOCK = 3;
178    ZX_VMO_OP_UNLOCK = 4;
179    ZX_VMO_OP_CACHE_SYNC = 6;
180    ZX_VMO_OP_CACHE_INVALIDATE = 7;
181    ZX_VMO_OP_CACHE_CLEAN = 8;
182    ZX_VMO_OP_CACHE_CLEAN_INVALIDATE = 9;
183    ZX_VMO_OP_ZERO = 10;
184    ZX_VMO_OP_TRY_LOCK = 11;
185    ZX_VMO_OP_DONT_NEED = 12;
186    ZX_VMO_OP_ALWAYS_NEED = 13;
187    ZX_VMO_OP_PREFETCH = 14;
188]);
189
190multiconst!(u32, [
191    ZX_VMAR_OP_COMMIT = 1;
192    ZX_VMAR_OP_DECOMMIT = 2;
193    ZX_VMAR_OP_MAP_RANGE = 3;
194    ZX_VMAR_OP_ZERO = 10;
195    ZX_VMAR_OP_DONT_NEED = 12;
196    ZX_VMAR_OP_ALWAYS_NEED = 13;
197    ZX_VMAR_OP_PREFETCH = 14;
198]);
199
200multiconst!(zx_vm_option_t, [
201    ZX_VM_PERM_READ                    = 1 << 0;
202    ZX_VM_PERM_WRITE                   = 1 << 1;
203    ZX_VM_PERM_EXECUTE                 = 1 << 2;
204    ZX_VM_COMPACT                      = 1 << 3;
205    ZX_VM_SPECIFIC                     = 1 << 4;
206    ZX_VM_SPECIFIC_OVERWRITE           = 1 << 5;
207    ZX_VM_CAN_MAP_SPECIFIC             = 1 << 6;
208    ZX_VM_CAN_MAP_READ                 = 1 << 7;
209    ZX_VM_CAN_MAP_WRITE                = 1 << 8;
210    ZX_VM_CAN_MAP_EXECUTE              = 1 << 9;
211    ZX_VM_MAP_RANGE                    = 1 << 10;
212    ZX_VM_REQUIRE_NON_RESIZABLE        = 1 << 11;
213    ZX_VM_ALLOW_FAULTS                 = 1 << 12;
214    ZX_VM_OFFSET_IS_UPPER_LIMIT        = 1 << 13;
215    ZX_VM_PERM_READ_IF_XOM_UNSUPPORTED = 1 << 14;
216    ZX_VM_FAULT_BEYOND_STREAM_SIZE     = 1 << 15;
217
218    // VM alignment options
219    ZX_VM_ALIGN_BASE                   = 24;
220    ZX_VM_ALIGN_1KB                    = 10 << ZX_VM_ALIGN_BASE;
221    ZX_VM_ALIGN_2KB                    = 11 << ZX_VM_ALIGN_BASE;
222    ZX_VM_ALIGN_4KB                    = 12 << ZX_VM_ALIGN_BASE;
223    ZX_VM_ALIGN_8KB                    = 13 << ZX_VM_ALIGN_BASE;
224    ZX_VM_ALIGN_16KB                   = 14 << ZX_VM_ALIGN_BASE;
225    ZX_VM_ALIGN_32KB                   = 15 << ZX_VM_ALIGN_BASE;
226    ZX_VM_ALIGN_64KB                   = 16 << ZX_VM_ALIGN_BASE;
227    ZX_VM_ALIGN_128KB                  = 17 << ZX_VM_ALIGN_BASE;
228    ZX_VM_ALIGN_256KB                  = 18 << ZX_VM_ALIGN_BASE;
229    ZX_VM_ALIGN_512KB                  = 19 << ZX_VM_ALIGN_BASE;
230    ZX_VM_ALIGN_1MB                    = 20 << ZX_VM_ALIGN_BASE;
231    ZX_VM_ALIGN_2MB                    = 21 << ZX_VM_ALIGN_BASE;
232    ZX_VM_ALIGN_4MB                    = 22 << ZX_VM_ALIGN_BASE;
233    ZX_VM_ALIGN_8MB                    = 23 << ZX_VM_ALIGN_BASE;
234    ZX_VM_ALIGN_16MB                   = 24 << ZX_VM_ALIGN_BASE;
235    ZX_VM_ALIGN_32MB                   = 25 << ZX_VM_ALIGN_BASE;
236    ZX_VM_ALIGN_64MB                   = 26 << ZX_VM_ALIGN_BASE;
237    ZX_VM_ALIGN_128MB                  = 27 << ZX_VM_ALIGN_BASE;
238    ZX_VM_ALIGN_256MB                  = 28 << ZX_VM_ALIGN_BASE;
239    ZX_VM_ALIGN_512MB                  = 29 << ZX_VM_ALIGN_BASE;
240    ZX_VM_ALIGN_1GB                    = 30 << ZX_VM_ALIGN_BASE;
241    ZX_VM_ALIGN_2GB                    = 31 << ZX_VM_ALIGN_BASE;
242    ZX_VM_ALIGN_4GB                    = 32 << ZX_VM_ALIGN_BASE;
243]);
244
245multiconst!(u32, [
246    ZX_PROCESS_SHARED = 1 << 0;
247]);
248
249// LINT.IfChange(zx_status_t)
250// matches ///zircon/system/public/zircon/errors.h
251multiconst!(zx_status_t, [
252    /// Indicates an operation was successful.
253    ZX_OK                         = 0;
254    /// The system encountered an otherwise unspecified error while performing the
255    /// operation.
256    ZX_ERR_INTERNAL               = -1;
257    /// The operation is not implemented, supported, or enabled.
258    ZX_ERR_NOT_SUPPORTED          = -2;
259    /// The system was not able to allocate some resource needed for the operation.
260    ZX_ERR_NO_RESOURCES           = -3;
261    /// The system was not able to allocate memory needed for the operation.
262    ZX_ERR_NO_MEMORY              = -4;
263    /// The system call was interrupted, but should be retried. This should not be
264    /// seen outside of the VDSO.
265    ZX_ERR_INTERRUPTED_RETRY      = -6;
266    /// An argument is invalid. For example, a null pointer when a null pointer is
267    /// not permitted.
268    ZX_ERR_INVALID_ARGS           = -10;
269    /// A specified handle value does not refer to a handle.
270    ZX_ERR_BAD_HANDLE             = -11;
271    /// The subject of the operation is the wrong type to perform the operation.
272    ///
273    /// For example: Attempting a message_read on a thread handle.
274    ZX_ERR_WRONG_TYPE             = -12;
275    /// The specified syscall number is invalid.
276    ZX_ERR_BAD_SYSCALL            = -13;
277    /// An argument is outside the valid range for this operation.
278    ZX_ERR_OUT_OF_RANGE           = -14;
279    /// The caller-provided buffer is too small for this operation.
280    ZX_ERR_BUFFER_TOO_SMALL       = -15;
281    /// The operation failed because the current state of the object does not allow
282    /// it, or a precondition of the operation is not satisfied.
283    ZX_ERR_BAD_STATE              = -20;
284    /// The time limit for the operation elapsed before the operation completed.
285    ZX_ERR_TIMED_OUT              = -21;
286    /// The operation cannot be performed currently but potentially could succeed if
287    /// the caller waits for a prerequisite to be satisfied, like waiting for a
288    /// handle to be readable or writable.
289    ///
290    /// Example: Attempting to read from a channel that has no messages waiting but
291    /// has an open remote will return `ZX_ERR_SHOULD_WAIT`. In contrast, attempting
292    /// to read from a channel that has no messages waiting and has a closed remote
293    /// end will return `ZX_ERR_PEER_CLOSED`.
294    ZX_ERR_SHOULD_WAIT            = -22;
295    /// The in-progress operation, for example, a wait, has been canceled.
296    ZX_ERR_CANCELED               = -23;
297    /// The operation failed because the remote end of the subject of the operation
298    /// was closed.
299    ZX_ERR_PEER_CLOSED            = -24;
300    /// The requested entity is not found.
301    ZX_ERR_NOT_FOUND              = -25;
302    /// An object with the specified identifier already exists.
303    ///
304    /// Example: Attempting to create a file when a file already exists with that
305    /// name.
306    ZX_ERR_ALREADY_EXISTS         = -26;
307    /// The operation failed because the named entity is already owned or controlled
308    /// by another entity. The operation could succeed later if the current owner
309    /// releases the entity.
310    ZX_ERR_ALREADY_BOUND          = -27;
311    /// The subject of the operation is currently unable to perform the operation.
312    ///
313    /// This is used when there's no direct way for the caller to observe when the
314    /// subject will be able to perform the operation and should thus retry.
315    ZX_ERR_UNAVAILABLE            = -28;
316    /// The caller did not have permission to perform the specified operation.
317    ZX_ERR_ACCESS_DENIED          = -30;
318    /// Otherwise-unspecified error occurred during I/O.
319    ZX_ERR_IO                     = -40;
320    /// The entity the I/O operation is being performed on rejected the operation.
321    ///
322    /// Example: an I2C device NAK'ing a transaction or a disk controller rejecting
323    /// an invalid command, or a stalled USB endpoint.
324    ZX_ERR_IO_REFUSED             = -41;
325    /// The data in the operation failed an integrity check and is possibly
326    /// corrupted.
327    ///
328    /// Example: CRC or Parity error.
329    ZX_ERR_IO_DATA_INTEGRITY      = -42;
330    /// The data in the operation is currently unavailable and may be permanently
331    /// lost.
332    ///
333    /// Example: A disk block is irrecoverably damaged.
334    ZX_ERR_IO_DATA_LOSS           = -43;
335    /// The device is no longer available (has been unplugged from the system,
336    /// powered down, or the driver has been unloaded).
337    ZX_ERR_IO_NOT_PRESENT         = -44;
338    /// More data was received from the device than expected.
339    ///
340    /// Example: a USB "babble" error due to a device sending more data than the
341    /// host queued to receive.
342    ZX_ERR_IO_OVERRUN             = -45;
343    /// An operation did not complete within the required timeframe.
344    ///
345    /// Example: A USB isochronous transfer that failed to complete due to an
346    /// overrun or underrun.
347    ZX_ERR_IO_MISSED_DEADLINE     = -46;
348    /// The data in the operation is invalid parameter or is out of range.
349    ///
350    /// Example: A USB transfer that failed to complete with TRB Error
351    ZX_ERR_IO_INVALID             = -47;
352    /// Path name is too long.
353    ZX_ERR_BAD_PATH               = -50;
354    /// The object is not a directory or does not support directory operations.
355    ///
356    /// Example: Attempted to open a file as a directory or attempted to do
357    /// directory operations on a file.
358    ZX_ERR_NOT_DIR                = -51;
359    /// Object is not a regular file.
360    ZX_ERR_NOT_FILE               = -52;
361    /// This operation would cause a file to exceed a filesystem-specific size
362    /// limit.
363    ZX_ERR_FILE_BIG               = -53;
364    /// The filesystem or device space is exhausted.
365    ZX_ERR_NO_SPACE               = -54;
366    /// The directory is not empty for an operation that requires it to be empty.
367    ///
368    /// For example, non-recursively deleting a directory with files still in it.
369    ZX_ERR_NOT_EMPTY              = -55;
370    /// An indicate to not call again.
371    ///
372    /// The flow control values `ZX_ERR_STOP`, `ZX_ERR_NEXT`, and `ZX_ERR_ASYNC` are
373    /// not errors and will never be returned by a system call or public API. They
374    /// allow callbacks to request their caller perform some other operation.
375    ///
376    /// For example, a callback might be called on every event until it returns
377    /// something other than `ZX_OK`. This status allows differentiation between
378    /// "stop due to an error" and "stop because work is done."
379    ZX_ERR_STOP                   = -60;
380    /// Advance to the next item.
381    ///
382    /// The flow control values `ZX_ERR_STOP`, `ZX_ERR_NEXT`, and `ZX_ERR_ASYNC` are
383    /// not errors and will never be returned by a system call or public API. They
384    /// allow callbacks to request their caller perform some other operation.
385    ///
386    /// For example, a callback could use this value to indicate it did not consume
387    /// an item passed to it, but by choice, not due to an error condition.
388    ZX_ERR_NEXT                   = -61;
389    /// Ownership of the item has moved to an asynchronous worker.
390    ///
391    /// The flow control values `ZX_ERR_STOP`, `ZX_ERR_NEXT`, and `ZX_ERR_ASYNC` are
392    /// not errors and will never be returned by a system call or public API. They
393    /// allow callbacks to request their caller perform some other operation.
394    ///
395    /// Unlike `ZX_ERR_STOP`, which implies that iteration on an object
396    /// should stop, and `ZX_ERR_NEXT`, which implies that iteration
397    /// should continue to the next item, `ZX_ERR_ASYNC` implies
398    /// that an asynchronous worker is responsible for continuing iteration.
399    ///
400    /// For example, a callback will be called on every event, but one event needs
401    /// to handle some work asynchronously before it can continue. `ZX_ERR_ASYNC`
402    /// implies the worker is responsible for resuming iteration once its work has
403    /// completed.
404    ZX_ERR_ASYNC                  = -62;
405    /// The specified protocol is not supported.
406    ZX_ERR_PROTOCOL_NOT_SUPPORTED = -70;
407    /// The host is unreachable.
408    ZX_ERR_ADDRESS_UNREACHABLE    = -71;
409    /// Address is being used by someone else.
410    ZX_ERR_ADDRESS_IN_USE         = -72;
411    /// The socket is not connected.
412    ZX_ERR_NOT_CONNECTED          = -73;
413    /// The remote peer rejected the connection.
414    ZX_ERR_CONNECTION_REFUSED     = -74;
415    /// The connection was reset.
416    ZX_ERR_CONNECTION_RESET       = -75;
417    /// The connection was aborted.
418    ZX_ERR_CONNECTION_ABORTED     = -76;
419]);
420// LINT.ThenChange(//zircon/vdso/errors.fidl)
421
422multiconst!(zx_signals_t, [
423    ZX_SIGNAL_NONE              = 0;
424    ZX_OBJECT_SIGNAL_ALL        = 0x00ffffff;
425    ZX_USER_SIGNAL_ALL          = 0xff000000;
426    ZX_OBJECT_SIGNAL_0          = 1 << 0;
427    ZX_OBJECT_SIGNAL_1          = 1 << 1;
428    ZX_OBJECT_SIGNAL_2          = 1 << 2;
429    ZX_OBJECT_SIGNAL_3          = 1 << 3;
430    ZX_OBJECT_SIGNAL_4          = 1 << 4;
431    ZX_OBJECT_SIGNAL_5          = 1 << 5;
432    ZX_OBJECT_SIGNAL_6          = 1 << 6;
433    ZX_OBJECT_SIGNAL_7          = 1 << 7;
434    ZX_OBJECT_SIGNAL_8          = 1 << 8;
435    ZX_OBJECT_SIGNAL_9          = 1 << 9;
436    ZX_OBJECT_SIGNAL_10         = 1 << 10;
437    ZX_OBJECT_SIGNAL_11         = 1 << 11;
438    ZX_OBJECT_SIGNAL_12         = 1 << 12;
439    ZX_OBJECT_SIGNAL_13         = 1 << 13;
440    ZX_OBJECT_SIGNAL_14         = 1 << 14;
441    ZX_OBJECT_SIGNAL_15         = 1 << 15;
442    ZX_OBJECT_SIGNAL_16         = 1 << 16;
443    ZX_OBJECT_SIGNAL_17         = 1 << 17;
444    ZX_OBJECT_SIGNAL_18         = 1 << 18;
445    ZX_OBJECT_SIGNAL_19         = 1 << 19;
446    ZX_OBJECT_SIGNAL_20         = 1 << 20;
447    ZX_OBJECT_SIGNAL_21         = 1 << 21;
448    ZX_OBJECT_SIGNAL_22         = 1 << 22;
449    ZX_OBJECT_HANDLE_CLOSED     = 1 << 23;
450    ZX_USER_SIGNAL_0            = 1 << 24;
451    ZX_USER_SIGNAL_1            = 1 << 25;
452    ZX_USER_SIGNAL_2            = 1 << 26;
453    ZX_USER_SIGNAL_3            = 1 << 27;
454    ZX_USER_SIGNAL_4            = 1 << 28;
455    ZX_USER_SIGNAL_5            = 1 << 29;
456    ZX_USER_SIGNAL_6            = 1 << 30;
457    ZX_USER_SIGNAL_7            = 1 << 31;
458
459    ZX_OBJECT_READABLE          = ZX_OBJECT_SIGNAL_0;
460    ZX_OBJECT_WRITABLE          = ZX_OBJECT_SIGNAL_1;
461    ZX_OBJECT_PEER_CLOSED       = ZX_OBJECT_SIGNAL_2;
462
463    // Cancelation (handle was closed while waiting with it)
464    ZX_SIGNAL_HANDLE_CLOSED     = ZX_OBJECT_HANDLE_CLOSED;
465
466    // Event
467    ZX_EVENT_SIGNALED           = ZX_OBJECT_SIGNAL_3;
468
469    // EventPair
470    ZX_EVENTPAIR_SIGNALED       = ZX_OBJECT_SIGNAL_3;
471    ZX_EVENTPAIR_PEER_CLOSED    = ZX_OBJECT_SIGNAL_2;
472
473    // Task signals (process, thread, job)
474    ZX_TASK_TERMINATED          = ZX_OBJECT_SIGNAL_3;
475
476    // Channel
477    ZX_CHANNEL_READABLE         = ZX_OBJECT_SIGNAL_0;
478    ZX_CHANNEL_WRITABLE         = ZX_OBJECT_SIGNAL_1;
479    ZX_CHANNEL_PEER_CLOSED      = ZX_OBJECT_SIGNAL_2;
480
481    // Clock
482    ZX_CLOCK_STARTED            = ZX_OBJECT_SIGNAL_4;
483    ZX_CLOCK_UPDATED            = ZX_OBJECT_SIGNAL_5;
484
485    // Socket
486    ZX_SOCKET_READABLE            = ZX_OBJECT_READABLE;
487    ZX_SOCKET_WRITABLE            = ZX_OBJECT_WRITABLE;
488    ZX_SOCKET_PEER_CLOSED         = ZX_OBJECT_PEER_CLOSED;
489    ZX_SOCKET_PEER_WRITE_DISABLED = ZX_OBJECT_SIGNAL_4;
490    ZX_SOCKET_WRITE_DISABLED      = ZX_OBJECT_SIGNAL_5;
491    ZX_SOCKET_READ_THRESHOLD      = ZX_OBJECT_SIGNAL_10;
492    ZX_SOCKET_WRITE_THRESHOLD     = ZX_OBJECT_SIGNAL_11;
493
494    // Resource
495    ZX_RESOURCE_DESTROYED       = ZX_OBJECT_SIGNAL_3;
496    ZX_RESOURCE_READABLE        = ZX_OBJECT_READABLE;
497    ZX_RESOURCE_WRITABLE        = ZX_OBJECT_WRITABLE;
498    ZX_RESOURCE_CHILD_ADDED     = ZX_OBJECT_SIGNAL_4;
499
500    // Fifo
501    ZX_FIFO_READABLE            = ZX_OBJECT_READABLE;
502    ZX_FIFO_WRITABLE            = ZX_OBJECT_WRITABLE;
503    ZX_FIFO_PEER_CLOSED         = ZX_OBJECT_PEER_CLOSED;
504
505    // Iob
506    ZX_IOB_PEER_CLOSED           = ZX_OBJECT_PEER_CLOSED;
507    ZX_IOB_SHARED_REGION_UPDATED = ZX_OBJECT_SIGNAL_3;
508
509    // Job
510    ZX_JOB_TERMINATED           = ZX_OBJECT_SIGNAL_3;
511    ZX_JOB_NO_JOBS              = ZX_OBJECT_SIGNAL_4;
512    ZX_JOB_NO_PROCESSES         = ZX_OBJECT_SIGNAL_5;
513
514    // Process
515    ZX_PROCESS_TERMINATED       = ZX_OBJECT_SIGNAL_3;
516
517    // Thread
518    ZX_THREAD_TERMINATED        = ZX_OBJECT_SIGNAL_3;
519    ZX_THREAD_RUNNING           = ZX_OBJECT_SIGNAL_4;
520    ZX_THREAD_SUSPENDED         = ZX_OBJECT_SIGNAL_5;
521
522    // Log
523    ZX_LOG_READABLE             = ZX_OBJECT_READABLE;
524    ZX_LOG_WRITABLE             = ZX_OBJECT_WRITABLE;
525
526    // Timer
527    ZX_TIMER_SIGNALED           = ZX_OBJECT_SIGNAL_3;
528
529    // Vmo
530    ZX_VMO_ZERO_CHILDREN        = ZX_OBJECT_SIGNAL_3;
531
532    // Counter
533    ZX_COUNTER_SIGNALED          = ZX_OBJECT_SIGNAL_3;
534    ZX_COUNTER_POSITIVE          = ZX_OBJECT_SIGNAL_4;
535    ZX_COUNTER_NON_POSITIVE      = ZX_OBJECT_SIGNAL_5;
536]);
537
538multiconst!(zx_obj_type_t, [
539    ZX_OBJ_TYPE_NONE                = 0;
540    ZX_OBJ_TYPE_PROCESS             = 1;
541    ZX_OBJ_TYPE_THREAD              = 2;
542    ZX_OBJ_TYPE_VMO                 = 3;
543    ZX_OBJ_TYPE_CHANNEL             = 4;
544    ZX_OBJ_TYPE_EVENT               = 5;
545    ZX_OBJ_TYPE_PORT                = 6;
546    ZX_OBJ_TYPE_INTERRUPT           = 9;
547    ZX_OBJ_TYPE_PCI_DEVICE          = 11;
548    ZX_OBJ_TYPE_DEBUGLOG            = 12;
549    ZX_OBJ_TYPE_SOCKET              = 14;
550    ZX_OBJ_TYPE_RESOURCE            = 15;
551    ZX_OBJ_TYPE_EVENTPAIR           = 16;
552    ZX_OBJ_TYPE_JOB                 = 17;
553    ZX_OBJ_TYPE_VMAR                = 18;
554    ZX_OBJ_TYPE_FIFO                = 19;
555    ZX_OBJ_TYPE_GUEST               = 20;
556    ZX_OBJ_TYPE_VCPU                = 21;
557    ZX_OBJ_TYPE_TIMER               = 22;
558    ZX_OBJ_TYPE_IOMMU               = 23;
559    ZX_OBJ_TYPE_BTI                 = 24;
560    ZX_OBJ_TYPE_PROFILE             = 25;
561    ZX_OBJ_TYPE_PMT                 = 26;
562    ZX_OBJ_TYPE_SUSPEND_TOKEN       = 27;
563    ZX_OBJ_TYPE_PAGER               = 28;
564    ZX_OBJ_TYPE_EXCEPTION           = 29;
565    ZX_OBJ_TYPE_CLOCK               = 30;
566    ZX_OBJ_TYPE_STREAM              = 31;
567    ZX_OBJ_TYPE_MSI                 = 32;
568    ZX_OBJ_TYPE_IOB                 = 33;
569    ZX_OBJ_TYPE_COUNTER             = 34;
570]);
571
572// System ABI commits to having no more than 64 object types.
573//
574// See zx_info_process_handle_stats_t for an example of a binary interface that
575// depends on having an upper bound for the number of object types.
576pub const ZX_OBJ_TYPE_UPPER_BOUND: usize = 64;
577
578// TODO: add an alias for this type in the C headers.
579multiconst!(u32, [
580    // Argument is a char[ZX_MAX_NAME_LEN].
581    ZX_PROP_NAME                      = 3;
582
583    // Argument is a uintptr_t.
584    #[cfg(target_arch = "x86_64")]
585    ZX_PROP_REGISTER_GS               = 2;
586    #[cfg(target_arch = "x86_64")]
587    ZX_PROP_REGISTER_FS               = 4;
588
589    // Argument is the value of ld.so's _dl_debug_addr, a uintptr_t.
590    ZX_PROP_PROCESS_DEBUG_ADDR        = 5;
591
592    // Argument is the base address of the vDSO mapping (or zero), a uintptr_t.
593    ZX_PROP_PROCESS_VDSO_BASE_ADDRESS = 6;
594
595    // Whether the dynamic loader should issue a debug trap when loading a shared
596    // library, either initially or when running (e.g. dlopen).
597    ZX_PROP_PROCESS_BREAK_ON_LOAD = 7;
598
599    // Argument is a size_t.
600    ZX_PROP_SOCKET_RX_THRESHOLD       = 12;
601    ZX_PROP_SOCKET_TX_THRESHOLD       = 13;
602
603    // Argument is a size_t, describing the number of packets a channel
604    // endpoint can have pending in its tx direction.
605    ZX_PROP_CHANNEL_TX_MSG_MAX        = 14;
606
607    // Terminate this job if the system is low on memory.
608    ZX_PROP_JOB_KILL_ON_OOM           = 15;
609
610    // Exception close behavior.
611    ZX_PROP_EXCEPTION_STATE           = 16;
612
613    // The size of the content in a VMO, in bytes.
614    ZX_PROP_VMO_CONTENT_SIZE          = 17;
615
616    // How an exception should be handled.
617    ZX_PROP_EXCEPTION_STRATEGY        = 18;
618
619    // Whether the stream is in append mode or not.
620    ZX_PROP_STREAM_MODE_APPEND        = 19;
621]);
622
623// Value for ZX_THREAD_STATE_SINGLE_STEP. The value can be 0 (not single-stepping), or 1
624// (single-stepping). Other values will give ZX_ERR_INVALID_ARGS.
625pub type zx_thread_state_single_step_t = u32;
626
627// Possible values for "kind" in zx_thread_read_state and zx_thread_write_state.
628multiconst!(zx_thread_state_topic_t, [
629    ZX_THREAD_STATE_GENERAL_REGS       = 0;
630    ZX_THREAD_STATE_FP_REGS            = 1;
631    ZX_THREAD_STATE_VECTOR_REGS        = 2;
632    // No 3 at the moment.
633    ZX_THREAD_STATE_DEBUG_REGS         = 4;
634    ZX_THREAD_STATE_SINGLE_STEP        = 5;
635]);
636
637// Possible values for "kind" in zx_vcpu_read_state and zx_vcpu_write_state.
638multiconst!(zx_vcpu_state_topic_t, [
639    ZX_VCPU_STATE   = 0;
640    ZX_VCPU_IO      = 1;
641]);
642
643// From //zircon/system/public/zircon/features.h
644multiconst!(u32, [
645    ZX_FEATURE_KIND_CPU                        = 0;
646    ZX_FEATURE_KIND_HW_BREAKPOINT_COUNT        = 1;
647    ZX_FEATURE_KIND_HW_WATCHPOINT_COUNT        = 2;
648    ZX_FEATURE_KIND_ADDRESS_TAGGING            = 3;
649    ZX_FEATURE_KIND_VM                         = 4;
650]);
651
652// From //zircon/system/public/zircon/features.h
653multiconst!(u32, [
654    ZX_HAS_CPU_FEATURES                   = 1 << 0;
655
656    ZX_VM_FEATURE_CAN_MAP_XOM             = 1 << 0;
657
658    ZX_ARM64_FEATURE_ISA_FP               = 1 << 1;
659    ZX_ARM64_FEATURE_ISA_ASIMD            = 1 << 2;
660    ZX_ARM64_FEATURE_ISA_AES              = 1 << 3;
661    ZX_ARM64_FEATURE_ISA_PMULL            = 1 << 4;
662    ZX_ARM64_FEATURE_ISA_SHA1             = 1 << 5;
663    ZX_ARM64_FEATURE_ISA_SHA256           = 1 << 6;
664    ZX_ARM64_FEATURE_ISA_CRC32            = 1 << 7;
665    ZX_ARM64_FEATURE_ISA_ATOMICS          = 1 << 8;
666    ZX_ARM64_FEATURE_ISA_RDM              = 1 << 9;
667    ZX_ARM64_FEATURE_ISA_SHA3             = 1 << 10;
668    ZX_ARM64_FEATURE_ISA_SM3              = 1 << 11;
669    ZX_ARM64_FEATURE_ISA_SM4              = 1 << 12;
670    ZX_ARM64_FEATURE_ISA_DP               = 1 << 13;
671    ZX_ARM64_FEATURE_ISA_DPB              = 1 << 14;
672    ZX_ARM64_FEATURE_ISA_FHM              = 1 << 15;
673    ZX_ARM64_FEATURE_ISA_TS               = 1 << 16;
674    ZX_ARM64_FEATURE_ISA_RNDR             = 1 << 17;
675    ZX_ARM64_FEATURE_ISA_SHA512           = 1 << 18;
676    ZX_ARM64_FEATURE_ISA_I8MM             = 1 << 19;
677    ZX_ARM64_FEATURE_ISA_SVE              = 1 << 20;
678    ZX_ARM64_FEATURE_ISA_ARM32            = 1 << 21;
679    ZX_ARM64_FEATURE_ISA_SHA2             = 1 << 6;
680    ZX_ARM64_FEATURE_ADDRESS_TAGGING_TBI  = 1 << 0;
681]);
682
683// From //zircon/system/public/zircon/syscalls/resource.h
684multiconst!(zx_rsrc_kind_t, [
685    ZX_RSRC_KIND_MMIO       = 0;
686    ZX_RSRC_KIND_IRQ        = 1;
687    ZX_RSRC_KIND_IOPORT     = 2;
688    ZX_RSRC_KIND_ROOT       = 3;
689    ZX_RSRC_KIND_SMC        = 4;
690    ZX_RSRC_KIND_SYSTEM     = 5;
691]);
692
693// From //zircon/system/public/zircon/syscalls/resource.h
694multiconst!(zx_rsrc_system_base_t, [
695    ZX_RSRC_SYSTEM_HYPERVISOR_BASE  = 0;
696    ZX_RSRC_SYSTEM_VMEX_BASE        = 1;
697    ZX_RSRC_SYSTEM_DEBUG_BASE       = 2;
698    ZX_RSRC_SYSTEM_INFO_BASE        = 3;
699    ZX_RSRC_SYSTEM_CPU_BASE         = 4;
700    ZX_RSRC_SYSTEM_POWER_BASE       = 5;
701    ZX_RSRC_SYSTEM_MEXEC_BASE       = 6;
702    ZX_RSRC_SYSTEM_ENERGY_INFO_BASE = 7;
703    ZX_RSRC_SYSTEM_IOMMU_BASE       = 8;
704    ZX_RSRC_SYSTEM_FRAMEBUFFER_BASE = 9;
705    ZX_RSRC_SYSTEM_PROFILE_BASE     = 10;
706    ZX_RSRC_SYSTEM_MSI_BASE         = 11;
707    ZX_RSRC_SYSTEM_DEBUGLOG_BASE    = 12;
708    ZX_RSRC_SYSTEM_STALL_BASE       = 13;
709    ZX_RSRC_SYSTEM_TRACING_BASE     = 14;
710    // A resource representing the ability to sample callstack information about other processes.
711    ZX_RSRC_SYSTEM_SAMPLING_BASE    = 15;
712]);
713
714// clock ids
715multiconst!(zx_clock_t, [
716    ZX_CLOCK_MONOTONIC = 0;
717    ZX_CLOCK_BOOT      = 1;
718]);
719
720// from //zircon/system/public/zircon/syscalls/clock.h
721multiconst!(u64, [
722    ZX_CLOCK_OPT_MONOTONIC = 1 << 0;
723    ZX_CLOCK_OPT_CONTINUOUS = 1 << 1;
724    ZX_CLOCK_OPT_AUTO_START = 1 << 2;
725    ZX_CLOCK_OPT_BOOT = 1 << 3;
726    ZX_CLOCK_OPT_MAPPABLE = 1 << 4;
727
728    // v1 clock update flags
729    ZX_CLOCK_UPDATE_OPTION_VALUE_VALID = 1 << 0;
730    ZX_CLOCK_UPDATE_OPTION_RATE_ADJUST_VALID = 1 << 1;
731    ZX_CLOCK_UPDATE_OPTION_ERROR_BOUND_VALID = 1 << 2;
732
733    // Additional v2 clock update flags
734    ZX_CLOCK_UPDATE_OPTION_REFERENCE_VALUE_VALID = 1 << 3;
735    ZX_CLOCK_UPDATE_OPTION_SYNTHETIC_VALUE_VALID = ZX_CLOCK_UPDATE_OPTION_VALUE_VALID;
736
737    ZX_CLOCK_ARGS_VERSION_1 = 1 << 58;
738    ZX_CLOCK_ARGS_VERSION_2 = 2 << 58;
739]);
740
741// from //zircon/system/public/zircon/syscalls/exception.h
742multiconst!(u32, [
743    ZX_EXCEPTION_CHANNEL_DEBUGGER = 1 << 0;
744    ZX_EXCEPTION_TARGET_JOB_DEBUGGER = 1 << 0;
745
746    // Returned when probing a thread for its blocked state.
747    ZX_EXCEPTION_CHANNEL_TYPE_NONE = 0;
748    ZX_EXCEPTION_CHANNEL_TYPE_DEBUGGER = 1;
749    ZX_EXCEPTION_CHANNEL_TYPE_THREAD = 2;
750    ZX_EXCEPTION_CHANNEL_TYPE_PROCESS = 3;
751    ZX_EXCEPTION_CHANNEL_TYPE_JOB = 4;
752    ZX_EXCEPTION_CHANNEL_TYPE_JOB_DEBUGGER = 5;
753]);
754
755/// A byte used only to control memory alignment. All padding bytes are considered equal
756/// regardless of their content.
757///
758/// Note that the kernel C/C++ struct definitions use explicit padding fields to ensure no implicit
759/// padding is added. This is important for security since implicit padding bytes are not always
760/// safely initialized. These explicit padding fields are mirrored in the Rust struct definitions
761/// to minimize the opportunities for mistakes and inconsistencies.
762#[repr(transparent)]
763#[derive(Copy, Clone, Eq, Default)]
764#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes))]
765pub struct PadByte(u8);
766
767impl PartialEq for PadByte {
768    fn eq(&self, _other: &Self) -> bool {
769        true
770    }
771}
772
773impl Hash for PadByte {
774    fn hash<H: Hasher>(&self, state: &mut H) {
775        state.write_u8(0);
776    }
777}
778
779impl Debug for PadByte {
780    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
781        f.write_str("-")
782    }
783}
784
785#[repr(C)]
786#[derive(Debug, Clone, Eq, PartialEq)]
787pub struct zx_clock_create_args_v1_t {
788    pub backstop_time: zx_time_t,
789}
790
791#[repr(C)]
792#[derive(Debug, Default, Clone, Eq, PartialEq)]
793pub struct zx_clock_rate_t {
794    pub synthetic_ticks: u32,
795    pub reference_ticks: u32,
796}
797
798#[repr(C)]
799#[derive(Debug, Default, Clone, Eq, PartialEq)]
800pub struct zx_clock_transformation_t {
801    pub reference_offset: i64,
802    pub synthetic_offset: i64,
803    pub rate: zx_clock_rate_t,
804}
805
806#[repr(C)]
807#[derive(Debug, Default, Clone, Eq, PartialEq)]
808pub struct zx_clock_details_v1_t {
809    pub options: u64,
810    pub backstop_time: zx_time_t,
811    pub reference_ticks_to_synthetic: zx_clock_transformation_t,
812    pub reference_to_synthetic: zx_clock_transformation_t,
813    pub error_bound: u64,
814    pub query_ticks: zx_ticks_t,
815    pub last_value_update_ticks: zx_ticks_t,
816    pub last_rate_adjust_update_ticks: zx_ticks_t,
817    pub last_error_bounds_update_ticks: zx_ticks_t,
818    pub generation_counter: u32,
819    padding1: [PadByte; 4],
820}
821
822#[repr(C)]
823#[derive(Debug, Clone, Eq, PartialEq)]
824pub struct zx_clock_update_args_v1_t {
825    pub rate_adjust: i32,
826    padding1: [PadByte; 4],
827    pub value: i64,
828    pub error_bound: u64,
829}
830
831#[repr(C)]
832#[derive(Debug, Default, Clone, Eq, PartialEq)]
833pub struct zx_clock_update_args_v2_t {
834    pub rate_adjust: i32,
835    padding1: [PadByte; 4],
836    pub synthetic_value: i64,
837    pub reference_value: i64,
838    pub error_bound: u64,
839}
840
841multiconst!(zx_stream_seek_origin_t, [
842    ZX_STREAM_SEEK_ORIGIN_START        = 0;
843    ZX_STREAM_SEEK_ORIGIN_CURRENT      = 1;
844    ZX_STREAM_SEEK_ORIGIN_END          = 2;
845]);
846
847// Stream constants
848pub const ZX_STREAM_MODE_READ: u32 = 1 << 0;
849pub const ZX_STREAM_MODE_WRITE: u32 = 1 << 1;
850pub const ZX_STREAM_MODE_APPEND: u32 = 1 << 2;
851
852pub const ZX_STREAM_APPEND: u32 = 1 << 0;
853
854pub const ZX_CPRNG_ADD_ENTROPY_MAX_LEN: usize = 256;
855
856// Socket flags and limits.
857pub const ZX_SOCKET_STREAM: u32 = 0;
858pub const ZX_SOCKET_DATAGRAM: u32 = 1 << 0;
859pub const ZX_SOCKET_DISPOSITION_WRITE_DISABLED: u32 = 1 << 0;
860pub const ZX_SOCKET_DISPOSITION_WRITE_ENABLED: u32 = 1 << 1;
861
862// VM Object clone flags
863pub const ZX_VMO_CHILD_SNAPSHOT: u32 = 1 << 0;
864pub const ZX_VMO_CHILD_SNAPSHOT_AT_LEAST_ON_WRITE: u32 = 1 << 4;
865pub const ZX_VMO_CHILD_RESIZABLE: u32 = 1 << 2;
866pub const ZX_VMO_CHILD_SLICE: u32 = 1 << 3;
867pub const ZX_VMO_CHILD_NO_WRITE: u32 = 1 << 5;
868pub const ZX_VMO_CHILD_REFERENCE: u32 = 1 << 6;
869pub const ZX_VMO_CHILD_SNAPSHOT_MODIFIED: u32 = 1 << 7;
870
871// channel write size constants
872pub const ZX_CHANNEL_MAX_MSG_HANDLES: u32 = 64;
873pub const ZX_CHANNEL_MAX_MSG_BYTES: u32 = 65536;
874pub const ZX_CHANNEL_MAX_MSG_IOVEC: u32 = 8192;
875
876// fifo write size constants
877pub const ZX_FIFO_MAX_SIZE_BYTES: u32 = 4096;
878
879// Min/max page size constants
880#[cfg(target_arch = "x86_64")]
881pub const ZX_MIN_PAGE_SHIFT: u32 = 12;
882#[cfg(target_arch = "x86_64")]
883pub const ZX_MAX_PAGE_SHIFT: u32 = 21;
884
885#[cfg(target_arch = "aarch64")]
886pub const ZX_MIN_PAGE_SHIFT: u32 = 12;
887#[cfg(target_arch = "aarch64")]
888pub const ZX_MAX_PAGE_SHIFT: u32 = 16;
889
890#[cfg(target_arch = "riscv64")]
891pub const ZX_MIN_PAGE_SHIFT: u32 = 12;
892#[cfg(target_arch = "riscv64")]
893pub const ZX_MAX_PAGE_SHIFT: u32 = 21;
894
895// Task response codes if a process is externally killed
896pub const ZX_TASK_RETCODE_SYSCALL_KILL: i64 = -1024;
897pub const ZX_TASK_RETCODE_OOM_KILL: i64 = -1025;
898pub const ZX_TASK_RETCODE_POLICY_KILL: i64 = -1026;
899pub const ZX_TASK_RETCODE_VDSO_KILL: i64 = -1027;
900pub const ZX_TASK_RETCODE_EXCEPTION_KILL: i64 = -1028;
901
902// Resource flags.
903pub const ZX_RSRC_FLAG_EXCLUSIVE: zx_rsrc_flags_t = 0x00010000;
904
905// Topics for CPU performance info syscalls
906pub const ZX_CPU_PERF_SCALE: u32 = 1;
907pub const ZX_CPU_DEFAULT_PERF_SCALE: u32 = 2;
908pub const ZX_CPU_POWER_LIMIT: u32 = 3;
909
910// Cache policy flags.
911pub const ZX_CACHE_POLICY_CACHED: u32 = 0;
912pub const ZX_CACHE_POLICY_UNCACHED: u32 = 1;
913pub const ZX_CACHE_POLICY_UNCACHED_DEVICE: u32 = 2;
914pub const ZX_CACHE_POLICY_WRITE_COMBINING: u32 = 3;
915
916// Flag bits for zx_cache_flush.
917multiconst!(u32, [
918    ZX_CACHE_FLUSH_INSN         = 1 << 0;
919    ZX_CACHE_FLUSH_DATA         = 1 << 1;
920    ZX_CACHE_FLUSH_INVALIDATE   = 1 << 2;
921]);
922
923#[repr(C)]
924#[derive(Debug, Copy, Clone, Eq, PartialEq)]
925pub struct zx_wait_item_t {
926    pub handle: zx_handle_t,
927    pub waitfor: zx_signals_t,
928    pub pending: zx_signals_t,
929}
930
931#[repr(C)]
932#[derive(Debug, Copy, Clone, Eq, PartialEq)]
933pub struct zx_waitset_result_t {
934    pub cookie: u64,
935    pub status: zx_status_t,
936    pub observed: zx_signals_t,
937}
938
939#[repr(C)]
940#[derive(Debug, Copy, Clone, Eq, PartialEq)]
941pub struct zx_handle_info_t {
942    pub handle: zx_handle_t,
943    pub ty: zx_obj_type_t,
944    pub rights: zx_rights_t,
945    pub unused: u32,
946}
947
948pub const ZX_CHANNEL_READ_MAY_DISCARD: u32 = 1;
949pub const ZX_CHANNEL_WRITE_USE_IOVEC: u32 = 2;
950
951#[repr(C)]
952#[derive(Debug, Copy, Clone, Eq, PartialEq)]
953pub struct zx_channel_call_args_t {
954    pub wr_bytes: *const u8,
955    pub wr_handles: *const zx_handle_t,
956    pub rd_bytes: *mut u8,
957    pub rd_handles: *mut zx_handle_t,
958    pub wr_num_bytes: u32,
959    pub wr_num_handles: u32,
960    pub rd_num_bytes: u32,
961    pub rd_num_handles: u32,
962}
963
964#[repr(C)]
965#[derive(Debug, Copy, Clone, Eq, PartialEq)]
966pub struct zx_channel_call_etc_args_t {
967    pub wr_bytes: *const u8,
968    pub wr_handles: *mut zx_handle_disposition_t,
969    pub rd_bytes: *mut u8,
970    pub rd_handles: *mut zx_handle_info_t,
971    pub wr_num_bytes: u32,
972    pub wr_num_handles: u32,
973    pub rd_num_bytes: u32,
974    pub rd_num_handles: u32,
975}
976
977#[repr(C)]
978#[derive(Debug, Copy, Clone, Eq, PartialEq)]
979pub struct zx_channel_iovec_t {
980    pub buffer: *const u8,
981    pub capacity: u32,
982    padding1: [PadByte; 4],
983}
984
985impl Default for zx_channel_iovec_t {
986    fn default() -> Self {
987        Self {
988            buffer: std::ptr::null(),
989            capacity: Default::default(),
990            padding1: Default::default(),
991        }
992    }
993}
994
995#[repr(C)]
996#[derive(Debug, Copy, Clone, Eq, PartialEq)]
997pub struct zx_handle_disposition_t {
998    pub operation: zx_handle_op_t,
999    pub handle: zx_handle_t,
1000    pub type_: zx_obj_type_t,
1001    pub rights: zx_rights_t,
1002    pub result: zx_status_t,
1003}
1004
1005#[repr(C)]
1006#[derive(Debug, Copy, Clone)]
1007pub struct zx_iovec_t {
1008    pub buffer: *const u8,
1009    pub capacity: usize,
1010}
1011
1012pub type zx_pci_irq_swizzle_lut_t = [[[u32; 4]; 8]; 32];
1013
1014#[repr(C)]
1015#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1016pub struct zx_pci_init_arg_t {
1017    pub dev_pin_to_global_irq: zx_pci_irq_swizzle_lut_t,
1018    pub num_irqs: u32,
1019    pub irqs: [zx_irq_t; 32],
1020    pub ecam_window_count: u32,
1021    // Note: the ecam_windows field is actually a variable size array.
1022    // We use a fixed size array to match the C repr.
1023    pub ecam_windows: [zx_ecam_window_t; 1],
1024}
1025
1026#[repr(C)]
1027#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1028pub struct zx_irq_t {
1029    pub global_irq: u32,
1030    pub level_triggered: bool,
1031    pub active_high: bool,
1032}
1033
1034#[repr(C)]
1035#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1036pub struct zx_ecam_window_t {
1037    pub base: u64,
1038    pub size: usize,
1039    pub bus_start: u8,
1040    pub bus_end: u8,
1041}
1042
1043#[repr(C)]
1044#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1045pub struct zx_pcie_device_info_t {
1046    pub vendor_id: u16,
1047    pub device_id: u16,
1048    pub base_class: u8,
1049    pub sub_class: u8,
1050    pub program_interface: u8,
1051    pub revision_id: u8,
1052    pub bus_id: u8,
1053    pub dev_id: u8,
1054    pub func_id: u8,
1055}
1056
1057#[repr(C)]
1058#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1059pub struct zx_pci_resource_t {
1060    pub type_: u32,
1061    pub size: usize,
1062    // TODO: Actually a union
1063    pub pio_addr: usize,
1064}
1065
1066// TODO: Actually a union
1067pub type zx_rrec_t = [u8; 64];
1068
1069// Ports V2
1070#[repr(u32)]
1071#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1072pub enum zx_packet_type_t {
1073    ZX_PKT_TYPE_USER = 0,
1074    ZX_PKT_TYPE_SIGNAL_ONE = 1,
1075    ZX_PKT_TYPE_GUEST_BELL = 3,
1076    ZX_PKT_TYPE_GUEST_MEM = 4,
1077    ZX_PKT_TYPE_GUEST_IO = 5,
1078    ZX_PKT_TYPE_GUEST_VCPU = 6,
1079    ZX_PKT_TYPE_INTERRUPT = 7,
1080    ZX_PKT_TYPE_PAGE_REQUEST = 9,
1081    ZX_PKT_TYPE_PROCESSOR_POWER_LEVEL_TRANSITION_REQUEST = 10,
1082    #[doc(hidden)]
1083    __Nonexhaustive,
1084}
1085
1086impl Default for zx_packet_type_t {
1087    fn default() -> Self {
1088        zx_packet_type_t::ZX_PKT_TYPE_USER
1089    }
1090}
1091
1092#[repr(u32)]
1093#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
1094pub enum zx_packet_guest_vcpu_type_t {
1095    #[default]
1096    ZX_PKT_GUEST_VCPU_INTERRUPT = 0,
1097    ZX_PKT_GUEST_VCPU_STARTUP = 1,
1098    #[doc(hidden)]
1099    __Nonexhaustive,
1100}
1101
1102#[repr(C)]
1103#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1104pub struct zx_packet_signal_t {
1105    pub trigger: zx_signals_t,
1106    pub observed: zx_signals_t,
1107    pub count: u64,
1108    pub timestamp: zx_time_t,
1109}
1110
1111pub const ZX_WAIT_ASYNC_TIMESTAMP: u32 = 1;
1112pub const ZX_WAIT_ASYNC_EDGE: u32 = 2;
1113pub const ZX_WAIT_ASYNC_BOOT_TIMESTAMP: u32 = 4;
1114
1115// Actually a union of different integer types, but this should be good enough.
1116pub type zx_packet_user_t = [u8; 32];
1117
1118#[repr(C)]
1119#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1120pub struct zx_port_packet_t {
1121    pub key: u64,
1122    pub packet_type: zx_packet_type_t,
1123    pub status: i32,
1124    pub union: [u8; 32],
1125}
1126
1127#[repr(C)]
1128#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1129pub struct zx_packet_guest_bell_t {
1130    pub addr: zx_gpaddr_t,
1131}
1132
1133#[repr(C)]
1134#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1135pub struct zx_packet_guest_io_t {
1136    pub port: u16,
1137    pub access_size: u8,
1138    pub input: bool,
1139    pub data: [u8; 4],
1140}
1141
1142#[repr(C)]
1143#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1144#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
1145pub struct zx_packet_guest_vcpu_interrupt_t {
1146    pub mask: u64,
1147    pub vector: u8,
1148    padding1: [PadByte; 7],
1149}
1150
1151#[repr(C)]
1152#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1153#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
1154pub struct zx_packet_guest_vcpu_startup_t {
1155    pub id: u64,
1156    pub entry: zx_gpaddr_t,
1157}
1158
1159#[repr(C)]
1160#[derive(Copy, Clone)]
1161#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
1162pub union zx_packet_guest_vcpu_union_t {
1163    pub interrupt: zx_packet_guest_vcpu_interrupt_t,
1164    pub startup: zx_packet_guest_vcpu_startup_t,
1165}
1166
1167#[cfg(feature = "zerocopy")]
1168impl Default for zx_packet_guest_vcpu_union_t {
1169    fn default() -> Self {
1170        Self::new_zeroed()
1171    }
1172}
1173
1174#[repr(C)]
1175#[derive(Copy, Clone, Default)]
1176pub struct zx_packet_guest_vcpu_t {
1177    pub r#type: zx_packet_guest_vcpu_type_t,
1178    padding1: [PadByte; 4],
1179    pub union: zx_packet_guest_vcpu_union_t,
1180    padding2: [PadByte; 8],
1181}
1182
1183impl PartialEq for zx_packet_guest_vcpu_t {
1184    fn eq(&self, other: &Self) -> bool {
1185        if self.r#type != other.r#type {
1186            return false;
1187        }
1188        match self.r#type {
1189            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_INTERRUPT => unsafe {
1190                self.union.interrupt == other.union.interrupt
1191            },
1192            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_STARTUP => unsafe {
1193                self.union.startup == other.union.startup
1194            },
1195            // No equality relationship is defined for invalid types.
1196            _ => false,
1197        }
1198    }
1199}
1200
1201impl Eq for zx_packet_guest_vcpu_t {}
1202
1203impl Debug for zx_packet_guest_vcpu_t {
1204    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1205        match self.r#type {
1206            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_INTERRUPT => {
1207                write!(f, "type: {:?} union: {:?}", self.r#type, unsafe { self.union.interrupt })
1208            }
1209            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_STARTUP => {
1210                write!(f, "type: {:?} union: {:?}", self.r#type, unsafe { self.union.startup })
1211            }
1212            _ => panic!("unexpected VCPU packet type"),
1213        }
1214    }
1215}
1216
1217#[repr(C)]
1218#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
1219pub struct zx_packet_page_request_t {
1220    pub command: zx_page_request_command_t,
1221    pub flags: u16,
1222    padding1: [PadByte; 4],
1223    pub offset: u64,
1224    pub length: u64,
1225    padding2: [PadByte; 8],
1226}
1227
1228#[repr(u16)]
1229#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
1230pub enum zx_page_request_command_t {
1231    #[default]
1232    ZX_PAGER_VMO_READ = 0x0000,
1233    ZX_PAGER_VMO_COMPLETE = 0x0001,
1234    ZX_PAGER_VMO_DIRTY = 0x0002,
1235    #[doc(hidden)]
1236    __Nonexhaustive,
1237}
1238
1239multiconst!(u32, [
1240    ZX_PAGER_OP_FAIL = 1;
1241    ZX_PAGER_OP_DIRTY = 2;
1242    ZX_PAGER_OP_WRITEBACK_BEGIN = 3;
1243    ZX_PAGER_OP_WRITEBACK_END = 4;
1244]);
1245
1246pub type zx_excp_type_t = u32;
1247
1248multiconst!(zx_excp_type_t, [
1249    ZX_EXCP_GENERAL               = 0x008;
1250    ZX_EXCP_FATAL_PAGE_FAULT      = 0x108;
1251    ZX_EXCP_UNDEFINED_INSTRUCTION = 0x208;
1252    ZX_EXCP_SW_BREAKPOINT         = 0x308;
1253    ZX_EXCP_HW_BREAKPOINT         = 0x408;
1254    ZX_EXCP_UNALIGNED_ACCESS      = 0x508;
1255
1256    ZX_EXCP_SYNTH                 = 0x8000;
1257
1258    ZX_EXCP_THREAD_STARTING       = 0x008 | ZX_EXCP_SYNTH;
1259    ZX_EXCP_THREAD_EXITING        = 0x108 | ZX_EXCP_SYNTH;
1260    ZX_EXCP_POLICY_ERROR          = 0x208 | ZX_EXCP_SYNTH;
1261    ZX_EXCP_PROCESS_STARTING      = 0x308 | ZX_EXCP_SYNTH;
1262    ZX_EXCP_USER                  = 0x309 | ZX_EXCP_SYNTH;
1263]);
1264
1265multiconst!(u32, [
1266    ZX_EXCP_USER_CODE_PROCESS_NAME_CHANGED = 0x0001;
1267
1268    ZX_EXCP_USER_CODE_USER0                = 0xF000;
1269    ZX_EXCP_USER_CODE_USER1                = 0xF001;
1270    ZX_EXCP_USER_CODE_USER2                = 0xF002;
1271]);
1272
1273#[repr(C)]
1274#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1275#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes))]
1276pub struct zx_exception_info_t {
1277    pub pid: zx_koid_t,
1278    pub tid: zx_koid_t,
1279    pub type_: zx_excp_type_t,
1280    padding1: [PadByte; 4],
1281}
1282
1283#[repr(C)]
1284#[derive(Default, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
1285pub struct zx_x86_64_exc_data_t {
1286    pub vector: u64,
1287    pub err_code: u64,
1288    pub cr2: u64,
1289}
1290
1291impl Debug for zx_x86_64_exc_data_t {
1292    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1293        write!(f, "vector 0x{:x} err_code {} cr2 0x{:x}", self.vector, self.err_code, self.cr2)
1294    }
1295}
1296
1297#[repr(C)]
1298#[derive(Default, Copy, Clone, Eq, PartialEq, FromBytes, Immutable)]
1299pub struct zx_arm64_exc_data_t {
1300    pub esr: u32,
1301    padding1: [PadByte; 4],
1302    pub far: u64,
1303    padding2: [PadByte; 8],
1304}
1305
1306impl Debug for zx_arm64_exc_data_t {
1307    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1308        write!(f, "esr 0x{:x} far 0x{:x}", self.esr, self.far)
1309    }
1310}
1311
1312#[repr(C)]
1313#[derive(Default, Copy, Clone, Eq, PartialEq, FromBytes, Immutable)]
1314pub struct zx_riscv64_exc_data_t {
1315    pub cause: u64,
1316    pub tval: u64,
1317    padding1: [PadByte; 8],
1318}
1319
1320impl Debug for zx_riscv64_exc_data_t {
1321    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1322        write!(f, "cause {} tval {}", self.cause, self.tval)
1323    }
1324}
1325
1326#[repr(C)]
1327#[derive(Copy, Clone, KnownLayout, FromBytes, Immutable)]
1328pub union zx_exception_header_arch_t {
1329    pub x86_64: zx_x86_64_exc_data_t,
1330    pub arm_64: zx_arm64_exc_data_t,
1331    pub riscv_64: zx_riscv64_exc_data_t,
1332}
1333
1334impl Debug for zx_exception_header_arch_t {
1335    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1336        write!(f, "zx_exception_header_arch_t ")?;
1337        #[cfg(target_arch = "x86_64")]
1338        {
1339            // SAFETY: Exception reports are presumed to be from the target architecture.
1340            // Even if it was not, it's sound to treat the union as another variant as
1341            // the size and alignment are the same, there is no internal padding, and
1342            // the variants have no validity invariants.
1343            let x86_64 = unsafe { self.x86_64 };
1344            write!(f, "{x86_64:?}")
1345        }
1346        #[cfg(target_arch = "aarch64")]
1347        {
1348            // SAFETY: Exception reports are presumed to be from the target architecture.
1349            // Even if it was not, it's sound to treat the union as another variant as
1350            // the size and alignment are the same, there is no internal padding, and
1351            // the variants have no validity invariants.
1352            let arm_64 = unsafe { self.arm_64 };
1353            write!(f, "{arm_64:?}")
1354        }
1355        #[cfg(target_arch = "riscv64")]
1356        {
1357            // SAFETY: Exception reports are presumed to be from the target architecture.
1358            // Even if it was not, it's sound to treat the union as another variant as
1359            // the size and alignment are the same, there is no internal padding, and
1360            // the variants have no validity invariants.
1361            let riscv_64 = unsafe { self.riscv_64 };
1362            write!(f, "{riscv_64:?}")
1363        }
1364    }
1365}
1366
1367#[repr(C)]
1368#[derive(Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
1369pub struct zx_exception_header_t {
1370    pub size: u32,
1371    pub type_: zx_excp_type_t,
1372}
1373
1374pub type zx_excp_policy_code_t = u32;
1375
1376multiconst!(zx_excp_policy_code_t, [
1377    ZX_EXCP_POLICY_CODE_BAD_HANDLE              = 0;
1378    ZX_EXCP_POLICY_CODE_WRONG_OBJECT            = 1;
1379    ZX_EXCP_POLICY_CODE_VMAR_WX                 = 2;
1380    ZX_EXCP_POLICY_CODE_NEW_ANY                 = 3;
1381    ZX_EXCP_POLICY_CODE_NEW_VMO                 = 4;
1382    ZX_EXCP_POLICY_CODE_NEW_CHANNEL             = 5;
1383    ZX_EXCP_POLICY_CODE_NEW_EVENT               = 6;
1384    ZX_EXCP_POLICY_CODE_NEW_EVENTPAIR           = 7;
1385    ZX_EXCP_POLICY_CODE_NEW_PORT                = 8;
1386    ZX_EXCP_POLICY_CODE_NEW_SOCKET              = 9;
1387    ZX_EXCP_POLICY_CODE_NEW_FIFO                = 10;
1388    ZX_EXCP_POLICY_CODE_NEW_TIMER               = 11;
1389    ZX_EXCP_POLICY_CODE_NEW_PROCESS             = 12;
1390    ZX_EXCP_POLICY_CODE_NEW_PROFILE             = 13;
1391    ZX_EXCP_POLICY_CODE_NEW_PAGER               = 14;
1392    ZX_EXCP_POLICY_CODE_AMBIENT_MARK_VMO_EXEC   = 15;
1393    ZX_EXCP_POLICY_CODE_CHANNEL_FULL_WRITE      = 16;
1394    ZX_EXCP_POLICY_CODE_PORT_TOO_MANY_PACKETS   = 17;
1395    ZX_EXCP_POLICY_CODE_BAD_SYSCALL             = 18;
1396    ZX_EXCP_POLICY_CODE_PORT_TOO_MANY_OBSERVERS = 19;
1397    ZX_EXCP_POLICY_CODE_HANDLE_LEAK             = 20;
1398    ZX_EXCP_POLICY_CODE_NEW_IOB                 = 21;
1399]);
1400
1401#[repr(C)]
1402#[derive(Debug, Copy, Clone, KnownLayout, FromBytes, Immutable)]
1403pub struct zx_exception_context_t {
1404    pub arch: zx_exception_header_arch_t,
1405    pub synth_code: zx_excp_policy_code_t,
1406    pub synth_data: u32,
1407}
1408
1409#[repr(C)]
1410#[derive(Debug, Copy, Clone, KnownLayout, FromBytes, Immutable)]
1411pub struct zx_exception_report_t {
1412    pub header: zx_exception_header_t,
1413    pub context: zx_exception_context_t,
1414}
1415
1416pub type zx_exception_state_t = u32;
1417
1418multiconst!(zx_exception_state_t, [
1419    ZX_EXCEPTION_STATE_TRY_NEXT    = 0;
1420    ZX_EXCEPTION_STATE_HANDLED     = 1;
1421    ZX_EXCEPTION_STATE_THREAD_EXIT = 2;
1422]);
1423
1424pub type zx_exception_strategy_t = u32;
1425
1426multiconst!(zx_exception_state_t, [
1427    ZX_EXCEPTION_STRATEGY_FIRST_CHANCE   = 0;
1428    ZX_EXCEPTION_STRATEGY_SECOND_CHANCE  = 1;
1429]);
1430
1431#[cfg(target_arch = "x86_64")]
1432#[repr(C)]
1433#[derive(Default, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
1434pub struct zx_thread_state_general_regs_t {
1435    pub rax: u64,
1436    pub rbx: u64,
1437    pub rcx: u64,
1438    pub rdx: u64,
1439    pub rsi: u64,
1440    pub rdi: u64,
1441    pub rbp: u64,
1442    pub rsp: u64,
1443    pub r8: u64,
1444    pub r9: u64,
1445    pub r10: u64,
1446    pub r11: u64,
1447    pub r12: u64,
1448    pub r13: u64,
1449    pub r14: u64,
1450    pub r15: u64,
1451    pub rip: u64,
1452    pub rflags: u64,
1453    pub fs_base: u64,
1454    pub gs_base: u64,
1455}
1456
1457#[cfg(target_arch = "x86_64")]
1458impl std::fmt::Debug for zx_thread_state_general_regs_t {
1459    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1460        f.debug_struct(std::any::type_name::<Self>())
1461            .field("rax", &format_args!("{:#x}", self.rax))
1462            .field("rbx", &format_args!("{:#x}", self.rbx))
1463            .field("rcx", &format_args!("{:#x}", self.rcx))
1464            .field("rdx", &format_args!("{:#x}", self.rdx))
1465            .field("rsi", &format_args!("{:#x}", self.rsi))
1466            .field("rdi", &format_args!("{:#x}", self.rdi))
1467            .field("rbp", &format_args!("{:#x}", self.rbp))
1468            .field("rsp", &format_args!("{:#x}", self.rsp))
1469            .field("r8", &format_args!("{:#x}", self.r8))
1470            .field("r9", &format_args!("{:#x}", self.r9))
1471            .field("r10", &format_args!("{:#x}", self.r10))
1472            .field("r11", &format_args!("{:#x}", self.r11))
1473            .field("r12", &format_args!("{:#x}", self.r12))
1474            .field("r13", &format_args!("{:#x}", self.r13))
1475            .field("r14", &format_args!("{:#x}", self.r14))
1476            .field("r15", &format_args!("{:#x}", self.r15))
1477            .field("rip", &format_args!("{:#x}", self.rip))
1478            .field("rflags", &format_args!("{:#x}", self.rflags))
1479            .field("fs_base", &format_args!("{:#x}", self.fs_base))
1480            .field("gs_base", &format_args!("{:#x}", self.gs_base))
1481            .finish()
1482    }
1483}
1484
1485#[cfg(target_arch = "x86_64")]
1486impl From<&zx_restricted_state_t> for zx_thread_state_general_regs_t {
1487    fn from(state: &zx_restricted_state_t) -> Self {
1488        Self {
1489            rdi: state.rdi,
1490            rsi: state.rsi,
1491            rbp: state.rbp,
1492            rbx: state.rbx,
1493            rdx: state.rdx,
1494            rcx: state.rcx,
1495            rax: state.rax,
1496            rsp: state.rsp,
1497            r8: state.r8,
1498            r9: state.r9,
1499            r10: state.r10,
1500            r11: state.r11,
1501            r12: state.r12,
1502            r13: state.r13,
1503            r14: state.r14,
1504            r15: state.r15,
1505            rip: state.ip,
1506            rflags: state.flags,
1507            fs_base: state.fs_base,
1508            gs_base: state.gs_base,
1509        }
1510    }
1511}
1512
1513#[cfg(target_arch = "aarch64")]
1514multiconst!(u64, [
1515    ZX_REG_CPSR_ARCH_32_MASK = 0x10;
1516    ZX_REG_CPSR_THUMB_MASK = 0x20;
1517]);
1518
1519#[cfg(target_arch = "aarch64")]
1520#[repr(C)]
1521#[derive(Default, Copy, Clone, Eq, PartialEq)]
1522pub struct zx_thread_state_general_regs_t {
1523    pub r: [u64; 30],
1524    pub lr: u64,
1525    pub sp: u64,
1526    pub pc: u64,
1527    pub cpsr: u64,
1528    pub tpidr: u64,
1529}
1530
1531#[cfg(target_arch = "aarch64")]
1532impl std::fmt::Debug for zx_thread_state_general_regs_t {
1533    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1534        struct RegisterAsHex(u64);
1535        impl std::fmt::Debug for RegisterAsHex {
1536            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1537                write!(f, "{:#x}", self.0)
1538            }
1539        }
1540
1541        f.debug_struct(std::any::type_name::<Self>())
1542            .field("r", &self.r.map(RegisterAsHex))
1543            .field("lr", &format_args!("{:#x}", self.lr))
1544            .field("sp", &format_args!("{:#x}", self.sp))
1545            .field("pc", &format_args!("{:#x}", self.pc))
1546            .field("cpsr", &format_args!("{:#x}", self.cpsr))
1547            .field("tpidr", &format_args!("{:#x}", self.tpidr))
1548            .finish()
1549    }
1550}
1551
1552#[cfg(target_arch = "aarch64")]
1553impl From<&zx_restricted_state_t> for zx_thread_state_general_regs_t {
1554    fn from(state: &zx_restricted_state_t) -> Self {
1555        if state.cpsr as u64 & ZX_REG_CPSR_ARCH_32_MASK == ZX_REG_CPSR_ARCH_32_MASK {
1556            // aarch32
1557            Self {
1558                r: [
1559                    state.r[0],
1560                    state.r[1],
1561                    state.r[2],
1562                    state.r[3],
1563                    state.r[4],
1564                    state.r[5],
1565                    state.r[6],
1566                    state.r[7],
1567                    state.r[8],
1568                    state.r[9],
1569                    state.r[10],
1570                    state.r[11],
1571                    state.r[12],
1572                    state.r[13],
1573                    state.r[14],
1574                    state.pc, // ELR overwrites this.
1575                    state.r[16],
1576                    state.r[17],
1577                    state.r[18],
1578                    state.r[19],
1579                    state.r[20],
1580                    state.r[21],
1581                    state.r[22],
1582                    state.r[23],
1583                    state.r[24],
1584                    state.r[25],
1585                    state.r[26],
1586                    state.r[27],
1587                    state.r[28],
1588                    state.r[29],
1589                ],
1590                lr: state.r[14], // R[14] for aarch32
1591                sp: state.r[13], // R[13] for aarch32
1592                // TODO(https://fxbug.dev/379669623) Should it be checked for thumb and make
1593                // sure it isn't over incrementing?
1594                pc: state.pc, // Zircon populated this from elr.
1595                cpsr: state.cpsr as u64,
1596                tpidr: state.tpidr_el0,
1597            }
1598        } else {
1599            Self {
1600                r: [
1601                    state.r[0],
1602                    state.r[1],
1603                    state.r[2],
1604                    state.r[3],
1605                    state.r[4],
1606                    state.r[5],
1607                    state.r[6],
1608                    state.r[7],
1609                    state.r[8],
1610                    state.r[9],
1611                    state.r[10],
1612                    state.r[11],
1613                    state.r[12],
1614                    state.r[13],
1615                    state.r[14],
1616                    state.r[15],
1617                    state.r[16],
1618                    state.r[17],
1619                    state.r[18],
1620                    state.r[19],
1621                    state.r[20],
1622                    state.r[21],
1623                    state.r[22],
1624                    state.r[23],
1625                    state.r[24],
1626                    state.r[25],
1627                    state.r[26],
1628                    state.r[27],
1629                    state.r[28],
1630                    state.r[29],
1631                ],
1632                lr: state.r[30],
1633                sp: state.sp,
1634                pc: state.pc,
1635                cpsr: state.cpsr as u64,
1636                tpidr: state.tpidr_el0,
1637            }
1638        }
1639    }
1640}
1641
1642#[cfg(target_arch = "riscv64")]
1643#[repr(C)]
1644#[derive(Default, Copy, Clone, Eq, PartialEq)]
1645pub struct zx_thread_state_general_regs_t {
1646    pub pc: u64,
1647    pub ra: u64,  // x1
1648    pub sp: u64,  // x2
1649    pub gp: u64,  // x3
1650    pub tp: u64,  // x4
1651    pub t0: u64,  // x5
1652    pub t1: u64,  // x6
1653    pub t2: u64,  // x7
1654    pub s0: u64,  // x8
1655    pub s1: u64,  // x9
1656    pub a0: u64,  // x10
1657    pub a1: u64,  // x11
1658    pub a2: u64,  // x12
1659    pub a3: u64,  // x13
1660    pub a4: u64,  // x14
1661    pub a5: u64,  // x15
1662    pub a6: u64,  // x16
1663    pub a7: u64,  // x17
1664    pub s2: u64,  // x18
1665    pub s3: u64,  // x19
1666    pub s4: u64,  // x20
1667    pub s5: u64,  // x21
1668    pub s6: u64,  // x22
1669    pub s7: u64,  // x23
1670    pub s8: u64,  // x24
1671    pub s9: u64,  // x25
1672    pub s10: u64, // x26
1673    pub s11: u64, // x27
1674    pub t3: u64,  // x28
1675    pub t4: u64,  // x29
1676    pub t5: u64,  // x30
1677    pub t6: u64,  // x31
1678}
1679
1680#[cfg(target_arch = "riscv64")]
1681impl std::fmt::Debug for zx_thread_state_general_regs_t {
1682    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1683        f.debug_struct(std::any::type_name::<Self>())
1684            .field("pc", &format_args!("{:#x}", self.pc))
1685            .field("ra", &format_args!("{:#x}", self.ra)) // x1
1686            .field("sp", &format_args!("{:#x}", self.sp)) // x2
1687            .field("gp", &format_args!("{:#x}", self.gp)) // x3
1688            .field("tp", &format_args!("{:#x}", self.tp)) // x4
1689            .field("t0", &format_args!("{:#x}", self.t0)) // x5
1690            .field("t1", &format_args!("{:#x}", self.t1)) // x6
1691            .field("t2", &format_args!("{:#x}", self.t2)) // x7
1692            .field("s0", &format_args!("{:#x}", self.s0)) // x8
1693            .field("s1", &format_args!("{:#x}", self.s1)) // x9
1694            .field("a0", &format_args!("{:#x}", self.a0)) // x10
1695            .field("a1", &format_args!("{:#x}", self.a1)) // x11
1696            .field("a2", &format_args!("{:#x}", self.a2)) // x12
1697            .field("a3", &format_args!("{:#x}", self.a3)) // x13
1698            .field("a4", &format_args!("{:#x}", self.a4)) // x14
1699            .field("a5", &format_args!("{:#x}", self.a5)) // x15
1700            .field("a6", &format_args!("{:#x}", self.a6)) // x16
1701            .field("a7", &format_args!("{:#x}", self.a7)) // x17
1702            .field("s2", &format_args!("{:#x}", self.s2)) // x18
1703            .field("s3", &format_args!("{:#x}", self.s3)) // x19
1704            .field("s4", &format_args!("{:#x}", self.s4)) // x20
1705            .field("s5", &format_args!("{:#x}", self.s5)) // x21
1706            .field("s6", &format_args!("{:#x}", self.s6)) // x22
1707            .field("s7", &format_args!("{:#x}", self.s7)) // x23
1708            .field("s8", &format_args!("{:#x}", self.s8)) // x24
1709            .field("s9", &format_args!("{:#x}", self.s9)) // x25
1710            .field("s10", &format_args!("{:#x}", self.s10)) // x26
1711            .field("s11", &format_args!("{:#x}", self.s11)) // x27
1712            .field("t3", &format_args!("{:#x}", self.t3)) // x28
1713            .field("t4", &format_args!("{:#x}", self.t4)) // x29
1714            .field("t5", &format_args!("{:#x}", self.t5)) // x30
1715            .field("t6", &format_args!("{:#x}", self.t6)) // x31
1716            .finish()
1717    }
1718}
1719
1720multiconst!(zx_restricted_reason_t, [
1721    ZX_RESTRICTED_REASON_SYSCALL = 0;
1722    ZX_RESTRICTED_REASON_EXCEPTION = 1;
1723    ZX_RESTRICTED_REASON_KICK = 2;
1724]);
1725
1726#[cfg(target_arch = "x86_64")]
1727#[repr(C)]
1728#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1729pub struct zx_restricted_state_t {
1730    pub rdi: u64,
1731    pub rsi: u64,
1732    pub rbp: u64,
1733    pub rbx: u64,
1734    pub rdx: u64,
1735    pub rcx: u64,
1736    pub rax: u64,
1737    pub rsp: u64,
1738    pub r8: u64,
1739    pub r9: u64,
1740    pub r10: u64,
1741    pub r11: u64,
1742    pub r12: u64,
1743    pub r13: u64,
1744    pub r14: u64,
1745    pub r15: u64,
1746    pub ip: u64,
1747    pub flags: u64,
1748    pub fs_base: u64,
1749    pub gs_base: u64,
1750}
1751
1752#[cfg(target_arch = "x86_64")]
1753impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
1754    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
1755        Self {
1756            rdi: registers.rdi,
1757            rsi: registers.rsi,
1758            rbp: registers.rbp,
1759            rbx: registers.rbx,
1760            rdx: registers.rdx,
1761            rcx: registers.rcx,
1762            rax: registers.rax,
1763            rsp: registers.rsp,
1764            r8: registers.r8,
1765            r9: registers.r9,
1766            r10: registers.r10,
1767            r11: registers.r11,
1768            r12: registers.r12,
1769            r13: registers.r13,
1770            r14: registers.r14,
1771            r15: registers.r15,
1772            ip: registers.rip,
1773            flags: registers.rflags,
1774            fs_base: registers.fs_base,
1775            gs_base: registers.gs_base,
1776        }
1777    }
1778}
1779
1780#[cfg(target_arch = "aarch64")]
1781#[repr(C)]
1782#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1783pub struct zx_restricted_state_t {
1784    pub r: [u64; 31], // Note: r[30] is `lr` which is separated out in the general regs.
1785    pub sp: u64,
1786    pub pc: u64,
1787    pub tpidr_el0: u64,
1788    // Contains only the user-controllable upper 4-bits (NZCV).
1789    pub cpsr: u32,
1790    padding1: [PadByte; 4],
1791}
1792
1793#[cfg(target_arch = "aarch64")]
1794impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
1795    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
1796        Self {
1797            r: [
1798                registers.r[0],
1799                registers.r[1],
1800                registers.r[2],
1801                registers.r[3],
1802                registers.r[4],
1803                registers.r[5],
1804                registers.r[6],
1805                registers.r[7],
1806                registers.r[8],
1807                registers.r[9],
1808                registers.r[10],
1809                registers.r[11],
1810                registers.r[12],
1811                registers.r[13],
1812                registers.r[14],
1813                registers.r[15],
1814                registers.r[16],
1815                registers.r[17],
1816                registers.r[18],
1817                registers.r[19],
1818                registers.r[20],
1819                registers.r[21],
1820                registers.r[22],
1821                registers.r[23],
1822                registers.r[24],
1823                registers.r[25],
1824                registers.r[26],
1825                registers.r[27],
1826                registers.r[28],
1827                registers.r[29],
1828                registers.lr, // for compat this works nicely with zircon.
1829            ],
1830            pc: registers.pc,
1831            tpidr_el0: registers.tpidr,
1832            sp: registers.sp,
1833            cpsr: registers.cpsr as u32,
1834            padding1: Default::default(),
1835        }
1836    }
1837}
1838
1839#[cfg(target_arch = "riscv64")]
1840pub type zx_restricted_state_t = zx_thread_state_general_regs_t;
1841
1842#[cfg(target_arch = "riscv64")]
1843impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
1844    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
1845        *registers
1846    }
1847}
1848
1849#[repr(C)]
1850#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1851#[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "riscv64"))]
1852pub struct zx_restricted_syscall_t {
1853    pub state: zx_restricted_state_t,
1854}
1855
1856#[repr(C)]
1857#[derive(Copy, Clone)]
1858#[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "riscv64"))]
1859pub struct zx_restricted_exception_t {
1860    pub state: zx_restricted_state_t,
1861    pub exception: zx_exception_report_t,
1862}
1863
1864#[cfg(target_arch = "x86_64")]
1865#[repr(C)]
1866#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1867pub struct zx_vcpu_state_t {
1868    pub rax: u64,
1869    pub rcx: u64,
1870    pub rdx: u64,
1871    pub rbx: u64,
1872    pub rsp: u64,
1873    pub rbp: u64,
1874    pub rsi: u64,
1875    pub rdi: u64,
1876    pub r8: u64,
1877    pub r9: u64,
1878    pub r10: u64,
1879    pub r11: u64,
1880    pub r12: u64,
1881    pub r13: u64,
1882    pub r14: u64,
1883    pub r15: u64,
1884    // Contains only the user-controllable lower 32-bits.
1885    pub rflags: u64,
1886}
1887
1888#[cfg(target_arch = "aarch64")]
1889#[repr(C)]
1890#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1891pub struct zx_vcpu_state_t {
1892    pub x: [u64; 31],
1893    pub sp: u64,
1894    // Contains only the user-controllable upper 4-bits (NZCV).
1895    pub cpsr: u32,
1896    padding1: [PadByte; 4],
1897}
1898
1899#[cfg(target_arch = "riscv64")]
1900#[repr(C)]
1901#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1902pub struct zx_vcpu_state_t {
1903    pub empty: u32,
1904}
1905
1906#[repr(C)]
1907#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1908pub struct zx_vcpu_io_t {
1909    pub access_size: u8,
1910    padding1: [PadByte; 3],
1911    pub data: [u8; 4],
1912}
1913
1914#[cfg(target_arch = "aarch64")]
1915#[repr(C)]
1916#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1917pub struct zx_packet_guest_mem_t {
1918    pub addr: zx_gpaddr_t,
1919    pub access_size: u8,
1920    pub sign_extend: bool,
1921    pub xt: u8,
1922    pub read: bool,
1923    pub data: u64,
1924}
1925
1926#[cfg(target_arch = "riscv64")]
1927#[repr(C)]
1928#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1929pub struct zx_packet_guest_mem_t {
1930    pub addr: zx_gpaddr_t,
1931    padding1: [PadByte; 24],
1932}
1933
1934pub const X86_MAX_INST_LEN: usize = 15;
1935
1936#[cfg(target_arch = "x86_64")]
1937#[repr(C)]
1938#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1939pub struct zx_packet_guest_mem_t {
1940    pub addr: zx_gpaddr_t,
1941    pub cr3: zx_gpaddr_t,
1942    pub rip: zx_vaddr_t,
1943    pub instruction_size: u8,
1944    pub default_operand_size: u8,
1945}
1946
1947#[repr(C)]
1948#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1949pub struct zx_packet_interrupt_t {
1950    pub timestamp: zx_time_t,
1951    padding1: [PadByte; 24],
1952}
1953
1954// Helper for constructing topics that have been versioned.
1955const fn info_topic(topic: u32, version: u32) -> u32 {
1956    (version << 28) | topic
1957}
1958
1959multiconst!(zx_object_info_topic_t, [
1960    ZX_INFO_NONE                       = 0;
1961    ZX_INFO_HANDLE_VALID               = 1;
1962    ZX_INFO_HANDLE_BASIC               = 2;  // zx_info_handle_basic_t[1]
1963    ZX_INFO_PROCESS                    = info_topic(3, 1);  // zx_info_process_t[1]
1964    ZX_INFO_PROCESS_THREADS            = 4;  // zx_koid_t[n]
1965    ZX_INFO_VMAR                       = 7;  // zx_info_vmar_t[1]
1966    ZX_INFO_JOB_CHILDREN               = 8;  // zx_koid_t[n]
1967    ZX_INFO_JOB_PROCESSES              = 9;  // zx_koid_t[n]
1968    ZX_INFO_THREAD                     = 10; // zx_info_thread_t[1]
1969    ZX_INFO_THREAD_EXCEPTION_REPORT    = info_topic(11, 1); // zx_exception_report_t[1]
1970    ZX_INFO_TASK_STATS                 = info_topic(12, 1); // zx_info_task_stats_t[1]
1971    ZX_INFO_PROCESS_MAPS               = info_topic(13, 2); // zx_info_maps_t[n]
1972    ZX_INFO_PROCESS_VMOS               = info_topic(14, 3); // zx_info_vmo_t[n]
1973    ZX_INFO_THREAD_STATS               = 15; // zx_info_thread_stats_t[1]
1974    ZX_INFO_CPU_STATS                  = 16; // zx_info_cpu_stats_t[n]
1975    ZX_INFO_KMEM_STATS                 = info_topic(17, 1); // zx_info_kmem_stats_t[1]
1976    ZX_INFO_RESOURCE                   = 18; // zx_info_resource_t[1]
1977    ZX_INFO_HANDLE_COUNT               = 19; // zx_info_handle_count_t[1]
1978    ZX_INFO_BTI                        = 20; // zx_info_bti_t[1]
1979    ZX_INFO_PROCESS_HANDLE_STATS       = 21; // zx_info_process_handle_stats_t[1]
1980    ZX_INFO_SOCKET                     = 22; // zx_info_socket_t[1]
1981    ZX_INFO_VMO                        = info_topic(23, 3); // zx_info_vmo_t[1]
1982    ZX_INFO_JOB                        = 24; // zx_info_job_t[1]
1983    ZX_INFO_TIMER                      = 25; // zx_info_timer_t[1]
1984    ZX_INFO_STREAM                     = 26; // zx_info_stream_t[1]
1985    ZX_INFO_HANDLE_TABLE               = 27; // zx_info_handle_extended_t[n]
1986    ZX_INFO_MSI                        = 28; // zx_info_msi_t[1]
1987    ZX_INFO_GUEST_STATS                = 29; // zx_info_guest_stats_t[1]
1988    ZX_INFO_TASK_RUNTIME               = info_topic(30, 1); // zx_info_task_runtime_t[1]
1989    ZX_INFO_KMEM_STATS_EXTENDED        = 31; // zx_info_kmem_stats_extended_t[1]
1990    ZX_INFO_VCPU                       = 32; // zx_info_vcpu_t[1]
1991    ZX_INFO_KMEM_STATS_COMPRESSION     = 33; // zx_info_kmem_stats_compression_t[1]
1992    ZX_INFO_IOB                        = 34; // zx_info_iob_t[1]
1993    ZX_INFO_IOB_REGIONS                = 35; // zx_iob_region_info_t[n]
1994    ZX_INFO_VMAR_MAPS                  = 36; // zx_info_maps_t[n]
1995    ZX_INFO_POWER_DOMAINS              = 37; // zx_info_power_domain_info_t[n]
1996    ZX_INFO_MEMORY_STALL               = 38; // zx_info_memory_stall_t[1]
1997    ZX_INFO_CLOCK_MAPPED_SIZE          = 40; // usize[1]
1998]);
1999
2000multiconst!(zx_system_memory_stall_type_t, [
2001    ZX_SYSTEM_MEMORY_STALL_SOME        = 0;
2002    ZX_SYSTEM_MEMORY_STALL_FULL        = 1;
2003]);
2004
2005// This macro takes struct-like syntax and creates another macro that can be used to create
2006// different instances of the struct with different names. This is used to keep struct definitions
2007// from drifting between this crate and the fuchsia-zircon crate where they are identical other
2008// than in name and location.
2009macro_rules! struct_decl_macro {
2010    ( $(#[$attrs:meta])* $vis:vis struct <$macro_name:ident> $($any:tt)* ) => {
2011        #[macro_export]
2012        macro_rules! $macro_name {
2013            ($name:ident) => {
2014                $(#[$attrs])* $vis struct $name $($any)*
2015            }
2016        }
2017    }
2018}
2019
2020// Don't need struct_decl_macro for this, the wrapper is different.
2021#[repr(C)]
2022#[derive(Default, Debug, Copy, Clone, Eq, KnownLayout, FromBytes, Immutable, PartialEq)]
2023pub struct zx_info_handle_basic_t {
2024    pub koid: zx_koid_t,
2025    pub rights: zx_rights_t,
2026    pub type_: zx_obj_type_t,
2027    pub related_koid: zx_koid_t,
2028    padding1: [PadByte; 4],
2029}
2030
2031struct_decl_macro! {
2032    #[repr(C)]
2033    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2034    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2035    pub struct <zx_info_handle_count_t> {
2036        pub handle_count: u32,
2037    }
2038}
2039
2040zx_info_handle_count_t!(zx_info_handle_count_t);
2041
2042// Don't need struct_decl_macro for this, the wrapper is different.
2043#[repr(C)]
2044#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
2045pub struct zx_info_socket_t {
2046    pub options: u32,
2047    pub rx_buf_max: usize,
2048    pub rx_buf_size: usize,
2049    pub rx_buf_available: usize,
2050    pub tx_buf_max: usize,
2051    pub tx_buf_size: usize,
2052}
2053
2054multiconst!(u32, [
2055    ZX_INFO_PROCESS_FLAG_STARTED = 1 << 0;
2056    ZX_INFO_PROCESS_FLAG_EXITED = 1 << 1;
2057    ZX_INFO_PROCESS_FLAG_DEBUGGER_ATTACHED = 1 << 2;
2058]);
2059
2060struct_decl_macro! {
2061    #[repr(C)]
2062    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2063    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2064    pub struct <zx_info_process_t> {
2065        pub return_code: i64,
2066        pub start_time: zx_time_t,
2067        pub flags: u32,
2068    }
2069}
2070
2071zx_info_process_t!(zx_info_process_t);
2072
2073struct_decl_macro! {
2074    #[repr(C)]
2075    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2076    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2077    pub struct <zx_info_job_t> {
2078        pub return_code: i64,
2079        pub exited: u8,
2080        pub kill_on_oom: u8,
2081        pub debugger_attached: u8,
2082    }
2083}
2084
2085zx_info_job_t!(zx_info_job_t);
2086
2087struct_decl_macro! {
2088    #[repr(C)]
2089    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2090    #[derive(zerocopy::FromBytes, zerocopy::IntoBytes, zerocopy::Immutable)]
2091    pub struct <zx_info_timer_t> {
2092        pub options: u32,
2093        pub clock_id: zx_clock_t,
2094        pub deadline: zx_time_t,
2095        pub slack: zx_duration_t,
2096    }
2097}
2098
2099zx_info_timer_t!(zx_info_timer_t);
2100
2101#[repr(C)]
2102#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2103pub struct zx_policy_basic {
2104    pub condition: u32,
2105    pub policy: u32,
2106}
2107
2108#[repr(C)]
2109#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2110pub struct zx_policy_timer_slack {
2111    pub min_slack: zx_duration_t,
2112    pub default_mode: u32,
2113}
2114
2115multiconst!(u32, [
2116    // policy options
2117    ZX_JOB_POL_RELATIVE = 0;
2118    ZX_JOB_POL_ABSOLUTE = 1;
2119
2120    // policy topic
2121    ZX_JOB_POL_BASIC = 0;
2122    ZX_JOB_POL_TIMER_SLACK = 1;
2123
2124    // policy conditions
2125    ZX_POL_BAD_HANDLE            = 0;
2126    ZX_POL_WRONG_OBJECT          = 1;
2127    ZX_POL_VMAR_WX               = 2;
2128    ZX_POL_NEW_ANY               = 3;
2129    ZX_POL_NEW_VMO               = 4;
2130    ZX_POL_NEW_CHANNEL           = 5;
2131    ZX_POL_NEW_EVENT             = 6;
2132    ZX_POL_NEW_EVENTPAIR         = 7;
2133    ZX_POL_NEW_PORT              = 8;
2134    ZX_POL_NEW_SOCKET            = 9;
2135    ZX_POL_NEW_FIFO              = 10;
2136    ZX_POL_NEW_TIMER             = 11;
2137    ZX_POL_NEW_PROCESS           = 12;
2138    ZX_POL_NEW_PROFILE           = 13;
2139    ZX_POL_NEW_PAGER             = 14;
2140    ZX_POL_AMBIENT_MARK_VMO_EXEC = 15;
2141
2142    // policy actions
2143    ZX_POL_ACTION_ALLOW           = 0;
2144    ZX_POL_ACTION_DENY            = 1;
2145    ZX_POL_ACTION_ALLOW_EXCEPTION = 2;
2146    ZX_POL_ACTION_DENY_EXCEPTION  = 3;
2147    ZX_POL_ACTION_KILL            = 4;
2148
2149    // timer slack default modes
2150    ZX_TIMER_SLACK_CENTER = 0;
2151    ZX_TIMER_SLACK_EARLY  = 1;
2152    ZX_TIMER_SLACK_LATE   = 2;
2153]);
2154
2155multiconst!(u32, [
2156    // critical options
2157    ZX_JOB_CRITICAL_PROCESS_RETCODE_NONZERO = 1 << 0;
2158]);
2159
2160// Don't use struct_decl_macro, wrapper is different.
2161#[repr(C)]
2162#[derive(
2163    Default, Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable, IntoBytes,
2164)]
2165pub struct zx_info_vmo_t {
2166    pub koid: zx_koid_t,
2167    pub name: [u8; ZX_MAX_NAME_LEN],
2168    pub size_bytes: u64,
2169    pub parent_koid: zx_koid_t,
2170    pub num_children: usize,
2171    pub num_mappings: usize,
2172    pub share_count: usize,
2173    pub flags: u32,
2174    padding1: [PadByte; 4],
2175    pub committed_bytes: u64,
2176    pub handle_rights: zx_rights_t,
2177    pub cache_policy: u32,
2178    pub metadata_bytes: u64,
2179    pub committed_change_events: u64,
2180    pub populated_bytes: u64,
2181    pub committed_private_bytes: u64,
2182    pub populated_private_bytes: u64,
2183    pub committed_scaled_bytes: u64,
2184    pub populated_scaled_bytes: u64,
2185    pub committed_fractional_scaled_bytes: u64,
2186    pub populated_fractional_scaled_bytes: u64,
2187}
2188
2189struct_decl_macro! {
2190    #[repr(C)]
2191    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2192    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2193    pub struct <zx_info_cpu_stats_t> {
2194        pub cpu_number: u32,
2195        pub flags: u32,
2196        pub idle_time: zx_duration_t,
2197        pub reschedules: u64,
2198        pub context_switches: u64,
2199        pub irq_preempts: u64,
2200        pub preempts: u64,
2201        pub yields: u64,
2202        pub ints: u64,
2203        pub timer_ints: u64,
2204        pub timers: u64,
2205        pub page_faults: u64,
2206        pub exceptions: u64,
2207        pub syscalls: u64,
2208        pub reschedule_ipis: u64,
2209        pub generic_ipis: u64,
2210    }
2211}
2212
2213zx_info_cpu_stats_t!(zx_info_cpu_stats_t);
2214
2215struct_decl_macro! {
2216    #[repr(C)]
2217    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2218    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2219    pub struct <zx_info_kmem_stats_t> {
2220        pub total_bytes: u64,
2221        pub free_bytes: u64,
2222        pub free_loaned_bytes: u64,
2223        pub wired_bytes: u64,
2224        pub total_heap_bytes: u64,
2225        pub free_heap_bytes: u64,
2226        pub vmo_bytes: u64,
2227        pub mmu_overhead_bytes: u64,
2228        pub ipc_bytes: u64,
2229        pub cache_bytes: u64,
2230        pub slab_bytes: u64,
2231        pub zram_bytes: u64,
2232        pub other_bytes: u64,
2233        pub vmo_reclaim_total_bytes: u64,
2234        pub vmo_reclaim_newest_bytes: u64,
2235        pub vmo_reclaim_oldest_bytes: u64,
2236        pub vmo_reclaim_disabled_bytes: u64,
2237        pub vmo_discardable_locked_bytes: u64,
2238        pub vmo_discardable_unlocked_bytes: u64,
2239    }
2240}
2241
2242zx_info_kmem_stats_t!(zx_info_kmem_stats_t);
2243
2244struct_decl_macro! {
2245    #[repr(C)]
2246    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2247    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2248    pub struct <zx_info_kmem_stats_extended_t> {
2249        pub total_bytes: u64,
2250        pub free_bytes: u64,
2251        pub wired_bytes: u64,
2252        pub total_heap_bytes: u64,
2253        pub free_heap_bytes: u64,
2254        pub vmo_bytes: u64,
2255        pub vmo_pager_total_bytes: u64,
2256        pub vmo_pager_newest_bytes: u64,
2257        pub vmo_pager_oldest_bytes: u64,
2258        pub vmo_discardable_locked_bytes: u64,
2259        pub vmo_discardable_unlocked_bytes: u64,
2260        pub mmu_overhead_bytes: u64,
2261        pub ipc_bytes: u64,
2262        pub other_bytes: u64,
2263        pub vmo_reclaim_disable_bytes: u64,
2264    }
2265}
2266
2267zx_info_kmem_stats_extended_t!(zx_info_kmem_stats_extended_t);
2268
2269struct_decl_macro! {
2270    #[repr(C)]
2271    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2272    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2273    pub struct <zx_info_kmem_stats_compression_t> {
2274        pub uncompressed_storage_bytes: u64,
2275        pub compressed_storage_bytes: u64,
2276        pub compressed_fragmentation_bytes: u64,
2277        pub compression_time: zx_duration_t,
2278        pub decompression_time: zx_duration_t,
2279        pub total_page_compression_attempts: u64,
2280        pub failed_page_compression_attempts: u64,
2281        pub total_page_decompressions: u64,
2282        pub compressed_page_evictions: u64,
2283        pub eager_page_compressions: u64,
2284        pub memory_pressure_page_compressions: u64,
2285        pub critical_memory_page_compressions: u64,
2286        pub pages_decompressed_unit_ns: u64,
2287        pub pages_decompressed_within_log_time: [u64; 8],
2288    }
2289}
2290
2291zx_info_kmem_stats_compression_t!(zx_info_kmem_stats_compression_t);
2292
2293struct_decl_macro! {
2294    #[repr(C)]
2295    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2296    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2297    pub struct <zx_info_resource_t> {
2298        pub kind: u32,
2299        pub flags: u32,
2300        pub base: u64,
2301        pub size: usize,
2302        pub name: [u8; ZX_MAX_NAME_LEN],
2303    }
2304}
2305
2306struct_decl_macro! {
2307    #[repr(C)]
2308    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2309    #[derive(zerocopy::FromBytes, zerocopy::IntoBytes, zerocopy::Immutable)]
2310    pub struct <zx_info_bti_t> {
2311        pub minimum_contiguity: u64,
2312        pub aspace_size: u64,
2313        pub pmo_count: u64,
2314        pub quarantine_count: u64,
2315    }
2316}
2317
2318zx_info_bti_t!(zx_info_bti_t);
2319
2320pub type zx_thread_state_t = u32;
2321
2322multiconst!(zx_thread_state_t, [
2323    ZX_THREAD_STATE_NEW = 0x0000;
2324    ZX_THREAD_STATE_RUNNING = 0x0001;
2325    ZX_THREAD_STATE_SUSPENDED = 0x0002;
2326    ZX_THREAD_STATE_BLOCKED = 0x0003;
2327    ZX_THREAD_STATE_DYING = 0x0004;
2328    ZX_THREAD_STATE_DEAD = 0x0005;
2329    ZX_THREAD_STATE_BLOCKED_EXCEPTION = 0x0103;
2330    ZX_THREAD_STATE_BLOCKED_SLEEPING = 0x0203;
2331    ZX_THREAD_STATE_BLOCKED_FUTEX = 0x0303;
2332    ZX_THREAD_STATE_BLOCKED_PORT = 0x0403;
2333    ZX_THREAD_STATE_BLOCKED_CHANNEL = 0x0503;
2334    ZX_THREAD_STATE_BLOCKED_WAIT_ONE = 0x0603;
2335    ZX_THREAD_STATE_BLOCKED_WAIT_MANY = 0x0703;
2336    ZX_THREAD_STATE_BLOCKED_INTERRUPT = 0x0803;
2337    ZX_THREAD_STATE_BLOCKED_PAGER = 0x0903;
2338]);
2339
2340#[repr(C)]
2341#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, zerocopy::FromBytes, zerocopy::Immutable)]
2342pub struct zx_info_thread_t {
2343    pub state: zx_thread_state_t,
2344    pub wait_exception_channel_type: u32,
2345    pub cpu_affinity_mask: zx_cpu_set_t,
2346}
2347
2348struct_decl_macro! {
2349    #[repr(C)]
2350    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2351    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2352    pub struct <zx_info_thread_stats_t> {
2353        pub total_runtime: zx_duration_t,
2354        pub last_scheduled_cpu: u32,
2355    }
2356}
2357
2358zx_info_thread_stats_t!(zx_info_thread_stats_t);
2359
2360zx_info_resource_t!(zx_info_resource_t);
2361
2362struct_decl_macro! {
2363    #[repr(C)]
2364    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2365    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2366    pub struct <zx_info_vmar_t> {
2367        pub base: usize,
2368        pub len: usize,
2369    }
2370}
2371
2372zx_info_vmar_t!(zx_info_vmar_t);
2373
2374struct_decl_macro! {
2375    #[repr(C)]
2376    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2377    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2378    pub struct <zx_info_task_stats_t> {
2379        pub mem_mapped_bytes: usize,
2380        pub mem_private_bytes: usize,
2381        pub mem_shared_bytes: usize,
2382        pub mem_scaled_shared_bytes: usize,
2383        pub mem_fractional_scaled_shared_bytes: u64,
2384    }
2385}
2386
2387zx_info_task_stats_t!(zx_info_task_stats_t);
2388
2389struct_decl_macro! {
2390    #[repr(C)]
2391    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2392    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2393    pub struct <zx_info_task_runtime_t> {
2394        pub cpu_time: zx_duration_t,
2395        pub queue_time: zx_duration_t,
2396        pub page_fault_time: zx_duration_t,
2397        pub lock_contention_time: zx_duration_t,
2398    }
2399}
2400
2401zx_info_task_runtime_t!(zx_info_task_runtime_t);
2402
2403multiconst!(zx_info_maps_type_t, [
2404    ZX_INFO_MAPS_TYPE_NONE    = 0;
2405    ZX_INFO_MAPS_TYPE_ASPACE  = 1;
2406    ZX_INFO_MAPS_TYPE_VMAR    = 2;
2407    ZX_INFO_MAPS_TYPE_MAPPING = 3;
2408]);
2409
2410struct_decl_macro! {
2411    #[repr(C)]
2412    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2413    #[derive(zerocopy::FromBytes, zerocopy::Immutable, IntoBytes)]
2414    pub struct <zx_info_maps_mapping_t> {
2415        pub mmu_flags: zx_vm_option_t,
2416        padding1: [PadByte; 4],
2417        pub vmo_koid: zx_koid_t,
2418        pub vmo_offset: u64,
2419        pub committed_bytes: usize,
2420        pub populated_bytes: usize,
2421        pub committed_private_bytes: usize,
2422        pub populated_private_bytes: usize,
2423        pub committed_scaled_bytes: usize,
2424        pub populated_scaled_bytes: usize,
2425        pub committed_fractional_scaled_bytes: u64,
2426        pub populated_fractional_scaled_bytes: u64,
2427    }
2428}
2429
2430zx_info_maps_mapping_t!(zx_info_maps_mapping_t);
2431
2432#[repr(C)]
2433#[derive(Copy, Clone, KnownLayout, FromBytes, Immutable)]
2434pub union InfoMapsTypeUnion {
2435    pub mapping: zx_info_maps_mapping_t,
2436}
2437
2438struct_decl_macro! {
2439    #[repr(C)]
2440    #[derive(Copy, Clone)]
2441    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2442    pub struct <zx_info_maps_t> {
2443        pub name: [u8; ZX_MAX_NAME_LEN],
2444        pub base: zx_vaddr_t,
2445        pub size: usize,
2446        pub depth: usize,
2447        pub r#type: zx_info_maps_type_t,
2448        pub u: InfoMapsTypeUnion,
2449    }
2450}
2451
2452zx_info_maps_t!(zx_info_maps_t);
2453
2454struct_decl_macro! {
2455    #[repr(C)]
2456    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
2457    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2458    pub struct <zx_info_process_handle_stats_t> {
2459        pub handle_count: [u32; ZX_OBJ_TYPE_UPPER_BOUND],
2460    }
2461}
2462
2463impl Default for zx_info_process_handle_stats_t {
2464    fn default() -> Self {
2465        Self { handle_count: [0; ZX_OBJ_TYPE_UPPER_BOUND] }
2466    }
2467}
2468
2469zx_info_process_handle_stats_t!(zx_info_process_handle_stats_t);
2470
2471struct_decl_macro! {
2472    #[repr(C)]
2473    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2474    #[derive(zerocopy::FromBytes, zerocopy::Immutable, zerocopy::IntoBytes)]
2475    pub struct <zx_info_memory_stall_t> {
2476        pub stall_time_some: zx_duration_mono_t,
2477        pub stall_time_full: zx_duration_mono_t,
2478    }
2479}
2480
2481zx_info_memory_stall_t!(zx_info_memory_stall_t);
2482
2483// from //zircon/system/public/zircon/syscalls/hypervisor.h
2484multiconst!(zx_guest_trap_t, [
2485    ZX_GUEST_TRAP_BELL = 0;
2486    ZX_GUEST_TRAP_MEM  = 1;
2487    ZX_GUEST_TRAP_IO   = 2;
2488]);
2489
2490pub const ZX_LOG_RECORD_MAX: usize = 256;
2491pub const ZX_LOG_RECORD_DATA_MAX: usize = 216;
2492
2493pub const DEBUGLOG_TRACE: u8 = 0x10;
2494pub const DEBUGLOG_DEBUG: u8 = 0x20;
2495pub const DEBUGLOG_INFO: u8 = 0x30;
2496pub const DEBUGLOG_WARNING: u8 = 0x40;
2497pub const DEBUGLOG_ERROR: u8 = 0x50;
2498pub const DEBUGLOG_FATAL: u8 = 0x60;
2499
2500struct_decl_macro! {
2501    #[repr(C)]
2502    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
2503    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2504    pub struct <zx_log_record_t> {
2505        pub sequence: u64,
2506        padding1: [PadByte; 4],
2507        pub datalen: u16,
2508        pub severity: u8,
2509        pub flags: u8,
2510        pub timestamp: zx_instant_boot_t,
2511        pub pid: u64,
2512        pub tid: u64,
2513        pub data: [u8; ZX_LOG_RECORD_DATA_MAX],
2514    }
2515}
2516const_assert_eq!(std::mem::size_of::<zx_log_record_t>(), ZX_LOG_RECORD_MAX);
2517
2518zx_log_record_t!(zx_log_record_t);
2519
2520impl Default for zx_log_record_t {
2521    fn default() -> zx_log_record_t {
2522        zx_log_record_t {
2523            sequence: 0,
2524            padding1: Default::default(),
2525            datalen: 0,
2526            severity: 0,
2527            flags: 0,
2528            timestamp: 0,
2529            pid: 0,
2530            tid: 0,
2531            data: [0; ZX_LOG_RECORD_DATA_MAX],
2532        }
2533    }
2534}
2535
2536multiconst!(u32, [
2537    ZX_LOG_FLAG_READABLE = 0x40000000;
2538]);
2539
2540// For C, the below types are currently forward declared for syscalls.h.
2541// We might want to investigate a better solution for Rust or removing those
2542// forward declarations.
2543//
2544// These are hand typed translations from C types into Rust structures using a C
2545// layout
2546
2547// source: zircon/system/public/zircon/syscalls/system.h
2548#[repr(C)]
2549pub struct zx_system_powerctl_arg_t {
2550    // rust can't express anonymous unions at this time
2551    // https://github.com/rust-lang/rust/issues/49804
2552    pub powerctl_internal: zx_powerctl_union,
2553}
2554
2555#[repr(C)]
2556#[derive(Copy, Clone)]
2557pub union zx_powerctl_union {
2558    acpi_transition_s_state: acpi_transition_s_state,
2559    x86_power_limit: x86_power_limit,
2560}
2561
2562#[repr(C)]
2563#[derive(Default, Debug, PartialEq, Copy, Clone)]
2564pub struct acpi_transition_s_state {
2565    target_s_state: u8, // Value between 1 and 5 indicating which S-state
2566    sleep_type_a: u8,   // Value from ACPI VM (SLP_TYPa)
2567    sleep_type_b: u8,   // Value from ACPI VM (SLP_TYPb)
2568    padding1: [PadByte; 9],
2569}
2570
2571#[repr(C)]
2572#[derive(Default, Debug, PartialEq, Copy, Clone)]
2573pub struct x86_power_limit {
2574    power_limit: u32, // PL1 value in milliwatts
2575    time_window: u32, // PL1 time window in microseconds
2576    clamp: u8,        // PL1 clamping enable
2577    enable: u8,       // PL1 enable
2578    padding1: [PadByte; 2],
2579}
2580
2581// source: zircon/system/public/zircon/syscalls/pci.h
2582pub type zx_pci_bar_types_t = u32;
2583
2584multiconst!(zx_pci_bar_types_t, [
2585            ZX_PCI_BAR_TYPE_UNUSED = 0;
2586            ZX_PCI_BAR_TYPE_MMIO = 1;
2587            ZX_PCI_BAR_TYPE_PIO = 2;
2588]);
2589
2590#[repr(C)]
2591pub struct zx_pci_bar_t {
2592    pub id: u32,
2593    pub ty: u32,
2594    pub size: usize,
2595    // rust can't express anonymous unions at this time
2596    // https://github.com/rust-lang/rust/issues/49804
2597    pub zx_pci_bar_union: zx_pci_bar_union,
2598}
2599
2600#[repr(C)]
2601#[derive(Copy, Clone)]
2602pub union zx_pci_bar_union {
2603    addr: usize,
2604    zx_pci_bar_union_struct: zx_pci_bar_union_struct,
2605}
2606
2607#[repr(C)]
2608#[derive(Default, Debug, PartialEq, Copy, Clone)]
2609pub struct zx_pci_bar_union_struct {
2610    handle: zx_handle_t,
2611    padding1: [PadByte; 4],
2612}
2613
2614// source: zircon/system/public/zircon/syscalls/smc.h
2615#[repr(C)]
2616#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
2617pub struct zx_smc_parameters_t {
2618    pub func_id: u32,
2619    padding1: [PadByte; 4],
2620    pub arg1: u64,
2621    pub arg2: u64,
2622    pub arg3: u64,
2623    pub arg4: u64,
2624    pub arg5: u64,
2625    pub arg6: u64,
2626    pub client_id: u16,
2627    pub secure_os_id: u16,
2628    padding2: [PadByte; 4],
2629}
2630
2631#[repr(C)]
2632#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2633pub struct zx_smc_result_t {
2634    pub arg0: u64,
2635    pub arg1: u64,
2636    pub arg2: u64,
2637    pub arg3: u64,
2638    pub arg6: u64,
2639}
2640
2641pub const ZX_CPU_SET_MAX_CPUS: usize = 512;
2642pub const ZX_CPU_SET_BITS_PER_WORD: usize = 64;
2643
2644#[repr(C)]
2645#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, zerocopy::FromBytes, zerocopy::Immutable)]
2646pub struct zx_cpu_set_t {
2647    pub mask: [u64; ZX_CPU_SET_MAX_CPUS / ZX_CPU_SET_BITS_PER_WORD],
2648}
2649
2650// source: zircon/system/public/zircon/syscalls/scheduler.h
2651#[repr(C)]
2652#[derive(Copy, Clone)]
2653pub struct zx_profile_info_t {
2654    pub flags: u32,
2655    padding1: [PadByte; 4],
2656    pub zx_profile_info_union: zx_profile_info_union,
2657    pub cpu_affinity_mask: zx_cpu_set_t,
2658}
2659
2660#[cfg(feature = "zerocopy")]
2661impl Default for zx_profile_info_t {
2662    fn default() -> Self {
2663        Self {
2664            flags: Default::default(),
2665            padding1: Default::default(),
2666            zx_profile_info_union: FromZeros::new_zeroed(),
2667            cpu_affinity_mask: Default::default(),
2668        }
2669    }
2670}
2671
2672#[repr(C)]
2673#[derive(Copy, Clone)]
2674#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
2675pub struct priority_params {
2676    pub priority: i32,
2677    padding1: [PadByte; 20],
2678}
2679
2680#[repr(C)]
2681#[derive(Copy, Clone)]
2682#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
2683pub union zx_profile_info_union {
2684    pub priority_params: priority_params,
2685    pub deadline_params: zx_sched_deadline_params_t,
2686}
2687
2688#[repr(C)]
2689#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2690#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, KnownLayout))]
2691pub struct zx_sched_deadline_params_t {
2692    pub capacity: zx_duration_t,
2693    pub relative_deadline: zx_duration_t,
2694    pub period: zx_duration_t,
2695}
2696
2697#[repr(C)]
2698#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2699pub struct zx_cpu_performance_scale_t {
2700    pub integer_part: u32,
2701    pub fractional_part: u32,
2702}
2703
2704#[repr(C)]
2705#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2706pub struct zx_cpu_performance_info_t {
2707    pub logical_cpu_number: u32,
2708    pub performance_scale: zx_cpu_performance_scale_t,
2709}
2710
2711#[repr(C)]
2712#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2713pub struct zx_cpu_power_limit_t {
2714    pub logical_cpu_number: u32,
2715    padding1: [PadByte; 4],
2716    pub max_power_nw: u64,
2717}
2718
2719#[repr(C)]
2720#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2721pub struct zx_iommu_desc_dummy_t {
2722    padding1: PadByte,
2723}
2724
2725multiconst!(u32, [
2726    ZX_IOMMU_TYPE_DUMMY = 0;
2727    ZX_IOMMU_TYPE_INTEL = 1;
2728]);
2729
2730#[repr(C)]
2731#[derive(Debug, Copy, Clone)]
2732pub struct zx_sampler_config_t {
2733    pub period: zx_duration_t,
2734    pub buffer_size: usize,
2735    pub iobuffer_discipline: u64,
2736}
2737
2738multiconst!(zx_processor_power_level_options_t, [
2739    ZX_PROCESSOR_POWER_LEVEL_OPTIONS_DOMAIN_INDEPENDENT = 1 << 0;
2740]);
2741
2742multiconst!(zx_processor_power_control_t, [
2743    ZX_PROCESSOR_POWER_CONTROL_CPU_DRIVER = 0;
2744    ZX_PROCESSOR_POWER_CONTROL_ARM_PSCI = 1;
2745    ZX_PROCESSOR_POWER_CONTROL_ARM_WFI = 2;
2746    ZX_PROCESSOR_POWER_CONTROL_RISCV_SBI = 3;
2747    ZX_PROCESSOR_POWER_CONTROL_RISCV_WFI = 4;
2748]);
2749
2750#[repr(C)]
2751#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2752pub struct zx_processor_power_level_t {
2753    pub options: zx_processor_power_level_options_t,
2754    pub processing_rate: u64,
2755    pub power_coefficient_nw: u64,
2756    pub control_interface: zx_processor_power_control_t,
2757    pub control_argument: u64,
2758    pub diagnostic_name: [u8; ZX_MAX_NAME_LEN],
2759    padding1: [PadByte; 32],
2760}
2761
2762#[repr(C)]
2763#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2764pub struct zx_processor_power_level_transition_t {
2765    pub latency: zx_duration_t,
2766    pub energy: u64,
2767    pub from: u8,
2768    pub to: u8,
2769    padding1: [PadByte; 6],
2770}
2771
2772#[repr(C)]
2773#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
2774pub struct zx_packet_processor_power_level_transition_request_t {
2775    pub domain_id: u32,
2776    pub options: u32,
2777    pub control_interface: u64,
2778    pub control_argument: u64,
2779    padding1: [PadByte; 8],
2780}
2781
2782#[repr(C)]
2783#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2784pub struct zx_processor_power_state_t {
2785    pub domain_id: u32,
2786    pub options: u32,
2787    pub control_interface: u64,
2788    pub control_argument: u64,
2789}
2790
2791#[repr(C)]
2792#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
2793pub struct zx_processor_power_domain_t {
2794    pub cpus: zx_cpu_set_t,
2795    pub domain_id: u32,
2796    padding1: [PadByte; 4],
2797}
2798
2799#[repr(C)]
2800#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2801pub struct zx_power_domain_info_t {
2802    pub cpus: zx_cpu_set_t,
2803    pub domain_id: u32,
2804    pub idle_power_levels: u8,
2805    pub active_power_levels: u8,
2806    padding1: [PadByte; 2],
2807}
2808
2809multiconst!(u32, [
2810    ZX_BTI_PERM_READ = 1 << 0;
2811    ZX_BTI_PERM_WRITE = 1 << 1;
2812    ZX_BTI_PERM_EXECUTE = 1 << 2;
2813    ZX_BTI_COMPRESS = 1 << 3;
2814    ZX_BTI_CONTIGUOUS = 1 << 4;
2815]);
2816
2817// Options for zx_port_create
2818multiconst!(u32, [
2819    ZX_PORT_BIND_TO_INTERRUPT = 1 << 0;
2820]);
2821
2822// Options for zx_interrupt_create
2823multiconst!(u32, [
2824    ZX_INTERRUPT_VIRTUAL = 0x10;
2825    ZX_INTERRUPT_TIMESTAMP_MONO = 1 << 6;
2826]);
2827
2828// Options for zx_interrupt_bind
2829multiconst!(u32, [
2830    ZX_INTERRUPT_BIND = 0;
2831    ZX_INTERRUPT_UNBIND = 1;
2832]);
2833
2834#[repr(C)]
2835pub struct zx_iob_region_t {
2836    pub r#type: zx_iob_region_type_t,
2837    pub access: zx_iob_access_t,
2838    pub size: u64,
2839    pub discipline: zx_iob_discipline_t,
2840    pub extension: zx_iob_region_extension_t,
2841}
2842
2843multiconst!(zx_iob_region_type_t, [
2844    ZX_IOB_REGION_TYPE_PRIVATE = 0;
2845    ZX_IOB_REGION_TYPE_SHARED = 1;
2846]);
2847
2848multiconst!(zx_iob_access_t, [
2849    ZX_IOB_ACCESS_EP0_CAN_MAP_READ = 1 << 0;
2850    ZX_IOB_ACCESS_EP0_CAN_MAP_WRITE = 1 << 1;
2851    ZX_IOB_ACCESS_EP0_CAN_MEDIATED_READ = 1 << 2;
2852    ZX_IOB_ACCESS_EP0_CAN_MEDIATED_WRITE = 1 << 3;
2853    ZX_IOB_ACCESS_EP1_CAN_MAP_READ = 1 << 4;
2854    ZX_IOB_ACCESS_EP1_CAN_MAP_WRITE = 1 << 5;
2855    ZX_IOB_ACCESS_EP1_CAN_MEDIATED_READ = 1 << 6;
2856    ZX_IOB_ACCESS_EP1_CAN_MEDIATED_WRITE = 1 << 7;
2857]);
2858
2859#[repr(C)]
2860#[derive(Copy, Clone)]
2861pub struct zx_iob_discipline_t {
2862    pub r#type: zx_iob_discipline_type_t,
2863    pub extension: zx_iob_discipline_extension_t,
2864}
2865
2866#[repr(C)]
2867#[derive(Copy, Clone)]
2868pub union zx_iob_discipline_extension_t {
2869    // This is in vdso-next.
2870    pub ring_buffer: zx_iob_discipline_mediated_write_ring_buffer_t,
2871    pub reserved: [PadByte; 64],
2872}
2873
2874#[repr(C)]
2875#[derive(Debug, Copy, Clone)]
2876pub struct zx_iob_discipline_mediated_write_ring_buffer_t {
2877    pub tag: u64,
2878    pub padding: [PadByte; 56],
2879}
2880
2881multiconst!(zx_iob_discipline_type_t, [
2882    ZX_IOB_DISCIPLINE_TYPE_NONE = 0;
2883    ZX_IOB_DISCIPLINE_TYPE_MEDIATED_WRITE_RING_BUFFER = 2;
2884]);
2885
2886#[repr(C)]
2887#[derive(Clone, Copy, Default)]
2888pub struct zx_iob_region_private_t {
2889    options: u32,
2890    padding: [PadByte; 28],
2891}
2892
2893#[repr(C)]
2894#[derive(Clone, Copy)]
2895pub struct zx_iob_region_shared_t {
2896    pub options: u32,
2897    pub shared_region: zx_handle_t,
2898    pub padding: [PadByte; 24],
2899}
2900
2901#[repr(C)]
2902pub union zx_iob_region_extension_t {
2903    pub private_region: zx_iob_region_private_t,
2904    pub shared_region: zx_iob_region_shared_t,
2905    pub max_extension: [u8; 32],
2906}
2907
2908#[repr(C)]
2909pub struct zx_wake_source_report_entry_t {
2910    pub koid: zx_koid_t,
2911    pub name: [u8; ZX_MAX_NAME_LEN],
2912    pub initial_signal_time: zx_instant_boot_t,
2913    pub last_signal_time: zx_instant_boot_t,
2914    pub last_ack_time: zx_instant_boot_t,
2915    pub signal_count: u32,
2916    pub flags: u32,
2917}
2918
2919#[repr(C)]
2920pub struct zx_wake_source_report_header_t {
2921    pub report_time: zx_instant_boot_t,
2922    pub suspend_start_time: zx_instant_boot_t,
2923    pub total_wake_sources: u32,
2924    pub unreported_wake_report_entries: u32,
2925}
2926
2927#[cfg(test)]
2928mod test {
2929    use super::*;
2930
2931    #[test]
2932    fn padded_struct_equality() {
2933        let test_struct = zx_clock_update_args_v1_t {
2934            rate_adjust: 222,
2935            padding1: Default::default(),
2936            value: 333,
2937            error_bound: 444,
2938        };
2939
2940        let different_data = zx_clock_update_args_v1_t { rate_adjust: 999, ..test_struct.clone() };
2941
2942        let different_padding = zx_clock_update_args_v1_t {
2943            padding1: [PadByte(0), PadByte(1), PadByte(2), PadByte(3)],
2944            ..test_struct.clone()
2945        };
2946
2947        // Structures with different data should not be equal.
2948        assert_ne!(test_struct, different_data);
2949        // Structures with only different padding should not be equal.
2950        assert_eq!(test_struct, different_padding);
2951    }
2952
2953    #[test]
2954    fn padded_struct_debug() {
2955        let test_struct = zx_clock_update_args_v1_t {
2956            rate_adjust: 222,
2957            padding1: Default::default(),
2958            value: 333,
2959            error_bound: 444,
2960        };
2961        let expectation = "zx_clock_update_args_v1_t { \
2962            rate_adjust: 222, \
2963            padding1: [-, -, -, -], \
2964            value: 333, \
2965            error_bound: 444 }";
2966        assert_eq!(format!("{:?}", test_struct), expectation);
2967    }
2968}