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