Skip to main content

zx_types/
lib.rs

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