blob: ef02d941c2adf7666c53210c24019d235dd3588b [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{
80}
81#else
82static inline long long timespec_to_us(const struct timespec *ts)
83{
84 return ((long long) ts->tv_sec * UM_USEC_PER_SEC) +
85 ts->tv_nsec / UM_NSEC_PER_USEC;
86}
87
88static int after_sleep_interval(struct timespec *ts)
89{
90 int usec = UM_USEC_PER_SEC / UM_HZ;
91 long long start_usecs = timespec_to_us(ts);
92 struct timeval tv;
93 struct itimerval interval;
94
95 /*
96 * It seems that rounding can increase the value returned from
97 * setitimer to larger than the one passed in. Over time,
98 * this will cause the remaining time to be greater than the
99 * tick interval. If this happens, then just reduce the first
100 * tick to the interval value.
101 */
102 if (start_usecs > usec)
103 start_usecs = usec;
104 tv = ((struct timeval) { .tv_sec = start_usecs / UM_USEC_PER_SEC,
105 .tv_usec = start_usecs % UM_USEC_PER_SEC });
106 interval = ((struct itimerval) { { 0, usec }, tv });
107
108 if (setitimer(ITIMER_VIRTUAL, &interval, NULL) == -1)
109 return -errno;
110
111 return 0;
112}
113#endif
114
Jeff Dikeb160fb62007-10-16 01:27:26 -0700115extern void alarm_handler(int sig, struct sigcontext *sc);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800116
Jeff Dikeb160fb62007-10-16 01:27:26 -0700117void idle_sleep(unsigned long long nsecs)
118{
Jeff Dike364e3a32007-11-28 16:21:51 -0800119 struct timespec ts;
120
121 /*
122 * nsecs can come in as zero, in which case, this starts a
123 * busy loop. To prevent this, reset nsecs to the tick
124 * interval if it is zero.
125 */
126 if (nsecs == 0)
127 nsecs = UM_NSEC_PER_SEC / UM_HZ;
128 ts = ((struct timespec) { .tv_sec = nsecs / UM_NSEC_PER_SEC,
129 .tv_nsec = nsecs % UM_NSEC_PER_SEC });
Jeff Dikeb160fb62007-10-16 01:27:26 -0700130
131 if (nanosleep(&ts, &ts) == 0)
132 alarm_handler(SIGVTALRM, NULL);
Jeff Dike364e3a32007-11-28 16:21:51 -0800133 after_sleep_interval(&ts);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800134}