db_misc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.h"
  22. #include "pal_internal.h"
  23. #include "pal_error.h"
  24. #include "api.h"
  25. PAL_NUM DkSystemTimeQuery (void)
  26. {
  27. store_frame(SystemTimeQuery);
  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 *) (buffer + bytes) = rand;
  49. bytes += sizeof(rand);
  50. } else {
  51. for (int i = 0 ; i < size - bytes ; i++)
  52. *(unsigned 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_BUF buffer, PAL_NUM size)
  64. {
  65. store_frame(RandomBitsRead);
  66. if (!buffer || !size)
  67. leave_frame(0, PAL_ERROR_INVAL);
  68. int ret = _DkRandomBitsRead(buffer, size);
  69. if (ret < 0)
  70. leave_frame(0, -ret);
  71. leave_frame(ret, 0);
  72. }
  73. PAL_PTR DkSegmentRegister (PAL_FLG reg, PAL_PTR addr)
  74. {
  75. store_frame(SegmentRegister);
  76. int ret;
  77. if (addr) {
  78. ret = _DkSegmentRegisterSet(reg, addr);
  79. if (ret < 0)
  80. leave_frame(NULL, -ret);
  81. leave_frame(addr, 0);
  82. } else {
  83. ret = _DkSegmentRegisterGet(reg, (void **) &addr);
  84. if (ret < 0)
  85. leave_frame(NULL, -ret);
  86. leave_frame(addr, 0);
  87. }
  88. }
  89. PAL_BOL DkInstructionCacheFlush (PAL_PTR addr, PAL_NUM size)
  90. {
  91. store_frame(InstructionCacheFlush);
  92. if (!addr || !size)
  93. leave_frame(PAL_FALSE, PAL_ERROR_INVAL);
  94. int ret = _DkInstructionCacheFlush(addr, size);
  95. if (ret < 0)
  96. leave_frame(PAL_FALSE, -ret);
  97. leave_frame(PAL_TRUE, 0);
  98. }
  99. PAL_NUM DkMemoryAvailableQuota (void)
  100. {
  101. store_frame(MemoryAvailableQuota);
  102. long quota = _DkMemoryAvailableQuota();
  103. leave_frame((quota < 0) ? 0 : quota, 0);
  104. }