zx_types/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#![allow(non_camel_case_types)]

use std::fmt::{self, Debug};
use std::hash::{Hash, Hasher};
use std::sync::atomic::AtomicI32;
#[cfg(feature = "zerocopy")]
use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout};

pub type zx_addr_t = usize;
pub type zx_stream_seek_origin_t = u32;
pub type zx_clock_t = u32;
pub type zx_duration_t = i64;
pub type zx_duration_mono_t = i64;
pub type zx_duration_mono_ticks_t = i64;
pub type zx_duration_boot_t = i64;
pub type zx_duration_boot_ticks_t = i64;
pub type zx_futex_t = AtomicI32;
pub type zx_gpaddr_t = usize;
pub type zx_guest_option_t = u32;
pub type zx_vcpu_option_t = u32;
pub type zx_guest_trap_t = u32;
pub type zx_handle_t = u32;
pub type zx_handle_op_t = u32;
pub type zx_koid_t = u64;
pub type zx_obj_type_t = u32;
pub type zx_object_info_topic_t = u32;
pub type zx_info_maps_type_t = u32;
pub type zx_instant_boot_t = i64;
pub type zx_instant_boot_ticks_t = i64;
pub type zx_instant_mono_t = i64;
pub type zx_instant_mono_ticks_t = i64;
pub type zx_iob_allocate_id_options_t = u32;
pub type zx_off_t = u64;
pub type zx_paddr_t = usize;
pub type zx_rights_t = u32;
pub type zx_rsrc_flags_t = u32;
pub type zx_rsrc_kind_t = u32;
pub type zx_signals_t = u32;
pub type zx_ssize_t = isize;
pub type zx_status_t = i32;
pub type zx_rsrc_system_base_t = u64;
pub type zx_ticks_t = i64;
pub type zx_time_t = i64;
pub type zx_txid_t = u32;
pub type zx_vaddr_t = usize;
pub type zx_vm_option_t = u32;
pub type zx_thread_state_topic_t = u32;
pub type zx_vcpu_state_topic_t = u32;
pub type zx_restricted_reason_t = u64;
pub type zx_processor_power_level_options_t = u64;
pub type zx_processor_power_control_t = u64;

macro_rules! const_assert {
    ($e:expr $(,)?) => {
        const _: [(); 1 - { const ASSERT: bool = $e; ASSERT as usize }] = [];
    };
}
macro_rules! const_assert_eq {
    ($lhs:expr, $rhs:expr $(,)?) => {
        const_assert!($lhs == $rhs);
    };
}

// TODO: magically coerce this to &`static str somehow?
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_string_view_t {
    pub c_str: *const u8, // Guaranteed NUL-terminated valid UTF-8.
    pub length: usize,
}

pub const ZX_MAX_NAME_LEN: usize = 32;

