blob: e49280599465ec9ed4f44f4b0ffdf7e0807d2339 [file] [log] [blame]
Gennady Sharapovcff65c42006-01-18 17:42:42 -08001/*
Jeff Dike4c9e1382007-10-16 01:26:54 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike{addtoit,linux.intel}.com)
Gennady Sharapovcff65c42006-01-18 17:42:42 -08003 * Licensed under the GPL
4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
Jeff Dike4c9e1382007-10-16 01:26:54 -07006#include <stddef.h>
7#include <errno.h>
8#include <signal.h>
Gennady Sharapovcff65c42006-01-18 17:42:42 -08009#include <time.h>
10#include <sys/time.h>
Gennady Sharapovcff65c42006-01-18 17:42:42 -080011#include "kern_constants.h"
12#include "os.h"
Jeff Dike4c9e1382007-10-16 01:26:54 -070013#include "user.h"
Gennady Sharapovcff65c42006-01-18 17:42:42 -080014
Jeff Dikea2f018b2007-10-16 01:27:22 -070015int set_interval(void)
Gennady Sharapovcff65c42006-01-18 17:42:42 -080016{
Jeff Dike1a805212007-10-16 01:27:28 -070017 int usec = UM_USEC_PER_SEC / UM_HZ;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080018 struct itimerval interval = ((struct itimerval) { { 0, usec },
19 { 0, usec } });
20
Jeff Dikea2f018b2007-10-16 01:27:22 -070021 if (setitimer(ITIMER_VIRTUAL, &interval, NULL) == -1)
Jeff Dike537ae942006-09-25 23:33:05 -070022 return -errno;
23
24 return 0;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080025}
26
Jeff Diked2753a6d2007-10-16 01:27:25 -070027int timer_one_shot(int ticks)
28{
Jeff Dike1a805212007-10-16 01:27:28 -070029 unsigned long usec = ticks * UM_USEC_PER_SEC / UM_HZ;
30 unsigned long sec = usec / UM_USEC_PER_SEC;
Jeff Diked2753a6d2007-10-16 01:27:25 -070031 struct itimerval interval;
32
Jeff Dike1a805212007-10-16 01:27:28 -070033 usec %= UM_USEC_PER_SEC;
Jeff Diked2753a6d2007-10-16 01:27:25 -070034 interval = ((struct itimerval) { { 0, 0 }, { sec, usec } });
35
36 if (setitimer(ITIMER_VIRTUAL, &interval, NULL) == -1)
37 return -errno;
38
39 return 0;
40}
41
Jeff Dike5f734612007-10-16 01:27:27 -070042/**
43 * timeval_to_ns - Convert timeval to nanoseconds
44 * @ts: pointer to the timeval variable to be converted
45 *
46 * Returns the scalar nanosecond representation of the timeval
47 * parameter.
48 *
49 * Ripped from linux/time.h because it's a kernel header, and thus
50 * unusable from here.
51 */
52static inline long long timeval_to_ns(const struct timeval *tv)
53{
54 return ((long long) tv->tv_sec * UM_NSEC_PER_SEC) +
55 tv->tv_usec * UM_NSEC_PER_USEC;
56}
57
58long long disable_timer(void)
Gennady Sharapovcff65c42006-01-18 17:42:42 -080059{
Jeff Dikeb160fb62007-10-16 01:27:26 -070060 struct itimerval time = ((struct itimerval) { { 0, 0 }, { 0, 0 } });
Jeff Dike4c9e1382007-10-16 01:26:54 -070061
Jeff Dike364e3a32007-11-28 16:21:51 -080062 if (setitimer(ITIMER_VIRTUAL, &time, &time) < 0)
Jeff Dike4c9e1382007-10-16 01:26:54 -070063 printk(UM_KERN_ERR "disable_timer - setitimer failed, "
64 "errno = %d\n", errno);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080065
Jeff Dike5f734612007-10-16 01:27:27 -070066 return timeval_to_ns(&time.it_value);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080067}
68
Jeff Dike5f734612007-10-16 01:27:27 -070069long long os_nsecs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 struct timeval tv;
72
73 gettimeofday(&tv, NULL);
Jeff Diked2753a6d2007-10-16 01:27:25 -070074 return timeval_to_ns(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Jeff Dike364e3a32007-11-28 16:21:51 -080077#ifdef UML_CONFIG_NO_HZ
78static int after_sleep_interval(struct timespec *ts)
79{
Jeff Dike0a765322007-12-01 12:16:29 -080080 return 0;
Jeff Dike364e3a32007-11-28 16:21:51 -080081}
82#else
83static inline long long timespec_to_us(const struct timespec *ts)
84{
85 return ((long long) ts->tv_sec * UM_USEC_PER_SEC) +
86 ts->tv_nsec / UM_NSEC_PER_USEC;
87}
88
89static int after_sleep_interval(struct timespec *ts)
90{
91 int usec = UM_USEC_PER_SEC / UM_HZ;
92 long long start_usecs = timespec_to_us(ts);
93 struct timeval tv;
94 struct itimerval interval;
95
96 /*
97 * It seems that rounding can increase the value returned from
98 * setitimer to larger than the one passed in. Over time,
99 * this will cause the remaining time to be greater than the
100 * tick interval. If this happens, then just reduce the first
101 * tick to the interval value.
102 */
103 if (start_usecs > usec)
104 start_usecs = usec;
105 tv = ((struct timeval) { .tv_sec = start_usecs / UM_USEC_PER_SEC,
106 .tv_usec = start_usecs % UM_USEC_PER_SEC });
107 interval = ((struct itimerval) { { 0, usec }, tv });
108
109 if (setitimer(ITIMER_VIRTUAL, &interval, NULL) == -1)
110 return -errno;
111
112 return 0;
113}
114#endif
115
Jeff Dikeb160fb62007-10-16 01:27:26 -0700116extern void alarm_handler(int sig, struct sigcontext *sc);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800117
Jeff Dikeb160fb62007-10-16 01:27:26 -0700118void idle_sleep(unsigned long long nsecs)
119{
Jeff Dike364e3a32007-11-28 16:21:51 -0800120 struct timespec ts;
121
122 /*
123 * nsecs can come in as zero, in which case, this starts a
124 * busy loop. To prevent this, reset nsecs to the tick
125 * interval if it is zero.
126 */
127 if (nsecs == 0)
128 nsecs = UM_NSEC_PER_SEC / UM_HZ;
129 ts = ((struct timespec) { .tv_sec = nsecs / UM_NSEC_PER_SEC,
130 .tv_nsec = nsecs % UM_NSEC_PER_SEC });
Jeff Dikeb160fb62007-10-16 01:27:26 -0700131
132 if (nanosleep(&ts, &ts) == 0)
133 alarm_handler(SIGVTALRM, NULL);
Jeff Dike364e3a32007-11-28 16:21:51 -0800134 after_sleep_interval(&ts);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800135}