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
1026pub type zx_pci_irq_swizzle_lut_t = [[[u32; 4]; 8]; 32];
1027
1028#[repr(C)]
1029#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1030pub struct zx_pci_init_arg_t {
1031    pub dev_pin_to_global_irq: zx_pci_irq_swizzle_lut_t,
1032    pub num_irqs: u32,
1033    pub irqs: [zx_irq_t; 32],
1034    pub ecam_window_count: u32,
1035    // Note: the ecam_windows field is actually a variable size array.
1036    // We use a fixed size array to match the C repr.
1037    pub ecam_windows: [zx_ecam_window_t; 1],
1038}
1039
1040#[repr(C)]
1041#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1042pub struct zx_irq_t {
1043    pub global_irq: u32,
1044    pub level_triggered: bool,
1045    pub active_high: bool,
1046}
1047
1048#[repr(C)]
1049#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1050pub struct zx_ecam_window_t {
1051    pub base: u64,
1052    pub size: usize,
1053    pub bus_start: u8,
1054    pub bus_end: u8,
1055}
1056
1057#[repr(C)]
1058#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1059pub struct zx_pcie_device_info_t {
1060    pub vendor_id: u16,
1061    pub device_id: u16,
1062    pub base_class: u8,
1063    pub sub_class: u8,
1064    pub program_interface: u8,
1065    pub revision_id: u8,
1066    pub bus_id: u8,
1067    pub dev_id: u8,
1068    pub func_id: u8,
1069}
1070
1071#[repr(C)]
1072#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1073pub struct zx_pci_resource_t {
1074    pub type_: u32,
1075    pub size: usize,
1076    // TODO: Actually a union
1077    pub pio_addr: usize,
1078}
1079
1080// TODO: Actually a union
1081pub type zx_rrec_t = [u8; 64];
1082
1083// Ports V2
1084#[repr(u32)]
1085#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1086pub enum zx_packet_type_t {
1087    ZX_PKT_TYPE_USER = 0,
1088    ZX_PKT_TYPE_SIGNAL_ONE = 1,
1089    ZX_PKT_TYPE_GUEST_BELL = 3,
1090    ZX_PKT_TYPE_GUEST_MEM = 4,
1091    ZX_PKT_TYPE_GUEST_IO = 5,
1092    ZX_PKT_TYPE_GUEST_VCPU = 6,
1093    ZX_PKT_TYPE_INTERRUPT = 7,
1094    ZX_PKT_TYPE_PAGE_REQUEST = 9,
1095    ZX_PKT_TYPE_PROCESSOR_POWER_LEVEL_TRANSITION_REQUEST = 10,
1096    #[doc(hidden)]
1097    __Nonexhaustive,
1098}
1099
1100impl Default for zx_packet_type_t {
1101    fn default() -> Self {
1102        zx_packet_type_t::ZX_PKT_TYPE_USER
1103    }
1104}
1105
1106#[repr(u32)]
1107#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
1108pub enum zx_packet_guest_vcpu_type_t {
1109    #[default]
1110    ZX_PKT_GUEST_VCPU_INTERRUPT = 0,
1111    ZX_PKT_GUEST_VCPU_STARTUP = 1,
1112    #[doc(hidden)]
1113    __Nonexhaustive,
1114}
1115
1116#[repr(C)]
1117#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1118pub struct zx_packet_signal_t {
1119    pub trigger: zx_signals_t,
1120    pub observed: zx_signals_t,
1121    pub count: u64,
1122    pub timestamp: zx_time_t,
1123}
1124
1125pub const ZX_WAIT_ASYNC_TIMESTAMP: u32 = 1;
1126pub const ZX_WAIT_ASYNC_EDGE: u32 = 2;
1127pub const ZX_WAIT_ASYNC_BOOT_TIMESTAMP: u32 = 4;
1128
1129// Actually a union of different integer types, but this should be good enough.
1130pub type zx_packet_user_t = [u8; 32];
1131
1132#[repr(C)]
1133#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1134pub struct zx_port_packet_t {
1135    pub key: u64,
1136    pub packet_type: zx_packet_type_t,
1137    pub status: i32,
1138    pub union: [u8; 32],
1139}
1140
1141#[repr(C)]
1142#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1143pub struct zx_packet_guest_bell_t {
1144    pub addr: zx_gpaddr_t,
1145}
1146
1147#[repr(C)]
1148#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1149pub struct zx_packet_guest_io_t {
1150    pub port: u16,
1151    pub access_size: u8,
1152    pub input: bool,
1153    pub data: [u8; 4],
1154}
1155
1156#[repr(C)]
1157#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1158#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
1159pub struct zx_packet_guest_vcpu_interrupt_t {
1160    pub mask: u64,
1161    pub vector: u8,
1162    padding1: [PadByte; 7],
1163}
1164
1165#[repr(C)]
1166#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1167#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
1168pub struct zx_packet_guest_vcpu_startup_t {
1169    pub id: u64,
1170    pub entry: zx_gpaddr_t,
1171}
1172
1173#[repr(C)]
1174#[derive(Copy, Clone)]
1175#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
1176pub union zx_packet_guest_vcpu_union_t {
1177    pub interrupt: zx_packet_guest_vcpu_interrupt_t,
1178    pub startup: zx_packet_guest_vcpu_startup_t,
1179}
1180
1181#[cfg(feature = "zerocopy")]
1182impl Default for zx_packet_guest_vcpu_union_t {
1183    fn default() -> Self {
1184        Self::new_zeroed()
1185    }
1186}
1187
1188#[repr(C)]
1189#[derive(Copy, Clone, Default)]
1190pub struct zx_packet_guest_vcpu_t {
1191    pub r#type: zx_packet_guest_vcpu_type_t,
1192    padding1: [PadByte; 4],
1193    pub union: zx_packet_guest_vcpu_union_t,
1194    padding2: [PadByte; 8],
1195}
1196
1197impl PartialEq for zx_packet_guest_vcpu_t {
1198    fn eq(&self, other: &Self) -> bool {
1199        if self.r#type != other.r#type {
1200            return false;
1201        }
1202        match self.r#type {
1203            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_INTERRUPT => unsafe {
1204                self.union.interrupt == other.union.interrupt
1205            },
1206            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_STARTUP => unsafe {
1207                self.union.startup == other.union.startup
1208            },
1209            // No equality relationship is defined for invalid types.
1210            _ => false,
1211        }
1212    }
1213}
1214
1215impl Eq for zx_packet_guest_vcpu_t {}
1216
1217impl Debug for zx_packet_guest_vcpu_t {
1218    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1219        match self.r#type {
1220            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_INTERRUPT => {
1221                write!(f, "type: {:?} union: {:?}", self.r#type, unsafe { self.union.interrupt })
1222            }
1223            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_STARTUP => {
1224                write!(f, "type: {:?} union: {:?}", self.r#type, unsafe { self.union.startup })
1225            }
1226            _ => panic!("unexpected VCPU packet type"),
1227        }
1228    }
1229}
1230
1231#[repr(C)]
1232#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
1233pub struct zx_packet_page_request_t {
1234    pub command: zx_page_request_command_t,
1235    pub flags: u16,
1236    padding1: [PadByte; 4],
1237    pub offset: u64,
1238    pub length: u64,
1239    padding2: [PadByte; 8],
1240}
1241
1242#[repr(u16)]
1243#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
1244pub enum zx_page_request_command_t {
1245    #[default]
1246    ZX_PAGER_VMO_READ = 0x0000,
1247    ZX_PAGER_VMO_COMPLETE = 0x0001,
1248    ZX_PAGER_VMO_DIRTY = 0x0002,
1249    #[doc(hidden)]
1250    __Nonexhaustive,
1251}
1252
1253multiconst!(u32, [
1254    ZX_PAGER_OP_FAIL = 1;
1255    ZX_PAGER_OP_DIRTY = 2;
1256    ZX_PAGER_OP_WRITEBACK_BEGIN = 3;
1257    ZX_PAGER_OP_WRITEBACK_END = 4;
1258]);
1259
1260pub type zx_excp_type_t = u32;
1261
1262multiconst!(zx_excp_type_t, [
1263    ZX_EXCP_GENERAL               = 0x008;
1264    ZX_EXCP_FATAL_PAGE_FAULT      = 0x108;
1265    ZX_EXCP_UNDEFINED_INSTRUCTION = 0x208;
1266    ZX_EXCP_SW_BREAKPOINT         = 0x308;
1267    ZX_EXCP_HW_BREAKPOINT         = 0x408;
1268    ZX_EXCP_UNALIGNED_ACCESS      = 0x508;
1269
1270    ZX_EXCP_SYNTH                 = 0x8000;
1271
1272    ZX_EXCP_THREAD_STARTING       = 0x008 | ZX_EXCP_SYNTH;
1273    ZX_EXCP_THREAD_EXITING        = 0x108 | ZX_EXCP_SYNTH;
1274    ZX_EXCP_POLICY_ERROR          = 0x208 | ZX_EXCP_SYNTH;
1275    ZX_EXCP_PROCESS_STARTING      = 0x308 | ZX_EXCP_SYNTH;
1276    ZX_EXCP_USER                  = 0x309 | ZX_EXCP_SYNTH;
1277]);
1278
1279multiconst!(u32, [
1280    ZX_EXCP_USER_CODE_PROCESS_NAME_CHANGED = 0x0001;
1281
1282    ZX_EXCP_USER_CODE_USER0                = 0xF000;
1283    ZX_EXCP_USER_CODE_USER1                = 0xF001;
1284    ZX_EXCP_USER_CODE_USER2                = 0xF002;
1285]);
1286
1287#[repr(C)]
1288#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1289#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes))]
1290pub struct zx_exception_info_t {
1291    pub pid: zx_koid_t,
1292    pub tid: zx_koid_t,
1293    pub type_: zx_excp_type_t,
1294    padding1: [PadByte; 4],
1295}
1296
1297#[repr(C)]
1298#[derive(Default, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
1299pub struct zx_x86_64_exc_data_t {
1300    pub vector: u64,
1301    pub err_code: u64,
1302    pub cr2: u64,
1303}
1304
1305impl Debug for zx_x86_64_exc_data_t {
1306    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1307        write!(f, "vector 0x{:x} err_code {} cr2 0x{:x}", self.vector, self.err_code, self.cr2)
1308    }
1309}
1310
1311#[repr(C)]
1312#[derive(Default, Copy, Clone, Eq, PartialEq, FromBytes, Immutable)]
1313pub struct zx_arm64_exc_data_t {
1314    pub esr: u32,
1315    padding1: [PadByte; 4],
1316    pub far: u64,
1317    padding2: [PadByte; 8],
1318}
1319
1320impl Debug for zx_arm64_exc_data_t {
1321    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1322        write!(f, "esr 0x{:x} far 0x{:x}", self.esr, self.far)
1323    }
1324}
1325
1326#[repr(C)]
1327#[derive(Default, Copy, Clone, Eq, PartialEq, FromBytes, Immutable)]
1328pub struct zx_riscv64_exc_data_t {
1329    pub cause: u64,
1330    pub tval: u64,
1331    padding1: [PadByte; 8],
1332}
1333
1334impl Debug for zx_riscv64_exc_data_t {
1335    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1336        write!(f, "cause {} tval {}", self.cause, self.tval)
1337    }
1338}
1339
1340#[repr(C)]
1341#[derive(Copy, Clone, KnownLayout, FromBytes, Immutable)]
1342pub union zx_exception_header_arch_t {
1343    pub x86_64: zx_x86_64_exc_data_t,
1344    pub arm_64: zx_arm64_exc_data_t,
1345    pub riscv_64: zx_riscv64_exc_data_t,
1346}
1347
1348impl Debug for zx_exception_header_arch_t {
1349    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1350        write!(f, "zx_exception_header_arch_t ")?;
1351        #[cfg(target_arch = "x86_64")]
1352        {
1353            // SAFETY: Exception reports are presumed to be from the target architecture.
1354            // Even if it was not, it's sound to treat the union as another variant as
1355            // the size and alignment are the same, there is no internal padding, and
1356            // the variants have no validity invariants.
1357            let x86_64 = unsafe { self.x86_64 };
1358            write!(f, "{x86_64:?}")
1359        }
1360        #[cfg(target_arch = "aarch64")]
1361        {
1362            // SAFETY: Exception reports are presumed to be from the target architecture.
1363            // Even if it was not, it's sound to treat the union as another variant as
1364            // the size and alignment are the same, there is no internal padding, and
1365            // the variants have no validity invariants.
1366            let arm_64 = unsafe { self.arm_64 };
1367            write!(f, "{arm_64:?}")
1368        }
1369        #[cfg(target_arch = "riscv64")]
1370        {
1371            // SAFETY: Exception reports are presumed to be from the target architecture.
1372            // Even if it was not, it's sound to treat the union as another variant as
1373            // the size and alignment are the same, there is no internal padding, and
1374            // the variants have no validity invariants.
1375            let riscv_64 = unsafe { self.riscv_64 };
1376            write!(f, "{riscv_64:?}")
1377        }
1378    }
1379}
1380
1381#[repr(C)]
1382#[derive(Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
1383pub struct zx_exception_header_t {
1384    pub size: u32,
1385    pub type_: zx_excp_type_t,
1386}
1387
1388pub type zx_excp_policy_code_t = u32;
1389
1390multiconst!(zx_excp_policy_code_t, [
1391    ZX_EXCP_POLICY_CODE_BAD_HANDLE              = 0;
1392    ZX_EXCP_POLICY_CODE_WRONG_OBJECT            = 1;
1393    ZX_EXCP_POLICY_CODE_VMAR_WX                 = 2;
1394    ZX_EXCP_POLICY_CODE_NEW_ANY                 = 3;
1395    ZX_EXCP_POLICY_CODE_NEW_VMO                 = 4;
1396    ZX_EXCP_POLICY_CODE_NEW_CHANNEL             = 5;
1397    ZX_EXCP_POLICY_CODE_NEW_EVENT               = 6;
1398    ZX_EXCP_POLICY_CODE_NEW_EVENTPAIR           = 7;
1399    ZX_EXCP_POLICY_CODE_NEW_PORT                = 8;
1400    ZX_EXCP_POLICY_CODE_NEW_SOCKET              = 9;
1401    ZX_EXCP_POLICY_CODE_NEW_FIFO                = 10;
1402    ZX_EXCP_POLICY_CODE_NEW_TIMER               = 11;
1403    ZX_EXCP_POLICY_CODE_NEW_PROCESS             = 12;
1404    ZX_EXCP_POLICY_CODE_NEW_PROFILE             = 13;
1405    ZX_EXCP_POLICY_CODE_NEW_PAGER               = 14;
1406    ZX_EXCP_POLICY_CODE_AMBIENT_MARK_VMO_EXEC   = 15;
1407    ZX_EXCP_POLICY_CODE_CHANNEL_FULL_WRITE      = 16;
1408    ZX_EXCP_POLICY_CODE_PORT_TOO_MANY_PACKETS   = 17;
1409    ZX_EXCP_POLICY_CODE_BAD_SYSCALL             = 18;
1410    ZX_EXCP_POLICY_CODE_PORT_TOO_MANY_OBSERVERS = 19;
1411    ZX_EXCP_POLICY_CODE_HANDLE_LEAK             = 20;
1412    ZX_EXCP_POLICY_CODE_NEW_IOB                 = 21;
1413]);
1414
1415#[repr(C)]
1416#[derive(Debug, Copy, Clone, KnownLayout, FromBytes, Immutable)]
1417pub struct zx_exception_context_t {
1418    pub arch: zx_exception_header_arch_t,
1419    pub synth_code: zx_excp_policy_code_t,
1420    pub synth_data: u32,
1421}
1422
1423#[repr(C)]
1424#[derive(Debug, Copy, Clone, KnownLayout, FromBytes, Immutable)]
1425pub struct zx_exception_report_t {
1426    pub header: zx_exception_header_t,
1427    pub context: zx_exception_context_t,
1428}
1429
1430pub type zx_exception_state_t = u32;
1431
1432multiconst!(zx_exception_state_t, [
1433    ZX_EXCEPTION_STATE_TRY_NEXT    = 0;
1434    ZX_EXCEPTION_STATE_HANDLED     = 1;
1435    ZX_EXCEPTION_STATE_THREAD_EXIT = 2;
1436]);
1437
1438pub type zx_exception_strategy_t = u32;
1439
1440multiconst!(zx_exception_state_t, [
1441    ZX_EXCEPTION_STRATEGY_FIRST_CHANCE   = 0;
1442    ZX_EXCEPTION_STRATEGY_SECOND_CHANCE  = 1;
1443]);
1444
1445#[cfg(target_arch = "x86_64")]
1446#[repr(C)]
1447#[derive(Default, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
1448pub struct zx_thread_state_general_regs_t {
1449    pub rax: u64,
1450    pub rbx: u64,
1451    pub rcx: u64,
1452    pub rdx: u64,
1453    pub rsi: u64,
1454    pub rdi: u64,
1455    pub rbp: u64,
1456    pub rsp: u64,
1457    pub r8: u64,
1458    pub r9: u64,
1459    pub r10: u64,
1460    pub r11: u64,
1461    pub r12: u64,
1462    pub r13: u64,
1463    pub r14: u64,
1464    pub r15: u64,
1465    pub rip: u64,
1466    pub rflags: u64,
1467    pub fs_base: u64,
1468    pub gs_base: u64,
1469}
1470
1471#[cfg(target_arch = "x86_64")]
1472impl core::fmt::Debug for zx_thread_state_general_regs_t {
1473    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1474        f.debug_struct(core::any::type_name::<Self>())
1475            .field("rax", &format_args!("{:#x}", self.rax))
1476            .field("rbx", &format_args!("{:#x}", self.rbx))
1477            .field("rcx", &format_args!("{:#x}", self.rcx))
1478            .field("rdx", &format_args!("{:#x}", self.rdx))
1479            .field("rsi", &format_args!("{:#x}", self.rsi))
1480            .field("rdi", &format_args!("{:#x}", self.rdi))
1481            .field("rbp", &format_args!("{:#x}", self.rbp))
1482            .field("rsp", &format_args!("{:#x}", self.rsp))
1483            .field("r8", &format_args!("{:#x}", self.r8))
1484            .field("r9", &format_args!("{:#x}", self.r9))
1485            .field("r10", &format_args!("{:#x}", self.r10))
1486            .field("r11", &format_args!("{:#x}", self.r11))
1487            .field("r12", &format_args!("{:#x}", self.r12))
1488            .field("r13", &format_args!("{:#x}", self.r13))
1489            .field("r14", &format_args!("{:#x}", self.r14))
1490            .field("r15", &format_args!("{:#x}", self.r15))
1491            .field("rip", &format_args!("{:#x}", self.rip))
1492            .field("rflags", &format_args!("{:#x}", self.rflags))
1493            .field("fs_base", &format_args!("{:#x}", self.fs_base))
1494            .field("gs_base", &format_args!("{:#x}", self.gs_base))
1495            .finish()
1496    }
1497}
1498
1499#[cfg(target_arch = "x86_64")]
1500impl From<&zx_restricted_state_t> for zx_thread_state_general_regs_t {
1501    fn from(state: &zx_restricted_state_t) -> Self {
1502        Self {
1503            rdi: state.rdi,
1504            rsi: state.rsi,
1505            rbp: state.rbp,
1506            rbx: state.rbx,
1507            rdx: state.rdx,
1508            rcx: state.rcx,
1509            rax: state.rax,
1510            rsp: state.rsp,
1511            r8: state.r8,
1512            r9: state.r9,
1513            r10: state.r10,
1514            r11: state.r11,
1515            r12: state.r12,
1516            r13: state.r13,
1517            r14: state.r14,
1518            r15: state.r15,
1519            rip: state.ip,
1520            rflags: state.flags,
1521            fs_base: state.fs_base,
1522            gs_base: state.gs_base,
1523        }
1524    }
1525}
1526
1527#[cfg(target_arch = "aarch64")]
1528multiconst!(u64, [
1529    ZX_REG_CPSR_ARCH_32_MASK = 0x10;
1530    ZX_REG_CPSR_THUMB_MASK = 0x20;
1531]);
1532
1533#[cfg(target_arch = "aarch64")]
1534#[repr(C)]
1535#[derive(Default, Copy, Clone, Eq, PartialEq)]
1536pub struct zx_thread_state_general_regs_t {
1537    pub r: [u64; 30],
1538    pub lr: u64,
1539    pub sp: u64,
1540    pub pc: u64,
1541    pub cpsr: u64,
1542    pub tpidr: u64,
1543}
1544
1545#[cfg(target_arch = "aarch64")]
1546impl core::fmt::Debug for zx_thread_state_general_regs_t {
1547    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1548        struct RegisterAsHex(u64);
1549        impl core::fmt::Debug for RegisterAsHex {
1550            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1551                write!(f, "{:#x}", self.0)
1552            }
1553        }
1554
1555        f.debug_struct(core::any::type_name::<Self>())
1556            .field("r", &self.r.map(RegisterAsHex))
1557            .field("lr", &format_args!("{:#x}", self.lr))
1558            .field("sp", &format_args!("{:#x}", self.sp))
1559            .field("pc", &format_args!("{:#x}", self.pc))
1560            .field("cpsr", &format_args!("{:#x}", self.cpsr))
1561            .field("tpidr", &format_args!("{:#x}", self.tpidr))
1562            .finish()
1563    }
1564}
1565
1566#[cfg(target_arch = "aarch64")]
1567impl From<&zx_restricted_state_t> for zx_thread_state_general_regs_t {
1568    fn from(state: &zx_restricted_state_t) -> Self {
1569        if state.cpsr as u64 & ZX_REG_CPSR_ARCH_32_MASK == ZX_REG_CPSR_ARCH_32_MASK {
1570            // aarch32
1571            Self {
1572                r: [
1573                    state.r[0],
1574                    state.r[1],
1575                    state.r[2],
1576                    state.r[3],
1577                    state.r[4],
1578                    state.r[5],
1579                    state.r[6],
1580                    state.r[7],
1581                    state.r[8],
1582                    state.r[9],
1583                    state.r[10],
1584                    state.r[11],
1585                    state.r[12],
1586                    state.r[13],
1587                    state.r[14],
1588                    state.pc, // ELR overwrites this.
1589                    state.r[16],
1590                    state.r[17],
1591                    state.r[18],
1592                    state.r[19],
1593                    state.r[20],
1594                    state.r[21],
1595                    state.r[22],
1596                    state.r[23],
1597                    state.r[24],
1598                    state.r[25],
1599                    state.r[26],
1600                    state.r[27],
1601                    state.r[28],
1602                    state.r[29],
1603                ],
1604                lr: state.r[14], // R[14] for aarch32
1605                sp: state.r[13], // R[13] for aarch32
1606                // TODO(https://fxbug.dev/379669623) Should it be checked for thumb and make
1607                // sure it isn't over incrementing?
1608                pc: state.pc, // Zircon populated this from elr.
1609                cpsr: state.cpsr as u64,
1610                tpidr: state.tpidr_el0,
1611            }
1612        } else {
1613            Self {
1614                r: [
1615                    state.r[0],
1616                    state.r[1],
1617                    state.r[2],
1618                    state.r[3],
1619                    state.r[4],
1620                    state.r[5],
1621                    state.r[6],
1622                    state.r[7],
1623                    state.r[8],
1624                    state.r[9],
1625                    state.r[10],
1626                    state.r[11],
1627                    state.r[12],
1628                    state.r[13],
1629                    state.r[14],
1630                    state.r[15],
1631                    state.r[16],
1632                    state.r[17],
1633                    state.r[18],
1634                    state.r[19],
1635                    state.r[20],
1636                    state.r[21],
1637                    state.r[22],
1638                    state.r[23],
1639                    state.r[24],
1640                    state.r[25],
1641                    state.r[26],
1642                    state.r[27],
1643                    state.r[28],
1644                    state.r[29],
1645                ],
1646                lr: state.r[30],
1647                sp: state.sp,
1648                pc: state.pc,
1649                cpsr: state.cpsr as u64,
1650                tpidr: state.tpidr_el0,
1651            }
1652        }
1653    }
1654}
1655
1656#[cfg(target_arch = "riscv64")]
1657#[repr(C)]
1658#[derive(Default, Copy, Clone, Eq, PartialEq)]
1659pub struct zx_thread_state_general_regs_t {
1660    pub pc: u64,
1661    pub ra: u64,  // x1
1662    pub sp: u64,  // x2
1663    pub gp: u64,  // x3
1664    pub tp: u64,  // x4
1665    pub t0: u64,  // x5
1666    pub t1: u64,  // x6
1667    pub t2: u64,  // x7
1668    pub s0: u64,  // x8
1669    pub s1: u64,  // x9
1670    pub a0: u64,  // x10
1671    pub a1: u64,  // x11
1672    pub a2: u64,  // x12
1673    pub a3: u64,  // x13
1674    pub a4: u64,  // x14
1675    pub a5: u64,  // x15
1676    pub a6: u64,  // x16
1677    pub a7: u64,  // x17
1678    pub s2: u64,  // x18
1679    pub s3: u64,  // x19
1680    pub s4: u64,  // x20
1681    pub s5: u64,  // x21
1682    pub s6: u64,  // x22
1683    pub s7: u64,  // x23
1684    pub s8: u64,  // x24
1685    pub s9: u64,  // x25
1686    pub s10: u64, // x26
1687    pub s11: u64, // x27
1688    pub t3: u64,  // x28
1689    pub t4: u64,  // x29
1690    pub t5: u64,  // x30
1691    pub t6: u64,  // x31
1692}
1693
1694#[cfg(target_arch = "riscv64")]
1695impl core::fmt::Debug for zx_thread_state_general_regs_t {
1696    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1697        f.debug_struct(core::any::type_name::<Self>())
1698            .field("pc", &format_args!("{:#x}", self.pc))
1699            .field("ra", &format_args!("{:#x}", self.ra)) // x1
1700            .field("sp", &format_args!("{:#x}", self.sp)) // x2
1701            .field("gp", &format_args!("{:#x}", self.gp)) // x3
1702            .field("tp", &format_args!("{:#x}", self.tp)) // x4
1703            .field("t0", &format_args!("{:#x}", self.t0)) // x5
1704            .field("t1", &format_args!("{:#x}", self.t1)) // x6
1705            .field("t2", &format_args!("{:#x}", self.t2)) // x7
1706            .field("s0", &format_args!("{:#x}", self.s0)) // x8
1707            .field("s1", &format_args!("{:#x}", self.s1)) // x9
1708            .field("a0", &format_args!("{:#x}", self.a0)) // x10
1709            .field("a1", &format_args!("{:#x}", self.a1)) // x11
1710            .field("a2", &format_args!("{:#x}", self.a2)) // x12
1711            .field("a3", &format_args!("{:#x}", self.a3)) // x13
1712            .field("a4", &format_args!("{:#x}", self.a4)) // x14
1713            .field("a5", &format_args!("{:#x}", self.a5)) // x15
1714            .field("a6", &format_args!("{:#x}", self.a6)) // x16
1715            .field("a7", &format_args!("{:#x}", self.a7)) // x17
1716            .field("s2", &format_args!("{:#x}", self.s2)) // x18
1717            .field("s3", &format_args!("{:#x}", self.s3)) // x19
1718            .field("s4", &format_args!("{:#x}", self.s4)) // x20
1719            .field("s5", &format_args!("{:#x}", self.s5)) // x21
1720            .field("s6", &format_args!("{:#x}", self.s6)) // x22
1721            .field("s7", &format_args!("{:#x}", self.s7)) // x23
1722            .field("s8", &format_args!("{:#x}", self.s8)) // x24
1723            .field("s9", &format_args!("{:#x}", self.s9)) // x25
1724            .field("s10", &format_args!("{:#x}", self.s10)) // x26
1725            .field("s11", &format_args!("{:#x}", self.s11)) // x27
1726            .field("t3", &format_args!("{:#x}", self.t3)) // x28
1727            .field("t4", &format_args!("{:#x}", self.t4)) // x29
1728            .field("t5", &format_args!("{:#x}", self.t5)) // x30
1729            .field("t6", &format_args!("{:#x}", self.t6)) // x31
1730            .finish()
1731    }
1732}
1733
1734multiconst!(zx_restricted_reason_t, [
1735    ZX_RESTRICTED_REASON_SYSCALL = 0;
1736    ZX_RESTRICTED_REASON_EXCEPTION = 1;
1737    ZX_RESTRICTED_REASON_KICK = 2;
1738    ZX_RESTRICTED_REASON_EXCEPTION_LOST = 3;
1739]);
1740
1741#[cfg(target_arch = "x86_64")]
1742#[repr(C)]
1743#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1744pub struct zx_restricted_state_t {
1745    pub rdi: u64,
1746    pub rsi: u64,
1747    pub rbp: u64,
1748    pub rbx: u64,
1749    pub rdx: u64,
1750    pub rcx: u64,
1751    pub rax: u64,
1752    pub rsp: u64,
1753    pub r8: u64,
1754    pub r9: u64,
1755    pub r10: u64,
1756    pub r11: u64,
1757    pub r12: u64,
1758    pub r13: u64,
1759    pub r14: u64,
1760    pub r15: u64,
1761    pub ip: u64,
1762    pub flags: u64,
1763    pub fs_base: u64,
1764    pub gs_base: u64,
1765}
1766
1767#[cfg(target_arch = "x86_64")]
1768impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
1769    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
1770        Self {
1771            rdi: registers.rdi,
1772            rsi: registers.rsi,
1773            rbp: registers.rbp,
1774            rbx: registers.rbx,
1775            rdx: registers.rdx,
1776            rcx: registers.rcx,
1777            rax: registers.rax,
1778            rsp: registers.rsp,
1779            r8: registers.r8,
1780            r9: registers.r9,
1781            r10: registers.r10,
1782            r11: registers.r11,
1783            r12: registers.r12,
1784            r13: registers.r13,
1785            r14: registers.r14,
1786            r15: registers.r15,
1787            ip: registers.rip,
1788            flags: registers.rflags,
1789            fs_base: registers.fs_base,
1790            gs_base: registers.gs_base,
1791        }
1792    }
1793}
1794
1795#[cfg(target_arch = "aarch64")]
1796#[repr(C)]
1797#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1798pub struct zx_restricted_state_t {
1799    pub r: [u64; 31], // Note: r[30] is `lr` which is separated out in the general regs.
1800    pub sp: u64,
1801    pub pc: u64,
1802    pub tpidr_el0: u64,
1803    // Contains only the user-controllable upper 4-bits (NZCV).
1804    pub cpsr: u32,
1805    padding1: [PadByte; 4],
1806}
1807
1808#[cfg(target_arch = "aarch64")]
1809impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
1810    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
1811        Self {
1812            r: [
1813                registers.r[0],
1814                registers.r[1],
1815                registers.r[2],
1816                registers.r[3],
1817                registers.r[4],
1818                registers.r[5],
1819                registers.r[6],
1820                registers.r[7],
1821                registers.r[8],
1822                registers.r[9],
1823                registers.r[10],
1824                registers.r[11],
1825                registers.r[12],
1826                registers.r[13],
1827                registers.r[14],
1828                registers.r[15],
1829                registers.r[16],
1830                registers.r[17],
1831                registers.r[18],
1832                registers.r[19],
1833                registers.r[20],
1834                registers.r[21],
1835                registers.r[22],
1836                registers.r[23],
1837                registers.r[24],
1838                registers.r[25],
1839                registers.r[26],
1840                registers.r[27],
1841                registers.r[28],
1842                registers.r[29],
1843                registers.lr, // for compat this works nicely with zircon.
1844            ],
1845            pc: registers.pc,
1846            tpidr_el0: registers.tpidr,
1847            sp: registers.sp,
1848            cpsr: registers.cpsr as u32,
1849            padding1: Default::default(),
1850        }
1851    }
1852}
1853
1854#[cfg(target_arch = "riscv64")]
1855pub type zx_restricted_state_t = zx_thread_state_general_regs_t;
1856
1857#[cfg(target_arch = "riscv64")]
1858impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
1859    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
1860        *registers
1861    }
1862}
1863
1864#[repr(C)]
1865#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1866#[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "riscv64"))]
1867pub struct zx_restricted_syscall_t {
1868    pub state: zx_restricted_state_t,
1869}
1870
1871#[repr(C)]
1872#[derive(Copy, Clone)]
1873#[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "riscv64"))]
1874pub struct zx_restricted_exception_t {
1875    pub state: zx_restricted_state_t,
1876    pub exception: zx_exception_report_t,
1877}
1878
1879#[cfg(target_arch = "x86_64")]
1880#[repr(C)]
1881#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1882pub struct zx_vcpu_state_t {
1883    pub rax: u64,
1884    pub rcx: u64,
1885    pub rdx: u64,
1886    pub rbx: u64,
1887    pub rsp: u64,
1888    pub rbp: u64,
1889    pub rsi: u64,
1890    pub rdi: u64,
1891    pub r8: u64,
1892    pub r9: u64,
1893    pub r10: u64,
1894    pub r11: u64,
1895    pub r12: u64,
1896    pub r13: u64,
1897    pub r14: u64,
1898    pub r15: u64,
1899    // Contains only the user-controllable lower 32-bits.
1900    pub rflags: u64,
1901}
1902
1903#[cfg(target_arch = "aarch64")]
1904#[repr(C)]
1905#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1906pub struct zx_vcpu_state_t {
1907    pub x: [u64; 31],
1908    pub sp: u64,
1909    // Contains only the user-controllable upper 4-bits (NZCV).
1910    pub cpsr: u32,
1911    padding1: [PadByte; 4],
1912}
1913
1914#[cfg(target_arch = "riscv64")]
1915#[repr(C)]
1916#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1917pub struct zx_vcpu_state_t {
1918    pub empty: u32,
1919}
1920
1921#[repr(C)]
1922#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1923pub struct zx_vcpu_io_t {
1924    pub access_size: u8,
1925    padding1: [PadByte; 3],
1926    pub data: [u8; 4],
1927}
1928
1929#[cfg(target_arch = "aarch64")]
1930#[repr(C)]
1931#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1932pub struct zx_packet_guest_mem_t {
1933    pub addr: zx_gpaddr_t,
1934    pub access_size: u8,
1935    pub sign_extend: bool,
1936    pub xt: u8,
1937    pub read: bool,
1938    pub data: u64,
1939}
1940
1941#[cfg(target_arch = "riscv64")]
1942#[repr(C)]
1943#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1944pub struct zx_packet_guest_mem_t {
1945    pub addr: zx_gpaddr_t,
1946    padding1: [PadByte; 24],
1947}
1948
1949pub const X86_MAX_INST_LEN: usize = 15;
1950
1951#[cfg(target_arch = "x86_64")]
1952#[repr(C)]
1953#[derive(Debug, Copy, Clone, Eq, PartialEq)]
1954pub struct zx_packet_guest_mem_t {
1955    pub addr: zx_gpaddr_t,
1956    pub cr3: zx_gpaddr_t,
1957    pub rip: zx_vaddr_t,
1958    pub instruction_size: u8,
1959    pub default_operand_size: u8,
1960}
1961
1962#[repr(C)]
1963#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
1964pub struct zx_packet_interrupt_t {
1965    pub timestamp: zx_time_t,
1966    padding1: [PadByte; 24],
1967}
1968
1969// Helper for constructing topics that have been versioned.
1970const fn info_topic(topic: u32, version: u32) -> u32 {
1971    (version << 28) | topic
1972}
1973
1974multiconst!(zx_object_info_topic_t, [
1975    ZX_INFO_NONE                       = 0;
1976    ZX_INFO_HANDLE_VALID               = 1;
1977    ZX_INFO_HANDLE_BASIC               = 2;  // zx_info_handle_basic_t[1]
1978    ZX_INFO_PROCESS                    = info_topic(3, 1);  // zx_info_process_t[1]
1979    ZX_INFO_PROCESS_THREADS            = 4;  // zx_koid_t[n]
1980    ZX_INFO_VMAR                       = 7;  // zx_info_vmar_t[1]
1981    ZX_INFO_JOB_CHILDREN               = 8;  // zx_koid_t[n]
1982    ZX_INFO_JOB_PROCESSES              = 9;  // zx_koid_t[n]
1983    ZX_INFO_THREAD                     = 10; // zx_info_thread_t[1]
1984    ZX_INFO_THREAD_EXCEPTION_REPORT    = info_topic(11, 1); // zx_exception_report_t[1]
1985    ZX_INFO_TASK_STATS                 = info_topic(12, 1); // zx_info_task_stats_t[1]
1986    ZX_INFO_PROCESS_MAPS               = info_topic(13, 2); // zx_info_maps_t[n]
1987    ZX_INFO_PROCESS_VMOS               = info_topic(14, 3); // zx_info_vmo_t[n]
1988    ZX_INFO_THREAD_STATS               = 15; // zx_info_thread_stats_t[1]
1989    ZX_INFO_CPU_STATS                  = 16; // zx_info_cpu_stats_t[n]
1990    ZX_INFO_KMEM_STATS                 = info_topic(17, 1); // zx_info_kmem_stats_t[1]
1991    ZX_INFO_RESOURCE                   = 18; // zx_info_resource_t[1]
1992    ZX_INFO_HANDLE_COUNT               = 19; // zx_info_handle_count_t[1]
1993    ZX_INFO_BTI                        = 20; // zx_info_bti_t[1]
1994    ZX_INFO_PROCESS_HANDLE_STATS       = 21; // zx_info_process_handle_stats_t[1]
1995    ZX_INFO_SOCKET                     = 22; // zx_info_socket_t[1]
1996    ZX_INFO_VMO                        = info_topic(23, 3); // zx_info_vmo_t[1]
1997    ZX_INFO_JOB                        = 24; // zx_info_job_t[1]
1998    ZX_INFO_TIMER                      = 25; // zx_info_timer_t[1]
1999    ZX_INFO_STREAM                     = 26; // zx_info_stream_t[1]
2000    ZX_INFO_HANDLE_TABLE               = 27; // zx_info_handle_extended_t[n]
2001    ZX_INFO_MSI                        = 28; // zx_info_msi_t[1]
2002    ZX_INFO_GUEST_STATS                = 29; // zx_info_guest_stats_t[1]
2003    ZX_INFO_TASK_RUNTIME               = info_topic(30, 1); // zx_info_task_runtime_t[1]
2004    ZX_INFO_KMEM_STATS_EXTENDED        = 31; // zx_info_kmem_stats_extended_t[1]
2005    ZX_INFO_VCPU                       = 32; // zx_info_vcpu_t[1]
2006    ZX_INFO_KMEM_STATS_COMPRESSION     = 33; // zx_info_kmem_stats_compression_t[1]
2007    ZX_INFO_IOB                        = 34; // zx_info_iob_t[1]
2008    ZX_INFO_IOB_REGIONS                = 35; // zx_iob_region_info_t[n]
2009    ZX_INFO_VMAR_MAPS                  = 36; // zx_info_maps_t[n]
2010    ZX_INFO_POWER_DOMAINS              = 37; // zx_info_power_domain_info_t[n]
2011    ZX_INFO_MEMORY_STALL               = 38; // zx_info_memory_stall_t[1]
2012    ZX_INFO_CLOCK_MAPPED_SIZE          = 40; // usize[1]
2013]);
2014
2015multiconst!(zx_system_memory_stall_type_t, [
2016    ZX_SYSTEM_MEMORY_STALL_SOME        = 0;
2017    ZX_SYSTEM_MEMORY_STALL_FULL        = 1;
2018]);
2019
2020// This macro takes struct-like syntax and creates another macro that can be used to create
2021// different instances of the struct with different names. This is used to keep struct definitions
2022// from drifting between this crate and the fuchsia-zircon crate where they are identical other
2023// than in name and location.
2024macro_rules! struct_decl_macro {
2025    ( $(#[$attrs:meta])* $vis:vis struct <$macro_name:ident> $($any:tt)* ) => {
2026        #[macro_export]
2027        macro_rules! $macro_name {
2028            ($name:ident) => {
2029                $(#[$attrs])* $vis struct $name $($any)*
2030            }
2031        }
2032    }
2033}
2034
2035// Don't need struct_decl_macro for this, the wrapper is different.
2036#[repr(C)]
2037#[derive(Default, Debug, Copy, Clone, Eq, KnownLayout, FromBytes, Immutable, PartialEq)]
2038pub struct zx_info_handle_basic_t {
2039    pub koid: zx_koid_t,
2040    pub rights: zx_rights_t,
2041    pub type_: zx_obj_type_t,
2042    pub related_koid: zx_koid_t,
2043    padding1: [PadByte; 4],
2044}
2045
2046// Don't need struct_decl_macro for this, the wrapper is different.
2047#[repr(C)]
2048#[derive(Default, Debug, Copy, Clone, Eq, KnownLayout, FromBytes, Immutable, PartialEq)]
2049pub struct zx_info_handle_extended_t {
2050    pub type_: zx_obj_type_t,
2051    pub handle_value: zx_handle_t,
2052    pub rights: zx_rights_t,
2053    pub reserved: u32,
2054    pub koid: zx_koid_t,
2055    pub related_koid: zx_koid_t,
2056    pub peer_owner_koid: zx_koid_t,
2057}
2058
2059struct_decl_macro! {
2060    #[repr(C)]
2061    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2062    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2063    pub struct <zx_info_handle_count_t> {
2064        pub handle_count: u32,
2065    }
2066}
2067
2068zx_info_handle_count_t!(zx_info_handle_count_t);
2069
2070// Don't need struct_decl_macro for this, the wrapper is different.
2071#[repr(C)]
2072#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
2073pub struct zx_info_socket_t {
2074    pub options: u32,
2075    pub rx_buf_max: usize,
2076    pub rx_buf_size: usize,
2077    pub rx_buf_available: usize,
2078    pub tx_buf_max: usize,
2079    pub tx_buf_size: usize,
2080}
2081
2082multiconst!(u32, [
2083    ZX_INFO_PROCESS_FLAG_STARTED = 1 << 0;
2084    ZX_INFO_PROCESS_FLAG_EXITED = 1 << 1;
2085    ZX_INFO_PROCESS_FLAG_DEBUGGER_ATTACHED = 1 << 2;
2086]);
2087
2088struct_decl_macro! {
2089    #[repr(C)]
2090    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2091    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2092    pub struct <zx_info_process_t> {
2093        pub return_code: i64,
2094        pub start_time: zx_time_t,
2095        pub flags: u32,
2096    }
2097}
2098
2099zx_info_process_t!(zx_info_process_t);
2100
2101struct_decl_macro! {
2102    #[repr(C)]
2103    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2104    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2105    pub struct <zx_info_job_t> {
2106        pub return_code: i64,
2107        pub exited: u8,
2108        pub kill_on_oom: u8,
2109        pub debugger_attached: u8,
2110    }
2111}
2112
2113zx_info_job_t!(zx_info_job_t);
2114
2115struct_decl_macro! {
2116    #[repr(C)]
2117    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2118    #[derive(zerocopy::FromBytes, zerocopy::IntoBytes, zerocopy::Immutable)]
2119    pub struct <zx_info_timer_t> {
2120        pub options: u32,
2121        pub clock_id: zx_clock_t,
2122        pub deadline: zx_time_t,
2123        pub slack: zx_duration_t,
2124    }
2125}
2126
2127zx_info_timer_t!(zx_info_timer_t);
2128
2129#[repr(C)]
2130#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2131pub struct zx_policy_basic {
2132    pub condition: u32,
2133    pub policy: u32,
2134}
2135
2136#[repr(C)]
2137#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2138pub struct zx_policy_timer_slack {
2139    pub min_slack: zx_duration_t,
2140    pub default_mode: u32,
2141}
2142
2143multiconst!(u32, [
2144    // policy options
2145    ZX_JOB_POL_RELATIVE = 0;
2146    ZX_JOB_POL_ABSOLUTE = 1;
2147
2148    // policy topic
2149    ZX_JOB_POL_BASIC = 0;
2150    ZX_JOB_POL_TIMER_SLACK = 1;
2151
2152    // policy conditions
2153    ZX_POL_BAD_HANDLE            = 0;
2154    ZX_POL_WRONG_OBJECT          = 1;
2155    ZX_POL_VMAR_WX               = 2;
2156    ZX_POL_NEW_ANY               = 3;
2157    ZX_POL_NEW_VMO               = 4;
2158    ZX_POL_NEW_CHANNEL           = 5;
2159    ZX_POL_NEW_EVENT             = 6;
2160    ZX_POL_NEW_EVENTPAIR         = 7;
2161    ZX_POL_NEW_PORT              = 8;
2162    ZX_POL_NEW_SOCKET            = 9;
2163    ZX_POL_NEW_FIFO              = 10;
2164    ZX_POL_NEW_TIMER             = 11;
2165    ZX_POL_NEW_PROCESS           = 12;
2166    ZX_POL_NEW_PROFILE           = 13;
2167    ZX_POL_NEW_PAGER             = 14;
2168    ZX_POL_AMBIENT_MARK_VMO_EXEC = 15;
2169    ZX_POL_NEW_IOB               = 16;
2170    ZX_POL_NEW_SAMPLER           = 17;
2171
2172    // policy actions
2173    ZX_POL_ACTION_ALLOW           = 0;
2174    ZX_POL_ACTION_DENY            = 1;
2175    ZX_POL_ACTION_ALLOW_EXCEPTION = 2;
2176    ZX_POL_ACTION_DENY_EXCEPTION  = 3;
2177    ZX_POL_ACTION_KILL            = 4;
2178
2179    // timer slack default modes
2180    ZX_TIMER_SLACK_CENTER = 0;
2181    ZX_TIMER_SLACK_EARLY  = 1;
2182    ZX_TIMER_SLACK_LATE   = 2;
2183]);
2184
2185multiconst!(u32, [
2186    // critical options
2187    ZX_JOB_CRITICAL_PROCESS_RETCODE_NONZERO = 1 << 0;
2188]);
2189
2190// Don't use struct_decl_macro, wrapper is different.
2191#[repr(C)]
2192#[derive(
2193    Default, Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable, IntoBytes,
2194)]
2195pub struct zx_info_vmo_t {
2196    pub koid: zx_koid_t,
2197    pub name: [u8; ZX_MAX_NAME_LEN],
2198    pub size_bytes: u64,
2199    pub parent_koid: zx_koid_t,
2200    pub num_children: usize,
2201    pub num_mappings: usize,
2202    pub share_count: usize,
2203    pub flags: u32,
2204    padding1: [PadByte; 4],
2205    pub committed_bytes: u64,
2206    pub handle_rights: zx_rights_t,
2207    pub cache_policy: u32,
2208    pub metadata_bytes: u64,
2209    pub committed_change_events: u64,
2210    pub populated_bytes: u64,
2211    pub committed_private_bytes: u64,
2212    pub populated_private_bytes: u64,
2213    pub committed_scaled_bytes: u64,
2214    pub populated_scaled_bytes: u64,
2215    pub committed_fractional_scaled_bytes: u64,
2216    pub populated_fractional_scaled_bytes: u64,
2217}
2218
2219struct_decl_macro! {
2220    #[repr(C)]
2221    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2222    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2223    pub struct <zx_info_cpu_stats_t> {
2224        pub cpu_number: u32,
2225        pub flags: u32,
2226        pub idle_time: zx_duration_t,
2227        pub normalized_busy_time: zx_duration_t,
2228        pub reschedules: u64,
2229        pub context_switches: u64,
2230        pub irq_preempts: u64,
2231        pub preempts: u64,
2232        pub yields: u64,
2233        pub ints: u64,
2234        pub timer_ints: u64,
2235        pub timers: u64,
2236        pub page_faults: u64,
2237        pub exceptions: u64,
2238        pub syscalls: u64,
2239        pub reschedule_ipis: u64,
2240        pub generic_ipis: u64,
2241        pub active_energy_consumption_nj: u64,
2242        pub idle_energy_consumption_nj: u64,
2243    }
2244}
2245
2246zx_info_cpu_stats_t!(zx_info_cpu_stats_t);
2247
2248struct_decl_macro! {
2249    #[repr(C)]
2250    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2251    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2252    pub struct <zx_info_kmem_stats_t> {
2253        pub total_bytes: u64,
2254        pub free_bytes: u64,
2255        pub free_loaned_bytes: u64,
2256        pub wired_bytes: u64,
2257        pub total_heap_bytes: u64,
2258        pub free_heap_bytes: u64,
2259        pub vmo_bytes: u64,
2260        pub mmu_overhead_bytes: u64,
2261        pub ipc_bytes: u64,
2262        pub cache_bytes: u64,
2263        pub slab_bytes: u64,
2264        pub zram_bytes: u64,
2265        pub other_bytes: u64,
2266        pub vmo_reclaim_total_bytes: u64,
2267        pub vmo_reclaim_newest_bytes: u64,
2268        pub vmo_reclaim_oldest_bytes: u64,
2269        pub vmo_reclaim_disabled_bytes: u64,
2270        pub vmo_discardable_locked_bytes: u64,
2271        pub vmo_discardable_unlocked_bytes: u64,
2272    }
2273}
2274
2275zx_info_kmem_stats_t!(zx_info_kmem_stats_t);
2276
2277struct_decl_macro! {
2278    #[repr(C)]
2279    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2280    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2281    pub struct <zx_info_kmem_stats_extended_t> {
2282        pub total_bytes: u64,
2283        pub free_bytes: u64,
2284        pub wired_bytes: u64,
2285        pub total_heap_bytes: u64,
2286        pub free_heap_bytes: u64,
2287        pub vmo_bytes: u64,
2288        pub vmo_pager_total_bytes: u64,
2289        pub vmo_pager_newest_bytes: u64,
2290        pub vmo_pager_oldest_bytes: u64,
2291        pub vmo_discardable_locked_bytes: u64,
2292        pub vmo_discardable_unlocked_bytes: u64,
2293        pub mmu_overhead_bytes: u64,
2294        pub ipc_bytes: u64,
2295        pub other_bytes: u64,
2296        pub vmo_reclaim_disable_bytes: u64,
2297    }
2298}
2299
2300zx_info_kmem_stats_extended_t!(zx_info_kmem_stats_extended_t);
2301
2302struct_decl_macro! {
2303    #[repr(C)]
2304    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2305    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2306    pub struct <zx_info_kmem_stats_compression_t> {
2307        pub uncompressed_storage_bytes: u64,
2308        pub compressed_storage_bytes: u64,
2309        pub compressed_fragmentation_bytes: u64,
2310        pub compression_time: zx_duration_t,
2311        pub decompression_time: zx_duration_t,
2312        pub total_page_compression_attempts: u64,
2313        pub failed_page_compression_attempts: u64,
2314        pub total_page_decompressions: u64,
2315        pub compressed_page_evictions: u64,
2316        pub eager_page_compressions: u64,
2317        pub memory_pressure_page_compressions: u64,
2318        pub critical_memory_page_compressions: u64,
2319        pub pages_decompressed_unit_ns: u64,
2320        pub pages_decompressed_within_log_time: [u64; 8],
2321    }
2322}
2323
2324zx_info_kmem_stats_compression_t!(zx_info_kmem_stats_compression_t);
2325
2326struct_decl_macro! {
2327    #[repr(C)]
2328    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2329    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2330    pub struct <zx_info_resource_t> {
2331        pub kind: u32,
2332        pub flags: u32,
2333        pub base: u64,
2334        pub size: usize,
2335        pub name: [u8; ZX_MAX_NAME_LEN],
2336    }
2337}
2338
2339struct_decl_macro! {
2340    #[repr(C)]
2341    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2342    #[derive(zerocopy::FromBytes, zerocopy::IntoBytes, zerocopy::Immutable)]
2343    pub struct <zx_info_bti_t> {
2344        pub minimum_contiguity: u64,
2345        pub aspace_size: u64,
2346        pub pmo_count: u64,
2347        pub quarantine_count: u64,
2348    }
2349}
2350
2351zx_info_bti_t!(zx_info_bti_t);
2352
2353pub type zx_thread_state_t = u32;
2354
2355multiconst!(zx_thread_state_t, [
2356    ZX_THREAD_STATE_NEW = 0x0000;
2357    ZX_THREAD_STATE_RUNNING = 0x0001;
2358    ZX_THREAD_STATE_SUSPENDED = 0x0002;
2359    ZX_THREAD_STATE_BLOCKED = 0x0003;
2360    ZX_THREAD_STATE_DYING = 0x0004;
2361    ZX_THREAD_STATE_DEAD = 0x0005;
2362    ZX_THREAD_STATE_BLOCKED_EXCEPTION = 0x0103;
2363    ZX_THREAD_STATE_BLOCKED_SLEEPING = 0x0203;
2364    ZX_THREAD_STATE_BLOCKED_FUTEX = 0x0303;
2365    ZX_THREAD_STATE_BLOCKED_PORT = 0x0403;
2366    ZX_THREAD_STATE_BLOCKED_CHANNEL = 0x0503;
2367    ZX_THREAD_STATE_BLOCKED_WAIT_ONE = 0x0603;
2368    ZX_THREAD_STATE_BLOCKED_WAIT_MANY = 0x0703;
2369    ZX_THREAD_STATE_BLOCKED_INTERRUPT = 0x0803;
2370    ZX_THREAD_STATE_BLOCKED_PAGER = 0x0903;
2371]);
2372
2373#[repr(C)]
2374#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, zerocopy::FromBytes, zerocopy::Immutable)]
2375pub struct zx_info_thread_t {
2376    pub state: zx_thread_state_t,
2377    pub wait_exception_channel_type: u32,
2378    pub cpu_affinity_mask: zx_cpu_set_t,
2379}
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_thread_stats_t> {
2386        pub total_runtime: zx_duration_t,
2387        pub last_scheduled_cpu: u32,
2388    }
2389}
2390
2391zx_info_thread_stats_t!(zx_info_thread_stats_t);
2392
2393zx_info_resource_t!(zx_info_resource_t);
2394
2395struct_decl_macro! {
2396    #[repr(C)]
2397    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2398    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2399    pub struct <zx_info_vmar_t> {
2400        pub base: usize,
2401        pub len: usize,
2402    }
2403}
2404
2405zx_info_vmar_t!(zx_info_vmar_t);
2406
2407struct_decl_macro! {
2408    #[repr(C)]
2409    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2410    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2411    pub struct <zx_info_task_stats_t> {
2412        pub mem_mapped_bytes: usize,
2413        pub mem_private_bytes: usize,
2414        pub mem_shared_bytes: usize,
2415        pub mem_scaled_shared_bytes: usize,
2416        pub mem_fractional_scaled_shared_bytes: u64,
2417    }
2418}
2419
2420zx_info_task_stats_t!(zx_info_task_stats_t);
2421
2422struct_decl_macro! {
2423    #[repr(C)]
2424    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2425    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2426    pub struct <zx_info_task_runtime_t> {
2427        pub cpu_time: zx_duration_t,
2428        pub queue_time: zx_duration_t,
2429        pub page_fault_time: zx_duration_t,
2430        pub lock_contention_time: zx_duration_t,
2431    }
2432}
2433
2434zx_info_task_runtime_t!(zx_info_task_runtime_t);
2435
2436multiconst!(zx_info_maps_type_t, [
2437    ZX_INFO_MAPS_TYPE_NONE    = 0;
2438    ZX_INFO_MAPS_TYPE_ASPACE  = 1;
2439    ZX_INFO_MAPS_TYPE_VMAR    = 2;
2440    ZX_INFO_MAPS_TYPE_MAPPING = 3;
2441]);
2442
2443struct_decl_macro! {
2444    #[repr(C)]
2445    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2446    #[derive(zerocopy::FromBytes, zerocopy::Immutable, IntoBytes)]
2447    pub struct <zx_info_maps_mapping_t> {
2448        pub mmu_flags: zx_vm_option_t,
2449        padding1: [PadByte; 4],
2450        pub vmo_koid: zx_koid_t,
2451        pub vmo_offset: u64,
2452        pub committed_bytes: usize,
2453        pub populated_bytes: usize,
2454        pub committed_private_bytes: usize,
2455        pub populated_private_bytes: usize,
2456        pub committed_scaled_bytes: usize,
2457        pub populated_scaled_bytes: usize,
2458        pub committed_fractional_scaled_bytes: u64,
2459        pub populated_fractional_scaled_bytes: u64,
2460    }
2461}
2462
2463zx_info_maps_mapping_t!(zx_info_maps_mapping_t);
2464
2465#[repr(C)]
2466#[derive(Copy, Clone, KnownLayout, FromBytes, Immutable)]
2467pub union InfoMapsTypeUnion {
2468    pub mapping: zx_info_maps_mapping_t,
2469}
2470
2471struct_decl_macro! {
2472    #[repr(C)]
2473    #[derive(Copy, Clone)]
2474    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2475    pub struct <zx_info_maps_t> {
2476        pub name: [u8; ZX_MAX_NAME_LEN],
2477        pub base: zx_vaddr_t,
2478        pub size: usize,
2479        pub depth: usize,
2480        pub r#type: zx_info_maps_type_t,
2481        pub u: InfoMapsTypeUnion,
2482    }
2483}
2484
2485zx_info_maps_t!(zx_info_maps_t);
2486
2487struct_decl_macro! {
2488    #[repr(C)]
2489    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
2490    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
2491    pub struct <zx_info_process_handle_stats_t> {
2492        pub handle_count: [u32; ZX_OBJ_TYPE_UPPER_BOUND],
2493    }
2494}
2495
2496impl Default for zx_info_process_handle_stats_t {
2497    fn default() -> Self {
2498        Self { handle_count: [0; ZX_OBJ_TYPE_UPPER_BOUND] }
2499    }
2500}
2501
2502zx_info_process_handle_stats_t!(zx_info_process_handle_stats_t);
2503
2504struct_decl_macro! {
2505    #[repr(C)]
2506    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
2507    #[derive(zerocopy::FromBytes, zerocopy::Immutable, zerocopy::IntoBytes)]
2508    pub struct <zx_info_memory_stall_t> {
2509        pub stall_time_some: zx_duration_mono_t,
2510        pub stall_time_full: zx_duration_mono_t,
2511    }
2512}
2513
2514zx_info_memory_stall_t!(zx_info_memory_stall_t);
2515
2516// from //zircon/system/public/zircon/syscalls/hypervisor.h
2517multiconst!(zx_guest_trap_t, [
2518    ZX_GUEST_TRAP_BELL = 0;
2519    ZX_GUEST_TRAP_MEM  = 1;
2520    ZX_GUEST_TRAP_IO   = 2;
2521]);
2522
2523pub const ZX_LOG_RECORD_MAX: usize = 256;
2524pub const ZX_LOG_RECORD_DATA_MAX: usize = 216;
2525
2526pub const DEBUGLOG_TRACE: u8 = 0x10;
2527pub const DEBUGLOG_DEBUG: u8 = 0x20;
2528pub const DEBUGLOG_INFO: u8 = 0x30;
2529pub const DEBUGLOG_WARNING: u8 = 0x40;
2530pub const DEBUGLOG_ERROR: u8 = 0x50;
2531pub const DEBUGLOG_FATAL: u8 = 0x60;
2532
2533#[repr(C)]
2534#[derive(
2535    Debug,
2536    Default,
2537    Copy,
2538    Clone,
2539    Eq,
2540    PartialEq,
2541    zerocopy::FromBytes,
2542    zerocopy::IntoBytes,
2543    zerocopy::Immutable,
2544)]
2545pub struct zx_log_record_header_t {
2546    pub sequence: u64,
2547    padding1: [PadByte; 4],
2548    pub datalen: u16,
2549    pub severity: u8,
2550    pub flags: u8,
2551    pub timestamp: zx_instant_boot_t,
2552    pub pid: u64,
2553    pub tid: u64,
2554}
2555
2556#[repr(C)]
2557#[derive(
2558    Debug, Copy, Clone, Eq, PartialEq, zerocopy::FromBytes, zerocopy::IntoBytes, zerocopy::Immutable,
2559)]
2560pub struct zx_log_record_t {
2561    pub header: zx_log_record_header_t,
2562    pub data: [u8; ZX_LOG_RECORD_DATA_MAX],
2563}
2564
2565const_assert_eq!(core::mem::size_of::<zx_log_record_t>(), ZX_LOG_RECORD_MAX);
2566
2567impl Default for zx_log_record_t {
2568    fn default() -> Self {
2569        Self { header: zx_log_record_header_t::default(), data: [0; ZX_LOG_RECORD_DATA_MAX] }
2570    }
2571}
2572
2573multiconst!(u32, [
2574    ZX_LOG_FLAG_READABLE = 0x40000000;
2575]);
2576
2577// For C, the below types are currently forward declared for syscalls.h.
2578// We might want to investigate a better solution for Rust or removing those
2579// forward declarations.
2580//
2581// These are hand typed translations from C types into Rust structures using a C
2582// layout
2583
2584// source: zircon/system/public/zircon/syscalls/system.h
2585#[repr(C)]
2586pub struct zx_system_powerctl_arg_t {
2587    // rust can't express anonymous unions at this time
2588    // https://github.com/rust-lang/rust/issues/49804
2589    pub powerctl_internal: zx_powerctl_union,
2590}
2591
2592#[repr(C)]
2593#[derive(Copy, Clone)]
2594pub union zx_powerctl_union {
2595    acpi_transition_s_state: acpi_transition_s_state,
2596    x86_power_limit: x86_power_limit,
2597}
2598
2599#[repr(C)]
2600#[derive(Default, Debug, PartialEq, Copy, Clone)]
2601pub struct acpi_transition_s_state {
2602    target_s_state: u8, // Value between 1 and 5 indicating which S-state
2603    sleep_type_a: u8,   // Value from ACPI VM (SLP_TYPa)
2604    sleep_type_b: u8,   // Value from ACPI VM (SLP_TYPb)
2605    padding1: [PadByte; 9],
2606}
2607
2608#[repr(C)]
2609#[derive(Default, Debug, PartialEq, Copy, Clone)]
2610pub struct x86_power_limit {
2611    power_limit: u32, // PL1 value in milliwatts
2612    time_window: u32, // PL1 time window in microseconds
2613    clamp: u8,        // PL1 clamping enable
2614    enable: u8,       // PL1 enable
2615    padding1: [PadByte; 2],
2616}
2617
2618// source: zircon/system/public/zircon/syscalls/pci.h
2619pub type zx_pci_bar_types_t = u32;
2620
2621multiconst!(zx_pci_bar_types_t, [
2622            ZX_PCI_BAR_TYPE_UNUSED = 0;
2623            ZX_PCI_BAR_TYPE_MMIO = 1;
2624            ZX_PCI_BAR_TYPE_PIO = 2;
2625]);
2626
2627#[repr(C)]
2628pub struct zx_pci_bar_t {
2629    pub id: u32,
2630    pub ty: u32,
2631    pub size: usize,
2632    // rust can't express anonymous unions at this time
2633    // https://github.com/rust-lang/rust/issues/49804
2634    pub zx_pci_bar_union: zx_pci_bar_union,
2635}
2636
2637#[repr(C)]
2638#[derive(Copy, Clone)]
2639pub union zx_pci_bar_union {
2640    addr: usize,
2641    zx_pci_bar_union_struct: zx_pci_bar_union_struct,
2642}
2643
2644#[repr(C)]
2645#[derive(Default, Debug, PartialEq, Copy, Clone)]
2646pub struct zx_pci_bar_union_struct {
2647    handle: zx_handle_t,
2648    padding1: [PadByte; 4],
2649}
2650
2651// source: zircon/system/public/zircon/syscalls/smc.h
2652#[repr(C)]
2653#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
2654pub struct zx_smc_parameters_t {
2655    pub func_id: u32,
2656    padding1: [PadByte; 4],
2657    pub arg1: u64,
2658    pub arg2: u64,
2659    pub arg3: u64,
2660    pub arg4: u64,
2661    pub arg5: u64,
2662    pub arg6: u64,
2663    pub client_id: u16,
2664    pub secure_os_id: u16,
2665    padding2: [PadByte; 4],
2666}
2667
2668#[repr(C)]
2669#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2670pub struct zx_smc_result_t {
2671    pub arg0: u64,
2672    pub arg1: u64,
2673    pub arg2: u64,
2674    pub arg3: u64,
2675    pub arg6: u64,
2676}
2677
2678pub const ZX_CPU_SET_MAX_CPUS: usize = 512;
2679pub const ZX_CPU_SET_BITS_PER_WORD: usize = 64;
2680
2681#[repr(C)]
2682#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, zerocopy::FromBytes, zerocopy::Immutable)]
2683pub struct zx_cpu_set_t {
2684    pub mask: [u64; ZX_CPU_SET_MAX_CPUS / ZX_CPU_SET_BITS_PER_WORD],
2685}
2686
2687// source: zircon/system/public/zircon/syscalls/scheduler.h
2688#[repr(C)]
2689#[derive(Copy, Clone)]
2690pub struct zx_profile_info_t {
2691    pub flags: u32,
2692    padding1: [PadByte; 4],
2693    pub zx_profile_info_union: zx_profile_info_union,
2694    pub cpu_affinity_mask: zx_cpu_set_t,
2695}
2696
2697#[cfg(feature = "zerocopy")]
2698impl Default for zx_profile_info_t {
2699    fn default() -> Self {
2700        Self {
2701            flags: Default::default(),
2702            padding1: Default::default(),
2703            zx_profile_info_union: FromZeros::new_zeroed(),
2704            cpu_affinity_mask: Default::default(),
2705        }
2706    }
2707}
2708
2709#[repr(C)]
2710#[derive(Copy, Clone)]
2711#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
2712pub struct priority_params {
2713    pub priority: i32,
2714    padding1: [PadByte; 20],
2715}
2716
2717#[repr(C)]
2718#[derive(Copy, Clone)]
2719#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
2720pub union zx_profile_info_union {
2721    pub priority_params: priority_params,
2722    pub deadline_params: zx_sched_deadline_params_t,
2723}
2724
2725#[repr(C)]
2726#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2727#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, KnownLayout))]
2728pub struct zx_sched_deadline_params_t {
2729    pub capacity: zx_duration_t,
2730    pub relative_deadline: zx_duration_t,
2731    pub period: zx_duration_t,
2732}
2733
2734#[repr(C)]
2735#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2736pub struct zx_cpu_performance_scale_t {
2737    pub integer_part: u32,
2738    pub fractional_part: u32,
2739}
2740
2741#[repr(C)]
2742#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2743pub struct zx_cpu_performance_info_t {
2744    pub logical_cpu_number: u32,
2745    pub performance_scale: zx_cpu_performance_scale_t,
2746}
2747
2748#[repr(C)]
2749#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2750pub struct zx_cpu_perf_limit_t {
2751    pub logical_cpu_number: u32,
2752    pub limit_type: u32,
2753    pub min: u64,
2754    pub max: u64,
2755}
2756
2757#[repr(C)]
2758#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2759pub struct zx_iommu_desc_stub_t {
2760    padding1: PadByte,
2761}
2762
2763multiconst!(u32, [
2764    ZX_IOMMU_TYPE_STUB = 0;
2765    ZX_IOMMU_TYPE_INTEL = 1;
2766]);
2767
2768pub const ZX_SAMPLER_MIN_PERIOD: zx_duration_t = 10_000;
2769pub const ZX_SAMPLER_MAX_BUFFER_SIZE: usize = 1024 * 1024 * 1024;
2770
2771#[repr(C)]
2772#[derive(Debug, Copy, Clone)]
2773#[cfg_attr(feature = "zerocopy", derive(FromBytes, IntoBytes, Immutable))]
2774pub struct zx_sampler_config_t {
2775    pub period: zx_duration_t,
2776    pub buffer_size: usize,
2777    pub iobuffer_discipline: u64,
2778}
2779
2780multiconst!(zx_processor_power_level_options_t, [
2781    ZX_PROCESSOR_POWER_LEVEL_OPTIONS_DOMAIN_INDEPENDENT = 1 << 0;
2782]);
2783
2784multiconst!(zx_processor_power_control_t, [
2785    ZX_PROCESSOR_POWER_CONTROL_CPU_DRIVER = 0;
2786    ZX_PROCESSOR_POWER_CONTROL_ARM_PSCI = 1;
2787    ZX_PROCESSOR_POWER_CONTROL_ARM_WFI = 2;
2788    ZX_PROCESSOR_POWER_CONTROL_RISCV_SBI = 3;
2789    ZX_PROCESSOR_POWER_CONTROL_RISCV_WFI = 4;
2790]);
2791
2792#[repr(C)]
2793#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2794pub struct zx_processor_power_level_t {
2795    pub options: zx_processor_power_level_options_t,
2796    pub processing_rate: u64,
2797    pub power_coefficient_nw: u64,
2798    pub control_interface: zx_processor_power_control_t,
2799    pub control_argument: u64,
2800    pub diagnostic_name: [u8; ZX_MAX_NAME_LEN],
2801    padding1: [PadByte; 32],
2802}
2803
2804#[repr(C)]
2805#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
2806pub struct zx_processor_power_level_transition_t {
2807    pub latency: zx_duration_t,
2808    pub energy: u64,
2809    pub from: u8,
2810    pub to: u8,
2811    padding1: [PadByte; 6],
2812}
2813
2814#[repr(C)]
2815#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
2816pub struct zx_packet_processor_power_level_transition_request_t {
2817    pub domain_id: u32,
2818    pub options: u32,
2819    pub control_interface: u64,
2820    pub control_argument: u64,
2821    padding1: [PadByte; 8],
2822}
2823
2824#[repr(C)]
2825#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2826pub struct zx_processor_power_state_t {
2827    pub domain_id: u32,
2828    pub options: u32,
2829    pub control_interface: u64,
2830    pub control_argument: u64,
2831}
2832
2833#[repr(C)]
2834#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
2835pub struct zx_processor_power_domain_t {
2836    pub cpus: zx_cpu_set_t,
2837    pub domain_id: u32,
2838    padding1: [PadByte; 4],
2839}
2840
2841#[repr(C)]
2842#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2843pub struct zx_power_domain_info_t {
2844    pub cpus: zx_cpu_set_t,
2845    pub domain_id: u32,
2846    pub idle_power_levels: u8,
2847    pub active_power_levels: u8,
2848    padding1: [PadByte; 2],
2849}
2850
2851multiconst!(u32, [
2852    ZX_BTI_PERM_READ = 1 << 0;
2853    ZX_BTI_PERM_WRITE = 1 << 1;
2854    ZX_BTI_PERM_EXECUTE = 1 << 2;
2855    ZX_BTI_COMPRESS = 1 << 3;
2856    ZX_BTI_CONTIGUOUS = 1 << 4;
2857]);
2858
2859// Options for zx_port_create
2860multiconst!(u32, [
2861    ZX_PORT_BIND_TO_INTERRUPT = 1 << 0;
2862]);
2863
2864// Options for zx_interrupt_create
2865multiconst!(u32, [
2866    ZX_INTERRUPT_VIRTUAL = 0x10;
2867    ZX_INTERRUPT_TIMESTAMP_MONO = 1 << 6;
2868]);
2869
2870// Options for zx_interrupt_bind
2871multiconst!(u32, [
2872    ZX_INTERRUPT_BIND = 0;
2873    ZX_INTERRUPT_UNBIND = 1;
2874]);
2875
2876#[repr(C)]
2877pub struct zx_iob_region_t {
2878    pub r#type: zx_iob_region_type_t,
2879    pub access: zx_iob_access_t,
2880    pub size: u64,
2881    pub discipline: zx_iob_discipline_t,
2882    pub extension: zx_iob_region_extension_t,
2883}
2884
2885multiconst!(zx_iob_region_type_t, [
2886    ZX_IOB_REGION_TYPE_PRIVATE = 0;
2887    ZX_IOB_REGION_TYPE_SHARED = 1;
2888]);
2889
2890multiconst!(zx_iob_access_t, [
2891    ZX_IOB_ACCESS_EP0_CAN_MAP_READ = 1 << 0;
2892    ZX_IOB_ACCESS_EP0_CAN_MAP_WRITE = 1 << 1;
2893    ZX_IOB_ACCESS_EP0_CAN_MEDIATED_READ = 1 << 2;
2894    ZX_IOB_ACCESS_EP0_CAN_MEDIATED_WRITE = 1 << 3;
2895    ZX_IOB_ACCESS_EP1_CAN_MAP_READ = 1 << 4;
2896    ZX_IOB_ACCESS_EP1_CAN_MAP_WRITE = 1 << 5;
2897    ZX_IOB_ACCESS_EP1_CAN_MEDIATED_READ = 1 << 6;
2898    ZX_IOB_ACCESS_EP1_CAN_MEDIATED_WRITE = 1 << 7;
2899]);
2900
2901#[repr(C)]
2902#[derive(Copy, Clone)]
2903pub struct zx_iob_discipline_t {
2904    pub r#type: zx_iob_discipline_type_t,
2905    pub extension: zx_iob_discipline_extension_t,
2906}
2907
2908#[repr(C)]
2909#[derive(Copy, Clone)]
2910pub union zx_iob_discipline_extension_t {
2911    // This is in vdso-next.
2912    pub ring_buffer: zx_iob_discipline_mediated_write_ring_buffer_t,
2913    pub reserved: [PadByte; 64],
2914}
2915
2916#[repr(C)]
2917#[derive(Debug, Copy, Clone)]
2918pub struct zx_iob_discipline_mediated_write_ring_buffer_t {
2919    pub tag: u64,
2920    pub padding: [PadByte; 56],
2921}
2922
2923multiconst!(zx_iob_discipline_type_t, [
2924    ZX_IOB_DISCIPLINE_TYPE_NONE = 0;
2925    ZX_IOB_DISCIPLINE_TYPE_MEDIATED_WRITE_RING_BUFFER = 2;
2926]);
2927
2928#[repr(C)]
2929#[derive(Clone, Copy, Default)]
2930pub struct zx_iob_region_private_t {
2931    options: u32,
2932    padding: [PadByte; 28],
2933}
2934
2935#[repr(C)]
2936#[derive(Clone, Copy)]
2937pub struct zx_iob_region_shared_t {
2938    pub options: u32,
2939    pub shared_region: zx_handle_t,
2940    pub padding: [PadByte; 24],
2941}
2942
2943#[repr(C)]
2944pub union zx_iob_region_extension_t {
2945    pub private_region: zx_iob_region_private_t,
2946    pub shared_region: zx_iob_region_shared_t,
2947    pub max_extension: [u8; 32],
2948}
2949
2950#[repr(C)]
2951pub struct zx_wake_source_report_entry_t {
2952    pub koid: zx_koid_t,
2953    pub name: [u8; ZX_MAX_NAME_LEN],
2954    pub initial_signal_time: zx_instant_boot_t,
2955    pub last_signal_time: zx_instant_boot_t,
2956    pub last_ack_time: zx_instant_boot_t,
2957    pub signal_count: u32,
2958    pub flags: u32,
2959}
2960
2961#[repr(C)]
2962pub struct zx_wake_source_report_header_t {
2963    pub report_time: zx_instant_boot_t,
2964    pub suspend_start_time: zx_instant_boot_t,
2965    pub total_wake_sources: u32,
2966    pub unreported_wake_report_entries: u32,
2967}
2968
2969#[cfg(test)]
2970mod test {
2971    #[cfg(test)]
2972    extern crate alloc;
2973
2974    use super::*;
2975
2976    #[test]
2977    fn padded_struct_equality() {
2978        let test_struct = zx_clock_update_args_v1_t {
2979            rate_adjust: 222,
2980            padding1: Default::default(),
2981            value: 333,
2982            error_bound: 444,
2983        };
2984
2985        let different_data = zx_clock_update_args_v1_t { rate_adjust: 999, ..test_struct.clone() };
2986
2987        let different_padding = zx_clock_update_args_v1_t {
2988            padding1: [PadByte(0), PadByte(1), PadByte(2), PadByte(3)],
2989            ..test_struct.clone()
2990        };
2991
2992        // Structures with different data should not be equal.
2993        assert_ne!(test_struct, different_data);
2994        // Structures with only different padding should not be equal.
2995        assert_eq!(test_struct, different_padding);
2996    }
2997
2998    #[test]
2999    fn padded_struct_debug() {
3000        let test_struct = zx_clock_update_args_v1_t {
3001            rate_adjust: 222,
3002            padding1: Default::default(),
3003            value: 333,
3004            error_bound: 444,
3005        };
3006        let expectation = "zx_clock_update_args_v1_t { \
3007            rate_adjust: 222, \
3008            padding1: [-, -, -, -], \
3009            value: 333, \
3010            error_bound: 444 }";
3011        assert_eq!(alloc::format!("{:?}", test_struct), expectation);
3012    }
3013}
3014
3015#[repr(C, align(32))]
3016#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
3017#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes, KnownLayout))]
3018pub struct zx_rseq_t {
3019    pub cpu_id: u32,
3020    pub reserved: u32,
3021    pub start_ip: u64,
3022    pub post_commit_offset: u64,
3023    pub abort_ip: u64,
3024}
3025
3026pub const ZX_INFO_INVALID_CPU: u32 = 0xFFFFFFFF;