enclave_mngr.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (C) 2011-2017 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 <string.h>
  32. #include "se_memory.h"
  33. #include "se_memcpy.h"
  34. #include "util.h"
  35. #include "enclave_mngr.h"
  36. #include "se_atomic.h"
  37. static uint32_t atomic_inc32(uint32_t volatile *val)
  38. {
  39. return se_atomic_inc(val);
  40. }
  41. uint32_t CEnclaveSim::m_counter = 1;
  42. sgx_enclave_id_t CEnclaveSim::gen_enclave_id(void)
  43. {
  44. //getpid() is to simulate fork() scenario, refer to do_ecall in sig_handler.cpp
  45. sgx_enclave_id_t id = ((uint64_t)getpid() << 32) | atomic_inc32(&m_counter);
  46. return id;
  47. }
  48. CEnclaveSim::CEnclaveSim(const secs_t* secs)
  49. {
  50. m_cpages = static_cast<size_t>(secs->size >> SE_PAGE_SHIFT);
  51. m_flags = new si_flags_t[m_cpages];
  52. // pages flags are initialized to -1
  53. memset(m_flags, 0xff, m_cpages * sizeof(si_flags_t));
  54. memcpy_s(&m_secs, sizeof(m_secs), secs, sizeof(*secs));
  55. m_enclave_id = gen_enclave_id();
  56. }
  57. CEnclaveSim::~CEnclaveSim()
  58. {
  59. delete[] m_flags;
  60. se_virtual_free(m_secs.base, (size_t)m_secs.size, MEM_RELEASE);
  61. }
  62. sgx_enclave_id_t CEnclaveSim::get_enclave_id() const
  63. {
  64. return m_enclave_id;
  65. }
  66. secs_t* CEnclaveSim::get_secs()
  67. {
  68. return &m_secs;
  69. }
  70. size_t CEnclaveSim::get_pg_idx(const void* pgaddr) const
  71. {
  72. return PTR_DIFF(pgaddr, m_secs.base) >> SE_PAGE_SHIFT;
  73. }
  74. bool CEnclaveSim::validate_pg_and_flags(const void* addr, si_flags_t flags)
  75. {
  76. // Must be page aligned
  77. if (!IS_PAGE_ALIGNED(addr))
  78. return false;
  79. size_t page_idx = get_pg_idx(addr);
  80. // Must be within enclave address space
  81. if (page_idx >= m_cpages)
  82. return false;
  83. // Requested flags should only set those visible by instuctions
  84. if ((flags & (~SI_FLAGS_EXTERNAL)))
  85. return false;
  86. return true;
  87. }
  88. bool CEnclaveSim::add_page(const void* addr, si_flags_t flags)
  89. {
  90. if (!validate_pg_and_flags(addr, flags))
  91. return false;
  92. // We only deal with these flags
  93. flags &= static_cast<si_flags_t>(SI_FLAGS_EXTERNAL);
  94. // Must not have been added yet
  95. size_t page_idx = get_pg_idx(addr);
  96. if (m_flags[page_idx] != (si_flags_t)-1)
  97. return false;
  98. m_flags[page_idx] = flags;
  99. return true;
  100. }
  101. bool CEnclaveSim::remove_page(const void* epc_lin_addr)
  102. {
  103. size_t page_idx = get_pg_idx(epc_lin_addr);
  104. if (m_flags[page_idx] != (si_flags_t)-1) {
  105. m_flags[page_idx] = (si_flags_t)-1;
  106. return true;
  107. }
  108. return false;
  109. }
  110. bool CEnclaveSim::is_tcs_page(const void* addr) const
  111. {
  112. // Must be page aligned
  113. if (!IS_PAGE_ALIGNED(addr))
  114. return false;
  115. size_t page_idx = get_pg_idx(addr);
  116. // Must be within enclave address space
  117. if (page_idx >= m_cpages)
  118. return false;
  119. return (m_flags[page_idx] & SI_FLAG_PT_MASK) == SI_FLAG_TCS;
  120. }
  121. //////////////////////////////////////////////////////////////////////
  122. CEnclaveMngr::CEnclaveMngr()
  123. {
  124. se_mutex_init(&m_list_lock);
  125. }
  126. CEnclaveMngr::~CEnclaveMngr()
  127. {
  128. se_mutex_destroy(&m_list_lock);
  129. std::list<CEnclaveSim*>::iterator it = m_enclave_list.begin();
  130. for (; it != m_enclave_list.end(); ++it)
  131. {
  132. delete (*it);
  133. }
  134. }
  135. // Note: this singleton implemenation is not multi-threading safe.
  136. CEnclaveMngr* CEnclaveMngr::get_instance()
  137. {
  138. static CEnclaveMngr mngr;
  139. return &mngr;
  140. }
  141. // Use constructor attribute to make sure that the later calling of
  142. // CEnclaveMngr::get_instance() is MT-safe.
  143. __attribute__ ((__constructor__)) static void build_mngr_instance()
  144. {
  145. CEnclaveMngr::get_instance();
  146. }
  147. void CEnclaveMngr::add(CEnclaveSim* ce)
  148. {
  149. if (ce != NULL)
  150. {
  151. se_mutex_lock(&m_list_lock);
  152. m_enclave_list.push_back(ce);
  153. se_mutex_unlock(&m_list_lock);
  154. }
  155. }
  156. void CEnclaveMngr::remove(CEnclaveSim* ce)
  157. {
  158. if (ce != NULL)
  159. {
  160. se_mutex_lock(&m_list_lock);
  161. m_enclave_list.remove(ce);
  162. se_mutex_unlock(&m_list_lock);
  163. }
  164. }
  165. CEnclaveSim* CEnclaveMngr::get_enclave(const sgx_enclave_id_t id)
  166. {
  167. CEnclaveSim* ce = NULL;
  168. se_mutex_lock(&m_list_lock);
  169. std::list<CEnclaveSim*>::iterator it = m_enclave_list.begin();
  170. for (; it != m_enclave_list.end(); ++it)
  171. {
  172. if ((*it)->get_enclave_id() == id)
  173. {
  174. ce = *it;
  175. break;
  176. }
  177. }
  178. se_mutex_unlock(&m_list_lock);
  179. return ce;
  180. }
  181. CEnclaveSim* CEnclaveMngr::get_enclave(const void* base_addr)
  182. {
  183. CEnclaveSim* ce = NULL;
  184. se_mutex_lock(&m_list_lock);
  185. std::list<CEnclaveSim*>::iterator it = m_enclave_list.begin();
  186. for (; it != m_enclave_list.end(); ++it)
  187. {
  188. secs_t* secs = (*it)->get_secs();
  189. if (base_addr >= secs->base &&
  190. PTR_DIFF(base_addr, secs->base) < secs->size)
  191. {
  192. ce = *it;
  193. break;
  194. }
  195. }
  196. se_mutex_unlock(&m_list_lock);
  197. return ce;
  198. }
  199. CEnclaveSim* CEnclaveMngr::get_enclave(const secs_t* secs)
  200. {
  201. // The pEnclaveSECS field might not have been initialized yet.
  202. return get_enclave(secs->base);
  203. }