// TODO: combine these macros with the bitflags and assoc consts macros below
// so that we only have to do one macro invocation.
// The result would look something like:
// multiconst!(bitflags, zx_rights_t, Rights, [RIGHT_NONE => ZX_RIGHT_NONE = 0; ...]);
// multiconst!(assoc_consts, zx_status_t, Status, [OK => ZX_OK = 0; ...]);
// Note that the actual name of the inner macro (e.g. `bitflags`) can't be a variable.
// It'll just have to be matched on manually
macro_rules! multiconst {
    ($typename:ident, [$($(#[$attr:meta])* $rawname:ident = $value:expr;)*]) => {
        $(
            $(#[$attr])*
            pub const $rawname: $typename = $value;
        )*
    }
}

multiconst!(zx_handle_t, [
    ZX_HANDLE_INVALID = 0;
]);

multiconst!(zx_handle_op_t, [
    ZX_HANDLE_OP_MOVE = 0;
    ZX_HANDLE_OP_DUPLICATE = 1;
]);

multiconst!(zx_koid_t, [
    ZX_KOID_INVALID = 0;
    ZX_KOID_KERNEL = 1;
    ZX_KOID_FIRST = 1024;
]);

multiconst!(zx_time_t, [
    ZX_TIME_INFINITE = i64::MAX;
    ZX_TIME_INFINITE_PAST = ::std::i64::MIN;
]);

multiconst!(zx_rights_t, [
    ZX_RIGHT_NONE           = 0;
    ZX_RIGHT_DUPLICATE      = 1 << 0;
    ZX_RIGHT_TRANSFER       = 1 << 1;
    ZX_RIGHT_READ           = 1 << 2;
    ZX_RIGHT_WRITE          = 1 << 3;
    ZX_RIGHT_EXECUTE        = 1 << 4;
    ZX_RIGHT_MAP            = 1 << 5;
    ZX_RIGHT_GET_PROPERTY   = 1 << 6;
    ZX_RIGHT_SET_PROPERTY   = 1 << 7;
    ZX_RIGHT_ENUMERATE      = 1 << 8;
    ZX_RIGHT_DESTROY        = 1 << 9;
    ZX_RIGHT_SET_POLICY     = 1 << 10;
    ZX_RIGHT_GET_POLICY     = 1 << 11;
    ZX_RIGHT_SIGNAL         = 1 << 12;
    ZX_RIGHT_SIGNAL_PEER    = 1 << 13;
    ZX_RIGHT_WAIT           = 1 << 14;
    ZX_RIGHT_INSPECT        = 1 << 15;
    ZX_RIGHT_MANAGE_JOB     = 1 << 16;
    ZX_RIGHT_MANAGE_PROCESS = 1 << 17;
    ZX_RIGHT_MANAGE_THREAD  = 1 << 18;
    ZX_RIGHT_APPLY_PROFILE  = 1 << 19;
    ZX_RIGHT_MANAGE_SOCKET  = 1 << 20;
    ZX_RIGHT_OP_CHILDREN    = 1 << 21;
    ZX_RIGHT_RESIZE         = 1 << 22;
    ZX_RIGHT_ATTACH_VMO     = 1 << 23;
    ZX_RIGHT_MANAGE_VMO     = 1 << 24;
    ZX_RIGHT_SAME_RIGHTS    = 1 << 31;
]);

multiconst!(u32, [
    ZX_VMO_RESIZABLE = 1 << 1;
    ZX_VMO_DISCARDABLE = 1 << 2;
    ZX_VMO_TRAP_DIRTY = 1 << 3;
    ZX_VMO_UNBOUNDED = 1 << 4;
]);

multiconst!(u64, [
    ZX_VMO_DIRTY_RANGE_IS_ZERO = 1;
]);

multiconst!(u32, [
    ZX_INFO_VMO_TYPE_PAGED = 1 << 0;
    ZX_INFO_VMO_RESIZABLE = 1 << 1;
    ZX_INFO_VMO_IS_COW_CLONE = 1 << 2;
    ZX_INFO_VMO_VIA_HANDLE = 1 << 3;
    ZX_INFO_VMO_VIA_MAPPING = 1 << 4;
    ZX_INFO_VMO_PAGER_BACKED = 1 << 5;
    ZX_INFO_VMO_CONTIGUOUS = 1 << 6;
    ZX_INFO_VMO_DISCARDABLE = 1 << 7;
    ZX_INFO_VMO_IMMUTABLE = 1 << 8;
    ZX_INFO_VMO_VIA_IOB_HANDLE = 1 << 9;
]);

// TODO: add an alias for this type in the C headers.
multiconst!(u32, [
    ZX_VMO_OP_COMMIT = 1;
    ZX_VMO_OP_DECOMMIT = 2;
    ZX_VMO_OP_LOCK = 3;
    ZX_VMO_OP_UNLOCK = 4;
    ZX_VMO_OP_CACHE_SYNC = 6;
    ZX_VMO_OP_CACHE_INVALIDATE = 7;
    ZX_VMO_OP_CACHE_CLEAN = 8;
    ZX_VMO_OP_CACHE_CLEAN_INVALIDATE = 9;
    ZX_VMO_OP_ZERO = 10;
    ZX_VMO_OP_TRY_LOCK = 11;
    ZX_VMO_OP_DONT_NEED = 12;
    ZX_VMO_OP_ALWAYS_NEED = 13;
    ZX_VMO_OP_PREFETCH = 14;
]);

multiconst!(zx_vm_option_t, [
    ZX_VM_PERM_READ                    = 1 << 0;
    ZX_VM_PERM_WRITE                   = 1 << 1;
    ZX_VM_PERM_EXECUTE                 = 1 << 2;
    ZX_VM_COMPACT                      = 1 << 3;
    ZX_VM_SPECIFIC                     = 1 << 4;
    ZX_VM_SPECIFIC_OVERWRITE           = 1 << 5;
    ZX_VM_CAN_MAP_SPECIFIC             = 1 << 6;
    ZX_VM_CAN_MAP_READ                 = 1 << 7;
    ZX_VM_CAN_MAP_WRITE                = 1 << 8;
    ZX_VM_CAN_MAP_EXECUTE              = 1 << 9;
    ZX_VM_MAP_RANGE                    = 1 << 10;
    ZX_VM_REQUIRE_NON_RESIZABLE        = 1 << 11;
    ZX_VM_ALLOW_FAULTS                 = 1 << 12;
    ZX_VM_OFFSET_IS_UPPER_LIMIT        = 1 << 13;
    ZX_VM_PERM_READ_IF_XOM_UNSUPPORTED = 1 << 14;

    // VM alignment options
    ZX_VM_ALIGN_BASE                   = 24;
    ZX_VM_ALIGN_1KB                    = 10 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_2KB                    = 11 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_4KB                    = 12 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_8KB                    = 13 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_16KB                   = 14 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_32KB                   = 15 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_64KB                   = 16 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_128KB                  = 17 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_256KB                  = 18 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_512KB                  = 19 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_1MB                    = 20 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_2MB                    = 21 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_4MB                    = 22 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_8MB                    = 23 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_16MB                   = 24 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_32MB                   = 25 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_64MB                   = 26 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_128MB                  = 27 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_256MB                  = 28 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_512MB                  = 29 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_1GB                    = 30 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_2GB                    = 31 << ZX_VM_ALIGN_BASE;
    ZX_VM_ALIGN_4GB                    = 32 << ZX_VM_ALIGN_BASE;
]);

multiconst!(u32, [
    ZX_PROCESS_SHARED = 1 << 0;
]);

// matches ///zircon/system/public/zircon/errors.h
multiconst!(zx_status_t, [
    ZX_OK                         = 0;
    ZX_ERR_INTERNAL               = -1;
    ZX_ERR_NOT_SUPPORTED          = -2;
    ZX_ERR_NO_RESOURCES           = -3;
    ZX_ERR_NO_MEMORY              = -4;
    ZX_ERR_INTERRUPTED_RETRY      = -6;
    ZX_ERR_INVALID_ARGS           = -10;
    ZX_ERR_BAD_HANDLE             = -11;
    ZX_ERR_WRONG_TYPE             = -12;
    ZX_ERR_BAD_SYSCALL            = -13;
    ZX_ERR_OUT_OF_RANGE           = -14;
    ZX_ERR_BUFFER_TOO_SMALL       = -15;
    ZX_ERR_BAD_STATE              = -20;
    ZX_ERR_TIMED_OUT              = -21;
    ZX_ERR_SHOULD_WAIT            = -22;
    ZX_ERR_CANCELED               = -23;
    ZX_ERR_PEER_CLOSED            = -24;
    ZX_ERR_NOT_FOUND              = -25;
    ZX_ERR_ALREADY_EXISTS         = -26;
    ZX_ERR_ALREADY_BOUND          = -27;
    ZX_ERR_UNAVAILABLE            = -28;
    ZX_ERR_ACCESS_DENIED          = -30;
    ZX_ERR_IO                     = -40;
    ZX_ERR_IO_REFUSED             = -41;
    ZX_ERR_IO_DATA_INTEGRITY      = -42;
    ZX_ERR_IO_DATA_LOSS           = -43;
    ZX_ERR_IO_NOT_PRESENT         = -44;
    ZX_ERR_IO_OVERRUN             = -45;
    ZX_ERR_IO_MISSED_DEADLINE     = -46;
    ZX_ERR_IO_INVALID             = -47;
    ZX_ERR_BAD_PATH               = -50;
    ZX_ERR_NOT_DIR                = -51;
    ZX_ERR_NOT_FILE               = -52;
    ZX_ERR_FILE_BIG               = -53;
    ZX_ERR_NO_SPACE               = -54;
    ZX_ERR_NOT_EMPTY              = -55;
    ZX_ERR_STOP                   = -60;
    ZX_ERR_NEXT                   = -61;
    ZX_ERR_ASYNC                  = -62;
    ZX_ERR_PROTOCOL_NOT_SUPPORTED = -70;
    ZX_ERR_ADDRESS_UNREACHABLE    = -71;
    ZX_ERR_ADDRESS_IN_USE         = -72;
    ZX_ERR_NOT_CONNECTED          = -73;
    ZX_ERR_CONNECTION_REFUSED     = -74;
    ZX_ERR_CONNECTION_RESET       = -75;
    ZX_ERR_CONNECTION_ABORTED     = -76;
]);

multiconst!(zx_signals_t, [
    ZX_SIGNAL_NONE              = 0;
    ZX_OBJECT_SIGNAL_ALL        = 0x00ffffff;
    ZX_USER_SIGNAL_ALL          = 0xff000000;
    ZX_OBJECT_SIGNAL_0          = 1 << 0;
    ZX_OBJECT_SIGNAL_1          = 1 << 1;
    ZX_OBJECT_SIGNAL_2          = 1 << 2;
    ZX_OBJECT_SIGNAL_3          = 1 << 3;
    ZX_OBJECT_SIGNAL_4          = 1 << 4;
    ZX_OBJECT_SIGNAL_5          = 1 << 5;
    ZX_OBJECT_SIGNAL_6          = 1 << 6;
    ZX_OBJECT_SIGNAL_7          = 1 << 7;
    ZX_OBJECT_SIGNAL_8          = 1 << 8;
    ZX_OBJECT_SIGNAL_9          = 1 << 9;
    ZX_OBJECT_SIGNAL_10         = 1 << 10;
    ZX_OBJECT_SIGNAL_11         = 1 << 11;
    ZX_OBJECT_SIGNAL_12         = 1 << 12;
    ZX_OBJECT_SIGNAL_13         = 1 << 13;
    ZX_OBJECT_SIGNAL_14         = 1 << 14;
    ZX_OBJECT_SIGNAL_15         = 1 << 15;
    ZX_OBJECT_SIGNAL_16         = 1 << 16;
    ZX_OBJECT_SIGNAL_17         = 1 << 17;
    ZX_OBJECT_SIGNAL_18         = 1 << 18;
    ZX_OBJECT_SIGNAL_19         = 1 << 19;
    ZX_OBJECT_SIGNAL_20         = 1 << 20;
    ZX_OBJECT_SIGNAL_21         = 1 << 21;
    ZX_OBJECT_SIGNAL_22         = 1 << 22;
    ZX_OBJECT_HANDLE_CLOSED     = 1 << 23;
    ZX_USER_SIGNAL_0            = 1 << 24;
    ZX_USER_SIGNAL_1            = 1 << 25;
    ZX_USER_SIGNAL_2            = 1 << 26;
    ZX_USER_SIGNAL_3            = 1 << 27;
    ZX_USER_SIGNAL_4            = 1 << 28;
    ZX_USER_SIGNAL_5            = 1 << 29;
    ZX_USER_SIGNAL_6            = 1 << 30;
    ZX_USER_SIGNAL_7            = 1 << 31;

    ZX_OBJECT_READABLE          = ZX_OBJECT_SIGNAL_0;
    ZX_OBJECT_WRITABLE          = ZX_OBJECT_SIGNAL_1;
    ZX_OBJECT_PEER_CLOSED       = ZX_OBJECT_SIGNAL_2;

    // Cancelation (handle was closed while waiting with it)
    ZX_SIGNAL_HANDLE_CLOSED     = ZX_OBJECT_HANDLE_CLOSED;

    // Event
    ZX_EVENT_SIGNALED           = ZX_OBJECT_SIGNAL_3;

    // EventPair
    ZX_EVENTPAIR_SIGNALED       = ZX_OBJECT_SIGNAL_3;
    ZX_EVENTPAIR_PEER_CLOSED    = ZX_OBJECT_SIGNAL_2;

    // Task signals (process, thread, job)
    ZX_TASK_TERMINATED          = ZX_OBJECT_SIGNAL_3;

    // Channel
    ZX_CHANNEL_READABLE         = ZX_OBJECT_SIGNAL_0;
    ZX_CHANNEL_WRITABLE         = ZX_OBJECT_SIGNAL_1;
    ZX_CHANNEL_PEER_CLOSED      = ZX_OBJECT_SIGNAL_2;

    // Clock
    ZX_CLOCK_STARTED            = ZX_OBJECT_SIGNAL_4;
    ZX_CLOCK_UPDATED            = ZX_OBJECT_SIGNAL_5;

    // Socket
    ZX_SOCKET_READABLE            = ZX_OBJECT_READABLE;
    ZX_SOCKET_WRITABLE            = ZX_OBJECT_WRITABLE;
    ZX_SOCKET_PEER_CLOSED         = ZX_OBJECT_PEER_CLOSED;
    ZX_SOCKET_PEER_WRITE_DISABLED = ZX_OBJECT_SIGNAL_4;
    ZX_SOCKET_WRITE_DISABLED      = ZX_OBJECT_SIGNAL_5;
    ZX_SOCKET_READ_THRESHOLD      = ZX_OBJECT_SIGNAL_10;
    ZX_SOCKET_WRITE_THRESHOLD     = ZX_OBJECT_SIGNAL_11;

    // Resource
    ZX_RESOURCE_DESTROYED       = ZX_OBJECT_SIGNAL_3;
    ZX_RESOURCE_READABLE        = ZX_OBJECT_READABLE;
    ZX_RESOURCE_WRITABLE        = ZX_OBJECT_WRITABLE;
    ZX_RESOURCE_CHILD_ADDED     = ZX_OBJECT_SIGNAL_4;

    // Fifo
    ZX_FIFO_READABLE            = ZX_OBJECT_READABLE;
    ZX_FIFO_WRITABLE            = ZX_OBJECT_WRITABLE;
    ZX_FIFO_PEER_CLOSED         = ZX_OBJECT_PEER_CLOSED;

    // Job
    ZX_JOB_TERMINATED           = ZX_OBJECT_SIGNAL_3;
    ZX_JOB_NO_JOBS              = ZX_OBJECT_SIGNAL_4;
    ZX_JOB_NO_PROCESSES         = ZX_OBJECT_SIGNAL_5;

    // Process
    ZX_PROCESS_TERMINATED       = ZX_OBJECT_SIGNAL_3;

    // Thread
    ZX_THREAD_TERMINATED        = ZX_OBJECT_SIGNAL_3;
    ZX_THREAD_RUNNING           = ZX_OBJECT_SIGNAL_4;
    ZX_THREAD_SUSPENDED         = ZX_OBJECT_SIGNAL_5;

    // Log
    ZX_LOG_READABLE             = ZX_OBJECT_READABLE;
    ZX_LOG_WRITABLE             = ZX_OBJECT_WRITABLE;

    // Timer
    ZX_TIMER_SIGNALED           = ZX_OBJECT_SIGNAL_3;

    // Vmo
    ZX_VMO_ZERO_CHILDREN        = ZX_OBJECT_SIGNAL_3;
]);

multiconst!(zx_obj_type_t, [
    ZX_OBJ_TYPE_NONE                = 0;
    ZX_OBJ_TYPE_PROCESS             = 1;
    ZX_OBJ_TYPE_THREAD              = 2;
    ZX_OBJ_TYPE_VMO                 = 3;
    ZX_OBJ_TYPE_CHANNEL             = 4;
    ZX_OBJ_TYPE_EVENT               = 5;
    ZX_OBJ_TYPE_PORT                = 6;
    ZX_OBJ_TYPE_INTERRUPT           = 9;
    ZX_OBJ_TYPE_PCI_DEVICE          = 11;
    ZX_OBJ_TYPE_DEBUGLOG            = 12;
    ZX_OBJ_TYPE_SOCKET              = 14;
    ZX_OBJ_TYPE_RESOURCE            = 15;
    ZX_OBJ_TYPE_EVENTPAIR           = 16;
    ZX_OBJ_TYPE_JOB                 = 17;
    ZX_OBJ_TYPE_VMAR                = 18;
    ZX_OBJ_TYPE_FIFO                = 19;
    ZX_OBJ_TYPE_GUEST               = 20;
    ZX_OBJ_TYPE_VCPU                = 21;
    ZX_OBJ_TYPE_TIMER               = 22;
    ZX_OBJ_TYPE_IOMMU               = 23;
    ZX_OBJ_TYPE_BTI                 = 24;
    ZX_OBJ_TYPE_PROFILE             = 25;
    ZX_OBJ_TYPE_PMT                 = 26;
    ZX_OBJ_TYPE_SUSPEND_TOKEN       = 27;
    ZX_OBJ_TYPE_PAGER               = 28;
    ZX_OBJ_TYPE_EXCEPTION           = 29;
    ZX_OBJ_TYPE_CLOCK               = 30;
    ZX_OBJ_TYPE_STREAM              = 31;
    ZX_OBJ_TYPE_MSI                 = 32;
    ZX_OBJ_TYPE_IOB                 = 33;
]);

// System ABI commits to having no more than 64 object types.
//
// See zx_info_process_handle_stats_t for an example of a binary interface that
// depends on having an upper bound for the number of object types.
pub const ZX_OBJ_TYPE_UPPER_BOUND: usize = 64;

// TODO: add an alias for this type in the C headers.
multiconst!(u32, [
    // Argument is a char[ZX_MAX_NAME_LEN].
    ZX_PROP_NAME                      = 3;

    // Argument is a uintptr_t.
    #[cfg(target_arch = "x86_64")]
    ZX_PROP_REGISTER_GS               = 2;
    #[cfg(target_arch = "x86_64")]
    ZX_PROP_REGISTER_FS               = 4;

    // Argument is the value of ld.so's _dl_debug_addr, a uintptr_t.
    ZX_PROP_PROCESS_DEBUG_ADDR        = 5;

    // Argument is the base address of the vDSO mapping (or zero), a uintptr_t.
    ZX_PROP_PROCESS_VDSO_BASE_ADDRESS = 6;

    // Whether the dynamic loader should issue a debug trap when loading a shared
    // library, either initially or when running (e.g. dlopen).
    ZX_PROP_PROCESS_BREAK_ON_LOAD = 7;

    // Argument is a size_t.
    ZX_PROP_SOCKET_RX_THRESHOLD       = 12;
    ZX_PROP_SOCKET_TX_THRESHOLD       = 13;

    // Argument is a size_t, describing the number of packets a channel
    // endpoint can have pending in its tx direction.
    ZX_PROP_CHANNEL_TX_MSG_MAX        = 14;

    // Terminate this job if the system is low on memory.
    ZX_PROP_JOB_KILL_ON_OOM           = 15;

    // Exception close behavior.
    ZX_PROP_EXCEPTION_STATE           = 16;

    // The size of the content in a VMO, in bytes.
    ZX_PROP_VMO_CONTENT_SIZE          = 17;

    // How an exception should be handled.
    ZX_PROP_EXCEPTION_STRATEGY        = 18;

    // Whether the stream is in append mode or not.
    ZX_PROP_STREAM_MODE_APPEND        = 19;
]);

// Value for ZX_THREAD_STATE_SINGLE_STEP. The value can be 0 (not single-stepping), or 1
// (single-stepping). Other values will give ZX_ERR_INVALID_ARGS.
pub type zx_thread_state_single_step_t = u32;

// Possible values for "kind" in zx_thread_read_state and zx_thread_write_state.
multiconst!(zx_thread_state_topic_t, [
    ZX_THREAD_STATE_GENERAL_REGS       = 0;
    ZX_THREAD_STATE_FP_REGS            = 1;
    ZX_THREAD_STATE_VECTOR_REGS        = 2;
    // No 3 at the moment.
    ZX_THREAD_STATE_DEBUG_REGS         = 4;
    ZX_THREAD_STATE_SINGLE_STEP        = 5;
]);

// Possible values for "kind" in zx_vcpu_read_state and zx_vcpu_write_state.
multiconst!(zx_vcpu_state_topic_t, [
    ZX_VCPU_STATE   = 0;
    ZX_VCPU_IO      = 1;
]);

// From //zircon/system/public/zircon/syscalls/resource.h
multiconst!(zx_rsrc_kind_t, [
    ZX_RSRC_KIND_MMIO       = 0;
    ZX_RSRC_KIND_IRQ        = 1;
    ZX_RSRC_KIND_IOPORT     = 2;
    ZX_RSRC_KIND_ROOT       = 3;
    ZX_RSRC_KIND_SMC        = 4;
    ZX_RSRC_KIND_SYSTEM     = 5;
]);

// From //zircon/system/public/zircon/syscalls/resource.h
multiconst!(zx_rsrc_system_base_t, [
    ZX_RSRC_SYSTEM_HYPERVISOR_BASE  = 0;
    ZX_RSRC_SYSTEM_VMEX_BASE        = 1;
    ZX_RSRC_SYSTEM_DEBUG_BASE       = 2;
    ZX_RSRC_SYSTEM_INFO_BASE        = 3;
    ZX_RSRC_SYSTEM_CPU_BASE         = 4;
    ZX_RSRC_SYSTEM_POWER_BASE       = 5;
    ZX_RSRC_SYSTEM_MEXEC_BASE       = 6;
    ZX_RSRC_SYSTEM_ENERGY_INFO_BASE = 7;
    ZX_RSRC_SYSTEM_IOMMU_BASE       = 8;
    ZX_RSRC_SYSTEM_FRAMEBUFFER_BASE = 9;
    ZX_RSRC_SYSTEM_PROFILE_BASE     = 10;
    ZX_RSRC_SYSTEM_MSI_BASE         = 11;
    ZX_RSRC_SYSTEM_DEBUGLOG_BASE    = 12;
]);

// clock ids
multiconst!(zx_clock_t, [
    ZX_CLOCK_MONOTONIC    = 0;
]);

// from //zircon/system/public/zircon/syscalls/clock.h
multiconst!(u64, [
    ZX_CLOCK_OPT_MONOTONIC = 1 << 0;
    ZX_CLOCK_OPT_CONTINUOUS = 1 << 1;
    ZX_CLOCK_OPT_AUTO_START = 1 << 2;
    ZX_CLOCK_OPT_BOOT = 1 << 3;

    // v1 clock update flags
    ZX_CLOCK_UPDATE_OPTION_VALUE_VALID = 1 << 0;
    ZX_CLOCK_UPDATE_OPTION_RATE_ADJUST_VALID = 1 << 1;
    ZX_CLOCK_UPDATE_OPTION_ERROR_BOUND_VALID = 1 << 2;

    // Additional v2 clock update flags
    ZX_CLOCK_UPDATE_OPTION_REFERENCE_VALUE_VALID = 1 << 3;
    ZX_CLOCK_UPDATE_OPTION_SYNTHETIC_VALUE_VALID = ZX_CLOCK_UPDATE_OPTION_VALUE_VALID;

    ZX_CLOCK_ARGS_VERSION_1 = 1 << 58;
    ZX_CLOCK_ARGS_VERSION_2 = 2 << 58;
]);

// from //zircon/system/public/zircon/syscalls/exception.h
multiconst!(u32, [
    ZX_EXCEPTION_CHANNEL_DEBUGGER = 1 << 0;
    ZX_EXCEPTION_TARGET_JOB_DEBUGGER = 1 << 0;

    // Returned when probing a thread for its blocked state.
    ZX_EXCEPTION_CHANNEL_TYPE_NONE = 0;
    ZX_EXCEPTION_CHANNEL_TYPE_DEBUGGER = 1;
    ZX_EXCEPTION_CHANNEL_TYPE_THREAD = 2;
    ZX_EXCEPTION_CHANNEL_TYPE_PROCESS = 3;
    ZX_EXCEPTION_CHANNEL_TYPE_JOB = 4;
    ZX_EXCEPTION_CHANNEL_TYPE_JOB_DEBUGGER = 5;
]);

/// A byte used only to control memory alignment. All padding bytes are considered equal
/// regardless of their content.
///
/// Note that the kernel C/C++ struct definitions use explicit padding fields to ensure no implicit
/// padding is added. This is important for security since implicit padding bytes are not always
/// safely initialized. These explicit padding fields are mirrored in the Rust struct definitions
/// to minimize the opportunities for mistakes and inconsistencies.
#[repr(transparent)]
#[derive(Copy, Clone, Eq, Default)]
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes))]
pub struct PadByte(u8);

impl PartialEq for PadByte {
    fn eq(&self, _other: &Self) -> bool {
        true
    }
}

impl Hash for PadByte {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u8(0);
    }
}

