db_misc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_misc.c
  15. *
  16. * This file contains APIs for miscellaneous use.
  17. */
  18. #include "api.h"
  19. #include "pal.h"
  20. #include "pal_defs.h"
  21. #include "pal_error.h"
  22. #include "pal_internal.h"
  23. PAL_NUM DkSystemTimeQuery(void) {
  24. ENTER_PAL_CALL(DkSystemTimeQuery);
  25. unsigned long time = _DkSystemTimeQuery();
  26. return time;
  27. }
  28. static PAL_LOCK lock = LOCK_INIT;
  29. static unsigned long seed;
  30. size_t _DkFastRandomBitsRead(void* buffer, size_t size) {
  31. unsigned long rand;
  32. size_t bytes = 0;
  33. _DkInternalLock(&lock);
  34. rand = seed;
  35. while (!seed) {
  36. _DkInternalUnlock(&lock);
  37. int ret = _DkRandomBitsRead(&rand, sizeof(rand));
  38. if (ret < 0)
  39. return ret;
  40. _DkInternalLock(&lock);
  41. seed = rand;
  42. }
  43. do {
  44. if (bytes + sizeof(rand) <= size) {
  45. *(unsigned long*)((char*)buffer + bytes) = rand;
  46. bytes += sizeof(rand);
  47. } else {
  48. for (size_t i = 0; i < size - bytes; i++)
  49. *(unsigned char*)((char*)buffer + bytes + i) = ((unsigned char*)&rand)[i];
  50. bytes = size;
  51. }
  52. do {
  53. rand = hash64(rand);
  54. } while (!rand);
  55. } while (bytes < size);
  56. seed = rand;
  57. _DkInternalUnlock(&lock);
  58. return bytes;
  59. }
  60. PAL_NUM DkRandomBitsRead(PAL_PTR buffer, PAL_NUM size) {
  61. ENTER_PAL_CALL(DkRandomBitsRead);
  62. int ret = _DkRandomBitsRead((void*)buffer, size);
  63. LEAVE_PAL_CALL_RETURN(ret);
  64. }
  65. PAL_PTR DkSegmentRegister(PAL_FLG reg, PAL_PTR addr) {
  66. ENTER_PAL_CALL(DkSegmentRegister);
  67. void* seg_addr = (void*)addr;
  68. int ret;
  69. if (addr) {
  70. ret = _DkSegmentRegisterSet(reg, seg_addr);
  71. } else {
  72. ret = _DkSegmentRegisterGet(reg, &seg_addr);
  73. }
  74. if (ret < 0) {
  75. _DkRaiseFailure(-ret);
  76. seg_addr = NULL;
  77. }
  78. LEAVE_PAL_CALL_RETURN((PAL_PTR)seg_addr);
  79. }
  80. PAL_BOL DkInstructionCacheFlush(PAL_PTR addr, PAL_NUM size) {
  81. ENTER_PAL_CALL(DkInstructionCacheFlush);
  82. if (!addr || !size) {
  83. _DkRaiseFailure(PAL_ERROR_INVAL);
  84. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  85. }
  86. int ret = _DkInstructionCacheFlush((void*)addr, size);
  87. if (ret < 0) {
  88. _DkRaiseFailure(-ret);
  89. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  90. }
  91. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  92. }
  93. PAL_NUM DkMemoryAvailableQuota(void) {
  94. ENTER_PAL_CALL(DkMemoryAvailableQuota);
  95. long quota = _DkMemoryAvailableQuota();
  96. if (quota < 0)
  97. quota = 0;
  98. LEAVE_PAL_CALL_RETURN((PAL_NUM)quota);
  99. }
  100. PAL_BOL
  101. DkCpuIdRetrieve(PAL_IDX leaf, PAL_IDX subleaf, PAL_IDX values[4]) {
  102. ENTER_PAL_CALL(DkCpuIdRetrieve);
  103. unsigned int vals[4];
  104. int ret = _DkCpuIdRetrieve(leaf, subleaf, vals);
  105. if (ret < 0) {
  106. _DkRaiseFailure(-ret);
  107. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  108. }
  109. values[0] = vals[0];
  110. values[1] = vals[1];
  111. values[2] = vals[2];
  112. values[3] = vals[3];
  113. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  114. }