vserver 1.9.3
[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 disable_timer(void)
57 {
58         struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
59         if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
60            (setitimer(ITIMER_REAL, &disable, NULL) < 0))
61                 printk("disnable_timer - setitimer failed, errno = %d\n",
62                        errno);
63 }
64
65 void switch_timers(int to_real)
66 {
67         struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
68         struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
69                                                         { 0, 1000000/hz() }});
70         int old, new;
71
72         if(to_real){
73                 old = ITIMER_VIRTUAL;
74                 new = ITIMER_REAL;
75         }
76         else {
77                 old = ITIMER_REAL;
78                 new = ITIMER_VIRTUAL;
79         }
80
81         if((setitimer(old, &disable, NULL) < 0) ||
82            (setitimer(new, &enable, NULL)))
83                 printk("switch_timers - setitimer failed, errno = %d\n",
84                        errno);
85 }
86
87 void uml_idle_timer(void)
88 {
89         if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
90                 panic("Couldn't unset SIGVTALRM handler");
91         
92         set_handler(SIGALRM, (__sighandler_t) alarm_handler, 
93                     SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
94         set_interval(ITIMER_REAL);
95 }
96
97 extern int do_posix_clock_monotonic_gettime(struct timespec *tp);
98
99 void time_init(void)
100 {
101         struct timespec now;
102
103         if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
104                 panic("Couldn't set SIGVTALRM handler");
105         set_interval(ITIMER_VIRTUAL);
106
107         do_posix_clock_monotonic_gettime(&now);
108         wall_to_monotonic.tv_sec = -now.tv_sec;
109         wall_to_monotonic.tv_nsec = -now.tv_nsec;
110 }
111
112 /* Declared in linux/time.h, which can't be included here */
113 extern void clock_was_set(void);
114
115 void do_gettimeofday(struct timeval *tv)
116 {
117         unsigned long flags;
118
119         flags = time_lock();
120         gettimeofday(tv, NULL);
121         timeradd(tv, &local_offset, tv);
122         time_unlock(flags);
123         clock_was_set();
124 }
125
126 int do_settimeofday(struct timespec *tv)
127 {
128         struct timeval now;
129         unsigned long flags;
130         struct timeval tv_in;
131
132         if ((unsigned long) tv->tv_nsec >= UM_NSEC_PER_SEC)
133                 return -EINVAL;
134
135         tv_in.tv_sec = tv->tv_sec;
136         tv_in.tv_usec = tv->tv_nsec / 1000;
137
138         flags = time_lock();
139         gettimeofday(&now, NULL);
140         timersub(&tv_in, &now, &local_offset);
141         time_unlock(flags);
142
143         return(0);
144 }
145
146 void idle_sleep(int secs)
147 {
148         struct timespec ts;
149
150         ts.tv_sec = secs;
151         ts.tv_nsec = 0;
152         nanosleep(&ts, NULL);
153 }
154
155 /*
156  * Overrides for Emacs so that we follow Linus's tabbing style.
157  * Emacs will notice this stuff at the end of the file and automatically
158  * adjust the settings for this buffer only.  This must remain at the end
159  * of the file.
160  * ---------------------------------------------------------------------------
161  * Local variables:
162  * c-file-style: "linux"
163  * End:
164  */