db_threading.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_threading.c
  17. *
  18. * This file contain APIs to create, exit and yield a thread.
  19. */
  20. #include "pal_defs.h"
  21. #include "pal_linux_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_linux.h"
  25. #include "pal_error.h"
  26. #include "pal_debug.h"
  27. #include "api.h"
  28. #include <errno.h>
  29. #include <linux/signal.h>
  30. #include <linux/mman.h>
  31. #include <linux/sched.h>
  32. #include <linux/types.h>
  33. #include <linux/wait.h>
  34. #if defined(__i386__)
  35. #include <asm/ldt.h>
  36. #else
  37. #include <asm/prctl.h>
  38. #endif
  39. /*
  40. * pal_thread_init(): An initialization wrapper of a newly-created thread (including
  41. * the first thread). This function accepts a TCB pointer to be set to the GS register
  42. * of the thread. The rest of the TCB is used as the alternative stack for signal
  43. * handling.
  44. */
  45. int pal_thread_init (void * tcbptr)
  46. {
  47. PAL_TCB * tcb = tcbptr;
  48. int ret;
  49. ret = INLINE_SYSCALL(arch_prctl, 2, ARCH_SET_GS, tcb);
  50. if (IS_ERR(ret))
  51. return -ERRNO(ret);
  52. if (tcb->alt_stack) {
  53. // Align stack to 16 bytes
  54. void * alt_stack_top = (void *) ((uint64_t) tcb & ~15);
  55. assert(alt_stack_top > tcb->alt_stack);
  56. stack_t ss;
  57. ss.ss_sp = alt_stack_top;
  58. ss.ss_flags = 0;
  59. ss.ss_size = alt_stack_top - tcb->alt_stack;
  60. ret = INLINE_SYSCALL(sigaltstack, 2, &ss, NULL);
  61. if (IS_ERR(ret))
  62. return -ERRNO(ret);
  63. }
  64. if (tcb->callback)
  65. return (*tcb->callback) (tcb->param);
  66. return 0;
  67. }
  68. /* _DkThreadCreate for internal use. Create an internal thread
  69. inside the current process. The arguments callback and param
  70. specify the starting function and parameters */
  71. int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
  72. const void * param)
  73. {
  74. void * stack = NULL;
  75. int ret = _DkVirtualMemoryAlloc(&stack, THREAD_STACK_SIZE + ALT_STACK_SIZE,
  76. 0, PAL_PROT_READ|PAL_PROT_WRITE);
  77. if (ret < 0)
  78. return ret;
  79. void * child_stack = stack + THREAD_STACK_SIZE;
  80. PAL_HANDLE hdl = malloc(HANDLE_SIZE(thread));
  81. if (!hdl) {
  82. ret = -ENOMEM;
  83. goto err;
  84. }
  85. SET_HANDLE_TYPE(hdl, thread);
  86. // Initialize TCB at the top of the alternative stack.
  87. PAL_TCB * tcb = child_stack + ALT_STACK_SIZE - sizeof(PAL_TCB);
  88. tcb->self = tcb;
  89. tcb->handle = hdl;
  90. tcb->alt_stack = child_stack; // Stack bottom
  91. tcb->callback = callback;
  92. tcb->param = (void *) param;
  93. /* align child_stack to 16 */
  94. child_stack = ALIGN_DOWN_PTR(child_stack, 16);
  95. ret = clone(pal_thread_init, child_stack,
  96. CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SYSVSEM|
  97. CLONE_THREAD|CLONE_SIGHAND|CLONE_PTRACE|
  98. CLONE_PARENT_SETTID,
  99. (void *) tcb, &hdl->thread.tid, NULL);
  100. if (IS_ERR(ret)) {
  101. ret = -PAL_ERROR_DENIED;
  102. goto err;
  103. }
  104. hdl->thread.stack = stack;
  105. *handle = hdl;
  106. return 0;
  107. err:
  108. if (stack)
  109. _DkVirtualMemoryFree(stack, THREAD_STACK_SIZE + ALT_STACK_SIZE);
  110. if (hdl)
  111. free(hdl);
  112. return ret;
  113. }
  114. int _DkThreadDelayExecution (unsigned long * duration)
  115. {
  116. struct timespec sleeptime;
  117. struct timespec remainingtime;
  118. const long VERY_LONG_TIME_IN_US = 1000000L * 60 * 60 * 24 * 365 * 128;
  119. if (*duration > VERY_LONG_TIME_IN_US) {
  120. /* avoid overflow with time_t */
  121. sleeptime.tv_sec = VERY_LONG_TIME_IN_US / 1000000;
  122. sleeptime.tv_nsec = 0;
  123. } else {
  124. sleeptime.tv_sec = *duration / 1000000;
  125. sleeptime.tv_nsec = (*duration - sleeptime.tv_sec * 1000000) * 1000;
  126. }
  127. int ret = INLINE_SYSCALL(nanosleep, 2, &sleeptime, &remainingtime);
  128. if (IS_ERR(ret)) {
  129. PAL_NUM remaining = remainingtime.tv_sec * 1000000 +
  130. remainingtime.tv_nsec / 1000;
  131. *duration -= remaining;
  132. return -PAL_ERROR_INTERRUPTED;
  133. }
  134. return 0;
  135. }
  136. /* PAL call DkThreadYieldExecution. Yield the execution
  137. of the current thread. */
  138. void _DkThreadYieldExecution (void)
  139. {
  140. INLINE_SYSCALL(sched_yield, 0);
  141. }
  142. /* _DkThreadExit for internal use: Thread exiting */
  143. void _DkThreadExit (void)
  144. {
  145. PAL_TCB* tcb = get_tcb();
  146. PAL_HANDLE handle = tcb->handle;
  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. INLINE_SYSCALL(munmap, 2, tcb->alt_stack, ALT_STACK_SIZE);
  156. }
  157. if (handle && handle->thread.stack) {
  158. // Free the thread stack
  159. INLINE_SYSCALL(munmap, 2, handle->thread.stack, THREAD_STACK_SIZE);
  160. // After this line, needs to exit the thread immediately
  161. }
  162. INLINE_SYSCALL(exit, 1, 0);
  163. }
  164. int _DkThreadResume (PAL_HANDLE threadHandle)
  165. {
  166. int ret = INLINE_SYSCALL(tgkill, 3,
  167. linux_state.pid,
  168. threadHandle->thread.tid,
  169. SIGCONT);
  170. if (IS_ERR(ret))
  171. return -PAL_ERROR_DENIED;
  172. return 0;
  173. }
  174. struct handle_ops thread_ops = {
  175. /* nothing */
  176. };