Skip to main content

abi_application/
api.rs

1use crate::*;
2use core::ffi::{c_char, c_void};
3
4/// Native application entry points, in slot order. The header precedes slot zero.
5/// Networking slots return Unsupported when the runtime omits networking, and so does
6/// the graphics slot when it omits graphics.
7#[repr(C)]
8pub struct XilaApi {
9    /// Packed major and minor Xila version: `(major << 16) | minor`.
10    pub version: u32,
11    /// Total size in bytes, including this header.
12    pub size: u32,
13    pub directory_open: unsafe extern "C" fn(
14        parent: *const Directory,
15        path: *const c_char,
16        path_length: usize,
17        rights: u32,
18        directory: *mut *mut Directory,
19    ) -> Status,
20    pub directory_close: unsafe extern "C" fn(directory: *mut Directory) -> Status,
21    pub directory_create: unsafe extern "C" fn(
22        directory: *const Directory,
23        path: *const c_char,
24        length: usize,
25    ) -> Status,
26    pub directory_remove: unsafe extern "C" fn(
27        directory: *const Directory,
28        path: *const c_char,
29        length: usize,
30        flags: u32,
31    ) -> Status,
32    pub directory_rename: unsafe extern "C" fn(
33        from: *const Directory,
34        from_path: *const c_char,
35        from_length: usize,
36        to: *const Directory,
37        to_path: *const c_char,
38        to_length: usize,
39    ) -> Status,
40    pub file_open: unsafe extern "C" fn(
41        directory: *const Directory,
42        path: *const c_char,
43        path_length: usize,
44        flags: u32,
45        file: *mut *mut File,
46    ) -> Status,
47    pub file_read: unsafe extern "C" fn(
48        file: *mut File,
49        buffer: *mut c_void,
50        size: usize,
51        count: *mut usize,
52    ) -> Status,
53    pub file_write: unsafe extern "C" fn(
54        file: *mut File,
55        buffer: *const c_void,
56        size: usize,
57        count: *mut usize,
58    ) -> Status,
59    pub file_seek: unsafe extern "C" fn(
60        file: *mut File,
61        offset: i64,
62        whence: u32,
63        position: *mut u64,
64    ) -> Status,
65    pub file_flush: unsafe extern "C" fn(file: *mut File) -> Status,
66    pub file_close: unsafe extern "C" fn(file: *mut File) -> Status,
67    pub file_set_size: unsafe extern "C" fn(file: *mut File, size: u64) -> Status,
68    pub file_get_statistics:
69        unsafe extern "C" fn(file: *mut File, output: *mut Statistics) -> Status,
70    pub directory_get_statistics: unsafe extern "C" fn(
71        directory: *const Directory,
72        path: *const c_char,
73        length: usize,
74        flags: u32,
75        output: *mut Statistics,
76    ) -> Status,
77    pub directory_iterator_open: unsafe extern "C" fn(
78        directory: *const Directory,
79        path: *const c_char,
80        length: usize,
81        output: *mut *mut DirectoryIterator,
82    ) -> Status,
83    pub directory_iterator_next: unsafe extern "C" fn(
84        iterator: *mut DirectoryIterator,
85        name_buffer: *mut c_char,
86        capacity: usize,
87        name_length: *mut usize,
88        kind: *mut u32,
89    ) -> Status,
90    pub directory_iterator_rewind: unsafe extern "C" fn(iterator: *mut DirectoryIterator) -> Status,
91    pub directory_iterator_close: unsafe extern "C" fn(iterator: *mut DirectoryIterator) -> Status,
92    pub time_monotonic: unsafe extern "C" fn(nanoseconds: *mut u64) -> Status,
93    pub time_realtime: unsafe extern "C" fn(nanoseconds: *mut i64) -> Status,
94    pub network_close: unsafe extern "C" fn(network: *mut Network) -> Status,
95    pub tcp_connect: unsafe extern "C" fn(
96        network: *const Network,
97        address: *const NetworkAddress,
98        timeout_ms: u32,
99        connection: *mut *mut Tcp,
100    ) -> Status,
101    pub tcp_listen: unsafe extern "C" fn(
102        network: *const Network,
103        address: *const NetworkAddress,
104        listener: *mut *mut TcpListener,
105    ) -> Status,
106    pub tcp_accept:
107        unsafe extern "C" fn(listener: *mut TcpListener, connection: *mut *mut Tcp) -> Status,
108    pub tcp_close: unsafe extern "C" fn(socket: *mut Tcp) -> Status,
109    pub tcp_listener_close: unsafe extern "C" fn(socket: *mut TcpListener) -> Status,
110    pub tcp_shutdown: unsafe extern "C" fn(socket: *mut Tcp, direction: u32) -> Status,
111    pub tcp_send: unsafe extern "C" fn(
112        socket: *mut Tcp,
113        buffer: *const c_void,
114        size: usize,
115        count: *mut usize,
116    ) -> Status,
117    pub tcp_receive: unsafe extern "C" fn(
118        socket: *mut Tcp,
119        buffer: *mut c_void,
120        size: usize,
121        count: *mut usize,
122    ) -> Status,
123    pub tcp_local_address:
124        unsafe extern "C" fn(socket: *const Tcp, address: *mut NetworkAddress) -> Status,
125    pub tcp_peer_address:
126        unsafe extern "C" fn(socket: *const Tcp, address: *mut NetworkAddress) -> Status,
127    pub udp_bind: unsafe extern "C" fn(
128        network: *const Network,
129        address: *const NetworkAddress,
130        socket: *mut *mut Udp,
131    ) -> Status,
132    pub udp_close: unsafe extern "C" fn(socket: *mut Udp) -> Status,
133    pub udp_send_to: unsafe extern "C" fn(
134        socket: *mut Udp,
135        buffer: *const c_void,
136        size: usize,
137        address: *const NetworkAddress,
138        count: *mut usize,
139    ) -> Status,
140    pub udp_receive_from: unsafe extern "C" fn(
141        socket: *mut Udp,
142        buffer: *mut c_void,
143        size: usize,
144        address: *mut NetworkAddress,
145        count: *mut usize,
146    ) -> Status,
147    pub network_resolve: unsafe extern "C" fn(
148        network: *const Network,
149        host: *const c_char,
150        host_length: usize,
151        family: u32,
152        addresses: *mut NetworkAddress,
153        count: *mut usize,
154    ) -> Status,
155    /// Allocate uninitialized memory aligned to XILA_HEAP_ALIGNMENT.
156    pub heap_allocate:
157        unsafe extern "C" fn(heap: *mut Heap, size: usize, allocation: *mut *mut c_void) -> Status,
158    /// Resize an allocation. Zero size frees the block and stores null.
159    pub heap_reallocate: unsafe extern "C" fn(
160        heap: *mut Heap,
161        pointer: *mut c_void,
162        size: usize,
163        allocation: *mut *mut c_void,
164    ) -> Status,
165    /// Release an allocation from this heap. Null succeeds.
166    pub heap_free: unsafe extern "C" fn(heap: *mut Heap, pointer: *mut c_void) -> Status,
167    /// Read a clock's nominal resolution: XILA_CLOCK_MONOTONIC or XILA_CLOCK_REALTIME.
168    /// Returns Unavailable when the host installed no clock.
169    pub time_resolution: unsafe extern "C" fn(clock: u32, nanoseconds: *mut u64) -> Status,
170    /// Suspend for at least the given nanoseconds on the executor's clock. A zero wait
171    /// yields once inside a coroutine and returns at once outside one.
172    pub time_sleep: unsafe extern "C" fn(nanoseconds: u64) -> Status,
173    /// Fill a buffer from the host-granted entropy source. Zero size succeeds. The
174    /// buffer contents are unspecified after a failure.
175    pub random_fill: unsafe extern "C" fn(buffer: *mut c_void, size: usize) -> Status,
176    /// Read the process's identifier.
177    pub process_id: unsafe extern "C" fn(process: *const Process, id: *mut u32) -> Status,
178    /// Read the identifier of the process that spawned this one, or zero (the root
179    /// task) once that process has exited.
180    pub process_parent_id: unsafe extern "C" fn(process: *const Process, id: *mut u32) -> Status,
181    /// End the process with an exit status. Does not return on success; outside a
182    /// native process coroutine it returns Unsupported.
183    pub process_exit: unsafe extern "C" fn(process: *const Process, status: i32) -> Status,
184    /// Start an executable below a directory capability as a child process.
185    pub process_spawn: unsafe extern "C" fn(
186        process: *const Process,
187        directory: *const Directory,
188        path: *const c_char,
189        path_length: usize,
190        spawn: *const Spawn,
191        child: *mut *mut Child,
192    ) -> Status,
193    /// Collect a child's exit status. Only the spawning process may wait; flags:
194    /// XILA_WAIT_NO_HANG.
195    pub process_wait: unsafe extern "C" fn(
196        process: *const Process,
197        child: *mut Child,
198        flags: u32,
199        status: *mut i32,
200    ) -> Status,
201    /// Release a child handle. Null succeeds.
202    pub process_child_close: unsafe extern "C" fn(child: *mut Child) -> Status,
203    /// Read an environment variable; BufferTooSmall stores the required length.
204    pub env_get: unsafe extern "C" fn(
205        process: *const Process,
206        name: *const c_char,
207        name_length: usize,
208        buffer: *mut c_char,
209        capacity: usize,
210        value_length: *mut usize,
211    ) -> Status,
212    /// Set an environment variable of this process.
213    pub env_set: unsafe extern "C" fn(
214        process: *const Process,
215        name: *const c_char,
216        name_length: usize,
217        value: *const c_char,
218        value_length: usize,
219    ) -> Status,
220    /// Remove an environment variable of this process. A missing one succeeds.
221    pub env_remove: unsafe extern "C" fn(
222        process: *const Process,
223        name: *const c_char,
224        name_length: usize,
225    ) -> Status,
226
227    /// Read a counted NAME=VALUE entry by index; NotFound marks the end.
228    pub env_entry: unsafe extern "C" fn(
229        process: *const Process,
230        index: usize,
231        buffer: *mut c_char,
232        capacity: usize,
233        entry_length: *mut usize,
234    ) -> Status,
235
236    /// Spawn a worker of the current process; stack_size zero chooses the default.
237    pub thread_spawn: unsafe extern "C" fn(
238        process: *const Process,
239        stack_size: usize,
240        entry: ThreadEntry,
241        argument: *mut c_void,
242        thread: *mut usize,
243    ) -> Status,
244    /// Collect the worker result. Only the thread that created it may join.
245    pub thread_join: unsafe extern "C" fn(thread: usize, result: *mut *mut c_void) -> Status,
246    pub thread_detach: unsafe extern "C" fn(thread: usize) -> Status,
247    pub thread_yield: unsafe extern "C" fn() -> Status,
248    pub thread_current: unsafe extern "C" fn(thread: *mut usize) -> Status,
249    pub thread_key_create: unsafe extern "C" fn(key: *mut usize) -> Status,
250    pub thread_key_delete: unsafe extern "C" fn(key: usize) -> Status,
251    pub thread_tls_get: unsafe extern "C" fn(key: usize, value: *mut *mut c_void) -> Status,
252    pub thread_tls_set: unsafe extern "C" fn(key: usize, value: *mut c_void) -> Status,
253    /// Address of the SDK-reserved libc state, valid until the current thread ends.
254    pub thread_libc: unsafe extern "C" fn(location: *mut *mut ThreadLibc) -> Status,
255    pub mutex_init: unsafe extern "C" fn(mutex: *mut Mutex, flags: u32) -> Status,
256    pub mutex_destroy: unsafe extern "C" fn(mutex: *mut Mutex) -> Status,
257    /// A contended XILA_MUTEX_TRY returns WouldBlock.
258    pub mutex_lock: unsafe extern "C" fn(mutex: *mut Mutex, flags: u32) -> Status,
259    pub mutex_unlock: unsafe extern "C" fn(mutex: *mut Mutex) -> Status,
260    pub condition_init: unsafe extern "C" fn(condition: *mut Condition) -> Status,
261    pub condition_destroy: unsafe extern "C" fn(condition: *mut Condition) -> Status,
262    /// Unlock and sleep atomically, then reacquire before returning. A null deadline
263    /// means infinite; otherwise use absolute nanoseconds on the selected ABI clock.
264    pub condition_wait: unsafe extern "C" fn(
265        condition: *mut Condition,
266        mutex: *mut Mutex,
267        clock: u32,
268        deadline: *const i64,
269    ) -> Status,
270    pub condition_notify: unsafe extern "C" fn(condition: *mut Condition, all: bool) -> Status,
271    /// Report whether any worker is live, including the caller if it is a worker.
272    pub thread_has_workers: unsafe extern "C" fn(live: *mut bool) -> Status,
273    /// Begin terminal cleanup. The process driver will only resume the caller.
274    pub thread_stop_others: unsafe extern "C" fn(process: *const Process) -> Status,
275    /// End a worker with a join result. This returns only on failure.
276    pub thread_exit: unsafe extern "C" fn(result: *mut c_void) -> Status,
277
278    /// Wait for readiness across file, pipe, directory and socket handles.
279    /// timeout_ms is relative; negative means infinite, zero probes once.
280    pub poll_wait: unsafe extern "C" fn(
281        entries: *mut PollEntry,
282        count: usize,
283        timeout_ms: i32,
284        ready_count: *mut usize,
285    ) -> Status,
286    /// Transfer from an explicit offset without changing the shared cursor.
287    pub file_read_at: unsafe extern "C" fn(
288        file: *mut File,
289        buffer: *mut c_void,
290        size: usize,
291        offset: u64,
292        count: *mut usize,
293    ) -> Status,
294    /// Transfer to an explicit offset without changing the shared cursor.
295    pub file_write_at: unsafe extern "C" fn(
296        file: *mut File,
297        buffer: *const c_void,
298        size: usize,
299        offset: u64,
300        count: *mut usize,
301    ) -> Status,
302    /// Return an owned file that shares the original cursor and status flags.
303    pub file_duplicate: unsafe extern "C" fn(file: *const File, output: *mut *mut File) -> Status,
304    /// Query queued pipe bytes or get/set file flags using a uint32_t buffer.
305    pub file_control: unsafe extern "C" fn(
306        file: *mut File,
307        command: u32,
308        input: *const c_void,
309        input_size: usize,
310        output: *mut c_void,
311        output_size: usize,
312    ) -> Status,
313    /// Report whether an open file is an interactive terminal.
314    pub file_is_terminal: unsafe extern "C" fn(file: *mut File, terminal: *mut bool) -> Status,
315    /// Create the read and write handles of an unnamed pipe.
316    /// The only supported flag is XILA_OPEN_NON_BLOCKING.
317    pub pipe_create: unsafe extern "C" fn(
318        process: *const Process,
319        flags: u32,
320        reader: *mut *mut File,
321        writer: *mut *mut File,
322    ) -> Status,
323    /// Run one graphics call by its ID from `xila/graphics.h`. `arguments` holds
324    /// `argument_count` words (at most seven; null at zero) and `result` receives the
325    /// call's word or 32-bit result. The SDK's generated functions pack the words.
326    pub graphics_call: unsafe extern "C" fn(
327        process: *const Process,
328        function: u32,
329        arguments: *const usize,
330        argument_count: usize,
331        result: *mut c_void,
332    ) -> Status,
333    /// Read a child's process identifier, the value its `process_id` reports.
334    pub process_child_id: unsafe extern "C" fn(child: *const Child, id: *mut u32) -> Status,
335}
336
337include!(concat!(env!("OUT_DIR"), "/api_layout.rs"));
338
339#[cfg(not(feature = "network"))]
340include!(concat!(env!("OUT_DIR"), "/disabled_network.rs"));
341
342#[cfg(feature = "graphics")]
343use crate::xila_graphics_call;
344#[cfg(not(feature = "graphics"))]
345include!(concat!(env!("OUT_DIR"), "/disabled_graphics.rs"));
346
347/// Shared API table. Native code receives its address through XilaStartup.
348pub static XILA_API: XilaApi = XilaApi {
349    version: (XILA_VERSION_MAJOR << 16) | XILA_VERSION_MINOR,
350    size: core::mem::size_of::<XilaApi>() as u32,
351    directory_open: xila_directory_open,
352    directory_close: xila_directory_close,
353    directory_create: xila_directory_create,
354    directory_remove: xila_directory_remove,
355    directory_rename: xila_directory_rename,
356    file_open: xila_file_open,
357    file_read: xila_file_read,
358    file_write: xila_file_write,
359    file_seek: xila_file_seek,
360    file_flush: xila_file_flush,
361    file_close: xila_file_close,
362    file_set_size: xila_file_set_size,
363    file_get_statistics: xila_file_get_statistics,
364    directory_get_statistics: xila_directory_get_statistics,
365    directory_iterator_open: xila_directory_iterator_open,
366    directory_iterator_next: xila_directory_iterator_next,
367    directory_iterator_rewind: xila_directory_iterator_rewind,
368    directory_iterator_close: xila_directory_iterator_close,
369    time_monotonic: xila_time_monotonic,
370    time_realtime: xila_time_realtime,
371    network_close: xila_network_close,
372    tcp_connect: xila_tcp_connect,
373    tcp_listen: xila_tcp_listen,
374    tcp_accept: xila_tcp_accept,
375    tcp_close: xila_tcp_close,
376    tcp_listener_close: xila_tcp_listener_close,
377    tcp_shutdown: xila_tcp_shutdown,
378    tcp_send: xila_tcp_send,
379    tcp_receive: xila_tcp_receive,
380    tcp_local_address: xila_tcp_local_address,
381    tcp_peer_address: xila_tcp_peer_address,
382    udp_bind: xila_udp_bind,
383    udp_close: xila_udp_close,
384    udp_send_to: xila_udp_send_to,
385    udp_receive_from: xila_udp_receive_from,
386    network_resolve: xila_network_resolve,
387    heap_allocate: xila_heap_allocate,
388    heap_reallocate: xila_heap_reallocate,
389    heap_free: xila_heap_free,
390    time_resolution: xila_time_resolution,
391    time_sleep: xila_time_sleep,
392    random_fill: xila_random_fill,
393    process_id: xila_process_id,
394    process_parent_id: xila_process_parent_id,
395    process_exit: xila_process_exit,
396    process_spawn: xila_process_spawn,
397    process_wait: xila_process_wait,
398    process_child_close: xila_process_child_close,
399    env_get: xila_env_get,
400    env_set: xila_env_set,
401    env_remove: xila_env_remove,
402    env_entry: xila_env_entry,
403    thread_spawn: xila_thread_spawn,
404    thread_join: xila_thread_join,
405    thread_detach: xila_thread_detach,
406    thread_yield: xila_thread_yield,
407    thread_current: xila_thread_current,
408    thread_key_create: xila_thread_key_create,
409    thread_key_delete: xila_thread_key_delete,
410    thread_tls_get: xila_thread_tls_get,
411    thread_tls_set: xila_thread_tls_set,
412    thread_libc: xila_thread_libc,
413    mutex_init: xila_mutex_init,
414    mutex_destroy: xila_mutex_destroy,
415    mutex_lock: xila_mutex_lock,
416    mutex_unlock: xila_mutex_unlock,
417    condition_init: xila_condition_init,
418    condition_destroy: xila_condition_destroy,
419    condition_wait: xila_condition_wait,
420    condition_notify: xila_condition_notify,
421    thread_has_workers: xila_thread_has_workers,
422    thread_stop_others: xila_thread_stop_others,
423    thread_exit: xila_thread_exit,
424    poll_wait: xila_poll_wait,
425    file_read_at: xila_file_read_at,
426    file_write_at: xila_file_write_at,
427    file_duplicate: xila_file_duplicate,
428    file_control: xila_file_control,
429    file_is_terminal: xila_file_is_terminal,
430    pipe_create: xila_pipe_create,
431    graphics_call: xila_graphics_call,
432    process_child_id: xila_process_child_id,
433};