Skip to main content

zx_types/
lib.rs

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