impl Debug for PadByte {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("-")
    }
}

#[repr(C)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct zx_clock_create_args_v1_t {
    pub backstop_time: zx_time_t,
}

#[repr(C)]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct zx_clock_rate_t {
    pub synthetic_ticks: u32,
    pub reference_ticks: u32,
}

#[repr(C)]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct zx_clock_transformation_t {
    pub reference_offset: i64,
    pub synthetic_offset: i64,
    pub rate: zx_clock_rate_t,
}

#[repr(C)]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct zx_clock_details_v1_t {
    pub options: u64,
    pub backstop_time: zx_time_t,
    pub reference_ticks_to_synthetic: zx_clock_transformation_t,
    pub reference_to_synthetic: zx_clock_transformation_t,
    pub error_bound: u64,
    pub query_ticks: zx_ticks_t,
    pub last_value_update_ticks: zx_ticks_t,
    pub last_rate_adjust_update_ticks: zx_ticks_t,
    pub last_error_bounds_update_ticks: zx_ticks_t,
    pub generation_counter: u32,
    padding1: [PadByte; 4],
}

#[repr(C)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct zx_clock_update_args_v1_t {
    pub rate_adjust: i32,
    padding1: [PadByte; 4],
    pub value: i64,
    pub error_bound: u64,
}

#[repr(C)]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct zx_clock_update_args_v2_t {
    pub rate_adjust: i32,
    padding1: [PadByte; 4],
    pub synthetic_value: i64,
    pub reference_value: i64,
    pub error_bound: u64,
}

multiconst!(zx_stream_seek_origin_t, [
    ZX_STREAM_SEEK_ORIGIN_START        = 0;
    ZX_STREAM_SEEK_ORIGIN_CURRENT      = 1;
    ZX_STREAM_SEEK_ORIGIN_END          = 2;
]);

// Stream constants
pub const ZX_STREAM_MODE_READ: u32 = 1 << 0;
pub const ZX_STREAM_MODE_WRITE: u32 = 1 << 1;
pub const ZX_STREAM_MODE_APPEND: u32 = 1 << 2;

pub const ZX_STREAM_APPEND: u32 = 1 << 0;

pub const ZX_CPRNG_ADD_ENTROPY_MAX_LEN: usize = 256;

// Socket flags and limits.
pub const ZX_SOCKET_STREAM: u32 = 0;
pub const ZX_SOCKET_DATAGRAM: u32 = 1 << 0;
pub const ZX_SOCKET_DISPOSITION_WRITE_DISABLED: u32 = 1 << 0;
pub const ZX_SOCKET_DISPOSITION_WRITE_ENABLED: u32 = 1 << 1;

// VM Object clone flags
pub const ZX_VMO_CHILD_SNAPSHOT: u32 = 1 << 0;
pub const ZX_VMO_CHILD_SNAPSHOT_AT_LEAST_ON_WRITE: u32 = 1 << 4;
pub const ZX_VMO_CHILD_RESIZABLE: u32 = 1 << 2;
pub const ZX_VMO_CHILD_SLICE: u32 = 1 << 3;
pub const ZX_VMO_CHILD_NO_WRITE: u32 = 1 << 5;
pub const ZX_VMO_CHILD_REFERENCE: u32 = 1 << 6;
pub const ZX_VMO_CHILD_SNAPSHOT_MODIFIED: u32 = 1 << 7;

// channel write size constants
pub const ZX_CHANNEL_MAX_MSG_HANDLES: u32 = 64;
pub const ZX_CHANNEL_MAX_MSG_BYTES: u32 = 65536;
pub const ZX_CHANNEL_MAX_MSG_IOVEC: u32 = 8192;

// fifo write size constants
pub const ZX_FIFO_MAX_SIZE_BYTES: u32 = 4096;

// Min/max page size constants
#[cfg(target_arch = "x86_64")]
pub const ZX_MIN_PAGE_SHIFT: u32 = 12;
#[cfg(target_arch = "x86_64")]
pub const ZX_MAX_PAGE_SHIFT: u32 = 21;

#[cfg(target_arch = "aarch64")]
pub const ZX_MIN_PAGE_SHIFT: u32 = 12;
#[cfg(target_arch = "aarch64")]
pub const ZX_MAX_PAGE_SHIFT: u32 = 16;

