1use super::endpoint::{self, CallSlot, CallState};
41use crate::{
42 async_io::{complete::push_completion, ring::find_ring},
43 process::process::Process,
44 sync::SpinLock,
45 syscall::{
46 error::SyscallError,
47 ipc_v2::{self as sys, WaitSpec},
48 },
49};
50use alloc::{sync::Arc, vec::Vec};
51use core::{
52 mem::size_of,
53 sync::atomic::{AtomicUsize, Ordering},
54};
55use strat9_abi::ipc_v2::{MsgBuf, MsgCall, IPC_NONBLOCK};
56
57pub const OP_EP_SEND: u8 = 13;
59pub const OP_EP_CALL: u8 = 14;
60pub const OP_EP_RECV: u8 = 15;
61pub const OP_EP_REPLY: u8 = 16;
62pub const OP_NOTIF_WAIT: u8 = 17;
63
64enum Kind {
66 Send,
68 Recv,
70 NotifWait,
72 CallSubmit { req: u64, resp: u64 },
76 Call { slot: Arc<CallSlot>, resp: MsgBuf },
78}
79
80struct Parked {
81 ring_id: u64,
82 user_data: u64,
83 handle: u64,
84 addr: u64,
85 kind: Kind,
86}
87
88static PARKED: SpinLock<Vec<Parked>> = SpinLock::new(Vec::new());
89static PARKED_COUNT: AtomicUsize = AtomicUsize::new(0);
91
92enum Attempt {
94 Done(i32),
96 Pending,
98}
99
100fn errno(error: SyscallError) -> i32 {
101 error as i64 as i32
102}
103
104fn finished(result: Result<u64, SyscallError>) -> Attempt {
105 match result {
106 Ok(value) => Attempt::Done(value as i32),
107 Err(SyscallError::Again) => Attempt::Pending,
108 Err(error) => Attempt::Done(errno(error)),
109 }
110}
111
112const NONBLOCK: u64 = IPC_NONBLOCK as u64;
113
114fn attempt(process: &Process, op: &mut Parked) -> Attempt {
115 if let Kind::CallSubmit { req, resp } = &op.kind {
116 let (req, resp) = (*req, *resp);
117 match sys::call_submit(process, op.handle, req, resp, WaitSpec::NONBLOCK) {
118 Ok((slot, resp)) => op.kind = Kind::Call { slot, resp },
119 Err(SyscallError::Again) => return Attempt::Pending,
120 Err(error) => return Attempt::Done(errno(error)),
121 }
122 }
123 match &op.kind {
124 Kind::Send => finished(sys::sys_ep_send(process, op.handle, op.addr, NONBLOCK, 0)),
125 Kind::Recv => finished(sys::sys_ep_recv(process, op.handle, op.addr, NONBLOCK, 0)),
126 Kind::NotifWait => finished(sys::sys_notif_wait(
127 process, op.handle, op.addr, NONBLOCK, 0,
128 )),
129 Kind::CallSubmit { .. } => unreachable!("converted to Kind::Call above"),
130 Kind::Call { slot, resp } => {
131 let state = *slot.state.lock();
132 match state {
133 CallState::Done | CallState::Cancelled => {
134 match sys::call_finish(process, slot, resp) {
135 Ok(_) => Attempt::Done(0),
136 Err(error) => Attempt::Done(errno(error)),
137 }
138 }
139 _ => Attempt::Pending,
140 }
141 }
142 }
143}
144
145pub enum Submitted {
147 Completed,
149 InFlight,
151}
152
153pub fn submit(
159 process: &Process,
160 ring_id: u64,
161 opcode: u8,
162 handle: u64,
163 addr: u64,
164 len: u32,
165 user_data: u64,
166) -> Result<Submitted, i32> {
167 let expected = match opcode {
168 OP_EP_SEND | OP_EP_RECV | OP_EP_REPLY => size_of::<MsgBuf>(),
169 OP_EP_CALL => size_of::<MsgCall>(),
170 OP_NOTIF_WAIT => size_of::<u64>(),
171 _ => return Err(errno(SyscallError::InvalidArgument)),
172 };
173 if len as usize != expected {
174 return Err(errno(SyscallError::InvalidArgument));
175 }
176
177 let mut op = Parked {
178 ring_id,
179 user_data,
180 handle,
181 addr,
182 kind: Kind::Send,
183 };
184 let first = match opcode {
185 OP_EP_SEND => attempt(process, &mut op),
186 OP_EP_RECV => {
187 op.kind = Kind::Recv;
188 attempt(process, &mut op)
189 }
190 OP_NOTIF_WAIT => {
191 op.kind = Kind::NotifWait;
192 attempt(process, &mut op)
193 }
194 OP_EP_REPLY => Attempt::Done(match sys::sys_ep_reply(process, handle, addr) {
196 Ok(_) => 0,
197 Err(error) => errno(error),
198 }),
199 OP_EP_CALL => {
200 op.kind = Kind::CallSubmit {
202 req: addr,
203 resp: addr + size_of::<MsgBuf>() as u64,
204 };
205 attempt(process, &mut op)
206 }
207 _ => unreachable!("opcode validated above"),
208 };
209
210 match first {
211 Attempt::Done(res) => {
212 push_completion(ring_id, user_data, res, 0);
213 Ok(Submitted::Completed)
214 }
215 Attempt::Pending => {
216 PARKED.lock().push(op);
217 PARKED_COUNT.fetch_add(1, Ordering::AcqRel);
218 Ok(Submitted::InFlight)
219 }
220 }
221}
222
223pub fn poll(process: &Process, ring_id: u64) -> u32 {
226 if PARKED_COUNT.load(Ordering::Acquire) == 0 {
227 return 0;
228 }
229 let mine: Vec<Parked> = {
231 let mut parked = PARKED.lock();
232 let (mine, rest): (Vec<Parked>, Vec<Parked>) =
233 parked.drain(..).partition(|op| op.ring_id == ring_id);
234 *parked = rest;
235 mine
236 };
237 PARKED_COUNT.fetch_sub(mine.len(), Ordering::AcqRel);
238
239 let mut completed = 0;
240 let mut still = Vec::new();
241 for mut op in mine {
242 match attempt(process, &mut op) {
243 Attempt::Done(res) => {
244 push_completion(op.ring_id, op.user_data, res, 0);
245 completed += 1;
246 }
247 Attempt::Pending => still.push(op),
248 }
249 }
250 if !still.is_empty() {
251 PARKED_COUNT.fetch_add(still.len(), Ordering::AcqRel);
252 PARKED.lock().extend(still);
253 }
254 completed
255}
256
257pub fn notify_event() {
261 if PARKED_COUNT.load(Ordering::Acquire) == 0 {
262 return;
263 }
264 let mut rings: Vec<u64> = Vec::new();
265 {
266 let parked = PARKED.lock();
267 for op in parked.iter() {
268 if !rings.contains(&op.ring_id) {
269 rings.push(op.ring_id);
270 }
271 }
272 }
273 for id in rings {
274 if let Some(ring) = find_ring(id) {
275 ring.ipc_kick.store(1, Ordering::Release);
276 ring.wq.wake_all();
277 }
278 }
279}
280
281pub fn cancel(ring_id: u64, target: u64) -> Result<(), SyscallError> {
291 let op = {
292 let mut parked = PARKED.lock();
293 match parked
294 .iter()
295 .position(|op| op.ring_id == ring_id && op.user_data == target)
296 {
297 Some(index) => parked.remove(index),
298 None => return Err(SyscallError::NotFound),
299 }
300 };
301 PARKED_COUNT.fetch_sub(1, Ordering::AcqRel);
302 if let Kind::Call { slot, .. } = op.kind {
304 endpoint::abandon_call(&slot);
305 }
306 push_completion(ring_id, target, errno(SyscallError::Canceled), 0);
307 Ok(())
308}
309
310pub fn drop_ring(ring_id: u64) {
313 let dropped: Vec<Parked> = {
314 let mut parked = PARKED.lock();
315 let (dropped, rest): (Vec<Parked>, Vec<Parked>) =
316 parked.drain(..).partition(|op| op.ring_id == ring_id);
317 *parked = rest;
318 dropped
319 };
320 PARKED_COUNT.fetch_sub(dropped.len(), Ordering::AcqRel);
321 for op in dropped {
322 if let Kind::Call { slot, .. } = op.kind {
323 endpoint::abandon_call(&slot);
324 }
325 }
326}