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
// Copyright 2016 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)]

#[macro_use]
extern crate bitflags;

use std::{cmp, fmt};

pub type mx_handle_t = i32;

pub type mx_status_t = i32;

pub type mx_futex_t = isize;
pub type mx_addr_t = usize;
pub type mx_paddr_t = usize;
pub type mx_vaddr_t = usize;

// Auto-generated using tools/gen_status.py
pub const MX_OK                    : mx_status_t = 0;
pub const MX_ERR_INTERNAL          : mx_status_t = -1;
pub const MX_ERR_NOT_SUPPORTED     : mx_status_t = -2;
pub const MX_ERR_NO_RESOURCES      : mx_status_t = -3;
pub const MX_ERR_NO_MEMORY         : mx_status_t = -4;
pub const MX_ERR_CALL_FAILED       : mx_status_t = -5;
pub const MX_ERR_INTERRUPTED_RETRY : mx_status_t = -6;
pub const MX_ERR_INVALID_ARGS      : mx_status_t = -10;
pub const MX_ERR_BAD_HANDLE        : mx_status_t = -11;
pub const MX_ERR_WRONG_TYPE        : mx_status_t = -12;
pub const MX_ERR_BAD_SYSCALL       : mx_status_t = -13;
pub const MX_ERR_OUT_OF_RANGE      : mx_status_t = -14;
pub const MX_ERR_BUFFER_TOO_SMALL  : mx_status_t = -15;
pub const MX_ERR_BAD_STATE         : mx_status_t = -20;
pub const MX_ERR_TIMED_OUT         : mx_status_t = -21;
pub const MX_ERR_SHOULD_WAIT       : mx_status_t = -22;
pub const MX_ERR_CANCELED          : mx_status_t = -23;
pub const MX_ERR_PEER_CLOSED       : mx_status_t = -24;
pub const MX_ERR_NOT_FOUND         : mx_status_t = -25;
pub const MX_ERR_ALREADY_EXISTS    : mx_status_t = -26;
pub const MX_ERR_ALREADY_BOUND     : mx_status_t = -27;
pub const MX_ERR_UNAVAILABLE       : mx_status_t = -28;
pub const MX_ERR_ACCESS_DENIED     : mx_status_t = -30;
pub const MX_ERR_IO                : mx_status_t = -40;
pub const MX_ERR_IO_REFUSED        : mx_status_t = -41;
pub const MX_ERR_IO_DATA_INTEGRITY : mx_status_t = -42;
pub const MX_ERR_IO_DATA_LOSS      : mx_status_t = -43;
pub const MX_ERR_BAD_PATH          : mx_status_t = -50;
pub const MX_ERR_NOT_DIR           : mx_status_t = -51;
pub const MX_ERR_NOT_FILE          : mx_status_t = -52;
pub const MX_ERR_FILE_BIG          : mx_status_t = -53;
pub const MX_ERR_NO_SPACE          : mx_status_t = -54;
pub const MX_ERR_STOP              : mx_status_t = -60;
pub const MX_ERR_NEXT              : mx_status_t = -61;

pub type mx_time_t = u64;
pub type mx_duration_t = u64;
pub const MX_TIME_INFINITE : mx_time_t = ::std::u64::MAX;

