+- add patches.fixes/linux-post-2.6.3-20040220
[linux-flexiantxendom0-3.2.10.git] / arch / um / kernel / time.c
1 /* 
2  * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <time.h>
10 #include <sys/time.h>
11 #include <signal.h>
12 #include <errno.h>
13 #include "user_util.h"
14 #include "kern_util.h"
15 #include "user.h"
16 #include "process.h"
17 #include "signal_user.h"
18 #include "time_user.h"
19 #include "kern_constants.h"
20
21 /* XXX This really needs to be declared and initialized in a kernel file since 
22  * it's in <linux/time.h>
23  */
24 extern struct timespec wall_to_monotonic;
25
26 extern struct timeval xtime;
27
28 struct timeval local_offset = { 0, 0 };
29
30 void timer(void)
31 {
32         gettimeofday(&xtime, NULL);
33         timeradd(&xtime, &local_offset, &xtime);
34 }
35
36 void set_interval(int timer_type)
37 {
38         int usec = 1000000/hz();
39         struct itimerval interval = ((struct itimerval) { { 0, usec },
40                                                           { 0, usec } });
41
42         if(setitimer(timer_type, &interval, NULL) == -1)
43                 panic("setitimer failed - errno = %d\n", errno);
44 }
45
46 void enable_timer(void)
47 {
48         int usec = 1000000/hz();
49         struct itimerval enable = ((struct itimerval) { { 0, usec },
50                                                         { 0, usec }});
51         if(setitimer(ITIMER_VIRTUAL, &enable, NULL))
52                 printk("enable_timer - setitimer failed, errno = %d\n",
53                        errno);
54 }
55
56 void switch_timers(int to_real)
57 {
58         struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
59         struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
60                                                         { 0, 1000000/hz() }});
61         int old, new;
62
63         if(to_real){
64                 old = ITIMER_VIRTUAL;
65                 new = ITIMER_REAL;
66         }
67         else {
68                 old = ITIMER_REAL;
69                 new = ITIMER_VIRTUAL;
70         }
71
72         if((setitimer(old, &disable, NULL) < 0) ||
73            (setitimer(new, &enable, NULL)))
74                 printk("switch_timers - setitimer failed, errno = %d\n",
75                        errno);
76 }
77
78 void uml_idle_timer(void)
79 {
80         if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
81                 panic("Couldn't unset SIGVTALRM handler");
82         
83         set_handler(SIGALRM, (__sighandler_t) alarm_handler, 
84                     SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
85         set_interval(ITIMER_REAL);
86 }
87
88 static unsigned long long get_host_hz(void)
89 {
90         char mhzline[16], *end;
91         int ret, mult, mhz, rest, len;
92
93         ret = cpu_feature("cpu MHz", mhzline, 
94                           sizeof(mhzline) / sizeof(mhzline[0]));
95         if(!ret)
96                 panic ("Could not get host MHZ");
97
98         mhz = strtoul(mhzline, &end, 10);
99
100         /* This business is to parse a floating point number without using
101          * floating types.
102          */
103
104         rest = 0;
105         mult = 0;
106         if(*end == '.'){
107                 end++;
108                 len = strlen(end);
109                 if(len < 6)
110                         mult = 6 - len;
111                 else if(len > 6)
112                         end[6] = '\0';
113                 rest = strtoul(end, NULL, 10);
114                 while(mult-- > 0)
115                         rest *= 10;
116         }
117
118         return(1000000 * mhz + rest);
119 }
120
121 unsigned long long host_hz = 0;
122
123 extern int do_posix_clock_monotonic_gettime(struct timespec *tp);
124
125 void time_init(void)
126 {
127         struct timespec now;
128  
129         host_hz = get_host_hz();
130         if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
131                 panic("Couldn't set SIGVTALRM handler");
132         set_interval(ITIMER_VIRTUAL);
133
134         do_posix_clock_monotonic_gettime(&now);
135         wall_to_monotonic.tv_sec = -now.tv_sec;
136         wall_to_monotonic.tv_nsec = -now.tv_nsec;
137 }
138
139 void do_gettimeofday(struct timeval *tv)
140 {
141         unsigned long flags;
142
143         flags = time_lock();
144         gettimeofday(tv, NULL);
145         timeradd(tv, &local_offset, tv);
146         time_unlock(flags);
147         clock_was_set();
148 }
149
150 int do_settimeofday(struct timespec *tv)
151 {
152         struct timeval now;
153         unsigned long flags;
154         struct timeval tv_in;
155
156         if ((unsigned long) tv->tv_nsec >= UM_NSEC_PER_SEC)
157                 return -EINVAL;
158
159         tv_in.tv_sec = tv->tv_sec;
160         tv_in.tv_usec = tv->tv_nsec / 1000;
161
162         flags = time_lock();
163         gettimeofday(&now, NULL);
164         timersub(&tv_in, &now, &local_offset);
165         time_unlock(flags);
166
167         return(0);
168 }
169
170 void idle_sleep(int secs)
171 {
172         struct timespec ts;
173
174         ts.tv_sec = secs;
175         ts.tv_nsec = 0;
176         nanosleep(&ts, NULL);
177 }
178
179 /*
180  * Overrides for Emacs so that we follow Linus's tabbing style.
181  * Emacs will notice this stuff at the end of the file and automatically
182  * adjust the settings for this buffer only.  This must remain at the end
183  * of the file.
184  * ---------------------------------------------------------------------------
185  * Local variables:
186  * c-file-style: "linux"
187  * End:
188  */