time.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/kernel.h"
  6. #include "linux/module.h"
  7. #include "linux/unistd.h"
  8. #include "linux/stddef.h"
  9. #include "linux/spinlock.h"
  10. #include "linux/time.h"
  11. #include "linux/sched.h"
  12. #include "linux/interrupt.h"
  13. #include "linux/init.h"
  14. #include "linux/delay.h"
  15. #include "linux/hrtimer.h"
  16. #include "asm/irq.h"
  17. #include "asm/param.h"
  18. #include "asm/current.h"
  19. #include "kern_util.h"
  20. #include "user_util.h"
  21. #include "mode.h"
  22. #include "os.h"
  23. int hz(void)
  24. {
  25. return(HZ);
  26. }
  27. /*
  28. * Scheduler clock - returns current time in nanosec units.
  29. */
  30. unsigned long long sched_clock(void)
  31. {
  32. return (unsigned long long)jiffies_64 * (1000000000 / HZ);
  33. }
  34. static unsigned long long prev_nsecs;
  35. #ifdef CONFIG_UML_REAL_TIME_CLOCK
  36. static long long delta; /* Deviation per interval */
  37. #endif
  38. void timer_irq(union uml_pt_regs *regs)
  39. {
  40. unsigned long long ticks = 0;
  41. #ifdef CONFIG_UML_REAL_TIME_CLOCK
  42. if(prev_nsecs){
  43. /* We've had 1 tick */
  44. unsigned long long nsecs = os_nsecs();
  45. delta += nsecs - prev_nsecs;
  46. prev_nsecs = nsecs;
  47. /* Protect against the host clock being set backwards */
  48. if(delta < 0)
  49. delta = 0;
  50. ticks += (delta * HZ) / BILLION;
  51. delta -= (ticks * BILLION) / HZ;
  52. }
  53. else prev_nsecs = os_nsecs();
  54. #else
  55. ticks = 1;
  56. #endif
  57. while(ticks > 0){
  58. do_IRQ(TIMER_IRQ, regs);
  59. ticks--;
  60. }
  61. }
  62. static DEFINE_SPINLOCK(timer_spinlock);
  63. static unsigned long long local_offset = 0;
  64. static inline unsigned long long get_time(void)
  65. {
  66. unsigned long long nsecs;
  67. unsigned long flags;
  68. spin_lock_irqsave(&timer_spinlock, flags);
  69. nsecs = os_nsecs();
  70. nsecs += local_offset;
  71. spin_unlock_irqrestore(&timer_spinlock, flags);
  72. return nsecs;
  73. }
  74. irqreturn_t um_timer(int irq, void *dev)
  75. {
  76. unsigned long long nsecs;
  77. unsigned long flags;
  78. write_seqlock_irqsave(&xtime_lock, flags);
  79. do_timer(1);
  80. nsecs = get_time();
  81. xtime.tv_sec = nsecs / NSEC_PER_SEC;
  82. xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
  83. write_sequnlock_irqrestore(&xtime_lock, flags);
  84. return IRQ_HANDLED;
  85. }
  86. static void register_timer(void)
  87. {
  88. int err;
  89. err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
  90. if(err != 0)
  91. printk(KERN_ERR "register_timer : request_irq failed - "
  92. "errno = %d\n", -err);
  93. err = set_interval(1);
  94. if(err != 0)
  95. printk(KERN_ERR "register_timer : set_interval failed - "
  96. "errno = %d\n", -err);
  97. }
  98. extern void (*late_time_init)(void);
  99. void time_init(void)
  100. {
  101. long long nsecs;
  102. nsecs = os_nsecs();
  103. set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
  104. -nsecs % BILLION);
  105. late_time_init = register_timer;
  106. }
  107. void do_gettimeofday(struct timeval *tv)
  108. {
  109. unsigned long long nsecs = get_time();
  110. tv->tv_sec = nsecs / NSEC_PER_SEC;
  111. /* Careful about calculations here - this was originally done as
  112. * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
  113. * which gave bogus (> 1000000) values. Dunno why, suspect gcc
  114. * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
  115. * problem that I missed.
  116. */
  117. nsecs -= tv->tv_sec * NSEC_PER_SEC;
  118. tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
  119. }
  120. static inline void set_time(unsigned long long nsecs)
  121. {
  122. unsigned long long now;
  123. unsigned long flags;
  124. spin_lock_irqsave(&timer_spinlock, flags);
  125. now = os_nsecs();
  126. local_offset = nsecs - now;
  127. spin_unlock_irqrestore(&timer_spinlock, flags);
  128. clock_was_set();
  129. }
  130. int do_settimeofday(struct timespec *tv)
  131. {
  132. set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
  133. return 0;
  134. }
  135. void timer_handler(int sig, union uml_pt_regs *regs)
  136. {
  137. local_irq_disable();
  138. irq_enter();
  139. update_process_times(CHOOSE_MODE(
  140. (UPT_SC(regs) && user_context(UPT_SP(regs))),
  141. (regs)->skas.is_user));
  142. irq_exit();
  143. local_irq_enable();
  144. if(current_thread->cpu == 0)
  145. timer_irq(regs);
  146. }