#[cfg(target_arch = "riscv64")]
pub const ZX_MIN_PAGE_SHIFT: u32 = 12;
#[cfg(target_arch = "riscv64")]
pub const ZX_MAX_PAGE_SHIFT: u32 = 21;

// Task response codes if a process is externally killed
pub const ZX_TASK_RETCODE_SYSCALL_KILL: i64 = -1024;
pub const ZX_TASK_RETCODE_OOM_KILL: i64 = -1025;
pub const ZX_TASK_RETCODE_POLICY_KILL: i64 = -1026;
pub const ZX_TASK_RETCODE_VDSO_KILL: i64 = -1027;
pub const ZX_TASK_RETCODE_EXCEPTION_KILL: i64 = -1028;

// Resource flags.
pub const ZX_RSRC_FLAG_EXCLUSIVE: zx_rsrc_flags_t = 0x00010000;

// Topics for CPU performance info syscalls
pub const ZX_CPU_PERF_SCALE: u32 = 1;
pub const ZX_CPU_DEFAULT_PERF_SCALE: u32 = 2;
pub const ZX_CPU_POWER_LIMIT: u32 = 3;

// Cache policy flags.
pub const ZX_CACHE_POLICY_CACHED: u32 = 0;
pub const ZX_CACHE_POLICY_UNCACHED: u32 = 1;
pub const ZX_CACHE_POLICY_UNCACHED_DEVICE: u32 = 2;
pub const ZX_CACHE_POLICY_WRITE_COMBINING: u32 = 3;

