time.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <time.h>
  9. #include <sys/time.h>
  10. #include <signal.h>
  11. #include <errno.h>
  12. #include "user_util.h"
  13. #include "kern_util.h"
  14. #include "user.h"
  15. #include "process.h"
  16. #include "kern_constants.h"
  17. #include "os.h"
  18. /* XXX This really needs to be declared and initialized in a kernel file since
  19. * it's in <linux/time.h>
  20. */
  21. extern struct timespec wall_to_monotonic;
  22. static void set_interval(int timer_type)
  23. {
  24. int usec = 1000000/hz();
  25. struct itimerval interval = ((struct itimerval) { { 0, usec },
  26. { 0, usec } });
  27. if(setitimer(timer_type, &interval, NULL) == -1)
  28. panic("setitimer failed - errno = %d\n", errno);
  29. }
  30. void enable_timer(void)
  31. {
  32. set_interval(ITIMER_VIRTUAL);
  33. }
  34. void disable_timer(void)
  35. {
  36. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  37. if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
  38. (setitimer(ITIMER_REAL, &disable, NULL) < 0))
  39. printk("disnable_timer - setitimer failed, errno = %d\n",
  40. errno);
  41. /* If there are signals already queued, after unblocking ignore them */
  42. set_handler(SIGALRM, SIG_IGN, 0, -1);
  43. set_handler(SIGVTALRM, SIG_IGN, 0, -1);
  44. }
  45. void switch_timers(int to_real)
  46. {
  47. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  48. struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
  49. { 0, 1000000/hz() }});
  50. int old, new;
  51. if(to_real){
  52. old = ITIMER_VIRTUAL;
  53. new = ITIMER_REAL;
  54. }
  55. else {
  56. old = ITIMER_REAL;
  57. new = ITIMER_VIRTUAL;
  58. }
  59. if((setitimer(old, &disable, NULL) < 0) ||
  60. (setitimer(new, &enable, NULL)))
  61. printk("switch_timers - setitimer failed, errno = %d\n",
  62. errno);
  63. }
  64. void uml_idle_timer(void)
  65. {
  66. if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
  67. panic("Couldn't unset SIGVTALRM handler");
  68. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  69. SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
  70. set_interval(ITIMER_REAL);
  71. }
  72. extern void ktime_get_ts(struct timespec *ts);
  73. #define do_posix_clock_monotonic_gettime(ts) ktime_get_ts(ts)
  74. void time_init(void)
  75. {
  76. struct timespec now;
  77. if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
  78. panic("Couldn't set SIGVTALRM handler");
  79. set_interval(ITIMER_VIRTUAL);
  80. do_posix_clock_monotonic_gettime(&now);
  81. wall_to_monotonic.tv_sec = -now.tv_sec;
  82. wall_to_monotonic.tv_nsec = -now.tv_nsec;
  83. }
  84. unsigned long long os_nsecs(void)
  85. {
  86. struct timeval tv;
  87. gettimeofday(&tv, NULL);
  88. return((unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000);
  89. }
  90. void idle_sleep(int secs)
  91. {
  92. struct timespec ts;
  93. ts.tv_sec = secs;
  94. ts.tv_nsec = 0;
  95. nanosleep(&ts, NULL);
  96. }
  97. /* XXX This partly duplicates init_irq_signals */
  98. void user_time_init(void)
  99. {
  100. set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
  101. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
  102. SIGALRM, SIGUSR2, -1);
  103. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  104. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
  105. SIGVTALRM, SIGUSR2, -1);
  106. set_interval(ITIMER_VIRTUAL);
  107. }