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