db_misc.c 4.0 KB

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