db_memory.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_memory.c
  17. *
  18. * This files contains APIs that allocate, free or protect virtual memory.
  19. */
  20. #include "pal_defs.h"
  21. #include "pal.h"
  22. #include "pal_internal.h"
  23. #include "pal_error.h"
  24. #include "pal_debug.h"
  25. #include "api.h"
  26. bool check_memory_overlap (void * addr, size_t size)
  27. {
  28. void * pal_min = pal_config.lib_text_start;
  29. void * pal_max = pal_config.lib_data_end;
  30. void * overlap_start = pal_min < addr ? addr : pal_min;
  31. void * overlap_end = addr + size < pal_max ? addr + size : pal_max;
  32. if (overlap_start < overlap_end) {
  33. printf("WARNING: Attempt to change PAL-internal memory!!!!"
  34. " You seriously don't want to do this.\n");
  35. return true;
  36. }
  37. return false;
  38. }
  39. PAL_BUF
  40. DkVirtualMemoryAlloc (PAL_BUF addr, PAL_NUM size, PAL_FLG alloc_type,
  41. PAL_FLG prot)
  42. {
  43. store_frame(VirtualMemoryAlloc);
  44. if ((addr && !ALLOC_ALIGNED(addr)) || !size || !ALLOC_ALIGNED(size)) {
  45. notify_failure(PAL_ERROR_INVAL);
  46. return NULL;
  47. }
  48. if (check_memory_overlap(addr, size)) {
  49. notify_failure(PAL_ERROR_DENIED);
  50. return NULL;
  51. }
  52. int ret = _DkVirtualMemoryAlloc(&addr, size, alloc_type, prot);
  53. if (ret < 0) {
  54. notify_failure(-ret);
  55. return NULL;
  56. }
  57. return addr;
  58. }
  59. void
  60. DkVirtualMemoryFree (PAL_BUF addr, PAL_NUM size)
  61. {
  62. store_frame(VirtualMemoryFree);
  63. if (!addr || !size) {
  64. notify_failure(PAL_ERROR_INVAL);
  65. return;
  66. }
  67. if (!ALLOC_ALIGNED(addr) || !ALLOC_ALIGNED(size)) {
  68. notify_failure(PAL_ERROR_INVAL);
  69. return;
  70. }
  71. if (check_memory_overlap(addr, size)) {
  72. notify_failure(PAL_ERROR_DENIED);
  73. return;
  74. }
  75. int ret = _DkVirtualMemoryFree(addr, size);
  76. if (ret < 0)
  77. notify_failure(-ret);
  78. }
  79. PAL_BOL
  80. DkVirtualMemoryProtect (PAL_BUF addr, PAL_NUM size, PAL_FLG prot)
  81. {
  82. store_frame(VirtualMemoryProtect);
  83. if (!addr || !size) {
  84. notify_failure(PAL_ERROR_INVAL);
  85. return PAL_FALSE;
  86. }
  87. if (!ALLOC_ALIGNED(addr) || !ALLOC_ALIGNED(size)) {
  88. notify_failure(PAL_ERROR_INVAL);
  89. return PAL_FALSE;
  90. }
  91. if (check_memory_overlap(addr, size)) {
  92. notify_failure(PAL_ERROR_DENIED);
  93. return NULL;
  94. }
  95. int ret = _DkVirtualMemoryProtect(addr, size, prot);
  96. if (ret < 0) {
  97. notify_failure(-ret);
  98. return PAL_FALSE;
  99. }
  100. return PAL_TRUE;
  101. }