ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <unistd.h>
8 #include <time.h>
9 #include <sys/time.h>
10 #include <signal.h>
11 #include <errno.h>
12 #include "linux/module.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
20 extern struct timeval xtime;
21
22 void timer(void)
23 {
24         gettimeofday(&xtime, NULL);
25 }
26
27 void set_interval(int timer_type)
28 {
29         int usec = 1000000/hz();
30         struct itimerval interval = ((struct itimerval) { { 0, usec },
31                                                           { 0, usec } });
32
33         if(setitimer(timer_type, &interval, NULL) == -1)
34                 panic("setitimer failed - errno = %d\n", errno);
35 }
36
37 void enable_timer(void)
38 {
39         int usec = 1000000/hz();
40         struct itimerval enable = ((struct itimerval) { { 0, usec },
41                                                         { 0, usec }});
42         if(setitimer(ITIMER_VIRTUAL, &enable, NULL))
43                 printk("enable_timer - setitimer failed, errno = %d\n",
44                        errno);
45 }
46
47 void switch_timers(int to_real)
48 {
49         struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
50         struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
51                                                         { 0, 1000000/hz() }});
52         int old, new;
53
54         if(to_real){
55                 old = ITIMER_VIRTUAL;
56                 new = ITIMER_REAL;
57         }
58         else {
59                 old = ITIMER_REAL;
60                 new = ITIMER_VIRTUAL;
61         }
62
63         if((setitimer(old, &disable, NULL) < 0) ||
64            (setitimer(new, &enable, NULL)))
65                 printk("switch_timers - setitimer failed, errno = %d\n",
66                        errno);
67 }
68
69 void idle_timer(void)
70 {
71         if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
72                 panic("Couldn't unset SIGVTALRM handler");
73         
74         set_handler(SIGALRM, (__sighandler_t) alarm_handler, 
75                     SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
76         set_interval(ITIMER_REAL);
77 }
78
79 void time_init(void)
80 {
81         if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
82                 panic("Couldn't set SIGVTALRM handler");
83         set_interval(ITIMER_VIRTUAL);
84 }
85
86 struct timeval local_offset = { 0, 0 };
87
88 void do_gettimeofday(struct timeval *tv)
89 {
90         unsigned long flags;
91
92         flags = time_lock();
93         gettimeofday(tv, NULL);
94         timeradd(tv, &local_offset, tv);
95         time_unlock(flags);
96         clock_was_set();
97 }
98
99 EXPORT_SYMBOL(do_gettimeofday);
100
101 int do_settimeofday(struct timespec *tv)
102 {
103         struct timeval now;
104         unsigned long flags;
105         struct timeval tv_in;
106
107         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
108                 return -EINVAL;
109
110         tv_in.tv_sec = tv->tv_sec;
111         tv_in.tv_usec = tv->tv_nsec / 1000;
112
113         flags = time_lock();
114         gettimeofday(&now, NULL);
115         timersub(&tv_in, &now, &local_offset);
116         time_unlock(flags);
117 }
118
119 EXPORT_SYMBOL(do_settimeofday);
120
121 void idle_sleep(int secs)
122 {
123         struct timespec ts;
124
125         ts.tv_sec = secs;
126         ts.tv_nsec = 0;
127         nanosleep(&ts, NULL);
128 }
129
130 /*
131  * Overrides for Emacs so that we follow Linus's tabbing style.
132  * Emacs will notice this stuff at the end of the file and automatically
133  * adjust the settings for this buffer only.  This must remain at the end
134  * of the file.
135  * ---------------------------------------------------------------------------
136  * Local variables:
137  * c-file-style: "linux"
138  * End:
139  */