db_misc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_debug.h"
  27. #include "pal_security.h"
  28. #include "api.h"
  29. #include <linux/time.h>
  30. #include <asm/fcntl.h>
  31. unsigned long _DkSystemTimeQuery (void)
  32. {
  33. unsigned long microsec;
  34. int ret = ocall_gettime(&microsec);
  35. assert(!ret);
  36. return microsec;
  37. }
  38. size_t _DkRandomBitsRead (void * buffer, size_t size)
  39. {
  40. uint32_t rand;
  41. for (size_t i = 0; i < size; i += sizeof(rand)) {
  42. rand = rdrand();
  43. memcpy(buffer + i, &rand, MIN(sizeof(rand), size - i));
  44. }
  45. return 0;
  46. }
  47. int _DkInstructionCacheFlush (const void * addr, int size)
  48. {
  49. __UNUSED(addr);
  50. __UNUSED(size);
  51. return -PAL_ERROR_NOTIMPLEMENTED;
  52. }
  53. int _DkSegmentRegisterSet (int reg, const void * addr)
  54. {
  55. /* GS is internally used, denied any access to it */
  56. if (reg != PAL_SEGMENT_FS)
  57. return -PAL_ERROR_DENIED;
  58. SET_ENCLAVE_TLS(fsbase, (void *) addr);
  59. wrfsbase((uint64_t) addr);
  60. return 0;
  61. }
  62. int _DkSegmentRegisterGet (int reg, void ** addr)
  63. {
  64. /* GS is internally used, denied any access to it */
  65. if (reg != PAL_SEGMENT_FS)
  66. return -PAL_ERROR_DENIED;
  67. *addr = (void *) GET_ENCLAVE_TLS(fsbase);
  68. return 0;
  69. }
  70. #define CPUID_CACHE_SIZE 64
  71. #define CPUID_CACHE_INVALID ((unsigned int)-1)
  72. static PAL_LOCK cpuid_cache_lock = LOCK_INIT;
  73. static struct pal_cpuid {
  74. unsigned int recently;
  75. unsigned int leaf, subleaf;
  76. unsigned int values[4];
  77. } pal_cpuid_cache[CPUID_CACHE_SIZE];
  78. static int pal_cpuid_cache_top = 0;
  79. static unsigned int pal_cpuid_clock = 0;
  80. int get_cpuid_from_cache (unsigned int leaf, unsigned int subleaf,
  81. unsigned int values[4])
  82. {
  83. _DkInternalLock(&cpuid_cache_lock);
  84. for (int i = 0 ; i < pal_cpuid_cache_top ; i++)
  85. if (pal_cpuid_cache[i].leaf == leaf &&
  86. pal_cpuid_cache[i].subleaf == subleaf) {
  87. values[0] = pal_cpuid_cache[i].values[0];
  88. values[1] = pal_cpuid_cache[i].values[1];
  89. values[2] = pal_cpuid_cache[i].values[2];
  90. values[3] = pal_cpuid_cache[i].values[3];
  91. pal_cpuid_cache[i].recently = ++pal_cpuid_clock;
  92. _DkInternalUnlock(&cpuid_cache_lock);
  93. return 0;
  94. }
  95. _DkInternalUnlock(&cpuid_cache_lock);
  96. return -PAL_ERROR_DENIED;
  97. }
  98. void add_cpuid_to_cache (unsigned int leaf, unsigned int subleaf,
  99. unsigned int values[4])
  100. {
  101. struct pal_cpuid * chosen;
  102. _DkInternalLock(&cpuid_cache_lock);
  103. if (pal_cpuid_cache_top < CPUID_CACHE_SIZE) {
  104. for (int i = 0 ; i < pal_cpuid_cache_top ; i++)
  105. if (pal_cpuid_cache[i].leaf == leaf &&
  106. pal_cpuid_cache[i].subleaf == subleaf) {
  107. _DkInternalUnlock(&cpuid_cache_lock);
  108. return;
  109. }
  110. chosen = &pal_cpuid_cache[pal_cpuid_cache_top++];
  111. } else {
  112. unsigned int oldest_clock = pal_cpuid_cache[0].recently;
  113. chosen = &pal_cpuid_cache[0];
  114. if (pal_cpuid_cache[0].leaf == leaf &&
  115. pal_cpuid_cache[0].subleaf == subleaf) {
  116. _DkInternalUnlock(&cpuid_cache_lock);
  117. return;
  118. }
  119. for (int i = 1 ; i < pal_cpuid_cache_top ; i++) {
  120. if (pal_cpuid_cache[i].leaf == leaf &&
  121. pal_cpuid_cache[i].subleaf == subleaf) {
  122. _DkInternalUnlock(&cpuid_cache_lock);
  123. return;
  124. }
  125. if (pal_cpuid_cache[i].recently > oldest_clock) {
  126. chosen = &pal_cpuid_cache[i];
  127. oldest_clock = pal_cpuid_cache[i].recently;
  128. }
  129. }
  130. }
  131. chosen->leaf = leaf;
  132. chosen->subleaf = subleaf;
  133. chosen->values[0] = values[0];
  134. chosen->values[1] = values[1];
  135. chosen->values[2] = values[2];
  136. chosen->values[3] = values[3];
  137. chosen->recently = ++pal_cpuid_clock;
  138. _DkInternalUnlock(&cpuid_cache_lock);
  139. }
  140. int _DkCpuIdRetrieve (unsigned int leaf, unsigned int subleaf,
  141. unsigned int values[4])
  142. {
  143. if (leaf != 0x4 && leaf != 0x7 && leaf != 0xb)
  144. subleaf = 0;
  145. if (!get_cpuid_from_cache(leaf, subleaf, values))
  146. return 0;
  147. if (IS_ERR(ocall_cpuid(leaf, subleaf, values)))
  148. return -PAL_ERROR_DENIED;
  149. add_cpuid_to_cache(leaf, subleaf, values);
  150. return 0;
  151. }