bitflags! {
    #[repr(C)]
    pub flags mx_signals_t: u32 {
        const MX_SIGNAL_NONE              = 0,
        const MX_OBJECT_SIGNAL_ALL        = 0x00ffffff,
        const MX_USER_SIGNAL_ALL          = 0xff000000,
        const MX_OBJECT_SIGNAL_0          = 1 << 0,
        const MX_OBJECT_SIGNAL_1          = 1 << 1,
        const MX_OBJECT_SIGNAL_2          = 1 << 2,
        const MX_OBJECT_SIGNAL_3          = 1 << 3,
        const MX_OBJECT_SIGNAL_4          = 1 << 4,
        const MX_OBJECT_SIGNAL_5          = 1 << 5,
        const MX_OBJECT_SIGNAL_6          = 1 << 6,
        const MX_OBJECT_SIGNAL_7          = 1 << 7,
        const MX_OBJECT_SIGNAL_8          = 1 << 8,
        const MX_OBJECT_SIGNAL_9          = 1 << 9,
        const MX_OBJECT_SIGNAL_10         = 1 << 10,
        const MX_OBJECT_SIGNAL_11         = 1 << 11,
        const MX_OBJECT_SIGNAL_12         = 1 << 12,
        const MX_OBJECT_SIGNAL_13         = 1 << 13,
        const MX_OBJECT_SIGNAL_14         = 1 << 14,
        const MX_OBJECT_SIGNAL_15         = 1 << 15,
        const MX_OBJECT_SIGNAL_16         = 1 << 16,
        const MX_OBJECT_SIGNAL_17         = 1 << 17,
        const MX_OBJECT_SIGNAL_18         = 1 << 18,
        const MX_OBJECT_SIGNAL_19         = 1 << 19,
        const MX_OBJECT_SIGNAL_20         = 1 << 20,
        const MX_OBJECT_SIGNAL_21         = 1 << 21,
        const MX_OBJECT_LAST_HANDLE       = 1 << 22,
        const MX_OBJECT_HANDLE_CLOSED     = 1 << 23,
        const MX_USER_SIGNAL_0            = 1 << 24,
        const MX_USER_SIGNAL_1            = 1 << 25,
        const MX_USER_SIGNAL_2            = 1 << 26,
        const MX_USER_SIGNAL_3            = 1 << 27,
        const MX_USER_SIGNAL_4            = 1 << 28,
        const MX_USER_SIGNAL_5            = 1 << 29,
        const MX_USER_SIGNAL_6            = 1 << 30,
        const MX_USER_SIGNAL_7            = 1 << 31,

        const MX_OBJECT_READABLE          = MX_OBJECT_SIGNAL_0.bits,
        const MX_OBJECT_WRITABLE          = MX_OBJECT_SIGNAL_1.bits,
        const MX_OBJECT_PEER_CLOSED       = MX_OBJECT_SIGNAL_2.bits,

        // Cancelation (handle was closed while waiting with it)
        const MX_SIGNAL_HANDLE_CLOSED     = MX_OBJECT_HANDLE_CLOSED.bits,

        // Only one user-more reference (handle) to the object exists.
        const MX_SIGNAL_LAST_HANDLE       = MX_OBJECT_LAST_HANDLE.bits,

        // Event
        const MX_EVENT_SIGNALED           = MX_OBJECT_SIGNAL_3.bits,

        // EventPair
        const MX_EPAIR_SIGNALED           = MX_OBJECT_SIGNAL_3.bits,
        const MX_EPAIR_CLOSED             = MX_OBJECT_SIGNAL_2.bits,

        // Task signals (process, thread, job)
        const MX_TASK_TERMINATED          = MX_OBJECT_SIGNAL_3.bits,

        // Channel
        const MX_CHANNEL_READABLE         = MX_OBJECT_SIGNAL_0.bits,
        const MX_CHANNEL_WRITABLE         = MX_OBJECT_SIGNAL_1.bits,
        const MX_CHANNEL_PEER_CLOSED      = MX_OBJECT_SIGNAL_2.bits,

        // Socket
        const MX_SOCKET_READABLE          = MX_OBJECT_SIGNAL_0.bits,
        const MX_SOCKET_WRITABLE          = MX_OBJECT_SIGNAL_1.bits,
        const MX_SOCKET_PEER_CLOSED       = MX_OBJECT_SIGNAL_2.bits,

        // Port
        const MX_PORT_READABLE            = MX_OBJECT_READABLE.bits,

        // Resource
        const MX_RESOURCE_DESTROYED       = MX_OBJECT_SIGNAL_3.bits,
        const MX_RESOURCE_READABLE        = MX_OBJECT_READABLE.bits,
        const MX_RESOURCE_WRITABLE        = MX_OBJECT_WRITABLE.bits,
        const MX_RESOURCE_CHILD_ADDED     = MX_OBJECT_SIGNAL_4.bits,

        // Fifo
        const MX_FIFO_READABLE            = MX_OBJECT_READABLE.bits,
        const MX_FIFO_WRITABLE            = MX_OBJECT_WRITABLE.bits,
        const MX_FIFO_PEER_CLOSED         = MX_OBJECT_PEER_CLOSED.bits,

        // Job
        const MX_JOB_NO_PROCESSES         = MX_OBJECT_SIGNAL_3.bits,
        const MX_JOB_NO_JOBS              = MX_OBJECT_SIGNAL_4.bits,

        // Process
        const MX_PROCESS_TERMINATED       = MX_OBJECT_SIGNAL_3.bits,

        // Thread
        const MX_THREAD_TERMINATED        = MX_OBJECT_SIGNAL_3.bits,

        // Log
        const MX_LOG_READABLE             = MX_OBJECT_READABLE.bits,
        const MX_LOG_WRITABLE             = MX_OBJECT_WRITABLE.bits,

        // Timer
        const MX_TIMER_SIGNALED           = MX_OBJECT_SIGNAL_3.bits,
    }
}

