futex.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. 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. 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 (!validate_read_and_copy_typed(&params, user_params))
  52. return -EFAULT;
  53. if (!validate_read_typed(params.userspace_address))
  54. return -EFAULT;
  55. switch (params.futex_op) {
  56. case FUTEX_WAIT: {
  57. i32 user_value;
  58. copy_from_user(&user_value, params.userspace_address);
  59. if (user_value != params.val)
  60. return -EAGAIN;
  61. timespec ts_abstimeout { 0, 0 };
  62. if (params.timeout && !validate_read_and_copy_typed(&ts_abstimeout, params.timeout))
  63. return -EFAULT;
  64. timeval* optional_timeout = nullptr;
  65. timeval relative_timeout { 0, 0 };
  66. if (params.timeout) {
  67. compute_relative_timeout_from_absolute(ts_abstimeout, relative_timeout);
  68. optional_timeout = &relative_timeout;
  69. }
  70. // FIXME: This is supposed to be interruptible by a signal, but right now WaitQueue cannot be interrupted.
  71. WaitQueue& wait_queue = futex_queue(params.userspace_address);
  72. Thread::BlockResult result = Thread::current()->wait_on(wait_queue, "Futex", optional_timeout);
  73. if (result == Thread::BlockResult::InterruptedByTimeout) {
  74. return -ETIMEDOUT;
  75. }
  76. break;
  77. }
  78. case FUTEX_WAKE:
  79. if (params.val == 0)
  80. return 0;
  81. if (params.val == 1) {
  82. futex_queue(params.userspace_address).wake_one();
  83. } else {
  84. futex_queue(params.userspace_address).wake_n(params.val);
  85. }
  86. break;
  87. }
  88. return 0;
  89. }
  90. }