// Flag bits for zx_cache_flush.
multiconst!(u32, [
    ZX_CACHE_FLUSH_INSN         = 1 << 0;
    ZX_CACHE_FLUSH_DATA         = 1 << 1;
    ZX_CACHE_FLUSH_INVALIDATE   = 1 << 2;
]);

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_wait_item_t {
    pub handle: zx_handle_t,
    pub waitfor: zx_signals_t,
    pub pending: zx_signals_t,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_waitset_result_t {
    pub cookie: u64,
    pub status: zx_status_t,
    pub observed: zx_signals_t,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_handle_info_t {
    pub handle: zx_handle_t,
    pub ty: zx_obj_type_t,
    pub rights: zx_rights_t,
    pub unused: u32,
}

pub const ZX_CHANNEL_READ_MAY_DISCARD: u32 = 1;
pub const ZX_CHANNEL_WRITE_USE_IOVEC: u32 = 2;

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_channel_call_args_t {
    pub wr_bytes: *const u8,
    pub wr_handles: *const zx_handle_t,
    pub rd_bytes: *mut u8,
    pub rd_handles: *mut zx_handle_t,
    pub wr_num_bytes: u32,
    pub wr_num_handles: u32,
    pub rd_num_bytes: u32,
    pub rd_num_handles: u32,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_channel_call_etc_args_t {
    pub wr_bytes: *const u8,
    pub wr_handles: *mut zx_handle_disposition_t,
    pub rd_bytes: *mut u8,
    pub rd_handles: *mut zx_handle_info_t,
    pub wr_num_bytes: u32,
    pub wr_num_handles: u32,
    pub rd_num_bytes: u32,
    pub rd_num_handles: u32,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_channel_iovec_t {
    pub buffer: *const u8,
    pub capacity: u32,
    padding1: [PadByte; 4],
}

impl Default for zx_channel_iovec_t {
    fn default() -> Self {
        Self {
            buffer: std::ptr::null(),
            capacity: Default::default(),
            padding1: Default::default(),
        }
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_handle_disposition_t {
    pub operation: zx_handle_op_t,
    pub handle: zx_handle_t,
    pub type_: zx_obj_type_t,
    pub rights: zx_rights_t,
    pub result: zx_status_t,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct zx_iovec_t {
    pub buffer: *const u8,
    pub capacity: usize,
}

pub type zx_pci_irq_swizzle_lut_t = [[[u32; 4]; 8]; 32];

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_pci_init_arg_t {
    pub dev_pin_to_global_irq: zx_pci_irq_swizzle_lut_t,
    pub num_irqs: u32,
    pub irqs: [zx_irq_t; 32],
    pub ecam_window_count: u32,
    // Note: the ecam_windows field is actually a variable size array.
    // We use a fixed size array to match the C repr.
    pub ecam_windows: [zx_ecam_window_t; 1],
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_irq_t {
    pub global_irq: u32,
    pub level_triggered: bool,
    pub active_high: bool,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_ecam_window_t {
    pub base: u64,
    pub size: usize,
    pub bus_start: u8,
    pub bus_end: u8,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_pcie_device_info_t {
    pub vendor_id: u16,
    pub device_id: u16,
    pub base_class: u8,
    pub sub_class: u8,
    pub program_interface: u8,
    pub revision_id: u8,
    pub bus_id: u8,
    pub dev_id: u8,
    pub func_id: u8,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_pci_resource_t {
    pub type_: u32,
    pub size: usize,
    // TODO: Actually a union
    pub pio_addr: usize,
}

// TODO: Actually a union
pub type zx_rrec_t = [u8; 64];

// Ports V2
#[repr(u32)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum zx_packet_type_t {
    ZX_PKT_TYPE_USER = 0,
    ZX_PKT_TYPE_SIGNAL_ONE = 1,
    ZX_PKT_TYPE_GUEST_BELL = 3,
    ZX_PKT_TYPE_GUEST_MEM = 4,
    ZX_PKT_TYPE_GUEST_IO = 5,
    ZX_PKT_TYPE_GUEST_VCPU = 6,
    ZX_PKT_TYPE_INTERRUPT = 7,
    ZX_PKT_TYPE_PAGE_REQUEST = 9,
    ZX_PKT_TYPE_PROCESSOR_POWER_LEVEL_TRANSITION_REQUEST = 10,
    #[doc(hidden)]
    __Nonexhaustive,
}

impl Default for zx_packet_type_t {
    fn default() -> Self {
        zx_packet_type_t::ZX_PKT_TYPE_USER
    }
}

#[repr(u32)]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
pub enum zx_packet_guest_vcpu_type_t {
    #[default]
    ZX_PKT_GUEST_VCPU_INTERRUPT = 0,
    ZX_PKT_GUEST_VCPU_STARTUP = 1,
    ZX_PKT_GUEST_VCPU_EXIT = 2,
    #[doc(hidden)]
    __Nonexhaustive,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct zx_packet_signal_t {
    pub trigger: zx_signals_t,
    pub observed: zx_signals_t,
    pub count: u64,
    pub timestamp: zx_time_t,
}

pub const ZX_WAIT_ASYNC_TIMESTAMP: u32 = 1;
pub const ZX_WAIT_ASYNC_EDGE: u32 = 2;
pub const ZX_WAIT_ASYNC_BOOT_TIMESTAMP: u32 = 4;

// Actually a union of different integer types, but this should be good enough.
pub type zx_packet_user_t = [u8; 32];

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_port_packet_t {
    pub key: u64,
    pub packet_type: zx_packet_type_t,
    pub status: i32,
    pub union: [u8; 32],
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_packet_guest_bell_t {
    pub addr: zx_gpaddr_t,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_packet_guest_io_t {
    pub port: u16,
    pub access_size: u8,
    pub input: bool,
    pub data: [u8; 4],
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
pub struct zx_packet_guest_vcpu_interrupt_t {
    pub mask: u64,
    pub vector: u8,
    padding1: [PadByte; 7],
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
pub struct zx_packet_guest_vcpu_startup_t {
    pub id: u64,
    pub entry: zx_gpaddr_t,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
pub struct zx_packet_guest_vcpu_exit_t {
    pub retcode: i64,
    padding1: [PadByte; 8],
}

#[repr(C)]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
pub union zx_packet_guest_vcpu_union_t {
    pub interrupt: zx_packet_guest_vcpu_interrupt_t,
    pub startup: zx_packet_guest_vcpu_startup_t,
    pub exit: zx_packet_guest_vcpu_exit_t,
}

#[cfg(feature = "zerocopy")]
impl Default for zx_packet_guest_vcpu_union_t {
    fn default() -> Self {
        Self::new_zeroed()
    }
}

#[repr(C)]
#[derive(Copy, Clone, Default)]
pub struct zx_packet_guest_vcpu_t {
    pub r#type: zx_packet_guest_vcpu_type_t,
    padding1: [PadByte; 4],
    pub union: zx_packet_guest_vcpu_union_t,
    padding2: [PadByte; 8],
}

impl PartialEq for zx_packet_guest_vcpu_t {
    fn eq(&self, other: &Self) -> bool {
        if self.r#type != other.r#type {
            return false;
        }
        match self.r#type {
            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_INTERRUPT => unsafe {
                self.union.interrupt == other.union.interrupt
            },
            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_STARTUP => unsafe {
                self.union.startup == other.union.startup
            },
            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_EXIT => unsafe {
                self.union.exit == other.union.exit
            },
            // No equality relationship is defined for invalid types.
            _ => false,
        }
    }
}

impl Eq for zx_packet_guest_vcpu_t {}

impl Debug for zx_packet_guest_vcpu_t {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.r#type {
            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_INTERRUPT => {
                write!(f, "type: {:?} union: {:?}", self.r#type, unsafe { self.union.interrupt })
            }
            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_STARTUP => {
                write!(f, "type: {:?} union: {:?}", self.r#type, unsafe { self.union.startup })
            }
            zx_packet_guest_vcpu_type_t::ZX_PKT_GUEST_VCPU_EXIT => {
                write!(f, "type: {:?} union: {:?}", self.r#type, unsafe { self.union.exit })
            }
            _ => panic!("unexpected VCPU packet type"),
        }
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
pub struct zx_packet_page_request_t {
    pub command: zx_page_request_command_t,
    pub flags: u16,
    padding1: [PadByte; 4],
    pub offset: u64,
    pub length: u64,
    padding2: [PadByte; 8],
}

#[repr(u16)]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
pub enum zx_page_request_command_t {
    #[default]
    ZX_PAGER_VMO_READ = 0x0000,
    ZX_PAGER_VMO_COMPLETE = 0x0001,
    ZX_PAGER_VMO_DIRTY = 0x0002,
    #[doc(hidden)]
    __Nonexhaustive,
}

multiconst!(u32, [
    ZX_PAGER_OP_FAIL = 1;
    ZX_PAGER_OP_DIRTY = 2;
    ZX_PAGER_OP_WRITEBACK_BEGIN = 3;
    ZX_PAGER_OP_WRITEBACK_END = 4;
]);

pub type zx_excp_type_t = u32;

multiconst!(zx_excp_type_t, [
    ZX_EXCP_GENERAL               = 0x008;
    ZX_EXCP_FATAL_PAGE_FAULT      = 0x108;
    ZX_EXCP_UNDEFINED_INSTRUCTION = 0x208;
    ZX_EXCP_SW_BREAKPOINT         = 0x308;
    ZX_EXCP_HW_BREAKPOINT         = 0x408;
    ZX_EXCP_UNALIGNED_ACCESS      = 0x508;

    ZX_EXCP_SYNTH                 = 0x8000;

    ZX_EXCP_THREAD_STARTING       = 0x008 | ZX_EXCP_SYNTH;
    ZX_EXCP_THREAD_EXITING        = 0x108 | ZX_EXCP_SYNTH;
    ZX_EXCP_POLICY_ERROR          = 0x208 | ZX_EXCP_SYNTH;
    ZX_EXCP_PROCESS_STARTING      = 0x308 | ZX_EXCP_SYNTH;
    ZX_EXCP_USER                  = 0x309 | ZX_EXCP_SYNTH;
]);

multiconst!(u32, [
    ZX_EXCP_USER_CODE_PROCESS_NAME_CHANGED = 0x0001;

    ZX_EXCP_USER_CODE_USER0                = 0xF000;
    ZX_EXCP_USER_CODE_USER1                = 0xF001;
    ZX_EXCP_USER_CODE_USER2                = 0xF002;
]);

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes))]
pub struct zx_exception_info_t {
    pub pid: zx_koid_t,
    pub tid: zx_koid_t,
    pub type_: zx_excp_type_t,
    padding1: [PadByte; 4],
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
pub struct zx_x86_64_exc_data_t {
    pub vector: u64,
    pub err_code: u64,
    pub cr2: u64,
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, FromBytes, Immutable)]
pub struct zx_arm64_exc_data_t {
    pub esr: u32,
    padding1: [PadByte; 4],
    pub far: u64,
    padding2: [PadByte; 8],
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, FromBytes, Immutable)]
pub struct zx_riscv64_exc_data_t {
    pub cause: u64,
    pub tval: u64,
    padding1: [PadByte; 8],
}

#[repr(C)]
#[derive(Copy, Clone, KnownLayout, FromBytes, Immutable)]
pub union zx_exception_header_arch_t {
    pub x86_64: zx_x86_64_exc_data_t,
    pub arm_64: zx_arm64_exc_data_t,
    pub riscv_64: zx_riscv64_exc_data_t,
}

impl Debug for zx_exception_header_arch_t {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "zx_exception_header_arch_t")
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
pub struct zx_exception_header_t {
    pub size: u32,
    pub type_: zx_excp_type_t,
}

pub type zx_excp_policy_code_t = u32;

multiconst!(zx_excp_policy_code_t, [
    ZX_EXCP_POLICY_CODE_BAD_HANDLE              = 0;
    ZX_EXCP_POLICY_CODE_WRONG_OBJECT            = 1;
    ZX_EXCP_POLICY_CODE_VMAR_WX                 = 2;
    ZX_EXCP_POLICY_CODE_NEW_ANY                 = 3;
    ZX_EXCP_POLICY_CODE_NEW_VMO                 = 4;
    ZX_EXCP_POLICY_CODE_NEW_CHANNEL             = 5;
    ZX_EXCP_POLICY_CODE_NEW_EVENT               = 6;
    ZX_EXCP_POLICY_CODE_NEW_EVENTPAIR           = 7;
    ZX_EXCP_POLICY_CODE_NEW_PORT                = 8;
    ZX_EXCP_POLICY_CODE_NEW_SOCKET              = 9;
    ZX_EXCP_POLICY_CODE_NEW_FIFO                = 10;
    ZX_EXCP_POLICY_CODE_NEW_TIMER               = 11;
    ZX_EXCP_POLICY_CODE_NEW_PROCESS             = 12;
    ZX_EXCP_POLICY_CODE_NEW_PROFILE             = 13;
    ZX_EXCP_POLICY_CODE_NEW_PAGER               = 14;
    ZX_EXCP_POLICY_CODE_AMBIENT_MARK_VMO_EXEC   = 15;
    ZX_EXCP_POLICY_CODE_CHANNEL_FULL_WRITE      = 16;
    ZX_EXCP_POLICY_CODE_PORT_TOO_MANY_PACKETS   = 17;
    ZX_EXCP_POLICY_CODE_BAD_SYSCALL             = 18;
    ZX_EXCP_POLICY_CODE_PORT_TOO_MANY_OBSERVERS = 19;
    ZX_EXCP_POLICY_CODE_HANDLE_LEAK             = 20;
    ZX_EXCP_POLICY_CODE_NEW_IOB                 = 21;
]);

#[repr(C)]
#[derive(Debug, Copy, Clone, KnownLayout, FromBytes, Immutable)]
pub struct zx_exception_context_t {
    pub arch: zx_exception_header_arch_t,
    pub synth_code: zx_excp_policy_code_t,
    pub synth_data: u32,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, KnownLayout, FromBytes, Immutable)]
pub struct zx_exception_report_t {
    pub header: zx_exception_header_t,
    pub context: zx_exception_context_t,
}

pub type zx_exception_state_t = u32;

multiconst!(zx_exception_state_t, [
    ZX_EXCEPTION_STATE_TRY_NEXT    = 0;
    ZX_EXCEPTION_STATE_HANDLED     = 1;
    ZX_EXCEPTION_STATE_THREAD_EXIT = 2;
]);

pub type zx_exception_strategy_t = u32;

multiconst!(zx_exception_state_t, [
    ZX_EXCEPTION_STRATEGY_FIRST_CHANCE   = 0;
    ZX_EXCEPTION_STRATEGY_SECOND_CHANCE  = 1;
]);

#[cfg(target_arch = "x86_64")]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
pub struct zx_thread_state_general_regs_t {
    pub rax: u64,
    pub rbx: u64,
    pub rcx: u64,
    pub rdx: u64,
    pub rsi: u64,
    pub rdi: u64,
    pub rbp: u64,
    pub rsp: u64,
    pub r8: u64,
    pub r9: u64,
    pub r10: u64,
    pub r11: u64,
    pub r12: u64,
    pub r13: u64,
    pub r14: u64,
    pub r15: u64,
    pub rip: u64,
    pub rflags: u64,
    pub fs_base: u64,
    pub gs_base: u64,
}

#[cfg(target_arch = "x86_64")]
impl From<&zx_restricted_state_t> for zx_thread_state_general_regs_t {
    fn from(state: &zx_restricted_state_t) -> Self {
        Self {
            rdi: state.rdi,
            rsi: state.rsi,
            rbp: state.rbp,
            rbx: state.rbx,
            rdx: state.rdx,
            rcx: state.rcx,
            rax: state.rax,
            rsp: state.rsp,
            r8: state.r8,
            r9: state.r9,
            r10: state.r10,
            r11: state.r11,
            r12: state.r12,
            r13: state.r13,
            r14: state.r14,
            r15: state.r15,
            rip: state.ip,
            rflags: state.flags,
            fs_base: state.fs_base,
            gs_base: state.gs_base,
        }
    }
}

#[cfg(target_arch = "aarch64")]
multiconst!(u64, [
    ZX_REG_CPSR_ARCH_32_MASK = 0x10;
    ZX_REG_CPSR_THUMB_MASK = 0x20;
]);

#[cfg(target_arch = "aarch64")]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_thread_state_general_regs_t {
    pub r: [u64; 30],
    pub lr: u64,
    pub sp: u64,
    pub pc: u64,
    pub cpsr: u64,
    pub tpidr: u64,
}

#[cfg(target_arch = "aarch64")]
impl From<&zx_restricted_state_t> for zx_thread_state_general_regs_t {
    fn from(state: &zx_restricted_state_t) -> Self {
        if state.cpsr as u64 & ZX_REG_CPSR_ARCH_32_MASK == ZX_REG_CPSR_ARCH_32_MASK {
            // aarch32
            Self {
                r: [
                    state.r[0],
                    state.r[1],
                    state.r[2],
                    state.r[3],
                    state.r[4],
                    state.r[5],
                    state.r[6],
                    state.r[7],
                    state.r[8],
                    state.r[9],
                    state.r[10],
                    state.r[11],
                    state.r[12],
                    state.r[13],
                    state.r[14],
                    state.pc, // ELR overwrites this.
                    state.r[16],
                    state.r[17],
                    state.r[18],
                    state.r[19],
                    state.r[20],
                    state.r[21],
                    state.r[22],
                    state.r[23],
                    state.r[24],
                    state.r[25],
                    state.r[26],
                    state.r[27],
                    state.r[28],
                    state.r[29],
                ],
                lr: state.r[14], // R[14] for aarch32
                sp: state.r[13], // R[13] for aarch32
                // TODO(https://fxbug.dev/379669623) Should it be checked for thumb and make
                // sure it isn't over incrementing?
                pc: state.pc, // Zircon populated this from elr.
                cpsr: state.cpsr as u64,
                tpidr: state.tpidr_el0,
            }
        } else {
            Self {
                r: [
                    state.r[0],
                    state.r[1],
                    state.r[2],
                    state.r[3],
                    state.r[4],
                    state.r[5],
                    state.r[6],
                    state.r[7],
                    state.r[8],
                    state.r[9],
                    state.r[10],
                    state.r[11],
                    state.r[12],
                    state.r[13],
                    state.r[14],
                    state.r[15],
                    state.r[16],
                    state.r[17],
                    state.r[18],
                    state.r[19],
                    state.r[20],
                    state.r[21],
                    state.r[22],
                    state.r[23],
                    state.r[24],
                    state.r[25],
                    state.r[26],
                    state.r[27],
                    state.r[28],
                    state.r[29],
                ],
                lr: state.r[30],
                sp: state.sp,
                pc: state.pc,
                cpsr: state.cpsr as u64,
                tpidr: state.tpidr_el0,
            }
        }
    }
}

#[cfg(target_arch = "riscv64")]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_thread_state_general_regs_t {
    pub pc: u64,
    pub ra: u64,  // x1
    pub sp: u64,  // x2
    pub gp: u64,  // x3
    pub tp: u64,  // x4
    pub t0: u64,  // x5
    pub t1: u64,  // x6
    pub t2: u64,  // x7
    pub s0: u64,  // x8
    pub s1: u64,  // x9
    pub a0: u64,  // x10
    pub a1: u64,  // x11
    pub a2: u64,  // x12
    pub a3: u64,  // x13
    pub a4: u64,  // x14
    pub a5: u64,  // x15
    pub a6: u64,  // x16
    pub a7: u64,  // x17
    pub s2: u64,  // x18
    pub s3: u64,  // x19
    pub s4: u64,  // x20
    pub s5: u64,  // x21
    pub s6: u64,  // x22
    pub s7: u64,  // x23
    pub s8: u64,  // x24
    pub s9: u64,  // x25
    pub s10: u64, // x26
    pub s11: u64, // x27
    pub t3: u64,  // x28
    pub t4: u64,  // x29
    pub t5: u64,  // x30
    pub t6: u64,  // x31
}

multiconst!(zx_restricted_reason_t, [
    ZX_RESTRICTED_REASON_SYSCALL = 0;
    ZX_RESTRICTED_REASON_EXCEPTION = 1;
    ZX_RESTRICTED_REASON_KICK = 2;
]);

#[cfg(target_arch = "x86_64")]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_restricted_state_t {
    pub rdi: u64,
    pub rsi: u64,
    pub rbp: u64,
    pub rbx: u64,
    pub rdx: u64,
    pub rcx: u64,
    pub rax: u64,
    pub rsp: u64,
    pub r8: u64,
    pub r9: u64,
    pub r10: u64,
    pub r11: u64,
    pub r12: u64,
    pub r13: u64,
    pub r14: u64,
    pub r15: u64,
    pub ip: u64,
    pub flags: u64,
    pub fs_base: u64,
    pub gs_base: u64,
}

#[cfg(target_arch = "x86_64")]
impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
        Self {
            rdi: registers.rdi,
            rsi: registers.rsi,
            rbp: registers.rbp,
            rbx: registers.rbx,
            rdx: registers.rdx,
            rcx: registers.rcx,
            rax: registers.rax,
            rsp: registers.rsp,
            r8: registers.r8,
            r9: registers.r9,
            r10: registers.r10,
            r11: registers.r11,
            r12: registers.r12,
            r13: registers.r13,
            r14: registers.r14,
            r15: registers.r15,
            ip: registers.rip,
            flags: registers.rflags,
            fs_base: registers.fs_base,
            gs_base: registers.gs_base,
        }
    }
}

#[cfg(target_arch = "aarch64")]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_restricted_state_t {
    pub r: [u64; 31], // Note: r[30] is `lr` which is separated out in the general regs.
    pub sp: u64,
    pub pc: u64,
    pub tpidr_el0: u64,
    // Contains only the user-controllable upper 4-bits (NZCV).
    pub cpsr: u32,
    padding1: [PadByte; 4],
}

#[cfg(target_arch = "aarch64")]
impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
        Self {
            r: [
                registers.r[0],
                registers.r[1],
                registers.r[2],
                registers.r[3],
                registers.r[4],
                registers.r[5],
                registers.r[6],
                registers.r[7],
                registers.r[8],
                registers.r[9],
                registers.r[10],
                registers.r[11],
                registers.r[12],
                registers.r[13],
                registers.r[14],
                registers.r[15],
                registers.r[16],
                registers.r[17],
                registers.r[18],
                registers.r[19],
                registers.r[20],
                registers.r[21],
                registers.r[22],
                registers.r[23],
                registers.r[24],
                registers.r[25],
                registers.r[26],
                registers.r[27],
                registers.r[28],
                registers.r[29],
                registers.lr, // for compat this works nicely with zircon.
            ],
            pc: registers.pc,
            tpidr_el0: registers.tpidr,
            sp: registers.sp,
            cpsr: registers.cpsr as u32,
            padding1: Default::default(),
        }
    }
}

#[cfg(target_arch = "riscv64")]
pub type zx_restricted_state_t = zx_thread_state_general_regs_t;

#[cfg(target_arch = "riscv64")]
impl From<&zx_thread_state_general_regs_t> for zx_restricted_state_t {
    fn from(registers: &zx_thread_state_general_regs_t) -> Self {
        *registers
    }
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "riscv64"))]
pub struct zx_restricted_syscall_t {
    pub state: zx_restricted_state_t,
}

#[repr(C)]
#[derive(Copy, Clone)]
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "riscv64"))]
pub struct zx_restricted_exception_t {
    pub state: zx_restricted_state_t,
    pub exception: zx_exception_report_t,
}

