enclave_ecalls.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "pal_linux.h"
  2. #include "pal_security.h"
  3. #include "pal_internal.h"
  4. #include <api.h>
  5. #include "ecall_types.h"
  6. #define SGX_CAST(type, item) ((type)(item))
  7. extern void * enclave_base, * enclave_top;
  8. static struct atomic_int enclave_start_called = ATOMIC_INIT(0);
  9. /*
  10. * Called from enclave_entry.S to execute ecalls.
  11. *
  12. * During normal operation handle_ecall will not return. The exception is that
  13. * it will return if invalid parameters are passed. In this case
  14. * enclave_entry.S will go into an endless loop since a clean return to urts is
  15. * not easy in all cases.
  16. *
  17. * Parameters:
  18. *
  19. * ecall_index:
  20. * Number of requested ecall. Untrusted.
  21. *
  22. * ecall_args:
  23. * Pointer to arguments for requested ecall. Untrusted.
  24. *
  25. * exit_target:
  26. * Address to return to after EEXIT. Untrusted.
  27. *
  28. * untrusted_stack:
  29. * Address to urts stack. Restored before EEXIT and used for ocall
  30. * arguments. Untrusted.
  31. *
  32. * enclave_base_addr:
  33. * Base address of enclave. Calculated dynamically in enclave_entry.S.
  34. * Trusted.
  35. */
  36. void handle_ecall (long ecall_index, void * ecall_args, void * exit_target,
  37. void * untrusted_stack, void * enclave_base_addr)
  38. {
  39. if (ecall_index < 0 || ecall_index >= ECALL_NR)
  40. return;
  41. if (!enclave_top) {
  42. enclave_base = enclave_base_addr;
  43. enclave_top = enclave_base_addr + GET_ENCLAVE_TLS(enclave_size);
  44. }
  45. SET_ENCLAVE_TLS(exit_target, exit_target);
  46. SET_ENCLAVE_TLS(ustack_top, untrusted_stack);
  47. SET_ENCLAVE_TLS(ustack, untrusted_stack);
  48. SET_ENCLAVE_TLS(clear_child_tid, NULL);
  49. SET_ENCLAVE_TLS(untrusted_area_cache.in_use, 0UL);
  50. if (atomic_cmpxchg(&enclave_start_called, 0, 1) == 0) {
  51. // ENCLAVE_START not yet called, so only valid ecall is ENCLAVE_START.
  52. if (ecall_index != ECALL_ENCLAVE_START) {
  53. // To keep things simple, we treat an invalid ecall_index like an
  54. // unsuccessful call to ENCLAVE_START.
  55. return;
  56. }
  57. ms_ecall_enclave_start_t * ms =
  58. (ms_ecall_enclave_start_t *) ecall_args;
  59. if (!ms || !sgx_is_completely_outside_enclave(ms, sizeof(*ms))) {
  60. return;
  61. }
  62. /* xsave size must be initialized early */
  63. init_xsave_size(ms->ms_sec_info->enclave_attributes.xfrm);
  64. /* pal_linux_main is responsible to check the passed arguments */
  65. pal_linux_main(ms->ms_args, ms->ms_args_size,
  66. ms->ms_env, ms->ms_env_size,
  67. ms->ms_sec_info);
  68. } else {
  69. // ENCLAVE_START already called (maybe successfully, maybe not), so
  70. // only valid ecall is THREAD_START.
  71. if (ecall_index != ECALL_THREAD_START) {
  72. return;
  73. }
  74. // Only allow THREAD_START after successful enclave initialization.
  75. if (!(pal_enclave_state.enclave_flags & PAL_ENCLAVE_INITIALIZED)) {
  76. return;
  77. }
  78. pal_start_thread();
  79. }
  80. // pal_linux_main and pal_start_thread should never return.
  81. }