db_memory.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_memory.c
  15. *
  16. * This files contains APIs that allocate, free or protect virtual memory.
  17. */
  18. #include "pal_defs.h"
  19. #include "pal.h"
  20. #include "pal_internal.h"
  21. #include "pal_error.h"
  22. #include "pal_debug.h"
  23. #include "api.h"
  24. PAL_PTR
  25. DkVirtualMemoryAlloc (PAL_PTR addr, PAL_NUM size, PAL_FLG alloc_type,
  26. PAL_FLG prot)
  27. {
  28. ENTER_PAL_CALL(DkVirtualMemoryAlloc);
  29. void * map_addr = (void *) addr;
  30. if ((addr && !ALLOC_ALIGNED(addr)) || !size || !ALLOC_ALIGNED(size)) {
  31. _DkRaiseFailure(PAL_ERROR_INVAL);
  32. LEAVE_PAL_CALL_RETURN((PAL_PTR) NULL);
  33. }
  34. if (map_addr && _DkCheckMemoryMappable((void *) map_addr, size)) {
  35. _DkRaiseFailure(PAL_ERROR_DENIED);
  36. LEAVE_PAL_CALL_RETURN((PAL_PTR) NULL);
  37. }
  38. int ret = _DkVirtualMemoryAlloc(&map_addr, size, alloc_type, prot);
  39. if (ret < 0) {
  40. _DkRaiseFailure(-ret);
  41. map_addr = NULL;
  42. }
  43. LEAVE_PAL_CALL_RETURN((PAL_PTR) map_addr);
  44. }
  45. void
  46. DkVirtualMemoryFree (PAL_PTR addr, PAL_NUM size)
  47. {
  48. ENTER_PAL_CALL(DkVirtualMemoryFree);
  49. if (!addr || !size) {
  50. _DkRaiseFailure(PAL_ERROR_INVAL);
  51. LEAVE_PAL_CALL();
  52. }
  53. if (!ALLOC_ALIGNED(addr) || !ALLOC_ALIGNED(size)) {
  54. _DkRaiseFailure(PAL_ERROR_INVAL);
  55. LEAVE_PAL_CALL();
  56. }
  57. if (_DkCheckMemoryMappable((void *) addr, size)) {
  58. _DkRaiseFailure(PAL_ERROR_DENIED);
  59. LEAVE_PAL_CALL();
  60. }
  61. int ret = _DkVirtualMemoryFree((void *) addr, size);
  62. if (ret < 0) {
  63. _DkRaiseFailure(-ret);
  64. }
  65. LEAVE_PAL_CALL();
  66. }
  67. PAL_BOL
  68. DkVirtualMemoryProtect (PAL_PTR addr, PAL_NUM size, PAL_FLG prot)
  69. {
  70. ENTER_PAL_CALL(DkVirtualMemoryProtect);
  71. if (!addr || !size) {
  72. _DkRaiseFailure(PAL_ERROR_INVAL);
  73. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  74. }
  75. if (!ALLOC_ALIGNED((void *) addr) || !ALLOC_ALIGNED(size)) {
  76. _DkRaiseFailure(PAL_ERROR_INVAL);
  77. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  78. }
  79. if (_DkCheckMemoryMappable((void *) addr, size)) {
  80. _DkRaiseFailure(PAL_ERROR_DENIED);
  81. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  82. }
  83. int ret = _DkVirtualMemoryProtect((void *) addr, size, prot);
  84. if (ret < 0) {
  85. _DkRaiseFailure(-ret);
  86. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  87. }
  88. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  89. }