enclave_untrusted.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include <api.h>
  14. #include <pal_error.h>
  15. #include <pal_internal.h>
  16. #include <pal_security.h>
  17. #include "enclave_ocalls.h"
  18. static PAL_LOCK malloc_lock = LOCK_INIT;
  19. static size_t pagesize = PRESET_PAGESIZE;
  20. #define SYSTEM_LOCK() _DkSpinLock(&malloc_lock)
  21. #define SYSTEM_UNLOCK() _DkSpinUnlock(&malloc_lock)
  22. #define PAGE_SIZE pagesize
  23. static inline void* __malloc(int size) {
  24. void* addr = NULL;
  25. ocall_mmap_untrusted(-1, 0, size, PROT_READ | PROT_WRITE, &addr);
  26. return addr;
  27. }
  28. #define system_malloc(size) __malloc(size)
  29. static inline void __free(void* addr, int size) {
  30. ocall_munmap_untrusted(addr, size);
  31. }
  32. #define system_free(addr, size) __free(addr, size)
  33. #include "slabmgr.h"
  34. static SLAB_MGR untrusted_slabmgr = NULL;
  35. void init_untrusted_slab_mgr() {
  36. if (untrusted_slabmgr)
  37. return;
  38. untrusted_slabmgr = create_slab_mgr();
  39. if (!untrusted_slabmgr)
  40. INIT_FAIL(PAL_ERROR_NOMEM, "cannot initialize slab manager");
  41. }
  42. void* malloc_untrusted(int size) {
  43. return slab_alloc(untrusted_slabmgr, size);
  44. }
  45. void free_untrusted(void* ptr) {
  46. slab_free(untrusted_slabmgr, ptr);
  47. }