db_threading.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_threading.c
  15. *
  16. * This file contain APIs to create, exit and yield a thread.
  17. */
  18. #include "pal_defs.h"
  19. #include "pal_linux_defs.h"
  20. #include "pal.h"
  21. #include "pal_internal.h"
  22. #include "pal_linux.h"
  23. #include "pal_linux_error.h"
  24. #include "pal_error.h"
  25. #include "pal_debug.h"
  26. #include "api.h"
  27. #include "ecall_types.h"
  28. #include <linux/signal.h>
  29. #include <linux/mman.h>
  30. #include <linux/sched.h>
  31. #include <linux/types.h>
  32. #include <linux/wait.h>
  33. #include <list.h>
  34. static PAL_LOCK thread_list_lock = LOCK_INIT;
  35. DEFINE_LISTP(pal_handle_thread);
  36. static LISTP_TYPE(pal_handle_thread) thread_list = LISTP_INIT;
  37. struct thread_param {
  38. int (*callback) (void *);
  39. const void * param;
  40. };
  41. extern void * enclave_base;
  42. /*
  43. * We do not currently handle tid counter wrap-around, and could, in
  44. * principle, end up with two threads with the same ID. This is ok, as strict
  45. * uniqueness is not required; the tid is only used for debugging. We could
  46. * ensure uniqueness if needed in the future
  47. */
  48. static PAL_IDX pal_assign_tid(void)
  49. {
  50. static struct atomic_int tid = ATOMIC_INIT(0);
  51. return _atomic_add(1, &tid);
  52. }
  53. void pal_start_thread (void)
  54. {
  55. struct pal_handle_thread *new_thread = NULL, *tmp;
  56. _DkInternalLock(&thread_list_lock);
  57. LISTP_FOR_EACH_ENTRY(tmp, &thread_list, list)
  58. if (!tmp->tcs) {
  59. new_thread = tmp;
  60. new_thread->tid = pal_assign_tid();
  61. new_thread->tcs =
  62. enclave_base + GET_ENCLAVE_TLS(tcs_offset);
  63. break;
  64. }
  65. _DkInternalUnlock(&thread_list_lock);
  66. if (!new_thread)
  67. return;
  68. struct thread_param * thread_param =
  69. (struct thread_param *) new_thread->param;
  70. int (*callback) (void *) = thread_param->callback;
  71. const void * param = thread_param->param;
  72. free(thread_param);
  73. new_thread->param = NULL;
  74. SET_ENCLAVE_TLS(thread, new_thread);
  75. SET_ENCLAVE_TLS(ready_for_exceptions, 1UL);
  76. PAL_TCB* pal_tcb = pal_get_tcb();
  77. memset(&pal_tcb->libos_tcb, 0, sizeof(pal_tcb->libos_tcb));
  78. callback((void *) param);
  79. _DkThreadExit(/*clear_child_tid=*/NULL);
  80. }
  81. /* _DkThreadCreate for internal use. Create an internal thread
  82. inside the current process. The arguments callback and param
  83. specify the starting function and parameters */
  84. int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
  85. const void * param)
  86. {
  87. PAL_HANDLE new_thread = malloc(HANDLE_SIZE(thread));
  88. SET_HANDLE_TYPE(new_thread, thread);
  89. /*
  90. * tid will be filled later by pal_start_thread()
  91. * tid is cleared to avoid random value here.
  92. */
  93. new_thread->thread.tid = 0;
  94. new_thread->thread.tcs = NULL;
  95. INIT_LIST_HEAD(&new_thread->thread, list);
  96. struct thread_param * thread_param = malloc(sizeof(struct thread_param));
  97. thread_param->callback = callback;
  98. thread_param->param = param;
  99. new_thread->thread.param = (void *) thread_param;
  100. _DkInternalLock(&thread_list_lock);
  101. LISTP_ADD_TAIL(&new_thread->thread, &thread_list, list);
  102. _DkInternalUnlock(&thread_list_lock);
  103. int ret = ocall_clone_thread();
  104. if (IS_ERR(ret))
  105. return unix_to_pal_error(ERRNO(ret));
  106. *handle = new_thread;
  107. return 0;
  108. }
  109. int _DkThreadDelayExecution (unsigned long * duration)
  110. {
  111. int ret = ocall_sleep(duration);
  112. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  113. }
  114. /* PAL call DkThreadYieldExecution. Yield the execution
  115. of the current thread. */
  116. void _DkThreadYieldExecution (void)
  117. {
  118. ocall_sleep(NULL);
  119. }
  120. /* _DkThreadExit for internal use: Thread exiting */
  121. noreturn void _DkThreadExit(int* clear_child_tid) {
  122. struct pal_handle_thread* exiting_thread = GET_ENCLAVE_TLS(thread);
  123. /* thread is ready to exit, must inform LibOS by erasing clear_child_tid;
  124. * note that we don't do it now (because this thread still occupies SGX
  125. * TCS slot) but during handle_thread_reset in assembly code */
  126. SET_ENCLAVE_TLS(clear_child_tid, clear_child_tid);
  127. static_assert(sizeof(*clear_child_tid) == 4, "unexpected clear_child_tid size");
  128. /* main thread is not part of the thread_list */
  129. if(exiting_thread != &pal_control.first_thread->thread) {
  130. _DkInternalLock(&thread_list_lock);
  131. LISTP_DEL(exiting_thread, &thread_list, list);
  132. _DkInternalUnlock(&thread_list_lock);
  133. }
  134. ocall_exit(0, /*is_exitgroup=*/false);
  135. }
  136. int _DkThreadResume (PAL_HANDLE threadHandle)
  137. {
  138. int ret = ocall_resume_thread(threadHandle->thread.tcs);
  139. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  140. }
  141. struct handle_ops thread_ops = {
  142. /* nothing */
  143. };