db_memory.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. PAL_PTR
  27. DkVirtualMemoryAlloc (PAL_PTR addr, PAL_NUM size, PAL_FLG alloc_type,
  28. PAL_FLG prot)
  29. {
  30. store_frame(VirtualMemoryAlloc);
  31. void * map_addr = (void *) addr;
  32. if ((addr && !ALLOC_ALIGNED(addr)) || !size || !ALLOC_ALIGNED(size))
  33. leave_frame(NULL, PAL_ERROR_INVAL);
  34. if (map_addr && _DkCheckMemoryMappable((void *) map_addr, size))
  35. leave_frame(NULL, PAL_ERROR_DENIED);
  36. int ret = _DkVirtualMemoryAlloc(&map_addr, size, alloc_type, prot);
  37. if (ret < 0)
  38. leave_frame(NULL, -ret);
  39. leave_frame(map_addr, 0);
  40. }
  41. void
  42. DkVirtualMemoryFree (PAL_PTR addr, PAL_NUM size)
  43. {
  44. store_frame(VirtualMemoryFree);
  45. if (!addr || !size)
  46. leave_frame(, PAL_ERROR_INVAL);
  47. if (!ALLOC_ALIGNED(addr) || !ALLOC_ALIGNED(size))
  48. leave_frame(, PAL_ERROR_INVAL);
  49. if (_DkCheckMemoryMappable((void *) addr, size))
  50. leave_frame(, PAL_ERROR_DENIED);
  51. int ret = _DkVirtualMemoryFree((void *) addr, size);
  52. if (ret < 0)
  53. leave_frame(, -ret);
  54. leave_frame(, 0);
  55. }
  56. PAL_BOL
  57. DkVirtualMemoryProtect (PAL_PTR addr, PAL_NUM size, PAL_FLG prot)
  58. {
  59. store_frame(VirtualMemoryProtect);
  60. if (!addr || !size)
  61. leave_frame(PAL_FALSE, PAL_ERROR_INVAL);
  62. if (!ALLOC_ALIGNED(addr) || !ALLOC_ALIGNED(size))
  63. leave_frame(PAL_FALSE, PAL_ERROR_INVAL);
  64. if (_DkCheckMemoryMappable((void *) addr, size))
  65. leave_frame(PAL_FALSE, PAL_ERROR_DENIED);
  66. int ret = _DkVirtualMemoryProtect((void *) addr, size, prot);
  67. if (ret < 0)
  68. leave_frame(PAL_FALSE, -ret);
  69. leave_frame(PAL_TRUE, 0);
  70. }