pub type mx_size_t = usize;
pub type mx_ssize_t = isize;

bitflags! {
    #[repr(C)]
    pub flags mx_rights_t: u32 {
        const MX_RIGHT_NONE         = 0,
        const MX_RIGHT_DUPLICATE    = 1 << 0,
        const MX_RIGHT_TRANSFER     = 1 << 1,
        const MX_RIGHT_READ         = 1 << 2,
        const MX_RIGHT_WRITE        = 1 << 3,
        const MX_RIGHT_EXECUTE      = 1 << 4,
        const MX_RIGHT_MAP          = 1 << 5,
        const MX_RIGHT_GET_PROPERTY = 1 << 6,
        const MX_RIGHT_SET_PROPERTY = 1 << 7,
        const MX_RIGHT_DEBUG        = 1 << 8,
        const MX_RIGHT_SAME_RIGHTS  = 1 << 31,
    }
}

// clock ids
pub const MX_CLOCK_MONOTONIC: u32 = 0;

// Buffer size limits on the cprng syscalls
pub const MX_CPRNG_DRAW_MAX_LEN: usize = 256;
pub const MX_CPRNG_ADD_ENTROPY_MAX_LEN: usize = 256;

// Socket flags and limits.
pub const MX_SOCKET_HALF_CLOSE: u32 = 1;

// VM Object opcodes
pub const MX_VMO_OP_COMMIT: u32 = 1;
pub const MX_VMO_OP_DECOMMIT: u32 = 2;
pub const MX_VMO_OP_LOCK: u32 = 3;
pub const MX_VMO_OP_UNLOCK: u32 = 4;
pub const MX_VMO_OP_LOOKUP: u32 = 5;
pub const MX_VMO_OP_CACHE_SYNC: u32 = 6;
pub const MX_VMO_OP_CACHE_INVALIDATE: u32 = 7;
pub const MX_VMO_OP_CACHE_CLEAN: u32 = 8;
pub const MX_VMO_OP_CACHE_CLEAN_INVALIDATE: u32 = 9;

