blob: 4ae73c0e54850c4068e1ddf0cf9afc33f6e96239 [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
Gennady Sharapovcff65c42006-01-18 17:42:42 -080020static 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
30void enable_timer(void)
31{
32 set_interval(ITIMER_VIRTUAL);
33}
34
35void 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
47void 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 Dikebacf4542006-07-10 04:45:09 -070069#ifdef UML_CONFIG_MODE_TT
Gennady Sharapovcff65c42006-01-18 17:42:42 -080070void 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 Dikebacf4542006-07-10 04:45:09 -070079#endif
Gennady Sharapovcff65c42006-01-18 17:42:42 -080080
Gennady Sharapovcff65c42006-01-18 17:42:42 -080081unsigned long long os_nsecs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct timeval tv;
84
85 gettimeofday(&tv, NULL);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080086 return((unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Gennady Sharapovcff65c42006-01-18 17:42:42 -080089void 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 Sharapovcff65c42006-01-18 17:42:42 -080098void user_time_init(void)
99{
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800100 set_interval(ITIMER_VIRTUAL);
101}