db_threading.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "ecall_types.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. #include <list.h>
  35. static PAL_LOCK thread_list_lock = LOCK_INIT;
  36. DEFINE_LISTP(pal_handle_thread);
  37. static LISTP_TYPE(pal_handle_thread) thread_list = LISTP_INIT;
  38. struct thread_param {
  39. int (*callback) (void *);
  40. const void * param;
  41. };
  42. extern void * enclave_base;
  43. void pal_start_thread (void)
  44. {
  45. struct pal_handle_thread *new_thread = NULL, *tmp;
  46. _DkInternalLock(&thread_list_lock);
  47. listp_for_each_entry(tmp, &thread_list, list)
  48. if (!tmp->tcs) {
  49. new_thread = tmp;
  50. new_thread->tcs =
  51. enclave_base + GET_ENCLAVE_TLS(tcs_offset);
  52. break;
  53. }
  54. _DkInternalUnlock(&thread_list_lock);
  55. if (!new_thread)
  56. return;
  57. struct thread_param * thread_param =
  58. (struct thread_param *) new_thread->param;
  59. int (*callback) (void *) = thread_param->callback;
  60. const void * param = thread_param->param;
  61. free(thread_param);
  62. new_thread->param = NULL;
  63. SET_ENCLAVE_TLS(thread, new_thread);
  64. callback((void *) param);
  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, int flags)
  71. {
  72. PAL_HANDLE new_thread = malloc(HANDLE_SIZE(thread));
  73. SET_HANDLE_TYPE(new_thread, thread);
  74. new_thread->thread.tcs = NULL;
  75. INIT_LIST_HEAD(&new_thread->thread, list);
  76. struct thread_param * thread_param = malloc(sizeof(struct thread_param));
  77. thread_param->callback = callback;
  78. thread_param->param = param;
  79. new_thread->thread.param = (void *) thread_param;
  80. _DkInternalLock(&thread_list_lock);
  81. listp_add_tail(&new_thread->thread, &thread_list, list);
  82. _DkInternalUnlock(&thread_list_lock);
  83. int ret = ocall_wake_thread(NULL);
  84. if (ret < 0)
  85. return ret;
  86. *handle = new_thread;
  87. return 0;
  88. }
  89. int _DkThreadDelayExecution (unsigned long * duration)
  90. {
  91. return ocall_sleep(duration);
  92. }
  93. /* PAL call DkThreadYieldExecution. Yield the execution
  94. of the current thread. */
  95. void _DkThreadYieldExecution (void)
  96. {
  97. ocall_sleep(NULL);
  98. }
  99. /* _DkThreadExit for internal use: Thread exiting */
  100. void _DkThreadExit (void)
  101. {
  102. ocall_exit();
  103. }
  104. int _DkThreadResume (PAL_HANDLE threadHandle)
  105. {
  106. return ocall_wake_thread(threadHandle->thread.tcs);
  107. }
  108. int _DkThreadGetCurrent (PAL_HANDLE * threadHandle)
  109. {
  110. *threadHandle = (PAL_HANDLE) GET_ENCLAVE_TLS(thread);
  111. return 0;
  112. }
  113. struct handle_ops thread_ops = {
  114. /* nothing */
  115. };