enclave_untrusted.c 1.8 KB

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