syscall.rs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. use core::{
  2. ffi::{c_int, c_longlong},
  3. ptr::null_mut,
  4. };
  5. use crate::{
  6. kdebug,
  7. syscall::{Syscall, SystemError},
  8. time::{sleep::nanosleep, TimeSpec},
  9. };
  10. use super::timekeeping::do_gettimeofday;
  11. pub type PosixTimeT = c_longlong;
  12. pub type PosixSusecondsT = c_int;
  13. #[repr(C)]
  14. #[derive(Default, Debug)]
  15. pub struct PosixTimeval {
  16. pub tv_sec: PosixTimeT,
  17. pub tv_usec: PosixSusecondsT,
  18. }
  19. #[repr(C)]
  20. #[derive(Default, Debug)]
  21. /// 当前时区信息
  22. pub struct PosixTimeZone {
  23. /// 格林尼治相对于当前时区相差的分钟数
  24. pub tz_minuteswest: c_int,
  25. /// DST矫正时差
  26. pub tz_dsttime: c_int,
  27. }
  28. /// 系统时区 暂时写定为东八区
  29. pub const SYS_TIMEZONE: PosixTimeZone = PosixTimeZone {
  30. tz_minuteswest: -480,
  31. tz_dsttime: 0,
  32. };
  33. impl Syscall {
  34. /// @brief 休眠指定时间(单位:纳秒)(提供给C的接口)
  35. ///
  36. /// @param sleep_time 指定休眠的时间
  37. ///
  38. /// @param rm_time 剩余休眠时间(传出参数)
  39. ///
  40. /// @return Ok(i32) 0
  41. ///
  42. /// @return Err(SystemError) 错误码
  43. pub fn nanosleep(
  44. sleep_time: *const TimeSpec,
  45. rm_time: *mut TimeSpec,
  46. ) -> Result<usize, SystemError> {
  47. if sleep_time == null_mut() {
  48. return Err(SystemError::EFAULT);
  49. }
  50. let slt_spec = TimeSpec {
  51. tv_sec: unsafe { *sleep_time }.tv_sec,
  52. tv_nsec: unsafe { *sleep_time }.tv_nsec,
  53. };
  54. let r: Result<usize, SystemError> = nanosleep(slt_spec).map(|slt_spec| {
  55. if rm_time != null_mut() {
  56. unsafe { *rm_time }.tv_sec = slt_spec.tv_sec;
  57. unsafe { *rm_time }.tv_nsec = slt_spec.tv_nsec;
  58. }
  59. 0
  60. });
  61. return r;
  62. }
  63. /// 获取cpu时间
  64. ///
  65. /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc
  66. pub fn clock() -> Result<usize, SystemError> {
  67. return Ok(super::timer::clock() as usize);
  68. }
  69. pub fn gettimeofday(
  70. tv: *mut PosixTimeval,
  71. _timezone: &PosixTimeZone,
  72. ) -> Result<usize, SystemError> {
  73. // TODO; 处理时区信息
  74. kdebug!("enter sys_do_gettimeofday");
  75. if tv == null_mut() {
  76. return Err(SystemError::EFAULT);
  77. }
  78. let posix_time = do_gettimeofday();
  79. unsafe {
  80. (*tv).tv_sec = posix_time.tv_sec;
  81. (*tv).tv_usec = posix_time.tv_usec;
  82. }
  83. kdebug!("exit sys_do_gettimeofday");
  84. return Ok(0);
  85. }
  86. }