#[cfg(target_arch = "x86_64")]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_vcpu_state_t {
    pub rax: u64,
    pub rcx: u64,
    pub rdx: u64,
    pub rbx: u64,
    pub rsp: u64,
    pub rbp: u64,
    pub rsi: u64,
    pub rdi: u64,
    pub r8: u64,
    pub r9: u64,
    pub r10: u64,
    pub r11: u64,
    pub r12: u64,
    pub r13: u64,
    pub r14: u64,
    pub r15: u64,
    // Contains only the user-controllable lower 32-bits.
    pub rflags: u64,
}

#[cfg(target_arch = "aarch64")]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_vcpu_state_t {
    pub x: [u64; 31],
    pub sp: u64,
    // Contains only the user-controllable upper 4-bits (NZCV).
    pub cpsr: u32,
    padding1: [PadByte; 4],
}

#[cfg(target_arch = "riscv64")]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_vcpu_state_t {
    pub empty: u32,
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_vcpu_io_t {
    pub access_size: u8,
    padding1: [PadByte; 3],
    pub data: [u8; 4],
}

#[cfg(target_arch = "aarch64")]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_packet_guest_mem_t {
    pub addr: zx_gpaddr_t,
    pub access_size: u8,
    pub sign_extend: bool,
    pub xt: u8,
    pub read: bool,
    pub data: u64,
}

#[cfg(target_arch = "riscv64")]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_packet_guest_mem_t {
    pub addr: zx_gpaddr_t,
    padding1: [PadByte; 24],
}

pub const X86_MAX_INST_LEN: usize = 15;

#[cfg(target_arch = "x86_64")]
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_packet_guest_mem_t {
    pub addr: zx_gpaddr_t,
    pub cr3: zx_gpaddr_t,
    pub rip: zx_vaddr_t,
    pub instruction_size: u8,
    pub default_operand_size: u8,
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_packet_interrupt_t {
    pub timestamp: zx_time_t,
    padding1: [PadByte; 24],
}

// Helper for constructing topics that have been versioned.
const fn info_topic(topic: u32, version: u32) -> u32 {
    (version << 28) | topic
}

multiconst!(zx_object_info_topic_t, [
    ZX_INFO_NONE                       = 0;
    ZX_INFO_HANDLE_VALID               = 1;
    ZX_INFO_HANDLE_BASIC               = 2;  // zx_info_handle_basic_t[1]
    ZX_INFO_PROCESS                    = info_topic(3, 1);  // zx_info_process_t[1]
    ZX_INFO_PROCESS_THREADS            = 4;  // zx_koid_t[n]
    ZX_INFO_VMAR                       = 7;  // zx_info_vmar_t[1]
    ZX_INFO_JOB_CHILDREN               = 8;  // zx_koid_t[n]
    ZX_INFO_JOB_PROCESSES              = 9;  // zx_koid_t[n]
    ZX_INFO_THREAD                     = 10; // zx_info_thread_t[1]
    ZX_INFO_THREAD_EXCEPTION_REPORT    = info_topic(11, 1); // zx_exception_report_t[1]
    ZX_INFO_TASK_STATS                 = info_topic(12, 1); // zx_info_task_stats_t[1]
    ZX_INFO_PROCESS_MAPS               = info_topic(13, 2); // zx_info_maps_t[n]
    ZX_INFO_PROCESS_VMOS               = info_topic(14, 3); // zx_info_vmo_t[n]
    ZX_INFO_THREAD_STATS               = 15; // zx_info_thread_stats_t[1]
    ZX_INFO_CPU_STATS                  = 16; // zx_info_cpu_stats_t[n]
    ZX_INFO_KMEM_STATS                 = info_topic(17, 1); // zx_info_kmem_stats_t[1]
    ZX_INFO_RESOURCE                   = 18; // zx_info_resource_t[1]
    ZX_INFO_HANDLE_COUNT               = 19; // zx_info_handle_count_t[1]
    ZX_INFO_BTI                        = 20; // zx_info_bti_t[1]
    ZX_INFO_PROCESS_HANDLE_STATS       = 21; // zx_info_process_handle_stats_t[1]
    ZX_INFO_SOCKET                     = 22; // zx_info_socket_t[1]
    ZX_INFO_VMO                        = info_topic(23, 3); // zx_info_vmo_t[1]
    ZX_INFO_JOB                        = 24; // zx_info_job_t[1]
    ZX_INFO_TIMER                      = 25; // zx_info_timer_t[1]
    ZX_INFO_STREAM                     = 26; // zx_info_stream_t[1]
    ZX_INFO_HANDLE_TABLE               = 27; // zx_info_handle_extended_t[n]
    ZX_INFO_MSI                        = 28; // zx_info_msi_t[1]
    ZX_INFO_GUEST_STATS                = 29; // zx_info_guest_stats_t[1]
    ZX_INFO_TASK_RUNTIME               = info_topic(30, 1); // zx_info_task_runtime_t[1]
    ZX_INFO_KMEM_STATS_EXTENDED        = 31; // zx_info_kmem_stats_extended_t[1]
    ZX_INFO_VCPU                       = 32; // zx_info_vcpu_t[1]
    ZX_INFO_KMEM_STATS_COMPRESSION     = 33; // zx_info_kmem_stats_compression_t[1]
    ZX_INFO_IOB                        = 34; // zx_info_iob_t[1]
    ZX_INFO_IOB_REGIONS                = 35; // zx_iob_region_info_t[n]
    ZX_INFO_VMAR_MAPS                  = 36; // zx_info_maps_t[n]
    ZX_INFO_POWER_DOMAINS                  = 37; // zx_info_power_domain_info_t[n]
]);

// This macro takes struct-like syntax and creates another macro that can be used to create
// different instances of the struct with different names. This is used to keep struct definitions
// from drifting between this crate and the fuchsia-zircon crate where they are identical other
// than in name and location.
macro_rules! struct_decl_macro {
    ( $(#[$attrs:meta])* $vis:vis struct <$macro_name:ident> $($any:tt)* ) => {
        #[macro_export]
        macro_rules! $macro_name {
            ($name:ident) => {
                $(#[$attrs])* $vis struct $name $($any)*
            }
        }
    }
}

// Don't need struct_decl_macro for this, the wrapper is different.
#[repr(C)]
#[derive(Default, Debug, Copy, Clone, Eq, FromBytes, Immutable, PartialEq)]
pub struct zx_info_handle_basic_t {
    pub koid: zx_koid_t,
    pub rights: zx_rights_t,
    pub type_: zx_obj_type_t,
    pub related_koid: zx_koid_t,
    padding1: [PadByte; 4],
}

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_handle_count_t> {
        pub handle_count: u32,
    }
}

zx_info_handle_count_t!(zx_info_handle_count_t);

// Don't need struct_decl_macro for this, the wrapper is different.
#[repr(C)]
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable)]
pub struct zx_info_socket_t {
    pub options: u32,
    pub rx_buf_max: usize,
    pub rx_buf_size: usize,
    pub rx_buf_available: usize,
    pub tx_buf_max: usize,
    pub tx_buf_size: usize,
}

multiconst!(u32, [
    ZX_INFO_PROCESS_FLAG_STARTED = 1 << 0;
    ZX_INFO_PROCESS_FLAG_EXITED = 1 << 1;
    ZX_INFO_PROCESS_FLAG_DEBUGGER_ATTACHED = 1 << 2;
]);

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_process_t> {
        pub return_code: i64,
        pub start_time: zx_time_t,
        pub flags: u32,
    }
}

zx_info_process_t!(zx_info_process_t);

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_job_t> {
        pub return_code: i64,
        pub exited: u8,
        pub kill_on_oom: u8,
        pub debugger_attached: u8,
    }
}

