Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 1 | /* |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike{addtoit,linux.intel}.com) |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame] | 6 | #include <stddef.h> |
| 7 | #include <errno.h> |
| 8 | #include <signal.h> |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 9 | #include <time.h> |
| 10 | #include <sys/time.h> |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 11 | #include "kern_constants.h" |
| 12 | #include "os.h" |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame] | 13 | #include "user.h" |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 14 | |
Jeff Dike | 181bde8 | 2007-10-16 01:27:22 -0700 | [diff] [blame] | 15 | static int is_real_timer = 0; |
| 16 | |
Jeff Dike | a2f018b | 2007-10-16 01:27:22 -0700 | [diff] [blame^] | 17 | int set_interval(void) |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 18 | { |
Jeff Dike | 532d0fa | 2007-10-16 01:27:21 -0700 | [diff] [blame] | 19 | int usec = 1000000/UM_HZ; |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 20 | struct itimerval interval = ((struct itimerval) { { 0, usec }, |
| 21 | { 0, usec } }); |
| 22 | |
Jeff Dike | a2f018b | 2007-10-16 01:27:22 -0700 | [diff] [blame^] | 23 | if (setitimer(ITIMER_VIRTUAL, &interval, NULL) == -1) |
Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 24 | return -errno; |
| 25 | |
| 26 | return 0; |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 27 | } |
| 28 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 29 | void disable_timer(void) |
| 30 | { |
| 31 | struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }}); |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame] | 32 | |
| 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 Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 38 | /* If there are signals already queued, after unblocking ignore them */ |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 39 | signal(SIGALRM, SIG_IGN); |
| 40 | signal(SIGVTALRM, SIG_IGN); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Jeff Dike | 181bde8 | 2007-10-16 01:27:22 -0700 | [diff] [blame] | 43 | int switch_timers(int to_real) |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 44 | { |
| 45 | struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }}); |
Jeff Dike | 181bde8 | 2007-10-16 01:27:22 -0700 | [diff] [blame] | 46 | struct itimerval enable; |
| 47 | int old, new, old_type = is_real_timer; |
| 48 | |
| 49 | if(to_real == old_type) |
| 50 | return to_real; |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 51 | |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame] | 52 | if (to_real) { |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 53 | old = ITIMER_VIRTUAL; |
| 54 | new = ITIMER_REAL; |
| 55 | } |
| 56 | else { |
| 57 | old = ITIMER_REAL; |
| 58 | new = ITIMER_VIRTUAL; |
| 59 | } |
| 60 | |
Jeff Dike | 181bde8 | 2007-10-16 01:27:22 -0700 | [diff] [blame] | 61 | if (setitimer(old, &disable, &enable) < 0) |
| 62 | printk(UM_KERN_ERR "switch_timers - setitimer disable failed, " |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame] | 63 | "errno = %d\n", errno); |
Jeff Dike | 181bde8 | 2007-10-16 01:27:22 -0700 | [diff] [blame] | 64 | |
| 65 | if((enable.it_value.tv_sec == 0) && (enable.it_value.tv_usec == 0)) |
| 66 | enable.it_value = enable.it_interval; |
| 67 | |
| 68 | if (setitimer(new, &enable, NULL)) |
| 69 | printk(UM_KERN_ERR "switch_timers - setitimer enable failed, " |
| 70 | "errno = %d\n", errno); |
| 71 | |
| 72 | is_real_timer = to_real; |
| 73 | return old_type; |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 76 | unsigned long long os_nsecs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
| 78 | struct timeval tv; |
| 79 | |
| 80 | gettimeofday(&tv, NULL); |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame] | 81 | return (unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 84 | void idle_sleep(int secs) |
| 85 | { |
| 86 | struct timespec ts; |
| 87 | |
| 88 | ts.tv_sec = secs; |
| 89 | ts.tv_nsec = 0; |
| 90 | nanosleep(&ts, NULL); |
| 91 | } |