db_threading.c 3.8 KB

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