enclave_untrusted.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include <pal_internal.h>
  16. #include <pal_security.h>
  17. #include <pal_error.h>
  18. #include <linux_list.h>
  19. #include <api.h>
  20. #include "enclave_ocalls.h"
  21. #define allocator pal_sec.untrusted_allocator
  22. #define untrusted_slabmgr (allocator.slabmgr)
  23. #define system_lock() _DkMutexLock(allocator.lock)
  24. #define system_unlock() _DkMutexUnlock(allocator.lock)
  25. #define PAGE_SIZE (allocator.alignment)
  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. void * malloc_untrusted (int size)
  40. {
  41. void * ptr = slab_alloc(untrusted_slabmgr, size);
  42. /* the slab manger will always remain at least one byte of padding,
  43. so we can feel free to assign an offset at the byte prior to
  44. the pointer */
  45. if (ptr)
  46. *(((unsigned char *) ptr) - 1) = 0;
  47. return ptr;
  48. }
  49. void free_untrusted (void * ptr)
  50. {
  51. ptr -= *(((unsigned char *) ptr) - 1);
  52. slab_free(untrusted_slabmgr, ptr);
  53. }