time.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "signal_user.h"
  17. #include "time_user.h"
  18. #include "kern_constants.h"
  19. /* XXX This really needs to be declared and initialized in a kernel file since
  20. * it's in <linux/time.h>
  21. */
  22. extern struct timespec wall_to_monotonic;
  23. extern struct timeval xtime;
  24. struct timeval local_offset = { 0, 0 };
  25. void timer(void)
  26. {
  27. gettimeofday(&xtime, NULL);
  28. timeradd(&xtime, &local_offset, &xtime);
  29. }
  30. static void set_interval(int timer_type)
  31. {
  32. int usec = 1000000/hz();
  33. struct itimerval interval = ((struct itimerval) { { 0, usec },
  34. { 0, usec } });
  35. if(setitimer(timer_type, &interval, NULL) == -1)
  36. panic("setitimer failed - errno = %d\n", errno);
  37. }
  38. void enable_timer(void)
  39. {
  40. set_interval(ITIMER_VIRTUAL);
  41. }
  42. void disable_timer(void)
  43. {
  44. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  45. if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
  46. (setitimer(ITIMER_REAL, &disable, NULL) < 0))
  47. printk("disnable_timer - setitimer failed, errno = %d\n",
  48. errno);
  49. /* If there are signals already queued, after unblocking ignore them */
  50. set_handler(SIGALRM, SIG_IGN, 0, -1);
  51. set_handler(SIGVTALRM, SIG_IGN, 0, -1);
  52. }
  53. void switch_timers(int to_real)
  54. {
  55. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  56. struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
  57. { 0, 1000000/hz() }});
  58. int old, new;
  59. if(to_real){
  60. old = ITIMER_VIRTUAL;
  61. new = ITIMER_REAL;
  62. }
  63. else {
  64. old = ITIMER_REAL;
  65. new = ITIMER_VIRTUAL;
  66. }
  67. if((setitimer(old, &disable, NULL) < 0) ||
  68. (setitimer(new, &enable, NULL)))
  69. printk("switch_timers - setitimer failed, errno = %d\n",
  70. errno);
  71. }
  72. void uml_idle_timer(void)
  73. {
  74. if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
  75. panic("Couldn't unset SIGVTALRM handler");
  76. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  77. SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
  78. set_interval(ITIMER_REAL);
  79. }
  80. extern int do_posix_clock_monotonic_gettime(struct timespec *tp);
  81. void time_init(void)
  82. {
  83. struct timespec now;
  84. if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
  85. panic("Couldn't set SIGVTALRM handler");
  86. set_interval(ITIMER_VIRTUAL);
  87. do_posix_clock_monotonic_gettime(&now);
  88. wall_to_monotonic.tv_sec = -now.tv_sec;
  89. wall_to_monotonic.tv_nsec = -now.tv_nsec;
  90. }
  91. /* Declared in linux/time.h, which can't be included here */
  92. extern void clock_was_set(void);
  93. void do_gettimeofday(struct timeval *tv)
  94. {
  95. unsigned long flags;
  96. flags = time_lock();
  97. gettimeofday(tv, NULL);
  98. timeradd(tv, &local_offset, tv);
  99. time_unlock(flags);
  100. clock_was_set();
  101. }
  102. int do_settimeofday(struct timespec *tv)
  103. {
  104. struct timeval now;
  105. unsigned long flags;
  106. struct timeval tv_in;
  107. if ((unsigned long) tv->tv_nsec >= UM_NSEC_PER_SEC)
  108. return -EINVAL;
  109. tv_in.tv_sec = tv->tv_sec;
  110. tv_in.tv_usec = tv->tv_nsec / 1000;
  111. flags = time_lock();
  112. gettimeofday(&now, NULL);
  113. timersub(&tv_in, &now, &local_offset);
  114. time_unlock(flags);
  115. return(0);
  116. }
  117. void idle_sleep(int secs)
  118. {
  119. struct timespec ts;
  120. ts.tv_sec = secs;
  121. ts.tv_nsec = 0;
  122. nanosleep(&ts, NULL);
  123. }
  124. /* XXX This partly duplicates init_irq_signals */
  125. void user_time_init(void)
  126. {
  127. set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
  128. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
  129. SIGALRM, SIGUSR2, -1);
  130. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  131. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
  132. SIGVTALRM, SIGUSR2, -1);
  133. set_interval(ITIMER_VIRTUAL);
  134. }