This commit was manufactured by cvs2svn to create tag
[linux-2.6.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         unsigned long long mhz;
92         int ret, mult, rest, len;
93
94         ret = cpu_feature("cpu MHz", mhzline, 
95                           sizeof(mhzline) / sizeof(mhzline[0]));
96         if(!ret)
97                 panic ("Could not get host MHZ");
98
99         mhz = strtoul(mhzline, &end, 10);
100
101         /* This business is to parse a floating point number without using
102          * floating types.
103          */
104
105         rest = 0;
106         mult = 0;
107         if(*end == '.'){
108                 end++;
109                 len = strlen(end);
110                 if(len < 6)
111                         mult = 6 - len;
112                 else if(len > 6)
113                         end[6] = '\0';
114                 rest = strtoul(end, NULL, 10);
115                 while(mult-- > 0)
116                         rest *= 10;
117         }
118
119         return(1000000 * mhz + rest);
120 }
121
122 unsigned long long host_hz = 0;
123
124 extern int do_posix_clock_monotonic_gettime(struct timespec *tp);
125
126 void time_init(void)
127 {
128         struct timespec now;
129  
130         host_hz = get_host_hz();
131         if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
132                 panic("Couldn't set SIGVTALRM handler");
133         set_interval(ITIMER_VIRTUAL);
134
135         do_posix_clock_monotonic_gettime(&now);
136         wall_to_monotonic.tv_sec = -now.tv_sec;
137         wall_to_monotonic.tv_nsec = -now.tv_nsec;
138 }
139
140 /* Declared in linux/time.h, which can't be included here */
141 extern void clock_was_set(void);
142
143 void do_gettimeofday(struct timeval *tv)
144 {
145         unsigned long flags;
146
147         flags = time_lock();
148         gettimeofday(tv, NULL);
149         timeradd(tv, &local_offset, tv);
150         time_unlock(flags);
151         clock_was_set();
152 }
153
154 int do_settimeofday(struct timespec *tv)
155 {
156         struct timeval now;
157         unsigned long flags;
158         struct timeval tv_in;
159
160         if ((unsigned long) tv->tv_nsec >= UM_NSEC_PER_SEC)
161                 return -EINVAL;
162
163         tv_in.tv_sec = tv->tv_sec;
164         tv_in.tv_usec = tv->tv_nsec / 1000;
165
166         flags = time_lock();
167         gettimeofday(&now, NULL);
168         timersub(&tv_in, &now, &local_offset);
169         time_unlock(flags);
170
171         return(0);
172 }
173
174 void idle_sleep(int secs)
175 {
176         struct timespec ts;
177
178         ts.tv_sec = secs;
179         ts.tv_nsec = 0;
180         nanosleep(&ts, NULL);
181 }
182
183 /*
184  * Overrides for Emacs so that we follow Linus's tabbing style.
185  * Emacs will notice this stuff at the end of the file and automatically
186  * adjust the settings for this buffer only.  This must remain at the end
187  * of the file.
188  * ---------------------------------------------------------------------------
189  * Local variables:
190  * c-file-style: "linux"
191  * End:
192  */