db_misc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_freebsd_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_freebsd.h"
  25. #include "pal_error.h"
  26. #include "pal_security.h"
  27. #include "api.h"
  28. #include <sys/time.h>
  29. #include <fcntl.h>
  30. unsigned long _DkSystemTimeQuery (void)
  31. {
  32. #if USE_CLOCK_GETTIME == 1
  33. struct timespec time;
  34. int ret;
  35. ret = INLINE_SYSCALL(clock_gettime, 2, CLOCK_MONOTONIC, &time);
  36. /* Come on, gettimeofday mostly never fails */
  37. if (IS_ERR(ret))
  38. return 0;
  39. /* in microseconds */
  40. return 1000000ULL * time.tv_sec + time.tv_nsec / 1000;
  41. #else
  42. struct timeval time;
  43. int ret;
  44. ret = INLINE_SYSCALL(gettimeofday, 2, &time, NULL);
  45. /* Come on, gettimeofday mostly never fails */
  46. if (IS_ERR(ret))
  47. return 0;
  48. /* in microseconds */
  49. return 1000000ULL * time.tv_sec + time.tv_usec;
  50. #endif
  51. }
  52. #if USE_ARCH_RDRAND == 1
  53. int _DkRandomBitsRead (void * buffer, int size)
  54. {
  55. int total_bytes = 0;
  56. do {
  57. unsigned long rand;
  58. asm volatile (".Lretry: rdrand %%rax\r\n jnc .Lretry\r\n"
  59. : "=a"(rand) :: "memory");
  60. if (total_bytes + sizeof(rand) <= size) {
  61. *(unsigned long *) (buffer + total_bytes) = rand;
  62. total_bytes += sizeof(rand);
  63. } else {
  64. for (int i = 0 ; i < size - total_bytes ; i++)
  65. *(unsigned char *) (buffer + total_bytes + i) = ((unsigned char *) &rand)[i];
  66. total_bytes = size;
  67. }
  68. } while (total_bytes < size);
  69. return total_bytes;
  70. }
  71. #else
  72. int _DkRandomBitsRead (void * buffer, int size)
  73. {
  74. if (!pal_sec.rand_gen) {
  75. int rand = INLINE_SYSCALL(open, 3, "/dev/urandom", O_RDONLY, 0);
  76. if (IS_ERR(rand))
  77. return -PAL_ERROR_DENIED;
  78. pal_sec.rand_gen = rand;
  79. }
  80. int total_bytes = 0;
  81. do {
  82. int bytes = INLINE_SYSCALL(read, 3, pal_sec.rand_gen,
  83. buffer + total_bytes, size - total_bytes);
  84. if (IS_ERR(bytes))
  85. return -PAL_ERROR_DENIED;
  86. total_bytes += bytes;
  87. } while (total_bytes < size);
  88. return total_bytes;
  89. }
  90. #endif
  91. #if defined(__i386__)
  92. #include <ldt.h>
  93. #else
  94. #include <x86/sysarch.h>
  95. #endif
  96. int _DkSegmentRegisterSet (int reg, const void * addr)
  97. {
  98. int ret = 0;
  99. #if defined(__i386__)
  100. struct user_desc u_info;
  101. ret = INLINE_SYSCALL(sysarch, 2, I386_GET_FSBASE, &u_info);
  102. if (IS_ERR(ret))
  103. return NULL;
  104. u_info->entry_number = -1;
  105. u_info->base_addr = (unsigned int) addr;
  106. ret = INLINE_SYSCALL(sysarch, 2, I386_SET_FSBASE, &u_info);
  107. #else
  108. if (reg == PAL_SEGMENT_FS) {
  109. ret = INLINE_SYSCALL(sysarch, 2, AMD64_SET_FSBASE, &addr);
  110. } else if (reg == PAL_SEGMENT_GS) {
  111. ret = INLINE_SYSCALL(sysarch, 2, AMD64_SET_GSBASE, &addr);
  112. } else {
  113. return -PAL_ERROR_INVAL;
  114. }
  115. #endif
  116. if (IS_ERR(ret))
  117. return -PAL_ERROR_DENIED;
  118. return 0;
  119. }
  120. int _DkSegmentRegisterGet (int reg, void ** addr)
  121. {
  122. int ret;
  123. #if defined(__i386__)
  124. struct user_desc u_info;
  125. ret = INLINE_SYSCALL(sysarch, 2, I386_GET_FSBASE, &u_info);
  126. if (IS_ERR(ret))
  127. return -PAL_ERROR_DENIED;
  128. *addr = (void *) u_info->base_addr;
  129. #else
  130. unsigned long ret_addr;
  131. if (reg == PAL_SEGMENT_FS) {
  132. ret = INLINE_SYSCALL(sysarch, 2, AMD64_GET_FSBASE, &ret_addr);
  133. } else if (reg == PAL_SEGMENT_GS) {
  134. ret = INLINE_SYSCALL(sysarch, 2, AMD64_GET_GSBASE, &ret_addr);
  135. } else {
  136. return -PAL_ERROR_INVAL;
  137. }
  138. if (IS_ERR(ret))
  139. return -PAL_ERROR_DENIED;
  140. *addr = (void *) ret_addr;
  141. #endif
  142. return 0;
  143. }
  144. int _DkInstructionCacheFlush (const void * addr, int size)
  145. {
  146. return -PAL_ERROR_NOTIMPLEMENTED;
  147. }
  148. static PAL_LOCK lock = LOCK_INIT;
  149. static unsigned long randval = 0;
  150. static int init_randgen (void)
  151. {
  152. unsigned long val;
  153. if (_DkRandomBitsRead(&val, sizeof(val)) < sizeof(val))
  154. return -PAL_ERROR_DENIED;
  155. _DkInternalLock(&lock);
  156. randval = val;
  157. _DkInternalUnlock(&lock);
  158. return 0;
  159. }
  160. int getrand (void * buffer, int size)
  161. {
  162. unsigned long val;
  163. int bytes = 0;
  164. _DkInternalLock(&lock);
  165. while (!randval) {
  166. _DkInternalUnlock(&lock);
  167. if (init_randgen() < 0)
  168. return -PAL_ERROR_DENIED;
  169. _DkInternalLock(&lock);
  170. }
  171. val = randval;
  172. randval++;
  173. _DkInternalUnlock(&lock);
  174. while (bytes + sizeof(unsigned long) <= size) {
  175. *(unsigned long *) (buffer + bytes) = val;
  176. val = hash64(val);
  177. bytes += sizeof(unsigned long);
  178. }
  179. if (bytes < size) {
  180. switch (size - bytes) {
  181. case 4:
  182. *(unsigned int *) (buffer + bytes) = randval & 0xffffffff;
  183. bytes += 4;
  184. break;
  185. case 2:
  186. *(unsigned short *) (buffer + bytes) = randval & 0xffff;
  187. bytes += 2;
  188. break;
  189. case 1:
  190. *(unsigned char *) (buffer + bytes) = randval & 0xff;
  191. bytes++;
  192. break;
  193. default: break;
  194. }
  195. randval = hash64(randval);
  196. }
  197. _DkInternalLock(&lock);
  198. randval = val;
  199. _DkInternalUnlock(&lock);
  200. return bytes;
  201. }