moana_std 0.1.2

moana 用户标准库
Documentation
//! 系统管理 syscall 用户态接口

use moa_uapi::{
    error::ERR_PERM,
    sysnr::{SYSNR_CLOCK_SET_OFFSET, SYSNR_DEBUG_PRINT, SYSNR_REBOOT},
    system::RebootAction,
};

/// system 系列 syscall 返回类型
pub type Result<T> = core::result::Result<T, SystemError>;

define_syscall_error! {
    /// system 系列 syscall 错误
    pub enum SystemError {
        /// 权限不足
        Perm = ERR_PERM,
    }
}

/// 调整墙上时间偏移(纳秒)
///
/// 内核通过 `wall_time = monotonic + offset` 计算墙上时间,
/// 此调用修改 offset。monotonic clock 不受影响。
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
pub fn clock_set_offset(delta: i64) -> Result<()> {
    let ret = unsafe { super::arch::syscall1(SYSNR_CLOCK_SET_OFFSET, delta as usize) };
    if ret < 0 { Err(SystemError::from_raw(ret)) } else { Ok(()) }
}

/// 重启 / 关机 / 停机
pub fn reboot(action: RebootAction) -> Result<()> {
    let ret = unsafe { super::arch::syscall1(SYSNR_REBOOT, action as usize) };
    if ret < 0 { Err(SystemError::from_raw(ret)) } else { Ok(()) }
}

/// 输出调试信息到内核控制台
pub fn debug_print(buf: &[u8]) -> Result<()> {
    let ret = unsafe { super::arch::syscall2(SYSNR_DEBUG_PRINT, buf.as_ptr() as usize, buf.len()) };
    if ret < 0 { Err(SystemError::from_raw(ret)) } else { Ok(()) }
}