time.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 prepare_timer(void * ptr)
  43. {
  44. int usec = 1000000/hz();
  45. *(struct itimerval *)ptr = ((struct itimerval) { { 0, usec },
  46. { 0, usec }});
  47. }
  48. void disable_timer(void)
  49. {
  50. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  51. if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
  52. (setitimer(ITIMER_REAL, &disable, NULL) < 0))
  53. printk("disnable_timer - setitimer failed, errno = %d\n",
  54. errno);
  55. /* If there are signals already queued, after unblocking ignore them */
  56. set_handler(SIGALRM, SIG_IGN, 0, -1);
  57. set_handler(SIGVTALRM, SIG_IGN, 0, -1);
  58. }
  59. void switch_timers(int to_real)
  60. {
  61. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  62. struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
  63. { 0, 1000000/hz() }});
  64. int old, new;
  65. if(to_real){
  66. old = ITIMER_VIRTUAL;
  67. new = ITIMER_REAL;
  68. }
  69. else {
  70. old = ITIMER_REAL;
  71. new = ITIMER_VIRTUAL;
  72. }
  73. if((setitimer(old, &disable, NULL) < 0) ||
  74. (setitimer(new, &enable, NULL)))
  75. printk("switch_timers - setitimer failed, errno = %d\n",
  76. errno);
  77. }
  78. void uml_idle_timer(void)
  79. {
  80. if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
  81. panic("Couldn't unset SIGVTALRM handler");
  82. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  83. SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
  84. set_interval(ITIMER_REAL);
  85. }
  86. extern int do_posix_clock_monotonic_gettime(struct timespec *tp);
  87. void time_init(void)
  88. {
  89. struct timespec now;
  90. if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
  91. panic("Couldn't set SIGVTALRM handler");
  92. set_interval(ITIMER_VIRTUAL);
  93. do_posix_clock_monotonic_gettime(&now);
  94. wall_to_monotonic.tv_sec = -now.tv_sec;
  95. wall_to_monotonic.tv_nsec = -now.tv_nsec;
  96. }
  97. /* Declared in linux/time.h, which can't be included here */
  98. extern void clock_was_set(void);
  99. void do_gettimeofday(struct timeval *tv)
  100. {
  101. unsigned long flags;
  102. flags = time_lock();
  103. gettimeofday(tv, NULL);
  104. timeradd(tv, &local_offset, tv);
  105. time_unlock(flags);
  106. clock_was_set();
  107. }
  108. int do_settimeofday(struct timespec *tv)
  109. {
  110. struct timeval now;
  111. unsigned long flags;
  112. struct timeval tv_in;
  113. if ((unsigned long) tv->tv_nsec >= UM_NSEC_PER_SEC)
  114. return -EINVAL;
  115. tv_in.tv_sec = tv->tv_sec;
  116. tv_in.tv_usec = tv->tv_nsec / 1000;
  117. flags = time_lock();
  118. gettimeofday(&now, NULL);
  119. timersub(&tv_in, &now, &local_offset);
  120. time_unlock(flags);
  121. return(0);
  122. }
  123. void idle_sleep(int secs)
  124. {
  125. struct timespec ts;
  126. ts.tv_sec = secs;
  127. ts.tv_nsec = 0;
  128. nanosleep(&ts, NULL);
  129. }
  130. /* XXX This partly duplicates init_irq_signals */
  131. void user_time_init(void)
  132. {
  133. set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
  134. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
  135. SIGALRM, SIGUSR2, -1);
  136. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  137. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
  138. SIGVTALRM, SIGUSR2, -1);
  139. set_interval(ITIMER_VIRTUAL);
  140. }