shim_time.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <errno.h>
  19. #include <pal.h>
  20. #include <pal_error.h>
  21. #include <shim_fs.h>
  22. #include <shim_handle.h>
  23. #include <shim_internal.h>
  24. #include <shim_table.h>
  25. int shim_do_gettimeofday(struct __kernel_timeval* tv, struct __kernel_timezone* tz) {
  26. if (!tv)
  27. return -EINVAL;
  28. if (test_user_memory(tv, sizeof(*tv), true))
  29. return -EFAULT;
  30. if (tz && test_user_memory(tz, sizeof(*tz), true))
  31. return -EFAULT;
  32. long time = DkSystemTimeQuery();
  33. if (time == -1)
  34. return -PAL_ERRNO;
  35. tv->tv_sec = time / 1000000;
  36. tv->tv_usec = time % 1000000;
  37. return 0;
  38. }
  39. time_t shim_do_time(time_t* tloc) {
  40. long time = DkSystemTimeQuery();
  41. if (time == -1)
  42. return -PAL_ERRNO;
  43. if (tloc && test_user_memory(tloc, sizeof(*tloc), true))
  44. return -EFAULT;
  45. time_t t = time / 1000000;
  46. if (tloc)
  47. *tloc = t;
  48. return t;
  49. }
  50. int shim_do_clock_gettime(clockid_t which_clock, struct timespec* tp) {
  51. /* all clock are the same */
  52. __UNUSED(which_clock);
  53. if (!tp)
  54. return -EINVAL;
  55. if (test_user_memory(tp, sizeof(*tp), true))
  56. return -EFAULT;
  57. long time = DkSystemTimeQuery();
  58. if (time == -1)
  59. return -PAL_ERRNO;
  60. tp->tv_sec = time / 1000000;
  61. tp->tv_nsec = (time % 1000000) * 1000;
  62. return 0;
  63. }
  64. int shim_do_clock_getres(clockid_t which_clock, struct timespec* tp) {
  65. /* all clock are the same */
  66. __UNUSED(which_clock);
  67. if (!tp)
  68. return -EINVAL;
  69. if (test_user_memory(tp, sizeof(*tp), true))
  70. return -EFAULT;
  71. tp->tv_sec = 0;
  72. tp->tv_nsec = 1000;
  73. return 0;
  74. }