sgx_thread.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_internal.h"
  4. #include "sgx_internal.h"
  5. #include <pthread.h>
  6. #include <linux/futex.h>
  7. #include <asm/prctl.h>
  8. #include "sgx_enclave.h"
  9. #include "debugger/sgx_gdb.h"
  10. __thread struct pal_enclave * current_enclave;
  11. __thread void * current_tcs;
  12. __thread unsigned long debug_register;
  13. unsigned long * get_debug_register (void)
  14. {
  15. return &debug_register;
  16. }
  17. void print_debug_register (void)
  18. {
  19. SGX_DBG(DBG_E, "debug = %016x\n", debug_register);
  20. }
  21. struct tcs_map {
  22. unsigned int tid;
  23. sgx_arch_tcs_t * tcs;
  24. };
  25. static struct tcs_map * tcs_map;
  26. static int tcs_num;
  27. void create_tcs_mapper (void * tcs_base, unsigned int thread_num)
  28. {
  29. sgx_arch_tcs_t * all_tcs = tcs_base;
  30. tcs_map = malloc(sizeof(struct tcs_map) * thread_num);
  31. for (int i = 0 ; i < thread_num ; i++) {
  32. tcs_map[i].tid = 0;
  33. tcs_map[i].tcs = &all_tcs[i];
  34. }
  35. tcs_num = thread_num;
  36. }
  37. void map_tcs (unsigned int tid)
  38. {
  39. for (int i = 0 ; i < tcs_num ; i++)
  40. if (!tcs_map[i].tid) {
  41. tcs_map[i].tid = tid;
  42. current_tcs = tcs_map[i].tcs;
  43. ((struct enclave_dbginfo *) DBGINFO_ADDR)->thread_tids[i] = tid;
  44. break;
  45. }
  46. }
  47. void unmap_tcs (void)
  48. {
  49. for (int i = 0 ; i < tcs_num ; i++)
  50. if (tcs_map[i].tcs == current_tcs) {
  51. SGX_DBG(DBG_I, "unmap TCS at 0x%08lx\n", tcs_map[i].tcs);
  52. tcs_map[i].tid = 0;
  53. current_tcs = NULL;
  54. ((struct enclave_dbginfo *) DBGINFO_ADDR)->thread_tids[i] = 0;
  55. break;
  56. }
  57. }
  58. struct thread_arg {
  59. struct pal_enclave * enclave;
  60. pthread_t thread;
  61. void (*func) (void *, void *);
  62. void * arg;
  63. unsigned int * child_tid;
  64. unsigned int tid;
  65. };
  66. static void * thread_start (void * arg)
  67. {
  68. struct thread_arg * thread_arg = (struct thread_arg *) arg;
  69. struct thread_arg local_arg = *thread_arg;
  70. local_arg.tid = thread_arg->tid = INLINE_SYSCALL(gettid, 0);
  71. INLINE_SYSCALL(futex, 6, &thread_arg->tid, FUTEX_WAKE, 1, NULL, NULL, 0);
  72. current_enclave = local_arg.enclave;
  73. map_tcs(local_arg.tid);
  74. if (!current_tcs) {
  75. SGX_DBG(DBG_E, "Cannot attach to any TCS!\n");
  76. return NULL;
  77. }
  78. ecall_thread_start(local_arg.func,
  79. local_arg.arg,
  80. local_arg.child_tid,
  81. local_arg.tid);
  82. unmap_tcs();
  83. return NULL;
  84. }
  85. int clone_thread(void (*func) (void *, void *), void * arg,
  86. unsigned int * child_tid, unsigned int * tid)
  87. {
  88. int ret;
  89. struct thread_arg new_arg;
  90. new_arg.enclave = current_enclave;
  91. new_arg.func = func;
  92. new_arg.arg = arg;
  93. new_arg.child_tid = child_tid;
  94. new_arg.tid = 0;
  95. ret = pthread_create(&new_arg.thread, NULL, thread_start, &new_arg);
  96. if (ret < 0)
  97. return ret;
  98. INLINE_SYSCALL(futex, 6, &new_arg.tid, FUTEX_WAIT, 0, NULL, NULL, 0);
  99. if (tid)
  100. *tid = new_arg.tid;
  101. return ret;
  102. }