use alloc::borrow::Cow;
use core::sync::atomic::{AtomicBool, Ordering};
use axpoll::{IoEvents, Pollable};
use axpoll_set::PollSet;
use bytemuck::{AnyBitPattern, NoUninit};
use crate::{
StarryError, StarryResult,
file::FileLike,
mm::{VmMutPtr, VmPtr},
task::UserTaskRef,
};
const IOC_READ: u32 = 2;
const IOC_WRITE: u32 = 1;
const fn ioc(dir: u32, ty: u8, nr: u8, size: u16) -> u32 {
(dir << 30) | ((size as u32) << 16) | ((ty as u32) << 8) | (nr as u32)
}
const SYNC_IOC_FILE_INFO: u32 = ioc(
IOC_READ | IOC_WRITE,
b'>',
4,
core::mem::size_of::<SyncFileInfo>() as u16,
);
const FENCE_NAME: &str = "starry-fence";
const FENCE_DRIVER_NAME: &str = "starry-card0";
#[repr(C)]
#[derive(Debug, Default, Clone, Copy, AnyBitPattern, NoUninit)]
pub struct SyncFileInfo {
pub name: [u8; 32],
pub status: i32,
pub flags: u32,
pub num_fences: u32,
pub pad: u32,
pub sync_fence_info: u64,
}
#[repr(C)]
#[derive(Debug, Default, Clone, Copy, AnyBitPattern, NoUninit)]
pub struct SyncFenceInfo {
pub obj_name: [u8; 32],
pub driver_name: [u8; 32],
pub status: i32,
pub flags: u32,
pub timestamp_ns: u64,
}
pub struct SyncFile {
signaled: AtomicBool,
poll_rx: PollSet,
}
impl SyncFile {
pub fn new() -> Self {
Self {
signaled: AtomicBool::new(false),
poll_rx: PollSet::new(),
}
}
pub fn mark_signaled(&self) {
if !self.signaled.swap(true, Ordering::Release) {
unsafe { self.poll_rx.wake(IoEvents::IN) };
}
}
pub fn is_signaled(&self) -> bool {
self.signaled.load(Ordering::Acquire)
}
fn status(&self) -> i32 {
if self.is_signaled() { 1 } else { 0 }
}
}
impl FileLike for SyncFile {
fn validate_write_access(&self) -> StarryResult {
Err(StarryError::InvalidInput)
}
fn path(&self) -> Cow<'_, str> {
"anon_inode:sync_file".into()
}
fn ioctl(&self, current: &UserTaskRef, cmd: u32, arg: usize) -> StarryResult<usize> {
match cmd {
SYNC_IOC_FILE_INFO => self.ioctl_file_info(current, arg),
_ => Err(StarryError::NotATty),
}
}
}
impl SyncFile {
fn ioctl_file_info(&self, current: &UserTaskRef, arg: usize) -> StarryResult<usize> {
let ptr = arg as *mut SyncFileInfo;
let mut info: SyncFileInfo = ptr.vm_read(current).map_err(|_| StarryError::BadAddress)?;
if info.flags != 0 || info.pad != 0 {
return Err(StarryError::InvalidInput);
}
let status = self.status();
let num_fences = 1u32;
if info.num_fences != 0 {
if info.num_fences < num_fences {
return Err(StarryError::InvalidInput);
}
if info.sync_fence_info == 0 {
return Err(StarryError::BadAddress);
}
let entry = SyncFenceInfo {
obj_name: name_bytes(FENCE_NAME),
driver_name: name_bytes(FENCE_DRIVER_NAME),
status,
flags: 0,
timestamp_ns: 0,
};
(info.sync_fence_info as *mut SyncFenceInfo)
.vm_write(current, entry)
.map_err(|_| StarryError::BadAddress)?;
}
info.name = name_bytes(FENCE_NAME);
info.status = status;
info.num_fences = num_fences;
ptr.vm_write(current, info)
.map_err(|_| StarryError::BadAddress)?;
Ok(0)
}
}
fn name_bytes(name: &str) -> [u8; 32] {
let mut out = [0u8; 32];
let bytes = name.as_bytes();
let len = bytes.len().min(out.len() - 1);
out[..len].copy_from_slice(&bytes[..len]);
out
}
impl Pollable for SyncFile {
fn poll(&self) -> IoEvents {
if self.is_signaled() {
IoEvents::IN
} else {
IoEvents::empty()
}
}
unsafe fn register_shared(
&self,
sink: &mut dyn axpoll::SharedRegistrationSink,
events: IoEvents,
) {
if events.contains(IoEvents::IN) {
unsafe { sink.register_shared(&self.poll_rx, IoEvents::IN) };
}
}
unsafe fn register_exclusive(
&self,
sink: &mut dyn axpoll::ExclusiveRegistrationSink,
events: IoEvents,
) {
if events.contains(IoEvents::IN) {
unsafe { sink.register_exclusive(&self.poll_rx, IoEvents::IN) };
}
}
}