sys_sbrk.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //! System call handler for the sbrk system call.
  2. use crate::arch::interrupt::TrapFrame;
  3. use crate::mm::ucontext::AddressSpace;
  4. use crate::syscall::table::{FormattedSyscallParam, Syscall};
  5. use crate::syscall::SYS_SBRK;
  6. use system_error::SystemError;
  7. use alloc::vec::Vec;
  8. /// Handler for the sbrk system call, which increments the program's data space (heap).
  9. pub struct SysSbrkHandle;
  10. impl Syscall for SysSbrkHandle {
  11. /// Returns the number of arguments this syscall takes.
  12. fn num_args(&self) -> usize {
  13. 1
  14. }
  15. /// Handles the sbrk system call.
  16. ///
  17. /// # Arguments
  18. /// * `args` - The syscall arguments, where args[0] is the increment value (isize).
  19. ///
  20. /// # Returns
  21. /// * On success, returns the previous program break (heap end) as usize.
  22. /// * On failure, returns a SystemError.
  23. fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
  24. let incr = Self::incr(args);
  25. let address_space = AddressSpace::current()?;
  26. assert!(address_space.read().user_mapper.utable.is_current());
  27. let mut address_space = address_space.write();
  28. let r = unsafe { address_space.sbrk(incr) }?;
  29. return Ok(r.data());
  30. }
  31. /// Formats the syscall arguments for display/debugging purposes.
  32. fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam> {
  33. vec![FormattedSyscallParam::new(
  34. "incr",
  35. format!("{}", Self::incr(args)),
  36. )]
  37. }
  38. }
  39. impl SysSbrkHandle {
  40. /// Extracts the increment argument from syscall parameters.
  41. fn incr(args: &[usize]) -> isize {
  42. args[0] as isize
  43. }
  44. }
  45. syscall_table_macros::declare_syscall!(SYS_SBRK, SysSbrkHandle);