db_memory.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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. PAL_PTR
  27. DkVirtualMemoryAlloc (PAL_PTR addr, PAL_NUM size, PAL_FLG alloc_type,
  28. PAL_FLG prot)
  29. {
  30. ENTER_PAL_CALL(DkVirtualMemoryAlloc);
  31. void * map_addr = (void *) addr;
  32. if ((addr && !ALLOC_ALIGNED(addr)) || !size || !ALLOC_ALIGNED(size)) {
  33. _DkRaiseFailure(PAL_ERROR_INVAL);
  34. LEAVE_PAL_CALL_RETURN((PAL_PTR) NULL);
  35. }
  36. if (map_addr && _DkCheckMemoryMappable((void *) map_addr, size)) {
  37. _DkRaiseFailure(PAL_ERROR_DENIED);
  38. LEAVE_PAL_CALL_RETURN((PAL_PTR) NULL);
  39. }
  40. int ret = _DkVirtualMemoryAlloc(&map_addr, size, alloc_type, prot);
  41. if (ret < 0) {
  42. _DkRaiseFailure(-ret);
  43. map_addr = NULL;
  44. }
  45. LEAVE_PAL_CALL_RETURN((PAL_PTR) map_addr);
  46. }
  47. void
  48. DkVirtualMemoryFree (PAL_PTR addr, PAL_NUM size)
  49. {
  50. ENTER_PAL_CALL(DkVirtualMemoryFree);
  51. if (!addr || !size) {
  52. _DkRaiseFailure(PAL_ERROR_INVAL);
  53. LEAVE_PAL_CALL();
  54. }
  55. if (!ALLOC_ALIGNED(addr) || !ALLOC_ALIGNED(size)) {
  56. _DkRaiseFailure(PAL_ERROR_INVAL);
  57. LEAVE_PAL_CALL();
  58. }
  59. if (_DkCheckMemoryMappable((void *) addr, size)) {
  60. _DkRaiseFailure(PAL_ERROR_DENIED);
  61. LEAVE_PAL_CALL();
  62. }
  63. int ret = _DkVirtualMemoryFree((void *) addr, size);
  64. if (ret < 0) {
  65. _DkRaiseFailure(-ret);
  66. }
  67. LEAVE_PAL_CALL();
  68. }
  69. PAL_BOL
  70. DkVirtualMemoryProtect (PAL_PTR addr, PAL_NUM size, PAL_FLG prot)
  71. {
  72. ENTER_PAL_CALL(DkVirtualMemoryProtect);
  73. if (!addr || !size) {
  74. _DkRaiseFailure(PAL_ERROR_INVAL);
  75. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  76. }
  77. if (!ALLOC_ALIGNED((void *) addr) || !ALLOC_ALIGNED(size)) {
  78. _DkRaiseFailure(PAL_ERROR_INVAL);
  79. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  80. }
  81. if (_DkCheckMemoryMappable((void *) addr, size)) {
  82. _DkRaiseFailure(PAL_ERROR_DENIED);
  83. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  84. }
  85. int ret = _DkVirtualMemoryProtect((void *) addr, size, prot);
  86. if (ret < 0) {
  87. _DkRaiseFailure(-ret);
  88. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  89. }
  90. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  91. }