blob: 6712ffad0242c55e3ce1c33e340e55ee09c818c8 [file] [log] [blame]
Gennady Sharapovcff65c42006-01-18 17:42:42 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 Sharapovcff65c42006-01-18 17:42:42 -080016#include "linux/hrtimer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 Torvalds1da177e2005-04-16 15:20:36 -070022#include "mode.h"
23#include "os.h"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025int hz(void)
26{
27 return(HZ);
28}
29
30/*
31 * Scheduler clock - returns current time in nanosec units.
32 */
33unsigned long long sched_clock(void)
34{
35 return (unsigned long long)jiffies_64 * (1000000000 / HZ);
36}
37
38/* Changed at early boot */
39int timer_irq_inited = 0;
40
41static int first_tick;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080042static unsigned long long prev_nsecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#ifdef CONFIG_UML_REAL_TIME_CLOCK
44static long long delta; /* Deviation per interval */
45#endif
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047void 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 Sharapovcff65c42006-01-18 17:42:42 -080061 unsigned long long nsecs = os_nsecs();
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Gennady Sharapovcff65c42006-01-18 17:42:42 -080063 delta += nsecs - prev_nsecs;
64 prev_nsecs = nsecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 /* Protect against the host clock being set backwards */
67 if(delta < 0)
68 delta = 0;
69
Gennady Sharapovcff65c42006-01-18 17:42:42 -080070 ticks += (delta * HZ) / BILLION;
71 delta -= (ticks * BILLION) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#else
73 ticks = 1;
74#endif
75 }
76 else {
Gennady Sharapovcff65c42006-01-18 17:42:42 -080077 prev_nsecs = os_nsecs();
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 first_tick = 1;
79 }
80
81 while(ticks > 0){
82 do_IRQ(TIMER_IRQ, regs);
83 ticks--;
84 }
85}
86
87void boot_timer_handler(int sig)
88{
89 struct pt_regs regs;
90
Gennady Sharapovcff65c42006-01-18 17:42:42 -080091 CHOOSE_MODE((void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 (UPT_SC(&regs.regs) = (struct sigcontext *) (&sig + 1)),
93 (void) (regs.regs.skas.is_user = 0));
94 do_timer(&regs);
95}
96
Gennady Sharapovcff65c42006-01-18 17:42:42 -080097static DEFINE_SPINLOCK(timer_spinlock);
98
99static unsigned long long local_offset = 0;
100
101static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114irqreturn_t um_timer(int irq, void *dev, struct pt_regs *regs)
115{
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800116 unsigned long long nsecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned long flags;
118
119 do_timer(regs);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 write_seqlock_irqsave(&xtime_lock, flags);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800122 nsecs = get_time() + local_offset;
123 xtime.tv_sec = nsecs / NSEC_PER_SEC;
124 xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 write_sequnlock_irqrestore(&xtime_lock, flags);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return(IRQ_HANDLED);
128}
129
130long um_time(int __user *tloc)
131{
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800132 long ret = get_time() / NSEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800134 if((tloc != NULL) && put_user(ret, tloc))
135 return -EFAULT;
136
137 return ret;
138}
139
140void 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
155static 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();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168long um_stime(int __user *tptr)
169{
170 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 if (get_user(value, tptr))
173 return -EFAULT;
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800174
175 set_time((unsigned long long) value * NSEC_PER_SEC);
176
177 return 0;
178}
179
180int do_settimeofday(struct timespec *tv)
181{
182 set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return 0;
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187void timer_handler(int sig, union uml_pt_regs *regs)
188{
189 local_irq_disable();
Jeff Dike7e1f49d2005-07-28 21:16:09 -0700190 irq_enter();
191 update_process_times(CHOOSE_MODE(user_context(UPT_SP(regs)),
192 (regs)->skas.is_user));
193 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 local_irq_enable();
195 if(current_thread->cpu == 0)
196 timer_irq(regs);
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199int __init timer_init(void)
200{
201 int err;
202
Jeff Dikefc47a0d2005-06-25 14:55:24 -0700203 user_time_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 err = request_irq(TIMER_IRQ, um_timer, SA_INTERRUPT, "timer", NULL);
205 if(err != 0)
206 printk(KERN_ERR "timer_init : request_irq failed - "
207 "errno = %d\n", -err);
208 timer_irq_inited = 1;
209 return(0);
210}
211
212__initcall(timer_init);