db_misc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #if USE_VDSO_GETTIME == 1
  34. # if USE_CLOCK_GETTIME == 1
  35. long int (*__vdso_clock_gettime) (long int clk, struct timespec * tp);
  36. # else
  37. long int (*__vdso_gettimeofday) (struct timeval *, void *);
  38. # endif
  39. #endif
  40. unsigned long _DkSystemTimeQuery (void)
  41. {
  42. #if USE_CLOCK_GETTIME == 1
  43. struct timespec time;
  44. int ret;
  45. #if USE_VDSO_GETTIME == 1
  46. if (__vdso_clock_gettime) {
  47. ret = __vdso_clock_gettime(CLOCK_REALTIME, &time);
  48. } else {
  49. #endif
  50. ret = INLINE_SYSCALL(clock_gettime, 2, CLOCK_REALTIME, &time);
  51. #if USE_VDSO_GETTIME == 1
  52. }
  53. #endif
  54. /* Come on, gettimeofday mostly never fails */
  55. if (IS_ERR(ret))
  56. return 0;
  57. /* in microseconds */
  58. return time.tv_sec * 1000000 + time.tv_nsec / 1000;
  59. #else
  60. struct timeval time;
  61. int ret;
  62. #if USE_VDSO_GETTIME == 1
  63. if (__vdso_gettimeofday) {
  64. ret = __vdso_gettimeofday(&time, NULL);
  65. } else {
  66. #endif
  67. #if defined(__x86_64__) && USE_VSYSCALL_GETTIME == 1
  68. ret = __gettimeofday(&time, NULL);
  69. #else
  70. ret = INLINE_SYSCALL(gettimeofday, 2, &time, NULL);
  71. #endif
  72. #if USE_VDSO_GETTIME == 1
  73. }
  74. #endif
  75. /* Come on, gettimeofday mostly never fails */
  76. if (IS_ERR(ret))
  77. return 0;
  78. /* in microseconds */
  79. return time.tv_sec * 1000000 + time.tv_usec;
  80. #endif
  81. }
  82. int _DkRandomBitsRead (void * buffer, int size)
  83. {
  84. if (!pal_sec_info.rand_gen) {
  85. int fd = INLINE_SYSCALL(open, 3, "/dev/urandom", O_RDONLY, 0);
  86. if (IS_ERR(fd))
  87. return -PAL_ERROR_DENIED;
  88. pal_sec_info.rand_gen = fd;
  89. }
  90. int bytes = INLINE_SYSCALL(read, 3, pal_sec_info.rand_gen, buffer, size);
  91. return IS_ERR(bytes) ? -PAL_ERROR_DENIED : bytes;
  92. }
  93. int _DkInstructionCacheFlush (const void * addr, size_t size)
  94. {
  95. return -PAL_ERROR_NOTIMPLEMENTED;
  96. }