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> |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 13 | #include "kern_util.h" |
| 14 | #include "user.h" |
| 15 | #include "process.h" |
| 16 | #include "kern_constants.h" |
| 17 | #include "os.h" |
Paolo 'Blaisorblade' Giarrusso | 493e375 | 2006-10-19 23:28:22 -0700 | [diff] [blame] | 18 | #include "uml-config.h" |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 19 | |
Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 20 | int set_interval(int is_virtual) |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 21 | { |
| 22 | int usec = 1000000/hz(); |
Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 23 | int timer_type = is_virtual ? ITIMER_VIRTUAL : ITIMER_REAL; |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 24 | struct itimerval interval = ((struct itimerval) { { 0, usec }, |
| 25 | { 0, usec } }); |
| 26 | |
| 27 | if(setitimer(timer_type, &interval, NULL) == -1) |
Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 28 | return -errno; |
| 29 | |
| 30 | return 0; |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 33 | void disable_timer(void) |
| 34 | { |
| 35 | struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }}); |
| 36 | if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) || |
| 37 | (setitimer(ITIMER_REAL, &disable, NULL) < 0)) |
| 38 | printk("disnable_timer - setitimer failed, errno = %d\n", |
| 39 | errno); |
| 40 | /* If there are signals already queued, after unblocking ignore them */ |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 41 | signal(SIGALRM, SIG_IGN); |
| 42 | signal(SIGVTALRM, SIG_IGN); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void switch_timers(int to_real) |
| 46 | { |
| 47 | struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }}); |
| 48 | struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() }, |
| 49 | { 0, 1000000/hz() }}); |
| 50 | int old, new; |
| 51 | |
| 52 | if(to_real){ |
| 53 | old = ITIMER_VIRTUAL; |
| 54 | new = ITIMER_REAL; |
| 55 | } |
| 56 | else { |
| 57 | old = ITIMER_REAL; |
| 58 | new = ITIMER_VIRTUAL; |
| 59 | } |
| 60 | |
| 61 | if((setitimer(old, &disable, NULL) < 0) || |
| 62 | (setitimer(new, &enable, NULL))) |
| 63 | printk("switch_timers - setitimer failed, errno = %d\n", |
| 64 | errno); |
| 65 | } |
| 66 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 67 | unsigned long long os_nsecs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | struct timeval tv; |
| 70 | |
| 71 | gettimeofday(&tv, NULL); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 72 | return((unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 75 | void idle_sleep(int secs) |
| 76 | { |
| 77 | struct timespec ts; |
| 78 | |
| 79 | ts.tv_sec = secs; |
| 80 | ts.tv_nsec = 0; |
| 81 | nanosleep(&ts, NULL); |
| 82 | } |