time.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /* Changed at early boot */
  35. int timer_irq_inited = 0;
  36. static unsigned long long prev_nsecs;
  37. #ifdef CONFIG_UML_REAL_TIME_CLOCK
  38. static long long delta; /* Deviation per interval */
  39. #endif
  40. void timer_irq(union uml_pt_regs *regs)
  41. {
  42. unsigned long long ticks = 0;
  43. #ifdef CONFIG_UML_REAL_TIME_CLOCK
  44. if(prev_nsecs){
  45. /* We've had 1 tick */
  46. unsigned long long nsecs = os_nsecs();
  47. delta += nsecs - prev_nsecs;
  48. prev_nsecs = nsecs;
  49. /* Protect against the host clock being set backwards */
  50. if(delta < 0)
  51. delta = 0;
  52. ticks += (delta * HZ) / BILLION;
  53. delta -= (ticks * BILLION) / HZ;
  54. }
  55. else prev_nsecs = os_nsecs();
  56. #else
  57. ticks = 1;
  58. #endif
  59. while(ticks > 0){
  60. do_IRQ(TIMER_IRQ, regs);
  61. ticks--;
  62. }
  63. }
  64. static DEFINE_SPINLOCK(timer_spinlock);
  65. static unsigned long long local_offset = 0;
  66. static inline unsigned long long get_time(void)
  67. {
  68. unsigned long long nsecs;
  69. unsigned long flags;
  70. spin_lock_irqsave(&timer_spinlock, flags);
  71. nsecs = os_nsecs();
  72. nsecs += local_offset;
  73. spin_unlock_irqrestore(&timer_spinlock, flags);
  74. return nsecs;
  75. }
  76. irqreturn_t um_timer(int irq, void *dev, struct pt_regs *regs)
  77. {
  78. unsigned long long nsecs;
  79. unsigned long flags;
  80. write_seqlock_irqsave(&xtime_lock, flags);
  81. do_timer(regs);
  82. nsecs = get_time() + local_offset;
  83. xtime.tv_sec = nsecs / NSEC_PER_SEC;
  84. xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
  85. write_sequnlock_irqrestore(&xtime_lock, flags);
  86. return IRQ_HANDLED;
  87. }
  88. static void register_timer(void)
  89. {
  90. int err;
  91. err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
  92. if(err != 0)
  93. printk(KERN_ERR "timer_init : request_irq failed - "
  94. "errno = %d\n", -err);
  95. timer_irq_inited = 1;
  96. user_time_init();
  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. }