Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * linux/kernel/itimer.c
  3  *
  4  * Copyright (C) 1992 Darren Senn
  5  */
  6 
  7 /* These are all the functions necessary to implement itimers */
  8 
  9 #include <linux/mm.h>
 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>
 15 
 16 #include <asm/uaccess.h>
 17 
 18 /**
 19  * itimer_get_remtime - get remaining time for the timer
 20  *
 21  * @timer: the timer to read
 22  *
 23  * Returns the delta between the expiry time and now, which can be
 24  * less than zero or 1usec for an pending expired timer
 25  */
 26 static struct timeval itimer_get_remtime(struct hrtimer *timer)
 27 {
 28         ktime_t rem = hrtimer_get_remaining(timer);
 29 
 30         /*
 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.
 34          */
 35         if (hrtimer_active(timer)) {
 36                 if (rem.tv64 <= 0)
 37                         rem.tv64 = NSEC_PER_USEC;
 38         } else
 39                 rem.tv64 = 0;
 40 
 41         return ktime_to_timeval(rem);
 42 }
 43 
 44 int do_getitimer(int which, struct itimerval *value)
 45 {
 46         struct task_struct *tsk = current;
 47         cputime_t cinterval, cval;
 48 
 49         switch (which) {
 50         case ITIMER_REAL:
 51                 spin_lock_irq(&tsk->sighand->siglock);
 52                 value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
 53                 value->it_interval =
 54                         ktime_to_timeval(tsk->signal->it_real_incr);
 55                 spin_unlock_irq(&tsk->sighand->siglock);
 56                 break;
 57         case ITIMER_VIRTUAL:
 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;
 65                         do {
 66                                 utime = cputime_add(utime, t->utime);
 67                                 t = next_thread(t);
 68                         } while (t != tsk);
 69                         if (cputime_le(cval, utime)) { /* about to fire */
 70                                 cval = jiffies_to_cputime(1);
 71                         } else {
 72                                 cval = cputime_sub(cval, utime);
 73                         }
 74                 }
 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);
 79                 break;
 80         case ITIMER_PROF:
 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,
 88                                                       tsk->signal->stime);
 89                         do {
 90                                 ptime = cputime_add(ptime,
 91                                                     cputime_add(t->utime,
 92                                                                 t->stime));
 93                                 t = next_thread(t);
 94                         } while (t != tsk);
 95                         if (cputime_le(cval, ptime)) { /* about to fire */
 96                                 cval = jiffies_to_cputime(1);
 97                         } else {
 98                                 cval = cputime_sub(cval, ptime);
 99                         }