zx_info_job_t!(zx_info_job_t);

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_policy_basic {
    pub condition: u32,
    pub policy: u32,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_policy_timer_slack {
    pub min_slack: zx_duration_t,
    pub default_mode: u32,
}

multiconst!(u32, [
    // policy options
    ZX_JOB_POL_RELATIVE = 0;
    ZX_JOB_POL_ABSOLUTE = 1;

    // policy topic
    ZX_JOB_POL_BASIC = 0;
    ZX_JOB_POL_TIMER_SLACK = 1;

    // policy conditions
    ZX_POL_BAD_HANDLE            = 0;
    ZX_POL_WRONG_OBJECT          = 1;
    ZX_POL_VMAR_WX               = 2;
    ZX_POL_NEW_ANY               = 3;
    ZX_POL_NEW_VMO               = 4;
    ZX_POL_NEW_CHANNEL           = 5;
    ZX_POL_NEW_EVENT             = 6;
    ZX_POL_NEW_EVENTPAIR         = 7;
    ZX_POL_NEW_PORT              = 8;
    ZX_POL_NEW_SOCKET            = 9;
    ZX_POL_NEW_FIFO              = 10;
    ZX_POL_NEW_TIMER             = 11;
    ZX_POL_NEW_PROCESS           = 12;
    ZX_POL_NEW_PROFILE           = 13;
    ZX_POL_NEW_PAGER             = 14;
    ZX_POL_AMBIENT_MARK_VMO_EXEC = 15;

    // policy actions
    ZX_POL_ACTION_ALLOW           = 0;
    ZX_POL_ACTION_DENY            = 1;
    ZX_POL_ACTION_ALLOW_EXCEPTION = 2;
    ZX_POL_ACTION_DENY_EXCEPTION  = 3;
    ZX_POL_ACTION_KILL            = 4;

    // timer slack default modes
    ZX_TIMER_SLACK_CENTER = 0;
    ZX_TIMER_SLACK_EARLY  = 1;
    ZX_TIMER_SLACK_LATE   = 2;
]);

multiconst!(u32, [
    // critical options
    ZX_JOB_CRITICAL_PROCESS_RETCODE_NONZERO = 1 << 0;
]);

// Don't use struct_decl_macro, wrapper is different.
#[repr(C)]
#[derive(
    Default, Debug, Copy, Clone, Eq, PartialEq, KnownLayout, FromBytes, Immutable, IntoBytes,
)]
pub struct zx_info_vmo_t {
    pub koid: zx_koid_t,
    pub name: [u8; ZX_MAX_NAME_LEN],
    pub size_bytes: u64,
    pub parent_koid: zx_koid_t,
    pub num_children: usize,
    pub num_mappings: usize,
    pub share_count: usize,
    pub flags: u32,
    padding1: [PadByte; 4],
    pub committed_bytes: u64,
    pub handle_rights: zx_rights_t,
    pub cache_policy: u32,
    pub metadata_bytes: u64,
    pub committed_change_events: u64,
    pub populated_bytes: u64,
    pub committed_private_bytes: u64,
    pub populated_private_bytes: u64,
    pub committed_scaled_bytes: u64,
    pub populated_scaled_bytes: u64,
    pub committed_fractional_scaled_bytes: u64,
    pub populated_fractional_scaled_bytes: u64,
}

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_cpu_stats_t> {
        pub cpu_number: u32,
        pub flags: u32,
        pub idle_time: zx_duration_t,
        pub reschedules: u64,
        pub context_switches: u64,
        pub irq_preempts: u64,
        pub preempts: u64,
        pub yields: u64,
        pub ints: u64,
        pub timer_ints: u64,
        pub timers: u64,
        pub page_faults: u64,
        pub exceptions: u64,
        pub syscalls: u64,
        pub reschedule_ipis: u64,
        pub generic_ipis: u64,
    }
}

zx_info_cpu_stats_t!(zx_info_cpu_stats_t);

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_kmem_stats_t> {
        pub total_bytes: u64,
        pub free_bytes: u64,
        pub free_loaned_bytes: u64,
        pub wired_bytes: u64,
        pub total_heap_bytes: u64,
        pub free_heap_bytes: u64,
        pub vmo_bytes: u64,
        pub mmu_overhead_bytes: u64,
        pub ipc_bytes: u64,
        pub cache_bytes: u64,
        pub slab_bytes: u64,
        pub zram_bytes: u64,
        pub other_bytes: u64,
        pub vmo_reclaim_total_bytes: u64,
        pub vmo_reclaim_newest_bytes: u64,
        pub vmo_reclaim_oldest_bytes: u64,
        pub vmo_reclaim_disabled_bytes: u64,
        pub vmo_discardable_locked_bytes: u64,
        pub vmo_discardable_unlocked_bytes: u64,
    }
}

zx_info_kmem_stats_t!(zx_info_kmem_stats_t);

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_kmem_stats_extended_t> {
        pub total_bytes: u64,
        pub free_bytes: u64,
        pub wired_bytes: u64,
        pub total_heap_bytes: u64,
        pub free_heap_bytes: u64,
        pub vmo_bytes: u64,
        pub vmo_pager_total_bytes: u64,
        pub vmo_pager_newest_bytes: u64,
        pub vmo_pager_oldest_bytes: u64,
        pub vmo_discardable_locked_bytes: u64,
        pub vmo_discardable_unlocked_bytes: u64,
        pub mmu_overhead_bytes: u64,
        pub ipc_bytes: u64,
        pub other_bytes: u64,
        pub vmo_reclaim_disable_bytes: u64,
    }
}

zx_info_kmem_stats_extended_t!(zx_info_kmem_stats_extended_t);

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_kmem_stats_compression_t> {
        pub uncompressed_storage_bytes: u64,
        pub compressed_storage_bytes: u64,
        pub compressed_fragmentation_bytes: u64,
        pub compression_time: zx_duration_t,
        pub decompression_time: zx_duration_t,
        pub total_page_compression_attempts: u64,
        pub failed_page_compression_attempts: u64,
        pub total_page_decompressions: u64,
        pub compressed_page_evictions: u64,
        pub eager_page_compressions: u64,
        pub memory_pressure_page_compressions: u64,
        pub critical_memory_page_compressions: u64,
        pub pages_decompressed_unit_ns: u64,
        pub pages_decompressed_within_log_time: [u64; 8],
    }
}

zx_info_kmem_stats_compression_t!(zx_info_kmem_stats_compression_t);

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_resource_t> {
        pub kind: u32,
        pub flags: u32,
        pub base: u64,
        pub size: usize,
        pub name: [u8; ZX_MAX_NAME_LEN],
    }
}

pub type zx_thread_state_t = u32;

multiconst!(zx_thread_state_t, [
    ZX_THREAD_STATE_NEW = 0x0000;
    ZX_THREAD_STATE_RUNNING = 0x0001;
    ZX_THREAD_STATE_SUSPENDED = 0x0002;
    ZX_THREAD_STATE_BLOCKED = 0x0003;
    ZX_THREAD_STATE_DYING = 0x0004;
    ZX_THREAD_STATE_DEAD = 0x0005;
    ZX_THREAD_STATE_BLOCKED_EXCEPTION = 0x0103;
    ZX_THREAD_STATE_BLOCKED_SLEEPING = 0x0203;
    ZX_THREAD_STATE_BLOCKED_FUTEX = 0x0303;
    ZX_THREAD_STATE_BLOCKED_PORT = 0x0403;
    ZX_THREAD_STATE_BLOCKED_CHANNEL = 0x0503;
    ZX_THREAD_STATE_BLOCKED_WAIT_ONE = 0x0603;
    ZX_THREAD_STATE_BLOCKED_WAIT_MANY = 0x0703;
    ZX_THREAD_STATE_BLOCKED_INTERRUPT = 0x0803;
    ZX_THREAD_STATE_BLOCKED_PAGER = 0x0903;
]);

#[repr(C)]
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, zerocopy::FromBytes, zerocopy::Immutable)]
pub struct zx_info_thread_t {
    pub state: zx_thread_state_t,
    pub wait_exception_channel_type: u32,
    pub cpu_affinity_mask: zx_cpu_set_t,
}

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_thread_stats_t> {
        pub total_runtime: zx_duration_t,
        pub last_scheduled_cpu: u32,
    }
}

zx_info_thread_stats_t!(zx_info_thread_stats_t);

zx_info_resource_t!(zx_info_resource_t);

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_vmar_t> {
        pub base: usize,
        pub len: usize,
    }
}

zx_info_vmar_t!(zx_info_vmar_t);

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_task_stats_t> {
        pub mem_mapped_bytes: usize,
        pub mem_private_bytes: usize,
        pub mem_shared_bytes: usize,
        pub mem_scaled_shared_bytes: usize,
        pub mem_fractional_scaled_shared_bytes: u64,
    }
}

zx_info_task_stats_t!(zx_info_task_stats_t);

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_task_runtime_t> {
        pub cpu_time: zx_duration_t,
        pub queue_time: zx_duration_t,
        pub page_fault_time: zx_duration_t,
        pub lock_contention_time: zx_duration_t,
    }
}

zx_info_task_runtime_t!(zx_info_task_runtime_t);

multiconst!(zx_info_maps_type_t, [
    ZX_INFO_MAPS_TYPE_NONE    = 0;
    ZX_INFO_MAPS_TYPE_ASPACE  = 1;
    ZX_INFO_MAPS_TYPE_VMAR    = 2;
    ZX_INFO_MAPS_TYPE_MAPPING = 3;
]);

struct_decl_macro! {
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable, IntoBytes)]
    pub struct <zx_info_maps_mapping_t> {
        pub mmu_flags: zx_vm_option_t,
        padding1: [PadByte; 4],
        pub vmo_koid: zx_koid_t,
        pub vmo_offset: u64,
        pub committed_bytes: usize,
        pub populated_bytes: usize,
        pub committed_private_bytes: usize,
        pub populated_private_bytes: usize,
        pub committed_scaled_bytes: usize,
        pub populated_scaled_bytes: usize,
        pub committed_fractional_scaled_bytes: u64,
        pub populated_fractional_scaled_bytes: u64,
    }
}

zx_info_maps_mapping_t!(zx_info_maps_mapping_t);

#[repr(C)]
#[derive(Copy, Clone, KnownLayout, FromBytes, Immutable)]
pub union InfoMapsTypeUnion {
    pub mapping: zx_info_maps_mapping_t,
}

struct_decl_macro! {
    #[repr(C)]
    #[derive(Copy, Clone)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_maps_t> {
        pub name: [u8; ZX_MAX_NAME_LEN],
        pub base: zx_vaddr_t,
        pub size: usize,
        pub depth: usize,
        pub r#type: zx_info_maps_type_t,
        pub u: InfoMapsTypeUnion,
    }
}

zx_info_maps_t!(zx_info_maps_t);

struct_decl_macro! {
    #[repr(C)]
    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_info_process_handle_stats_t> {
        pub handle_count: [u32; ZX_OBJ_TYPE_UPPER_BOUND],
    }
}

impl Default for zx_info_process_handle_stats_t {
    fn default() -> Self {
        Self { handle_count: [0; ZX_OBJ_TYPE_UPPER_BOUND] }
    }
}

zx_info_process_handle_stats_t!(zx_info_process_handle_stats_t);

