futex.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Time.h>
  27. #include <Kernel/Process.h>
  28. namespace Kernel {
  29. static void compute_relative_timeout_from_absolute(const timeval& absolute_time, timeval& relative_time)
  30. {
  31. // Convert absolute time to relative time of day.
  32. timeval_sub(absolute_time, kgettimeofday(), relative_time);
  33. }
  34. static void compute_relative_timeout_from_absolute(const timespec& absolute_time, timeval& relative_time)
  35. {
  36. timeval tv_absolute_time;
  37. timespec_to_timeval(absolute_time, tv_absolute_time);
  38. compute_relative_timeout_from_absolute(tv_absolute_time, relative_time);
  39. }
  40. WaitQueue& Process::futex_queue(Userspace<const i32*> userspace_address)
  41. {
  42. auto& queue = m_futex_queues.ensure(userspace_address.ptr());
  43. if (!queue)
  44. queue = make<WaitQueue>();
  45. return *queue;
  46. }
  47. int Process::sys$futex(Userspace<const Syscall::SC_futex_params*> user_params)
  48. {
  49. REQUIRE_PROMISE(thread);
  50. Syscall::SC_futex_params params;
  51. if (!copy_from_user(&params, user_params))
  52. return -EFAULT;
  53. switch (params.futex_op) {
  54. case FUTEX_WAIT: {
  55. i32 user_value;
  56. if (!copy_from_user(&user_value, params.userspace_address))
  57. return -EFAULT;
  58. if (user_value != params.val)
  59. return -EAGAIN;
  60. timespec ts_abstimeout { 0, 0 };
  61. if (params.timeout && !copy_from_user(&ts_abstimeout, params.timeout))
  62. return -EFAULT;
  63. timeval* optional_timeout = nullptr;
  64. timeval relative_timeout { 0, 0 };
  65. if (params.timeout) {
  66. compute_relative_timeout_from_absolute(ts_abstimeout, relative_timeout);
  67. optional_timeout = &relative_timeout;
  68. }
  69. // FIXME: This is supposed to be interruptible by a signal, but right now WaitQueue cannot be interrupted.
  70. WaitQueue& wait_queue = futex_queue((FlatPtr)params.userspace_address);
  71. Thread::BlockResult result = Thread::current()->wait_on(wait_queue, "Futex", optional_timeout);
  72. if (result == Thread::BlockResult::InterruptedByTimeout) {
  73. return -ETIMEDOUT;
  74. }
  75. break;
  76. }
  77. case FUTEX_WAKE:
  78. if (params.val == 0)
  79. return 0;
  80. if (params.val == 1) {
  81. futex_queue((FlatPtr)params.userspace_address).wake_one();
  82. } else {
  83. futex_queue((FlatPtr)params.userspace_address).wake_n(params.val);
  84. }
  85. break;
  86. }
  87. return 0;
  88. }
  89. }