db_threading.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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. /* default size of a new thread */
  35. #define PAL_THREAD_STACK_SIZE allocsize
  36. /* _DkThreadCreate for internal use. Create an internal thread
  37. inside the current process. The arguments callback and param
  38. specify the starting function and parameters */
  39. int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
  40. const void * param, int flags)
  41. {
  42. void * child_stack = NULL;
  43. if (_DkVirtualMemoryAlloc(&child_stack,
  44. PAL_THREAD_STACK_SIZE, 0,
  45. PAL_PROT_READ|PAL_PROT_WRITE) < 0)
  46. return -PAL_ERROR_NOMEM;
  47. /* move child_stack to the top of stack. */
  48. child_stack += PAL_THREAD_STACK_SIZE;
  49. /* align child_stack to 16 */
  50. child_stack = (void *) ((uintptr_t) child_stack & ~16);
  51. flags &= PAL_THREAD_MASK;
  52. int tid = 0;
  53. int ret = __clone(callback, child_stack,
  54. CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SYSVSEM|
  55. CLONE_THREAD|CLONE_SIGHAND|CLONE_PTRACE|
  56. CLONE_PARENT_SETTID|flags,
  57. param, &tid, NULL);
  58. if (IS_ERR(ret))
  59. return -PAL_ERROR_DENIED;
  60. PAL_HANDLE hdl = malloc(HANDLE_SIZE(thread));
  61. SET_HANDLE_TYPE(hdl, thread);
  62. hdl->thread.tid = tid;
  63. *handle = hdl;
  64. /* _DkThreadAdd(tid); */
  65. return 0;
  66. }
  67. #if defined(__i386__)
  68. #include <asm/ldt.h>
  69. #else
  70. #include <asm/prctl.h>
  71. #endif
  72. /* PAL call DkThreadPrivate: set up the thread private area for the
  73. current thread. if addr is 0, return the current thread private
  74. area. */
  75. void * _DkThreadPrivate (void * addr)
  76. {
  77. int ret = 0;
  78. if ((void *) addr == 0) {
  79. #if defined(__i386__)
  80. struct user_desc u_info;
  81. ret = INLINE_SYSCALL(get_thread_area, 1, &u_info);
  82. if (IS_ERR(ret))
  83. return NULL;
  84. void * thread_area = u_info->base_addr;
  85. #else
  86. unsigned long ret_addr;
  87. ret = INLINE_SYSCALL(arch_prctl, 2, ARCH_GET_FS, &ret_addr);
  88. if (IS_ERR(ret))
  89. return NULL;
  90. void * thread_area = (void *) ret_addr;
  91. #endif
  92. return thread_area;
  93. } else {
  94. #if defined(__i386__)
  95. struct user_desc u_info;
  96. ret = INLINE_SYSCALL(get_thread_area, 1, &u_info);
  97. if (IS_ERR(ret))
  98. return NULL;
  99. u_info->entry_number = -1;
  100. u_info->base_addr = (unsigned int) addr;
  101. ret = INLINE_SYSCALL(set_thread_area, 1, &u_info);
  102. #else
  103. ret = INLINE_SYSCALL(arch_prctl, 2, ARCH_SET_FS, addr);
  104. #endif
  105. if (IS_ERR(ret))
  106. return NULL;
  107. return addr;
  108. }
  109. }
  110. int _DkThreadDelayExecution (unsigned long * duration)
  111. {
  112. struct timespec sleeptime;
  113. struct timespec remainingtime;
  114. long sec = (unsigned long) *duration / 1000000;
  115. long microsec = (unsigned long) *duration - (sec * 1000000);
  116. sleeptime.tv_sec = sec;
  117. sleeptime.tv_nsec = microsec * 1000;
  118. int ret = INLINE_SYSCALL(nanosleep, 2, &sleeptime, &remainingtime);
  119. if (IS_ERR(ret)) {
  120. PAL_NUM remaining = remainingtime.tv_sec * 1000000 +
  121. remainingtime.tv_nsec / 1000;
  122. *duration -= remaining;
  123. return -PAL_ERROR_INTERRUPTED;
  124. }
  125. return 0;
  126. }
  127. /* PAL call DkThreadYieldExecution. Yield the execution
  128. of the current thread. */
  129. void _DkThreadYieldExecution (void)
  130. {
  131. INLINE_SYSCALL(sched_yield, 0);
  132. }
  133. /* _DkThreadExit for internal use: Thread exiting */
  134. void _DkThreadExit (int exitcode)
  135. {
  136. INLINE_SYSCALL(exit, 1, 0);
  137. }
  138. int _DkThreadResume (PAL_HANDLE threadHandle)
  139. {
  140. int ret = INLINE_SYSCALL(tgkill, 3, pal_linux_config.pid,
  141. threadHandle->thread.tid, SIGCONT);
  142. if (IS_ERR(ret))
  143. return -PAL_ERROR_DENIED;
  144. return 0;
  145. }
  146. struct handle_ops thread_ops = {
  147. /* nothing */
  148. };