db_threading.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_error.h"
  24. #include "pal_debug.h"
  25. #include "api.h"
  26. #include <errno.h>
  27. #include <linux/signal.h>
  28. #include <linux/mman.h>
  29. #include <linux/sched.h>
  30. #include <linux/types.h>
  31. #include <linux/wait.h>
  32. #if defined(__i386__)
  33. #include <asm/ldt.h>
  34. #else
  35. #include <asm/prctl.h>
  36. #endif
  37. /*
  38. * pal_thread_init(): An initialization wrapper of a newly-created thread (including
  39. * the first thread). This function accepts a TCB pointer to be set to the GS register
  40. * of the thread. The rest of the TCB is used as the alternative stack for signal
  41. * handling.
  42. */
  43. int pal_thread_init (void * tcbptr)
  44. {
  45. PAL_TCB_LINUX * tcb = tcbptr;
  46. int ret;
  47. ret = INLINE_SYSCALL(arch_prctl, 2, ARCH_SET_GS, tcb);
  48. if (IS_ERR(ret))
  49. return -ERRNO(ret);
  50. if (tcb->alt_stack) {
  51. // Align stack to 16 bytes
  52. void* alt_stack_top = ALIGN_DOWN_PTR(tcb, 16);
  53. assert(alt_stack_top > tcb->alt_stack);
  54. stack_t ss;
  55. ss.ss_sp = alt_stack_top;
  56. ss.ss_flags = 0;
  57. ss.ss_size = alt_stack_top - tcb->alt_stack;
  58. ret = INLINE_SYSCALL(sigaltstack, 2, &ss, NULL);
  59. if (IS_ERR(ret))
  60. return -ERRNO(ret);
  61. }
  62. if (tcb->callback)
  63. return (*tcb->callback) (tcb->param);
  64. return 0;
  65. }
  66. /* _DkThreadCreate for internal use. Create an internal thread
  67. inside the current process. The arguments callback and param
  68. specify the starting function and parameters */
  69. int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
  70. const void * param)
  71. {
  72. int ret = 0;
  73. PAL_HANDLE hdl = NULL;
  74. void * stack = malloc(THREAD_STACK_SIZE + ALT_STACK_SIZE);
  75. if (!stack) {
  76. ret = -ENOMEM;
  77. goto err;
  78. }
  79. memset(stack, 0, THREAD_STACK_SIZE + ALT_STACK_SIZE);
  80. void * child_stack = stack + THREAD_STACK_SIZE;
  81. hdl = malloc(HANDLE_SIZE(thread));
  82. if (!hdl) {
  83. ret = -ENOMEM;
  84. goto err;
  85. }
  86. SET_HANDLE_TYPE(hdl, thread);
  87. // Initialize TCB at the top of the alternative stack.
  88. PAL_TCB_LINUX * tcb = child_stack + ALT_STACK_SIZE - sizeof(PAL_TCB_LINUX);
  89. tcb->common.self = &tcb->common;
  90. tcb->handle = hdl;
  91. tcb->alt_stack = child_stack; // Stack bottom
  92. tcb->callback = callback;
  93. tcb->param = (void *) param;
  94. /* align child_stack to 16 */
  95. child_stack = ALIGN_DOWN_PTR(child_stack, 16);
  96. ret = clone(pal_thread_init, child_stack,
  97. CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SYSVSEM|
  98. CLONE_THREAD|CLONE_SIGHAND|CLONE_PTRACE|
  99. CLONE_PARENT_SETTID,
  100. (void *) tcb, &hdl->thread.tid, NULL);
  101. if (IS_ERR(ret)) {
  102. ret = -PAL_ERROR_DENIED;
  103. goto err;
  104. }
  105. hdl->thread.stack = stack;
  106. *handle = hdl;
  107. return 0;
  108. err:
  109. free(stack);
  110. free(hdl);
  111. return ret;
  112. }
  113. int _DkThreadDelayExecution (unsigned long * duration)
  114. {
  115. struct timespec sleeptime;
  116. struct timespec remainingtime;
  117. const unsigned long VERY_LONG_TIME_IN_US = 1000000L * 60 * 60 * 24 * 365 * 128;
  118. if (*duration > VERY_LONG_TIME_IN_US) {
  119. /* avoid overflow with time_t */
  120. sleeptime.tv_sec = VERY_LONG_TIME_IN_US / 1000000;
  121. sleeptime.tv_nsec = 0;
  122. } else {
  123. sleeptime.tv_sec = *duration / 1000000;
  124. sleeptime.tv_nsec = (*duration - sleeptime.tv_sec * 1000000) * 1000;
  125. }
  126. int ret = INLINE_SYSCALL(nanosleep, 2, &sleeptime, &remainingtime);
  127. if (IS_ERR(ret)) {
  128. PAL_NUM remaining = remainingtime.tv_sec * 1000000 +
  129. remainingtime.tv_nsec / 1000;
  130. *duration -= remaining;
  131. return -PAL_ERROR_INTERRUPTED;
  132. }
  133. return 0;
  134. }
  135. /* PAL call DkThreadYieldExecution. Yield the execution
  136. of the current thread. */
  137. void _DkThreadYieldExecution (void)
  138. {
  139. INLINE_SYSCALL(sched_yield, 0);
  140. }
  141. /* _DkThreadExit for internal use: Thread exiting */
  142. noreturn void _DkThreadExit (void)
  143. {
  144. PAL_TCB_LINUX* tcb = get_tcb_linux();
  145. PAL_HANDLE handle = tcb->handle;
  146. block_async_signals(true);
  147. if (tcb->alt_stack) {
  148. stack_t ss;
  149. ss.ss_sp = NULL;
  150. ss.ss_flags = SS_DISABLE;
  151. ss.ss_size = 0;
  152. // Take precautions to unset the TCB and alternative stack first.
  153. INLINE_SYSCALL(arch_prctl, 2, ARCH_SET_GS, 0);
  154. INLINE_SYSCALL(sigaltstack, 2, &ss, NULL);
  155. }
  156. if (handle) {
  157. // Free the thread stack
  158. free(handle->thread.stack);
  159. // After this line, needs to exit the thread immediately
  160. }
  161. INLINE_SYSCALL(exit, 1, 0);
  162. while (true) {
  163. /* nothing */
  164. }
  165. }
  166. int _DkThreadResume (PAL_HANDLE threadHandle)
  167. {
  168. int ret = INLINE_SYSCALL(tgkill, 3,
  169. linux_state.pid,
  170. threadHandle->thread.tid,
  171. SIGCONT);
  172. if (IS_ERR(ret))
  173. return -PAL_ERROR_DENIED;
  174. return 0;
  175. }
  176. struct handle_ops thread_ops = {
  177. /* nothing */
  178. };