123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #include <string.h>
- #include "thread_data.h"
- #include "global_data.h"
- #include "sgx_edger8r.h"
- #include "rts.h"
- #include "util.h"
- #include "xsave.h"
- #include "trts_internal.h"
- extern "C" sgx_status_t asm_oret(uintptr_t sp, void *ms);
- extern "C" sgx_status_t __morestack(const unsigned int index, void *ms);
- #define do_ocall __morestack
- sgx_status_t sgx_ocall(const unsigned int index, void *ms)
- {
-
- thread_data_t *thread_data = get_thread_data();
-
-
- if(thread_data->exception_flag != 0) {
- return SGX_ERROR_OCALL_NOT_ALLOWED;
- }
-
- if(static_cast<size_t>(index) >= g_dyn_entry_table.nr_ocall)
- {
- return SGX_ERROR_INVALID_FUNCTION;
- }
-
- uint8_t buffer[FXSAVE_SIZE] = {0};
- save_and_clean_xfeature_regs(buffer);
-
- sgx_status_t status = do_ocall(index, ms);
-
- restore_xfeature_regs(buffer);
-
- memset_s(buffer, FXSAVE_SIZE, 0, FXSAVE_SIZE);
- return status;
- }
- extern "C"
- uintptr_t update_ocall_lastsp(ocall_context_t* context)
- {
- thread_data_t* thread_data = get_thread_data();
- uintptr_t last_sp = 0;
- last_sp = thread_data->last_sp;
- context->pre_last_sp = last_sp;
- if (context->pre_last_sp == thread_data->stack_base_addr)
- {
- context->ocall_depth = 1;
- } else {
-
-
- ocall_context_t* context_pre = reinterpret_cast<ocall_context_t*>(context->pre_last_sp);
- context->ocall_depth = context_pre->ocall_depth + 1;
- }
- thread_data->last_sp = reinterpret_cast<uintptr_t>(context);
- return last_sp;
- }
- sgx_status_t do_oret(void *ms)
- {
- thread_data_t *thread_data = get_thread_data();
- uintptr_t last_sp = thread_data->last_sp;
- ocall_context_t *context = reinterpret_cast<ocall_context_t*>(thread_data->last_sp);
- if(0 == last_sp || last_sp <= (uintptr_t)&context)
- {
- return SGX_ERROR_UNEXPECTED;
- }
-
-
- if(last_sp > thread_data->stack_base_addr - 30 * sizeof(size_t))
- {
- return SGX_ERROR_UNEXPECTED;
- }
- if(context->ocall_flag != OCALL_FLAG)
- {
- return SGX_ERROR_UNEXPECTED;
- }
- if(context->pre_last_sp > thread_data->stack_base_addr
- || context->pre_last_sp <= (uintptr_t)context)
- {
- return SGX_ERROR_UNEXPECTED;
- }
- thread_data->last_sp = context->pre_last_sp;
- asm_oret(last_sp, ms);
-
-
- return SGX_ERROR_UNEXPECTED;
- }
|