db_threading.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /* default size of a new thread stack */
  35. /* DEP 2/4/17: There is enough stuff allocated on the PAL stack now
  36. * that we need two pages in Linux host mode. In SGX mode,
  37. * the enclave/non-clave split makes 1 page in/1 out sufficient.
  38. */
  39. #define THREAD_STACK_SIZE (pal_state.alloc_align * 2)
  40. /* _DkThreadCreate for internal use. Create an internal thread
  41. inside the current process. The arguments callback and param
  42. specify the starting function and parameters */
  43. int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
  44. const void * param, int flags)
  45. {
  46. void * child_stack = NULL;
  47. if (_DkVirtualMemoryAlloc(&child_stack, THREAD_STACK_SIZE, 0,
  48. PAL_PROT_READ|PAL_PROT_WRITE) < 0)
  49. return -PAL_ERROR_NOMEM;
  50. /* move child_stack to the top of stack. */
  51. child_stack += THREAD_STACK_SIZE;
  52. /* align child_stack to 16 */
  53. child_stack = (void *) ((uintptr_t) child_stack & ~16);
  54. int tid = 0;
  55. int ret = clone(callback, child_stack,
  56. CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SYSVSEM|
  57. CLONE_THREAD|CLONE_SIGHAND|CLONE_PTRACE|
  58. CLONE_PARENT_SETTID,
  59. param, &tid, NULL);
  60. if (IS_ERR(ret))
  61. return -PAL_ERROR_DENIED;
  62. PAL_HANDLE hdl = malloc(HANDLE_SIZE(thread));
  63. SET_HANDLE_TYPE(hdl, thread);
  64. hdl->thread.tid = tid;
  65. *handle = hdl;
  66. /* _DkThreadAdd(tid); */
  67. return 0;
  68. }
  69. int _DkThreadDelayExecution (unsigned long * duration)
  70. {
  71. struct timespec sleeptime;
  72. struct timespec remainingtime;
  73. long sec = (unsigned long) *duration / 1000000;
  74. long microsec = (unsigned long) *duration - (sec * 1000000);
  75. sleeptime.tv_sec = sec;
  76. sleeptime.tv_nsec = microsec * 1000;
  77. int ret = INLINE_SYSCALL(nanosleep, 2, &sleeptime, &remainingtime);
  78. if (IS_ERR(ret)) {
  79. PAL_NUM remaining = remainingtime.tv_sec * 1000000 +
  80. remainingtime.tv_nsec / 1000;
  81. *duration -= remaining;
  82. return -PAL_ERROR_INTERRUPTED;
  83. }
  84. return 0;
  85. }
  86. /* PAL call DkThreadYieldExecution. Yield the execution
  87. of the current thread. */
  88. void _DkThreadYieldExecution (void)
  89. {
  90. INLINE_SYSCALL(sched_yield, 0);
  91. }
  92. /* _DkThreadExit for internal use: Thread exiting */
  93. void _DkThreadExit (void)
  94. {
  95. INLINE_SYSCALL(exit, 1, 0);
  96. }
  97. int _DkThreadResume (PAL_HANDLE threadHandle)
  98. {
  99. int ret = INLINE_SYSCALL(tgkill, 3,
  100. linux_state.pid,
  101. threadHandle->thread.tid,
  102. SIGCONT);
  103. if (IS_ERR(ret))
  104. return -PAL_ERROR_DENIED;
  105. return 0;
  106. }
  107. struct handle_ops thread_ops = {
  108. /* nothing */
  109. };