db_misc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * db_misc.c
  17. *
  18. * This file contains APIs for miscellaneous use.
  19. */
  20. #include "pal_defs.h"
  21. #include "pal_linux_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_linux.h"
  25. #include "pal_error.h"
  26. #include "pal_security.h"
  27. #include "api.h"
  28. #include <linux/time.h>
  29. #include <asm/fcntl.h>
  30. int __gettimeofday(struct timeval *tv, struct timezone *tz);
  31. unsigned long _DkSystemTimeQueryEarly (void)
  32. {
  33. #if USE_CLOCK_GETTIME == 1
  34. struct timespec time;
  35. int ret;
  36. ret = INLINE_SYSCALL(clock_gettime, 2, CLOCK_MONOTONIC, &time);
  37. /* Come on, gettimeofday mostly never fails */
  38. if (IS_ERR(ret))
  39. return 0;
  40. /* in microseconds */
  41. return 1000000ULL * time.tv_sec + time.tv_nsec / 1000;
  42. #else
  43. struct timeval time;
  44. int ret;
  45. ret = INLINE_SYSCALL(gettimeofday, 2, &time, NULL);
  46. /* Come on, gettimeofday mostly never fails */
  47. if (IS_ERR(ret))
  48. return 0;
  49. /* in microseconds */
  50. return 1000000ULL * time.tv_sec + time.tv_usec;
  51. #endif
  52. }
  53. unsigned long _DkSystemTimeQuery (void)
  54. {
  55. #if USE_CLOCK_GETTIME == 1
  56. struct timespec time;
  57. int ret;
  58. #if USE_VDSO_GETTIME == 1
  59. if (linux_state.vdso_clock_gettime) {
  60. ret = linux_state.vdso_clock_gettime(CLOCK_MONOTONIC, &time);
  61. } else {
  62. #endif
  63. ret = INLINE_SYSCALL(clock_gettime, 2, CLOCK_MONOTONIC, &time);
  64. #if USE_VDSO_GETTIME == 1
  65. }
  66. #endif
  67. /* Come on, gettimeofday mostly never fails */
  68. if (IS_ERR(ret))
  69. return 0;
  70. /* in microseconds */
  71. return 1000000ULL * time.tv_sec + time.tv_nsec / 1000;
  72. #else
  73. struct timeval time;
  74. int ret;
  75. #if USE_VDSO_GETTIME == 1
  76. if (linux_state.vdso_gettimeofday) {
  77. ret = linux_state.vdso_gettimeofday(&time, NULL);
  78. } else {
  79. #endif
  80. #if USE_VSYSCALL_GETTIME == 1
  81. ret = __gettimeofday(&time, NULL);
  82. #else
  83. ret = INLINE_SYSCALL(gettimeofday, 2, &time, NULL);
  84. #endif
  85. #if USE_VDSO_GETTIME == 1
  86. }
  87. #endif
  88. /* Come on, gettimeofday mostly never fails */
  89. if (IS_ERR(ret))
  90. return 0;
  91. /* in microseconds */
  92. return 1000000ULL * time.tv_sec + time.tv_usec;
  93. #endif
  94. }
  95. #if USE_ARCH_RDRAND == 1
  96. int _DkRandomBitsRead (void * buffer, int size)
  97. {
  98. int total_bytes = 0;
  99. do {
  100. unsigned long rand;
  101. asm volatile (".Lretry: rdrand %%rax\r\n jnc .Lretry\r\n"
  102. : "=a"(rand) :: "memory");
  103. if (total_bytes + sizeof(rand) <= size) {
  104. *(unsigned long *) (buffer + total_bytes) = rand;
  105. total_bytes += sizeof(rand);
  106. } else {
  107. for (int i = 0 ; i < size - total_bytes ; i++)
  108. *(unsigned char *) (buffer + total_bytes + i) = ((unsigned char *) &rand)[i];
  109. total_bytes = size;
  110. }
  111. } while (total_bytes < size);
  112. return total_bytes;
  113. }
  114. #else
  115. int _DkRandomBitsRead (void * buffer, int size)
  116. {
  117. if (!pal_sec.random_device) {
  118. int fd = INLINE_SYSCALL(open, 3, RANDGEN_DEVICE, O_RDONLY, 0);
  119. if (IS_ERR(fd))
  120. return -PAL_ERROR_DENIED;
  121. pal_sec.random_device = fd;
  122. }
  123. int total_bytes = 0;
  124. do {
  125. int bytes = INLINE_SYSCALL(read, 3, pal_sec.random_device,
  126. buffer + total_bytes, size - total_bytes);
  127. if (IS_ERR(bytes))
  128. return -PAL_ERROR_DENIED;
  129. total_bytes += bytes;
  130. } while (total_bytes < size);
  131. return total_bytes;
  132. }
  133. #endif
  134. #if defined(__i386__)
  135. #include <asm/ldt.h>
  136. #else
  137. #include <asm/prctl.h>
  138. #endif
  139. int _DkSegmentRegisterSet (int reg, const void * addr)
  140. {
  141. int ret = 0;
  142. #if defined(__i386__)
  143. struct user_desc u_info;
  144. ret = INLINE_SYSCALL(get_thread_area, 1, &u_info);
  145. if (IS_ERR(ret))
  146. return NULL;
  147. u_info->entry_number = -1;
  148. u_info->base_addr = (unsigned int) addr;
  149. ret = INLINE_SYSCALL(set_thread_area, 1, &u_info);
  150. #else
  151. if (reg == PAL_SEGMENT_FS) {
  152. ret = INLINE_SYSCALL(arch_prctl, 2, ARCH_SET_FS, addr);
  153. } else if (reg == PAL_SEGMENT_GS) {
  154. ret = INLINE_SYSCALL(arch_prctl, 2, ARCH_SET_GS, addr);
  155. } else {
  156. return -PAL_ERROR_INVAL;
  157. }
  158. #endif
  159. if (IS_ERR(ret))
  160. return -PAL_ERROR_DENIED;
  161. return 0;
  162. }
  163. int _DkSegmentRegisterGet (int reg, void ** addr)
  164. {
  165. int ret;
  166. #if defined(__i386__)
  167. struct user_desc u_info;
  168. ret = INLINE_SYSCALL(get_thread_area, 1, &u_info);
  169. if (IS_ERR(ret))
  170. return -PAL_ERROR_DENIED;
  171. *addr = (void *) u_info->base_addr;
  172. #else
  173. unsigned long ret_addr;
  174. if (reg == PAL_SEGMENT_FS) {
  175. ret = INLINE_SYSCALL(arch_prctl, 2, ARCH_GET_FS, &ret_addr);
  176. } else if (reg == PAL_SEGMENT_GS) {
  177. ret = INLINE_SYSCALL(arch_prctl, 2, ARCH_GET_GS, &ret_addr);
  178. } else {
  179. return -PAL_ERROR_INVAL;
  180. }
  181. if (IS_ERR(ret))
  182. return -PAL_ERROR_DENIED;
  183. *addr = (void *) ret_addr;
  184. #endif
  185. return 0;
  186. }
  187. int _DkInstructionCacheFlush (const void * addr, int size)
  188. {
  189. return -PAL_ERROR_NOTIMPLEMENTED;
  190. }
  191. int _DkCpuIdRetrieve (unsigned int leaf, unsigned int subleaf,
  192. unsigned int values[4])
  193. {
  194. return -PAL_ERROR_NOTIMPLEMENTED;
  195. }