| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com) | 
|  | 3 | * Licensed under the GPL | 
|  | 4 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 |  | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 6 | #include <stdio.h> | 
|  | 7 | #include <stdlib.h> | 
|  | 8 | #include <unistd.h> | 
|  | 9 | #include <time.h> | 
|  | 10 | #include <sys/time.h> | 
|  | 11 | #include <signal.h> | 
|  | 12 | #include <errno.h> | 
|  | 13 | #include "user_util.h" | 
|  | 14 | #include "kern_util.h" | 
|  | 15 | #include "user.h" | 
|  | 16 | #include "process.h" | 
|  | 17 | #include "kern_constants.h" | 
|  | 18 | #include "os.h" | 
| Paolo 'Blaisorblade' Giarrusso | 493e375 | 2006-10-19 23:28:22 -0700 | [diff] [blame] | 19 | #include "uml-config.h" | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 20 |  | 
| Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 21 | int set_interval(int is_virtual) | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 22 | { | 
|  | 23 | int usec = 1000000/hz(); | 
| Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 24 | int timer_type = is_virtual ? ITIMER_VIRTUAL : ITIMER_REAL; | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 25 | struct itimerval interval = ((struct itimerval) { { 0, usec }, | 
|  | 26 | { 0, usec } }); | 
|  | 27 |  | 
|  | 28 | if(setitimer(timer_type, &interval, NULL) == -1) | 
| Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 29 | return -errno; | 
|  | 30 |  | 
|  | 31 | return 0; | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 32 | } | 
|  | 33 |  | 
| Paolo 'Blaisorblade' Giarrusso | 493e375 | 2006-10-19 23:28:22 -0700 | [diff] [blame] | 34 | #ifdef UML_CONFIG_MODE_TT | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 35 | void enable_timer(void) | 
|  | 36 | { | 
| Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 37 | set_interval(1); | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 38 | } | 
| Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 39 | #endif | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 40 |  | 
|  | 41 | void disable_timer(void) | 
|  | 42 | { | 
|  | 43 | struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }}); | 
|  | 44 | if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) || | 
|  | 45 | (setitimer(ITIMER_REAL, &disable, NULL) < 0)) | 
|  | 46 | printk("disnable_timer - setitimer failed, errno = %d\n", | 
|  | 47 | errno); | 
|  | 48 | /* If there are signals already queued, after unblocking ignore them */ | 
| Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 49 | signal(SIGALRM, SIG_IGN); | 
|  | 50 | signal(SIGVTALRM, SIG_IGN); | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
|  | 53 | void switch_timers(int to_real) | 
|  | 54 | { | 
|  | 55 | struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }}); | 
|  | 56 | struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() }, | 
|  | 57 | { 0, 1000000/hz() }}); | 
|  | 58 | int old, new; | 
|  | 59 |  | 
|  | 60 | if(to_real){ | 
|  | 61 | old = ITIMER_VIRTUAL; | 
|  | 62 | new = ITIMER_REAL; | 
|  | 63 | } | 
|  | 64 | else { | 
|  | 65 | old = ITIMER_REAL; | 
|  | 66 | new = ITIMER_VIRTUAL; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | if((setitimer(old, &disable, NULL) < 0) || | 
|  | 70 | (setitimer(new, &enable, NULL))) | 
|  | 71 | printk("switch_timers - setitimer failed, errno = %d\n", | 
|  | 72 | errno); | 
|  | 73 | } | 
|  | 74 |  | 
| Jeff Dike | bacf454 | 2006-07-10 04:45:09 -0700 | [diff] [blame] | 75 | #ifdef UML_CONFIG_MODE_TT | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 76 | void uml_idle_timer(void) | 
|  | 77 | { | 
|  | 78 | if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR) | 
|  | 79 | panic("Couldn't unset SIGVTALRM handler"); | 
|  | 80 |  | 
|  | 81 | set_handler(SIGALRM, (__sighandler_t) alarm_handler, | 
|  | 82 | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1); | 
| Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 83 | set_interval(0); | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 84 | } | 
| Jeff Dike | bacf454 | 2006-07-10 04:45:09 -0700 | [diff] [blame] | 85 | #endif | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 86 |  | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 87 | unsigned long long os_nsecs(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { | 
|  | 89 | struct timeval tv; | 
|  | 90 |  | 
|  | 91 | gettimeofday(&tv, NULL); | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 92 | return((unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 95 | void idle_sleep(int secs) | 
|  | 96 | { | 
|  | 97 | struct timespec ts; | 
|  | 98 |  | 
|  | 99 | ts.tv_sec = secs; | 
|  | 100 | ts.tv_nsec = 0; | 
|  | 101 | nanosleep(&ts, NULL); | 
|  | 102 | } |