db_mutex.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_mutex.c
  15. *
  16. * This file contains APIs that provide operations of (futex based) mutexes.
  17. * Based on "Mutexes and Condition Variables using Futexes"
  18. * (http://locklessinc.com/articles/mutex_cv_futex)
  19. */
  20. #include <asm/errno.h>
  21. #include <atomic.h>
  22. #include <errno.h>
  23. #include <limits.h>
  24. #include <linux/futex.h>
  25. #include <linux/time.h>
  26. #include "api.h"
  27. #include "pal.h"
  28. #include "pal_debug.h"
  29. #include "pal_defs.h"
  30. #include "pal_error.h"
  31. #include "pal_internal.h"
  32. #include "pal_linux.h"
  33. #include "pal_linux_defs.h"
  34. #include "pal_linux_error.h"
  35. #define MUTEX_SPINLOCK_TIMES 100
  36. #define MUTEX_UNLOCKED 0
  37. #define MUTEX_LOCKED 1
  38. int _DkMutexCreate(PAL_HANDLE* handle, int initialCount) {
  39. PAL_HANDLE mut = malloc(HANDLE_SIZE(mutex));
  40. SET_HANDLE_TYPE(mut, mutex);
  41. atomic_set(&mut->mutex.mut.nwaiters, 0);
  42. mut->mutex.mut.locked = malloc_untrusted(sizeof(int64_t));
  43. if (!mut->mutex.mut.locked) {
  44. free(mut);
  45. return -PAL_ERROR_NOMEM;
  46. }
  47. *(mut->mutex.mut.locked) = initialCount;
  48. *handle = mut;
  49. return 0;
  50. }
  51. int _DkMutexLockTimeout(struct mutex_handle* m, int64_t timeout_us) {
  52. int ret = 0;
  53. if (MUTEX_UNLOCKED == cmpxchg(m->locked, MUTEX_UNLOCKED, MUTEX_LOCKED))
  54. goto success;
  55. if (timeout_us == 0) {
  56. ret = -PAL_ERROR_TRYAGAIN;
  57. goto out;
  58. }
  59. // Bump up the waiters count; we are probably going to block
  60. atomic_inc(&m->nwaiters);
  61. while (MUTEX_LOCKED == cmpxchg(m->locked, MUTEX_UNLOCKED, MUTEX_LOCKED)) {
  62. /*
  63. * Chia-Che 12/7/2017: m->locked points to untrusted memory, so
  64. * can be used for futex. Potentially this design may allow
  65. * attackers to change the mutex value and cause DoS.
  66. */
  67. ret = ocall_futex((int*)m->locked, FUTEX_WAIT, MUTEX_LOCKED, timeout_us);
  68. if (IS_ERR(ret)) {
  69. if (ERRNO(ret) == EWOULDBLOCK) {
  70. ret = -PAL_ERROR_TRYAGAIN;
  71. atomic_dec(&m->nwaiters);
  72. } else {
  73. ret = unix_to_pal_error(ERRNO(ret));
  74. atomic_dec(&m->nwaiters);
  75. }
  76. goto out;
  77. }
  78. }
  79. atomic_dec(&m->nwaiters);
  80. success:
  81. ret = 0;
  82. out:
  83. return ret;
  84. }
  85. int _DkMutexLock(struct mutex_handle* m) {
  86. return _DkMutexLockTimeout(m, -1);
  87. }
  88. int _DkMutexAcquireTimeout(PAL_HANDLE handle, int64_t timeout_us) {
  89. return _DkMutexLockTimeout(&handle->mutex.mut, timeout_us);
  90. }
  91. int _DkMutexUnlock(struct mutex_handle* m) {
  92. int ret = 0;
  93. int need_wake;
  94. /* Unlock */
  95. *(m->locked) = MUTEX_UNLOCKED; // TODO: this is not atomic!
  96. /* We need to make sure the write to locked is visible to lock-ers
  97. * before we read the waiter count. */
  98. MB();
  99. need_wake = atomic_read(&m->nwaiters);
  100. /* If we need to wake someone up... */
  101. if (need_wake)
  102. ocall_futex((int*)m->locked, FUTEX_WAKE, 1, -1);
  103. return ret;
  104. }
  105. void _DkMutexRelease(PAL_HANDLE handle) {
  106. struct mutex_handle* mut = &handle->mutex.mut;
  107. int ret = _DkMutexUnlock(mut);
  108. if (ret < 0)
  109. _DkRaiseFailure(ret);
  110. return;
  111. }
  112. static int mutex_wait(PAL_HANDLE handle, int64_t timeout_us) {
  113. return _DkMutexAcquireTimeout(handle, timeout_us);
  114. }
  115. static int mutex_close(PAL_HANDLE handle) {
  116. free_untrusted((int64_t*)handle->mutex.mut.locked);
  117. return 0;
  118. }
  119. struct handle_ops mutex_ops = {
  120. .wait = &mutex_wait,
  121. .close = &mutex_close,
  122. };