time.c 4.2 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
 * Licensed under the GPL
 */

#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/unistd.h"
#include "linux/stddef.h"
#include "linux/spinlock.h"
#include "linux/time.h"
#include "linux/sched.h"
#include "linux/interrupt.h"
#include "linux/init.h"
#include "linux/delay.h"
16
#include "linux/hrtimer.h"
L
Linus Torvalds 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include "asm/irq.h"
#include "asm/param.h"
#include "asm/current.h"
#include "kern_util.h"
#include "mode.h"
#include "os.h"

int hz(void)
{
	return(HZ);
}

/*
 * Scheduler clock - returns current time in nanosec units.
 */
unsigned long long sched_clock(void)
{
	return (unsigned long long)jiffies_64 * (1000000000 / HZ);
}

#ifdef CONFIG_UML_REAL_TIME_CLOCK
J
Jeff Dike 已提交
38
static unsigned long long prev_nsecs[NR_CPUS];
J
Jeff Dike 已提交
39
static long long delta[NR_CPUS];		/* Deviation per interval */
L
Linus Torvalds 已提交
40 41 42 43 44 45
#endif

void timer_irq(union uml_pt_regs *regs)
{
	unsigned long long ticks = 0;
#ifdef CONFIG_UML_REAL_TIME_CLOCK
J
Jeff Dike 已提交
46 47
	int c = cpu();
	if(prev_nsecs[c]){
L
Linus Torvalds 已提交
48
		/* We've had 1 tick */
49
		unsigned long long nsecs = os_nsecs();
L
Linus Torvalds 已提交
50

J
Jeff Dike 已提交
51 52
		delta[c] += nsecs - prev_nsecs[c];
		prev_nsecs[c] = nsecs;
L
Linus Torvalds 已提交
53 54

		/* Protect against the host clock being set backwards */
J
Jeff Dike 已提交
55 56
		if(delta[c] < 0)
			delta[c] = 0;
L
Linus Torvalds 已提交
57

J
Jeff Dike 已提交
58 59
		ticks += (delta[c] * HZ) / BILLION;
		delta[c] -= (ticks * BILLION) / HZ;
J
Jeff Dike 已提交
60
	}
J
Jeff Dike 已提交
61
	else prev_nsecs[c] = os_nsecs();
L
Linus Torvalds 已提交
62
#else
J
Jeff Dike 已提交
63
	ticks = 1;
L
Linus Torvalds 已提交
64 65 66 67 68 69 70
#endif
	while(ticks > 0){
		do_IRQ(TIMER_IRQ, regs);
		ticks--;
	}
}

J
Jeff Dike 已提交
71
/* Protects local_offset */
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
static DEFINE_SPINLOCK(timer_spinlock);
static unsigned long long local_offset = 0;

static inline unsigned long long get_time(void)
{
	unsigned long long nsecs;
	unsigned long flags;

	spin_lock_irqsave(&timer_spinlock, flags);
	nsecs = os_nsecs();
	nsecs += local_offset;
	spin_unlock_irqrestore(&timer_spinlock, flags);

	return nsecs;
}

A
Al Viro 已提交
88
irqreturn_t um_timer(int irq, void *dev)
L
Linus Torvalds 已提交
89
{
90
	unsigned long long nsecs;
L
Linus Torvalds 已提交
91 92
	unsigned long flags;

93 94
	write_seqlock_irqsave(&xtime_lock, flags);

95
	do_timer(1);
96

J
Jeff Dike 已提交
97
#ifdef CONFIG_UML_REAL_TIME_CLOCK
J
Jeff Dike 已提交
98
	nsecs = get_time();
J
Jeff Dike 已提交
99 100 101 102
#else
	nsecs = (unsigned long long) xtime.tv_sec * BILLION + xtime.tv_nsec +
		BILLION / HZ;
#endif
103 104
	xtime.tv_sec = nsecs / NSEC_PER_SEC;
	xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
105

L
Linus Torvalds 已提交
106
	write_sequnlock_irqrestore(&xtime_lock, flags);
107

108
	return IRQ_HANDLED;
L
Linus Torvalds 已提交
109 110
}

111 112 113 114 115 116
static void register_timer(void)
{
	int err;

	err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
	if(err != 0)
J
Jeff Dike 已提交
117
		printk(KERN_ERR "register_timer : request_irq failed - "
118 119
		       "errno = %d\n", -err);

J
Jeff Dike 已提交
120 121 122 123
	err = set_interval(1);
	if(err != 0)
		printk(KERN_ERR "register_timer : set_interval failed - "
		       "errno = %d\n", -err);
124 125 126 127 128 129 130 131 132 133 134
}

extern void (*late_time_init)(void);

void time_init(void)
{
	long long nsecs;

	nsecs = os_nsecs();
	set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
				-nsecs % BILLION);
J
Jeff Dike 已提交
135
	set_normalized_timespec(&xtime, nsecs / BILLION, nsecs % BILLION);
136 137 138
	late_time_init = register_timer;
}

139 140
void do_gettimeofday(struct timeval *tv)
{
J
Jeff Dike 已提交
141
#ifdef CONFIG_UML_REAL_TIME_CLOCK
142
	unsigned long long nsecs = get_time();
J
Jeff Dike 已提交
143 144 145 146
#else
	unsigned long long nsecs = (unsigned long long) xtime.tv_sec * BILLION +
		xtime.tv_nsec;
#endif
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	tv->tv_sec = nsecs / NSEC_PER_SEC;
	/* Careful about calculations here - this was originally done as
	 * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
	 * which gave bogus (> 1000000) values.  Dunno why, suspect gcc
	 * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
	 * problem that I missed.
	 */
	nsecs -= tv->tv_sec * NSEC_PER_SEC;
	tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
}

static inline void set_time(unsigned long long nsecs)
{
	unsigned long long now;
	unsigned long flags;

	spin_lock_irqsave(&timer_spinlock, flags);
	now = os_nsecs();
	local_offset = nsecs - now;
	spin_unlock_irqrestore(&timer_spinlock, flags);

	clock_was_set();
L
Linus Torvalds 已提交
169 170
}

171 172 173 174
int do_settimeofday(struct timespec *tv)
{
	set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);

L
Linus Torvalds 已提交
175 176 177 178 179 180
	return 0;
}

void timer_handler(int sig, union uml_pt_regs *regs)
{
	local_irq_disable();
J
Jeff Dike 已提交
181
	irq_enter();
182 183 184
	update_process_times(CHOOSE_MODE(
	                     (UPT_SC(regs) && user_context(UPT_SP(regs))),
			     (regs)->skas.is_user));
J
Jeff Dike 已提交
185
	irq_exit();
L
Linus Torvalds 已提交
186 187 188 189
	local_irq_enable();
	if(current_thread->cpu == 0)
		timer_irq(regs);
}