blob: 31fb3235f55205beebaca4c84ec0a55426e1e75b [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
69void uml_idle_timer(void)
70{
71 if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
72 panic("Couldn't unset SIGVTALRM handler");
73
74 set_handler(SIGALRM, (__sighandler_t) alarm_handler,
75 SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
76 set_interval(ITIMER_REAL);
77}
78
Gennady Sharapovcff65c42006-01-18 17:42:42 -080079unsigned long long os_nsecs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 struct timeval tv;
82
83 gettimeofday(&tv, NULL);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080084 return((unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Gennady Sharapovcff65c42006-01-18 17:42:42 -080087void idle_sleep(int secs)
88{
89 struct timespec ts;
90
91 ts.tv_sec = secs;
92 ts.tv_nsec = 0;
93 nanosleep(&ts, NULL);
94}
95
Gennady Sharapovcff65c42006-01-18 17:42:42 -080096void user_time_init(void)
97{
Gennady Sharapovcff65c42006-01-18 17:42:42 -080098 set_interval(ITIMER_VIRTUAL);
99}