posix-stubs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
  19. /* Architectures may override SYS_NI and COMPAT_SYS_NI */
  20. #include <asm/syscall_wrapper.h>
  21. #endif
  22. asmlinkage long sys_ni_posix_timers(void)
  23. {
  24. pr_err_once("process %d (%s) attempted a POSIX timer syscall "
  25. "while CONFIG_POSIX_TIMERS is not set\n",
  26. current->pid, current->comm);
  27. return -ENOSYS;
  28. }
  29. #ifndef SYS_NI
  30. #define SYS_NI(name) SYSCALL_ALIAS(sys_##name, sys_ni_posix_timers)
  31. #endif
  32. #ifndef COMPAT_SYS_NI
  33. #define COMPAT_SYS_NI(name) SYSCALL_ALIAS(compat_sys_##name, sys_ni_posix_timers)
  34. #endif
  35. SYS_NI(timer_create);
  36. SYS_NI(timer_gettime);
  37. SYS_NI(timer_getoverrun);
  38. SYS_NI(timer_settime);
  39. SYS_NI(timer_delete);
  40. SYS_NI(clock_adjtime);
  41. SYS_NI(getitimer);
  42. SYS_NI(setitimer);
  43. SYS_NI(clock_adjtime32);
  44. #ifdef __ARCH_WANT_SYS_ALARM
  45. SYS_NI(alarm);
  46. #endif
  47. /*
  48. * We preserve minimal support for CLOCK_REALTIME and CLOCK_MONOTONIC
  49. * as it is easy to remain compatible with little code. CLOCK_BOOTTIME
  50. * is also included for convenience as at least systemd uses it.
  51. */
  52. SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  53. const struct __kernel_timespec __user *, tp)
  54. {
  55. struct timespec64 new_tp;
  56. if (which_clock != CLOCK_REALTIME)
  57. return -EINVAL;
  58. if (get_timespec64(&new_tp, tp))
  59. return -EFAULT;
  60. return do_sys_settimeofday64(&new_tp, NULL);
  61. }
  62. int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)
  63. {
  64. switch (which_clock) {
  65. case CLOCK_REALTIME:
  66. ktime_get_real_ts64(tp);
  67. break;
  68. case CLOCK_MONOTONIC:
  69. ktime_get_ts64(tp);
  70. timens_add_monotonic(tp);
  71. break;
  72. case CLOCK_BOOTTIME:
  73. ktime_get_boottime_ts64(tp);
  74. timens_add_boottime(tp);
  75. break;
  76. default:
  77. return -EINVAL;
  78. }
  79. return 0;
  80. }
  81. SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
  82. struct __kernel_timespec __user *, tp)
  83. {
  84. int ret;
  85. struct timespec64 kernel_tp;
  86. ret = do_clock_gettime(which_clock, &kernel_tp);
  87. if (ret)
  88. return ret;
  89. if (put_timespec64(&kernel_tp, tp))
  90. return -EFAULT;
  91. return 0;
  92. }
  93. SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, struct __kernel_timespec __user *, tp)
  94. {
  95. struct timespec64 rtn_tp = {
  96. .tv_sec = 0,
  97. .tv_nsec = hrtimer_resolution,
  98. };
  99. switch (which_clock) {
  100. case CLOCK_REALTIME:
  101. case CLOCK_MONOTONIC:
  102. case CLOCK_BOOTTIME:
  103. if (put_timespec64(&rtn_tp, tp))
  104. return -EFAULT;
  105. return 0;
  106. default:
  107. return -EINVAL;
  108. }
  109. }
  110. SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
  111. const struct __kernel_timespec __user *, rqtp,
  112. struct __kernel_timespec __user *, rmtp)
  113. {
  114. struct timespec64 t;
  115. ktime_t texp;
  116. switch (which_clock) {
  117. case CLOCK_REALTIME:
  118. case CLOCK_MONOTONIC:
  119. case CLOCK_BOOTTIME:
  120. break;
  121. default:
  122. return -EINVAL;
  123. }
  124. if (get_timespec64(&t, rqtp))
  125. return -EFAULT;
  126. if (!timespec64_valid(&t))
  127. return -EINVAL;
  128. if (flags & TIMER_ABSTIME)
  129. rmtp = NULL;
  130. current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
  131. current->restart_block.nanosleep.rmtp = rmtp;
  132. texp = timespec64_to_ktime(t);
  133. if (flags & TIMER_ABSTIME)
  134. texp = timens_ktime_to_host(which_clock, texp);
  135. return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
  136. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  137. which_clock);
  138. }
  139. #ifdef CONFIG_COMPAT
  140. COMPAT_SYS_NI(timer_create);
  141. #endif
  142. #if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA)
  143. COMPAT_SYS_NI(getitimer);
  144. COMPAT_SYS_NI(setitimer);
  145. #endif
  146. #ifdef CONFIG_COMPAT_32BIT_TIME
  147. SYS_NI(timer_settime32);
  148. SYS_NI(timer_gettime32);
  149. SYSCALL_DEFINE2(clock_settime32, const clockid_t, which_clock,
  150. struct old_timespec32 __user *, tp)
  151. {
  152. struct timespec64 new_tp;
  153. if (which_clock != CLOCK_REALTIME)
  154. return -EINVAL;
  155. if (get_old_timespec32(&new_tp, tp))
  156. return -EFAULT;
  157. return do_sys_settimeofday64(&new_tp, NULL);
  158. }
  159. SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
  160. struct old_timespec32 __user *, tp)
  161. {
  162. int ret;
  163. struct timespec64 kernel_tp;
  164. ret = do_clock_gettime(which_clock, &kernel_tp);
  165. if (ret)
  166. return ret;
  167. if (put_old_timespec32(&kernel_tp, tp))
  168. return -EFAULT;
  169. return 0;
  170. }
  171. SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
  172. struct old_timespec32 __user *, tp)
  173. {
  174. struct timespec64 rtn_tp = {
  175. .tv_sec = 0,
  176. .tv_nsec = hrtimer_resolution,
  177. };
  178. switch (which_clock) {
  179. case CLOCK_REALTIME:
  180. case CLOCK_MONOTONIC:
  181. case CLOCK_BOOTTIME:
  182. if (put_old_timespec32(&rtn_tp, tp))
  183. return -EFAULT;
  184. return 0;
  185. default:
  186. return -EINVAL;
  187. }
  188. }
  189. SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
  190. struct old_timespec32 __user *, rqtp,
  191. struct old_timespec32 __user *, rmtp)
  192. {
  193. struct timespec64 t;
  194. ktime_t texp;
  195. switch (which_clock) {
  196. case CLOCK_REALTIME:
  197. case CLOCK_MONOTONIC:
  198. case CLOCK_BOOTTIME:
  199. break;
  200. default:
  201. return -EINVAL;
  202. }
  203. if (get_old_timespec32(&t, rqtp))
  204. return -EFAULT;
  205. if (!timespec64_valid(&t))
  206. return -EINVAL;
  207. if (flags & TIMER_ABSTIME)
  208. rmtp = NULL;
  209. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  210. current->restart_block.nanosleep.compat_rmtp = rmtp;
  211. texp = timespec64_to_ktime(t);
  212. if (flags & TIMER_ABSTIME)
  213. texp = timens_ktime_to_host(which_clock, texp);
  214. return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
  215. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  216. which_clock);
  217. }
  218. #endif