0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/linkage.h>
0010 #include <linux/kernel.h>
0011 #include <linux/sched.h>
0012 #include <linux/errno.h>
0013 #include <linux/syscalls.h>
0014 #include <linux/ktime.h>
0015 #include <linux/timekeeping.h>
0016 #include <linux/posix-timers.h>
0017 #include <linux/time_namespace.h>
0018 #include <linux/compat.h>
0019
0020 #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
0021
0022 #include <asm/syscall_wrapper.h>
0023 #endif
0024
0025 asmlinkage long sys_ni_posix_timers(void)
0026 {
0027 pr_err_once("process %d (%s) attempted a POSIX timer syscall "
0028 "while CONFIG_POSIX_TIMERS is not set\n",
0029 current->pid, current->comm);
0030 return -ENOSYS;
0031 }
0032
0033 #ifndef SYS_NI
0034 #define SYS_NI(name) SYSCALL_ALIAS(sys_##name, sys_ni_posix_timers)
0035 #endif
0036
0037 #ifndef COMPAT_SYS_NI
0038 #define COMPAT_SYS_NI(name) SYSCALL_ALIAS(compat_sys_##name, sys_ni_posix_timers)
0039 #endif
0040
0041 SYS_NI(timer_create);
0042 SYS_NI(timer_gettime);
0043 SYS_NI(timer_getoverrun);
0044 SYS_NI(timer_settime);
0045 SYS_NI(timer_delete);
0046 SYS_NI(clock_adjtime);
0047 SYS_NI(getitimer);
0048 SYS_NI(setitimer);
0049 SYS_NI(clock_adjtime32);
0050 #ifdef __ARCH_WANT_SYS_ALARM
0051 SYS_NI(alarm);
0052 #endif
0053
0054
0055
0056
0057
0058
0059
0060 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
0061 const struct __kernel_timespec __user *, tp)
0062 {
0063 struct timespec64 new_tp;
0064
0065 if (which_clock != CLOCK_REALTIME)
0066 return -EINVAL;
0067 if (get_timespec64(&new_tp, tp))
0068 return -EFAULT;
0069
0070 return do_sys_settimeofday64(&new_tp, NULL);
0071 }
0072
0073 static int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)
0074 {
0075 switch (which_clock) {
0076 case CLOCK_REALTIME:
0077 ktime_get_real_ts64(tp);
0078 break;
0079 case CLOCK_MONOTONIC:
0080 ktime_get_ts64(tp);
0081 timens_add_monotonic(tp);
0082 break;
0083 case CLOCK_BOOTTIME:
0084 ktime_get_boottime_ts64(tp);
0085 timens_add_boottime(tp);
0086 break;
0087 default:
0088 return -EINVAL;
0089 }
0090
0091 return 0;
0092 }
0093
0094 SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
0095 struct __kernel_timespec __user *, tp)
0096 {
0097 int ret;
0098 struct timespec64 kernel_tp;
0099
0100 ret = do_clock_gettime(which_clock, &kernel_tp);
0101 if (ret)
0102 return ret;
0103
0104 if (put_timespec64(&kernel_tp, tp))
0105 return -EFAULT;
0106 return 0;
0107 }
0108
0109 SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, struct __kernel_timespec __user *, tp)
0110 {
0111 struct timespec64 rtn_tp = {
0112 .tv_sec = 0,
0113 .tv_nsec = hrtimer_resolution,
0114 };
0115
0116 switch (which_clock) {
0117 case CLOCK_REALTIME:
0118 case CLOCK_MONOTONIC:
0119 case CLOCK_BOOTTIME:
0120 if (put_timespec64(&rtn_tp, tp))
0121 return -EFAULT;
0122 return 0;
0123 default:
0124 return -EINVAL;
0125 }
0126 }
0127
0128 SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
0129 const struct __kernel_timespec __user *, rqtp,
0130 struct __kernel_timespec __user *, rmtp)
0131 {
0132 struct timespec64 t;
0133 ktime_t texp;
0134
0135 switch (which_clock) {
0136 case CLOCK_REALTIME:
0137 case CLOCK_MONOTONIC:
0138 case CLOCK_BOOTTIME:
0139 break;
0140 default:
0141 return -EINVAL;
0142 }
0143
0144 if (get_timespec64(&t, rqtp))
0145 return -EFAULT;
0146 if (!timespec64_valid(&t))
0147 return -EINVAL;
0148 if (flags & TIMER_ABSTIME)
0149 rmtp = NULL;
0150 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
0151 current->restart_block.nanosleep.rmtp = rmtp;
0152 texp = timespec64_to_ktime(t);
0153 if (flags & TIMER_ABSTIME)
0154 texp = timens_ktime_to_host(which_clock, texp);
0155 return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
0156 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
0157 which_clock);
0158 }
0159
0160 #ifdef CONFIG_COMPAT
0161 COMPAT_SYS_NI(timer_create);
0162 #endif
0163
0164 #if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA)
0165 COMPAT_SYS_NI(getitimer);
0166 COMPAT_SYS_NI(setitimer);
0167 #endif
0168
0169 #ifdef CONFIG_COMPAT_32BIT_TIME
0170 SYS_NI(timer_settime32);
0171 SYS_NI(timer_gettime32);
0172
0173 SYSCALL_DEFINE2(clock_settime32, const clockid_t, which_clock,
0174 struct old_timespec32 __user *, tp)
0175 {
0176 struct timespec64 new_tp;
0177
0178 if (which_clock != CLOCK_REALTIME)
0179 return -EINVAL;
0180 if (get_old_timespec32(&new_tp, tp))
0181 return -EFAULT;
0182
0183 return do_sys_settimeofday64(&new_tp, NULL);
0184 }
0185
0186 SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
0187 struct old_timespec32 __user *, tp)
0188 {
0189 int ret;
0190 struct timespec64 kernel_tp;
0191
0192 ret = do_clock_gettime(which_clock, &kernel_tp);
0193 if (ret)
0194 return ret;
0195
0196 if (put_old_timespec32(&kernel_tp, tp))
0197 return -EFAULT;
0198 return 0;
0199 }
0200
0201 SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
0202 struct old_timespec32 __user *, tp)
0203 {
0204 struct timespec64 rtn_tp = {
0205 .tv_sec = 0,
0206 .tv_nsec = hrtimer_resolution,
0207 };
0208
0209 switch (which_clock) {
0210 case CLOCK_REALTIME:
0211 case CLOCK_MONOTONIC:
0212 case CLOCK_BOOTTIME:
0213 if (put_old_timespec32(&rtn_tp, tp))
0214 return -EFAULT;
0215 return 0;
0216 default:
0217 return -EINVAL;
0218 }
0219 }
0220
0221 SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
0222 struct old_timespec32 __user *, rqtp,
0223 struct old_timespec32 __user *, rmtp)
0224 {
0225 struct timespec64 t;
0226 ktime_t texp;
0227
0228 switch (which_clock) {
0229 case CLOCK_REALTIME:
0230 case CLOCK_MONOTONIC:
0231 case CLOCK_BOOTTIME:
0232 break;
0233 default:
0234 return -EINVAL;
0235 }
0236
0237 if (get_old_timespec32(&t, rqtp))
0238 return -EFAULT;
0239 if (!timespec64_valid(&t))
0240 return -EINVAL;
0241 if (flags & TIMER_ABSTIME)
0242 rmtp = NULL;
0243 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
0244 current->restart_block.nanosleep.compat_rmtp = rmtp;
0245 texp = timespec64_to_ktime(t);
0246 if (flags & TIMER_ABSTIME)
0247 texp = timens_ktime_to_host(which_clock, texp);
0248 return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
0249 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
0250 which_clock);
0251 }
0252 #endif