db_threading.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 = (void *) ((uint64_t) tcb & ~15);
  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. void * stack = NULL;
  73. int ret = _DkVirtualMemoryAlloc(&stack, THREAD_STACK_SIZE + ALT_STACK_SIZE,
  74. 0, PAL_PROT_READ|PAL_PROT_WRITE);
  75. if (ret < 0)
  76. return ret;
  77. void * child_stack = stack + THREAD_STACK_SIZE;
  78. PAL_HANDLE hdl = malloc(HANDLE_SIZE(thread));
  79. if (!hdl) {
  80. ret = -ENOMEM;
  81. goto err;
  82. }
  83. SET_HANDLE_TYPE(hdl, thread);
  84. // Initialize TCB at the top of the alternative stack.
  85. PAL_TCB_LINUX * tcb = child_stack + ALT_STACK_SIZE - sizeof(PAL_TCB_LINUX);
  86. tcb->common.self = &tcb->common;
  87. tcb->handle = hdl;
  88. tcb->alt_stack = child_stack; // Stack bottom
  89. tcb->callback = callback;
  90. tcb->param = (void *) param;
  91. /* align child_stack to 16 */
  92. child_stack = ALIGN_DOWN_PTR(child_stack, 16);
  93. ret = clone(pal_thread_init, child_stack,
  94. CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SYSVSEM|
  95. CLONE_THREAD|CLONE_SIGHAND|CLONE_PTRACE|
  96. CLONE_PARENT_SETTID,
  97. (void *) tcb, &hdl->thread.tid, NULL);
  98. if (IS_ERR(ret)) {
  99. ret = -PAL_ERROR_DENIED;
  100. goto err;
  101. }
  102. hdl->thread.stack = stack;
  103. *handle = hdl;
  104. return 0;
  105. err:
  106. if (stack)
  107. _DkVirtualMemoryFree(stack, THREAD_STACK_SIZE + ALT_STACK_SIZE);
  108. if (hdl)
  109. free(hdl);
  110. return ret;
  111. }
  112. int _DkThreadDelayExecution (unsigned long * duration)
  113. {
  114. struct timespec sleeptime;
  115. struct timespec remainingtime;
  116. const unsigned long VERY_LONG_TIME_IN_US = 1000000L * 60 * 60 * 24 * 365 * 128;
  117. if (*duration > VERY_LONG_TIME_IN_US) {
  118. /* avoid overflow with time_t */
  119. sleeptime.tv_sec = VERY_LONG_TIME_IN_US / 1000000;
  120. sleeptime.tv_nsec = 0;
  121. } else {
  122. sleeptime.tv_sec = *duration / 1000000;
  123. sleeptime.tv_nsec = (*duration - sleeptime.tv_sec * 1000000) * 1000;
  124. }
  125. int ret = INLINE_SYSCALL(nanosleep, 2, &sleeptime, &remainingtime);
  126. if (IS_ERR(ret)) {
  127. PAL_NUM remaining = remainingtime.tv_sec * 1000000 +
  128. remainingtime.tv_nsec / 1000;
  129. *duration -= remaining;
  130. return -PAL_ERROR_INTERRUPTED;
  131. }
  132. return 0;
  133. }
  134. /* PAL call DkThreadYieldExecution. Yield the execution
  135. of the current thread. */
  136. void _DkThreadYieldExecution (void)
  137. {
  138. INLINE_SYSCALL(sched_yield, 0);
  139. }
  140. /* _DkThreadExit for internal use: Thread exiting */
  141. noreturn void _DkThreadExit (void)
  142. {
  143. PAL_TCB_LINUX* tcb = get_tcb_linux();
  144. PAL_HANDLE handle = tcb->handle;
  145. if (tcb->alt_stack) {
  146. stack_t ss;
  147. ss.ss_sp = NULL;
  148. ss.ss_flags = SS_DISABLE;
  149. ss.ss_size = 0;
  150. // Take precautions to unset the TCB and alternative stack first.
  151. INLINE_SYSCALL(arch_prctl, 2, ARCH_SET_GS, 0);
  152. INLINE_SYSCALL(sigaltstack, 2, &ss, NULL);
  153. INLINE_SYSCALL(munmap, 2, tcb->alt_stack, ALT_STACK_SIZE);
  154. }
  155. if (handle && handle->thread.stack) {
  156. // Free the thread stack
  157. INLINE_SYSCALL(munmap, 2, handle->thread.stack, THREAD_STACK_SIZE);
  158. // After this line, needs to exit the thread immediately
  159. }
  160. INLINE_SYSCALL(exit, 1, 0);
  161. while (true) {
  162. /* nothing */
  163. }
  164. }
  165. int _DkThreadResume (PAL_HANDLE threadHandle)
  166. {
  167. int ret = INLINE_SYSCALL(tgkill, 3,
  168. linux_state.pid,
  169. threadHandle->thread.tid,
  170. SIGCONT);
  171. if (IS_ERR(ret))
  172. return -PAL_ERROR_DENIED;
  173. return 0;
  174. }
  175. struct handle_ops thread_ops = {
  176. /* nothing */
  177. };