2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
13 #include "user_util.h"
14 #include "kern_util.h"
17 #include "signal_user.h"
18 #include "time_user.h"
19 #include "kern_constants.h"
21 /* XXX This really needs to be declared and initialized in a kernel file since
22 * it's in <linux/time.h>
24 extern struct timespec wall_to_monotonic;
26 extern struct timeval xtime;
28 struct timeval local_offset = { 0, 0 };
32 gettimeofday(&xtime, NULL);
33 timeradd(&xtime, &local_offset, &xtime);
36 void set_interval(int timer_type)
38 int usec = 1000000/hz();
39 struct itimerval interval = ((struct itimerval) { { 0, usec },
42 if(setitimer(timer_type, &interval, NULL) == -1)
43 panic("setitimer failed - errno = %d\n", errno);
46 void enable_timer(void)
48 int usec = 1000000/hz();
49 struct itimerval enable = ((struct itimerval) { { 0, usec },
51 if(setitimer(ITIMER_VIRTUAL, &enable, NULL))
52 printk("enable_timer - setitimer failed, errno = %d\n",
56 void switch_timers(int to_real)
58 struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
59 struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
60 { 0, 1000000/hz() }});
72 if((setitimer(old, &disable, NULL) < 0) ||
73 (setitimer(new, &enable, NULL)))
74 printk("switch_timers - setitimer failed, errno = %d\n",
78 void uml_idle_timer(void)
80 if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
81 panic("Couldn't unset SIGVTALRM handler");
83 set_handler(SIGALRM, (__sighandler_t) alarm_handler,
84 SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
85 set_interval(ITIMER_REAL);
88 static unsigned long long get_host_hz(void)
90 char mhzline[16], *end;
91 unsigned long long mhz;
92 int ret, mult, rest, len;
94 ret = cpu_feature("cpu MHz", mhzline,
95 sizeof(mhzline) / sizeof(mhzline[0]));
97 panic ("Could not get host MHZ");
99 mhz = strtoul(mhzline, &end, 10);
101 /* This business is to parse a floating point number without using
114 rest = strtoul(end, NULL, 10);
119 return(1000000 * mhz + rest);
122 unsigned long long host_hz = 0;
124 extern int do_posix_clock_monotonic_gettime(struct timespec *tp);
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);
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;
140 /* Declared in linux/time.h, which can't be included here */
141 extern void clock_was_set(void);
143 void do_gettimeofday(struct timeval *tv)
148 gettimeofday(tv, NULL);
149 timeradd(tv, &local_offset, tv);
154 int do_settimeofday(struct timespec *tv)
158 struct timeval tv_in;
160 if ((unsigned long) tv->tv_nsec >= UM_NSEC_PER_SEC)
163 tv_in.tv_sec = tv->tv_sec;
164 tv_in.tv_usec = tv->tv_nsec / 1000;
167 gettimeofday(&now, NULL);
168 timersub(&tv_in, &now, &local_offset);
174 void idle_sleep(int secs)
180 nanosleep(&ts, NULL);
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
188 * ---------------------------------------------------------------------------
190 * c-file-style: "linux"