time.c 3.9 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[NR_CPUS];
  35. #ifdef CONFIG_UML_REAL_TIME_CLOCK
  36. static long long delta[NR_CPUS]; /* 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. int c = cpu();
  43. if(prev_nsecs[c]){
  44. /* We've had 1 tick */
  45. unsigned long long nsecs = os_nsecs();
  46. delta[c] += nsecs - prev_nsecs[c];
  47. prev_nsecs[c] = nsecs;
  48. /* Protect against the host clock being set backwards */
  49. if(delta[c] < 0)
  50. delta[c] = 0;
  51. ticks += (delta[c] * HZ) / BILLION;
  52. delta[c] -= (ticks * BILLION) / HZ;
  53. }
  54. else prev_nsecs[c] = os_nsecs();
  55. #else
  56. ticks = 1;
  57. #endif
  58. while(ticks > 0){
  59. do_IRQ(TIMER_IRQ, regs);
  60. ticks--;
  61. }
  62. }
  63. /* Protects local_offset */
  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)
  77. {
  78. unsigned long long nsecs;
  79. unsigned long flags;
  80. write_seqlock_irqsave(&xtime_lock, flags);
  81. do_timer(1);
  82. nsecs = get_time();
  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 "register_timer : request_irq failed - "
  94. "errno = %d\n", -err);
  95. err = set_interval(1);
  96. if(err != 0)
  97. printk(KERN_ERR "register_timer : set_interval failed - "
  98. "errno = %d\n", -err);
  99. }
  100. extern void (*late_time_init)(void);
  101. void time_init(void)
  102. {
  103. long long nsecs;
  104. nsecs = os_nsecs();
  105. set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
  106. -nsecs % BILLION);
  107. late_time_init = register_timer;
  108. }
  109. void do_gettimeofday(struct timeval *tv)
  110. {
  111. unsigned long long nsecs = get_time();
  112. tv->tv_sec = nsecs / NSEC_PER_SEC;
  113. /* Careful about calculations here - this was originally done as
  114. * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
  115. * which gave bogus (> 1000000) values. Dunno why, suspect gcc
  116. * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
  117. * problem that I missed.
  118. */
  119. nsecs -= tv->tv_sec * NSEC_PER_SEC;
  120. tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
  121. }
  122. static inline void set_time(unsigned long long nsecs)
  123. {
  124. unsigned long long now;
  125. unsigned long flags;
  126. spin_lock_irqsave(&timer_spinlock, flags);
  127. now = os_nsecs();
  128. local_offset = nsecs - now;
  129. spin_unlock_irqrestore(&timer_spinlock, flags);
  130. clock_was_set();
  131. }
  132. int do_settimeofday(struct timespec *tv)
  133. {
  134. set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
  135. return 0;
  136. }
  137. void timer_handler(int sig, union uml_pt_regs *regs)
  138. {
  139. local_irq_disable();
  140. irq_enter();
  141. update_process_times(CHOOSE_MODE(
  142. (UPT_SC(regs) && user_context(UPT_SP(regs))),
  143. (regs)->skas.is_user));
  144. irq_exit();
  145. local_irq_enable();
  146. if(current_thread->cpu == 0)
  147. timer_irq(regs);
  148. }