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"
30 * Scheduler clock - returns current time in nanosec units.
32 unsigned long long sched_clock(void)
34 return (unsigned long long)jiffies_64
* (1000000000 / HZ
);
37 static unsigned long long prev_nsecs
[NR_CPUS
];
38 #ifdef CONFIG_UML_REAL_TIME_CLOCK
39 static long long delta
[NR_CPUS
]; /* Deviation per interval */
42 void timer_irq(union uml_pt_regs
*regs
)
44 unsigned long long ticks
= 0;
45 #ifdef CONFIG_UML_REAL_TIME_CLOCK
48 /* We've had 1 tick */
49 unsigned long long nsecs
= os_nsecs();
51 delta
[c
] += nsecs
- prev_nsecs
[c
];
52 prev_nsecs
[c
] = nsecs
;
54 /* Protect against the host clock being set backwards */
58 ticks
+= (delta
[c
] * HZ
) / BILLION
;
59 delta
[c
] -= (ticks
* BILLION
) / HZ
;
61 else prev_nsecs
[c
] = os_nsecs();
66 do_IRQ(TIMER_IRQ
, regs
);
71 /* Protects local_offset */
72 static DEFINE_SPINLOCK(timer_spinlock
);
73 static unsigned long long local_offset
= 0;
75 static inline unsigned long long get_time(void)
77 unsigned long long nsecs
;
80 spin_lock_irqsave(&timer_spinlock
, flags
);
82 nsecs
+= local_offset
;
83 spin_unlock_irqrestore(&timer_spinlock
, flags
);
88 irqreturn_t
um_timer(int irq
, void *dev
)
90 unsigned long long nsecs
;
93 write_seqlock_irqsave(&xtime_lock
, flags
);
98 xtime
.tv_sec
= nsecs
/ NSEC_PER_SEC
;
99 xtime
.tv_nsec
= nsecs
- xtime
.tv_sec
* NSEC_PER_SEC
;
101 write_sequnlock_irqrestore(&xtime_lock
, flags
);
106 static void register_timer(void)
110 err
= request_irq(TIMER_IRQ
, um_timer
, IRQF_DISABLED
, "timer", NULL
);
112 printk(KERN_ERR
"register_timer : request_irq failed - "
113 "errno = %d\n", -err
);
115 err
= set_interval(1);
117 printk(KERN_ERR
"register_timer : set_interval failed - "
118 "errno = %d\n", -err
);
121 extern void (*late_time_init
)(void);
128 set_normalized_timespec(&wall_to_monotonic
, -nsecs
/ BILLION
,
130 late_time_init
= register_timer
;
133 void do_gettimeofday(struct timeval
*tv
)
135 unsigned long long nsecs
= get_time();
137 tv
->tv_sec
= nsecs
/ NSEC_PER_SEC
;
138 /* Careful about calculations here - this was originally done as
139 * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
140 * which gave bogus (> 1000000) values. Dunno why, suspect gcc
141 * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
142 * problem that I missed.
144 nsecs
-= tv
->tv_sec
* NSEC_PER_SEC
;
145 tv
->tv_usec
= (unsigned long) nsecs
/ NSEC_PER_USEC
;
148 static inline void set_time(unsigned long long nsecs
)
150 unsigned long long now
;
153 spin_lock_irqsave(&timer_spinlock
, flags
);
155 local_offset
= nsecs
- now
;
156 spin_unlock_irqrestore(&timer_spinlock
, flags
);
161 int do_settimeofday(struct timespec
*tv
)
163 set_time((unsigned long long) tv
->tv_sec
* NSEC_PER_SEC
+ tv
->tv_nsec
);
168 void timer_handler(int sig
, union uml_pt_regs
*regs
)
172 update_process_times(CHOOSE_MODE(
173 (UPT_SC(regs
) && user_context(UPT_SP(regs
))),
174 (regs
)->skas
.is_user
));
177 if(current_thread
->cpu
== 0)