db_memory.c 3.0 KB

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