db_misc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "pal_defs.h"
  19. #include "pal.h"
  20. #include "pal_internal.h"
  21. #include "pal_error.h"
  22. #include "api.h"
  23. PAL_NUM DkSystemTimeQuery (void)
  24. {
  25. ENTER_PAL_CALL(DkSystemTimeQuery);
  26. unsigned long time = _DkSystemTimeQuery();
  27. return time;
  28. }
  29. static PAL_LOCK lock = LOCK_INIT;
  30. static unsigned long seed;
  31. size_t _DkFastRandomBitsRead (void * buffer, size_t size)
  32. {
  33. unsigned long rand;
  34. size_t bytes = 0;
  35. _DkInternalLock(&lock);
  36. rand = seed;
  37. while (!seed) {
  38. _DkInternalUnlock(&lock);
  39. int ret = _DkRandomBitsRead(&rand, sizeof(rand));
  40. if (ret < 0)
  41. return ret;
  42. _DkInternalLock(&lock);
  43. seed = rand;
  44. }
  45. do {
  46. if (bytes + sizeof(rand) <= size) {
  47. *(unsigned long *) ((char *) buffer + bytes) = rand;
  48. bytes += sizeof(rand);
  49. } else {
  50. for (size_t i = 0 ; i < size - bytes ; i++)
  51. *(unsigned char *) ((char *) buffer + bytes + i) = ((unsigned char *) &rand)[i];
  52. bytes = size;
  53. }
  54. do {
  55. rand = hash64(rand);
  56. } while (!rand);
  57. } while (bytes < size);
  58. seed = rand;
  59. _DkInternalUnlock(&lock);
  60. return bytes;
  61. }
  62. PAL_NUM DkRandomBitsRead (PAL_PTR buffer, PAL_NUM size)
  63. {
  64. ENTER_PAL_CALL(DkRandomBitsRead);
  65. int ret = _DkRandomBitsRead((void *) buffer, size);
  66. LEAVE_PAL_CALL_RETURN(ret);
  67. }
  68. PAL_PTR DkSegmentRegister (PAL_FLG reg, PAL_PTR addr)
  69. {
  70. ENTER_PAL_CALL(DkSegmentRegister);
  71. void * seg_addr = (void *) addr;
  72. int ret;
  73. if (addr) {
  74. ret = _DkSegmentRegisterSet(reg, seg_addr);
  75. } else {
  76. ret = _DkSegmentRegisterGet(reg, &seg_addr);
  77. }
  78. if (ret < 0) {
  79. _DkRaiseFailure(-ret);
  80. seg_addr = NULL;
  81. }
  82. LEAVE_PAL_CALL_RETURN((PAL_PTR) seg_addr);
  83. }
  84. PAL_BOL DkInstructionCacheFlush (PAL_PTR addr, PAL_NUM size)
  85. {
  86. ENTER_PAL_CALL(DkInstructionCacheFlush);
  87. if (!addr || !size) {
  88. _DkRaiseFailure(PAL_ERROR_INVAL);
  89. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  90. }
  91. int ret = _DkInstructionCacheFlush((void *) addr, size);
  92. if (ret < 0) {
  93. _DkRaiseFailure(-ret);
  94. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  95. }
  96. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  97. }
  98. PAL_NUM DkMemoryAvailableQuota (void)
  99. {
  100. ENTER_PAL_CALL(DkMemoryAvailableQuota);
  101. long quota = _DkMemoryAvailableQuota();
  102. if (quota < 0)
  103. quota = 0;
  104. LEAVE_PAL_CALL_RETURN((PAL_NUM) quota);
  105. }
  106. PAL_BOL
  107. DkCpuIdRetrieve (PAL_IDX leaf, PAL_IDX subleaf, PAL_IDX values[4])
  108. {
  109. ENTER_PAL_CALL(DkCpuIdRetrieve);
  110. unsigned int vals[4];
  111. int ret = _DkCpuIdRetrieve(leaf, subleaf, vals);
  112. if (ret < 0) {
  113. _DkRaiseFailure(-ret);
  114. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  115. }
  116. values[0] = vals[0];
  117. values[1] = vals[1];
  118. values[2] = vals[2];
  119. values[3] = vals[3];
  120. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  121. }