1- /*
1+ /*
22 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
33 * Licensed under the GPL
44 */
1313#include "linux/interrupt.h"
1414#include "linux/init.h"
1515#include "linux/delay.h"
16+ #include "linux/hrtimer.h"
1617#include "asm/irq.h"
1718#include "asm/param.h"
1819#include "asm/current.h"
1920#include "kern_util.h"
2021#include "user_util.h"
21- #include "time_user.h"
2222#include "mode.h"
2323#include "os.h"
2424
@@ -39,7 +39,7 @@ unsigned long long sched_clock(void)
3939int timer_irq_inited = 0 ;
4040
4141static int first_tick ;
42- static unsigned long long prev_usecs ;
42+ static unsigned long long prev_nsecs ;
4343#ifdef CONFIG_UML_REAL_TIME_CLOCK
4444static long long delta ; /* Deviation per interval */
4545#endif
@@ -58,23 +58,23 @@ void timer_irq(union uml_pt_regs *regs)
5858 if (first_tick ){
5959#ifdef CONFIG_UML_REAL_TIME_CLOCK
6060 /* We've had 1 tick */
61- unsigned long long usecs = os_usecs ();
61+ unsigned long long nsecs = os_nsecs ();
6262
63- delta += usecs - prev_usecs ;
64- prev_usecs = usecs ;
63+ delta += nsecs - prev_nsecs ;
64+ prev_nsecs = nsecs ;
6565
6666 /* Protect against the host clock being set backwards */
6767 if (delta < 0 )
6868 delta = 0 ;
6969
70- ticks += (delta * HZ ) / MILLION ;
71- delta -= (ticks * MILLION ) / HZ ;
70+ ticks += (delta * HZ ) / BILLION ;
71+ delta -= (ticks * BILLION ) / HZ ;
7272#else
7373 ticks = 1 ;
7474#endif
7575 }
7676 else {
77- prev_usecs = os_usecs ();
77+ prev_nsecs = os_nsecs ();
7878 first_tick = 1 ;
7979 }
8080
@@ -88,45 +88,99 @@ void boot_timer_handler(int sig)
8888{
8989 struct pt_regs regs ;
9090
91- CHOOSE_MODE ((void )
91+ CHOOSE_MODE ((void )
9292 (UPT_SC (& regs .regs ) = (struct sigcontext * ) (& sig + 1 )),
9393 (void ) (regs .regs .skas .is_user = 0 ));
9494 do_timer (& regs );
9595}
9696
97+ static DEFINE_SPINLOCK (timer_spinlock );
98+
99+ static unsigned long long local_offset = 0 ;
100+
101+ static inline unsigned long long get_time (void )
102+ {
103+ unsigned long long nsecs ;
104+ unsigned long flags ;
105+
106+ spin_lock_irqsave (& timer_spinlock , flags );
107+ nsecs = os_nsecs ();
108+ nsecs += local_offset ;
109+ spin_unlock_irqrestore (& timer_spinlock , flags );
110+
111+ return nsecs ;
112+ }
113+
97114irqreturn_t um_timer (int irq , void * dev , struct pt_regs * regs )
98115{
116+ unsigned long long nsecs ;
99117 unsigned long flags ;
100118
101119 do_timer (regs );
120+
102121 write_seqlock_irqsave (& xtime_lock , flags );
103- timer ();
122+ nsecs = get_time () + local_offset ;
123+ xtime .tv_sec = nsecs / NSEC_PER_SEC ;
124+ xtime .tv_nsec = nsecs - xtime .tv_sec * NSEC_PER_SEC ;
104125 write_sequnlock_irqrestore (& xtime_lock , flags );
126+
105127 return (IRQ_HANDLED );
106128}
107129
108130long um_time (int __user * tloc )
109131{
110- struct timeval now ;
132+ long ret = get_time () / NSEC_PER_SEC ;
111133
112- do_gettimeofday (& now );
113- if (tloc ) {
114- if (put_user (now .tv_sec , tloc ))
115- now .tv_sec = - EFAULT ;
116- }
117- return now .tv_sec ;
134+ if ((tloc != NULL ) && put_user (ret , tloc ))
135+ return - EFAULT ;
136+
137+ return ret ;
138+ }
139+
140+ void do_gettimeofday (struct timeval * tv )
141+ {
142+ unsigned long long nsecs = get_time ();
143+
144+ tv -> tv_sec = nsecs / NSEC_PER_SEC ;
145+ /* Careful about calculations here - this was originally done as
146+ * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
147+ * which gave bogus (> 1000000) values. Dunno why, suspect gcc
148+ * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
149+ * problem that I missed.
150+ */
151+ nsecs -= tv -> tv_sec * NSEC_PER_SEC ;
152+ tv -> tv_usec = (unsigned long ) nsecs / NSEC_PER_USEC ;
153+ }
154+
155+ static inline void set_time (unsigned long long nsecs )
156+ {
157+ unsigned long long now ;
158+ unsigned long flags ;
159+
160+ spin_lock_irqsave (& timer_spinlock , flags );
161+ now = os_nsecs ();
162+ local_offset = nsecs - now ;
163+ spin_unlock_irqrestore (& timer_spinlock , flags );
164+
165+ clock_was_set ();
118166}
119167
120168long um_stime (int __user * tptr )
121169{
122170 int value ;
123- struct timespec new ;
124171
125172 if (get_user (value , tptr ))
126173 return - EFAULT ;
127- new .tv_sec = value ;
128- new .tv_nsec = 0 ;
129- do_settimeofday (& new );
174+
175+ set_time ((unsigned long long ) value * NSEC_PER_SEC );
176+
177+ return 0 ;
178+ }
179+
180+ int do_settimeofday (struct timespec * tv )
181+ {
182+ set_time ((unsigned long long ) tv -> tv_sec * NSEC_PER_SEC + tv -> tv_nsec );
183+
130184 return 0 ;
131185}
132186
@@ -142,21 +196,6 @@ void timer_handler(int sig, union uml_pt_regs *regs)
142196 timer_irq (regs );
143197}
144198
145- static DEFINE_SPINLOCK (timer_spinlock );
146-
147- unsigned long time_lock (void )
148- {
149- unsigned long flags ;
150-
151- spin_lock_irqsave (& timer_spinlock , flags );
152- return (flags );
153- }
154-
155- void time_unlock (unsigned long flags )
156- {
157- spin_unlock_irqrestore (& timer_spinlock , flags );
158- }
159-
160199int __init timer_init (void )
161200{
162201 int err ;
@@ -171,14 +210,3 @@ int __init timer_init(void)
171210}
172211
173212__initcall (timer_init );
174-
175- /*
176- * Overrides for Emacs so that we follow Linus's tabbing style.
177- * Emacs will notice this stuff at the end of the file and automatically
178- * adjust the settings for this buffer only. This must remain at the end
179- * of the file.
180- * ---------------------------------------------------------------------------
181- * Local variables:
182- * c-file-style: "linux"
183- * End:
184- */
0 commit comments