enclave_mngr.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 _SE_ENCLAVE_MNGR_H_
  32. #define _SE_ENCLAVE_MNGR_H_
  33. #include <list>
  34. #include "arch.h"
  35. #include "sgx_eid.h"
  36. #include "se_thread.h"
  37. class CEnclaveSim
  38. {
  39. public:
  40. CEnclaveSim(const secs_t* secs);
  41. virtual ~CEnclaveSim(void);
  42. // The following functions are declared virtual so that they can
  43. // be called from the tRTS instruction simulation functions.
  44. virtual sgx_enclave_id_t get_enclave_id() const;
  45. virtual secs_t* get_secs();
  46. virtual size_t get_pg_idx(const void* pgaddr) const;
  47. virtual bool add_page(const void* pgaddr, si_flags_t flags);
  48. virtual bool remove_page(const void* epc_lin_addr);
  49. virtual bool is_tcs_page(const void* pgaddr) const;
  50. private:
  51. secs_t m_secs;
  52. si_flags_t* m_flags; // memory managed by CEnclaveSim
  53. size_t m_cpages; // page count
  54. sgx_enclave_id_t m_enclave_id; // an unique Id for the enclave
  55. static uint32_t m_counter; // enclave counter
  56. sgx_enclave_id_t gen_enclave_id(void);
  57. bool validate_pg_and_flags(const void* pgaddr, si_flags_t flags);
  58. CEnclaveSim(const CEnclaveSim& es);
  59. CEnclaveSim& operator=(const CEnclaveSim& es);
  60. };
  61. class CEnclaveMngr
  62. {
  63. public:
  64. // The factory method for CEnclaveMngr.
  65. //
  66. // Note: this factory is not thread-safe.
  67. static CEnclaveMngr* get_instance();
  68. ~CEnclaveMngr();
  69. void add(CEnclaveSim* ce);
  70. void remove(CEnclaveSim* ce);
  71. CEnclaveSim* get_enclave(const sgx_enclave_id_t id);
  72. CEnclaveSim* get_enclave(const secs_t* secs);
  73. CEnclaveSim* get_enclave(const void* base_addr);
  74. private:
  75. std::list<CEnclaveSim*> m_enclave_list;
  76. // to protect the access to m_enclave_list
  77. se_mutex_t m_list_lock;
  78. CEnclaveMngr();
  79. CEnclaveMngr(const CEnclaveMngr& em);
  80. CEnclaveMngr& operator=(const CEnclaveMngr& em);
  81. };
  82. #endif