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