blob: 5939653f99eab45d6a193ab396ffebe53d5a3d4a [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 Dike537ae942006-09-25 23:33:05 -070015int set_interval(int is_virtual)
Gennady Sharapovcff65c42006-01-18 17:42:42 -080016{
Jeff Dike532d0fa2007-10-16 01:27:21 -070017 int usec = 1000000/UM_HZ;
Jeff Dike537ae942006-09-25 23:33:05 -070018 int timer_type = is_virtual ? ITIMER_VIRTUAL : ITIMER_REAL;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080019 struct itimerval interval = ((struct itimerval) { { 0, usec },
20 { 0, usec } });
21
Jeff Dike4c9e1382007-10-16 01:26:54 -070022 if (setitimer(timer_type, &interval, NULL) == -1)
Jeff Dike537ae942006-09-25 23:33:05 -070023 return -errno;
24
25 return 0;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080026}
27
Gennady Sharapovcff65c42006-01-18 17:42:42 -080028void disable_timer(void)
29{
30 struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
Jeff Dike4c9e1382007-10-16 01:26:54 -070031
32 if ((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
33 (setitimer(ITIMER_REAL, &disable, NULL) < 0))
34 printk(UM_KERN_ERR "disable_timer - setitimer failed, "
35 "errno = %d\n", errno);
36
Gennady Sharapovcff65c42006-01-18 17:42:42 -080037 /* If there are signals already queued, after unblocking ignore them */
Jeff Dike4b84c692006-09-25 23:33:04 -070038 signal(SIGALRM, SIG_IGN);
39 signal(SIGVTALRM, SIG_IGN);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080040}
41
42void switch_timers(int to_real)
43{
44 struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
Jeff Dike532d0fa2007-10-16 01:27:21 -070045 struct itimerval enable = ((struct itimerval) { { 0, 1000000/UM_HZ },
46 { 0, 1000000/UM_HZ }});
Gennady Sharapovcff65c42006-01-18 17:42:42 -080047 int old, new;
48
Jeff Dike4c9e1382007-10-16 01:26:54 -070049 if (to_real) {
Gennady Sharapovcff65c42006-01-18 17:42:42 -080050 old = ITIMER_VIRTUAL;
51 new = ITIMER_REAL;
52 }
53 else {
54 old = ITIMER_REAL;
55 new = ITIMER_VIRTUAL;
56 }
57
Jeff Dike4c9e1382007-10-16 01:26:54 -070058 if ((setitimer(old, &disable, NULL) < 0) ||
59 (setitimer(new, &enable, NULL)))
60 printk(UM_KERN_ERR "switch_timers - setitimer failed, "
61 "errno = %d\n", errno);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080062}
63
Gennady Sharapovcff65c42006-01-18 17:42:42 -080064unsigned long long os_nsecs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 struct timeval tv;
67
68 gettimeofday(&tv, NULL);
Jeff Dike4c9e1382007-10-16 01:26:54 -070069 return (unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Gennady Sharapovcff65c42006-01-18 17:42:42 -080072void idle_sleep(int secs)
73{
74 struct timespec ts;
75
76 ts.tv_sec = secs;
77 ts.tv_nsec = 0;
78 nanosleep(&ts, NULL);
79}