enclave_ecalls.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include "pal_linux.h"
  4. #include "pal_security.h"
  5. #include "pal_internal.h"
  6. #include <api.h>
  7. #include "ecall_types.h"
  8. #define SGX_CAST(type, item) ((type) (item))
  9. void pal_linux_main (const char ** arguments, const char ** environments,
  10. struct pal_sec * sec_info);
  11. void pal_start_thread (void);
  12. extern void * enclave_base, * enclave_top;
  13. int handle_ecall (long ecall_index, void * ecall_args, void * exit_target,
  14. void * untrusted_stack, void * enclave_base_addr)
  15. {
  16. if (ecall_index < 0 || ecall_index >= ECALL_NR)
  17. return -PAL_ERROR_INVAL;
  18. if (!enclave_base) {
  19. enclave_base = enclave_base_addr;
  20. enclave_top = enclave_base_addr + GET_ENCLAVE_TLS(enclave_size);
  21. }
  22. if (sgx_is_within_enclave(exit_target, 0))
  23. return -PAL_ERROR_DENIED;
  24. if (sgx_is_within_enclave(untrusted_stack, 0))
  25. return -PAL_ERROR_DENIED;
  26. SET_ENCLAVE_TLS(exit_target, exit_target);
  27. SET_ENCLAVE_TLS(ustack_top, untrusted_stack);
  28. SET_ENCLAVE_TLS(ustack, untrusted_stack);
  29. switch(ecall_index) {
  30. case ECALL_ENCLAVE_START: {
  31. ms_ecall_enclave_start_t * ms =
  32. (ms_ecall_enclave_start_t *) ecall_args;
  33. if (!ms) return -PAL_ERROR_INVAL;
  34. pal_linux_main(ms->ms_arguments, ms->ms_environments,
  35. ms->ms_sec_info);
  36. break;
  37. }
  38. case ECALL_THREAD_START:
  39. pal_start_thread();
  40. break;
  41. }
  42. ocall_exit();
  43. return 0;
  44. }