100                 }
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);
105                 break;
106         default:
107                 return(-EINVAL);
108         }
109         return 0;
110 }
111 
112 asmlinkage long sys_getitimer(int which, struct itimerval __user *value)
113 {
114         int error = -EFAULT;
115         struct itimerval get_buffer;
116 
117         if (value) {
118                 error = do_getitimer(which, &get_buffer);
119                 if (!error &&
120                     copy_to_user(value, &get_buffer, sizeof(get_buffer)))
121                         error = -EFAULT;
122         }
123         return error;
124 }
125 
126 
127 /*
128  * The timer is automagically restarted, when interval != 0
129  */
130 enum hrtimer_restart it_real_fn(struct hrtimer *timer)
131 {
132         struct signal_struct *sig =
133                 container_of(timer, struct signal_struct, real_timer);
134 
135         kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid);
136 
137         return HRTIMER_NORESTART;
138 }
139 
140 /*
141  * Returns true if the timeval is in canonical form
142  */
143 #define timeval_valid(t) \
144         (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
145 
146 int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
147 {
148         struct task_struct *tsk = current;
149         struct hrtimer *timer;
150         ktime_t expires;
151         cputime_t cval, cinterval, nval, ninterval;
152 
153         /*
154          * Validate the timevals in value.
155          */
156         if (!timeval_valid(&value->it_value) ||
157             !timeval_valid(&value->it_interval))
158                 return -EINVAL;
159 
160         switch (which) {
161         case ITIMER_REAL:
162 again:
163                 spin_lock_irq(&tsk->sighand->siglock);
164                 timer = &tsk->signal->real_timer;
165                 if (ovalue) {
166                         ovalue->it_value = itimer_get_remtime(timer);
167                         ovalue->it_interval
168                                 = ktime_to_timeval(tsk->signal->it_real_incr);
169                 }
170                 /* We are sharing ->siglock with it_real_fn() */
171                 if (hrtimer_try_to_cancel(timer) < 0) {
172                         spin_unlock_irq(&tsk->sighand->siglock);
173                         hrtimer_wait_for_timer(&tsk->signal->real_timer);
174                         goto again;
175                 }
176                 expires = timeval_to_ktime(value->it_value);
177                 if (expires.tv64 != 0) {
178                         tsk->signal->it_real_incr =
179                                 timeval_to_ktime(value->it_interval);
180                         hrtimer_start(timer, expires, HRTIMER_MODE_REL);
181                 } else
182                         tsk->signal->it_real_incr.tv64 = 0;
183 
184                 spin_unlock_irq(&tsk->sighand->siglock);
185                 break;
186         case ITIMER_VIRTUAL:
187                 nval = timeval_to_cputime(&value->it_value);
188                 ninterval = timeval_to_cputime(&value->it_interval);
189                 read_lock(&tasklist_lock);
190                 spin_lock_irq(&tsk->sighand->siglock);
191                 cval = tsk->signal->it_virt_expires;
192                 cinterval = tsk->signal->it_virt_incr;
193                 if (!cputime_eq(cval, cputime_zero) ||
194                     !cputime_eq(nval, cputime_zero)) {
195                         if (cputime_gt(nval, cputime_zero))
196                                 nval = cputime_add(nval,
197                                                    jiffies_to_cputime(1));
198                         set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
199                                               &nval, &cval);
200                 }
201                 tsk->signal->it_virt_expires = nval;
202                 tsk->signal->it_virt_incr = ninterval;
203                 spin_unlock_irq(&tsk->sighand->siglock);
204                 read_unlock(&tasklist_lock);
205                 if (ovalue) {
206                         cputime_to_timeval(cval, &ovalue->it_value);
207                         cputime_to_timeval(cinterval, &ovalue->it_interval);
208                 }
209                 break;
210         case ITIMER_PROF:
211                 nval = timeval_to_cputime(&value->it_value);
212                 ninterval = timeval_to_cputime(&value->it_interval);
213                 read_lock(&tasklist_lock);
214                 spin_lock_irq(&tsk->sighand->siglock);
215                 cval = tsk->signal->it_prof_expires;
216                 cinterval = tsk->signal->it_prof_incr;
217                 if (!cputime_eq(cval, cputime_zero) ||
218                     !cputime_eq(nval, cputime_zero)) {
219                         if (cputime_gt(nval, cputime_zero))
220                                 nval = cputime_add(nval,
221                                                    jiffies_to_cputime(1));
222                         set_process_cpu_timer(tsk, CPUCLOCK_PROF,
223                                               &nval, &cval);
224                 }
225                 tsk->signal->it_prof_expires = nval;
226                 tsk->signal->it_prof_incr = ninterval;
227                 spin_unlock_irq(&tsk->sighand->siglock);
228                 read_unlock(&tasklist_lock);
229                 if (ovalue) {
230                         cputime_to_timeval(cval, &ovalue->it_value);
231                         cputime_to_timeval(cinterval, &ovalue->it_interval);
232                 }
233                 break;
234         default:
235                 return -EINVAL;
236         }
237         return 0;
238 }
239 
240 /**
241  * alarm_setitimer - set alarm in seconds
242  *
243  * @seconds:    number of seconds until alarm
244  *              0 disables the alarm
245  *
246  * Returns the remaining time in seconds of a pending timer or 0 when
247  * the timer is not active.
248  *
249  * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
250  * negative timeval settings which would cause immediate expiry.
251  */
252 unsigned int alarm_setitimer(unsigned int seconds)
253 {
254         struct itimerval it_new, it_old;
255 
256 #if BITS_PER_LONG < 64
257         if (seconds > INT_MAX)
258                 seconds = INT_MAX;
259 #endif
260         it_new.it_value.tv_sec = seconds;
261         it_new.it_value.tv_usec = 0;
262         it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
263 
264         do_setitimer(ITIMER_REAL, &it_new, &it_old);
265 
266         /*
267          * We can't return 0 if we have an alarm pending ...  And we'd
268          * better return too much than too little anyway
269          */
270         if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
271               it_old.it_value.tv_usec >= 500000)
272                 it_old.it_value.tv_sec++;
273 
274         return it_old.it_value.tv_sec;
275 }
276 
277 asmlinkage long sys_setitimer(int which,
278                               struct itimerval __user *value,
279                               struct itimerval __user *ovalue)
280 {
281         struct itimerval set_buffer, get_buffer;
282         int error;
283 
284         if (value) {
285                 if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
286                         return -EFAULT;
287         } else
288                 memset((char *) &set_buffer, 0, sizeof(set_buffer));
289 
290         error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
291         if (error || !ovalue)
292                 return error;
293 
294         if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
295                 return -EFAULT;
296         return 0;
297 }
298 
  This page was automatically generated by the LXR engine.