clock.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include <Kernel/Time/TimeManagement.h>
  29. namespace Kernel {
  30. int Process::sys$clock_gettime(clockid_t clock_id, Userspace<timespec*> user_ts)
  31. {
  32. REQUIRE_PROMISE(stdio);
  33. timespec ts = {};
  34. switch (clock_id) {
  35. case CLOCK_MONOTONIC:
  36. ts.tv_sec = TimeManagement::the().seconds_since_boot();
  37. ts.tv_nsec = TimeManagement::the().ticks_this_second() * 1000000;
  38. break;
  39. case CLOCK_REALTIME:
  40. ts = TimeManagement::the().epoch_time();
  41. break;
  42. default:
  43. return -EINVAL;
  44. }
  45. if (!copy_to_user(user_ts, &ts))
  46. return -EFAULT;
  47. return 0;
  48. }
  49. int Process::sys$clock_settime(clockid_t clock_id, Userspace<const timespec*> user_ts)
  50. {
  51. REQUIRE_PROMISE(settime);
  52. if (!is_superuser())
  53. return -EPERM;
  54. timespec ts;
  55. if (!copy_from_user(&ts, user_ts))
  56. return -EFAULT;
  57. switch (clock_id) {
  58. case CLOCK_REALTIME:
  59. TimeManagement::the().set_epoch_time(ts);
  60. break;
  61. default:
  62. return -EINVAL;
  63. }
  64. return 0;
  65. }
  66. int Process::sys$clock_nanosleep(Userspace<const Syscall::SC_clock_nanosleep_params*> user_params)
  67. {
  68. REQUIRE_PROMISE(stdio);
  69. Syscall::SC_clock_nanosleep_params params;
  70. if (!copy_from_user(&params, user_params))
  71. return -EFAULT;
  72. timespec requested_sleep;
  73. if (!copy_from_user(&requested_sleep, params.requested_sleep))
  74. return -EFAULT;
  75. bool is_absolute = params.flags & TIMER_ABSTIME;
  76. switch (params.clock_id) {
  77. case CLOCK_MONOTONIC: {
  78. u64 wakeup_time;
  79. if (is_absolute) {
  80. u64 time_to_wake = (requested_sleep.tv_sec * 1000 + requested_sleep.tv_nsec / 1000000);
  81. wakeup_time = Thread::current()->sleep_until(time_to_wake);
  82. } else {
  83. u64 ticks_to_sleep = requested_sleep.tv_sec * TimeManagement::the().ticks_per_second();
  84. ticks_to_sleep += requested_sleep.tv_nsec * TimeManagement::the().ticks_per_second() / 1000000000;
  85. if (!ticks_to_sleep)
  86. return 0;
  87. wakeup_time = Thread::current()->sleep(ticks_to_sleep);
  88. }
  89. if (wakeup_time > g_uptime) {
  90. u64 ticks_left = wakeup_time - g_uptime;
  91. if (!is_absolute && params.remaining_sleep) {
  92. timespec remaining_sleep = {};
  93. remaining_sleep.tv_sec = ticks_left / TimeManagement::the().ticks_per_second();
  94. ticks_left -= remaining_sleep.tv_sec * TimeManagement::the().ticks_per_second();
  95. remaining_sleep.tv_nsec = ticks_left * 1000000000 / TimeManagement::the().ticks_per_second();
  96. if (!copy_to_user(params.remaining_sleep, &remaining_sleep))
  97. return -EFAULT;
  98. }
  99. return -EINTR;
  100. }
  101. return 0;
  102. }
  103. default:
  104. return -EINVAL;
  105. }
  106. }
  107. int Process::sys$adjtime(Userspace<const timeval*> user_delta, Userspace<timeval*> user_old_delta)
  108. {
  109. if (user_old_delta) {
  110. timespec old_delta_ts = TimeManagement::the().remaining_epoch_time_adjustment();
  111. timeval old_delta;
  112. timespec_to_timeval(old_delta_ts, old_delta);
  113. if (!copy_to_user(user_old_delta, &old_delta))
  114. return -EFAULT;
  115. }
  116. if (user_delta) {
  117. REQUIRE_PROMISE(settime);
  118. if (!is_superuser())
  119. return -EPERM;
  120. timeval delta;
  121. if (!copy_from_user(&delta, user_delta))
  122. return -EFAULT;
  123. if (delta.tv_usec < 0 || delta.tv_usec >= 1'000'000)
  124. return -EINVAL;
  125. timespec delta_ts;
  126. timeval_to_timespec(delta, delta_ts);
  127. TimeManagement::the().set_remaining_epoch_time_adjustment(delta_ts);
  128. }
  129. return 0;
  130. }
  131. int Process::sys$gettimeofday(Userspace<timeval*> user_tv)
  132. {
  133. REQUIRE_PROMISE(stdio);
  134. auto tv = kgettimeofday();
  135. if (!copy_to_user(user_tv, &tv))
  136. return -EFAULT;
  137. return 0;
  138. }
  139. }