magenta_sys/
lib.rs

1// Copyright 2016 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#![allow(non_camel_case_types)]
6
7#[macro_use]
8extern crate bitflags;
9
10use std::{cmp, fmt};
11
12pub type mx_handle_t = i32;
13
14pub type mx_status_t = i32;
15
16pub type mx_futex_t = isize;
17pub type mx_addr_t = usize;
18pub type mx_paddr_t = usize;
19pub type mx_vaddr_t = usize;
20
21// Auto-generated using tools/gen_status.py
22pub const MX_OK                    : mx_status_t = 0;
23pub const MX_ERR_INTERNAL          : mx_status_t = -1;
24pub const MX_ERR_NOT_SUPPORTED     : mx_status_t = -2;
25pub const MX_ERR_NO_RESOURCES      : mx_status_t = -3;
26pub const MX_ERR_NO_MEMORY         : mx_status_t = -4;
27pub const MX_ERR_CALL_FAILED       : mx_status_t = -5;
28pub const MX_ERR_INTERRUPTED_RETRY : mx_status_t = -6;
29pub const MX_ERR_INVALID_ARGS      : mx_status_t = -10;
30pub const MX_ERR_BAD_HANDLE        : mx_status_t = -11;
31pub const MX_ERR_WRONG_TYPE        : mx_status_t = -12;
32pub const MX_ERR_BAD_SYSCALL       : mx_status_t = -13;
33pub const MX_ERR_OUT_OF_RANGE      : mx_status_t = -14;
34pub const MX_ERR_BUFFER_TOO_SMALL  : mx_status_t = -15;
35pub const MX_ERR_BAD_STATE         : mx_status_t = -20;
36pub const MX_ERR_TIMED_OUT         : mx_status_t = -21;
37pub const MX_ERR_SHOULD_WAIT       : mx_status_t = -22;
38pub const MX_ERR_CANCELED          : mx_status_t = -23;
39pub const MX_ERR_PEER_CLOSED       : mx_status_t = -24;
40pub const MX_ERR_NOT_FOUND         : mx_status_t = -25;
41pub const MX_ERR_ALREADY_EXISTS    : mx_status_t = -26;
42pub const MX_ERR_ALREADY_BOUND     : mx_status_t = -27;
43pub const MX_ERR_UNAVAILABLE       : mx_status_t = -28;
44pub const MX_ERR_ACCESS_DENIED     : mx_status_t = -30;
45pub const MX_ERR_IO                : mx_status_t = -40;
46pub const MX_ERR_IO_REFUSED        : mx_status_t = -41;
47pub const MX_ERR_IO_DATA_INTEGRITY : mx_status_t = -42;
48pub const MX_ERR_IO_DATA_LOSS      : mx_status_t = -43;
49pub const MX_ERR_BAD_PATH          : mx_status_t = -50;
50pub const MX_ERR_NOT_DIR           : mx_status_t = -51;
51pub const MX_ERR_NOT_FILE          : mx_status_t = -52;
52pub const MX_ERR_FILE_BIG          : mx_status_t = -53;
53pub const MX_ERR_NO_SPACE          : mx_status_t = -54;
54pub const MX_ERR_STOP              : mx_status_t = -60;
55pub const MX_ERR_NEXT              : mx_status_t = -61;
56
57pub type mx_time_t = u64;
58pub type mx_duration_t = u64;
59pub const MX_TIME_INFINITE : mx_time_t = ::std::u64::MAX;
60
61bitflags! {
62    #[repr(C)]
63    pub flags mx_signals_t: u32 {
64        const MX_SIGNAL_NONE              = 0,
65        const MX_OBJECT_SIGNAL_ALL        = 0x00ffffff,
66        const MX_USER_SIGNAL_ALL          = 0xff000000,
67        const MX_OBJECT_SIGNAL_0          = 1 << 0,
68        const MX_OBJECT_SIGNAL_1          = 1 << 1,
69        const MX_OBJECT_SIGNAL_2          = 1 << 2,
70        const MX_OBJECT_SIGNAL_3          = 1 << 3,
71        const MX_OBJECT_SIGNAL_4          = 1 << 4,
72        const MX_OBJECT_SIGNAL_5          = 1 << 5,
73        const MX_OBJECT_SIGNAL_6          = 1 << 6,
74        const MX_OBJECT_SIGNAL_7          = 1 << 7,
75        const MX_OBJECT_SIGNAL_8          = 1 << 8,
76        const MX_OBJECT_SIGNAL_9          = 1 << 9,
77        const MX_OBJECT_SIGNAL_10         = 1 << 10,
78        const MX_OBJECT_SIGNAL_11         = 1 << 11,
79        const MX_OBJECT_SIGNAL_12         = 1 << 12,
80        const MX_OBJECT_SIGNAL_13         = 1 << 13,
81        const MX_OBJECT_SIGNAL_14         = 1 << 14,
82        const MX_OBJECT_SIGNAL_15         = 1 << 15,
83        const MX_OBJECT_SIGNAL_16         = 1 << 16,
84        const MX_OBJECT_SIGNAL_17         = 1 << 17,
85        const MX_OBJECT_SIGNAL_18         = 1 << 18,
86        const MX_OBJECT_SIGNAL_19         = 1 << 19,
87        const MX_OBJECT_SIGNAL_20         = 1 << 20,
88        const MX_OBJECT_SIGNAL_21         = 1 << 21,
89        const MX_OBJECT_LAST_HANDLE       = 1 << 22,
90        const MX_OBJECT_HANDLE_CLOSED     = 1 << 23,
91        const MX_USER_SIGNAL_0            = 1 << 24,
92        const MX_USER_SIGNAL_1            = 1 << 25,
93        const MX_USER_SIGNAL_2            = 1 << 26,
94        const MX_USER_SIGNAL_3            = 1 << 27,
95        const MX_USER_SIGNAL_4            = 1 << 28,
96        const MX_USER_SIGNAL_5            = 1 << 29,
97        const MX_USER_SIGNAL_6            = 1 << 30,
98        const MX_USER_SIGNAL_7            = 1 << 31,
99
100        const MX_OBJECT_READABLE          = MX_OBJECT_SIGNAL_0.bits,
101        const MX_OBJECT_WRITABLE          = MX_OBJECT_SIGNAL_1.bits,
102        const MX_OBJECT_PEER_CLOSED       = MX_OBJECT_SIGNAL_2.bits,
103
104        // Cancelation (handle was closed while waiting with it)
105        const MX_SIGNAL_HANDLE_CLOSED     = MX_OBJECT_HANDLE_CLOSED.bits,
106
107        // Only one user-more reference (handle) to the object exists.
108        const MX_SIGNAL_LAST_HANDLE       = MX_OBJECT_LAST_HANDLE.bits,
109
110        // Event
111        const MX_EVENT_SIGNALED           = MX_OBJECT_SIGNAL_3.bits,
112
113        // EventPair
114        const MX_EPAIR_SIGNALED           = MX_OBJECT_SIGNAL_3.bits,
115        const MX_EPAIR_CLOSED             = MX_OBJECT_SIGNAL_2.bits,
116
117        // Task signals (process, thread, job)
118        const MX_TASK_TERMINATED          = MX_OBJECT_SIGNAL_3.bits,
119
120        // Channel
121        const MX_CHANNEL_READABLE         = MX_OBJECT_SIGNAL_0.bits,
122        const MX_CHANNEL_WRITABLE         = MX_OBJECT_SIGNAL_1.bits,
123        const MX_CHANNEL_PEER_CLOSED      = MX_OBJECT_SIGNAL_2.bits,
124
125        // Socket
126        const MX_SOCKET_READABLE          = MX_OBJECT_SIGNAL_0.bits,
127        const MX_SOCKET_WRITABLE          = MX_OBJECT_SIGNAL_1.bits,
128        const MX_SOCKET_PEER_CLOSED       = MX_OBJECT_SIGNAL_2.bits,
129
130        // Port
131        const MX_PORT_READABLE            = MX_OBJECT_READABLE.bits,
132
133        // Resource
134        const MX_RESOURCE_DESTROYED       = MX_OBJECT_SIGNAL_3.bits,
135        const MX_RESOURCE_READABLE        = MX_OBJECT_READABLE.bits,
136        const MX_RESOURCE_WRITABLE        = MX_OBJECT_WRITABLE.bits,
137        const MX_RESOURCE_CHILD_ADDED     = MX_OBJECT_SIGNAL_4.bits,
138
139        // Fifo
140        const MX_FIFO_READABLE            = MX_OBJECT_READABLE.bits,
141        const MX_FIFO_WRITABLE            = MX_OBJECT_WRITABLE.bits,
142        const MX_FIFO_PEER_CLOSED         = MX_OBJECT_PEER_CLOSED.bits,
143
144        // Job
145        const MX_JOB_NO_PROCESSES         = MX_OBJECT_SIGNAL_3.bits,
146        const MX_JOB_NO_JOBS              = MX_OBJECT_SIGNAL_4.bits,
147
148        // Process
149        const MX_PROCESS_TERMINATED       = MX_OBJECT_SIGNAL_3.bits,
150
151        // Thread
152        const MX_THREAD_TERMINATED        = MX_OBJECT_SIGNAL_3.bits,
153
154        // Log
155        const MX_LOG_READABLE             = MX_OBJECT_READABLE.bits,
156        const MX_LOG_WRITABLE             = MX_OBJECT_WRITABLE.bits,
157
158        // Timer
159        const MX_TIMER_SIGNALED           = MX_OBJECT_SIGNAL_3.bits,
160    }
161}
162
163pub type mx_size_t = usize;
164pub type mx_ssize_t = isize;
165
166bitflags! {
167    #[repr(C)]
168    pub flags mx_rights_t: u32 {
169        const MX_RIGHT_NONE         = 0,
170        const MX_RIGHT_DUPLICATE    = 1 << 0,
171        const MX_RIGHT_TRANSFER     = 1 << 1,
172        const MX_RIGHT_READ         = 1 << 2,
173        const MX_RIGHT_WRITE        = 1 << 3,
174        const MX_RIGHT_EXECUTE      = 1 << 4,
175        const MX_RIGHT_MAP          = 1 << 5,
176        const MX_RIGHT_GET_PROPERTY = 1 << 6,
177        const MX_RIGHT_SET_PROPERTY = 1 << 7,
178        const MX_RIGHT_DEBUG        = 1 << 8,
179        const MX_RIGHT_SAME_RIGHTS  = 1 << 31,
180    }
181}
182
183// clock ids
184pub const MX_CLOCK_MONOTONIC: u32 = 0;
185
186// Buffer size limits on the cprng syscalls
187pub const MX_CPRNG_DRAW_MAX_LEN: usize = 256;
188pub const MX_CPRNG_ADD_ENTROPY_MAX_LEN: usize = 256;
189
190// Socket flags and limits.
191pub const MX_SOCKET_HALF_CLOSE: u32 = 1;
192
193// VM Object opcodes
194pub const MX_VMO_OP_COMMIT: u32 = 1;
195pub const MX_VMO_OP_DECOMMIT: u32 = 2;
196pub const MX_VMO_OP_LOCK: u32 = 3;
197pub const MX_VMO_OP_UNLOCK: u32 = 4;
198pub const MX_VMO_OP_LOOKUP: u32 = 5;
199pub const MX_VMO_OP_CACHE_SYNC: u32 = 6;
200pub const MX_VMO_OP_CACHE_INVALIDATE: u32 = 7;
201pub const MX_VMO_OP_CACHE_CLEAN: u32 = 8;
202pub const MX_VMO_OP_CACHE_CLEAN_INVALIDATE: u32 = 9;
203
204// VM Object clone flags
205pub const MX_VMO_CLONE_COPY_ON_WRITE: u32 = 1;
206
207#[repr(C)]
208#[derive(Debug, Copy, Clone, Eq, PartialEq)]
209pub enum mx_cache_policy_t {
210    MX_CACHE_POLICY_CACHED = 0,
211    MX_CACHE_POLICY_UNCACHED = 1,
212    MX_CACHE_POLICY_UNCACHED_DEVICE = 2,
213    MX_CACHE_POLICY_WRITE_COMBINING = 3,
214}
215
216#[repr(C)]
217#[derive(Debug, Copy, Clone, Eq, PartialEq)]
218pub struct mx_wait_item_t {
219    pub handle: mx_handle_t,
220    pub waitfor: mx_signals_t,
221    pub pending: mx_signals_t,
222}
223
224#[repr(C)]
225#[derive(Debug, Copy, Clone, Eq, PartialEq)]
226pub struct mx_waitset_result_t {
227    pub cookie: u64,
228    pub status: mx_status_t,
229    pub observed: mx_signals_t,
230}
231
232#[repr(C)]
233#[derive(Debug, Copy, Clone, Eq, PartialEq)]
234pub struct mx_channel_call_args_t {
235    pub wr_bytes: *const u8,
236    pub wr_handles: *const mx_handle_t,
237    pub rd_bytes: *mut u8,
238    pub rd_handles: *mut mx_handle_t,
239    pub wr_num_bytes: u32,
240    pub wr_num_handles: u32,
241    pub rd_num_bytes: u32,
242    pub rd_num_handles: u32,
243}
244
245pub type mx_pci_irq_swizzle_lut_t = [[[u32; 4]; 8]; 32];
246
247#[repr(C)]
248#[derive(Debug, Copy, Clone, Eq, PartialEq)]
249pub struct mx_pci_init_arg_t {
250    pub dev_pin_to_global_irq: mx_pci_irq_swizzle_lut_t,
251    pub num_irqs: u32,
252    pub irqs: [mx_irq_t; 32],
253    pub ecam_window_count: u32,
254    // Note: the ecam_windows field is actually a variable size array.
255    // We use a fixed size array to match the C repr.
256    pub ecam_windows: [mx_ecam_window_t; 1],
257}
258
259#[repr(C)]
260#[derive(Debug, Copy, Clone, Eq, PartialEq)]
261pub struct mx_irq_t {
262    pub global_irq: u32,
263    pub level_triggered: bool,
264    pub active_high: bool,
265}
266
267#[repr(C)]
268#[derive(Debug, Copy, Clone, Eq, PartialEq)]
269pub struct mx_ecam_window_t {
270    pub base: u64,
271    pub size: usize,
272    pub bus_start: u8,
273    pub bus_end: u8,
274}
275
276#[repr(C)]
277#[derive(Debug, Copy, Clone, Eq, PartialEq)]
278pub struct mx_pcie_device_info_t {
279    pub vendor_id: u16,
280    pub device_id: u16,
281    pub base_class: u8,
282    pub sub_class: u8,
283    pub program_interface: u8,
284    pub revision_id: u8,
285    pub bus_id: u8,
286    pub dev_id: u8,
287    pub func_id: u8,
288}
289
290#[repr(C)]
291#[derive(Debug, Copy, Clone, Eq, PartialEq)]
292pub struct mx_pci_resource_t {
293    pub type_: u32,
294    pub size: usize,
295    // TODO: Actually a union
296    pub pio_addr: usize,
297}
298
299// TODO: Actually a union
300pub type mx_rrec_t = [u8; 64];
301
302// Ports V2
303#[repr(u32)]
304#[derive(Debug, Copy, Clone, Eq, PartialEq)]
305pub enum mx_packet_type_t {
306    MX_PKT_TYPE_USER = 0,
307    MX_PKT_TYPE_SIGNAL_ONE = 1,
308    MX_PKT_TYPE_SIGNAL_REP = 2,
309}
310
311impl Default for mx_packet_type_t {
312    fn default() -> Self {
313        mx_packet_type_t::MX_PKT_TYPE_USER
314    }
315}
316
317#[repr(C)]
318#[derive(Debug, Copy, Clone)]
319pub struct mx_packet_signal_t {
320    pub trigger: mx_signals_t,
321    pub observed: mx_signals_t,
322    pub count: u64,
323}
324
325pub const MX_WAIT_ASYNC_ONCE: u32 = 0;
326pub const MX_WAIT_ASYNC_REPEATING: u32 = 1;
327
328// Actually a union of different integer types, but this should be good enough.
329pub type mx_packet_user_t = [u8; 32];
330
331#[repr(C)]
332#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
333pub struct mx_port_packet_t {
334    pub key: u64,
335    pub packet_type: mx_packet_type_t,
336    pub status: i32,
337    pub union: [u8; 32],
338}
339
340#[repr(C)]
341#[derive(Debug, Copy, Clone, Eq, PartialEq)]
342pub struct mx_guest_io_t {
343    port: u16,
344    access_size: u8,
345    input: bool,
346    // TODO: Actually a union
347    data: [u8; 4],
348}
349
350#[cfg(target_arch="aarch64")]
351#[repr(C)]
352#[derive(Debug, Copy, Clone, Eq, PartialEq)]
353pub struct mx_guest_memory_t {
354    addr: mx_vaddr_t,
355    inst: u32,
356}
357
358pub const X86_MAX_INST_LEN: usize = 15;
359
360#[cfg(target_arch="x86_64")]
361#[repr(C)]
362#[derive(Debug, Copy, Clone, Eq, PartialEq)]
363pub struct mx_guest_memory_t {
364    addr: mx_vaddr_t,
365    inst_len: u8,
366    inst_buf: [u8; X86_MAX_INST_LEN],
367}
368
369#[repr(u8)]
370#[derive(Debug, Copy, Clone, Eq, PartialEq)]
371pub enum mx_guest_packet_t_type {
372    MX_GUEST_PKT_MEMORY = 1,
373    MX_GUEST_PKT_IO = 2,
374}
375
376#[repr(C)]
377#[derive(Copy, Clone)]
378pub union mx_guest_packet_t_union {
379    // MX_GUEST_PKT_MEMORY
380    memory: mx_guest_memory_t,
381    // MX_GUEST_PKT_IO
382    io: mx_guest_io_t,
383}
384
385// Note: values of this type must maintain the invariant that
386// `packet_type` correctly indicates the type of `contents`.
387// Failure to do so will result in unsafety.
388#[repr(C)]
389#[derive(Copy, Clone)]
390pub struct mx_guest_packet_t {
391    packet_type: mx_guest_packet_t_type,
392    contents: mx_guest_packet_t_union,
393}
394
395impl fmt::Debug for mx_guest_packet_t {
396    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
397        write!(f, "mx_guest_packet_t {{ packet_type: {:?}, contents: ", self.packet_type)?;
398        match self.packet_type {
399            mx_guest_packet_t_type::MX_GUEST_PKT_MEMORY =>
400                write!(f, "mx_guest_packet_t_union {{ memory: {:?} }} }}",
401                    unsafe { self.contents.memory }
402                ),
403            mx_guest_packet_t_type::MX_GUEST_PKT_IO =>
404                write!(f, "mx_guest_packet_t_union {{ io: {:?} }} }}",
405                    unsafe { self.contents.io }
406                ),
407        }
408    }
409}
410
411impl cmp::PartialEq for mx_guest_packet_t {
412    fn eq(&self, other: &Self) -> bool {
413        (self.packet_type == other.packet_type) &&
414        match self.packet_type {
415            mx_guest_packet_t_type::MX_GUEST_PKT_MEMORY =>
416                unsafe { self.contents.memory == other.contents.memory },
417            mx_guest_packet_t_type::MX_GUEST_PKT_IO =>
418                unsafe { self.contents.io == other.contents.io },
419        }
420    }
421}
422
423impl cmp::Eq for mx_guest_packet_t {}
424
425#[cfg(target_arch="x86_64")]
426#[repr(C)]
427#[derive(Debug, Copy, Clone, Eq, PartialEq)]
428pub struct mx_vcpu_create_args_t {
429    pub ip: mx_vaddr_t,
430    pub cr3: mx_vaddr_t,
431    pub apic_vmo: mx_handle_t,
432}
433
434#[cfg(not(target_arch="x86_64"))]
435#[repr(C)]
436#[derive(Debug, Copy, Clone, Eq, PartialEq)]
437pub struct mx_vcpu_create_args_t {
438    pub ip: mx_vaddr_t,
439}
440
441include!("definitions.rs");