// VM Object clone flags
pub const MX_VMO_CLONE_COPY_ON_WRITE: u32 = 1;

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum mx_cache_policy_t {
    MX_CACHE_POLICY_CACHED = 0,
    MX_CACHE_POLICY_UNCACHED = 1,
    MX_CACHE_POLICY_UNCACHED_DEVICE = 2,
    MX_CACHE_POLICY_WRITE_COMBINING = 3,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct mx_wait_item_t {
    pub handle: mx_handle_t,
    pub waitfor: mx_signals_t,
    pub pending: mx_signals_t,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct mx_waitset_result_t {
    pub cookie: u64,
    pub status: mx_status_t,
    pub observed: mx_signals_t,
}

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

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

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct mx_pci_init_arg_t {
    pub dev_pin_to_global_irq: mx_pci_irq_swizzle_lut_t,
    pub num_irqs: u32,
    pub irqs: [mx_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: [mx_ecam_window_t; 1],
}

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

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct mx_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 mx_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 mx_pci_resource_t {
    pub type_: u32,
    pub size: usize,
    // TODO: Actually a union
    pub pio_addr: usize,
}

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

// Ports V2
#[repr(u32)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum mx_packet_type_t {
    MX_PKT_TYPE_USER = 0,
    MX_PKT_TYPE_SIGNAL_ONE = 1,
    MX_PKT_TYPE_SIGNAL_REP = 2,
}

impl Default for mx_packet_type_t {
    fn default() -> Self {
        mx_packet_type_t::MX_PKT_TYPE_USER
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct mx_packet_signal_t {
    pub trigger: mx_signals_t,
    pub observed: mx_signals_t,
    pub count: u64,
}

pub const MX_WAIT_ASYNC_ONCE: u32 = 0;
pub const MX_WAIT_ASYNC_REPEATING: u32 = 1;

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

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

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

#[cfg(target_arch="aarch64")]
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct mx_guest_memory_t {
    addr: mx_vaddr_t,
    inst: u32,
}

pub const X86_MAX_INST_LEN: usize = 15;

#[cfg(target_arch="x86_64")]
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct mx_guest_memory_t {
    addr: mx_vaddr_t,
    inst_len: u8,
    inst_buf: [u8; X86_MAX_INST_LEN],
}

#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum mx_guest_packet_t_type {
    MX_GUEST_PKT_MEMORY = 1,
    MX_GUEST_PKT_IO = 2,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union mx_guest_packet_t_union {
    // MX_GUEST_PKT_MEMORY
    memory: mx_guest_memory_t,
    // MX_GUEST_PKT_IO
    io: mx_guest_io_t,
}

// Note: values of this type must maintain the invariant that
// `packet_type` correctly indicates the type of `contents`.
// Failure to do so will result in unsafety.
#[repr(C)]
#[derive(Copy, Clone)]
pub struct mx_guest_packet_t {
    packet_type: mx_guest_packet_t_type,
    contents: mx_guest_packet_t_union,
}

impl fmt::Debug for mx_guest_packet_t {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "mx_guest_packet_t {{ packet_type: {:?}, contents: ", self.packet_type)?;
        match self.packet_type {
            mx_guest_packet_t_type::MX_GUEST_PKT_MEMORY =>
                write!(f, "mx_guest_packet_t_union {{ memory: {:?} }} }}",
                    unsafe { self.contents.memory }
                ),
            mx_guest_packet_t_type::MX_GUEST_PKT_IO =>
                write!(f, "mx_guest_packet_t_union {{ io: {:?} }} }}",
                    unsafe { self.contents.io }
                ),
        }
    }
}

impl cmp::PartialEq for mx_guest_packet_t {
    fn eq(&self, other: &Self) -> bool {
        (self.packet_type == other.packet_type) &&
        match self.packet_type {
            mx_guest_packet_t_type::MX_GUEST_PKT_MEMORY =>
                unsafe { self.contents.memory == other.contents.memory },
            mx_guest_packet_t_type::MX_GUEST_PKT_IO =>
                unsafe { self.contents.io == other.contents.io },
        }
    }
}

impl cmp::Eq for mx_guest_packet_t {}

#[cfg(target_arch="x86_64")]
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct mx_vcpu_create_args_t {
    pub ip: mx_vaddr_t,
    pub cr3: mx_vaddr_t,
    pub apic_vmo: mx_handle_t,
}

#[cfg(not(target_arch="x86_64"))]
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct mx_vcpu_create_args_t {
    pub ip: mx_vaddr_t,
}

include!("definitions.rs");