2 * linux/kernel/itimer.c
4 * Copyright (C) 1992 Darren Senn
7 /* These are all the functions necessary to implement itimers */
10 #include <linux/smp_lock.h>
11 #include <linux/interrupt.h>
12 #include <linux/syscalls.h>
13 #include <linux/time.h>
14 #include <linux/posix-timers.h>
15 #include <linux/hrtimer.h>
17 #include <asm/uaccess.h>
20 * itimer_get_remtime - get remaining time for the timer
22 * @timer: the timer to read
24 * Returns the delta between the expiry time and now, which can be
25 * less than zero or 1usec for an pending expired timer
27 static struct timeval itimer_get_remtime(struct hrtimer *timer)
29 ktime_t rem = hrtimer_get_remaining(timer);
32 * Racy but safe: if the itimer expires after the above
33 * hrtimer_get_remtime() call but before this condition
34 * then we return 0 - which is correct.
36 if (hrtimer_active(timer)) {
38 rem.tv64 = NSEC_PER_USEC;
42 return ktime_to_timeval(rem);
45 int do_getitimer(int which, struct itimerval *value)
47 struct task_struct *tsk = current;
48 cputime_t cinterval, cval;
52 value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
54 ktime_to_timeval(tsk->signal->it_real_incr);
57 read_lock(&tasklist_lock);
58 spin_lock_irq(&tsk->sighand->siglock);
59 cval = tsk->signal->it_virt_expires;
60 cinterval = tsk->signal->it_virt_incr;
61 if (!cputime_eq(cval, cputime_zero)) {
62 struct task_struct *t = tsk;
63 cputime_t utime = tsk->signal->utime;
65 utime = cputime_add(utime, t->utime);
68 if (cputime_le(cval, utime)) { /* about to fire */
69 cval = jiffies_to_cputime(1);
71 cval = cputime_sub(cval, utime);
74 spin_unlock_irq(&tsk->sighand->siglock);
75 read_unlock(&tasklist_lock);
76 cputime_to_timeval(cval, &value->it_value);
77 cputime_to_timeval(cinterval, &value->it_interval);
80 read_lock(&tasklist_lock);
81 spin_lock_irq(&tsk->sighand->siglock);
82 cval = tsk->signal->it_prof_expires;
83 cinterval = tsk->signal->it_prof_incr;
84 if (!cputime_eq(cval, cputime_zero)) {
85 struct task_struct *t = tsk;
86 cputime_t ptime = cputime_add(tsk->signal->utime,
89 ptime = cputime_add(ptime,
94 if (cputime_le(cval, ptime)) { /* about to fire */
95 cval = jiffies_to_cputime(1);
97 cval = cputime_sub(cval, ptime);
100 spin_unlock_irq(&tsk->sighand->siglock);
101 read_unlock(&tasklist_lock);
102 cputime_to_timeval(cval, &value->it_value);
103 cputime_to_timeval(cinterval, &value->it_interval);
111 asmlinkage long sys_getitimer(int which, struct itimerval __user *value)
114 struct itimerval get_buffer;
117 error = do_getitimer(which, &get_buffer);
119 copy_to_user(value, &get_buffer, sizeof(get_buffer)))
127 * The timer is automagically restarted, when interval != 0
129 int it_real_fn(void *data)
131 struct task_struct *tsk = (struct task_struct *) data;
133 send_group_sig_info(SIGALRM, SEND_SIG_PRIV, tsk);
135 if (tsk->signal->it_real_incr.tv64 != 0) {
136 hrtimer_forward(&tsk->signal->real_timer,
137 tsk->signal->it_real_incr);
139 return HRTIMER_RESTART;
141 return HRTIMER_NORESTART;
144 int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
146 struct task_struct *tsk = current;
147 struct hrtimer *timer;
149 cputime_t cval, cinterval, nval, ninterval;
153 timer = &tsk->signal->real_timer;
154 hrtimer_cancel(timer);
156 ovalue->it_value = itimer_get_remtime(timer);
158 = ktime_to_timeval(tsk->signal->it_real_incr);
160 tsk->signal->it_real_incr =
161 timeval_to_ktime(value->it_interval);
162 expires = timeval_to_ktime(value->it_value);
163 if (expires.tv64 != 0)
164 hrtimer_start(timer, expires, HRTIMER_REL);
167 nval = timeval_to_cputime(&value->it_value);
168 ninterval = timeval_to_cputime(&value->it_interval);
169 read_lock(&tasklist_lock);
170 spin_lock_irq(&tsk->sighand->siglock);
171 cval = tsk->signal->it_virt_expires;
172 cinterval = tsk->signal->it_virt_incr;
173 if (!cputime_eq(cval, cputime_zero) ||
174 !cputime_eq(nval, cputime_zero)) {
175 if (cputime_gt(nval, cputime_zero))
176 nval = cputime_add(nval,
177 jiffies_to_cputime(1));
178 set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
181 tsk->signal->it_virt_expires = nval;
182 tsk->signal->it_virt_incr = ninterval;
183 spin_unlock_irq(&tsk->sighand->siglock);
184 read_unlock(&tasklist_lock);
186 cputime_to_timeval(cval, &ovalue->it_value);
187 cputime_to_timeval(cinterval, &ovalue->it_interval);
191 nval = timeval_to_cputime(&value->it_value);
192 ninterval = timeval_to_cputime(&value->it_interval);
193 read_lock(&tasklist_lock);
194 spin_lock_irq(&tsk->sighand->siglock);
195 cval = tsk->signal->it_prof_expires;
196 cinterval = tsk->signal->it_prof_incr;
197 if (!cputime_eq(cval, cputime_zero) ||
198 !cputime_eq(nval, cputime_zero)) {
199 if (cputime_gt(nval, cputime_zero))
200 nval = cputime_add(nval,
201 jiffies_to_cputime(1));
202 set_process_cpu_timer(tsk, CPUCLOCK_PROF,
205 tsk->signal->it_prof_expires = nval;
206 tsk->signal->it_prof_incr = ninterval;
207 spin_unlock_irq(&tsk->sighand->siglock);
208 read_unlock(&tasklist_lock);
210 cputime_to_timeval(cval, &ovalue->it_value);
211 cputime_to_timeval(cinterval, &ovalue->it_interval);
220 asmlinkage long sys_setitimer(int which,
221 struct itimerval __user *value,
222 struct itimerval __user *ovalue)
224 struct itimerval set_buffer, get_buffer;
228 if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
231 memset((char *) &set_buffer, 0, sizeof(set_buffer));
233 error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
234 if (error || !ovalue)
237 if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))