shim_time.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_time.c
  15. *
  16. * Implementation of system call "gettimeofday", "time" and "clock_gettime".
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_table.h>
  20. #include <shim_handle.h>
  21. #include <shim_fs.h>
  22. #include <pal.h>
  23. #include <pal_error.h>
  24. #include <errno.h>
  25. int shim_do_gettimeofday (struct __kernel_timeval * tv,
  26. struct __kernel_timezone * tz)
  27. {
  28. if (!tv)
  29. return -EINVAL;
  30. if (test_user_memory(tv, sizeof(*tv), true))
  31. return -EFAULT;
  32. if (tz && test_user_memory(tz, sizeof(*tz), true))
  33. return -EFAULT;
  34. long time = DkSystemTimeQuery();
  35. if (time == -1)
  36. return -PAL_ERRNO;
  37. tv->tv_sec = time / 1000000;
  38. tv->tv_usec = time % 1000000;
  39. return 0;
  40. }
  41. time_t shim_do_time (time_t * tloc)
  42. {
  43. long time = DkSystemTimeQuery();
  44. if (time == -1)
  45. return -PAL_ERRNO;
  46. if (tloc && test_user_memory(tloc, sizeof(*tloc), true))
  47. return -EFAULT;
  48. time_t t = time / 1000000;
  49. if (tloc)
  50. *tloc = t;
  51. return t;
  52. }
  53. int shim_do_clock_gettime (clockid_t which_clock,
  54. struct timespec * tp)
  55. {
  56. /* all clock are the same */
  57. __UNUSED(which_clock);
  58. if (!tp)
  59. return -EINVAL;
  60. if (test_user_memory(tp, sizeof(*tp), true))
  61. return -EFAULT;
  62. long time = DkSystemTimeQuery();
  63. if (time == -1)
  64. return -PAL_ERRNO;
  65. tp->tv_sec = time / 1000000;
  66. tp->tv_nsec = (time % 1000000) * 1000;
  67. return 0;
  68. }
  69. int shim_do_clock_getres (clockid_t which_clock,
  70. struct timespec * tp)
  71. {
  72. /* all clock are the same */
  73. __UNUSED(which_clock);
  74. if (!tp)
  75. return -EINVAL;
  76. if (test_user_memory(tp, sizeof(*tp), true))
  77. return -EFAULT;
  78. tp->tv_sec = 0;
  79. tp->tv_nsec = 1000;
  80. return 0;
  81. }