blob: 38be096e750f0ec98d4887275f9f48abc274be3b [file] [log] [blame]
Gennady Sharapovcff65c42006-01-18 17:42:42 -08001/*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
Gennady Sharapovcff65c42006-01-18 17:42:42 -08006#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
Jeff Dike537ae942006-09-25 23:33:05 -070020int set_interval(int is_virtual)
Gennady Sharapovcff65c42006-01-18 17:42:42 -080021{
22 int usec = 1000000/hz();
Jeff Dike537ae942006-09-25 23:33:05 -070023 int timer_type = is_virtual ? ITIMER_VIRTUAL : ITIMER_REAL;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080024 struct itimerval interval = ((struct itimerval) { { 0, usec },
25 { 0, usec } });
26
27 if(setitimer(timer_type, &interval, NULL) == -1)
Jeff Dike537ae942006-09-25 23:33:05 -070028 return -errno;
29
30 return 0;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080031}
32
Jeff Dike537ae942006-09-25 23:33:05 -070033#ifdef CONFIG_MODE_TT
Gennady Sharapovcff65c42006-01-18 17:42:42 -080034void enable_timer(void)
35{
Jeff Dike537ae942006-09-25 23:33:05 -070036 set_interval(1);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080037}
Jeff Dike537ae942006-09-25 23:33:05 -070038#endif
Gennady Sharapovcff65c42006-01-18 17:42:42 -080039
40void disable_timer(void)
41{
42 struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
43 if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
44 (setitimer(ITIMER_REAL, &disable, NULL) < 0))
45 printk("disnable_timer - setitimer failed, errno = %d\n",
46 errno);
47 /* If there are signals already queued, after unblocking ignore them */
Jeff Dike4b84c692006-09-25 23:33:04 -070048 signal(SIGALRM, SIG_IGN);
49 signal(SIGVTALRM, SIG_IGN);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080050}
51
52void switch_timers(int to_real)
53{
54 struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
55 struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
56 { 0, 1000000/hz() }});
57 int old, new;
58
59 if(to_real){
60 old = ITIMER_VIRTUAL;
61 new = ITIMER_REAL;
62 }
63 else {
64 old = ITIMER_REAL;
65 new = ITIMER_VIRTUAL;
66 }
67
68 if((setitimer(old, &disable, NULL) < 0) ||
69 (setitimer(new, &enable, NULL)))
70 printk("switch_timers - setitimer failed, errno = %d\n",
71 errno);
72}
73
Jeff Dikebacf4542006-07-10 04:45:09 -070074#ifdef UML_CONFIG_MODE_TT
Gennady Sharapovcff65c42006-01-18 17:42:42 -080075void uml_idle_timer(void)
76{
77 if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
78 panic("Couldn't unset SIGVTALRM handler");
79
80 set_handler(SIGALRM, (__sighandler_t) alarm_handler,
81 SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
Jeff Dike537ae942006-09-25 23:33:05 -070082 set_interval(0);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080083}
Jeff Dikebacf4542006-07-10 04:45:09 -070084#endif
Gennady Sharapovcff65c42006-01-18 17:42:42 -080085
Gennady Sharapovcff65c42006-01-18 17:42:42 -080086unsigned long long os_nsecs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 struct timeval tv;
89
90 gettimeofday(&tv, NULL);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080091 return((unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Gennady Sharapovcff65c42006-01-18 17:42:42 -080094void idle_sleep(int secs)
95{
96 struct timespec ts;
97
98 ts.tv_sec = secs;
99 ts.tv_nsec = 0;
100 nanosleep(&ts, NULL);
101}