db_threading.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /* default size of a new thread */
  36. #define PAL_THREAD_STACK_SIZE allocsize
  37. struct thread_param {
  38. int (*callback) (void *);
  39. const void * param;
  40. PAL_HANDLE event, thread;
  41. };
  42. void pal_start_thread (void * __param)
  43. {
  44. struct thread_param * pal_param = (struct thread_param *) __param;
  45. int (*callback) (void *) = *(void * volatile *) &pal_param->callback;
  46. void *param = *(void * volatile *) &pal_param->param;
  47. _DkEventWait(pal_param->event);
  48. volatile PAL_HANDLE thread = pal_param->thread;
  49. _DkEventDestroy(pal_param->event);
  50. free(pal_param);
  51. ENCLAVE_TLS(thread) = thread;
  52. callback(param);
  53. }
  54. /* _DkThreadCreate for internal use. Create an internal thread
  55. inside the current process. The arguments callback and param
  56. specify the starting function and parameters */
  57. int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
  58. const void * param, int flags)
  59. {
  60. struct thread_param * pal_param = malloc(sizeof(struct thread_param));
  61. pal_param->callback = callback;
  62. pal_param->param = param;
  63. _DkEventCreate(&pal_param->event, false, true);
  64. flags &= PAL_THREAD_MASK;
  65. unsigned int tid = 0;
  66. int ret = ocall_clone_thread(pal_start_thread, pal_param, NULL, &tid);
  67. if (ret < 0)
  68. return ret;
  69. assert(tid);
  70. PAL_HANDLE hdl = malloc(HANDLE_SIZE(thread));
  71. SET_HANDLE_TYPE(hdl, thread);
  72. hdl->thread.tid = tid;
  73. pal_param->thread = *handle = hdl;
  74. _DkEventSet(pal_param->event, 1);
  75. return 0;
  76. }
  77. int _DkThreadDelayExecution (unsigned long * duration)
  78. {
  79. return ocall_sleep(duration);
  80. }
  81. /* PAL call DkThreadYieldExecution. Yield the execution
  82. of the current thread. */
  83. void _DkThreadYieldExecution (void)
  84. {
  85. ocall_schedule(0);
  86. }
  87. /* _DkThreadExit for internal use: Thread exiting */
  88. void _DkThreadExit (void)
  89. {
  90. ocall_exit();
  91. }
  92. int _DkThreadResume (PAL_HANDLE threadHandle)
  93. {
  94. return ocall_schedule(threadHandle->thread.tid);
  95. }
  96. int _DkThreadGetCurrent (PAL_HANDLE * threadHandle)
  97. {
  98. *threadHandle = (PAL_HANDLE) ENCLAVE_TLS(thread);
  99. return 0;
  100. }
  101. struct handle_ops thread_ops = {
  102. /* nothing */
  103. };