native_loader/run.rs
1extern crate alloc;
2
3use alloc::{string::String, vec::Vec};
4
5use abi_application::{File, NativeEntry, ProcessLaunch, ProcessOwner, drive_owned};
6use stackful::Coroutine;
7
8use crate::{Error, LoadedImage};
9
10/// Capabilities and arguments owned by the loaded program's coroutine.
11#[derive(Default)]
12pub struct Startup {
13 pub arguments: Vec<String>,
14 pub standard_input: Option<File>,
15 pub standard_output: Option<File>,
16 pub standard_error: Option<File>,
17 /// Makes the program a process: it gets a process capability, its explicit
18 /// environment if any and its home directory. Without it the program is
19 /// started like a linked one, with none of these, and cannot exit early.
20 pub process: Option<ProcessLaunch>,
21}
22
23impl LoadedImage {
24 /// Build the coroutine entry of the program and the owner of what it runs on.
25 ///
26 /// # Safety
27 /// Same as [`LoadedImage::spawn`].
28 unsafe fn prepare(
29 self,
30 startup: Startup,
31 ) -> Result<(impl FnOnce() -> i32 + 'static, ProcessOwner<Self>), Error> {
32 // SAFETY: the loader validated the entry point, and the owner keeps the image
33 // mapped until the program can no longer run.
34 let entry: NativeEntry = unsafe { core::mem::transmute(self.entry_address().as_ptr()) };
35 let Startup {
36 arguments,
37 standard_input,
38 standard_output,
39 standard_error,
40 process: launch,
41 } = startup;
42 let owner = ProcessOwner::from_startup(
43 self,
44 arguments,
45 standard_input,
46 standard_output,
47 standard_error,
48 )
49 .map_err(|_| Error::AllocationFailed)?;
50 let run = unsafe { owner.entry(entry, launch) };
51 Ok((run, owner))
52 }
53
54 /// Prepare to run the ELF entry point on the calling task's executor.
55 ///
56 /// Allocating the process state can fail with [`Error::AllocationFailed`],
57 /// which releases the image, the arguments and the streams. Otherwise the returned
58 /// future spawns the task and resolves to its join handle and identifier, like
59 /// `stackful::spawn_with_result`.
60 ///
61 /// The image, the process heap and the host state are released when the task finishes
62 /// or is cancelled, or when the future is dropped before it completes. So is
63 /// everything the program holds. A program that returns, exits or is cancelled has
64 /// its arguments, its startup streams and its graphics objects released, and so are
65 /// the files, directories, listings, children and sockets it opened and did not close.
66 /// A cancelled program is a task future that is dropped while its coroutine is
67 /// suspended: its coroutine stack is freed, with no destructor run on it.
68 ///
69 /// # Safety
70 /// The image must come from a trusted source and implement
71 /// `int32_t _xila_start(const XilaStartup *)`. Its entry and callees must
72 /// obey the startup lifetime rules, fit on the stack, and return without
73 /// unwinding or leaving code or data references in other tasks. The caller
74 /// must meet the executor affinity and single-core requirements of
75 /// `stackful::spawn`.
76 pub unsafe fn spawn<T: 'static>(
77 self,
78 manager: &'static task::Manager,
79 parent: task::TaskIdentifier,
80 name: &str,
81 stack: stackful::Stack,
82 startup: Startup,
83 map: impl FnOnce(i32) -> T + 'static,
84 ) -> Result<
85 impl Future<Output = task::Result<(task::JoinHandle<T>, task::TaskIdentifier)>>,
86 Error,
87 > {
88 let (entry, owner) = unsafe { self.prepare(startup)? };
89 Ok(unsafe {
90 stackful::spawn_with_driver(
91 manager,
92 parent,
93 name,
94 stack,
95 entry,
96 move |coroutine| drive_owned(coroutine, owner.erase()),
97 map,
98 )
99 })
100 }
101
102 /// Run the ELF entry point on the calling task, as a future that resolves to its exit
103 /// status, without spawning a task for it.
104 ///
105 /// Dropping the future before it completes cancels the program, as dropping the task
106 /// future of [`LoadedImage::spawn`] does: its coroutine stack is freed, and everything
107 /// it held is released. The program runs inside the task that polls the future, which is
108 /// how a process learns its identity.
109 ///
110 /// # Safety
111 /// Same as [`LoadedImage::spawn`]; the future must be polled on the executor that
112 /// created it.
113 pub unsafe fn start(
114 self,
115 stack: stackful::Stack,
116 startup: Startup,
117 ) -> Result<impl Future<Output = i32> + 'static, Error> {
118 let (entry, owner) = unsafe { self.prepare(startup)? };
119 let coroutine = unsafe { Coroutine::new(stack, entry) };
120 Ok(drive_owned(coroutine, owner.erase()))
121 }
122}