[PATCH] uml: time initialization tidying
[pandora-kernel.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 static 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         set_interval(ITIMER_VIRTUAL);
49 }
50
51 void disable_timer(void)
52 {
53         struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
54         if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
55            (setitimer(ITIMER_REAL, &disable, NULL) < 0))
56                 printk("disnable_timer - setitimer failed, errno = %d\n",
57                        errno);
58         /* If there are signals already queued, after unblocking ignore them */
59         set_handler(SIGALRM, SIG_IGN, 0, -1);
60         set_handler(SIGVTALRM, SIG_IGN, 0, -1);
61 }
62
63 void switch_timers(int to_real)
64 {
65         struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
66         struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
67                                                         { 0, 1000000/hz() }});
68         int old, new;
69
70         if(to_real){
71                 old = ITIMER_VIRTUAL;
72                 new = ITIMER_REAL;
73         }
74         else {
75                 old = ITIMER_REAL;
76                 new = ITIMER_VIRTUAL;
77         }
78
79         if((setitimer(old, &disable, NULL) < 0) ||
80            (setitimer(new, &enable, NULL)))
81                 printk("switch_timers - setitimer failed, errno = %d\n",
82                        errno);
83 }
84
85 void uml_idle_timer(void)
86 {
87         if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
88                 panic("Couldn't unset SIGVTALRM handler");
89         
90         set_handler(SIGALRM, (__sighandler_t) alarm_handler, 
91                     SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
92         set_interval(ITIMER_REAL);
93 }
94
95 extern int do_posix_clock_monotonic_gettime(struct timespec *tp);
96
97 void time_init(void)
98 {
99         struct timespec now;
100
101         if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
102                 panic("Couldn't set SIGVTALRM handler");
103         set_interval(ITIMER_VIRTUAL);
104
105         do_posix_clock_monotonic_gettime(&now);
106         wall_to_monotonic.tv_sec = -now.tv_sec;
107         wall_to_monotonic.tv_nsec = -now.tv_nsec;
108 }
109
110 /* Declared in linux/time.h, which can't be included here */
111 extern void clock_was_set(void);
112
113 void do_gettimeofday(struct timeval *tv)
114 {
115         unsigned long flags;
116
117         flags = time_lock();
118         gettimeofday(tv, NULL);
119         timeradd(tv, &local_offset, tv);
120         time_unlock(flags);
121         clock_was_set();
122 }
123
124 int do_settimeofday(struct timespec *tv)
125 {
126         struct timeval now;
127         unsigned long flags;
128         struct timeval tv_in;
129
130         if ((unsigned long) tv->tv_nsec >= UM_NSEC_PER_SEC)
131                 return -EINVAL;
132
133         tv_in.tv_sec = tv->tv_sec;
134         tv_in.tv_usec = tv->tv_nsec / 1000;
135
136         flags = time_lock();
137         gettimeofday(&now, NULL);
138         timersub(&tv_in, &now, &local_offset);
139         time_unlock(flags);
140
141         return(0);
142 }
143
144 void idle_sleep(int secs)
145 {
146         struct timespec ts;
147
148         ts.tv_sec = secs;
149         ts.tv_nsec = 0;
150         nanosleep(&ts, NULL);
151 }
152
153 /* XXX This partly duplicates init_irq_signals */
154
155 void user_time_init(void)
156 {
157         set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
158                     SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
159                     SIGALRM, SIGUSR2, -1);
160         set_handler(SIGALRM, (__sighandler_t) alarm_handler,
161                     SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
162                     SIGVTALRM, SIGUSR2, -1);
163         set_interval(ITIMER_VIRTUAL);
164 }