shim_time.c 2.7 KB

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