sgx_time.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "platform_api_vmcore.h"
  6. #define TRACE_FUNC() os_printf("undefined %s\n", __FUNCTION__)
  7. #define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__)
  8. int
  9. ocall_clock_gettime(int *p_ret, unsigned clock_id, void *tp_buf,
  10. unsigned int tp_buf_size);
  11. int
  12. ocall_clock_getres(int *p_ret, int clock_id, void *res_buf,
  13. unsigned int res_buf_size);
  14. int
  15. ocall_utimensat(int *p_ret, int dirfd, const char *pathname,
  16. const void *times_buf, unsigned int times_buf_size, int flags);
  17. int
  18. ocall_futimens(int *p_ret, int fd, const void *times_buf,
  19. unsigned int times_buf_size);
  20. int
  21. ocall_clock_nanosleep(int *p_ret, unsigned clock_id, int flags,
  22. const void *req_buf, unsigned int req_buf_size,
  23. const void *rem_buf, unsigned int rem_buf_size);
  24. uint64
  25. os_time_get_boot_microsecond()
  26. {
  27. #ifndef SGX_DISABLE_WASI
  28. struct timespec ts;
  29. if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
  30. return 0;
  31. }
  32. return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
  33. #else
  34. return 0;
  35. #endif
  36. }
  37. #ifndef SGX_DISABLE_WASI
  38. int
  39. clock_getres(int clock_id, struct timespec *res)
  40. {
  41. int ret;
  42. if (ocall_clock_getres(&ret, clock_id, (void *)res, sizeof(struct timespec))
  43. != SGX_SUCCESS) {
  44. TRACE_OCALL_FAIL();
  45. return -1;
  46. }
  47. if (ret == -1)
  48. errno = get_errno();
  49. return ret;
  50. }
  51. int
  52. clock_gettime(clockid_t clock_id, struct timespec *tp)
  53. {
  54. int ret;
  55. if (ocall_clock_gettime(&ret, clock_id, (void *)tp, sizeof(struct timespec))
  56. != SGX_SUCCESS) {
  57. TRACE_OCALL_FAIL();
  58. return -1;
  59. }
  60. if (ret == -1)
  61. errno = get_errno();
  62. return ret;
  63. }
  64. int
  65. utimensat(int dirfd, const char *pathname, const struct timespec times[2],
  66. int flags)
  67. {
  68. int ret;
  69. if (ocall_utimensat(&ret, dirfd, pathname, (void *)times,
  70. sizeof(struct timespec) * 2, flags)
  71. != SGX_SUCCESS) {
  72. TRACE_OCALL_FAIL();
  73. return -1;
  74. }
  75. if (ret == -1)
  76. errno = get_errno();
  77. return ret;
  78. }
  79. int
  80. futimens(int fd, const struct timespec times[2])
  81. {
  82. int ret;
  83. if (ocall_futimens(&ret, fd, (void *)times, sizeof(struct timespec) * 2)
  84. != SGX_SUCCESS) {
  85. TRACE_OCALL_FAIL();
  86. return -1;
  87. }
  88. if (ret == -1)
  89. errno = get_errno();
  90. return ret;
  91. }
  92. int
  93. clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *request,
  94. struct timespec *remain)
  95. {
  96. int ret;
  97. if (ocall_clock_nanosleep(&ret, clock_id, flags, (void *)request,
  98. sizeof(struct timespec), (void *)remain,
  99. sizeof(struct timespec))
  100. != SGX_SUCCESS) {
  101. TRACE_OCALL_FAIL();
  102. return -1;
  103. }
  104. if (ret == -1)
  105. errno = get_errno();
  106. return ret;
  107. }
  108. #endif