sgx_time.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_us()
  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. uint64
  38. os_time_thread_cputime_us(void)
  39. {
  40. #ifndef SGX_DISABLE_WASI
  41. struct timespec ts;
  42. if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0) {
  43. return 0;
  44. }
  45. return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
  46. #else
  47. return 0;
  48. #endif
  49. }
  50. #ifndef SGX_DISABLE_WASI
  51. int
  52. clock_getres(int clock_id, struct timespec *res)
  53. {
  54. int ret;
  55. if (ocall_clock_getres(&ret, clock_id, (void *)res, 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. clock_gettime(clockid_t clock_id, struct timespec *tp)
  66. {
  67. int ret;
  68. if (ocall_clock_gettime(&ret, clock_id, (void *)tp, sizeof(struct timespec))
  69. != SGX_SUCCESS) {
  70. TRACE_OCALL_FAIL();
  71. return -1;
  72. }
  73. if (ret == -1)
  74. errno = get_errno();
  75. return ret;
  76. }
  77. int
  78. utimensat(int dirfd, const char *pathname, const struct timespec times[2],
  79. int flags)
  80. {
  81. int ret;
  82. if (ocall_utimensat(&ret, dirfd, pathname, (void *)times,
  83. sizeof(struct timespec) * 2, flags)
  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. futimens(int fd, const struct timespec times[2])
  94. {
  95. int ret;
  96. if (ocall_futimens(&ret, fd, (void *)times, sizeof(struct timespec) * 2)
  97. != SGX_SUCCESS) {
  98. TRACE_OCALL_FAIL();
  99. return -1;
  100. }
  101. if (ret == -1)
  102. errno = get_errno();
  103. return ret;
  104. }
  105. int
  106. clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *request,
  107. struct timespec *remain)
  108. {
  109. int ret;
  110. if (ocall_clock_nanosleep(&ret, clock_id, flags, (void *)request,
  111. sizeof(struct timespec), (void *)remain,
  112. sizeof(struct timespec))
  113. != SGX_SUCCESS) {
  114. TRACE_OCALL_FAIL();
  115. return -1;
  116. }
  117. if (ret == -1)
  118. errno = get_errno();
  119. return ret;
  120. }
  121. #endif