123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include <string.h>
- #include "thread_data.h"
- #include "global_data.h"
- #include "util.h"
- #include "xsave.h"
- #include "sgx_trts.h"
- #include "init_optimized_lib.h"
- #include "trts_internal.h"
- # include "linux/elf_parser.h"
- #include "rts.h"
- uint64_t g_cpu_feature_indicator = 0;
- const volatile global_data_t g_global_data = {1, 2, 3, 4, 0,
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0}}};
- uint32_t g_enclave_state = ENCLAVE_INIT_NOT_STARTED;
- extern "C" {
- uintptr_t __stack_chk_guard = 0;
- #define __weak_alias(alias,sym) \
- __asm__(".weak " __STRING(alias) " ; " \
- __STRING(alias) " = " __STRING(sym))
- __weak_alias(__intel_security_cookie, __stack_chk_guard);
- }
- extern "C" int init_enclave(void *enclave_base, void *ms)
- {
- if(enclave_base == NULL || ms == NULL)
- {
- return -1;
- }
-
- if(0 != relocate_enclave(enclave_base))
- {
- return -1;
- }
-
-
- cpu_sdk_info_t *info = (cpu_sdk_info_t *)ms;
- if(!sgx_is_outside_enclave(info, sizeof(cpu_sdk_info_t)))
- {
- return -1;
- }
- const sdk_version_t sdk_version = info->version;
- const uint64_t cpu_features = info->cpu_features;
-
- if (sdk_version != SDK_VERSION_1_5)
- return -1;
-
- uint64_t xfrm = get_xfeature_state();
-
- if(0 != init_optimized_libs(cpu_features, xfrm))
- {
- CLEAN_XFEATURE_REGS
- return -1;
- }
- if(SGX_SUCCESS != sgx_read_rand((unsigned char*)&__stack_chk_guard,
- sizeof(__stack_chk_guard)))
- {
- CLEAN_XFEATURE_REGS
- return -1;
- }
-
- CLEAN_XFEATURE_REGS
- return 0;
- }
- sgx_status_t do_init_enclave(void *ms)
- {
- void *enclave_base = get_enclave_base();
- if(ENCLAVE_INIT_NOT_STARTED != lock_enclave())
- {
- return SGX_ERROR_UNEXPECTED;
- }
- if(0 != init_enclave(enclave_base, ms))
- {
- return SGX_ERROR_UNEXPECTED;
- }
- memset(GET_PTR(void, enclave_base, g_global_data.heap_offset), 0, g_global_data.heap_size);
- g_enclave_state = ENCLAVE_INIT_DONE;
- return SGX_SUCCESS;
- }
|