db_memory.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "enclave_pages.h"
  20. #include "pal.h"
  21. #include "pal_debug.h"
  22. #include "pal_defs.h"
  23. #include "pal_error.h"
  24. #include "pal_internal.h"
  25. #include "pal_linux.h"
  26. #include "pal_linux_defs.h"
  27. #include "pal_security.h"
  28. extern struct atomic_int g_alloced_pages;
  29. extern size_t g_page_size;
  30. bool _DkCheckMemoryMappable(const void* addr, size_t size) {
  31. if (addr < DATA_END && addr + size > TEXT_START) {
  32. SGX_DBG(DBG_E, "Address %p-%p is not mappable\n", addr, addr + size);
  33. return true;
  34. }
  35. /* FIXME: this function is almost useless now; note that _DkVirtualMemoryAlloc() checks whether
  36. * [addr, addr + size) overlaps with VMAs and errors out */
  37. return false;
  38. }
  39. int _DkVirtualMemoryAlloc(void** paddr, uint64_t size, int alloc_type, int prot) {
  40. if (!size || !WITHIN_MASK(prot, PAL_PROT_MASK))
  41. return -PAL_ERROR_INVAL;
  42. void* addr = *paddr;
  43. if ((alloc_type & PAL_ALLOC_INTERNAL) && addr) {
  44. /* internal-PAL memory allocation never uses fixed addresses */
  45. return -PAL_ERROR_INVAL;
  46. }
  47. void* mem = get_enclave_pages(addr, size, alloc_type & PAL_ALLOC_INTERNAL);
  48. if (!mem)
  49. return addr ? -PAL_ERROR_DENIED : -PAL_ERROR_NOMEM;
  50. memset(mem, 0, size);
  51. *paddr = mem;
  52. return 0;
  53. }
  54. int _DkVirtualMemoryFree(void* addr, uint64_t size) {
  55. if (sgx_is_completely_within_enclave(addr, size)) {
  56. int ret = free_enclave_pages(addr, size);
  57. if (ret < 0) {
  58. return ret;
  59. }
  60. } else {
  61. /* possible to have untrusted mapping, simply unmap memory outside the enclave */
  62. ocall_munmap_untrusted(addr, size);
  63. }
  64. return 0;
  65. }
  66. int _DkVirtualMemoryProtect(void* addr, uint64_t size, int prot) {
  67. __UNUSED(addr);
  68. __UNUSED(size);
  69. __UNUSED(prot);
  70. static struct atomic_int at_cnt = {.counter = 0};
  71. if (atomic_cmpxchg(&at_cnt, 0, 1) == 0)
  72. SGX_DBG(DBG_M, "[Warning] DkVirtualMemoryProtect is unimplemented in Linux-SGX PAL");
  73. return 0;
  74. }
  75. uint64_t _DkMemoryQuota(void) {
  76. return pal_sec.heap_max - pal_sec.heap_min;
  77. }
  78. uint64_t _DkMemoryAvailableQuota(void) {
  79. return (pal_sec.heap_max - pal_sec.heap_min) - atomic_read(&g_alloced_pages) * g_page_size;
  80. }