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" |
| 19 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 20 | static void set_interval(int timer_type) |
| 21 | { |
| 22 | int usec = 1000000/hz(); |
| 23 | struct itimerval interval = ((struct itimerval) { { 0, usec }, |
| 24 | { 0, usec } }); |
| 25 | |
| 26 | if(setitimer(timer_type, &interval, NULL) == -1) |
| 27 | panic("setitimer failed - errno = %d\n", errno); |
| 28 | } |
| 29 | |
| 30 | void enable_timer(void) |
| 31 | { |
| 32 | set_interval(ITIMER_VIRTUAL); |
| 33 | } |
| 34 | |
| 35 | void disable_timer(void) |
| 36 | { |
| 37 | struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }}); |
| 38 | if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) || |
| 39 | (setitimer(ITIMER_REAL, &disable, NULL) < 0)) |
| 40 | printk("disnable_timer - setitimer failed, errno = %d\n", |
| 41 | errno); |
| 42 | /* If there are signals already queued, after unblocking ignore them */ |
| 43 | set_handler(SIGALRM, SIG_IGN, 0, -1); |
| 44 | set_handler(SIGVTALRM, SIG_IGN, 0, -1); |
| 45 | } |
| 46 | |
| 47 | void switch_timers(int to_real) |
| 48 | { |
| 49 | struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }}); |
| 50 | struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() }, |
| 51 | { 0, 1000000/hz() }}); |
| 52 | int old, new; |
| 53 | |
| 54 | if(to_real){ |
| 55 | old = ITIMER_VIRTUAL; |
| 56 | new = ITIMER_REAL; |
| 57 | } |
| 58 | else { |
| 59 | old = ITIMER_REAL; |
| 60 | new = ITIMER_VIRTUAL; |
| 61 | } |
| 62 | |
| 63 | if((setitimer(old, &disable, NULL) < 0) || |
| 64 | (setitimer(new, &enable, NULL))) |
| 65 | printk("switch_timers - setitimer failed, errno = %d\n", |
| 66 | errno); |
| 67 | } |
| 68 | |
Jeff Dike | bacf454 | 2006-07-10 04:45:09 -0700 | [diff] [blame] | 69 | #ifdef UML_CONFIG_MODE_TT |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 70 | void uml_idle_timer(void) |
| 71 | { |
| 72 | if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR) |
| 73 | panic("Couldn't unset SIGVTALRM handler"); |
| 74 | |
| 75 | set_handler(SIGALRM, (__sighandler_t) alarm_handler, |
| 76 | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1); |
| 77 | set_interval(ITIMER_REAL); |
| 78 | } |
Jeff Dike | bacf454 | 2006-07-10 04:45:09 -0700 | [diff] [blame] | 79 | #endif |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 80 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 81 | unsigned long long os_nsecs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| 83 | struct timeval tv; |
| 84 | |
| 85 | gettimeofday(&tv, NULL); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 86 | return((unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 89 | void idle_sleep(int secs) |
| 90 | { |
| 91 | struct timespec ts; |
| 92 | |
| 93 | ts.tv_sec = secs; |
| 94 | ts.tv_nsec = 0; |
| 95 | nanosleep(&ts, NULL); |
| 96 | } |
| 97 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 98 | void user_time_init(void) |
| 99 | { |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 100 | set_interval(ITIMER_VIRTUAL); |
| 101 | } |