enclave.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 _ENCLAVE_H_
  32. #define _ENCLAVE_H_
  33. #include "se_wrapper.h"
  34. #include "tcs.h"
  35. #include "create_param.h"
  36. #include "sgx_eid.h"
  37. #include "routine.h"
  38. #include "loader.h"
  39. #include "file.h"
  40. #include "uncopyable.h"
  41. #include "node.h"
  42. class CLoader;
  43. class CEnclave: private Uncopyable
  44. {
  45. public:
  46. explicit CEnclave(CLoader &ldr);
  47. ~CEnclave();
  48. void* get_start_address(){ return m_start_addr;};
  49. void * get_symbol_address(const char * const symbol);
  50. sgx_enclave_id_t get_enclave_id();
  51. uint32_t get_enclave_version();
  52. size_t get_dynamic_tcs_list_size();
  53. CTrustThreadPool * get_thread_pool() { return m_thread_pool; }
  54. uint64_t get_size() { return m_size; };
  55. sgx_status_t ecall(const int proc, const void *ocall_table, void *ms);
  56. int ocall(const unsigned int proc, const sgx_ocall_table_t *ocall_table, void *ms);
  57. void destroy();
  58. uint32_t atomic_inc_ref() { return se_atomic_inc(&m_ref); }
  59. uint32_t atomic_dec_ref() { return se_atomic_dec(&m_ref); }
  60. uint32_t get_ref() { return m_ref; }
  61. void mark_zombie() { m_zombie = true; }
  62. bool is_zombie() { return m_zombie; }
  63. sgx_status_t initialize(const se_file_t& file, const sgx_enclave_id_t enclave_id, void * const start_addr, const uint64_t enclave_size, const uint32_t tcs_policy, const uint32_t enclave_version, const uint32_t tcs_min_pool);
  64. void add_thread(tcs_t * const tcs, bool is_unallocated);
  65. void add_thread(CTrustThread * const trust_thread);
  66. const debug_enclave_info_t* get_debug_info();
  67. void set_dbg_flag(bool dbg_flag) { m_dbg_flag = dbg_flag; }
  68. bool get_dbg_flag() { return m_dbg_flag; }
  69. int set_extra_debug_info(secs_t& secs);
  70. //rdunlock is used in signal handler
  71. void rdunlock() { se_rdunlock(&m_rwlock); }
  72. void push_ocall_frame(ocall_frame_t* frame_point, CTrustThread *trust_thread);
  73. void pop_ocall_frame(CTrustThread *trust_thread);
  74. bool update_trust_thread_debug_flag(void*, uint8_t);
  75. bool update_debug_flag(uint8_t);
  76. sgx_status_t fill_tcs_mini_pool();
  77. sgx_status_t fill_tcs_mini_pool_fn();
  78. uint8_t* get_sealed_key();
  79. void set_sealed_key(uint8_t *sealed_key);
  80. private:
  81. CTrustThread * get_tcs(int ecall_cmd);
  82. void put_tcs(CTrustThread *trust_thread);
  83. sgx_status_t error_trts2urts(unsigned int trts_error);
  84. CLoader &m_loader;
  85. sgx_enclave_id_t m_enclave_id;
  86. void *m_start_addr;
  87. uint64_t m_size;
  88. se_rwlock_t m_rwlock;
  89. uint32_t m_power_event_flag;
  90. uint32_t m_ref;
  91. bool m_zombie;
  92. CTrustThreadPool *m_thread_pool;
  93. debug_enclave_info_t m_enclave_info;
  94. bool m_dbg_flag;
  95. bool m_destroyed;
  96. uint32_t m_version;
  97. sgx_ocall_table_t *m_ocall_table;
  98. pthread_t m_pthread_tid;
  99. bool m_pthread_is_valid;
  100. se_handle_t m_new_thread_event;
  101. uint8_t *m_sealed_key;
  102. };
  103. class CEnclavePool: private Uncopyable
  104. {
  105. public:
  106. static CEnclavePool *instance();
  107. int add_enclave(CEnclave *enclave);
  108. CEnclave * get_enclave(const sgx_enclave_id_t enclave_id);
  109. CEnclave * ref_enclave(const sgx_enclave_id_t enclave_id);
  110. void unref_enclave(CEnclave *enclave);
  111. int get_symbol_ordinal(const sgx_enclave_id_t enclave_id, const char *symbol, int *ordinal);
  112. CEnclave * remove_enclave(const sgx_enclave_id_t enclave_id, sgx_status_t &status);
  113. se_handle_t get_event(const void * const);
  114. void notify_debugger();
  115. private:
  116. CEnclavePool();
  117. Node<sgx_enclave_id_t, CEnclave*> *m_enclave_list;
  118. se_mutex_t m_enclave_mutex; //sync for add/get/remove enclave.
  119. static CEnclavePool m_instance;
  120. };
  121. #define AbnormalTermination() (FALSE)
  122. #endif