db_mutex.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_mutex.c
  17. *
  18. * This file contains APIs that provide operations of (futex based) mutexes.
  19. * Based on "Mutexes and Condition Variables using Futexes"
  20. * (http://locklessinc.com/articles/mutex_cv_futex)
  21. */
  22. #include "pal_defs.h"
  23. #include "pal_linux_defs.h"
  24. #include "pal.h"
  25. #include "pal_internal.h"
  26. #include "pal_linux.h"
  27. #include "pal_linux_error.h"
  28. #include "pal_error.h"
  29. #include "pal_debug.h"
  30. #include "api.h"
  31. #include <linux/futex.h>
  32. #include <limits.h>
  33. #include <atomic.h>
  34. #include <linux/time.h>
  35. #include <errno.h>
  36. #include <asm/errno.h>
  37. #define MUTEX_SPINLOCK_TIMES 100
  38. #define MUTEX_UNLOCKED 0
  39. #define MUTEX_LOCKED 1
  40. int
  41. _DkMutexCreate (PAL_HANDLE handle, int initialCount)
  42. {
  43. /*
  44. * Allocation and free of the handle are done outside of host-specific code.
  45. * This code initializes the mutex state that is host-specific,
  46. * including how initialCount is encoded.
  47. */
  48. SET_HANDLE_TYPE(handle, mutex);
  49. atomic_set(&handle->mutex.mut.nwaiters, 0);
  50. handle->mutex.mut.locked = initialCount;
  51. return 0;
  52. }
  53. void _DkMutexDestroy (PAL_HANDLE handle)
  54. {
  55. free(handle);
  56. }
  57. int _DkMutexLockTimeout (struct mutex_handle * m, uint64_t timeout)
  58. {
  59. int ret = 0;
  60. if (MUTEX_UNLOCKED == cmpxchg(&m->locked, MUTEX_UNLOCKED, MUTEX_LOCKED))
  61. goto success;
  62. if (timeout == 0) {
  63. ret = -PAL_ERROR_TRYAGAIN;
  64. goto out;
  65. }
  66. // Bump up the waiters count; we are probably going to block
  67. atomic_inc(&m->nwaiters);
  68. while (MUTEX_LOCKED == cmpxchg(&m->locked, MUTEX_UNLOCKED, MUTEX_LOCKED)) {
  69. // This is broken. The mutex is in enclave memory, the URTS can't
  70. // do FUTEX_WAIT on it. This call will always fail and the next level
  71. // up needs to retry.
  72. ret = ocall_futex((int *) m, FUTEX_WAIT, MUTEX_LOCKED, timeout == -1 ? NULL : &timeout);
  73. if (ret < 0) {
  74. if (-ret == EWOULDBLOCK) {
  75. ret = -PAL_ERROR_TRYAGAIN;
  76. atomic_dec(&m->nwaiters);
  77. goto out;
  78. }
  79. ret = unix_to_pal_error(ERRNO(ret));
  80. atomic_dec(&m->nwaiters);
  81. goto out;
  82. }
  83. }
  84. atomic_dec(&m->nwaiters);
  85. success:
  86. ret = 0;
  87. out:
  88. return ret;
  89. }
  90. int _DkMutexLock (struct mutex_handle * m)
  91. {
  92. int ret = 0, i;
  93. return _DkMutexLockTimeout(m, -1);
  94. }
  95. int _DkMutexAcquireTimeout (PAL_HANDLE handle, int _timeout)
  96. {
  97. struct mutex_handle * mut = &handle->mutex.mut;
  98. return _DkMutexLockTimeout(mut, _timeout);
  99. }
  100. int _DkMutexUnlock (struct mutex_handle * m)
  101. {
  102. int ret = 0;
  103. int need_wake;
  104. /* Unlock */
  105. m->locked = 0;
  106. /* We need to make sure the write to locked is visible to lock-ers
  107. * before we read the waiter count. */
  108. mb();
  109. need_wake= atomic_read(&m->nwaiters);
  110. /* If we need to wake someone up... */
  111. if (need_wake)
  112. ocall_futex((int *) m, FUTEX_WAKE, 1, NULL);
  113. return ret;
  114. }
  115. void _DkMutexRelease (PAL_HANDLE handle)
  116. {
  117. struct mutex_handle * mut = &handle->mutex.mut;
  118. int ret = _DkMutexUnlock(mut);
  119. if (ret < 0)
  120. _DkRaiseFailure(ret);
  121. return;
  122. }