time.c 4.2 KB

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