2 * linux/kernel/itimer.c
4 * Copyright (C) 1992 Darren Senn
7 /* These are all the functions necessary to implement itimers */
10 #include <linux/interrupt.h>
11 #include <linux/syscalls.h>
12 #include <linux/time.h>
13 #include <linux/posix-timers.h>
14 #include <linux/hrtimer.h>
16 #include <asm/uaccess.h>
19 * itimer_get_remtime - get remaining time for the timer
21 * @timer: the timer to read
23 * Returns the delta between the expiry time and now, which can be
24 * less than zero or 1usec for an pending expired timer
26 static struct timeval itimer_get_remtime(struct hrtimer *timer)
28 ktime_t rem = hrtimer_get_remaining(timer);
31 * Racy but safe: if the itimer expires after the above
32 * hrtimer_get_remtime() call but before this condition
33 * then we return 0 - which is correct.
35 if (hrtimer_active(timer)) {
37 rem.tv64 = NSEC_PER_USEC;
41 return ktime_to_timeval(rem);
44 int do_getitimer(int which, struct itimerval *value)
46 struct task_struct *tsk = current;
47 cputime_t cinterval, cval;
51 spin_lock_irq(&tsk->sighand->siglock);
52 value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
54 ktime_to_timeval(tsk->signal->it_real_incr);
55 spin_unlock_irq(&tsk->sighand->siglock);
58 read_lock(&tasklist_lock);
59 spin_lock_irq(&tsk->sighand->siglock);
60 cval = tsk->signal->it_virt_expires;
61 cinterval = tsk->signal->it_virt_incr;
62 if (!cputime_eq(cval, cputime_zero)) {
63 struct task_struct *t = tsk;
64 cputime_t utime = tsk->signal->utime;
66 utime = cputime_add(utime, t->utime);
69 if (cputime_le(cval, utime)) { /* about to fire */
70 cval = jiffies_to_cputime(1);
72 cval = cputime_sub(cval, utime);
75 spin_unlock_irq(&tsk->sighand->siglock);
76 read_unlock(&tasklist_lock);
77 cputime_to_timeval(cval, &value->it_value);
78 cputime_to_timeval(cinterval, &value->it_interval);
81 read_lock(&tasklist_lock);
82 spin_lock_irq(&tsk->sighand->siglock);
83 cval = tsk->signal->it_prof_expires;
84 cinterval = tsk->signal->it_prof_incr;
85 if (!cputime_eq(cval, cputime_zero)) {
86 struct task_struct *t = tsk;
87 cputime_t ptime = cputime_add(tsk->signal->utime,
90 ptime = cputime_add(ptime,
95 if (cputime_le(cval, ptime)) { /* about to fire */
96 cval = jiffies_to_cputime(1);
98 cval = cputime_sub(cval, ptime);
101 spin_unlock_irq(&tsk->sighand->siglock);
102 read_unlock(&tasklist_lock);
103 cputime_to_timeval(cval, &value->it_value);
104 cputime_to_timeval(cinterval, &value->it_interval);
112 asmlinkage long sys_getitimer(int which, struct itimerval __user *value)
115 struct itimerval get_buffer;
118 error = do_getitimer(which, &get_buffer);
120 copy_to_user(value, &get_buffer, sizeof(get_buffer)))
128 * The timer is automagically restarted, when interval != 0
130 enum hrtimer_restart it_real_fn(struct hrtimer *timer)
132 struct signal_struct *sig =
133 container_of(timer, struct signal_struct, real_timer);
135 kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid);
137 return HRTIMER_NORESTART;
141 * Returns true if the timeval is in canonical form
143 #define timeval_valid(t) \
144 (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
146 int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
148 struct task_struct *tsk = current;
149 struct hrtimer *timer;
151 cputime_t cval, cinterval, nval, ninterval;
154 * Validate the timevals in value.
156 if (!timeval_valid(&value->it_value) ||
157 !timeval_valid(&value->it_interval))
163 spin_lock_irq(&tsk->sighand->siglock);
164 timer = &tsk->signal->real_timer;
166 ovalue->it_value = itimer_get_remtime(timer);
168 = ktime_to_timeval(tsk->signal->it_real_incr);
170 /* We are sharing ->siglock with it_real_fn() */
171 if (hrtimer_try_to_cancel(timer) < 0) {
172 spin_unlock_irq(&tsk->sighand->siglock);
175 expires = timeval_to_ktime(value->it_value);
176 if (expires.tv64 != 0) {
177 tsk->signal->it_real_incr =
178 timeval_to_ktime(value->it_interval);
179 hrtimer_start(timer, expires, HRTIMER_MODE_REL);
181 tsk->signal->it_real_incr.tv64 = 0;
183 spin_unlock_irq(&tsk->sighand->siglock);
186 nval = timeval_to_cputime(&value->it_value);
187 ninterval = timeval_to_cputime(&value->it_interval);
188 read_lock(&tasklist_lock);
189 spin_lock_irq(&tsk->sighand->siglock);
190 cval = tsk->signal->it_virt_expires;
191 cinterval = tsk->signal->it_virt_incr;
192 if (!cputime_eq(cval, cputime_zero) ||
193 !cputime_eq(nval, cputime_zero)) {
194 if (cputime_gt(nval, cputime_zero))
195 nval = cputime_add(nval,
196 jiffies_to_cputime(1));
197 set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
200 tsk->signal->it_virt_expires = nval;
201 tsk->signal->it_virt_incr = ninterval;
202 spin_unlock_irq(&tsk->sighand->siglock);
203 read_unlock(&tasklist_lock);
205 cputime_to_timeval(cval, &ovalue->it_value);
206 cputime_to_timeval(cinterval, &ovalue->it_interval);
210 nval = timeval_to_cputime(&value->it_value);
211 ninterval = timeval_to_cputime(&value->it_interval);
212 read_lock(&tasklist_lock);
213 spin_lock_irq(&tsk->sighand->siglock);
214 cval = tsk->signal->it_prof_expires;
215 cinterval = tsk->signal->it_prof_incr;
216 if (!cputime_eq(cval, cputime_zero) ||
217 !cputime_eq(nval, cputime_zero)) {
218 if (cputime_gt(nval, cputime_zero))
219 nval = cputime_add(nval,
220 jiffies_to_cputime(1));
221 set_process_cpu_timer(tsk, CPUCLOCK_PROF,
224 tsk->signal->it_prof_expires = nval;
225 tsk->signal->it_prof_incr = ninterval;
226 spin_unlock_irq(&tsk->sighand->siglock);
227 read_unlock(&tasklist_lock);
229 cputime_to_timeval(cval, &ovalue->it_value);
230 cputime_to_timeval(cinterval, &ovalue->it_interval);
240 * alarm_setitimer - set alarm in seconds
242 * @seconds: number of seconds until alarm
243 * 0 disables the alarm
245 * Returns the remaining time in seconds of a pending timer or 0 when
246 * the timer is not active.
248 * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
249 * negative timeval settings which would cause immediate expiry.
251 unsigned int alarm_setitimer(unsigned int seconds)
253 struct itimerval it_new, it_old;
255 #if BITS_PER_LONG < 64
256 if (seconds > INT_MAX)
259 it_new.it_value.tv_sec = seconds;
260 it_new.it_value.tv_usec = 0;
261 it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
263 do_setitimer(ITIMER_REAL, &it_new, &it_old);
266 * We can't return 0 if we have an alarm pending ... And we'd
267 * better return too much than too little anyway
269 if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
270 it_old.it_value.tv_usec >= 500000)
271 it_old.it_value.tv_sec++;
273 return it_old.it_value.tv_sec;
276 asmlinkage long sys_setitimer(int which,
277 struct itimerval __user *value,
278 struct itimerval __user *ovalue)
280 struct itimerval set_buffer, get_buffer;
284 if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
287 memset((char *) &set_buffer, 0, sizeof(set_buffer));
289 error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
290 if (error || !ovalue)
293 if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))