tcs.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 "tcs.h"
  32. #include "se_trace.h"
  33. #include "sgx_error.h"
  34. #include "se_memory.h"
  35. #include "se_thread.h"
  36. #include <assert.h>
  37. #include "routine.h"
  38. #include "enclave_creator.h"
  39. #include "rts.h"
  40. #include "enclave.h"
  41. extern se_thread_id_t get_thread_id();
  42. int do_ecall(const int fn, const void *ocall_table, const void *ms, CTrustThread *trust_thread);
  43. CTrustThread::CTrustThread(tcs_t *tcs, CEnclave* enclave)
  44. : m_tcs(tcs)
  45. , m_enclave(enclave)
  46. , m_reference(0)
  47. , m_event(NULL)
  48. {
  49. memset(&m_tcs_info, 0, sizeof(debug_tcs_info_t));
  50. m_tcs_info.TCS_address = reinterpret_cast<void*>(tcs);
  51. m_tcs_info.ocall_frame = 0;
  52. m_tcs_info.thread_id = 0;
  53. }
  54. CTrustThread::~CTrustThread()
  55. {
  56. se_event_destroy(m_event);
  57. m_event = NULL;
  58. }
  59. se_handle_t CTrustThread::get_event()
  60. {
  61. if (m_event == NULL)
  62. m_event = se_event_init();
  63. return m_event;
  64. }
  65. void CTrustThread::push_ocall_frame(ocall_frame_t* frame_point)
  66. {
  67. frame_point->index = this->get_reference();
  68. frame_point->pre_last_frame = m_tcs_info.ocall_frame;
  69. m_tcs_info.ocall_frame = reinterpret_cast<uintptr_t>(frame_point);
  70. m_tcs_info.thread_id = get_thread_id();
  71. }
  72. void CTrustThread::pop_ocall_frame()
  73. {
  74. ocall_frame_t* last_ocall_frame = reinterpret_cast<ocall_frame_t*>(m_tcs_info.ocall_frame);
  75. if (last_ocall_frame)
  76. {
  77. m_tcs_info.ocall_frame = last_ocall_frame->pre_last_frame;
  78. }
  79. }
  80. CTrustThreadPool::CTrustThreadPool(uint32_t tcs_min_pool)
  81. {
  82. m_thread_list = NULL;
  83. m_utility_thread = NULL;
  84. m_tcs_min_pool = tcs_min_pool;
  85. m_need_to_wait_for_new_thread = false;
  86. }
  87. CTrustThreadPool::~CTrustThreadPool()
  88. {
  89. LockGuard lock(&m_thread_mutex);
  90. //destroy free tcs list
  91. for(vector<CTrustThread *>::iterator it=m_free_thread_vector.begin(); it!=m_free_thread_vector.end(); it++)
  92. {
  93. delete *it;
  94. }
  95. m_free_thread_vector.clear();
  96. //destroy unallocated tcs list
  97. for(vector<CTrustThread *>::iterator it=m_unallocated_threads.begin(); it!=m_unallocated_threads.end(); it++)
  98. {
  99. delete *it;
  100. }
  101. m_unallocated_threads.clear();
  102. //destroy thread cache
  103. Node<se_thread_id_t, CTrustThread*>* it = m_thread_list, *tmp = NULL;
  104. while (it != NULL)
  105. {
  106. delete it->value;
  107. tmp = it;
  108. it = it->next;
  109. delete tmp;
  110. }
  111. m_thread_list = NULL;
  112. if (m_utility_thread)
  113. {
  114. delete m_utility_thread;
  115. m_utility_thread = NULL;
  116. }
  117. }
  118. void get_thread_set(vector<se_thread_id_t> &thread_vector);
  119. inline int CTrustThreadPool::find_thread(vector<se_thread_id_t> &thread_vector, se_thread_id_t thread_id)
  120. {
  121. for(vector<se_thread_id_t>::iterator it=thread_vector.begin(); it!=thread_vector.end(); it++)
  122. if(*it == thread_id)
  123. return TRUE;
  124. return FALSE;
  125. }
  126. inline CTrustThread * CTrustThreadPool::get_free_thread()
  127. {
  128. LockGuard lock(&m_free_thread_mutex);
  129. if(true == m_free_thread_vector.empty())
  130. {
  131. return NULL;
  132. }
  133. //if there is free tcs, remove it from free list
  134. CTrustThread *thread_node = m_free_thread_vector.back();
  135. m_free_thread_vector.pop_back();
  136. return thread_node;
  137. }
  138. //This tcs policy is bind tcs with one thread.
  139. int CTrustThreadPool::bind_thread(const se_thread_id_t thread_id, CTrustThread * const trust_thread)
  140. {
  141. if (m_thread_list == NULL) {
  142. m_thread_list = new Node<se_thread_id_t, CTrustThread*>(thread_id, trust_thread);
  143. } else {
  144. Node<se_thread_id_t, CTrustThread*>* it = new Node<se_thread_id_t, CTrustThread*>(thread_id, trust_thread);
  145. if (m_thread_list->InsertNext(it) == false) {
  146. delete it;
  147. SE_TRACE(SE_TRACE_WARNING, "trust thread %x is already added to the list\n", trust_thread);
  148. return FALSE;
  149. }
  150. }
  151. return TRUE;
  152. }
  153. CTrustThread * CTrustThreadPool::get_bound_thread(const se_thread_id_t thread_id)
  154. {
  155. CTrustThread *trust_thread = nullptr;
  156. if (m_thread_list)
  157. {
  158. auto it = m_thread_list->Find(thread_id);
  159. if (it)
  160. trust_thread = it->value;
  161. }
  162. return trust_thread;
  163. }
  164. CTrustThread * CTrustThreadPool::add_thread(tcs_t * const tcs, CEnclave * const enclave, bool is_unallocated)
  165. {
  166. CTrustThread *trust_thread = new CTrustThread(tcs, enclave);
  167. LockGuard lock(&m_thread_mutex);
  168. //add tcs to free list
  169. if(!is_unallocated)
  170. {
  171. if (g_enclave_creator->is_EDMM_supported(enclave->get_enclave_id()) && !m_utility_thread && (enclave->get_dynamic_tcs_list_size() != 0))
  172. m_utility_thread = trust_thread;
  173. else
  174. m_free_thread_vector.push_back(trust_thread);
  175. }
  176. else
  177. {
  178. m_unallocated_threads.push_back(trust_thread);
  179. }
  180. return trust_thread;
  181. }
  182. CTrustThread *CTrustThreadPool::get_bound_thread(const tcs_t *tcs)
  183. {
  184. //Since now this function will be call outside, we need get lock to protect map
  185. LockGuard lock(&m_thread_mutex);
  186. CTrustThread *trust_thread = NULL;
  187. if (m_thread_list == NULL)
  188. return NULL;
  189. Node<se_thread_id_t, CTrustThread*>* it = m_thread_list;
  190. while (it != NULL) {
  191. trust_thread = it->value;
  192. if(trust_thread->get_tcs() == tcs) {
  193. return trust_thread;
  194. }
  195. it = it->next;
  196. }
  197. return NULL;
  198. }
  199. std::vector<CTrustThread *> CTrustThreadPool::get_thread_list()
  200. {
  201. LockGuard lock(&m_thread_mutex);
  202. vector<CTrustThread *> threads;
  203. for(vector<CTrustThread *>::iterator it = m_free_thread_vector.begin(); it != m_free_thread_vector.end(); it++)
  204. {
  205. threads.push_back(*it);
  206. }
  207. Node<se_thread_id_t, CTrustThread*>* it = m_thread_list;
  208. while (it != NULL) {
  209. threads.push_back(it->value);
  210. it = it->next;
  211. }
  212. return threads;
  213. }
  214. void CTrustThreadPool::reset()
  215. {
  216. //get lock at the begin of list walk.
  217. LockGuard lock(&m_thread_mutex);
  218. //walk through thread cache to free every element;
  219. Node<se_thread_id_t, CTrustThread*>* it = m_thread_list, *tmp = NULL;
  220. while(it != NULL)
  221. {
  222. tmp = it;
  223. it = it->next;
  224. CTrustThread *trust_thread = tmp->value;
  225. //remove from thread cache
  226. delete tmp;
  227. trust_thread->reset_ref();
  228. add_to_free_thread_vector(trust_thread);
  229. }
  230. m_thread_list = NULL;
  231. return;
  232. }
  233. void CTrustThreadPool::wake_threads()
  234. {
  235. LockGuard lock(&m_thread_mutex);
  236. Node<se_thread_id_t, CTrustThread*>* it = m_thread_list;
  237. while (it != NULL) {
  238. CTrustThread *thread = it->value;
  239. se_handle_t event = thread->get_event();
  240. se_event_wake(event);
  241. it = it->next;
  242. }
  243. }
  244. CTrustThread * CTrustThreadPool::_acquire_thread()
  245. {
  246. //try to get tcs from thread cache
  247. se_thread_id_t thread_id = get_thread_id();
  248. CTrustThread *trust_thread = get_bound_thread(thread_id);
  249. if(NULL != trust_thread && m_utility_thread != trust_thread)
  250. {
  251. return trust_thread;
  252. }
  253. //try get tcs from free list;
  254. trust_thread = get_free_thread();
  255. //if there is no free tcs, collect useless tcs.
  256. if(NULL == trust_thread)
  257. {
  258. if(!garbage_collect())
  259. return NULL;
  260. //get tcs from free list again.
  261. trust_thread = get_free_thread();
  262. assert(NULL != trust_thread);
  263. }
  264. //we have got a free tcs. add the tcs to thread cache
  265. bind_thread(thread_id, trust_thread);
  266. return trust_thread;
  267. }
  268. CTrustThread * CTrustThreadPool::acquire_thread(bool is_initialize_ecall)
  269. {
  270. LockGuard lock(&m_thread_mutex);
  271. CTrustThread *trust_thread = NULL;
  272. if(is_initialize_ecall == true)
  273. {
  274. if (m_utility_thread)
  275. {
  276. trust_thread = m_utility_thread;
  277. assert(trust_thread != NULL);
  278. }
  279. else
  280. {
  281. trust_thread = _acquire_thread();
  282. }
  283. }
  284. else
  285. {
  286. trust_thread = _acquire_thread();
  287. // for edmm feature, we don't support simulation mode yet
  288. // m_utility_thread will be NULL in simulation mode
  289. if(NULL == trust_thread && NULL != m_utility_thread)
  290. {
  291. m_need_to_wait_for_new_thread_cond.lock();
  292. m_utility_thread->get_enclave()->fill_tcs_mini_pool_fn();
  293. m_need_to_wait_for_new_thread = true;
  294. while(m_need_to_wait_for_new_thread != false)
  295. {
  296. m_need_to_wait_for_new_thread_cond.wait();
  297. }
  298. m_need_to_wait_for_new_thread_cond.unlock();
  299. trust_thread = _acquire_thread();
  300. }
  301. }
  302. if(trust_thread)
  303. {
  304. trust_thread->increase_ref();
  305. }
  306. if(is_initialize_ecall != true &&
  307. need_to_new_thread() == true)
  308. {
  309. m_utility_thread->get_enclave()->fill_tcs_mini_pool_fn();
  310. }
  311. return trust_thread;
  312. }
  313. //Do nothing for bind mode, the tcs is always bound to a thread.
  314. void CTrustThreadPool::release_thread(CTrustThread * const trust_thread)
  315. {
  316. LockGuard lock(&m_thread_mutex);
  317. trust_thread->decrease_ref();
  318. return;
  319. }
  320. bool CTrustThreadPool::is_dynamic_thread_exist()
  321. {
  322. if (m_unallocated_threads.empty())
  323. {
  324. return false;
  325. }
  326. else
  327. {
  328. return true;
  329. }
  330. }
  331. bool CTrustThreadPool::need_to_new_thread()
  332. {
  333. LockGuard lock(&m_free_thread_mutex);
  334. if (m_unallocated_threads.empty())
  335. {
  336. return false;
  337. }
  338. if(m_tcs_min_pool == 0 && m_free_thread_vector.size() > m_tcs_min_pool)
  339. {
  340. return false;
  341. }
  342. if(m_tcs_min_pool != 0 && m_free_thread_vector.size() >= m_tcs_min_pool)
  343. {
  344. return false;
  345. }
  346. return true;
  347. }
  348. static int make_tcs(size_t tcs)
  349. {
  350. return g_enclave_creator->mktcs(tcs);
  351. }
  352. struct ms_str
  353. {
  354. void * ms;
  355. };
  356. #define fastcall __attribute__((regparm(3),noinline,visibility("default")))
  357. //this function is used to notify GDB scripts
  358. //GDB is supposed to have a breakpoint on urts_add_tcs to receive debug interupt
  359. //once the breakpoint has been hit, GDB extracts the address of tcs and sets DBGOPTIN for the tcs
  360. extern "C" void fastcall urts_add_tcs(tcs_t * const tcs)
  361. {
  362. UNUSED(tcs);
  363. SE_TRACE(SE_TRACE_WARNING, "urts_add_tcs %x\n", tcs);
  364. }
  365. sgx_status_t CTrustThreadPool::new_thread()
  366. {
  367. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  368. if(!m_utility_thread)
  369. {
  370. return ret;
  371. }
  372. if (m_unallocated_threads.empty())
  373. {
  374. return SGX_SUCCESS;
  375. }
  376. size_t octbl_buf[ROUND_TO(sizeof(sgx_ocall_table_t) + sizeof(void*), sizeof(size_t)) / sizeof(size_t)];
  377. sgx_ocall_table_t *octbl = reinterpret_cast<sgx_ocall_table_t*>(octbl_buf);
  378. octbl->count = 1;
  379. void **ocalls = octbl->ocall;
  380. *ocalls = reinterpret_cast<void*>(make_tcs);
  381. CTrustThread *trust_thread = m_unallocated_threads.back();
  382. tcs_t *tcsp = trust_thread->get_tcs();
  383. struct ms_str ms1;
  384. ms1.ms = tcsp;
  385. ret = (sgx_status_t)do_ecall(ECMD_MKTCS, octbl, &ms1, m_utility_thread);
  386. if (SGX_SUCCESS == ret )
  387. {
  388. //add tcs to debug tcs info list
  389. trust_thread->get_enclave()->add_thread(trust_thread);
  390. add_to_free_thread_vector(trust_thread);
  391. m_unallocated_threads.pop_back();
  392. urts_add_tcs(tcsp);
  393. }
  394. return ret;
  395. }
  396. void CTrustThreadPool::add_to_free_thread_vector(CTrustThread* it)
  397. {
  398. LockGuard lock(&m_free_thread_mutex);
  399. m_free_thread_vector.push_back(it);
  400. }
  401. sgx_status_t CTrustThreadPool::fill_tcs_mini_pool()
  402. {
  403. sgx_status_t ret = SGX_SUCCESS;
  404. bool stop = false;
  405. while(stop != true)
  406. {
  407. if(need_to_new_thread() == true)
  408. {
  409. ret = new_thread();
  410. if(ret != SGX_SUCCESS)
  411. {
  412. stop= true;
  413. }
  414. }
  415. else
  416. {
  417. stop = true;
  418. }
  419. m_need_to_wait_for_new_thread_cond.lock();
  420. if(m_need_to_wait_for_new_thread == true)
  421. {
  422. m_need_to_wait_for_new_thread = false;
  423. m_need_to_wait_for_new_thread_cond.signal();
  424. }
  425. m_need_to_wait_for_new_thread_cond.unlock();
  426. }
  427. return ret;
  428. }
  429. //The return value stand for the number of free trust thread.
  430. int CThreadPoolBindMode::garbage_collect()
  431. {
  432. int nr_free = 0;
  433. //if free list is NULL, recycle tcs.
  434. //get thread id set of current process
  435. vector<se_thread_id_t> thread_vector;
  436. get_thread_set(thread_vector);
  437. //walk through thread cache to see if there is any thread that has exited
  438. Node<se_thread_id_t, CTrustThread*>* it = m_thread_list, *pre = NULL, *tmp = NULL;
  439. while(it != NULL)
  440. {
  441. se_thread_id_t thread_id = it->key;
  442. //if the thread has exited
  443. if(FALSE == find_thread(thread_vector, thread_id))
  444. {
  445. //if the reference is not 0, there must be some wrong termination, so we can't recycle such trust thread.
  446. //return to free_tcs list
  447. if(0 == it->value->get_reference())
  448. {
  449. add_to_free_thread_vector(it->value);
  450. nr_free++;
  451. }
  452. else
  453. {
  454. //the list only record the pointer of trust thread, so we can delete it first and then erase from map.
  455. delete it->value;
  456. }
  457. tmp = it;
  458. it = it->next;
  459. if (tmp == m_thread_list)
  460. m_thread_list = it;
  461. if (pre != NULL)
  462. pre->next = it;
  463. //remove from thread cache
  464. delete tmp;
  465. }
  466. else
  467. {
  468. pre = it;
  469. it = it->next;
  470. }
  471. }
  472. return nr_free;
  473. }
  474. int CThreadPoolUnBindMode::garbage_collect()
  475. {
  476. int nr_free = 0;
  477. //walk through to free unused trust thread
  478. Node<se_thread_id_t, CTrustThread*>* it = m_thread_list, *pre = NULL, *tmp = NULL;
  479. while(it != NULL)
  480. {
  481. //if the reference is 0, then the trust thread is not in use, so return to free_tcs list
  482. if(0 == it->value->get_reference())
  483. {
  484. add_to_free_thread_vector(it->value);
  485. nr_free++;
  486. tmp = it;
  487. it = it->next;
  488. if (tmp == m_thread_list)
  489. m_thread_list = it;
  490. if (pre != NULL)
  491. pre->next = it;
  492. //remove from thread cache
  493. delete tmp;
  494. }
  495. else
  496. {
  497. pre = it;
  498. it = it->next;
  499. }
  500. }
  501. return nr_free;
  502. }