enclave.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C) 2011-2016 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. CTrustThreadPool * get_thread_pool() { return m_thread_pool; }
  52. uint64_t get_size() { return m_size; };
  53. sgx_status_t ecall(const int proc, const void *ocall_table, void *ms);
  54. int ocall(const unsigned int proc, const sgx_ocall_table_t *ocall_table, void *ms);
  55. void destroy();
  56. uint32_t atomic_inc_ref() { return se_atomic_inc(&m_ref); }
  57. uint32_t atomic_dec_ref() { return se_atomic_dec(&m_ref); }
  58. uint32_t get_ref() { return m_ref; }
  59. void mark_zombie() { m_zombie = true; }
  60. bool is_zombie() { return m_zombie; }
  61. 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);
  62. void add_thread(tcs_t * const tcs);
  63. const debug_enclave_info_t* get_debug_info();
  64. void set_dbg_flag(bool dbg_flag) { m_dbg_flag = dbg_flag; }
  65. bool get_dbg_flag() { return m_dbg_flag; }
  66. int set_extra_debug_info(secs_t& secs);
  67. //rdunlock is used in signal handler
  68. void rdunlock() { se_rdunlock(&m_rwlock); }
  69. void push_ocall_frame(ocall_frame_t* frame_point, CTrustThread *trust_thread);
  70. void pop_ocall_frame(CTrustThread *trust_thread);
  71. bool update_trust_thread_debug_flag(void*, uint8_t);
  72. bool update_debug_flag(uint8_t);
  73. private:
  74. CTrustThread * get_tcs();
  75. void put_tcs(CTrustThread *trust_thread);
  76. sgx_status_t error_trts2urts(unsigned int trts_error);
  77. CLoader &m_loader;
  78. sgx_enclave_id_t m_enclave_id;
  79. void *m_start_addr;
  80. uint64_t m_size;
  81. se_rwlock_t m_rwlock;
  82. uint32_t m_power_event_flag;
  83. uint32_t m_ref;
  84. bool m_zombie;
  85. CTrustThreadPool *m_thread_pool;
  86. debug_enclave_info_t m_enclave_info;
  87. bool m_dbg_flag;
  88. bool m_destroyed;
  89. };
  90. class CEnclavePool: private Uncopyable
  91. {
  92. public:
  93. static CEnclavePool *instance();
  94. int add_enclave(CEnclave *enclave);
  95. CEnclave * get_enclave(const sgx_enclave_id_t enclave_id);
  96. CEnclave * ref_enclave(const sgx_enclave_id_t enclave_id);
  97. void unref_enclave(CEnclave *enclave);
  98. int get_symbol_ordinal(const sgx_enclave_id_t enclave_id, const char *symbol, int *ordinal);
  99. CEnclave * remove_enclave(const sgx_enclave_id_t enclave_id, sgx_status_t &status);
  100. se_handle_t get_event(const void * const);
  101. void notify_debugger();
  102. private:
  103. CEnclavePool();
  104. Node<sgx_enclave_id_t, CEnclave*> *m_enclave_list;
  105. se_mutex_t m_enclave_mutex; //sync for add/get/remove enclave.
  106. static CEnclavePool m_instance;
  107. };
  108. #define AbnormalTermination() (FALSE)
  109. #endif