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