driver_api.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include <string.h>
  32. #include "se_wrapper.h"
  33. #include "util.h"
  34. #include "enclave.h"
  35. #include "enclave_mngr.h"
  36. #include "sgxsim.h"
  37. #include "driver_api.h"
  38. #define TO_STR(e) #e
  39. #define BUG_ON(cond, rv) do { \
  40. if (cond) { \
  41. SE_TRACE(SE_TRACE_DEBUG, "*** BUG ***: %s\n", TO_STR(cond)); \
  42. return rv; \
  43. } \
  44. } while (0)
  45. /* Allocate linear address space. */
  46. int create_enclave(secs_t *secs,
  47. sgx_enclave_id_t *enclave_id,
  48. void **start_addr)
  49. {
  50. CEnclaveSim *ce;
  51. sec_info_t sinfo;
  52. page_info_t pinfo;
  53. BUG_ON(secs == NULL, SGX_ERROR_UNEXPECTED);
  54. BUG_ON(enclave_id == NULL, SGX_ERROR_UNEXPECTED);
  55. BUG_ON(start_addr == NULL, SGX_ERROR_UNEXPECTED);
  56. memset(&sinfo, 0, sizeof(sinfo));
  57. sinfo.flags = SI_FLAGS_SECS;
  58. memset(&pinfo, 0, sizeof(pinfo));
  59. pinfo.src_page = secs;
  60. pinfo.sec_info = &sinfo;
  61. ce = reinterpret_cast<CEnclaveSim*>(DoECREATE_SW(&pinfo));
  62. if (ce == NULL) {
  63. SE_TRACE(SE_TRACE_DEBUG, "out of memory.\n");
  64. return SGX_ERROR_OUT_OF_MEMORY;
  65. }
  66. *start_addr = ce->get_secs()->base;
  67. *enclave_id = ce->get_enclave_id();
  68. secs->base = *start_addr;
  69. return SGX_SUCCESS;
  70. }
  71. int add_enclave_page(sgx_enclave_id_t enclave_id,
  72. void *source,
  73. size_t offset,
  74. const sec_info_t &secinfo,
  75. uint32_t attr)
  76. {
  77. sec_info_t sinfo;
  78. page_info_t pinfo;
  79. CEnclaveMngr *mngr;
  80. CEnclaveSim *ce;
  81. UNUSED(attr);
  82. mngr = CEnclaveMngr::get_instance();
  83. ce = mngr->get_enclave(enclave_id);
  84. if (ce == NULL) {
  85. SE_TRACE(SE_TRACE_DEBUG,
  86. "enclave (id = %llu) not found.\n",
  87. enclave_id);
  88. return SGX_ERROR_INVALID_ENCLAVE_ID;
  89. }
  90. memset(&sinfo, 0, sizeof(sec_info_t));
  91. sinfo.flags = secinfo.flags;
  92. if(memcmp(&sinfo, &secinfo, sizeof(sec_info_t)))
  93. return SGX_ERROR_UNEXPECTED;
  94. memset(&pinfo, 0, sizeof(pinfo));
  95. pinfo.secs = ce->get_secs();
  96. pinfo.lin_addr = (char*)ce->get_secs()->base + offset;
  97. pinfo.src_page = source;
  98. pinfo.sec_info = &sinfo;
  99. /* Passing NULL here when there is no EPC mgmt. */
  100. return (int)DoEADD_SW(&pinfo, GET_PTR(void, ce->get_secs()->base, offset));
  101. }
  102. int init_enclave(sgx_enclave_id_t enclave_id,
  103. enclave_css_t *enclave_css,
  104. token_t *launch)
  105. {
  106. CEnclaveMngr* mngr = CEnclaveMngr::get_instance();
  107. CEnclaveSim* ce = mngr->get_enclave(enclave_id);
  108. if (ce == NULL) {
  109. SE_TRACE(SE_TRACE_DEBUG,
  110. "enclave (id = %llu) not found.\n",
  111. enclave_id);
  112. return SGX_ERROR_INVALID_ENCLAVE_ID;
  113. }
  114. return (int)DoEINIT_SW(ce->get_secs(), enclave_css, launch);
  115. }
  116. int destroy_enclave(sgx_enclave_id_t enclave_id)
  117. {
  118. CEnclaveMngr* mngr = CEnclaveMngr::get_instance();
  119. CEnclaveSim* ce = mngr->get_enclave(enclave_id);
  120. if (ce == NULL) {
  121. SE_TRACE(SE_TRACE_DEBUG,
  122. "enclave (id = %llu) not found.\n",
  123. enclave_id);
  124. return SGX_ERROR_INVALID_ENCLAVE_ID;
  125. }
  126. /* In simulation mode, all allocated pages will be freed upon the later
  127. `delete ce'. Just remove the first page here. */
  128. DoEREMOVE_SW(0, ce->get_secs()->base);
  129. mngr->remove(ce);
  130. delete ce;
  131. return SGX_SUCCESS;
  132. }