posix-stubs.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Dummy stubs used when CONFIG_POSIX_TIMERS=n
  4. *
  5. * Created by: Nicolas Pitre, July 2016
  6. * Copyright: (C) 2016 Linaro Limited
  7. */
  8. #include <linux/linkage.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/errno.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/ktime.h>
  14. #include <linux/timekeeping.h>
  15. #include <linux/posix-timers.h>
  16. #include <linux/time_namespace.h>
  17. #include <linux/compat.h>
  18. /*
  19. * We preserve minimal support for CLOCK_REALTIME and CLOCK_MONOTONIC
  20. * as it is easy to remain compatible with little code. CLOCK_BOOTTIME
  21. * is also included for convenience as at least systemd uses it.
  22. */
  23. SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  24. const struct __kernel_timespec __user *, tp)
  25. {
  26. struct timespec64 new_tp;
  27. if (which_clock != CLOCK_REALTIME)
  28. return -EINVAL;
  29. if (get_timespec64(&new_tp, tp))
  30. return -EFAULT;
  31. return do_sys_settimeofday64(&new_tp, NULL);
  32. }
  33. static int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)
  34. {
  35. switch (which_clock) {
  36. case CLOCK_REALTIME:
  37. ktime_get_real_ts64(tp);
  38. break;
  39. case CLOCK_MONOTONIC:
  40. ktime_get_ts64(tp);
  41. timens_add_monotonic(tp);
  42. break;
  43. case CLOCK_BOOTTIME:
  44. ktime_get_boottime_ts64(tp);
  45. timens_add_boottime(tp);
  46. break;
  47. default:
  48. return -EINVAL;
  49. }
  50. return 0;
  51. }
  52. SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
  53. struct __kernel_timespec __user *, tp)
  54. {
  55. int ret;
  56. struct timespec64 kernel_tp;
  57. ret = do_clock_gettime(which_clock, &kernel_tp);
  58. if (ret)
  59. return ret;
  60. if (put_timespec64(&kernel_tp, tp))
  61. return -EFAULT;
  62. return 0;
  63. }
  64. SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, struct __kernel_timespec __user *, tp)
  65. {
  66. struct timespec64 rtn_tp = {
  67. .tv_sec = 0,
  68. .tv_nsec = hrtimer_resolution,
  69. };
  70. switch (which_clock) {
  71. case CLOCK_REALTIME:
  72. case CLOCK_MONOTONIC:
  73. case CLOCK_BOOTTIME:
  74. if (put_timespec64(&rtn_tp, tp))
  75. return -EFAULT;
  76. return 0;
  77. default:
  78. return -EINVAL;
  79. }
  80. }
  81. SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
  82. const struct __kernel_timespec __user *, rqtp,
  83. struct __kernel_timespec __user *, rmtp)
  84. {
  85. struct timespec64 t;
  86. ktime_t texp;
  87. switch (which_clock) {
  88. case CLOCK_REALTIME:
  89. case CLOCK_MONOTONIC:
  90. case CLOCK_BOOTTIME:
  91. break;
  92. default:
  93. return -EINVAL;
  94. }
  95. if (get_timespec64(&t, rqtp))
  96. return -EFAULT;
  97. if (!timespec64_valid(&t))
  98. return -EINVAL;
  99. if (flags & TIMER_ABSTIME)
  100. rmtp = NULL;
  101. current->restart_block.fn = do_no_restart_syscall;
  102. current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
  103. current->restart_block.nanosleep.rmtp = rmtp;
  104. texp = timespec64_to_ktime(t);
  105. if (flags & TIMER_ABSTIME)
  106. texp = timens_ktime_to_host(which_clock, texp);
  107. return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
  108. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  109. which_clock);
  110. }
  111. #ifdef CONFIG_COMPAT_32BIT_TIME
  112. SYSCALL_DEFINE2(clock_settime32, const clockid_t, which_clock,
  113. struct old_timespec32 __user *, tp)
  114. {
  115. struct timespec64 new_tp;
  116. if (which_clock != CLOCK_REALTIME)
  117. return -EINVAL;
  118. if (get_old_timespec32(&new_tp, tp))
  119. return -EFAULT;
  120. return do_sys_settimeofday64(&new_tp, NULL);
  121. }
  122. SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
  123. struct old_timespec32 __user *, tp)
  124. {
  125. int ret;
  126. struct timespec64 kernel_tp;
  127. ret = do_clock_gettime(which_clock, &kernel_tp);
  128. if (ret)
  129. return ret;
  130. if (put_old_timespec32(&kernel_tp, tp))
  131. return -EFAULT;
  132. return 0;
  133. }
  134. SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
  135. struct old_timespec32 __user *, tp)
  136. {
  137. struct timespec64 rtn_tp = {
  138. .tv_sec = 0,
  139. .tv_nsec = hrtimer_resolution,
  140. };
  141. switch (which_clock) {
  142. case CLOCK_REALTIME:
  143. case CLOCK_MONOTONIC:
  144. case CLOCK_BOOTTIME:
  145. if (put_old_timespec32(&rtn_tp, tp))
  146. return -EFAULT;
  147. return 0;
  148. default:
  149. return -EINVAL;
  150. }
  151. }
  152. SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
  153. struct old_timespec32 __user *, rqtp,
  154. struct old_timespec32 __user *, rmtp)
  155. {
  156. struct timespec64 t;
  157. ktime_t texp;
  158. switch (which_clock) {
  159. case CLOCK_REALTIME:
  160. case CLOCK_MONOTONIC:
  161. case CLOCK_BOOTTIME:
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. if (get_old_timespec32(&t, rqtp))
  167. return -EFAULT;
  168. if (!timespec64_valid(&t))
  169. return -EINVAL;
  170. if (flags & TIMER_ABSTIME)
  171. rmtp = NULL;
  172. current->restart_block.fn = do_no_restart_syscall;
  173. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  174. current->restart_block.nanosleep.compat_rmtp = rmtp;
  175. texp = timespec64_to_ktime(t);
  176. if (flags & TIMER_ABSTIME)
  177. texp = timens_ktime_to_host(which_clock, texp);
  178. return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
  179. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  180. which_clock);
  181. }
  182. #endif