Skip to main content

zx_types/
lib.rs

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