db_memory.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_linux_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_linux.h"
  25. #include "pal_security.h"
  26. #include "pal_error.h"
  27. #include "pal_debug.h"
  28. #include "api.h"
  29. #include <asm/mman.h>
  30. #include "enclave_pages.h"
  31. #define PAL_VMA_MAX 64
  32. static struct pal_vma {
  33. void * top, * bottom;
  34. } pal_vmas[PAL_VMA_MAX];
  35. static unsigned int pal_nvmas = 0;
  36. static struct spinlock pal_vma_lock;
  37. bool _DkCheckMemoryMappable (const void * addr, int size)
  38. {
  39. if (addr < DATA_END && addr + size > TEXT_START) {
  40. printf("address %p-%p is not mappable\n", addr, addr + size);
  41. return true;
  42. }
  43. _DkSpinLock(&pal_vma_lock);
  44. for (int i = 0 ; i < pal_nvmas ; i++)
  45. if (addr < pal_vmas[i].top && addr + size > pal_vmas[i].bottom) {
  46. printf("address %p-%p is not mappable\n", addr, addr + size);
  47. _DkSpinUnlock(&pal_vma_lock);
  48. return true;
  49. }
  50. _DkSpinUnlock(&pal_vma_lock);
  51. return false;
  52. }
  53. int _DkVirtualMemoryAlloc (void ** paddr, uint64_t size, int alloc_type, int prot)
  54. {
  55. void * addr = *paddr, * mem;
  56. //int flags = HOST_FLAGS(alloc_type, prot|PAL_PROT_WRITECOPY);
  57. //prot = HOST_PROT(prot);
  58. /* The memory should have MAP_PRIVATE and MAP_ANONYMOUS */
  59. //flags |= MAP_ANONYMOUS|(addr ? MAP_FIXED : 0);
  60. //mem = (void *) ARCH_MMAP(addr, size, prot, flags, -1, 0);
  61. if ((alloc_type & PAL_ALLOC_INTERNAL) && addr)
  62. return -PAL_ERROR_INVAL;
  63. if (size == 0)
  64. asm volatile ("int $3");
  65. mem = get_reserved_pages(addr, size);
  66. if (!mem)
  67. return addr ? -PAL_ERROR_DENIED : -PAL_ERROR_NOMEM;
  68. memset(mem, 0, size);
  69. if (alloc_type & PAL_ALLOC_INTERNAL) {
  70. SGX_DBG(DBG_M, "pal allocates %p-%p for internal use\n", mem, mem + size);
  71. _DkSpinLock(&pal_vma_lock);
  72. assert(pal_nvmas < PAL_VMA_MAX);
  73. pal_vmas[pal_nvmas].bottom = mem;
  74. pal_vmas[pal_nvmas].top = mem + size;
  75. pal_nvmas++;
  76. _DkSpinUnlock(&pal_vma_lock);
  77. }
  78. *paddr = mem;
  79. return 0;
  80. }
  81. int _DkVirtualMemoryFree (void * addr, uint64_t size)
  82. {
  83. free_pages(addr, size);
  84. return 0;
  85. }
  86. int _DkVirtualMemoryProtect (void * addr, uint64_t size, int prot)
  87. {
  88. return 0;
  89. }
  90. unsigned long _DkMemoryQuota (void)
  91. {
  92. return pal_sec.heap_max - pal_sec.heap_min;
  93. }
  94. extern struct atomic_int alloced_pages;
  95. extern unsigned int pagesz;
  96. unsigned long _DkMemoryAvailableQuota (void)
  97. {
  98. return (pal_sec.heap_max - pal_sec.heap_min) -
  99. atomic_read(&alloced_pages) * pagesz;
  100. }