tcs.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #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();
  70. virtual ~CTrustThreadPool();
  71. CTrustThread * acquire_thread();
  72. void release_thread(CTrustThread * const trust_thread);
  73. CTrustThread *add_thread(tcs_t * const tcs, CEnclave * const enclave);
  74. CTrustThread *get_bound_thread(const tcs_t *tcs);
  75. void reset();
  76. void wake_threads();
  77. protected:
  78. virtual int garbage_collect() = 0;
  79. inline int find_thread(vector<se_thread_id_t> &thread_vector, se_thread_id_t thread_id);
  80. inline CTrustThread * get_free_thread();
  81. int bind_thread(const se_thread_id_t thread_id, CTrustThread * const trust_thread);
  82. CTrustThread * get_bound_thread(const se_thread_id_t thread_id);
  83. vector<CTrustThread *> m_free_thread_vector;
  84. Node<se_thread_id_t, CTrustThread *> *m_thread_list;
  85. Mutex m_thread_mutex; //protect both thread_cache list and fress tcs list. The mutex is recursive.
  86. //Thread can operate the two list when it get the mutex
  87. private:
  88. CTrustThread * _acquire_thread();
  89. };
  90. class CThreadPoolBindMode : public CTrustThreadPool
  91. {
  92. public:
  93. CThreadPoolBindMode():CTrustThreadPool(){}
  94. private:
  95. virtual int garbage_collect();
  96. };
  97. class CThreadPoolUnBindMode : public CTrustThreadPool
  98. {
  99. public:
  100. CThreadPoolUnBindMode():CTrustThreadPool(){}
  101. private:
  102. virtual int garbage_collect();
  103. };
  104. #endif