Skip to main content

kernel/syscall/syscalls/
debug_write.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! Debug Write Syscall Handler, writes a kernel log message at NOTICE severity
3//!
4//! Authors: MarioS271
5
6use crate::knotice;
7use crate::lib::types::user_slice::UserSlice;
8use crate::syscall::result::{SyscallError, SyscallResult};
9
10const BUFFER_SIZE: usize = 512;
11
12/// Debug Write Syscall Handler
13pub fn handler(user_slice: UserSlice) -> SyscallResult<()> {
14    if user_slice.is_empty() {
15        return Ok(());
16    }
17
18    let len = user_slice.len();
19    if len > BUFFER_SIZE as u64 {
20        return Err(SyscallError::InvalidArgument);
21    }
22
23    let mut buffer = [0u8; BUFFER_SIZE];
24    user_slice.copy_to_slice(&mut buffer[..len as usize]);
25
26    for byte in &mut buffer[..len as usize] {
27        if !matches!(*byte, 0x20..=0x7e | b'\n') {
28            *byte = b'?';
29        }
30    }
31
32    let text = core::str::from_utf8(&buffer[..len as usize]).unwrap_or("(error while parsing raw utf8 to string)");
33    knotice!("{}", text);
34
35    Ok(())
36}