enclave_mutex.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "sgx_defs.h"
  32. #include "enclave.h"
  33. #include "se_event.h"
  34. #include "internal/se_error_internal.h"
  35. /* wait on untrusted event */
  36. extern "C" int sgx_thread_wait_untrusted_event_ocall(const void *self)
  37. {
  38. if (self == NULL)
  39. return SGX_ERROR_INVALID_PARAMETER;
  40. se_handle_t hevent = CEnclavePool::instance()->get_event(self);
  41. if (hevent == NULL)
  42. return SE_ERROR_MUTEX_GET_EVENT;
  43. if (SE_MUTEX_SUCCESS != se_event_wait(hevent))
  44. return SE_ERROR_MUTEX_WAIT_EVENT;
  45. return SGX_SUCCESS;
  46. }
  47. /* set untrusted event */
  48. extern "C" int sgx_thread_set_untrusted_event_ocall(const void *waiter)
  49. {
  50. if (waiter == NULL)
  51. return SGX_ERROR_INVALID_PARAMETER;
  52. se_handle_t hevent = CEnclavePool::instance()->get_event(waiter);
  53. if (hevent == NULL)
  54. return SE_ERROR_MUTEX_GET_EVENT;
  55. if (SE_MUTEX_SUCCESS != se_event_wake(hevent))
  56. return SE_ERROR_MUTEX_WAKE_EVENT;
  57. return SGX_SUCCESS;
  58. }
  59. extern "C" int sgx_thread_set_multiple_untrusted_events_ocall(const void **waiters, size_t total)
  60. {
  61. if (waiters == NULL || *waiters == NULL)
  62. return SGX_ERROR_INVALID_PARAMETER;
  63. for (unsigned int i = 0; i < total; i++) {
  64. se_handle_t hevent = CEnclavePool::instance()->get_event(*waiters++);
  65. if (hevent == NULL)
  66. return SE_ERROR_MUTEX_GET_EVENT;
  67. if (SE_MUTEX_SUCCESS != se_event_wake(hevent))
  68. return SE_ERROR_MUTEX_WAKE_EVENT;
  69. }
  70. return SGX_SUCCESS;
  71. }
  72. extern "C" int sgx_thread_setwait_untrusted_events_ocall(const void *waiter, const void *self)
  73. {
  74. int ret = sgx_thread_set_untrusted_event_ocall(waiter);
  75. if (ret != SGX_SUCCESS) return ret;
  76. return sgx_thread_wait_untrusted_event_ocall(self);
  77. }