sgx_graphene.c 3.5 KB

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