db_mutex.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /*
  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. PAL_HANDLE mut = malloc(HANDLE_SIZE(mutex));
  44. SET_HANDLE_TYPE(mut, mutex);
  45. atomic_set(&mut->mutex.mut.nwaiters, 0);
  46. mut->mutex.mut.locked = malloc_untrusted(sizeof(int64_t));
  47. if (!mut->mutex.mut.locked) {
  48. free(mut);
  49. return -PAL_ERROR_NOMEM;
  50. }
  51. *(mut->mutex.mut.locked) = initialCount;
  52. *handle = mut;
  53. return 0;
  54. }
  55. int _DkMutexLockTimeout (struct mutex_handle * m, uint64_t timeout)
  56. {
  57. int ret = 0;
  58. if (MUTEX_UNLOCKED == cmpxchg(m->locked, MUTEX_UNLOCKED, MUTEX_LOCKED))
  59. goto success;
  60. if (timeout == 0) {
  61. ret = -PAL_ERROR_TRYAGAIN;
  62. goto out;
  63. }
  64. // Bump up the waiters count; we are probably going to block
  65. atomic_inc(&m->nwaiters);
  66. while (MUTEX_LOCKED == cmpxchg(m->locked, MUTEX_UNLOCKED, MUTEX_LOCKED)) {
  67. /*
  68. * Chia-Che 12/7/2017: m->locked points to untrusted memory, so
  69. * can be used for futex. Potentially this design may allow
  70. * attackers to change the mutex value and cause DoS.
  71. */
  72. ret = ocall_futex((int *) m->locked, 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. return _DkMutexLockTimeout(m, -1);
  93. }
  94. int _DkMutexAcquireTimeout (PAL_HANDLE handle, int _timeout)
  95. {
  96. struct mutex_handle * mut = &handle->mutex.mut;
  97. return _DkMutexLockTimeout(mut, _timeout);
  98. }
  99. int _DkMutexUnlock (struct mutex_handle * m)
  100. {
  101. int ret = 0;
  102. int need_wake;
  103. /* Unlock */
  104. *(m->locked) = 0;
  105. /* We need to make sure the write to locked is visible to lock-ers
  106. * before we read the waiter count. */
  107. mb();
  108. need_wake= atomic_read(&m->nwaiters);
  109. /* If we need to wake someone up... */
  110. if (need_wake)
  111. ocall_futex((int *) m->locked, FUTEX_WAKE, 1, NULL);
  112. return ret;
  113. }
  114. void _DkMutexRelease (PAL_HANDLE handle)
  115. {
  116. struct mutex_handle * mut = &handle->mutex.mut;
  117. int ret = _DkMutexUnlock(mut);
  118. if (ret < 0)
  119. _DkRaiseFailure(ret);
  120. return;
  121. }
  122. static int mutex_wait (PAL_HANDLE handle, uint64_t timeout)
  123. {
  124. return _DkMutexAcquireTimeout(handle, timeout);
  125. }
  126. static int mutex_close (PAL_HANDLE handle)
  127. {
  128. free_untrusted(handle->mutex.mut.locked);
  129. return 0;
  130. }
  131. struct handle_ops mutex_ops = {
  132. .wait = &mutex_wait,
  133. .close = &mutex_close,
  134. };