time.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. void time_init(void)
  73. {
  74. if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
  75. panic("Couldn't set SIGVTALRM handler");
  76. set_interval(ITIMER_VIRTUAL);
  77. time_init_kern();
  78. }
  79. unsigned long long os_nsecs(void)
  80. {
  81. struct timeval tv;
  82. gettimeofday(&tv, NULL);
  83. return((unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000);
  84. }
  85. void idle_sleep(int secs)
  86. {
  87. struct timespec ts;
  88. ts.tv_sec = secs;
  89. ts.tv_nsec = 0;
  90. nanosleep(&ts, NULL);
  91. }
  92. /* XXX This partly duplicates init_irq_signals */
  93. void user_time_init(void)
  94. {
  95. set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
  96. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
  97. SIGALRM, SIGUSR2, -1);
  98. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  99. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
  100. SIGVTALRM, SIGUSR2, -1);
  101. set_interval(ITIMER_VIRTUAL);
  102. }