blob: 574b134f0502775ce5e120611b8c80d59ee28dec [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 Dike532d0fa2007-10-16 01:27:21 -070017 int usec = 1000000/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{
29 unsigned long usec = ticks * 1000000 / UM_HZ;
30 unsigned long sec = usec / 1000000;
31 struct itimerval interval;
32
33 usec %= 1000000;
34 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 Dikeb160fb62007-10-16 01:27:26 -070062 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 Dikeb160fb62007-10-16 01:27:26 -070077extern void alarm_handler(int sig, struct sigcontext *sc);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080078
Jeff Dikeb160fb62007-10-16 01:27:26 -070079void idle_sleep(unsigned long long nsecs)
80{
81 struct timespec ts = { .tv_sec = nsecs / BILLION,
82 .tv_nsec = nsecs % BILLION };
83
84 if (nanosleep(&ts, &ts) == 0)
85 alarm_handler(SIGVTALRM, NULL);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080086}