enclave_untrusted.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <spinlock.h>
  18. #include "enclave_ocalls.h"
  19. static spinlock_t malloc_lock = INIT_SPINLOCK_UNLOCKED;
  20. static size_t g_page_size = PRESET_PAGESIZE;
  21. #define SYSTEM_LOCK() spinlock_lock(&malloc_lock)
  22. #define SYSTEM_UNLOCK() spinlock_unlock(&malloc_lock)
  23. #define SYSTEM_LOCKED() spinlock_is_locked(&malloc_lock)
  24. #define ALLOC_ALIGNMENT g_page_size
  25. static inline void* __malloc(int size) {
  26. void* addr = NULL;
  27. ocall_mmap_untrusted(-1, 0, size, PROT_READ | PROT_WRITE, &addr);
  28. return addr;
  29. }
  30. #define system_malloc(size) __malloc(size)
  31. static inline void __free(void* addr, int size) {
  32. ocall_munmap_untrusted(addr, size);
  33. }
  34. #define system_free(addr, size) __free(addr, size)
  35. #include "slabmgr.h"
  36. static SLAB_MGR untrusted_slabmgr = NULL;
  37. void init_untrusted_slab_mgr() {
  38. if (untrusted_slabmgr)
  39. return;
  40. untrusted_slabmgr = create_slab_mgr();
  41. if (!untrusted_slabmgr)
  42. INIT_FAIL(PAL_ERROR_NOMEM, "cannot initialize slab manager");
  43. }
  44. void* malloc_untrusted(int size) {
  45. return slab_alloc(untrusted_slabmgr, size);
  46. }
  47. void free_untrusted(void* ptr) {
  48. slab_free(untrusted_slabmgr, ptr);
  49. }