itimer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * linux/kernel/itimer.c
  3. *
  4. * Copyright (C) 1992 Darren Senn
  5. */
  6. /* These are all the functions necessary to implement itimers */
  7. #include <linux/mm.h>
  8. #include <linux/smp_lock.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/syscalls.h>
  11. #include <linux/time.h>
  12. #include <linux/posix-timers.h>
  13. #include <linux/hrtimer.h>
  14. #include <asm/uaccess.h>
  15. /**
  16. * itimer_get_remtime - get remaining time for the timer
  17. *
  18. * @timer: the timer to read
  19. *
  20. * Returns the delta between the expiry time and now, which can be
  21. * less than zero or 1usec for an pending expired timer
  22. */
  23. static struct timeval itimer_get_remtime(struct hrtimer *timer)
  24. {
  25. ktime_t rem = hrtimer_get_remaining(timer);
  26. /*
  27. * Racy but safe: if the itimer expires after the above
  28. * hrtimer_get_remtime() call but before this condition
  29. * then we return 0 - which is correct.
  30. */
  31. if (hrtimer_active(timer)) {
  32. if (rem.tv64 <= 0)
  33. rem.tv64 = NSEC_PER_USEC;
  34. } else
  35. rem.tv64 = 0;
  36. return ktime_to_timeval(rem);
  37. }
  38. int do_getitimer(int which, struct itimerval *value)
  39. {
  40. struct task_struct *tsk = current;
  41. cputime_t cinterval, cval;
  42. switch (which) {
  43. case ITIMER_REAL:
  44. value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
  45. value->it_interval =
  46. ktime_to_timeval(tsk->signal->it_real_incr);
  47. break;
  48. case ITIMER_VIRTUAL:
  49. read_lock(&tasklist_lock);
  50. spin_lock_irq(&tsk->sighand->siglock);
  51. cval = tsk->signal->it_virt_expires;
  52. cinterval = tsk->signal->it_virt_incr;
  53. if (!cputime_eq(cval, cputime_zero)) {
  54. struct task_struct *t = tsk;
  55. cputime_t utime = tsk->signal->utime;
  56. do {
  57. utime = cputime_add(utime, t->utime);
  58. t = next_thread(t);
  59. } while (t != tsk);
  60. if (cputime_le(cval, utime)) { /* about to fire */
  61. cval = jiffies_to_cputime(1);
  62. } else {
  63. cval = cputime_sub(cval, utime);
  64. }
  65. }
  66. spin_unlock_irq(&tsk->sighand->siglock);
  67. read_unlock(&tasklist_lock);
  68. cputime_to_timeval(cval, &value->it_value);
  69. cputime_to_timeval(cinterval, &value->it_interval);
  70. break;
  71. case ITIMER_PROF:
  72. read_lock(&tasklist_lock);
  73. spin_lock_irq(&tsk->sighand->siglock);
  74. cval = tsk->signal->it_prof_expires;
  75. cinterval = tsk->signal->it_prof_incr;
  76. if (!cputime_eq(cval, cputime_zero)) {
  77. struct task_struct *t = tsk;
  78. cputime_t ptime = cputime_add(tsk->signal->utime,
  79. tsk->signal->stime);
  80. do {
  81. ptime = cputime_add(ptime,
  82. cputime_add(t->utime,
  83. t->stime));
  84. t = next_thread(t);
  85. } while (t != tsk);
  86. if (cputime_le(cval, ptime)) { /* about to fire */
  87. cval = jiffies_to_cputime(1);
  88. } else {
  89. cval = cputime_sub(cval, ptime);
  90. }
  91. }
  92. spin_unlock_irq(&tsk->sighand->siglock);
  93. read_unlock(&tasklist_lock);
  94. cputime_to_timeval(cval, &value->it_value);
  95. cputime_to_timeval(cinterval, &value->it_interval);
  96. break;
  97. default:
  98. return(-EINVAL);
  99. }
  100. return 0;
  101. }
  102. asmlinkage long sys_getitimer(int which, struct itimerval __user *value)
  103. {
  104. int error = -EFAULT;
  105. struct itimerval get_buffer;
  106. if (value) {
  107. error = do_getitimer(which, &get_buffer);
  108. if (!error &&
  109. copy_to_user(value, &get_buffer, sizeof(get_buffer)))
  110. error = -EFAULT;
  111. }
  112. return error;
  113. }
  114. /*
  115. * The timer is automagically restarted, when interval != 0
  116. */
  117. int it_real_fn(void *data)
  118. {
  119. struct task_struct *tsk = (struct task_struct *) data;
  120. send_group_sig_info(SIGALRM, SEND_SIG_PRIV, tsk);
  121. if (tsk->signal->it_real_incr.tv64 != 0) {
  122. hrtimer_forward(&tsk->signal->real_timer,
  123. tsk->signal->it_real_incr);
  124. return HRTIMER_RESTART;
  125. }
  126. return HRTIMER_NORESTART;
  127. }
  128. int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  129. {
  130. struct task_struct *tsk = current;
  131. struct hrtimer *timer;
  132. ktime_t expires;
  133. cputime_t cval, cinterval, nval, ninterval;
  134. switch (which) {
  135. case ITIMER_REAL:
  136. timer = &tsk->signal->real_timer;
  137. hrtimer_cancel(timer);
  138. if (ovalue) {
  139. ovalue->it_value = itimer_get_remtime(timer);
  140. ovalue->it_interval
  141. = ktime_to_timeval(tsk->signal->it_real_incr);
  142. }
  143. tsk->signal->it_real_incr =
  144. timeval_to_ktime(value->it_interval);
  145. expires = timeval_to_ktime(value->it_value);
  146. if (expires.tv64 != 0)
  147. hrtimer_start(timer, expires, HRTIMER_REL);
  148. break;
  149. case ITIMER_VIRTUAL:
  150. nval = timeval_to_cputime(&value->it_value);
  151. ninterval = timeval_to_cputime(&value->it_interval);
  152. read_lock(&tasklist_lock);
  153. spin_lock_irq(&tsk->sighand->siglock);
  154. cval = tsk->signal->it_virt_expires;
  155. cinterval = tsk->signal->it_virt_incr;
  156. if (!cputime_eq(cval, cputime_zero) ||
  157. !cputime_eq(nval, cputime_zero)) {
  158. if (cputime_gt(nval, cputime_zero))
  159. nval = cputime_add(nval,
  160. jiffies_to_cputime(1));
  161. set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
  162. &nval, &cval);
  163. }
  164. tsk->signal->it_virt_expires = nval;
  165. tsk->signal->it_virt_incr = ninterval;
  166. spin_unlock_irq(&tsk->sighand->siglock);
  167. read_unlock(&tasklist_lock);
  168. if (ovalue) {
  169. cputime_to_timeval(cval, &ovalue->it_value);
  170. cputime_to_timeval(cinterval, &ovalue->it_interval);
  171. }
  172. break;
  173. case ITIMER_PROF:
  174. nval = timeval_to_cputime(&value->it_value);
  175. ninterval = timeval_to_cputime(&value->it_interval);
  176. read_lock(&tasklist_lock);
  177. spin_lock_irq(&tsk->sighand->siglock);
  178. cval = tsk->signal->it_prof_expires;
  179. cinterval = tsk->signal->it_prof_incr;
  180. if (!cputime_eq(cval, cputime_zero) ||
  181. !cputime_eq(nval, cputime_zero)) {
  182. if (cputime_gt(nval, cputime_zero))
  183. nval = cputime_add(nval,
  184. jiffies_to_cputime(1));
  185. set_process_cpu_timer(tsk, CPUCLOCK_PROF,
  186. &nval, &cval);
  187. }
  188. tsk->signal->it_prof_expires = nval;
  189. tsk->signal->it_prof_incr = ninterval;
  190. spin_unlock_irq(&tsk->sighand->siglock);
  191. read_unlock(&tasklist_lock);
  192. if (ovalue) {
  193. cputime_to_timeval(cval, &ovalue->it_value);
  194. cputime_to_timeval(cinterval, &ovalue->it_interval);
  195. }
  196. break;
  197. default:
  198. return -EINVAL;
  199. }
  200. return 0;
  201. }
  202. asmlinkage long sys_setitimer(int which,
  203. struct itimerval __user *value,
  204. struct itimerval __user *ovalue)
  205. {
  206. struct itimerval set_buffer, get_buffer;
  207. int error;
  208. if (value) {
  209. if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
  210. return -EFAULT;
  211. } else
  212. memset((char *) &set_buffer, 0, sizeof(set_buffer));
  213. error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
  214. if (error || !ovalue)
  215. return error;
  216. if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
  217. return -EFAULT;
  218. return 0;
  219. }