tcs.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifndef _TCS_H_
  32. #define _TCS_H_
  33. #include "arch.h"
  34. #include "se_wrapper.h"
  35. #include "util.h"
  36. #include "sgx_error.h"
  37. #include "se_debugger_lib.h"
  38. #include "se_lock.hpp"
  39. #include <vector>
  40. #include "node.h"
  41. using namespace std;
  42. typedef int (*bridge_fn_t)(const void*);
  43. class CEnclave;
  44. class CTrustThread: private Uncopyable
  45. {
  46. public:
  47. CTrustThread(tcs_t *tcs, CEnclave* enclave);
  48. ~CTrustThread();
  49. int get_reference() { return m_reference; }
  50. void increase_ref() { m_reference++; }
  51. void decrease_ref() { m_reference--; }
  52. tcs_t *get_tcs() { return m_tcs; }
  53. CEnclave *get_enclave() { return m_enclave; }
  54. se_handle_t get_event();
  55. void reset_ref() { m_reference = 0; }
  56. debug_tcs_info_t* get_debug_info(){return &m_tcs_info;}
  57. void push_ocall_frame(ocall_frame_t* frame_point);
  58. void pop_ocall_frame();
  59. private:
  60. tcs_t *m_tcs;
  61. CEnclave *m_enclave;
  62. int m_reference; //it will increase by 1 before ecall, and decrease after ecall.
  63. se_handle_t m_event;
  64. debug_tcs_info_t m_tcs_info;
  65. };
  66. class CTrustThreadPool: private Uncopyable
  67. {
  68. public:
  69. CTrustThreadPool(uint32_t tcs_min_pool);
  70. virtual ~CTrustThreadPool();
  71. CTrustThread * acquire_thread(int ecall_cmd);
  72. void release_thread(CTrustThread * const trust_thread);
  73. CTrustThread *add_thread(tcs_t * const tcs, CEnclave * const enclave, bool is_unallocated);
  74. CTrustThread *get_bound_thread(const tcs_t *tcs);
  75. std::vector<CTrustThread *> get_thread_list();
  76. void reset();
  77. void wake_threads();
  78. sgx_status_t new_thread();
  79. sgx_status_t fill_tcs_mini_pool();
  80. bool need_to_new_thread();
  81. bool is_dynamic_thread_exist();
  82. protected:
  83. virtual int garbage_collect() = 0;
  84. inline int find_thread(vector<se_thread_id_t> &thread_vector, se_thread_id_t thread_id);
  85. inline CTrustThread * get_free_thread();
  86. int bind_thread(const se_thread_id_t thread_id, CTrustThread * const trust_thread);
  87. void unbind_thread(const se_thread_id_t thread_id);
  88. CTrustThread * get_bound_thread(const se_thread_id_t thread_id);
  89. void add_to_free_thread_vector(CTrustThread* it);
  90. vector<CTrustThread *> m_free_thread_vector;
  91. vector<CTrustThread *> m_unallocated_threads;
  92. Node<se_thread_id_t, CTrustThread *> *m_thread_list;
  93. Mutex m_thread_mutex; //protect thread_cache list. The mutex is recursive.
  94. //Thread can operate the list when it get the mutex
  95. Mutex m_free_thread_mutex; //protect free threads.
  96. Cond m_need_to_wait_for_new_thread_cond;
  97. private:
  98. CTrustThread * _acquire_thread();
  99. CTrustThread *m_utility_thread;
  100. uint64_t m_tcs_min_pool;
  101. bool m_need_to_wait_for_new_thread;
  102. };
  103. class CThreadPoolBindMode : public CTrustThreadPool
  104. {
  105. public:
  106. CThreadPoolBindMode(uint32_t tcs_min_pool):CTrustThreadPool(tcs_min_pool){}
  107. private:
  108. virtual int garbage_collect();
  109. };
  110. class CThreadPoolUnBindMode : public CTrustThreadPool
  111. {
  112. public:
  113. CThreadPoolUnBindMode(uint32_t tcs_min_pool):CTrustThreadPool(tcs_min_pool){}
  114. private:
  115. virtual int garbage_collect();
  116. };
  117. #endif