db_misc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 randval = 0;
  33. static int init_randgen (void)
  34. {
  35. unsigned long val;
  36. if (_DkRandomBitsRead(&val, sizeof(val)) < sizeof(val))
  37. return -PAL_ERROR_DENIED;
  38. _DkInternalLock(&lock);
  39. randval = val;
  40. _DkInternalUnlock(&lock);
  41. return 0;
  42. }
  43. int getrand (void * buffer, int size)
  44. {
  45. unsigned long val;
  46. int bytes = 0;
  47. _DkInternalLock(&lock);
  48. while (!randval) {
  49. _DkInternalUnlock(&lock);
  50. if (init_randgen() < 0)
  51. return -PAL_ERROR_DENIED;
  52. _DkInternalLock(&lock);
  53. }
  54. val = randval;
  55. randval++;
  56. _DkInternalUnlock(&lock);
  57. while (bytes + sizeof(unsigned long) <= size) {
  58. *(unsigned long *) (buffer + bytes) = val;
  59. val = hash64(val);
  60. bytes += sizeof(unsigned long);
  61. }
  62. if (bytes < size) {
  63. switch (size - bytes) {
  64. case 4:
  65. *(unsigned int *) (buffer + bytes) = randval & 0xffffffff;
  66. bytes += 4;
  67. break;
  68. case 2:
  69. *(unsigned short *) (buffer + bytes) = randval & 0xffff;
  70. bytes += 2;
  71. break;
  72. case 1:
  73. *(unsigned char *) (buffer + bytes) = randval & 0xff;
  74. bytes++;
  75. break;
  76. default: break;
  77. }
  78. randval = hash64(randval);
  79. }
  80. _DkInternalLock(&lock);
  81. randval = val;
  82. _DkInternalUnlock(&lock);
  83. return bytes;
  84. }
  85. PAL_NUM DkRandomBitsRead (PAL_BUF buffer, PAL_NUM size)
  86. {
  87. store_frame(RandomBitsRead);
  88. if (!buffer || !size) {
  89. notify_failure(PAL_ERROR_INVAL);
  90. return 0;
  91. }
  92. int ret = _DkRandomBitsRead(buffer, size);
  93. if (ret < 0) {
  94. notify_failure(-ret);
  95. return 0;
  96. }
  97. return ret;
  98. }
  99. PAL_BOL DkInstructionCacheFlush (PAL_PTR addr, PAL_NUM size)
  100. {
  101. store_frame(InstructionCacheFlush);
  102. if (!addr || !size) {
  103. notify_failure(PAL_ERROR_INVAL);
  104. return PAL_FALSE;
  105. }
  106. int ret = _DkInstructionCacheFlush(addr, size);
  107. if (ret < 0) {
  108. notify_failure(-ret);
  109. return PAL_FALSE;
  110. }
  111. return PAL_TRUE;
  112. }