blob: 8fa2ae7f30261b3c749f2c1f4a666f210dd0f896 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "time_user.h"
18#include "kern_constants.h"
Gennady Sharapov0805d892006-01-08 01:01:29 -080019#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21/* XXX This really needs to be declared and initialized in a kernel file since
22 * it's in <linux/time.h>
23 */
24extern struct timespec wall_to_monotonic;
25
26extern struct timeval xtime;
27
28struct timeval local_offset = { 0, 0 };
29
30void timer(void)
31{
32 gettimeofday(&xtime, NULL);
33 timeradd(&xtime, &local_offset, &xtime);
34}
35
Jeff Dikefc47a0d2005-06-25 14:55:24 -070036static void set_interval(int timer_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
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
46void enable_timer(void)
47{
Jeff Dikefc47a0d2005-06-25 14:55:24 -070048 set_interval(ITIMER_VIRTUAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Bodo Stroesser9786a8f2005-07-07 17:56:50 -070051void prepare_timer(void * ptr)
52{
53 int usec = 1000000/hz();
54 *(struct itimerval *)ptr = ((struct itimerval) { { 0, usec },
55 { 0, usec }});
56}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058void disable_timer(void)
59{
60 struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
61 if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
62 (setitimer(ITIMER_REAL, &disable, NULL) < 0))
63 printk("disnable_timer - setitimer failed, errno = %d\n",
64 errno);
65 /* If there are signals already queued, after unblocking ignore them */
66 set_handler(SIGALRM, SIG_IGN, 0, -1);
67 set_handler(SIGVTALRM, SIG_IGN, 0, -1);
68}
69
70void switch_timers(int to_real)
71{
72 struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
73 struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
74 { 0, 1000000/hz() }});
75 int old, new;
76
77 if(to_real){
78 old = ITIMER_VIRTUAL;
79 new = ITIMER_REAL;
80 }
81 else {
82 old = ITIMER_REAL;
83 new = ITIMER_VIRTUAL;
84 }
85
86 if((setitimer(old, &disable, NULL) < 0) ||
87 (setitimer(new, &enable, NULL)))
88 printk("switch_timers - setitimer failed, errno = %d\n",
89 errno);
90}
91
92void uml_idle_timer(void)
93{
94 if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
95 panic("Couldn't unset SIGVTALRM handler");
96
97 set_handler(SIGALRM, (__sighandler_t) alarm_handler,
98 SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
99 set_interval(ITIMER_REAL);
100}
101
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800102extern void ktime_get_ts(struct timespec *ts);
103#define do_posix_clock_monotonic_gettime(ts) ktime_get_ts(ts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105void time_init(void)
106{
107 struct timespec now;
108
109 if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
110 panic("Couldn't set SIGVTALRM handler");
111 set_interval(ITIMER_VIRTUAL);
112
113 do_posix_clock_monotonic_gettime(&now);
114 wall_to_monotonic.tv_sec = -now.tv_sec;
115 wall_to_monotonic.tv_nsec = -now.tv_nsec;
116}
117
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800118/* Defined in linux/ktimer.h, which can't be included here */
119#define clock_was_set() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121void do_gettimeofday(struct timeval *tv)
122{
123 unsigned long flags;
124
125 flags = time_lock();
126 gettimeofday(tv, NULL);
127 timeradd(tv, &local_offset, tv);
128 time_unlock(flags);
129 clock_was_set();
130}
131
132int do_settimeofday(struct timespec *tv)
133{
134 struct timeval now;
135 unsigned long flags;
136 struct timeval tv_in;
137
138 if ((unsigned long) tv->tv_nsec >= UM_NSEC_PER_SEC)
139 return -EINVAL;
140
141 tv_in.tv_sec = tv->tv_sec;
142 tv_in.tv_usec = tv->tv_nsec / 1000;
143
144 flags = time_lock();
145 gettimeofday(&now, NULL);
146 timersub(&tv_in, &now, &local_offset);
147 time_unlock(flags);
148
149 return(0);
150}
151
152void idle_sleep(int secs)
153{
154 struct timespec ts;
155
156 ts.tv_sec = secs;
157 ts.tv_nsec = 0;
158 nanosleep(&ts, NULL);
159}
160
Jeff Dikefc47a0d2005-06-25 14:55:24 -0700161/* XXX This partly duplicates init_irq_signals */
162
163void user_time_init(void)
164{
165 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
166 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
167 SIGALRM, SIGUSR2, -1);
168 set_handler(SIGALRM, (__sighandler_t) alarm_handler,
169 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
170 SIGVTALRM, SIGUSR2, -1);
171 set_interval(ITIMER_VIRTUAL);
172}