2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
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"
16 #include "linux/hrtimer.h"
18 #include "asm/param.h"
19 #include "asm/current.h"
20 #include "kern_util.h"
21 #include "user_util.h"
31 * Scheduler clock - returns current time in nanosec units.
33 unsigned long long sched_clock(void)
35 return (unsigned long long)jiffies_64 * (1000000000 / HZ);
38 static unsigned long long prev_nsecs;
39 #ifdef CONFIG_UML_REAL_TIME_CLOCK
40 static long long delta; /* Deviation per interval */
43 void timer_irq(union uml_pt_regs *regs)
45 unsigned long long ticks = 0;
47 #ifdef CONFIG_UML_REAL_TIME_CLOCK
49 /* We've had 1 tick */
50 unsigned long long nsecs = os_nsecs();
52 delta += nsecs - prev_nsecs;
55 /* Protect against the host clock being set backwards */
59 ticks += (delta * HZ) / BILLION;
60 delta -= (ticks * BILLION) / HZ;
62 else prev_nsecs = os_nsecs();
67 do_IRQ(TIMER_IRQ, regs);
72 static DEFINE_SPINLOCK(timer_spinlock);
74 static unsigned long long local_offset = 0;
76 static inline unsigned long long get_time(void)
78 unsigned long long nsecs;
81 spin_lock_irqsave(&timer_spinlock, flags);
83 nsecs += local_offset;
84 spin_unlock_irqrestore(&timer_spinlock, flags);
89 irqreturn_t um_timer(int irq, void *dev)
91 unsigned long long nsecs;
94 write_seqlock_irqsave(&xtime_lock, flags);
99 xtime.tv_sec = nsecs / NSEC_PER_SEC;
100 xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
102 write_sequnlock_irqrestore(&xtime_lock, flags);
107 static void register_timer(void)
111 err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
113 printk(KERN_ERR "register_timer : request_irq failed - "
114 "errno = %d\n", -err);
116 err = set_interval(1);
118 printk(KERN_ERR "register_timer : set_interval failed - "
119 "errno = %d\n", -err);
122 extern void (*late_time_init)(void);
129 set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
131 late_time_init = register_timer;
134 void do_gettimeofday(struct timeval *tv)
136 unsigned long long nsecs = get_time();
138 tv->tv_sec = nsecs / NSEC_PER_SEC;
139 /* Careful about calculations here - this was originally done as
140 * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
141 * which gave bogus (> 1000000) values. Dunno why, suspect gcc
142 * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
143 * problem that I missed.
145 nsecs -= tv->tv_sec * NSEC_PER_SEC;
146 tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
149 static inline void set_time(unsigned long long nsecs)
151 unsigned long long now;
154 spin_lock_irqsave(&timer_spinlock, flags);
156 local_offset = nsecs - now;
157 spin_unlock_irqrestore(&timer_spinlock, flags);
162 int do_settimeofday(struct timespec *tv)
164 set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
169 void timer_handler(int sig, union uml_pt_regs *regs)
173 update_process_times(CHOOSE_MODE(
174 (UPT_SC(regs) && user_context(UPT_SP(regs))),
175 (regs)->skas.is_user));
178 if(current_thread->cpu == 0)