time_kern.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "asm/irq.h"
  16. #include "asm/param.h"
  17. #include "asm/current.h"
  18. #include "kern_util.h"
  19. #include "user_util.h"
  20. #include "time_user.h"
  21. #include "mode.h"
  22. #include "os.h"
  23. u64 jiffies_64 = INITIAL_JIFFIES;
  24. EXPORT_SYMBOL(jiffies_64);
  25. int hz(void)
  26. {
  27. return(HZ);
  28. }
  29. /*
  30. * Scheduler clock - returns current time in nanosec units.
  31. */
  32. unsigned long long sched_clock(void)
  33. {
  34. return (unsigned long long)jiffies_64 * (1000000000 / HZ);
  35. }
  36. /* Changed at early boot */
  37. int timer_irq_inited = 0;
  38. static int first_tick;
  39. static unsigned long long prev_usecs;
  40. #ifdef CONFIG_UML_REAL_TIME_CLOCK
  41. static long long delta; /* Deviation per interval */
  42. #endif
  43. void timer_irq(union uml_pt_regs *regs)
  44. {
  45. unsigned long long ticks = 0;
  46. if(!timer_irq_inited){
  47. /* This is to ensure that ticks don't pile up when
  48. * the timer handler is suspended */
  49. first_tick = 0;
  50. return;
  51. }
  52. if(first_tick){
  53. #ifdef CONFIG_UML_REAL_TIME_CLOCK
  54. /* We've had 1 tick */
  55. unsigned long long usecs = os_usecs();
  56. delta += usecs - prev_usecs;
  57. prev_usecs = usecs;
  58. /* Protect against the host clock being set backwards */
  59. if(delta < 0)
  60. delta = 0;
  61. ticks += (delta * HZ) / MILLION;
  62. delta -= (ticks * MILLION) / HZ;
  63. #else
  64. ticks = 1;
  65. #endif
  66. }
  67. else {
  68. prev_usecs = os_usecs();
  69. first_tick = 1;
  70. }
  71. while(ticks > 0){
  72. do_IRQ(TIMER_IRQ, regs);
  73. ticks--;
  74. }
  75. }
  76. void boot_timer_handler(int sig)
  77. {
  78. struct pt_regs regs;
  79. CHOOSE_MODE((void)
  80. (UPT_SC(&regs.regs) = (struct sigcontext *) (&sig + 1)),
  81. (void) (regs.regs.skas.is_user = 0));
  82. do_timer(&regs);
  83. }
  84. irqreturn_t um_timer(int irq, void *dev, struct pt_regs *regs)
  85. {
  86. unsigned long flags;
  87. do_timer(regs);
  88. write_seqlock_irqsave(&xtime_lock, flags);
  89. timer();
  90. write_sequnlock_irqrestore(&xtime_lock, flags);
  91. return(IRQ_HANDLED);
  92. }
  93. long um_time(int __user *tloc)
  94. {
  95. struct timeval now;
  96. do_gettimeofday(&now);
  97. if (tloc) {
  98. if (put_user(now.tv_sec, tloc))
  99. now.tv_sec = -EFAULT;
  100. }
  101. return now.tv_sec;
  102. }
  103. long um_stime(int __user *tptr)
  104. {
  105. int value;
  106. struct timespec new;
  107. if (get_user(value, tptr))
  108. return -EFAULT;
  109. new.tv_sec = value;
  110. new.tv_nsec = 0;
  111. do_settimeofday(&new);
  112. return 0;
  113. }
  114. void timer_handler(int sig, union uml_pt_regs *regs)
  115. {
  116. local_irq_disable();
  117. irq_enter();
  118. update_process_times(CHOOSE_MODE(user_context(UPT_SP(regs)),
  119. (regs)->skas.is_user));
  120. irq_exit();
  121. local_irq_enable();
  122. if(current_thread->cpu == 0)
  123. timer_irq(regs);
  124. }
  125. static DEFINE_SPINLOCK(timer_spinlock);
  126. unsigned long time_lock(void)
  127. {
  128. unsigned long flags;
  129. spin_lock_irqsave(&timer_spinlock, flags);
  130. return(flags);
  131. }
  132. void time_unlock(unsigned long flags)
  133. {
  134. spin_unlock_irqrestore(&timer_spinlock, flags);
  135. }
  136. int __init timer_init(void)
  137. {
  138. int err;
  139. user_time_init();
  140. err = request_irq(TIMER_IRQ, um_timer, SA_INTERRUPT, "timer", NULL);
  141. if(err != 0)
  142. printk(KERN_ERR "timer_init : request_irq failed - "
  143. "errno = %d\n", -err);
  144. timer_irq_inited = 1;
  145. return(0);
  146. }
  147. __initcall(timer_init);
  148. /*
  149. * Overrides for Emacs so that we follow Linus's tabbing style.
  150. * Emacs will notice this stuff at the end of the file and automatically
  151. * adjust the settings for this buffer only. This must remain at the end
  152. * of the file.
  153. * ---------------------------------------------------------------------------
  154. * Local variables:
  155. * c-file-style: "linux"
  156. * End:
  157. */