sgx_graphene.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <pal.h>
  16. #include <pal_error.h>
  17. #include <atomic.h>
  18. #include <linux/futex.h>
  19. #include <errno.h>
  20. #include "sgx_internal.h"
  21. int _DkEventSet (PAL_HANDLE event, int wakeup)
  22. {
  23. int ret = 0;
  24. if (event->event.isnotification) {
  25. // Leave it signaled, wake all
  26. if (atomic_cmpxchg(&event->event.signaled, 0, 1) == 0) {
  27. int nwaiters = atomic_read(&event->event.nwaiters);
  28. if (nwaiters) {
  29. if (wakeup != -1 && nwaiters > wakeup)
  30. nwaiters = wakeup;
  31. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled,
  32. FUTEX_WAKE, nwaiters, NULL, NULL, 0);
  33. if (IS_ERR(ret))
  34. atomic_set(&event->event.signaled, 0);
  35. }
  36. }
  37. } else {
  38. // Only one thread wakes up, leave unsignaled
  39. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAKE, 1,
  40. NULL, NULL, 0);
  41. }
  42. return IS_ERR(ret) ? PAL_ERROR_TRYAGAIN : ret;
  43. }
  44. int _DkEventWait (PAL_HANDLE event)
  45. {
  46. int ret = 0;
  47. if (!event->event.isnotification || !atomic_read(&event->event.signaled)) {
  48. atomic_inc(&event->event.nwaiters);
  49. do {
  50. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAIT,
  51. 0, NULL, NULL, 0);
  52. if (IS_ERR(ret)) {
  53. if (ERRNO(ret) == EWOULDBLOCK) {
  54. ret = 0;
  55. } else {
  56. ret = -PAL_ERROR_DENIED;
  57. break;
  58. }
  59. }
  60. } while (event->event.isnotification &&
  61. !atomic_read(&event->event.signaled));
  62. atomic_dec(&event->event.nwaiters);
  63. }
  64. return ret;
  65. }
  66. #define PRINTBUF_SIZE 256
  67. struct printbuf {
  68. int idx; // current buffer index
  69. int cnt; // total bytes printed so far
  70. char buf[PRINTBUF_SIZE];
  71. };
  72. static int
  73. fputch(void * f, int ch, struct printbuf * b)
  74. {
  75. b->buf[b->idx++] = ch;
  76. if (b->idx == PRINTBUF_SIZE - 1) {
  77. INLINE_SYSCALL(write, 3, 2, b->buf, b->idx);
  78. b->idx = 0;
  79. }
  80. b->cnt++;
  81. return 0;
  82. }
  83. static int
  84. vprintf(const char * fmt, va_list *ap)
  85. {
  86. struct printbuf b;
  87. b.idx = 0;
  88. b.cnt = 0;
  89. vfprintfmt((void *) &fputch, NULL, &b, fmt, ap);
  90. INLINE_SYSCALL(write, 3, 2, b.buf, b.idx);
  91. return b.cnt;
  92. }
  93. int
  94. pal_printf(const char * fmt, ...)
  95. {
  96. va_list ap;
  97. int cnt;
  98. va_start(ap, fmt);
  99. cnt = vprintf(fmt, &ap);
  100. va_end(ap);
  101. return cnt;
  102. }