Skip to main content

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    // Virtual Interrupt
533    ZX_VIRTUAL_INTERRUPT_UNTRIGGERED = ZX_OBJECT_SIGNAL_4;
534
535    // Counter
536    ZX_COUNTER_SIGNALED          = ZX_OBJECT_SIGNAL_3;
537    ZX_COUNTER_POSITIVE          = ZX_OBJECT_SIGNAL_4;
538    ZX_COUNTER_NON_POSITIVE      = ZX_OBJECT_SIGNAL_5;
539]);
540
541multiconst!(zx_obj_type_t, [
542    ZX_OBJ_TYPE_NONE                = 0;
543    ZX_OBJ_TYPE_PROCESS             = 1;
544    ZX_OBJ_TYPE_THREAD              = 2;
545    ZX_OBJ_TYPE_VMO                 = 3;
546    ZX_OBJ_TYPE_CHANNEL             = 4;
547    ZX_OBJ_TYPE_EVENT               = 5;
548    ZX_OBJ_TYPE_PORT                = 6;
549    ZX_OBJ_TYPE_INTERRUPT           = 9;
550    ZX_OBJ_TYPE_PCI_DEVICE          = 11;
551    ZX_OBJ_TYPE_DEBUGLOG            = 12;
552    ZX_OBJ_TYPE_SOCKET              = 14;
553    ZX_OBJ_TYPE_RESOURCE            = 15;
554    ZX_OBJ_TYPE_EVENTPAIR           = 16;
555    ZX_OBJ_TYPE_JOB                 = 17;
556    ZX_OBJ_TYPE_VMAR                = 18;
557    ZX_OBJ_TYPE_FIFO                = 19;
558    ZX_OBJ_TYPE_GUEST               = 20;
559    ZX_OBJ_TYPE_VCPU                = 21;
560    ZX_OBJ_TYPE_TIMER               = 22;
561    ZX_OBJ_TYPE_IOMMU               = 23;
562    ZX_OBJ_TYPE_BTI                 = 24;
563    ZX_OBJ_TYPE_PROFILE             = 25;
564    ZX_OBJ_TYPE_PMT                 = 26;
565    ZX_OBJ_TYPE_SUSPEND_TOKEN       = 27;
566    ZX_OBJ_TYPE_PAGER               = 28;
567    ZX_OBJ_TYPE_EXCEPTION           = 29;
568    ZX_OBJ_TYPE_CLOCK               = 30;
569    ZX_OBJ_TYPE_STREAM              = 31;
570    ZX_OBJ_TYPE_MSI                 = 32;
571    ZX_OBJ_TYPE_IOB                 = 33;
572    ZX_OBJ_TYPE_COUNTER             = 34;
573]);
574
575// System ABI commits to having no more than 64 object types.
576//
577// See zx_info_process_handle_stats_t for an example of a binary interface that
578// depends on having an upper bound for the number of object types.
579pub const ZX_OBJ_TYPE_UPPER_BOUND: usize = 64;
580
581// TODO: add an alias for this type in the C headers.
582multiconst!(u32, [
583    // Argument is a char[ZX_MAX_NAME_LEN].
584    ZX_PROP_NAME                      = 3;
585
586    // Argument is a uintptr_t.
587    #[cfg(target_arch = "x86_64")]
588    ZX_PROP_REGISTER_GS               = 2;
589    #[cfg(target_arch = "x86_64")]
590    ZX_PROP_REGISTER_FS               = 4;
591
592    // Argument is the value of ld.so's _dl_debug_addr, a uintptr_t.
593    ZX_PROP_PROCESS_DEBUG_ADDR        = 5;
594
595    // Argument is the base address of the vDSO mapping (or zero), a uintptr_t.
596    ZX_PROP_PROCESS_VDSO_BASE_ADDRESS = 6;
597
598    // Whether the dynamic loader should issue a debug trap when loading a shared
599    // library, either initially or when running (e.g. dlopen).
600    ZX_PROP_PROCESS_BREAK_ON_LOAD = 7;
601
602    // Argument is a size_t.
603    ZX_PROP_SOCKET_RX_THRESHOLD       = 12;
604    ZX_PROP_SOCKET_TX_THRESHOLD       = 13;
605
606    // Argument is a size_t, describing the number of packets a channel
607    // endpoint can have pending in its tx direction.
608    ZX_PROP_CHANNEL_TX_MSG_MAX        = 14;
609
610    // Terminate this job if the system is low on memory.
611    ZX_PROP_JOB_KILL_ON_OOM           = 15;
612
613    // Exception close behavior.
614    ZX_PROP_EXCEPTION_STATE           = 16;
615
616    // The size of the content in a VMO, in bytes.
617    ZX_PROP_VMO_CONTENT_SIZE          = 17;
618
619    // How an exception should be handled.
620    ZX_PROP_EXCEPTION_STRATEGY        = 18;
621
622    // Whether the stream is in append mode or not.
623    ZX_PROP_STREAM_MODE_APPEND        = 19;
624]);
625
626// Value for ZX_THREAD_STATE_SINGLE_STEP. The value can be 0 (not single-stepping), or 1
627// (single-stepping). Other values will give ZX_ERR_INVALID_ARGS.
628pub type zx_thread_state_single_step_t = u32;
629
630// Possible values for "kind" in zx_thread_read_state and zx_thread_write_state.
631multiconst!(zx_thread_state_topic_t, [
632    ZX_THREAD_STATE_GENERAL_REGS       = 0;
633    ZX_THREAD_STATE_FP_REGS            = 1;
634    ZX_THREAD_STATE_VECTOR_REGS        = 2;
635    // No 3 at the moment.
636    ZX_THREAD_STATE_DEBUG_REGS         = 4;
637    ZX_THREAD_STATE_SINGLE_STEP        = 5;
638]);
639
640// Possible values for "kind" in zx_vcpu_read_state and zx_vcpu_write_state.
641multiconst!(zx_vcpu_state_topic_t, [
642    ZX_VCPU_STATE   = 0;
643    ZX_VCPU_IO      = 1;
644]);
645
646// From //zircon/system/public/zircon/features.h
647multiconst!(u32, [
648    ZX_FEATURE_KIND_CPU                        = 0;
649    ZX_FEATURE_KIND_HW_BREAKPOINT_COUNT        = 1;
650    ZX_FEATURE_KIND_HW_WATCHPOINT_COUNT        = 2;
651    ZX_FEATURE_KIND_ADDRESS_TAGGING            = 3;
652    ZX_FEATURE_KIND_VM                         = 4;
653]);
654
655// From //zircon/system/public/zircon/features.h
656multiconst!(u32, [
657    ZX_HAS_CPU_FEATURES                   = 1 << 0;
658
659    ZX_VM_FEATURE_CAN_MAP_XOM             = 1 << 0;
660
661    ZX_ARM64_FEATURE_ISA_FP               = 1 << 1;
662    ZX_ARM64_FEATURE_ISA_ASIMD            = 1 << 2;
663    ZX_ARM64_FEATURE_ISA_AES              = 1 << 3;
664    ZX_ARM64_FEATURE_ISA_PMULL            = 1 << 4;
665    ZX_ARM64_FEATURE_ISA_SHA1             = 1 << 5;
666    ZX_ARM64_FEATURE_ISA_SHA256           = 1 << 6;
667    ZX_ARM64_FEATURE_ISA_CRC32            = 1 << 7;
668    ZX_ARM64_FEATURE_ISA_ATOMICS          = 1 << 8;
669    ZX_ARM64_FEATURE_ISA_RDM              = 1 << 9;
670    ZX_ARM64_FEATURE_ISA_SHA3             = 1 << 10;
671    ZX_ARM64_FEATURE_ISA_SM3              = 1 << 11;
672    ZX_ARM64_FEATURE_ISA_SM4              = 1 << 12;
673    ZX_ARM64_FEATURE_ISA_DP               = 1 << 13;
674    ZX_ARM64_FEATURE_ISA_DPB              = 1 << 14;
675    ZX_ARM64_FEATURE_ISA_FHM              = 1 << 15;
676    ZX_ARM64_FEATURE_ISA_TS               = 1 << 16;
677    ZX_ARM64_FEATURE_ISA_RNDR             = 1 << 17;
678    ZX_ARM64_FEATURE_ISA_SHA512           = 1 << 18;
679    ZX_ARM64_FEATURE_ISA_I8MM             = 1 << 19;
680    ZX_ARM64_FEATURE_ISA_SVE              = 1 << 20;
681    ZX_ARM64_FEATURE_ISA_ARM32            = 1 << 21;
682    ZX_ARM64_FEATURE_ISA_SHA2             = 1 << 6;
683    ZX_ARM64_FEATURE_ADDRESS_TAGGING_TBI  = 1 << 0;
684]);
685
686// From //zircon/system/public/zircon/syscalls/resource.h
687multiconst!(zx_rsrc_kind_t, [
688    ZX_RSRC_KIND_MMIO       = 0;
689    ZX_RSRC_KIND_IRQ        = 1;
690    ZX_RSRC_KIND_IOPORT     = 2;
691    ZX_RSRC_KIND_ROOT       = 3;
692    ZX_RSRC_KIND_SMC        = 4;
693    ZX_RSRC_KIND_SYSTEM     = 5;
694]);
695
696// From //zircon/system/public/zircon/syscalls/resource.h
697multiconst!(zx_rsrc_system_base_t, [
698    ZX_RSRC_SYSTEM_HYPERVISOR_BASE  = 0;
699    ZX_RSRC_SYSTEM_VMEX_BASE        = 1;
700    ZX_RSRC_SYSTEM_DEBUG_BASE       = 2;
701    ZX_RSRC_SYSTEM_INFO_BASE        = 3;
702    ZX_RSRC_SYSTEM_CPU_BASE         = 4;
703    ZX_RSRC_SYSTEM_POWER_BASE       = 5;
704    ZX_RSRC_SYSTEM_MEXEC_BASE       = 6;
705    ZX_RSRC_SYSTEM_ENERGY_INFO_BASE = 7;
706    ZX_RSRC_SYSTEM_IOMMU_BASE       = 8;
707    ZX_RSRC_SYSTEM_FRAMEBUFFER_BASE = 9;
708    ZX_RSRC_SYSTEM_PROFILE_BASE     = 10;
709    ZX_RSRC_SYSTEM_MSI_BASE         = 11;
710    ZX_RSRC_SYSTEM_DEBUGLOG_BASE    = 12;
711    ZX_RSRC_SYSTEM_STALL_BASE       = 13;
712    ZX_RSRC_SYSTEM_TRACING_BASE     = 14;
713    // A resource representing the ability to sample callstack information about other processes.
714    ZX_RSRC_SYSTEM_SAMPLING_BASE    = 15;
715]);
716
717// clock ids
718multiconst!(zx_clock_t, [
719    ZX_CLOCK_MONOTONIC = 0;
720    ZX_CLOCK_BOOT      = 1;
721]);
722
723// from //zircon/system/public/zircon/syscalls/clock.h
724multiconst!(u64, [
725    ZX_CLOCK_OPT_MONOTONIC = 1 << 0;
726    ZX_CLOCK_OPT_CONTINUOUS = 1 << 1;
727    ZX_CLOCK_OPT_AUTO_START = 1 << 2;
728    ZX_CLOCK_OPT_BOOT = 1 << 3;
729    ZX_CLOCK_OPT_MAPPABLE = 1 << 4;
730
731    // v1 clock update flags
732    ZX_CLOCK_UPDATE_OPTION_VALUE_VALID = 1 << 0;
733    ZX_CLOCK_UPDATE_OPTION_RATE_ADJUST_VALID = 1 << 1;
734    ZX_CLOCK_UPDATE_OPTION_ERROR_BOUND_VALID = 1 << 2;
735
736    // Additional v2 clock update flags
737    ZX_CLOCK_UPDATE_OPTION_REFERENCE_VALUE_VALID = 1 << 3;
738    ZX_CLOCK_UPDATE_OPTION_SYNTHETIC_VALUE_VALID = ZX_CLOCK_UPDATE_OPTION_VALUE_VALID;
739
740    ZX_CLOCK_ARGS_VERSION_1 = 1 << 58;
741    ZX_CLOCK_ARGS_VERSION_2 = 2 << 58;
742]);
743
744// from //zircon/system/public/zircon/syscalls/exception.h
745multiconst!(u32, [
746    ZX_EXCEPTION_CHANNEL_DEBUGGER = 1 << 0;
747    ZX_EXCEPTION_TARGET_JOB_DEBUGGER = 1 << 0;
748
749    // Returned when probing a thread for its blocked state.
750    ZX_EXCEPTION_CHANNEL_TYPE_NONE = 0;
751    ZX_EXCEPTION_CHANNEL_TYPE_DEBUGGER = 1;
752    ZX_EXCEPTION_CHANNEL_TYPE_THREAD = 2;
753    ZX_EXCEPTION_CHANNEL_TYPE_PROCESS = 3;
754    ZX_EXCEPTION_CHANNEL_TYPE_JOB = 4;
755    ZX_EXCEPTION_CHANNEL_TYPE_JOB_DEBUGGER = 5;
756]);
757
758/// A byte used only to control memory alignment. All padding bytes are considered equal
759/// regardless of their content.
760///
761/// Note that the kernel C/C++ struct definitions use explicit padding fields to ensure no implicit
762/// padding is added. This is important for security since implicit padding bytes are not always
763/// safely initialized. These explicit padding fields are mirrored in the Rust struct definitions
764/// to minimize the opportunities for mistakes and inconsistencies.
765#[repr(transparent)]
766#[derive(Copy, Clone, Eq, Default)]
767#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes))]
768pub struct PadByte(u8);
769
770impl PartialEq for PadByte {
771    fn eq(&self, _other: &Self) -> bool {
772        true
773    }
774}
775
776impl Hash for PadByte {
777    fn hash<H: Hasher>(&self, state: &mut H) {
778        state.write_u8(0);
779    }
780}
781
782impl Debug for PadByte {
783    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
784        f.write_str("-")
785    }
786}
787
788#[repr(C)]
789#[derive(Debug, Clone, Eq, PartialEq)]
790pub struct zx_clock_create_args_v1_t {
791    pub backstop_time: zx_time_t,
792}
793
794#[repr(C)]
795#[derive(Debug, Default, Clone, Eq, PartialEq)]
796pub struct zx_clock_rate_t {
797    pub synthetic_ticks: u32,
798    pub reference_ticks: u32,
799}
800
801#[repr(C)]
802#[derive(Debug, Default, Clone, Eq, PartialEq)]
803pub struct zx_clock_transformation_t {
804    pub reference_offset: i64,
805    pub synthetic_offset: i64,
806    pub rate: zx_clock_rate_t,
807}
808
809#[repr(C)]
810#[derive(Debug, Default, Clone, Eq, PartialEq)]
811pub struct zx_clock_details_v1_t {
812    pub options: u64,
813    pub backstop_time: zx_time_t,
814    pub reference_ticks_to_synthetic: zx_clock_transformation_t,
815    pub reference_to_synthetic: zx_clock_transformation_t,
816    pub error_bound: u64,
817    pub query_ticks: zx_ticks_t,
818    pub last_value_update_ticks: zx_ticks_t,
819    pub last_rate_adjust_update_ticks: zx_ticks_t,
820    pub last_error_bounds_update_ticks: zx_ticks_t,
821    pub generation_counter: u32,
822    padding1: [PadByte; 4],
823}
824
825#[repr(C)]
826#[derive(Debug, Clone, Eq, PartialEq)]
827pub struct zx_clock_update_args_v1_t {
828    pub rate_adjust: i32,
829    padding1: [PadByte; 4],
830    pub value: i64,
831    pub error_bound: u64,
832}
833
834#[repr(C)]
835#[derive(Debug, Default, Clone, Eq, PartialEq)]
836pub struct zx_clock_update_args_v2_t {
837    pub rate_adjust: i32,
838    padding1: [PadByte; 4],
839    pub synthetic_value: i64,
840    pub reference_value: i64,
841    pub error_bound: u64,
842}
843
844multiconst!(zx_stream_seek_origin_t, [
845    ZX_STREAM_SEEK_ORIGIN_START        = 0;
846    ZX_STREAM_SEEK_ORIGIN_CURRENT      = 1;
847    ZX_STREAM_SEEK_ORIGIN_END          = 2;
848]);
849
850// Stream constants
851pub const ZX_STREAM_MODE_READ: u32 = 1 << 0;
852pub const ZX_STREAM_MODE_WRITE: u32 = 1 << 1;
853pub const ZX_STREAM_MODE_APPEND: u32 = 1 << 2;
854
855pub const ZX_STREAM_APPEND: u32 = 1 << 0;
856
857pub const ZX_CPRNG_ADD_ENTROPY_MAX_LEN: usize = 256;
858
859// Socket flags and limits.
860pub const ZX_SOCKET_STREAM: u32 = 0;
861pub const ZX_SOCKET_DATAGRAM: u32 = 1 << 0;
862pub const ZX_SOCKET_DISPOSITION_WRITE_DISABLED: u32 = 1 << 0;
863pub const ZX_SOCKET_DISPOSITION_WRITE_ENABLED: u32 = 1 << 1;
864
865// VM Object clone flags
866pub const ZX_VMO_CHILD_SNAPSHOT: u32 = 1 << 0;
867pub const ZX_VMO_CHILD_SNAPSHOT_AT_LEAST_ON_WRITE: u32 = 1 << 4;
868pub const ZX_VMO_CHILD_RESIZABLE: u32 = 1 << 2;
869pub const ZX_VMO_CHILD_SLICE: u32 = 1 << 3;
870pub const ZX_VMO_CHILD_NO_WRITE: u32 = 1 << 5;
871pub const ZX_VMO_CHILD_REFERENCE: u32 = 1 << 6;
872pub const ZX_VMO_CHILD_SNAPSHOT_MODIFIED: u32 = 1 << 7;
873
874// channel write size constants
875pub const ZX_CHANNEL_MAX_MSG_HANDLES: u32 = 64;
876pub const ZX_CHANNEL_MAX_MSG_BYTES: u32 = 65536;
877pub const ZX_CHANNEL_MAX_MSG_IOVEC: u32 = 8192;
878
879// fifo write size constants
880pub const ZX_FIFO_MAX_SIZE_BYTES: u32 = 4096;
881
882// Min/max page size constants
883#[cfg(target_arch = "x86_64")]
884pub const ZX_MIN_PAGE_SHIFT: u32 = 12;
885#[cfg(target_arch = "x86_64")]
886pub const ZX_MAX_PAGE_SHIFT: u32 = 21;
887
888#[cfg(target_arch = "aarch64")]
889pub const ZX_MIN_PAGE_SHIFT: u32 = 12;
890#[cfg(target_arch = "aarch64")]
891pub const ZX_MAX_PAGE_SHIFT: u32 = 16;
892
893#[cfg(target_arch = "riscv64")]
894pub const ZX_MIN_PAGE_SHIFT: u32 = 12;
895#[cfg(target_arch = "riscv64")]
896pub const ZX_MAX_PAGE_SHIFT: u32 = 21;
897
898// Task response codes if a process is externally killed
899pub const ZX_TASK_RETCODE_SYSCALL_KILL: i64 = -1024;
900pub const ZX_TASK_RETCODE_OOM_KILL: i64 = -1025;
901pub const ZX_TASK_RETCODE_POLICY_KILL: i64 = -1026;
902pub const ZX_TASK_RETCODE_VDSO_KILL: i64 = -1027;
903pub const ZX_TASK_RETCODE_EXCEPTION_KILL: i64 = -1028;
904
905// Resource flags.
906pub const ZX_RSRC_FLAG_EXCLUSIVE: zx_rsrc_flags_t = 0x00010000;
907
908// Topics for CPU performance info syscalls
909pub const ZX_CPU_PERF_SCALE: u32 = 1;
910pub const ZX_CPU_DEFAULT_PERF_SCALE: u32 = 2;
911pub const ZX_CPU_PERF_LIMIT: u32 = 3;
912
913// Perf limit types.
914pub const ZX_CPU_PERF_LIMIT_TYPE_RATE: u32 = 0;
915pub const ZX_CPU_PERF_LIMIT_TYPE_POWER: u32 = 1;
916
917// Cache policy flags.
918pub const ZX_CACHE_POLICY_CACHED: u32 = 0;
919pub const ZX_CACHE_POLICY_UNCACHED: u32 = 1;
920pub const ZX_CACHE_POLICY_UNCACHED_DEVICE: u32 = 2;
921pub const ZX_CACHE_POLICY_WRITE_COMBINING: u32 = 3;
922
923// Flag bits for zx_cache_flush.
924multiconst!(u32, [
925    ZX_CACHE_FLUSH_INSN         = 1 << 0;
926    ZX_CACHE_FLUSH_DATA         = 1 << 1;
927    ZX_CACHE_FLUSH_INVALIDATE   = 1 << 2;
928]);
929
930#[repr(C)]
931#[derive(Debug, Copy, Clone, Eq, PartialEq)]
932pub struct zx_wait_item_t {
933    pub handle: zx_handle_t,
934    pub waitfor: zx_signals_t,
935    pub pending: zx_signals_t,
936}
937
938#[repr(C)]
939#[derive(Debug, Copy, Clone, Eq, PartialEq)]
940pub struct zx_waitset_result_t {
941    pub cookie: u64,
942    pub status: zx_status_t,
943    pub observed: zx_signals_t,
944}
945
946#[repr(C)]
947#[derive(Debug, Copy, Clone, Eq, PartialEq)]
948pub struct zx_handle_info_t {
949    pub handle: zx_handle_t,
950    pub ty: zx_obj_type_t,
951    pub rights: zx_rights_t,
952    pub unused: u32,
953}
954
955pub const ZX_CHANNEL_READ_MAY_DISCARD: u32 = 1;
956pub const ZX_CHANNEL_WRITE_USE_IOVEC: u32 = 2;
957
958#[repr(C)]
959#[derive(Debug, Copy, Clone, Eq, PartialEq)]
960pub struct zx_channel_call_args_t {
961    pub wr_bytes: *const u8,
962    pub wr_handles: *const zx_handle_t,
963    pub rd_bytes: *mut u8,
964    pub rd_handles: *mut zx_handle_t,
965    pub wr_num_bytes: u32,
966    pub wr_num_handles: u32,
967    pub rd_num_bytes: u32,
968    pub rd_num_handles: u32,
969}
970
971#[repr(C)]
972#[derive(Debug, Copy, Clone, Eq, PartialEq)]
973pub struct zx_channel_call_etc_args_t {
974    pub wr_bytes: *const u8,
975    pub wr_handles: *mut zx_handle_disposition_t,
976    pub rd_bytes: *mut u8,
977    pub rd_handles: *mut zx_handle_info_t,
978    pub wr_num_bytes: u32,
979    pub wr_num_handles: u32,
980    pub rd_num_bytes: u32,
981    pub rd_num_handles: u32,
982}
983
984#[repr(C)]
985#[derive(Debug, Copy, Clone, Eq, PartialEq)]
986pub struct zx_channel_iovec_t {
987    pub buffer: *const u8,
988    pub capacity: u32,
989    padding1: [PadByte; 4],
990}
991
992impl Default for zx_channel_iovec_t {
993    fn default() -> Self {
994        Self {
995            buffer: std::ptr::null(),
996            capacity: Default::default(),
997            padding1: Default::default(),
998        }
999    }
1000}
1001
1002#[repr(C)]
1003#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1004pub struct zx_handle_disposition_t {
1005    pub operation: zx_handle_op_t,
1006    pub handle: zx_handle_t,
1007    pub type_: zx_obj_type_t,
1008    pub rights: zx_rights_t,
1009    pub result: zx_status_t,
1010}
1011
1012#[repr(C)]
1013#[derive(Debug, Copy, Clone)]
1014pub struct zx_iovec_t {
1015    pub buffer: *const u8,
1016    pub capacity: usize,
1017}
1018
1019pub type zx_pci_irq_swizzle_lut_t = [[[u32; 4]; 8]; 32];
1020
1021#[repr(C)]
1022#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1023pub struct zx_pci_init_arg_t {
1024    pub dev_pin_to_global_irq: zx_pci_irq_swizzle_lut_t,
1025    pub num_irqs: u32,
1026    pub irqs: [zx_irq_t; 32],
1027    pub ecam_window_count: u32,
1028    // Note: the ecam_windows field is actually a variable size array.
1029    // We use a fixed size array to match the C repr.
1030    pub ecam_windows: [zx_ecam_window_t; 1],
1031}
1032
1033#[repr(C)]
1034#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1035pub struct zx_irq_t {
1036    pub global_irq: u32,
1037    pub level_triggered: bool,
1038    pub active_high: bool,
1039}
1040
1041#[repr(C)]
1042#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1043pub struct zx_ecam_window_t {
1044    pub base: u64,
1045    pub size: usize,
1046    pub bus_start: u8,
1047    pub bus_end: u8,
1048}
1049
1050#[repr(C)]
1051#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1052pub struct zx_pcie_device_info_t {
1053    pub vendor_id: u16,
1054    pub device_id: u16,
1055    pub base_class: u8,
1056    pub sub_class: u8,
1057    pub program_interface: u8,
1058    pub revision_id: u8,
1059    pub bus_id: u8,
1060    pub dev_id: u8,
1061    pub func_id: u8,
1062}
1063
1064#[repr(C)]
1065#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1066pub struct zx_pci_resource_t {
1067    pub type_: u32,
1068    pub size: usize,
1069    // TODO: Actually a union
1070    pub pio_addr: usize,
1071}
1072
1073// TODO: Actually a union
1074pub type zx_rrec_t = [u8; 64];
1075
1076// Ports V2
1077#[repr(u32)]
1078#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1079pub enum zx_packet_type_t {
1080    ZX_PKT_TYPE_USER = 0,
1081    ZX_PKT_TYPE_SIGNAL_ONE = 1,
1082    ZX_PKT_TYPE_GUEST_BELL = 3,
1083    ZX_PKT_TYPE_GUEST_MEM = 4,
1084    ZX_PKT_TYPE_GUEST_IO = 5,
1085    ZX_PKT_TYPE_GUEST_VCPU = 6,
1086    ZX_PKT_TYPE_INTERRUPT = 7,
1087    ZX_PKT_TYPE_PAGE_REQUEST = 9,
1088    ZX_PKT_TYPE_PROCESSOR_POWER_LEVEL_TRANSITION_REQUEST = 10,
1089    #[doc(hidden)]
1090    __Nonexhaustive,
1091}
1092
1093impl Default for zx_packet_type_t {
1094    fn default() -> Self {
1095        zx_packet_type_t::ZX_PKT_TYPE_USER
1096    }
1097}
1098
1099#[repr(u32)]
1100#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
1101pub enum zx_packet_guest_vcpu_type_t {
1102    #[default]
1103    ZX_PKT_GUEST_VCPU_INTERRUPT = 0,
1104    ZX_PKT_GUEST_VCPU_STARTUP = 1,
1105    #[doc(hidden)]
1106    __Nonexhaustive,
1107}
1108
1109#[repr(C)]
1110#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1111pub struct zx_packet_signal_t {
1112    pub trigger: zx_signals_t,
1113    pub observed: zx_signals_t,
1114    pub count: u64,
1115    pub timestamp: zx_time_t,
1116}
1117
1118pub const ZX_WAIT_ASYNC_TIMESTAMP: u32 = 1;
1119pub const ZX_WAIT_ASYNC_EDGE: u32 = 2;
1120pub const ZX_WAIT_ASYNC_BOOT_TIMESTAMP: u32 = 4;
1121
1122// Actually a union of different integer types, but this should be good enough.
1123pub type zx_packet_user_t = [u8; 32];
1124
1125#[repr(C)]
1126#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1127pub struct zx_port_packet_t {
1128    pub key: u64,
1129    pub packet_type: zx_packet_type_t,
1130    pub status: i32,
1131    pub union: [u8; 32],
1132}
1133
1134#[repr(C)]
1135#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1136pub struct zx_packet_guest_bell_t {
1137    pub addr: zx_gpaddr_t,
1138}
1139
1140#[repr(C)]
1141#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1142pub struct zx_packet_guest_io_t {
1143    pub port: u16,
1144    pub access_size: u8,
1145    pub input: bool,
1146    pub data: [u8; 4],
1147}
1148
1149#[repr(C)]
1150#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1151#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
1152pub struct zx_packet_guest_vcpu_interrupt_t {
1153    pub mask: u64,
1154    pub vector: u8,
1155    padding1: [PadByte; 7],
1156}
1157
1158#[repr(C)]
1159#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1160#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
1161pub struct zx_packet_guest_vcpu_startup_t {
1162    pub id: u64,
1163    pub entry: zx_gpaddr_t,
1164}
1165
1166#[repr(C)]
1167#[derive(Copy, Clone)]
1168#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
1169pub union zx_packet_guest_vcpu_union_t {
1170    pub interrupt: zx_packet_guest_vcpu_interrupt_t,
1171    pub startup: zx_packet_guest_vcpu_startup_t,
1172}
1173
1174#[cfg(feature = "zerocopy")]
1175impl Default for zx_packet_guest_vcpu_union_t {
1176    fn default() -> Self {
1177        Self::new_zeroed()
1178    }
1179}
1180
1181#[repr(C)]
1182#[derive(Copy, Clone, Default)]
1183pub struct zx_packet_guest_vcpu_t {
1184    pub r#type: zx_packet_guest_vcpu_type_t,
1185    padding1: [PadByte; 4],
1186    pub union: zx_packet_guest_vcpu_union_t,
1187    padding2: [PadByte; 8],
1188}
1189
1190impl PartialEq for zx_packet_guest_vcpu_t {
1191    fn eq(&self, other: &Self) -> bool {
1192        if self.r#type != other.r#type {
1193            return false;
1194        }
1195        match self.r#type {
1196            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_INTERRUPT => unsafe {
1197                self.union.interrupt == other.union.interrupt
1198            },
1199            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_STARTUP => unsafe {
1200                self.union.startup == other.union.startup
1201            },
1202            // No equality relationship is defined for invalid types.
1203            _ => false,
1204        }
1205    }
1206}
1207
1208impl Eq for zx_packet_guest_vcpu_t {}
1209
1210impl Debug for zx_packet_guest_vcpu_t {
1211    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1212        match self.r#type {
1213            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_INTERRUPT => {
1214                write!(f, "type: {:?} union: {:?}", self.r#type, unsafe { self.union.interrupt })
1215            }
1216            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_STARTUP => {
1217                write!(f, "type: {:?} union: {:?}", self.r#type, unsafe { self.union.startup })
1218            }
1219            _ => panic!("unexpected VCPU packet type"),
1220        }
1221    }
1222}
1223
1224#[repr(C)]
1225#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
1226pub struct zx_packet_page_request_t {
1227    pub command: zx_page_request_command_t,
1228    pub flags: u16,
1229    padding1: [PadByte; 4],
1230    pub offset: u64,
1231    pub length: u64,
1232    padding2: [PadByte; 8],
1233}
1234
1235#[repr(u16)]
1236#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
1237pub enum zx_page_request_command_t {
1238    #[default]
1239    ZX_PAGER_VMO_READ = 0x0000,
1240    ZX_PAGER_VMO_COMPLETE = 0x0001,
1241    ZX_PAGER_VMO_DIRTY = 0x0002,
1242    #[doc(hidden)]
1243    __Nonexhaustive,
1244}
1245
1246multiconst!(u32, [
1247    ZX_PAGER_OP_FAIL = 1;
1248    ZX_PAGER_OP_DIRTY = 2;
1249    ZX_PAGER_OP_WRITEBACK_BEGIN = 3;
1250    ZX_PAGER_OP_WRITEBACK_END = 4;
1251]);
1252
1253pub type zx_excp_type_t = u32;
1254
1255multiconst!(zx_excp_type_t, [
1256    ZX_EXCP_GENERAL               = 0x008;
1257    ZX_EXCP_FATAL_PAGE_FAULT      = 0x108;
1258    ZX_EXCP_UNDEFINED_INSTRUCTION = 0x208;
1259    ZX_EXCP_SW_BREAKPOINT         = 0x308;
1260    ZX_EXCP_HW_BREAKPOINT         = 0x408;
1261    ZX_EXCP_UNALIGNED_ACCESS      = 0x508;
1262
1263    ZX_EXCP_SYNTH                 = 0x8000;
1264
1265    ZX_EXCP_THREAD_STARTING       = 0x008 | ZX_EXCP_SYNTH;
1266    ZX_EXCP_THREAD_EXITING        = 0x108 | ZX_EXCP_SYNTH;
1267    ZX_EXCP_POLICY_ERROR          = 0x208 | ZX_EXCP_SYNTH;
1268    ZX_EXCP_PROCESS_STARTING      = 0x308 | ZX_EXCP_SYNTH;
1269    ZX_EXCP_USER                  = 0x309 | ZX_EXCP_SYNTH;
1270]);
1271
1272multiconst!(u32, [
1273    ZX_EXCP_USER_CODE_PROCESS_NAME_CHANGED = 0x0001;
1274
1275    ZX_EXCP_USER_CODE_USER0                = 0xF000;
1276    ZX_EXCP_USER_CODE_USER1                = 0xF001;
1277    ZX_EXCP_USER_CODE_USER2                = 0xF002;
1278]);
1279
1280#[repr(C)]
1281#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1282#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes))]
1283pub struct zx_exception_info_t {
1284    pub pid: zx_koid_t,
1285    pub tid: zx_koid_t,
1286    pub type_: zx_excp_type_t,
1287    padding1: [PadByte; 4],
1288}
1289
1290#[repr(C)]
1291#[derive(Default, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
1292pub struct zx_x86_64_exc_data_t {
1293    pub vector: u64,
1294    pub err_code: u64,
1295    pub cr2: u64,
1296}
1297
1298impl Debug for zx_x86_64_exc_data_t {
1299    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1300        write!(f, "vector 0x{:x} err_code {} cr2 0x{:x}", self.vector, self.err_code, self.cr2)
1301    }
1302}
1303
1304#[repr(C)]
1305#[derive(Default, Copy, Clone, Eq, PartialEq, FromBytes, Immutable)]
1306pub struct zx_arm64_exc_data_t {
1307    pub esr: u32,
1308    padding1: [PadByte; 4],
1309    pub far: u64,
1310    padding2: [PadByte; 8],
1311}
1312
1313impl Debug for zx_arm64_exc_data_t {
1314    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1315        write!(f, "esr 0x{:x} far 0x{:x}", self.esr, self.far)
1316    }
1317}
1318
1319#[repr(C)]
1320#[derive(Default, Copy, Clone, Eq, PartialEq, FromBytes, Immutable)]
1321pub struct zx_riscv64_exc_data_t {
1322    pub cause: u64,
1323    pub tval: u64,
1324    padding1: [PadByte; 8],
1325}
1326
1327impl Debug for zx_riscv64_exc_data_t {
1328    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1329        write!(f, "cause {} tval {}", self.cause, self.tval)
1330    }
1331}
1332
1333#[repr(C)]
1334#[derive(Copy, Clone, KnownLayout, FromBytes, Immutable)]
1335pub union zx_exception_header_arch_t {
1336    pub x86_64: zx_x86_64_exc_data_t,
1337    pub arm_64: zx_arm64_exc_data_t,
1338    pub riscv_64: zx_riscv64_exc_data_t,
1339}
1340
1341impl Debug for zx_exception_header_arch_t {
1342    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1343        write!(f, "zx_exception_header_arch_t ")?;
1344        #[cfg(target_arch = "x86_64")]
1345        {
1346            // SAFETY: Exception reports are presumed to be from the target architecture.
1347            // Even if it was not, it's sound to treat the union as another variant as
1348            // the size and alignment are the same, there is no internal padding, and
1349            // the variants have no validity invariants.
1350            let x86_64 = unsafe { self.x86_64 };
1351            write!(f, "{x86_64:?}")
1352        }
1353        #[cfg(target_arch = "aarch64")]
1354        {
1355            // SAFETY: Exception reports are presumed to be from the target architecture.
1356            // Even if it was not, it's sound to treat the union as another variant as
1357            // the size and alignment are the same, there is no internal padding, and
1358            // the variants have no validity invariants.
1359            let arm_64 = unsafe { self.arm_64 };
1360            write!(f, "{arm_64:?}")
1361        }
1362        #[cfg(target_arch = "riscv64")]
1363        {
1364            // SAFETY: Exception reports are presumed to be from the target architecture.
1365            // Even if it was not, it's sound to treat the union as another variant as
1366            // the size and alignment are the same, there is no internal padding, and
1367            // the variants have no validity invariants.
1368            let riscv_64 = unsafe { self.riscv_64 };
1369            write!(f, "{riscv_64:?}")
1370        }
1371    }
1372}
1373
1374#[repr(C)]
1375#[derive(Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
1376pub struct zx_exception_header_t {
1377    pub size: u32,
1378    pub type_: zx_excp_type_t,
1379}
1380
1381pub type zx_excp_policy_code_t = u32;
1382
1383multiconst!(zx_excp_policy_code_t, [
1384    ZX_EXCP_POLICY_CODE_BAD_HANDLE              = 0;
1385    ZX_EXCP_POLICY_CODE_WRONG_OBJECT            = 1;
1386    ZX_EXCP_POLICY_CODE_VMAR_WX                 = 2;
1387    ZX_EXCP_POLICY_CODE_NEW_ANY                 = 3;
1388    ZX_EXCP_POLICY_CODE_NEW_VMO                 = 4;
1389    ZX_EXCP_POLICY_CODE_NEW_CHANNEL             = 5;
1390    ZX_EXCP_POLICY_CODE_NEW_EVENT               = 6;
1391    ZX_EXCP_POLICY_CODE_NEW_EVENTPAIR           = 7;
1392    ZX_EXCP_POLICY_CODE_NEW_PORT                = 8;
1393    ZX_EXCP_POLICY_CODE_NEW_SOCKET              = 9;
1394    ZX_EXCP_POLICY_CODE_NEW_FIFO                = 10;
1395    ZX_EXCP_POLICY_CODE_NEW_TIMER               = 11;
1396    ZX_EXCP_POLICY_CODE_NEW_PROCESS             = 12;
1397    ZX_EXCP_POLICY_CODE_NEW_PROFILE             = 13;
1398    ZX_EXCP_POLICY_CODE_NEW_PAGER               = 14;
1399    ZX_EXCP_POLICY_CODE_AMBIENT_MARK_VMO_EXEC   = 15;
1400    ZX_EXCP_POLICY_CODE_CHANNEL_FULL_WRITE      = 16;
1401    ZX_EXCP_POLICY_CODE_PORT_TOO_MANY_PACKETS   = 17;
1402    ZX_EXCP_POLICY_CODE_BAD_SYSCALL             = 18;
1403    ZX_EXCP_POLICY_CODE_PORT_TOO_MANY_OBSERVERS = 19;
1404    ZX_EXCP_POLICY_CODE_HANDLE_LEAK             = 20;
1405    ZX_EXCP_POLICY_CODE_NEW_IOB                 = 21;
1406]);
1407
1408#[repr(C)]
1409#[derive(Debug, Copy, Clone, KnownLayout, FromBytes, Immutable)]
1410pub struct zx_exception_context_t {
1411    pub arch: zx_exception_header_arch_t,
1412    pub synth_code: zx_excp_policy_code_t,
1413    pub synth_data: u32,
1414}
1415
1416#[repr(C)]
1417#[derive(Debug, Copy, Clone, KnownLayout, FromBytes, Immutable)]
1418pub struct zx_exception_report_t {
1419    pub header: zx_exception_header_t,
1420    pub context: zx_exception_context_t,
1421}
1422
1423pub type zx_exception_state_t = u32;
1424
1425multiconst!(zx_exception_state_t, [
1426    ZX_EXCEPTION_STATE_TRY_NEXT    = 0;
1427    ZX_EXCEPTION_STATE_HANDLED     = 1;
1428    ZX_EXCEPTION_STATE_THREAD_EXIT = 2;
1429]);
1430
1431pub type zx_exception_strategy_t = u32;
1432
1433multiconst!(zx_exception_state_t, [
1434    ZX_EXCEPTION_STRATEGY_FIRST_CHANCE   = 0;
1435    ZX_EXCEPTION_STRATEGY_SECOND_CHANCE  = 1;
1436]);
1437
1438#[cfg(target_arch = "x86_64")]
1439#[repr(C)]
1440#[derive(Default, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
1441pub struct zx_thread_state_general_regs_t {
1442    pub rax: u64,
1443    pub rbx: u64,
1444    pub rcx: u64,
1445    pub rdx: u64,
1446    pub rsi: u64,
1447    pub rdi: u64,
1448    pub rbp: u64,
1449    pub rsp: u64,
1450    pub r8: u64,
1451    pub r9: u64,
1452    pub r10: u64,
1453    pub r11: u64,
1454    pub r12: u64,
1455    pub r13: u64,
1456    pub r14: u64,
1457    pub r15: u64,
1458    pub rip: u64,
1459    pub rflags: u64,
1460    pub fs_base: u64,
1461    pub gs_base: u64,
1462}
1463
1464#[cfg(target_arch = "x86_64")]
1465impl std::fmt::Debug for zx_thread_state_general_regs_t {
1466    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1467        f.debug_struct(std::any::type_name::<Self>())
1468            .field("rax", &format_args!("{:#x}", self.rax))
1469            .field("rbx", &format_args!("{:#x}", self.rbx))
1470            .field("rcx", &format_args!("{:#x}", self.rcx))
1471            .field("rdx", &format_args!("{:#x}", self.rdx))
1472            .field("rsi", &format_args!("{:#x}", self.rsi))
1473            .field("rdi", &format_args!("{:#x}", self.rdi))
1474            .field("rbp", &format_args!("{:#x}", self.rbp))
1475            .field("rsp", &format_args!("{:#x}", self.rsp))
1476            .field("r8", &format_args!("{:#x}", self.r8))
1477            .field("r9", &format_args!("{:#x}", self.r9))
1478            .field("r10", &format_args!("{:#x}", self.r10))
1479            .field("r11", &format_args!("{:#x}", self.r11))
1480            .field("r12", &format_args!("{:#x}", self.r12))
1481            .field("r13", &format_args!("{:#x}", self.r13))
1482            .field("r14", &format_args!("{:#x}", self.r14))
1483            .field("r15", &format_args!("{:#x}", self.r15))
1484            .field("rip", &format_args!("{:#x}", self.rip))
1485            .field("rflags", &format_args!("{:#x}", self.rflags))
1486            .field("fs_base", &format_args!("{:#x}", self.fs_base))
1487            .field("gs_base", &format_args!("{:#x}", self.gs_base))
1488            .finish()
1489    }
1490}
1491
1492#[cfg(target_arch = "x86_64")]
1493impl From<&zx_restricted_state_t> for zx_thread_state_general_regs_t {
1494    fn from(state: &zx_restricted_state_t) -> Self {
1495        Self {
1496            rdi: state.rdi,
1497            rsi: state.rsi,
1498            rbp: state.rbp,
1499            rbx: state.rbx,
1500            rdx: state.rdx,
1501            rcx: state.rcx,
1502            rax: state.rax,
1503            rsp: state.rsp,
1504            r8: state.r8,
1505            r9: state.r9,
1506            r10: state.r10,
1507            r11: state.r11,
1508            r12: state.r12,
1509            r13: state.r13,
1510            r14: state.r14,
1511            r15: state.r15,
1512            rip: state.ip,
1513            rflags: state.flags,
1514            fs_base: state.fs_base,
1515            gs_base: state.gs_base,
1516        }
1517    }
1518}
1519
1520#[cfg(target_arch = "aarch64")]
1521multiconst!(u64, [
1522    ZX_REG_CPSR_ARCH_32_MASK = 0x10;
1523    ZX_REG_CPSR_THUMB_MASK = 0x20;
1524]);
1525
1526#[cfg(target_arch = "aarch64")]
1527#[repr(C)]
1528#[derive(Default, Copy, Clone, Eq, PartialEq)]
1529pub struct zx_thread_state_general_regs_t {
1530    pub r: [u64; 30],
1531    pub lr: u64,
1532    pub sp: u64,
1533    pub pc: u64,
1534    pub cpsr: u64,
1535    pub tpidr: u64,
1536}
1537
1538#[cfg(target_arch = "aarch64")]
1539impl std::fmt::Debug for zx_thread_state_general_regs_t {
1540    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1541        struct RegisterAsHex(u64);
1542        impl std::fmt::Debug for RegisterAsHex {
1543            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1544                write!(f, "{:#x}", self.0)
1545            }
1546        }
1547
1548        f.debug_struct(std::any::type_name::<Self>())
1549            .field("r", &self.r.map(RegisterAsHex))
1550            .field("lr", &format_args!("{:#x}", self.lr))
1551            .field("sp", &format_args!("{:#x}", self.sp))
1552            .field("pc", &format_args!("{:#x}", self.pc))
1553            .field("cpsr", &format_args!("{:#x}", self.cpsr))
1554            .field("tpidr", &format_args!("{:#x}", self.tpidr))
1555            .finish()
1556    }
1557}
1558
1559#[cfg(target_arch = "aarch64")]
1560impl From<&zx_restricted_state_t> for zx_thread_state_general_regs_t {
1561    fn from(state: &zx_restricted_state_t) -> Self {
1562        if state.cpsr as u64 & ZX_REG_CPSR_ARCH_32_MASK == ZX_REG_CPSR_ARCH_32_MASK {
1563            // aarch32
1564            Self {
1565                r: [
1566                    state.r[0],
1567                    state.r[1],
1568                    state.r[2],
1569                    state.r[3],
1570                    state.r[4],
1571                    state.r[5],
1572                    state.r[6],
1573                    state.r[7],
1574                    state.r[8],
1575                    state.r[9],
1576                    state.r[10],
1577                    state.r[11],
1578                    state.r[12],
1579                    state.r[13],
1580                    state.r[14],
1581                    state.pc, // ELR overwrites this.
1582                    state.r[16],
1583                    state.r[17],
1584                    state.r[18],
1585                    state.r[19],
1586                    state.r[20],
1587                    state.r[21],
1588                    state.r[22],
1589                    state.r[23],
1590                    state.r[24],
1591                    state.r[25],
1592                    state.r[26],
1593                    state.r[27],
1594                    state.r[28],
1595                    state.r[29],
1596                ],
1597                lr: state.r[14], // R[14] for aarch32
1598                sp: state.r[13], // R[13] for aarch32
1599                // TODO(https://fxbug.dev/379669623) Should it be checked for thumb and make
1600                // sure it isn't over incrementing?
1601                pc: state.pc, // Zircon populated this from elr.
1602                cpsr: state.cpsr as u64,
1603                tpidr: state.tpidr_el0,
1604            }
1605        } else {
1606            Self {
1607                r: [
1608                    state.r[0],
1609                    state.r[1],
1610                    state.r[2],
1611                    state.r[3],
1612                    state.r[4],
1613                    state.r[5],
1614                    state.r[6],
1615                    state.r[7],
1616                    state.r[8],
1617                    state.r[9],
1618                    state.r[10],
1619                    state.r[11],
1620                    state.r[12],
1621                    state.r[13],
1622                    state.r[14],
1623                    state.r[15],
1624                    state.r[16],
1625                    state.r[17],
1626                    state.r[18],
1627                    state.r[19],
1628                    state.r[20],
1629                    state.r[21],
1630                    state.r[22],
1631                    state.r[23],
1632                    state.r[24],
1633                    state.r[25],
1634                    state.r[26],
1635                    state.r[27],
1636                    state.r[28],
1637                    state.r[29],
1638                ],
1639                lr: state.r[30],
1640                sp: state.sp,
1641                pc: state.pc,
1642                cpsr: state.cpsr as u64,
1643                tpidr: state.tpidr_el0,
1644            }
1645        }
1646    }
1647}
1648
1649#[cfg(target_arch = "riscv64")]
1650#[repr(C)]
1651#[derive(Default, Copy, Clone, Eq, PartialEq)]
1652pub struct zx_thread_state_general_regs_t {
1653    pub pc: u64,
1654    pub ra: u64,  // x1
1655    pub sp: u64,  // x2
1656    pub gp: u64,  // x3
1657    pub tp: u64,  // x4
1658    pub t0: u64,  // x5
1659    pub t1: u64,  // x6
1660    pub t2: u64,  // x7
1661    pub s0: u64,  // x8
1662    pub s1: u64,  // x9
1663    pub a0: u64,  // x10
1664    pub a1: u64,  // x11
1665    pub a2: u64,  // x12
1666    pub a3: u64,  // x13
1667    pub a4: u64,  // x14
1668    pub a5: u64,  // x15
1669    pub a6: u64,  // x16
1670    pub a7: u64,  // x17
1671    pub s2: u64,  // x18
1672    pub s3: u64,  // x19
1673    pub s4: u64,  // x20
1674    pub s5: u64,  // x21
1675    pub s6: u64,  // x22
1676    pub s7: u64,  // x23
1677    pub s8: u64,  // x24
1678    pub s9: u64,  // x25
1679    pub s10: u64, // x26
1680    pub s11: u64, // x27
1681    pub t3: u64,  // x28
1682    pub t4: u64,  // x29
1683    pub t5: u64,  // x30
1684    pub t6: u64,  // x31
1685}
1686
1687#[cfg(target_arch = "riscv64")]
1688impl std::fmt::Debug for zx_thread_state_general_regs_t {
1689    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1690        f.debug_struct(std::any::type_name::<Self>())
1691            .field("pc", &format_args!("{:#x}", self.pc))
1692            .field("ra", &format_args!("{:#x}", self.ra)) // x1
1693            .field("sp", &format_args!("{:#x}", self.sp)) // x2
1694            .field("gp", &format_args!("{:#x}", self.gp)) // x3
1695            .field("tp", &format_args!("{:#x}", self.tp)) // x4
1696            .field("t0", &format_args!("{:#x}", self.t0)) // x5
1697            .field("t1", &format_args!("{:#x}", self.t1)) // x6
1698            .field("t2", &format_args!("{:#x}", self.t2)) // x7
1699            .field("s0", &format_args!("{:#x}", self.s0)) // x8
1700            .field("s1", &format_args!("{:#x}", self.s1)) // x9
1701            .field("a0", &format_args!("{:#x}", self.a0)) // x10
1702            .field("a1", &format_args!("{:#x}", self.a1)) // x11
1703            .field("a2", &format_args!("{:#x}", self.a2)) // x12
1704            .field("a3", &format_args!("{:#x}", self.a3)) // x13
1705            .field("a4", &format_args!("{:#x}", self.a4)) // x14
1706            .field("a5", &format_args!("{:#x}", self.a5)) // x15
1707            .field("a6", &format_args!("{:#x}", self.a6)) // x16
1708            .field("a7", &format_args!("{:#x}", self.a7)) // x17
1709            .field("s2", &format_args!("{:#x}", self.s2)) // x18
1710            .field("s3", &format_args!("{:#x}", self.s3)) // x19
1711            .field("s4", &format_args!("{:#x}", self.s4)) // x20
1712            .field("s5", &format_args!("{:#x}", self.s5)) // x21
1713            .field("s6", &format_args!("{:#x}", self.s6)) // x22
1714            .field("s7", &format_args!("{:#x}", self.s7)) // x23
1715            .field("s8", &format_args!("{:#x}", self.s8)) // x24
1716            .field("s9", &format_args!("{:#x}", self.s9)) // x25
1717            .field("s10", &format_args!("{:#x}", self.s10)) // x26
1718            .field("s11", &format_args!("{:#x}", self.s11)) // x27
1719            .field("t3", &format_args!("{:#x}", self.t3)) // x28
1720            .field("t4", &format_args!("{:#x}", self.t4)) // x29
1721            .field("t5", &format_args!("{:#x}", self.t5)) // x30
1722            .field("t6", &format_args!("{:#x}", self.t6)) // x31
1723            .finish()
1724    }
1725}
1726
1727multiconst!(zx_restricted_reason_t, [
1728    ZX_RESTRICTED_REASON_SYSCALL = 0;
1729    ZX_RESTRICTED_REASON_EXCEPTION = 1;
1730    ZX_RESTRICTED_REASON_KICK = 2;
1731]);
1732
1733#[cfg(target_arch = "x86_64")]
1734#[repr(C)]
1735#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1736pub struct zx_restricted_state_t {
1737    pub rdi: u64,
1738    pub rsi: u64,
1739    pub rbp: u64,
1740    pub rbx: u64,
1741    pub rdx: u64,
1742    pub rcx: u64,
1743    pub rax: u64,
1744    pub rsp: u64,
1745    pub r8: u64,
1746    pub r9: u64,
1747    pub r10: u64,
1748    pub r11: u64,
1749    pub r12: u64,
1750    pub r13: u64,
1751    pub r14: u64,
1752    pub r15: u64,
1753    pub ip: u64,
1754    pub flags: u64,
1755    pub fs_base: u64,
1756    pub gs_base: u64,
1757}
1758
1759#[cfg(target_arch = "x86_64")]
1760impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
1761    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
1762        Self {
1763            rdi: registers.rdi,
1764            rsi: registers.rsi,
1765            rbp: registers.rbp,
1766            rbx: registers.rbx,
1767            rdx: registers.rdx,
1768            rcx: registers.rcx,
1769            rax: registers.rax,
1770            rsp: registers.rsp,
1771            r8: registers.r8,
1772            r9: registers.r9,
1773            r10: registers.r10,
1774            r11: registers.r11,
1775            r12: registers.r12,
1776            r13: registers.r13,
1777            r14: registers.r14,
1778            r15: registers.r15,
1779            ip: registers.rip,
1780            flags: registers.rflags,
1781            fs_base: registers.fs_base,
1782            gs_base: registers.gs_base,
1783        }
1784    }
1785}
1786
1787#[cfg(target_arch = "aarch64")]
1788#[repr(C)]
1789#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1790pub struct zx_restricted_state_t {
1791    pub r: [u64; 31], // Note: r[30] is `lr` which is separated out in the general regs.
1792    pub sp: u64,
1793    pub pc: u64,
1794    pub tpidr_el0: u64,
1795    // Contains only the user-controllable upper 4-bits (NZCV).
1796    pub cpsr: u32,
1797    padding1: [PadByte; 4],
1798}
1799
1800#[cfg(target_arch = "aarch64")]
1801impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
1802    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
1803        Self {
1804            r: [
1805                registers.r[0],
1806                registers.r[1],
1807                registers.r[2],
1808                registers.r[3],
1809                registers.r[4],
1810                registers.r[5],
1811                registers.r[6],
1812                registers.r[7],
1813                registers.r[8],
1814                registers.r[9],
1815                registers.r[10],
1816                registers.r[11],
1817                registers.r[12],
1818                registers.r[13],
1819                registers.r[14],
1820                registers.r[15],
1821                registers.r[16],
1822                registers.r[17],
1823                registers.r[18],
1824                registers.r[19],
1825                registers.r[20],
1826                registers.r[21],
1827                registers.r[22],
1828                registers.r[23],
1829                registers.r[24],
1830                registers.r[25],
1831                registers.r[26],
1832                registers.r[27],
1833                registers.r[28],
1834                registers.r[29],
1835                registers.lr, // for compat this works nicely with zircon.
1836            ],
1837            pc: registers.pc,
1838            tpidr_el0: registers.tpidr,
1839            sp: registers.sp,
1840            cpsr: registers.cpsr as u32,
1841            padding1: Default::default(),
1842        }
1843    }
1844}
1845
1846#[cfg(target_arch = "riscv64")]
1847pub type zx_restricted_state_t = zx_thread_state_general_regs_t;
1848
1849#[cfg(target_arch = "riscv64")]
1850impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
1851    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
1852        *registers
1853    }
1854}
1855
1856#[repr(C)]
1857#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1858#[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "riscv64"))]
1859pub struct zx_restricted_syscall_t {
1860    pub state: zx_restricted_state_t,
1861}
1862
1863#[repr(C)]
1864#[derive(Copy, Clone)]
1865#[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "riscv64"))]
1866pub struct zx_restricted_exception_t {
1867    pub state: zx_restricted_state_t,
1868    pub exception: zx_exception_report_t,
1869}
1870
1871#[cfg(target_arch = "x86_64")]
1872#[repr(C)]
1873#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1874pub struct zx_vcpu_state_t {
1875    pub rax: u64,
1876    pub rcx: u64,
1877    pub rdx: u64,
1878    pub rbx: u64,
1879    pub rsp: u64,
1880    pub rbp: u64,
1881    pub rsi: u64,
1882    pub rdi: u64,
1883    pub r8: u64,
1884    pub r9: u64,
1885    pub r10: u64,
1886    pub r11: u64,
1887    pub r12: u64,
1888    pub r13: u64,
1889    pub r14: u64,
1890    pub r15: u64,
1891    // Contains only the user-controllable lower 32-bits.
1892    pub rflags: u64,
1893}
1894
1895#[cfg(target_arch = "aarch64")]
1896#[repr(C)]
1897#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1898pub struct zx_vcpu_state_t {
1899    pub x: [u64; 31],
1900    pub sp: u64,
1901    // Contains only the user-controllable upper 4-bits (NZCV).
1902    pub cpsr: u32,
1903    padding1: [PadByte; 4],
1904}
1905
1906#[cfg(target_arch = "riscv64")]
1907#[repr(C)]
1908#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1909pub struct zx_vcpu_state_t {
1910    pub empty: u32,
1911}
1912
1913#[repr(C)]
1914#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1915pub struct zx_vcpu_io_t {
1916    pub access_size: u8,
1917    padding1: [PadByte; 3],
1918    pub data: [u8; 4],
1919}
1920
1921#[cfg(target_arch = "aarch64")]
1922#[repr(C)]
1923#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1924pub struct zx_packet_guest_mem_t {
1925    pub addr: zx_gpaddr_t,
1926    pub access_size: u8,
1927    pub sign_extend: bool,
1928    pub xt: u8,
1929    pub read: bool,
1930    pub data: u64,
1931}
1932
1933#[cfg(target_arch = "riscv64")]
1934#[repr(C)]
1935#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1936pub struct zx_packet_guest_mem_t {
1937    pub addr: zx_gpaddr_t,
1938    padding1: [PadByte; 24],
1939}
1940
1941pub const X86_MAX_INST_LEN: usize = 15;
1942
1943#[cfg(target_arch = "x86_64")]
1944#[repr(C)]
1945#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1946pub struct zx_packet_guest_mem_t {
1947    pub addr: zx_gpaddr_t,
1948    pub cr3: zx_gpaddr_t,
1949    pub rip: zx_vaddr_t,
1950    pub instruction_size: u8,
1951    pub default_operand_size: u8,
1952}
1953
1954#[repr(C)]
1955#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1956pub struct zx_packet_interrupt_t {
1957    pub timestamp: zx_time_t,
1958    padding1: [PadByte; 24],
1959}
1960
1961// Helper for constructing topics that have been versioned.
1962const fn info_topic(topic: u32, version: u32) -> u32 {
1963    (version << 28) | topic
1964}
1965
1966multiconst!(zx_object_info_topic_t, [
1967    ZX_INFO_NONE                       = 0;
1968    ZX_INFO_HANDLE_VALID               = 1;
1969    ZX_INFO_HANDLE_BASIC               = 2;  // zx_info_handle_basic_t[1]
1970    ZX_INFO_PROCESS                    = info_topic(3, 1);  // zx_info_process_t[1]
1971    ZX_INFO_PROCESS_THREADS            = 4;  // zx_koid_t[n]
1972    ZX_INFO_VMAR                       = 7;  // zx_info_vmar_t[1]
1973    ZX_INFO_JOB_CHILDREN               = 8;  // zx_koid_t[n]
1974    ZX_INFO_JOB_PROCESSES              = 9;  // zx_koid_t[n]
1975    ZX_INFO_THREAD                     = 10; // zx_info_thread_t[1]
1976    ZX_INFO_THREAD_EXCEPTION_REPORT    = info_topic(11, 1); // zx_exception_report_t[1]
1977    ZX_INFO_TASK_STATS                 = info_topic(12, 1); // zx_info_task_stats_t[1]
1978    ZX_INFO_PROCESS_MAPS               = info_topic(13, 2); // zx_info_maps_t[n]
1979    ZX_INFO_PROCESS_VMOS               = info_topic(14, 3); // zx_info_vmo_t[n]
1980    ZX_INFO_THREAD_STATS               = 15; // zx_info_thread_stats_t[1]
1981    ZX_INFO_CPU_STATS                  = 16; // zx_info_cpu_stats_t[n]
1982    ZX_INFO_KMEM_STATS                 = info_topic(17, 1); // zx_info_kmem_stats_t[1]
1983    ZX_INFO_RESOURCE                   = 18; // zx_info_resource_t[1]
1984    ZX_INFO_HANDLE_COUNT               = 19; // zx_info_handle_count_t[1]
1985    ZX_INFO_BTI                        = 20; // zx_info_bti_t[1]
1986    ZX_INFO_PROCESS_HANDLE_STATS       = 21; // zx_info_process_handle_stats_t[1]
1987    ZX_INFO_SOCKET                     = 22; // zx_info_socket_t[1]
1988    ZX_INFO_VMO                        = info_topic(23, 3); // zx_info_vmo_t[1]
1989    ZX_INFO_JOB                        = 24; // zx_info_job_t[1]
1990    ZX_INFO_TIMER                      = 25; // zx_info_timer_t[1]
1991    ZX_INFO_STREAM                     = 26; // zx_info_stream_t[1]
1992    ZX_INFO_HANDLE_TABLE               = 27; // zx_info_handle_extended_t[n]
1993    ZX_INFO_MSI                        = 28; // zx_info_msi_t[1]
1994    ZX_INFO_GUEST_STATS                = 29; // zx_info_guest_stats_t[1]
1995    ZX_INFO_TASK_RUNTIME               = info_topic(30, 1); // zx_info_task_runtime_t[1]
1996    ZX_INFO_KMEM_STATS_EXTENDED        = 31; // zx_info_kmem_stats_extended_t[1]
1997    ZX_INFO_VCPU                       = 32; // zx_info_vcpu_t[1]
1998    ZX_INFO_KMEM_STATS_COMPRESSION     = 33; // zx_info_kmem_stats_compression_t[1]
1999    ZX_INFO_IOB                        = 34; // zx_info_iob_t[1]
2000    ZX_INFO_IOB_REGIONS                = 35; // zx_iob_region_info_t[n]
2001    ZX_INFO_VMAR_MAPS                  = 36; // zx_info_maps_t[n]
2002    ZX_INFO_POWER_DOMAINS              = 37; // zx_info_power_domain_info_t[n]
2003    ZX_INFO_MEMORY_STALL               = 38; // zx_info_memory_stall_t[1]
2004    ZX_INFO_CLOCK_MAPPED_SIZE          = 40; // usize[1]
2005]);
2006
2007multiconst!(zx_system_memory_stall_type_t, [
2008    ZX_SYSTEM_MEMORY_STALL_SOME        = 0;
2009    ZX_SYSTEM_MEMORY_STALL_FULL        = 1;
2010]);
2011
2012// This macro takes struct-like syntax and creates another macro that can be used to create
2013// different instances of the struct with different names. This is used to keep struct definitions
2014// from drifting between this crate and the fuchsia-zircon crate where they are identical other
2015// than in name and location.
2016macro_rules! struct_decl_macro {
2017    ( $(#[$attrs:meta])* $vis:vis struct <$macro_name:ident> $($any:tt)* ) => {
2018        #[macro_export]
2019        macro_rules! $macro_name {
2020            ($name:ident) => {
2021                $(#[$attrs])* $vis struct $name $($any)*
2022            }
2023        }
2024    }
2025}
2026
2027// Don't need struct_decl_macro for this, the wrapper is different.
2028#[repr(C)]
2029#[derive(Default, Debug, Copy, Clone, Eq, KnownLayout, FromBytes, Immutable, PartialEq)]
2030pub struct zx_info_handle_basic_t {
2031    pub koid: zx_koid_t,
2032    pub rights: zx_rights_t,
2033    pub type_: zx_obj_type_t,
2034    pub related_koid: zx_koid_t,
2035    padding1: [PadByte; 4],
2036}
2037
2038struct_decl_macro! {
2039    #[repr(C)]
2040    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2041    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2042    pub struct <zx_info_handle_count_t> {
2043        pub handle_count: u32,
2044    }
2045}
2046
2047zx_info_handle_count_t!(zx_info_handle_count_t);
2048
2049// Don't need struct_decl_macro for this, the wrapper is different.
2050#[repr(C)]
2051#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
2052pub struct zx_info_socket_t {
2053    pub options: u32,
2054    pub rx_buf_max: usize,
2055    pub rx_buf_size: usize,
2056    pub rx_buf_available: usize,
2057    pub tx_buf_max: usize,
2058    pub tx_buf_size: usize,
2059}
2060
2061multiconst!(u32, [
2062    ZX_INFO_PROCESS_FLAG_STARTED = 1 << 0;
2063    ZX_INFO_PROCESS_FLAG_EXITED = 1 << 1;
2064    ZX_INFO_PROCESS_FLAG_DEBUGGER_ATTACHED = 1 << 2;
2065]);
2066
2067struct_decl_macro! {
2068    #[repr(C)]
2069    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2070    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2071    pub struct <zx_info_process_t> {
2072        pub return_code: i64,
2073        pub start_time: zx_time_t,
2074        pub flags: u32,
2075    }
2076}
2077
2078zx_info_process_t!(zx_info_process_t);
2079
2080struct_decl_macro! {
2081    #[repr(C)]
2082    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2083    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2084    pub struct <zx_info_job_t> {
2085        pub return_code: i64,
2086        pub exited: u8,
2087        pub kill_on_oom: u8,
2088        pub debugger_attached: u8,
2089    }
2090}
2091
2092zx_info_job_t!(zx_info_job_t);
2093
2094struct_decl_macro! {
2095    #[repr(C)]
2096    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2097    #[derive(zerocopy::FromBytes, zerocopy::IntoBytes, zerocopy::Immutable)]
2098    pub struct <zx_info_timer_t> {
2099        pub options: u32,
2100        pub clock_id: zx_clock_t,
2101        pub deadline: zx_time_t,
2102        pub slack: zx_duration_t,
2103    }
2104}
2105
2106zx_info_timer_t!(zx_info_timer_t);
2107
2108#[repr(C)]
2109#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2110pub struct zx_policy_basic {
2111    pub condition: u32,
2112    pub policy: u32,
2113}
2114
2115#[repr(C)]
2116#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2117pub struct zx_policy_timer_slack {
2118    pub min_slack: zx_duration_t,
2119    pub default_mode: u32,
2120}
2121
2122multiconst!(u32, [
2123    // policy options
2124    ZX_JOB_POL_RELATIVE = 0;
2125    ZX_JOB_POL_ABSOLUTE = 1;
2126
2127    // policy topic
2128    ZX_JOB_POL_BASIC = 0;
2129    ZX_JOB_POL_TIMER_SLACK = 1;
2130
2131    // policy conditions
2132    ZX_POL_BAD_HANDLE            = 0;
2133    ZX_POL_WRONG_OBJECT          = 1;
2134    ZX_POL_VMAR_WX               = 2;
2135    ZX_POL_NEW_ANY               = 3;
2136    ZX_POL_NEW_VMO               = 4;
2137    ZX_POL_NEW_CHANNEL           = 5;
2138    ZX_POL_NEW_EVENT             = 6;
2139    ZX_POL_NEW_EVENTPAIR         = 7;
2140    ZX_POL_NEW_PORT              = 8;
2141    ZX_POL_NEW_SOCKET            = 9;
2142    ZX_POL_NEW_FIFO              = 10;
2143    ZX_POL_NEW_TIMER             = 11;
2144    ZX_POL_NEW_PROCESS           = 12;
2145    ZX_POL_NEW_PROFILE           = 13;
2146    ZX_POL_NEW_PAGER             = 14;
2147    ZX_POL_AMBIENT_MARK_VMO_EXEC = 15;
2148
2149    // policy actions
2150    ZX_POL_ACTION_ALLOW           = 0;
2151    ZX_POL_ACTION_DENY            = 1;
2152    ZX_POL_ACTION_ALLOW_EXCEPTION = 2;
2153    ZX_POL_ACTION_DENY_EXCEPTION  = 3;
2154    ZX_POL_ACTION_KILL            = 4;
2155
2156    // timer slack default modes
2157    ZX_TIMER_SLACK_CENTER = 0;
2158    ZX_TIMER_SLACK_EARLY  = 1;
2159    ZX_TIMER_SLACK_LATE   = 2;
2160]);
2161
2162multiconst!(u32, [
2163    // critical options
2164    ZX_JOB_CRITICAL_PROCESS_RETCODE_NONZERO = 1 << 0;
2165]);
2166
2167// Don't use struct_decl_macro, wrapper is different.
2168#[repr(C)]
2169#[derive(
2170    Default, Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable, IntoBytes,
2171)]
2172pub struct zx_info_vmo_t {
2173    pub koid: zx_koid_t,
2174    pub name: [u8; ZX_MAX_NAME_LEN],
2175    pub size_bytes: u64,
2176    pub parent_koid: zx_koid_t,
2177    pub num_children: usize,
2178    pub num_mappings: usize,
2179    pub share_count: usize,
2180    pub flags: u32,
2181    padding1: [PadByte; 4],
2182    pub committed_bytes: u64,
2183    pub handle_rights: zx_rights_t,
2184    pub cache_policy: u32,
2185    pub metadata_bytes: u64,
2186    pub committed_change_events: u64,
2187    pub populated_bytes: u64,
2188    pub committed_private_bytes: u64,
2189    pub populated_private_bytes: u64,
2190    pub committed_scaled_bytes: u64,
2191    pub populated_scaled_bytes: u64,
2192    pub committed_fractional_scaled_bytes: u64,
2193    pub populated_fractional_scaled_bytes: u64,
2194}
2195
2196struct_decl_macro! {
2197    #[repr(C)]
2198    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2199    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2200    pub struct <zx_info_cpu_stats_t> {
2201        pub cpu_number: u32,
2202        pub flags: u32,
2203        pub idle_time: zx_duration_t,
2204        pub normalized_busy_time: zx_duration_t,
2205        pub reschedules: u64,
2206        pub context_switches: u64,
2207        pub irq_preempts: u64,
2208        pub preempts: u64,
2209        pub yields: u64,
2210        pub ints: u64,
2211        pub timer_ints: u64,
2212        pub timers: u64,
2213        pub page_faults: u64,
2214        pub exceptions: u64,
2215        pub syscalls: u64,
2216        pub reschedule_ipis: u64,
2217        pub generic_ipis: u64,
2218        pub active_energy_consumption_nj: u64,
2219        pub idle_energy_consumption_nj: u64,
2220    }
2221}
2222
2223zx_info_cpu_stats_t!(zx_info_cpu_stats_t);
2224
2225struct_decl_macro! {
2226    #[repr(C)]
2227    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2228    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2229    pub struct <zx_info_kmem_stats_t> {
2230        pub total_bytes: u64,
2231        pub free_bytes: u64,
2232        pub free_loaned_bytes: u64,
2233        pub wired_bytes: u64,
2234        pub total_heap_bytes: u64,
2235        pub free_heap_bytes: u64,
2236        pub vmo_bytes: u64,
2237        pub mmu_overhead_bytes: u64,
2238        pub ipc_bytes: u64,
2239        pub cache_bytes: u64,
2240        pub slab_bytes: u64,
2241        pub zram_bytes: u64,
2242        pub other_bytes: u64,
2243        pub vmo_reclaim_total_bytes: u64,
2244        pub vmo_reclaim_newest_bytes: u64,
2245        pub vmo_reclaim_oldest_bytes: u64,
2246        pub vmo_reclaim_disabled_bytes: u64,
2247        pub vmo_discardable_locked_bytes: u64,
2248        pub vmo_discardable_unlocked_bytes: u64,
2249    }
2250}
2251
2252zx_info_kmem_stats_t!(zx_info_kmem_stats_t);
2253
2254struct_decl_macro! {
2255    #[repr(C)]
2256    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2257    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2258    pub struct <zx_info_kmem_stats_extended_t> {
2259        pub total_bytes: u64,
2260        pub free_bytes: u64,
2261        pub wired_bytes: u64,
2262        pub total_heap_bytes: u64,
2263        pub free_heap_bytes: u64,
2264        pub vmo_bytes: u64,
2265        pub vmo_pager_total_bytes: u64,
2266        pub vmo_pager_newest_bytes: u64,
2267        pub vmo_pager_oldest_bytes: u64,
2268        pub vmo_discardable_locked_bytes: u64,
2269        pub vmo_discardable_unlocked_bytes: u64,
2270        pub mmu_overhead_bytes: u64,
2271        pub ipc_bytes: u64,
2272        pub other_bytes: u64,
2273        pub vmo_reclaim_disable_bytes: u64,
2274    }
2275}
2276
2277zx_info_kmem_stats_extended_t!(zx_info_kmem_stats_extended_t);
2278
2279struct_decl_macro! {
2280    #[repr(C)]
2281    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2282    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2283    pub struct <zx_info_kmem_stats_compression_t> {
2284        pub uncompressed_storage_bytes: u64,
2285        pub compressed_storage_bytes: u64,
2286        pub compressed_fragmentation_bytes: u64,
2287        pub compression_time: zx_duration_t,
2288        pub decompression_time: zx_duration_t,
2289        pub total_page_compression_attempts: u64,
2290        pub failed_page_compression_attempts: u64,
2291        pub total_page_decompressions: u64,
2292        pub compressed_page_evictions: u64,
2293        pub eager_page_compressions: u64,
2294        pub memory_pressure_page_compressions: u64,
2295        pub critical_memory_page_compressions: u64,
2296        pub pages_decompressed_unit_ns: u64,
2297        pub pages_decompressed_within_log_time: [u64; 8],
2298    }
2299}
2300
2301zx_info_kmem_stats_compression_t!(zx_info_kmem_stats_compression_t);
2302
2303struct_decl_macro! {
2304    #[repr(C)]
2305    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2306    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2307    pub struct <zx_info_resource_t> {
2308        pub kind: u32,
2309        pub flags: u32,
2310        pub base: u64,
2311        pub size: usize,
2312        pub name: [u8; ZX_MAX_NAME_LEN],
2313    }
2314}
2315
2316struct_decl_macro! {
2317    #[repr(C)]
2318    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2319    #[derive(zerocopy::FromBytes, zerocopy::IntoBytes, zerocopy::Immutable)]
2320    pub struct <zx_info_bti_t> {
2321        pub minimum_contiguity: u64,
2322        pub aspace_size: u64,
2323        pub pmo_count: u64,
2324        pub quarantine_count: u64,
2325    }
2326}
2327
2328zx_info_bti_t!(zx_info_bti_t);
2329
2330pub type zx_thread_state_t = u32;
2331
2332multiconst!(zx_thread_state_t, [
2333    ZX_THREAD_STATE_NEW = 0x0000;
2334    ZX_THREAD_STATE_RUNNING = 0x0001;
2335    ZX_THREAD_STATE_SUSPENDED = 0x0002;
2336    ZX_THREAD_STATE_BLOCKED = 0x0003;
2337    ZX_THREAD_STATE_DYING = 0x0004;
2338    ZX_THREAD_STATE_DEAD = 0x0005;
2339    ZX_THREAD_STATE_BLOCKED_EXCEPTION = 0x0103;
2340    ZX_THREAD_STATE_BLOCKED_SLEEPING = 0x0203;
2341    ZX_THREAD_STATE_BLOCKED_FUTEX = 0x0303;
2342    ZX_THREAD_STATE_BLOCKED_PORT = 0x0403;
2343    ZX_THREAD_STATE_BLOCKED_CHANNEL = 0x0503;
2344    ZX_THREAD_STATE_BLOCKED_WAIT_ONE = 0x0603;
2345    ZX_THREAD_STATE_BLOCKED_WAIT_MANY = 0x0703;
2346    ZX_THREAD_STATE_BLOCKED_INTERRUPT = 0x0803;
2347    ZX_THREAD_STATE_BLOCKED_PAGER = 0x0903;
2348]);
2349
2350#[repr(C)]
2351#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, zerocopy::FromBytes, zerocopy::Immutable)]
2352pub struct zx_info_thread_t {
2353    pub state: zx_thread_state_t,
2354    pub wait_exception_channel_type: u32,
2355    pub cpu_affinity_mask: zx_cpu_set_t,
2356}
2357
2358struct_decl_macro! {
2359    #[repr(C)]
2360    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2361    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2362    pub struct <zx_info_thread_stats_t> {
2363        pub total_runtime: zx_duration_t,
2364        pub last_scheduled_cpu: u32,
2365    }
2366}
2367
2368zx_info_thread_stats_t!(zx_info_thread_stats_t);
2369
2370zx_info_resource_t!(zx_info_resource_t);
2371
2372struct_decl_macro! {
2373    #[repr(C)]
2374    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2375    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2376    pub struct <zx_info_vmar_t> {
2377        pub base: usize,
2378        pub len: usize,
2379    }
2380}
2381
2382zx_info_vmar_t!(zx_info_vmar_t);
2383
2384struct_decl_macro! {
2385    #[repr(C)]
2386    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2387    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2388    pub struct <zx_info_task_stats_t> {
2389        pub mem_mapped_bytes: usize,
2390        pub mem_private_bytes: usize,
2391        pub mem_shared_bytes: usize,
2392        pub mem_scaled_shared_bytes: usize,
2393        pub mem_fractional_scaled_shared_bytes: u64,
2394    }
2395}
2396
2397zx_info_task_stats_t!(zx_info_task_stats_t);
2398
2399struct_decl_macro! {
2400    #[repr(C)]
2401    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2402    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2403    pub struct <zx_info_task_runtime_t> {
2404        pub cpu_time: zx_duration_t,
2405        pub queue_time: zx_duration_t,
2406        pub page_fault_time: zx_duration_t,
2407        pub lock_contention_time: zx_duration_t,
2408    }
2409}
2410
2411zx_info_task_runtime_t!(zx_info_task_runtime_t);
2412
2413multiconst!(zx_info_maps_type_t, [
2414    ZX_INFO_MAPS_TYPE_NONE    = 0;
2415    ZX_INFO_MAPS_TYPE_ASPACE  = 1;
2416    ZX_INFO_MAPS_TYPE_VMAR    = 2;
2417    ZX_INFO_MAPS_TYPE_MAPPING = 3;
2418]);
2419
2420struct_decl_macro! {
2421    #[repr(C)]
2422    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2423    #[derive(zerocopy::FromBytes, zerocopy::Immutable, IntoBytes)]
2424    pub struct <zx_info_maps_mapping_t> {
2425        pub mmu_flags: zx_vm_option_t,
2426        padding1: [PadByte; 4],
2427        pub vmo_koid: zx_koid_t,
2428        pub vmo_offset: u64,
2429        pub committed_bytes: usize,
2430        pub populated_bytes: usize,
2431        pub committed_private_bytes: usize,
2432        pub populated_private_bytes: usize,
2433        pub committed_scaled_bytes: usize,
2434        pub populated_scaled_bytes: usize,
2435        pub committed_fractional_scaled_bytes: u64,
2436        pub populated_fractional_scaled_bytes: u64,
2437    }
2438}
2439
2440zx_info_maps_mapping_t!(zx_info_maps_mapping_t);
2441
2442#[repr(C)]
2443#[derive(Copy, Clone, KnownLayout, FromBytes, Immutable)]
2444pub union InfoMapsTypeUnion {
2445    pub mapping: zx_info_maps_mapping_t,
2446}
2447
2448struct_decl_macro! {
2449    #[repr(C)]
2450    #[derive(Copy, Clone)]
2451    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2452    pub struct <zx_info_maps_t> {
2453        pub name: [u8; ZX_MAX_NAME_LEN],
2454        pub base: zx_vaddr_t,
2455        pub size: usize,
2456        pub depth: usize,
2457        pub r#type: zx_info_maps_type_t,
2458        pub u: InfoMapsTypeUnion,
2459    }
2460}
2461
2462zx_info_maps_t!(zx_info_maps_t);
2463
2464struct_decl_macro! {
2465    #[repr(C)]
2466    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
2467    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2468    pub struct <zx_info_process_handle_stats_t> {
2469        pub handle_count: [u32; ZX_OBJ_TYPE_UPPER_BOUND],
2470    }
2471}
2472
2473impl Default for zx_info_process_handle_stats_t {
2474    fn default() -> Self {
2475        Self { handle_count: [0; ZX_OBJ_TYPE_UPPER_BOUND] }
2476    }
2477}
2478
2479zx_info_process_handle_stats_t!(zx_info_process_handle_stats_t);
2480
2481struct_decl_macro! {
2482    #[repr(C)]
2483    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2484    #[derive(zerocopy::FromBytes, zerocopy::Immutable, zerocopy::IntoBytes)]
2485    pub struct <zx_info_memory_stall_t> {
2486        pub stall_time_some: zx_duration_mono_t,
2487        pub stall_time_full: zx_duration_mono_t,
2488    }
2489}
2490
2491zx_info_memory_stall_t!(zx_info_memory_stall_t);
2492
2493// from //zircon/system/public/zircon/syscalls/hypervisor.h
2494multiconst!(zx_guest_trap_t, [
2495    ZX_GUEST_TRAP_BELL = 0;
2496    ZX_GUEST_TRAP_MEM  = 1;
2497    ZX_GUEST_TRAP_IO   = 2;
2498]);
2499
2500pub const ZX_LOG_RECORD_MAX: usize = 256;
2501pub const ZX_LOG_RECORD_DATA_MAX: usize = 216;
2502
2503pub const DEBUGLOG_TRACE: u8 = 0x10;
2504pub const DEBUGLOG_DEBUG: u8 = 0x20;
2505pub const DEBUGLOG_INFO: u8 = 0x30;
2506pub const DEBUGLOG_WARNING: u8 = 0x40;
2507pub const DEBUGLOG_ERROR: u8 = 0x50;
2508pub const DEBUGLOG_FATAL: u8 = 0x60;
2509
2510struct_decl_macro! {
2511    #[repr(C)]
2512    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
2513    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2514    pub struct <zx_log_record_t> {
2515        pub sequence: u64,
2516        padding1: [PadByte; 4],
2517        pub datalen: u16,
2518        pub severity: u8,
2519        pub flags: u8,
2520        pub timestamp: zx_instant_boot_t,
2521        pub pid: u64,
2522        pub tid: u64,
2523        pub data: [u8; ZX_LOG_RECORD_DATA_MAX],
2524    }
2525}
2526const_assert_eq!(std::mem::size_of::<zx_log_record_t>(), ZX_LOG_RECORD_MAX);
2527
2528zx_log_record_t!(zx_log_record_t);
2529
2530impl Default for zx_log_record_t {
2531    fn default() -> zx_log_record_t {
2532        zx_log_record_t {
2533            sequence: 0,
2534            padding1: Default::default(),
2535            datalen: 0,
2536            severity: 0,
2537            flags: 0,
2538            timestamp: 0,
2539            pid: 0,
2540            tid: 0,
2541            data: [0; ZX_LOG_RECORD_DATA_MAX],
2542        }
2543    }
2544}
2545
2546multiconst!(u32, [
2547    ZX_LOG_FLAG_READABLE = 0x40000000;
2548]);
2549
2550// For C, the below types are currently forward declared for syscalls.h.
2551// We might want to investigate a better solution for Rust or removing those
2552// forward declarations.
2553//
2554// These are hand typed translations from C types into Rust structures using a C
2555// layout
2556
2557// source: zircon/system/public/zircon/syscalls/system.h
2558#[repr(C)]
2559pub struct zx_system_powerctl_arg_t {
2560    // rust can't express anonymous unions at this time
2561    // https://github.com/rust-lang/rust/issues/49804
2562    pub powerctl_internal: zx_powerctl_union,
2563}
2564
2565#[repr(C)]
2566#[derive(Copy, Clone)]
2567pub union zx_powerctl_union {
2568    acpi_transition_s_state: acpi_transition_s_state,
2569    x86_power_limit: x86_power_limit,
2570}
2571
2572#[repr(C)]
2573#[derive(Default, Debug, PartialEq, Copy, Clone)]
2574pub struct acpi_transition_s_state {
2575    target_s_state: u8, // Value between 1 and 5 indicating which S-state
2576    sleep_type_a: u8,   // Value from ACPI VM (SLP_TYPa)
2577    sleep_type_b: u8,   // Value from ACPI VM (SLP_TYPb)
2578    padding1: [PadByte; 9],
2579}
2580
2581#[repr(C)]
2582#[derive(Default, Debug, PartialEq, Copy, Clone)]
2583pub struct x86_power_limit {
2584    power_limit: u32, // PL1 value in milliwatts
2585    time_window: u32, // PL1 time window in microseconds
2586    clamp: u8,        // PL1 clamping enable
2587    enable: u8,       // PL1 enable
2588    padding1: [PadByte; 2],
2589}
2590
2591// source: zircon/system/public/zircon/syscalls/pci.h
2592pub type zx_pci_bar_types_t = u32;
2593
2594multiconst!(zx_pci_bar_types_t, [
2595            ZX_PCI_BAR_TYPE_UNUSED = 0;
2596            ZX_PCI_BAR_TYPE_MMIO = 1;
2597            ZX_PCI_BAR_TYPE_PIO = 2;
2598]);
2599
2600#[repr(C)]
2601pub struct zx_pci_bar_t {
2602    pub id: u32,
2603    pub ty: u32,
2604    pub size: usize,
2605    // rust can't express anonymous unions at this time
2606    // https://github.com/rust-lang/rust/issues/49804
2607    pub zx_pci_bar_union: zx_pci_bar_union,
2608}
2609
2610#[repr(C)]
2611#[derive(Copy, Clone)]
2612pub union zx_pci_bar_union {
2613    addr: usize,
2614    zx_pci_bar_union_struct: zx_pci_bar_union_struct,
2615}
2616
2617#[repr(C)]
2618#[derive(Default, Debug, PartialEq, Copy, Clone)]
2619pub struct zx_pci_bar_union_struct {
2620    handle: zx_handle_t,
2621    padding1: [PadByte; 4],
2622}
2623
2624// source: zircon/system/public/zircon/syscalls/smc.h
2625#[repr(C)]
2626#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
2627pub struct zx_smc_parameters_t {
2628    pub func_id: u32,
2629    padding1: [PadByte; 4],
2630    pub arg1: u64,
2631    pub arg2: u64,
2632    pub arg3: u64,
2633    pub arg4: u64,
2634    pub arg5: u64,
2635    pub arg6: u64,
2636    pub client_id: u16,
2637    pub secure_os_id: u16,
2638    padding2: [PadByte; 4],
2639}
2640
2641#[repr(C)]
2642#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2643pub struct zx_smc_result_t {
2644    pub arg0: u64,
2645    pub arg1: u64,
2646    pub arg2: u64,
2647    pub arg3: u64,
2648    pub arg6: u64,
2649}
2650
2651pub const ZX_CPU_SET_MAX_CPUS: usize = 512;
2652pub const ZX_CPU_SET_BITS_PER_WORD: usize = 64;
2653
2654#[repr(C)]
2655#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, zerocopy::FromBytes, zerocopy::Immutable)]
2656pub struct zx_cpu_set_t {
2657    pub mask: [u64; ZX_CPU_SET_MAX_CPUS / ZX_CPU_SET_BITS_PER_WORD],
2658}
2659
2660// source: zircon/system/public/zircon/syscalls/scheduler.h
2661#[repr(C)]
2662#[derive(Copy, Clone)]
2663pub struct zx_profile_info_t {
2664    pub flags: u32,
2665    padding1: [PadByte; 4],
2666    pub zx_profile_info_union: zx_profile_info_union,
2667    pub cpu_affinity_mask: zx_cpu_set_t,
2668}
2669
2670#[cfg(feature = "zerocopy")]
2671impl Default for zx_profile_info_t {
2672    fn default() -> Self {
2673        Self {
2674            flags: Default::default(),
2675            padding1: Default::default(),
2676            zx_profile_info_union: FromZeros::new_zeroed(),
2677            cpu_affinity_mask: Default::default(),
2678        }
2679    }
2680}
2681
2682#[repr(C)]
2683#[derive(Copy, Clone)]
2684#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
2685pub struct priority_params {
2686    pub priority: i32,
2687    padding1: [PadByte; 20],
2688}
2689
2690#[repr(C)]
2691#[derive(Copy, Clone)]
2692#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
2693pub union zx_profile_info_union {
2694    pub priority_params: priority_params,
2695    pub deadline_params: zx_sched_deadline_params_t,
2696}
2697
2698#[repr(C)]
2699#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2700#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, KnownLayout))]
2701pub struct zx_sched_deadline_params_t {
2702    pub capacity: zx_duration_t,
2703    pub relative_deadline: zx_duration_t,
2704    pub period: zx_duration_t,
2705}
2706
2707#[repr(C)]
2708#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2709pub struct zx_cpu_performance_scale_t {
2710    pub integer_part: u32,
2711    pub fractional_part: u32,
2712}
2713
2714#[repr(C)]
2715#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2716pub struct zx_cpu_performance_info_t {
2717    pub logical_cpu_number: u32,
2718    pub performance_scale: zx_cpu_performance_scale_t,
2719}
2720
2721#[repr(C)]
2722#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2723pub struct zx_cpu_perf_limit_t {
2724    pub logical_cpu_number: u32,
2725    pub limit_type: u32,
2726    pub min: u64,
2727    pub max: u64,
2728}
2729
2730#[repr(C)]
2731#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2732pub struct zx_iommu_desc_stub_t {
2733    padding1: PadByte,
2734}
2735
2736multiconst!(u32, [
2737    ZX_IOMMU_TYPE_STUB = 0;
2738    ZX_IOMMU_TYPE_INTEL = 1;
2739]);
2740
2741#[repr(C)]
2742#[derive(Debug, Copy, Clone)]
2743pub struct zx_sampler_config_t {
2744    pub period: zx_duration_t,
2745    pub buffer_size: usize,
2746    pub iobuffer_discipline: u64,
2747}
2748
2749multiconst!(zx_processor_power_level_options_t, [
2750    ZX_PROCESSOR_POWER_LEVEL_OPTIONS_DOMAIN_INDEPENDENT = 1 << 0;
2751]);
2752
2753multiconst!(zx_processor_power_control_t, [
2754    ZX_PROCESSOR_POWER_CONTROL_CPU_DRIVER = 0;
2755    ZX_PROCESSOR_POWER_CONTROL_ARM_PSCI = 1;
2756    ZX_PROCESSOR_POWER_CONTROL_ARM_WFI = 2;
2757    ZX_PROCESSOR_POWER_CONTROL_RISCV_SBI = 3;
2758    ZX_PROCESSOR_POWER_CONTROL_RISCV_WFI = 4;
2759]);
2760
2761#[repr(C)]
2762#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2763pub struct zx_processor_power_level_t {
2764    pub options: zx_processor_power_level_options_t,
2765    pub processing_rate: u64,
2766    pub power_coefficient_nw: u64,
2767    pub control_interface: zx_processor_power_control_t,
2768    pub control_argument: u64,
2769    pub diagnostic_name: [u8; ZX_MAX_NAME_LEN],
2770    padding1: [PadByte; 32],
2771}
2772
2773#[repr(C)]
2774#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2775pub struct zx_processor_power_level_transition_t {
2776    pub latency: zx_duration_t,
2777    pub energy: u64,
2778    pub from: u8,
2779    pub to: u8,
2780    padding1: [PadByte; 6],
2781}
2782
2783#[repr(C)]
2784#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
2785pub struct zx_packet_processor_power_level_transition_request_t {
2786    pub domain_id: u32,
2787    pub options: u32,
2788    pub control_interface: u64,
2789    pub control_argument: u64,
2790    padding1: [PadByte; 8],
2791}
2792
2793#[repr(C)]
2794#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2795pub struct zx_processor_power_state_t {
2796    pub domain_id: u32,
2797    pub options: u32,
2798    pub control_interface: u64,
2799    pub control_argument: u64,
2800}
2801
2802#[repr(C)]
2803#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
2804pub struct zx_processor_power_domain_t {
2805    pub cpus: zx_cpu_set_t,
2806    pub domain_id: u32,
2807    padding1: [PadByte; 4],
2808}
2809
2810#[repr(C)]
2811#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2812pub struct zx_power_domain_info_t {
2813    pub cpus: zx_cpu_set_t,
2814    pub domain_id: u32,
2815    pub idle_power_levels: u8,
2816    pub active_power_levels: u8,
2817    padding1: [PadByte; 2],
2818}
2819
2820multiconst!(u32, [
2821    ZX_BTI_PERM_READ = 1 << 0;
2822    ZX_BTI_PERM_WRITE = 1 << 1;
2823    ZX_BTI_PERM_EXECUTE = 1 << 2;
2824    ZX_BTI_COMPRESS = 1 << 3;
2825    ZX_BTI_CONTIGUOUS = 1 << 4;
2826]);
2827
2828// Options for zx_port_create
2829multiconst!(u32, [
2830    ZX_PORT_BIND_TO_INTERRUPT = 1 << 0;
2831]);
2832
2833// Options for zx_interrupt_create
2834multiconst!(u32, [
2835    ZX_INTERRUPT_VIRTUAL = 0x10;
2836    ZX_INTERRUPT_TIMESTAMP_MONO = 1 << 6;
2837]);
2838
2839// Options for zx_interrupt_bind
2840multiconst!(u32, [
2841    ZX_INTERRUPT_BIND = 0;
2842    ZX_INTERRUPT_UNBIND = 1;
2843]);
2844
2845#[repr(C)]
2846pub struct zx_iob_region_t {
2847    pub r#type: zx_iob_region_type_t,
2848    pub access: zx_iob_access_t,
2849    pub size: u64,
2850    pub discipline: zx_iob_discipline_t,
2851    pub extension: zx_iob_region_extension_t,
2852}
2853
2854multiconst!(zx_iob_region_type_t, [
2855    ZX_IOB_REGION_TYPE_PRIVATE = 0;
2856    ZX_IOB_REGION_TYPE_SHARED = 1;
2857]);
2858
2859multiconst!(zx_iob_access_t, [
2860    ZX_IOB_ACCESS_EP0_CAN_MAP_READ = 1 << 0;
2861    ZX_IOB_ACCESS_EP0_CAN_MAP_WRITE = 1 << 1;
2862    ZX_IOB_ACCESS_EP0_CAN_MEDIATED_READ = 1 << 2;
2863    ZX_IOB_ACCESS_EP0_CAN_MEDIATED_WRITE = 1 << 3;
2864    ZX_IOB_ACCESS_EP1_CAN_MAP_READ = 1 << 4;
2865    ZX_IOB_ACCESS_EP1_CAN_MAP_WRITE = 1 << 5;
2866    ZX_IOB_ACCESS_EP1_CAN_MEDIATED_READ = 1 << 6;
2867    ZX_IOB_ACCESS_EP1_CAN_MEDIATED_WRITE = 1 << 7;
2868]);
2869
2870#[repr(C)]
2871#[derive(Copy, Clone)]
2872pub struct zx_iob_discipline_t {
2873    pub r#type: zx_iob_discipline_type_t,
2874    pub extension: zx_iob_discipline_extension_t,
2875}
2876
2877#[repr(C)]
2878#[derive(Copy, Clone)]
2879pub union zx_iob_discipline_extension_t {
2880    // This is in vdso-next.
2881    pub ring_buffer: zx_iob_discipline_mediated_write_ring_buffer_t,
2882    pub reserved: [PadByte; 64],
2883}
2884
2885#[repr(C)]
2886#[derive(Debug, Copy, Clone)]
2887pub struct zx_iob_discipline_mediated_write_ring_buffer_t {
2888    pub tag: u64,
2889    pub padding: [PadByte; 56],
2890}
2891
2892multiconst!(zx_iob_discipline_type_t, [
2893    ZX_IOB_DISCIPLINE_TYPE_NONE = 0;
2894    ZX_IOB_DISCIPLINE_TYPE_MEDIATED_WRITE_RING_BUFFER = 2;
2895]);
2896
2897#[repr(C)]
2898#[derive(Clone, Copy, Default)]
2899pub struct zx_iob_region_private_t {
2900    options: u32,
2901    padding: [PadByte; 28],
2902}
2903
2904#[repr(C)]
2905#[derive(Clone, Copy)]
2906pub struct zx_iob_region_shared_t {
2907    pub options: u32,
2908    pub shared_region: zx_handle_t,
2909    pub padding: [PadByte; 24],
2910}
2911
2912#[repr(C)]
2913pub union zx_iob_region_extension_t {
2914    pub private_region: zx_iob_region_private_t,
2915    pub shared_region: zx_iob_region_shared_t,
2916    pub max_extension: [u8; 32],
2917}
2918
2919#[repr(C)]
2920pub struct zx_wake_source_report_entry_t {
2921    pub koid: zx_koid_t,
2922    pub name: [u8; ZX_MAX_NAME_LEN],
2923    pub initial_signal_time: zx_instant_boot_t,
2924    pub last_signal_time: zx_instant_boot_t,
2925    pub last_ack_time: zx_instant_boot_t,
2926    pub signal_count: u32,
2927    pub flags: u32,
2928}
2929
2930#[repr(C)]
2931pub struct zx_wake_source_report_header_t {
2932    pub report_time: zx_instant_boot_t,
2933    pub suspend_start_time: zx_instant_boot_t,
2934    pub total_wake_sources: u32,
2935    pub unreported_wake_report_entries: u32,
2936}
2937
2938#[cfg(test)]
2939mod test {
2940    use super::*;
2941
2942    #[test]
2943    fn padded_struct_equality() {
2944        let test_struct = zx_clock_update_args_v1_t {
2945            rate_adjust: 222,
2946            padding1: Default::default(),
2947            value: 333,
2948            error_bound: 444,
2949        };
2950
2951        let different_data = zx_clock_update_args_v1_t { rate_adjust: 999, ..test_struct.clone() };
2952
2953        let different_padding = zx_clock_update_args_v1_t {
2954            padding1: [PadByte(0), PadByte(1), PadByte(2), PadByte(3)],
2955            ..test_struct.clone()
2956        };
2957
2958        // Structures with different data should not be equal.
2959        assert_ne!(test_struct, different_data);
2960        // Structures with only different padding should not be equal.
2961        assert_eq!(test_struct, different_padding);
2962    }
2963
2964    #[test]
2965    fn padded_struct_debug() {
2966        let test_struct = zx_clock_update_args_v1_t {
2967            rate_adjust: 222,
2968            padding1: Default::default(),
2969            value: 333,
2970            error_bound: 444,
2971        };
2972        let expectation = "zx_clock_update_args_v1_t { \
2973            rate_adjust: 222, \
2974            padding1: [-, -, -, -], \
2975            value: 333, \
2976            error_bound: 444 }";
2977        assert_eq!(format!("{:?}", test_struct), expectation);
2978    }
2979}
2980
2981#[repr(C, align(32))]
2982#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2983#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes, KnownLayout))]
2984pub struct zx_rseq_t {
2985    pub cpu_id: u32,
2986    pub reserved: u32,
2987    pub start_ip: u64,
2988    pub post_commit_offset: u64,
2989    pub abort_ip: u64,
2990}
2991
2992pub const ZX_INFO_INVALID_CPU: u32 = 0xFFFFFFFF;