time_kern.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 do_boot_timer_handler(struct sigcontext * sc)
  75. {
  76. struct pt_regs regs;
  77. CHOOSE_MODE((void) (UPT_SC(&regs.regs) = sc),
  78. (void) (regs.regs.skas.is_user = 0));
  79. do_timer(&regs);
  80. }
  81. static DEFINE_SPINLOCK(timer_spinlock);
  82. static unsigned long long local_offset = 0;
  83. static inline unsigned long long get_time(void)
  84. {
  85. unsigned long long nsecs;
  86. unsigned long flags;
  87. spin_lock_irqsave(&timer_spinlock, flags);
  88. nsecs = os_nsecs();
  89. nsecs += local_offset;
  90. spin_unlock_irqrestore(&timer_spinlock, flags);
  91. return nsecs;
  92. }
  93. irqreturn_t um_timer(int irq, void *dev, struct pt_regs *regs)
  94. {
  95. unsigned long long nsecs;
  96. unsigned long flags;
  97. do_timer(regs);
  98. write_seqlock_irqsave(&xtime_lock, flags);
  99. nsecs = get_time() + local_offset;
  100. xtime.tv_sec = nsecs / NSEC_PER_SEC;
  101. xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
  102. write_sequnlock_irqrestore(&xtime_lock, flags);
  103. return(IRQ_HANDLED);
  104. }
  105. long um_time(int __user *tloc)
  106. {
  107. long ret = get_time() / NSEC_PER_SEC;
  108. if((tloc != NULL) && put_user(ret, tloc))
  109. return -EFAULT;
  110. return ret;
  111. }
  112. void do_gettimeofday(struct timeval *tv)
  113. {
  114. unsigned long long nsecs = get_time();
  115. tv->tv_sec = nsecs / NSEC_PER_SEC;
  116. /* Careful about calculations here - this was originally done as
  117. * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
  118. * which gave bogus (> 1000000) values. Dunno why, suspect gcc
  119. * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
  120. * problem that I missed.
  121. */
  122. nsecs -= tv->tv_sec * NSEC_PER_SEC;
  123. tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
  124. }
  125. static inline void set_time(unsigned long long nsecs)
  126. {
  127. unsigned long long now;
  128. unsigned long flags;
  129. spin_lock_irqsave(&timer_spinlock, flags);
  130. now = os_nsecs();
  131. local_offset = nsecs - now;
  132. spin_unlock_irqrestore(&timer_spinlock, flags);
  133. clock_was_set();
  134. }
  135. long um_stime(int __user *tptr)
  136. {
  137. int value;
  138. if (get_user(value, tptr))
  139. return -EFAULT;
  140. set_time((unsigned long long) value * NSEC_PER_SEC);
  141. return 0;
  142. }
  143. int do_settimeofday(struct timespec *tv)
  144. {
  145. set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
  146. return 0;
  147. }
  148. void timer_handler(int sig, union uml_pt_regs *regs)
  149. {
  150. local_irq_disable();
  151. irq_enter();
  152. update_process_times(CHOOSE_MODE(
  153. (UPT_SC(regs) && user_context(UPT_SP(regs))),
  154. (regs)->skas.is_user));
  155. irq_exit();
  156. local_irq_enable();
  157. if(current_thread->cpu == 0)
  158. timer_irq(regs);
  159. }
  160. int __init timer_init(void)
  161. {
  162. int err;
  163. user_time_init();
  164. err = request_irq(TIMER_IRQ, um_timer, SA_INTERRUPT, "timer", NULL);
  165. if(err != 0)
  166. printk(KERN_ERR "timer_init : request_irq failed - "
  167. "errno = %d\n", -err);
  168. timer_irq_inited = 1;
  169. return(0);
  170. }
  171. __initcall(timer_init);