blob: f22fcdfd42505513d2ac38951b5496d40574762f [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_util.h"
Gennady Sharapovcff65c42006-01-18 17:42:42 -080012#include "kern_constants.h"
13#include "os.h"
Jeff Dike4c9e1382007-10-16 01:26:54 -070014#include "user.h"
Gennady Sharapovcff65c42006-01-18 17:42:42 -080015
Jeff Dike537ae942006-09-25 23:33:05 -070016int set_interval(int is_virtual)
Gennady Sharapovcff65c42006-01-18 17:42:42 -080017{
18 int usec = 1000000/hz();
Jeff Dike537ae942006-09-25 23:33:05 -070019 int timer_type = is_virtual ? ITIMER_VIRTUAL : ITIMER_REAL;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080020 struct itimerval interval = ((struct itimerval) { { 0, usec },
21 { 0, usec } });
22
Jeff Dike4c9e1382007-10-16 01:26:54 -070023 if (setitimer(timer_type, &interval, NULL) == -1)
Jeff Dike537ae942006-09-25 23:33:05 -070024 return -errno;
25
26 return 0;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080027}
28
Gennady Sharapovcff65c42006-01-18 17:42:42 -080029void disable_timer(void)
30{
31 struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
Jeff Dike4c9e1382007-10-16 01:26:54 -070032
33 if ((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
34 (setitimer(ITIMER_REAL, &disable, NULL) < 0))
35 printk(UM_KERN_ERR "disable_timer - setitimer failed, "
36 "errno = %d\n", errno);
37
Gennady Sharapovcff65c42006-01-18 17:42:42 -080038 /* If there are signals already queued, after unblocking ignore them */
Jeff Dike4b84c692006-09-25 23:33:04 -070039 signal(SIGALRM, SIG_IGN);
40 signal(SIGVTALRM, SIG_IGN);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080041}
42
43void switch_timers(int to_real)
44{
45 struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
46 struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
47 { 0, 1000000/hz() }});
48 int old, new;
49
Jeff Dike4c9e1382007-10-16 01:26:54 -070050 if (to_real) {
Gennady Sharapovcff65c42006-01-18 17:42:42 -080051 old = ITIMER_VIRTUAL;
52 new = ITIMER_REAL;
53 }
54 else {
55 old = ITIMER_REAL;
56 new = ITIMER_VIRTUAL;
57 }
58
Jeff Dike4c9e1382007-10-16 01:26:54 -070059 if ((setitimer(old, &disable, NULL) < 0) ||
60 (setitimer(new, &enable, NULL)))
61 printk(UM_KERN_ERR "switch_timers - setitimer failed, "
62 "errno = %d\n", errno);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080063}
64
Gennady Sharapovcff65c42006-01-18 17:42:42 -080065unsigned long long os_nsecs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 struct timeval tv;
68
69 gettimeofday(&tv, NULL);
Jeff Dike4c9e1382007-10-16 01:26:54 -070070 return (unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Gennady Sharapovcff65c42006-01-18 17:42:42 -080073void idle_sleep(int secs)
74{
75 struct timespec ts;
76
77 ts.tv_sec = secs;
78 ts.tv_nsec = 0;
79 nanosleep(&ts, NULL);
80}