time.c 3.8 KB

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