// from //zircon/system/public/zircon/syscalls/hypervisor.h
multiconst!(zx_guest_option_t, [
    ZX_GUEST_OPT_NORMAL = 0;
]);

multiconst!(zx_guest_trap_t, [
    ZX_GUEST_TRAP_BELL = 0;
    ZX_GUEST_TRAP_MEM  = 1;
    ZX_GUEST_TRAP_IO   = 2;
]);

pub const ZX_LOG_RECORD_MAX: usize = 256;
pub const ZX_LOG_RECORD_DATA_MAX: usize = 216;

pub const DEBUGLOG_TRACE: u8 = 0x10;
pub const DEBUGLOG_DEBUG: u8 = 0x20;
pub const DEBUGLOG_INFO: u8 = 0x30;
pub const DEBUGLOG_WARNING: u8 = 0x40;
pub const DEBUGLOG_ERROR: u8 = 0x50;
pub const DEBUGLOG_FATAL: u8 = 0x60;

struct_decl_macro! {
    #[repr(C)]
    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
    #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
    pub struct <zx_log_record_t> {
        pub sequence: u64,
        padding1: [PadByte; 4],
        pub datalen: u16,
        pub severity: u8,
        pub flags: u8,
        pub timestamp: zx_instant_boot_t,
        pub pid: u64,
        pub tid: u64,
        pub data: [u8; ZX_LOG_RECORD_DATA_MAX],
    }
}
const_assert_eq!(std::mem::size_of::<zx_log_record_t>(), ZX_LOG_RECORD_MAX);

zx_log_record_t!(zx_log_record_t);

impl Default for zx_log_record_t {
    fn default() -> zx_log_record_t {
        zx_log_record_t {
            sequence: 0,
            padding1: Default::default(),
            datalen: 0,
            severity: 0,
            flags: 0,
            timestamp: 0,
            pid: 0,
            tid: 0,
            data: [0; ZX_LOG_RECORD_DATA_MAX],
        }
    }
}

multiconst!(u32, [
    ZX_LOG_FLAG_READABLE = 0x40000000;
]);

// For C, the below types are currently forward declared for syscalls.h.
// We might want to investigate a better solution for Rust or removing those
// forward declarations.
//
// These are hand typed translations from C types into Rust structures using a C
// layout

// source: zircon/system/public/zircon/syscalls/system.h
#[repr(C)]
pub struct zx_system_powerctl_arg_t {
    // rust can't express anonymous unions at this time
    // https://github.com/rust-lang/rust/issues/49804
    pub powerctl_internal: zx_powerctl_union,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union zx_powerctl_union {
    acpi_transition_s_state: acpi_transition_s_state,
    x86_power_limit: x86_power_limit,
}

#[repr(C)]
#[derive(Default, Debug, PartialEq, Copy, Clone)]
pub struct acpi_transition_s_state {
    target_s_state: u8, // Value between 1 and 5 indicating which S-state
    sleep_type_a: u8,   // Value from ACPI VM (SLP_TYPa)
    sleep_type_b: u8,   // Value from ACPI VM (SLP_TYPb)
    padding1: [PadByte; 9],
}

#[repr(C)]
#[derive(Default, Debug, PartialEq, Copy, Clone)]
pub struct x86_power_limit {
    power_limit: u32, // PL1 value in milliwatts
    time_window: u32, // PL1 time window in microseconds
    clamp: u8,        // PL1 clamping enable
    enable: u8,       // PL1 enable
    padding1: [PadByte; 2],
}

// source: zircon/system/public/zircon/syscalls/pci.h
pub type zx_pci_bar_types_t = u32;

multiconst!(zx_pci_bar_types_t, [
            ZX_PCI_BAR_TYPE_UNUSED = 0;
            ZX_PCI_BAR_TYPE_MMIO = 1;
            ZX_PCI_BAR_TYPE_PIO = 2;
]);

#[repr(C)]
pub struct zx_pci_bar_t {
    pub id: u32,
    pub ty: u32,
    pub size: usize,
    // rust can't express anonymous unions at this time
    // https://github.com/rust-lang/rust/issues/49804
    pub zx_pci_bar_union: zx_pci_bar_union,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union zx_pci_bar_union {
    addr: usize,
    zx_pci_bar_union_struct: zx_pci_bar_union_struct,
}

#[repr(C)]
#[derive(Default, Debug, PartialEq, Copy, Clone)]
pub struct zx_pci_bar_union_struct {
    handle: zx_handle_t,
    padding1: [PadByte; 4],
}

// source: zircon/system/public/zircon/syscalls/smc.h
#[repr(C)]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
pub struct zx_smc_parameters_t {
    pub func_id: u32,
    padding1: [PadByte; 4],
    pub arg1: u64,
    pub arg2: u64,
    pub arg3: u64,
    pub arg4: u64,
    pub arg5: u64,
    pub arg6: u64,
    pub client_id: u16,
    pub secure_os_id: u16,
    padding2: [PadByte; 4],
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_smc_result_t {
    pub arg0: u64,
    pub arg1: u64,
    pub arg2: u64,
    pub arg3: u64,
    pub arg6: u64,
}

pub const ZX_CPU_SET_MAX_CPUS: usize = 512;
pub const ZX_CPU_SET_BITS_PER_WORD: usize = 64;

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, zerocopy::FromBytes, zerocopy::Immutable)]
pub struct zx_cpu_set_t {
    pub mask: [u64; ZX_CPU_SET_MAX_CPUS / ZX_CPU_SET_BITS_PER_WORD],
}

// source: zircon/system/public/zircon/syscalls/scheduler.h
#[repr(C)]
#[derive(Copy, Clone)]
pub struct zx_profile_info_t {
    pub flags: u32,
    padding1: [PadByte; 4],
    pub zx_profile_info_union: zx_profile_info_union,
    pub cpu_affinity_mask: zx_cpu_set_t,
}

#[cfg(feature = "zerocopy")]
impl Default for zx_profile_info_t {
    fn default() -> Self {
        Self {
            flags: Default::default(),
            padding1: Default::default(),
            zx_profile_info_union: FromZeros::new_zeroed(),
            cpu_affinity_mask: Default::default(),
        }
    }
}

#[repr(C)]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
pub struct priority_params {
    pub priority: i32,
    padding1: [PadByte; 20],
}

#[repr(C)]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable))]
pub union zx_profile_info_union {
    pub priority_params: priority_params,
    pub deadline_params: zx_sched_deadline_params_t,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, KnownLayout))]
pub struct zx_sched_deadline_params_t {
    pub capacity: zx_duration_t,
    pub relative_deadline: zx_duration_t,
    pub period: zx_duration_t,
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_cpu_performance_scale_t {
    pub integer_part: u32,
    pub fractional_part: u32,
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_cpu_performance_info_t {
    pub logical_cpu_number: u32,
    pub performance_scale: zx_cpu_performance_scale_t,
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_cpu_power_limit_t {
    pub logical_cpu_number: u32,
    padding1: [PadByte; 4],
    pub max_power_nw: u64,
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_iommu_desc_dummy_t {
    padding1: PadByte,
}

multiconst!(u32, [
    ZX_IOMMU_TYPE_DUMMY = 0;
    ZX_IOMMU_TYPE_INTEL = 1;
]);

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct zx_sampler_config_t {
    pub period: zx_duration_t,
    pub buffer_size: usize,
    pub iobuffer_discipline: u64,
}

multiconst!(zx_processor_power_level_options_t, [
    ZX_PROCESSOR_POWER_LEVEL_OPTIONS_DOMAIN_INDEPENDENT = 1 << 0;
]);

multiconst!(zx_processor_power_control_t, [
    ZX_PROCESSOR_POWER_CONTROL_CPU_DRIVER = 0;
    ZX_PROCESSOR_POWER_CONTROL_ARM_PSCI = 1;
    ZX_PROCESSOR_POWER_CONTROL_ARM_WFI = 2;
    ZX_PROCESSOR_POWER_CONTROL_RISCV_SBI = 3;
    ZX_PROCESSOR_POWER_CONTROL_RISCV_WFI = 4;
]);

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_processor_power_level_t {
    pub options: zx_processor_power_level_options_t,
    pub processing_rate: u64,
    pub power_coefficient_nw: u64,
    pub control_interface: zx_processor_power_control_t,
    pub control_argument: u64,
    pub diagnostic_name: [u8; ZX_MAX_NAME_LEN],
    padding1: [PadByte; 32],
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct zx_processor_power_level_transition_t {
    pub latency: zx_duration_t,
    pub energy: u64,
    pub from: u8,
    pub to: u8,
    padding1: [PadByte; 6],
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
pub struct zx_packet_processor_power_level_transition_request_t {
    pub domain_id: u32,
    pub options: u32,
    pub control_interface: u64,
    pub control_argument: u64,
    padding1: [PadByte; 8],
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_processor_power_state_t {
    pub domain_id: u32,
    pub options: u32,
    pub control_interface: u64,
    pub control_argument: u64,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
pub struct zx_processor_power_domain_t {
    pub cpus: zx_cpu_set_t,
    pub domain_id: u32,
    padding1: [PadByte; 4],
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct zx_power_domain_info_t {
    pub cpus: zx_cpu_set_t,
    pub domain_id: u32,
    pub idle_power_levels: u8,
    pub active_power_levels: u8,
    padding1: [PadByte; 2],
}

multiconst!(u32, [
    ZX_BTI_PERM_READ = 1 << 0;
    ZX_BTI_PERM_WRITE = 1 << 1;
    ZX_BTI_PERM_EXECUTE = 1 << 2;
    ZX_BTI_COMPRESS = 1 << 3;
    ZX_BTI_CONTIGUOUS = 1 << 4;
]);

// Options for zx_port_create
multiconst!(u32, [
    ZX_PORT_BIND_TO_INTERRUPT = 1 << 0;
]);

// Options for zx_interrupt_bind
multiconst!(u32, [
    ZX_INTERRUPT_BIND = 0;
    ZX_INTERRUPT_UNBIND = 1;
]);

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn padded_struct_equality() {
        let test_struct = zx_clock_update_args_v1_t {
            rate_adjust: 222,
            padding1: Default::default(),
            value: 333,
            error_bound: 444,
        };

        let different_data = zx_clock_update_args_v1_t { rate_adjust: 999, ..test_struct.clone() };

        let different_padding = zx_clock_update_args_v1_t {
            padding1: [PadByte(0), PadByte(1), PadByte(2), PadByte(3)],
            ..test_struct.clone()
        };

        // Structures with different data should not be equal.
        assert_ne!(test_struct, different_data);
        // Structures with only different padding should not be equal.
        assert_eq!(test_struct, different_padding);
    }

    #[test]
    fn padded_struct_debug() {
        let test_struct = zx_clock_update_args_v1_t {
            rate_adjust: 222,
            padding1: Default::default(),
            value: 333,
            error_bound: 444,
        };
        let expectation = "zx_clock_update_args_v1_t { \
            rate_adjust: 222, \
            padding1: [-, -, -, -], \
            value: 333, \
            error_bound: 444 }";
        assert_eq!(format!("{:?}", test_struct), expectation);
    }
}