time_kern.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 int first_tick;
  37. static unsigned long long prev_nsecs;
  38. #ifdef CONFIG_UML_REAL_TIME_CLOCK
  39. static long long delta; /* Deviation per interval */
  40. #endif
  41. void timer_irq(union uml_pt_regs *regs)
  42. {
  43. unsigned long long ticks = 0;
  44. if(!timer_irq_inited){
  45. /* This is to ensure that ticks don't pile up when
  46. * the timer handler is suspended */
  47. first_tick = 0;
  48. return;
  49. }
  50. if(first_tick){
  51. #ifdef CONFIG_UML_REAL_TIME_CLOCK
  52. /* We've had 1 tick */
  53. unsigned long long nsecs = os_nsecs();
  54. delta += nsecs - prev_nsecs;
  55. prev_nsecs = nsecs;
  56. /* Protect against the host clock being set backwards */
  57. if(delta < 0)
  58. delta = 0;
  59. ticks += (delta * HZ) / BILLION;
  60. delta -= (ticks * BILLION) / HZ;
  61. #else
  62. ticks = 1;
  63. #endif
  64. }
  65. else {
  66. prev_nsecs = os_nsecs();
  67. first_tick = 1;
  68. }
  69. while(ticks > 0){
  70. do_IRQ(TIMER_IRQ, regs);
  71. ticks--;
  72. }
  73. }
  74. void time_init_kern(void)
  75. {
  76. unsigned long long nsecs;
  77. nsecs = os_nsecs();
  78. set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
  79. -nsecs % BILLION);
  80. }
  81. void do_boot_timer_handler(struct sigcontext * sc)
  82. {
  83. struct pt_regs regs;
  84. CHOOSE_MODE((void) (UPT_SC(&regs.regs) = sc),
  85. (void) (regs.regs.skas.is_user = 0));
  86. do_timer(&regs);
  87. }
  88. static DEFINE_SPINLOCK(timer_spinlock);
  89. static unsigned long long local_offset = 0;
  90. static inline unsigned long long get_time(void)
  91. {
  92. unsigned long long nsecs;
  93. unsigned long flags;
  94. spin_lock_irqsave(&timer_spinlock, flags);
  95. nsecs = os_nsecs();
  96. nsecs += local_offset;
  97. spin_unlock_irqrestore(&timer_spinlock, flags);
  98. return nsecs;
  99. }
  100. irqreturn_t um_timer(int irq, void *dev, struct pt_regs *regs)
  101. {
  102. unsigned long long nsecs;
  103. unsigned long flags;
  104. do_timer(regs);
  105. write_seqlock_irqsave(&xtime_lock, flags);
  106. nsecs = get_time() + local_offset;
  107. xtime.tv_sec = nsecs / NSEC_PER_SEC;
  108. xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
  109. write_sequnlock_irqrestore(&xtime_lock, flags);
  110. return(IRQ_HANDLED);
  111. }
  112. long um_time(int __user *tloc)
  113. {
  114. long ret = get_time() / NSEC_PER_SEC;
  115. if((tloc != NULL) && put_user(ret, tloc))
  116. return -EFAULT;
  117. return ret;
  118. }
  119. void do_gettimeofday(struct timeval *tv)
  120. {
  121. unsigned long long nsecs = get_time();
  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. long um_stime(int __user *tptr)
  143. {
  144. int value;
  145. if (get_user(value, tptr))
  146. return -EFAULT;
  147. set_time((unsigned long long) value * NSEC_PER_SEC);
  148. return 0;
  149. }
  150. int do_settimeofday(struct timespec *tv)
  151. {
  152. set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
  153. return 0;
  154. }
  155. void timer_handler(int sig, union uml_pt_regs *regs)
  156. {
  157. local_irq_disable();
  158. irq_enter();
  159. update_process_times(CHOOSE_MODE(
  160. (UPT_SC(regs) && user_context(UPT_SP(regs))),
  161. (regs)->skas.is_user));
  162. irq_exit();
  163. local_irq_enable();
  164. if(current_thread->cpu == 0)
  165. timer_irq(regs);
  166. }
  167. int __init timer_init(void)
  168. {
  169. int err;
  170. user_time_init();
  171. err = request_irq(TIMER_IRQ, um_timer, SA_INTERRUPT, "timer", NULL);
  172. if(err != 0)
  173. printk(KERN_ERR "timer_init : request_irq failed - "
  174. "errno = %d\n", -err);
  175. timer_irq_inited = 1;
  176. return(0);
  177. }
  178. arch_initcall(timer_init);