123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #include "se_memcpy.h"
- #include "thread_data.h"
- #include "global_data.h"
- #include "rts.h"
- #include "util.h"
- #include "xsave.h"
- #include "sgx_trts.h"
- #include "sgx_spinlock.h"
- #include "global_init.h"
- #include "trts_internal.h"
- # include "linux/elf_parser.h"
- # define GET_TLS_INFO elf_tls_info
- static sgx_status_t is_ecall_allowed(uint32_t ordinal)
- {
- if(ordinal >= g_ecall_table.nr_ecall)
- {
- return SGX_ERROR_INVALID_FUNCTION;
- }
- thread_data_t *thread_data = get_thread_data();
- if(thread_data->last_sp == thread_data->stack_base_addr)
- {
-
- if (g_ecall_table.ecall_table[ordinal].is_priv)
- return SGX_ERROR_ECALL_NOT_ALLOWED;
- return SGX_SUCCESS;
- }
- ocall_context_t *context = reinterpret_cast<ocall_context_t*>(thread_data->last_sp);
- if(context->ocall_flag != OCALL_FLAG)
- {
-
- abort();
- }
- uintptr_t ocall_index = context->ocall_index;
- if(ocall_index >= g_dyn_entry_table.nr_ocall)
- {
- return SGX_ERROR_INVALID_FUNCTION;
- }
- return (g_dyn_entry_table.entry_table[ocall_index * g_ecall_table.nr_ecall + ordinal] ? SGX_SUCCESS : SGX_ERROR_ECALL_NOT_ALLOWED);
- }
- static sgx_status_t get_func_addr(uint32_t ordinal, void **addr)
- {
- sgx_status_t status = is_ecall_allowed(ordinal);
- if(SGX_SUCCESS != status)
- {
- return status;
- }
- *addr = const_cast<void *>(g_ecall_table.ecall_table[ordinal].ecall_addr);
- if(!sgx_is_within_enclave(*addr, 0))
- {
- return SGX_ERROR_UNEXPECTED;
- }
- return SGX_SUCCESS;
- }
- static volatile bool g_is_first_ecall = true;
- static volatile sgx_spinlock_t g_ife_lock = SGX_SPINLOCK_INITIALIZER;
- typedef sgx_status_t (*ecall_func_t)(void *ms);
- static sgx_status_t trts_ecall(uint32_t ordinal, void *ms)
- {
- if (unlikely(g_is_first_ecall))
- {
-
- thread_data_t *thread_data = get_thread_data();
- if (thread_data->last_sp != thread_data->stack_base_addr)
- {
- return SGX_ERROR_ECALL_NOT_ALLOWED;
- }
- sgx_spin_lock(&g_ife_lock);
- if (g_is_first_ecall)
- {
-
- init_global_object();
- g_is_first_ecall = false;
- }
- sgx_spin_unlock(&g_ife_lock);
- }
- void *addr = NULL;
- sgx_status_t status = get_func_addr(ordinal, &addr);
- if(status == SGX_SUCCESS)
- {
- ecall_func_t func = (ecall_func_t)addr;
- status = func(ms);
- }
-
- CLEAN_XFEATURE_REGS
- return status;
- }
- extern "C" void init_stack_guard();
- static sgx_status_t do_init_thread(void *tcs)
- {
- thread_data_t *thread_data = GET_PTR(thread_data_t, tcs, g_global_data.td_template.self_addr);
- memcpy_s(thread_data, SE_PAGE_SIZE, const_cast<thread_data_t *>(&g_global_data.td_template), sizeof(thread_data_t));
- thread_data->last_sp += (size_t)tcs;
- thread_data->self_addr += (size_t)tcs;
- thread_data->stack_base_addr += (size_t)tcs;
- thread_data->stack_limit_addr += (size_t)tcs;
- thread_data->first_ssa_gpr += (size_t)tcs;
- thread_data->tls_array += (size_t)tcs;
- thread_data->tls_addr += (size_t)tcs;
-
- thread_data->last_sp -= (size_t)STATIC_STACK_SIZE;
- thread_data->stack_base_addr -= (size_t)STATIC_STACK_SIZE;
- uintptr_t tls_addr = 0;
- size_t tdata_size = 0;
- if(0 != GET_TLS_INFO(&__ImageBase, &tls_addr, &tdata_size))
- {
- return SGX_ERROR_UNEXPECTED;
- }
- if(tls_addr)
- {
- memset((void *)TRIM_TO_PAGE(thread_data->tls_addr), 0, ROUND_TO_PAGE(thread_data->self_addr - thread_data->tls_addr));
- memcpy_s((void *)(thread_data->tls_addr), thread_data->self_addr - thread_data->tls_addr, (void *)tls_addr, tdata_size);
- }
- init_stack_guard();
- return SGX_SUCCESS;
- }
- sgx_status_t do_ecall(int index, void *ms, void *tcs)
- {
- sgx_status_t status = SGX_ERROR_UNEXPECTED;
- if(ENCLAVE_INIT_DONE != get_enclave_state())
- {
- return status;
- }
- thread_data_t *thread_data = get_thread_data();
- if( (NULL == thread_data) || ((thread_data->stack_base_addr == thread_data->last_sp) && (0 != g_global_data.thread_policy)))
- {
- status = do_init_thread(tcs);
- if(0 != status)
- {
- return status;
- }
- }
- status = trts_ecall(index, ms);
- return status;
- }
|