enclave_untrusted.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include <pal_internal.h>
  16. #include <pal_security.h>
  17. #include <pal_error.h>
  18. #include <api.h>
  19. #include <assert.h>
  20. #include "enclave_ocalls.h"
  21. static PAL_LOCK malloc_lock = LOCK_INIT;
  22. static int pagesize = PRESET_PAGESIZE;
  23. #define system_lock() _DkSpinLock(&malloc_lock)
  24. #define system_unlock() _DkSpinUnlock(&malloc_lock)
  25. #define PAGE_SIZE pagesize
  26. static inline void * __malloc (int size)
  27. {
  28. void * addr = NULL;
  29. ocall_alloc_untrusted(size, &addr);
  30. return addr;
  31. }
  32. #define system_malloc(size) __malloc(size)
  33. static inline void __free (void * addr, int size)
  34. {
  35. ocall_unmap_untrusted(addr, size);
  36. }
  37. #define system_free(addr, size) __free(addr, size)
  38. #include "slabmgr.h"
  39. static SLAB_MGR untrusted_slabmgr = NULL;
  40. void init_untrusted_slab_mgr (int pagesize)
  41. {
  42. if (untrusted_slabmgr)
  43. return;
  44. untrusted_slabmgr = create_slab_mgr();
  45. if (!untrusted_slabmgr)
  46. init_fail(PAL_ERROR_NOMEM, "cannot initialize slab manager");
  47. }
  48. void * malloc_untrusted (int size)
  49. {
  50. return slab_alloc(untrusted_slabmgr, size);
  51. }
  52. void free_untrusted (void * ptr)
  53. {
  54. slab_free(untrusted_slabmgr, ptr);
  55. }