Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include "linux/kernel.h" |
| 7 | #include "linux/module.h" |
| 8 | #include "linux/unistd.h" |
| 9 | #include "linux/stddef.h" |
| 10 | #include "linux/spinlock.h" |
| 11 | #include "linux/time.h" |
| 12 | #include "linux/sched.h" |
| 13 | #include "linux/interrupt.h" |
| 14 | #include "linux/init.h" |
| 15 | #include "linux/delay.h" |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 16 | #include "linux/hrtimer.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "asm/irq.h" |
| 18 | #include "asm/param.h" |
| 19 | #include "asm/current.h" |
| 20 | #include "kern_util.h" |
| 21 | #include "user_util.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "mode.h" |
| 23 | #include "os.h" |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | int hz(void) |
| 26 | { |
| 27 | return(HZ); |
| 28 | } |
| 29 | |
| 30 | /* |
| 31 | * Scheduler clock - returns current time in nanosec units. |
| 32 | */ |
| 33 | unsigned long long sched_clock(void) |
| 34 | { |
| 35 | return (unsigned long long)jiffies_64 * (1000000000 / HZ); |
| 36 | } |
| 37 | |
| 38 | /* Changed at early boot */ |
| 39 | int timer_irq_inited = 0; |
| 40 | |
| 41 | static int first_tick; |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 42 | static unsigned long long prev_nsecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #ifdef CONFIG_UML_REAL_TIME_CLOCK |
| 44 | static long long delta; /* Deviation per interval */ |
| 45 | #endif |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | void timer_irq(union uml_pt_regs *regs) |
| 48 | { |
| 49 | unsigned long long ticks = 0; |
| 50 | |
| 51 | if(!timer_irq_inited){ |
| 52 | /* This is to ensure that ticks don't pile up when |
| 53 | * the timer handler is suspended */ |
| 54 | first_tick = 0; |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if(first_tick){ |
| 59 | #ifdef CONFIG_UML_REAL_TIME_CLOCK |
| 60 | /* We've had 1 tick */ |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 61 | unsigned long long nsecs = os_nsecs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 63 | delta += nsecs - prev_nsecs; |
| 64 | prev_nsecs = nsecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /* Protect against the host clock being set backwards */ |
| 67 | if(delta < 0) |
| 68 | delta = 0; |
| 69 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 70 | ticks += (delta * HZ) / BILLION; |
| 71 | delta -= (ticks * BILLION) / HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #else |
| 73 | ticks = 1; |
| 74 | #endif |
| 75 | } |
| 76 | else { |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 77 | prev_nsecs = os_nsecs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | first_tick = 1; |
| 79 | } |
| 80 | |
| 81 | while(ticks > 0){ |
| 82 | do_IRQ(TIMER_IRQ, regs); |
| 83 | ticks--; |
| 84 | } |
| 85 | } |
| 86 | |
Bodo Stroesser | 2c332a2 | 2006-01-18 17:42:43 -0800 | [diff] [blame^] | 87 | void do_boot_timer_handler(struct sigcontext * sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | struct pt_regs regs; |
| 90 | |
Bodo Stroesser | 2c332a2 | 2006-01-18 17:42:43 -0800 | [diff] [blame^] | 91 | CHOOSE_MODE((void) (UPT_SC(®s.regs) = sc), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | (void) (regs.regs.skas.is_user = 0)); |
| 93 | do_timer(®s); |
| 94 | } |
| 95 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 96 | static DEFINE_SPINLOCK(timer_spinlock); |
| 97 | |
| 98 | static unsigned long long local_offset = 0; |
| 99 | |
| 100 | static inline unsigned long long get_time(void) |
| 101 | { |
| 102 | unsigned long long nsecs; |
| 103 | unsigned long flags; |
| 104 | |
| 105 | spin_lock_irqsave(&timer_spinlock, flags); |
| 106 | nsecs = os_nsecs(); |
| 107 | nsecs += local_offset; |
| 108 | spin_unlock_irqrestore(&timer_spinlock, flags); |
| 109 | |
| 110 | return nsecs; |
| 111 | } |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | irqreturn_t um_timer(int irq, void *dev, struct pt_regs *regs) |
| 114 | { |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 115 | unsigned long long nsecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | unsigned long flags; |
| 117 | |
| 118 | do_timer(regs); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | write_seqlock_irqsave(&xtime_lock, flags); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 121 | nsecs = get_time() + local_offset; |
| 122 | xtime.tv_sec = nsecs / NSEC_PER_SEC; |
| 123 | xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | write_sequnlock_irqrestore(&xtime_lock, flags); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return(IRQ_HANDLED); |
| 127 | } |
| 128 | |
| 129 | long um_time(int __user *tloc) |
| 130 | { |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 131 | long ret = get_time() / NSEC_PER_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 133 | if((tloc != NULL) && put_user(ret, tloc)) |
| 134 | return -EFAULT; |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | void do_gettimeofday(struct timeval *tv) |
| 140 | { |
| 141 | unsigned long long nsecs = get_time(); |
| 142 | |
| 143 | tv->tv_sec = nsecs / NSEC_PER_SEC; |
| 144 | /* Careful about calculations here - this was originally done as |
| 145 | * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC |
| 146 | * which gave bogus (> 1000000) values. Dunno why, suspect gcc |
| 147 | * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion |
| 148 | * problem that I missed. |
| 149 | */ |
| 150 | nsecs -= tv->tv_sec * NSEC_PER_SEC; |
| 151 | tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC; |
| 152 | } |
| 153 | |
| 154 | static inline void set_time(unsigned long long nsecs) |
| 155 | { |
| 156 | unsigned long long now; |
| 157 | unsigned long flags; |
| 158 | |
| 159 | spin_lock_irqsave(&timer_spinlock, flags); |
| 160 | now = os_nsecs(); |
| 161 | local_offset = nsecs - now; |
| 162 | spin_unlock_irqrestore(&timer_spinlock, flags); |
| 163 | |
| 164 | clock_was_set(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | long um_stime(int __user *tptr) |
| 168 | { |
| 169 | int value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | if (get_user(value, tptr)) |
| 172 | return -EFAULT; |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 173 | |
| 174 | set_time((unsigned long long) value * NSEC_PER_SEC); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | int do_settimeofday(struct timespec *tv) |
| 180 | { |
| 181 | set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec); |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | void timer_handler(int sig, union uml_pt_regs *regs) |
| 187 | { |
| 188 | local_irq_disable(); |
Jeff Dike | 7e1f49d | 2005-07-28 21:16:09 -0700 | [diff] [blame] | 189 | irq_enter(); |
| 190 | update_process_times(CHOOSE_MODE(user_context(UPT_SP(regs)), |
| 191 | (regs)->skas.is_user)); |
| 192 | irq_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | local_irq_enable(); |
| 194 | if(current_thread->cpu == 0) |
| 195 | timer_irq(regs); |
| 196 | } |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | int __init timer_init(void) |
| 199 | { |
| 200 | int err; |
| 201 | |
Jeff Dike | fc47a0d | 2005-06-25 14:55:24 -0700 | [diff] [blame] | 202 | user_time_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | err = request_irq(TIMER_IRQ, um_timer, SA_INTERRUPT, "timer", NULL); |
| 204 | if(err != 0) |
| 205 | printk(KERN_ERR "timer_init : request_irq failed - " |
| 206 | "errno = %d\n", -err); |
| 207 | timer_irq_inited = 1; |
| 208 | return(0); |
| 209 | } |
| 210 | |
| 211 | __initcall(timer_init); |