db_misc.c 5.8 KB

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