trts_ocall.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "thread_data.h"
  33. #include "global_data.h"
  34. #include "sgx_edger8r.h"
  35. #include "rts.h"
  36. #include "util.h"
  37. #include "trts_internal.h"
  38. extern "C" sgx_status_t asm_oret(uintptr_t sp, void *ms);
  39. extern "C" sgx_status_t __morestack(const unsigned int index, void *ms);
  40. #define do_ocall __morestack
  41. //
  42. // sgx_ocall
  43. // Parameters:
  44. // index - the index in the ocall table
  45. // ms - the mashalling structure
  46. // Return Value:
  47. // OCALL status
  48. //
  49. sgx_status_t sgx_ocall(const unsigned int index, void *ms)
  50. {
  51. // sgx_ocall is not allowed during exception handling
  52. thread_data_t *thread_data = get_thread_data();
  53. // we have exceptions being handled
  54. if(thread_data->exception_flag != 0) {
  55. return SGX_ERROR_OCALL_NOT_ALLOWED;
  56. }
  57. // the OCALL index should be within the ocall table range
  58. // -2, -3 and -4 should be allowed to test SDK 2.0 features
  59. if((index != 0) &&
  60. (index != (unsigned int)EDMM_TRIM) &&
  61. (index != (unsigned int)EDMM_TRIM_COMMIT) &&
  62. (index != (unsigned int)EDMM_MODPR) &&
  63. static_cast<size_t>(index) >= g_dyn_entry_table.nr_ocall)
  64. {
  65. return SGX_ERROR_INVALID_FUNCTION;
  66. }
  67. // do sgx_ocall
  68. sgx_status_t status = do_ocall(index, ms);
  69. return status;
  70. }
  71. extern "C"
  72. uintptr_t update_ocall_lastsp(ocall_context_t* context)
  73. {
  74. thread_data_t* thread_data = get_thread_data();
  75. uintptr_t last_sp = 0;
  76. last_sp = thread_data->last_sp;
  77. context->pre_last_sp = last_sp;
  78. if (context->pre_last_sp == thread_data->stack_base_addr)
  79. {
  80. context->ocall_depth = 1;
  81. } else {
  82. // thread_data->last_sp is only set when ocall or exception handling occurs
  83. // ocall is block during exception handling, so last_sp is always ocall frame here
  84. ocall_context_t* context_pre = reinterpret_cast<ocall_context_t*>(context->pre_last_sp);
  85. context->ocall_depth = context_pre->ocall_depth + 1;
  86. }
  87. thread_data->last_sp = reinterpret_cast<uintptr_t>(context);
  88. return last_sp;
  89. }
  90. sgx_status_t do_oret(void *ms)
  91. {
  92. thread_data_t *thread_data = get_thread_data();
  93. uintptr_t last_sp = thread_data->last_sp;
  94. ocall_context_t *context = reinterpret_cast<ocall_context_t*>(thread_data->last_sp);
  95. if(0 == last_sp || last_sp <= (uintptr_t)&context)
  96. {
  97. return SGX_ERROR_UNEXPECTED;
  98. }
  99. // At least 1 ecall frame and 1 ocall frame are expected on stack.
  100. // 30 is an estimated value: 8 for enclave_entry and 22 for do_ocall.
  101. if(last_sp > thread_data->stack_base_addr - 30 * sizeof(size_t))
  102. {
  103. return SGX_ERROR_UNEXPECTED;
  104. }
  105. if(context->ocall_flag != OCALL_FLAG)
  106. {
  107. return SGX_ERROR_UNEXPECTED;
  108. }
  109. if(context->pre_last_sp > thread_data->stack_base_addr
  110. || context->pre_last_sp <= (uintptr_t)context)
  111. {
  112. return SGX_ERROR_UNEXPECTED;
  113. }
  114. thread_data->last_sp = context->pre_last_sp;
  115. asm_oret(last_sp, ms);
  116. // Should not come here
  117. return SGX_ERROR_UNEXPECTED;
  118. }