enclave_ecalls.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. int handle_ecall (long ecall_index, void * ecall_args, void * exit_target,
  13. void * untrusted_stack, void * enclave_base)
  14. {
  15. if (ecall_index < 0 || ecall_index >= ECALL_NR)
  16. return -PAL_ERROR_INVAL;
  17. if (!pal_enclave.enclave_base) {
  18. pal_enclave.enclave_base = enclave_base;
  19. pal_enclave.enclave_size = GET_ENCLAVE_TLS(enclave_size);
  20. }
  21. if (sgx_is_within_enclave(exit_target, 0))
  22. return -PAL_ERROR_DENIED;
  23. if (sgx_is_within_enclave(untrusted_stack, 0))
  24. return -PAL_ERROR_DENIED;
  25. SET_ENCLAVE_TLS(exit_target, exit_target);
  26. SET_ENCLAVE_TLS(ustack_top, untrusted_stack);
  27. SET_ENCLAVE_TLS(ustack, untrusted_stack);
  28. switch(ecall_index) {
  29. case ECALL_ENCLAVE_START: {
  30. ms_ecall_enclave_start_t * ms =
  31. (ms_ecall_enclave_start_t *) ecall_args;
  32. if (!ms) return -PAL_ERROR_INVAL;
  33. pal_linux_main(ms->ms_arguments, ms->ms_environments,
  34. ms->ms_sec_info);
  35. break;
  36. }
  37. case ECALL_THREAD_START:
  38. pal_start_thread();
  39. break;
  40. }
  41. ocall_exit();
  42. return 0;
  43. }