zx_types/
lib.rs

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