📄 time.c
字号:
/* * linux/arch/x86-64/kernel/time.c * * "High Precision Event Timer" based timekeeping. * * Copyright (c) 1991,1992,1995 Linus Torvalds * Copyright (c) 1994 Alan Modra * Copyright (c) 1995 Markus Kuhn * Copyright (c) 1996 Ingo Molnar * Copyright (c) 1998 Andrea Arcangeli * Copyright (c) 2002 Vojtech Pavlik * Copyright (c) 2003 Andi Kleen * RTC support code taken from arch/i386/kernel/timers/time_hpet.c */#include <linux/kernel.h>#include <linux/sched.h>#include <linux/interrupt.h>#include <linux/init.h>#include <linux/mc146818rtc.h>#include <linux/time.h>#include <linux/ioport.h>#include <linux/module.h>#include <linux/device.h>#include <linux/sysdev.h>#include <linux/bcd.h>#include <linux/kallsyms.h>#include <linux/acpi.h>#ifdef CONFIG_ACPI#include <acpi/achware.h> /* for PM timer frequency */#endif#include <asm/8253pit.h>#include <asm/pgtable.h>#include <asm/vsyscall.h>#include <asm/timex.h>#include <asm/proto.h>#include <asm/hpet.h>#include <asm/sections.h>#include <linux/cpufreq.h>#include <linux/hpet.h>#ifdef CONFIG_X86_LOCAL_APIC#include <asm/apic.h>#endif#ifdef CONFIG_CPU_FREQstatic void cpufreq_delayed_get(void);#endifextern void i8254_timer_resume(void);extern int using_apic_timer;DEFINE_SPINLOCK(rtc_lock);DEFINE_SPINLOCK(i8253_lock);static int nohpet __initdata = 0;static int notsc __initdata = 0;#undef HPET_HACK_ENABLE_DANGEROUSunsigned int cpu_khz; /* TSC clocks / usec, not used here */static unsigned long hpet_period; /* fsecs / HPET clock */unsigned long hpet_tick; /* HPET clocks / interrupt */static int hpet_use_timer; /* Use counter of hpet for time keeping, otherwise PIT */unsigned long vxtime_hz = PIT_TICK_RATE;int report_lost_ticks; /* command line option */unsigned long long monotonic_base;struct vxtime_data __vxtime __section_vxtime; /* for vsyscalls */volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;unsigned long __wall_jiffies __section_wall_jiffies = INITIAL_JIFFIES;struct timespec __xtime __section_xtime;struct timezone __sys_tz __section_sys_tz;static inline void rdtscll_sync(unsigned long *tsc){#ifdef CONFIG_SMP sync_core();#endif rdtscll(*tsc);}/* * do_gettimeoffset() returns microseconds since last timer interrupt was * triggered by hardware. A memory read of HPET is slower than a register read * of TSC, but much more reliable. It's also synchronized to the timer * interrupt. Note that do_gettimeoffset() may return more than hpet_tick, if a * timer interrupt has happened already, but vxtime.trigger wasn't updated yet. * This is not a problem, because jiffies hasn't updated either. They are bound * together by xtime_lock. */static inline unsigned int do_gettimeoffset_tsc(void){ unsigned long t; unsigned long x; rdtscll_sync(&t); if (t < vxtime.last_tsc) t = vxtime.last_tsc; /* hack */ x = ((t - vxtime.last_tsc) * vxtime.tsc_quot) >> 32; return x;}static inline unsigned int do_gettimeoffset_hpet(void){ /* cap counter read to one tick to avoid inconsistencies */ unsigned long counter = hpet_readl(HPET_COUNTER) - vxtime.last; return (min(counter,hpet_tick) * vxtime.quot) >> 32;}unsigned int (*do_gettimeoffset)(void) = do_gettimeoffset_tsc;/* * This version of gettimeofday() has microsecond resolution and better than * microsecond precision, as we're using at least a 10 MHz (usually 14.31818 * MHz) HPET timer. */void do_gettimeofday(struct timeval *tv){ unsigned long seq, t; unsigned int sec, usec; do { seq = read_seqbegin(&xtime_lock); sec = xtime.tv_sec; usec = xtime.tv_nsec / 1000; /* i386 does some correction here to keep the clock monotonous even when ntpd is fixing drift. But they didn't work for me, there is a non monotonic clock anyways with ntp. I dropped all corrections now until a real solution can be found. Note when you fix it here you need to do the same in arch/x86_64/kernel/vsyscall.c and export all needed variables in vmlinux.lds. -AK */ t = (jiffies - wall_jiffies) * (1000000L / HZ) + do_gettimeoffset(); usec += t; } while (read_seqretry(&xtime_lock, seq)); tv->tv_sec = sec + usec / 1000000; tv->tv_usec = usec % 1000000;}EXPORT_SYMBOL(do_gettimeofday);/* * settimeofday() first undoes the correction that gettimeofday would do * on the time, and then saves it. This is ugly, but has been like this for * ages already. */int do_settimeofday(struct timespec *tv){ time_t wtm_sec, sec = tv->tv_sec; long wtm_nsec, nsec = tv->tv_nsec; if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) return -EINVAL; write_seqlock_irq(&xtime_lock); nsec -= do_gettimeoffset() * 1000 + (jiffies - wall_jiffies) * (NSEC_PER_SEC/HZ); wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec); wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec); set_normalized_timespec(&xtime, sec, nsec); set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec); ntp_clear(); write_sequnlock_irq(&xtime_lock); clock_was_set(); return 0;}EXPORT_SYMBOL(do_settimeofday);unsigned long profile_pc(struct pt_regs *regs){ unsigned long pc = instruction_pointer(regs); /* Assume the lock function has either no stack frame or only a single word. This checks if the address on the stack looks like a kernel text address. There is a small window for false hits, but in that case the tick is just accounted to the spinlock function. Better would be to write these functions in assembler again and check exactly. */ if (in_lock_functions(pc)) { char *v = *(char **)regs->rsp; if ((v >= _stext && v <= _etext) || (v >= _sinittext && v <= _einittext) || (v >= (char *)MODULES_VADDR && v <= (char *)MODULES_END)) return (unsigned long)v; return ((unsigned long *)regs->rsp)[1]; } return pc;}EXPORT_SYMBOL(profile_pc);/* * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500 * ms after the second nowtime has started, because when nowtime is written * into the registers of the CMOS clock, it will jump to the next second * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data * sheet for details. */static void set_rtc_mmss(unsigned long nowtime){ int real_seconds, real_minutes, cmos_minutes; unsigned char control, freq_select;/* * IRQs are disabled when we're called from the timer interrupt, * no need for spin_lock_irqsave() */ spin_lock(&rtc_lock);/* * Tell the clock it's being set and stop it. */ control = CMOS_READ(RTC_CONTROL); CMOS_WRITE(control | RTC_SET, RTC_CONTROL); freq_select = CMOS_READ(RTC_FREQ_SELECT); CMOS_WRITE(freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT); cmos_minutes = CMOS_READ(RTC_MINUTES); BCD_TO_BIN(cmos_minutes);/* * since we're only adjusting minutes and seconds, don't interfere with hour * overflow. This avoids messing with unknown time zones but requires your RTC * not to be off by more than 15 minutes. Since we're calling it only when * our clock is externally synchronized using NTP, this shouldn't be a problem. */ real_seconds = nowtime % 60; real_minutes = nowtime / 60; if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1) real_minutes += 30; /* correct for half hour time zone */ real_minutes %= 60;#if 0 /* AMD 8111 is a really bad time keeper and hits this regularly. It probably was an attempt to avoid screwing up DST, but ignore that for now. */ if (abs(real_minutes - cmos_minutes) >= 30) { printk(KERN_WARNING "time.c: can't update CMOS clock " "from %d to %d\n", cmos_minutes, real_minutes); } else#endif { BIN_TO_BCD(real_seconds); BIN_TO_BCD(real_minutes); CMOS_WRITE(real_seconds, RTC_SECONDS); CMOS_WRITE(real_minutes, RTC_MINUTES); }/* * The following flags have to be released exactly in this order, otherwise the * DS12887 (popular MC146818A clone with integrated battery and quartz) will * not reset the oscillator and will not update precisely 500 ms later. You * won't find this mentioned in the Dallas Semiconductor data sheets, but who * believes data sheets anyway ... -- Markus Kuhn */ CMOS_WRITE(control, RTC_CONTROL); CMOS_WRITE(freq_select, RTC_FREQ_SELECT); spin_unlock(&rtc_lock);}/* monotonic_clock(): returns # of nanoseconds passed since time_init() * Note: This function is required to return accurate * time even in the absence of multiple timer ticks. */unsigned long long monotonic_clock(void){ unsigned long seq; u32 last_offset, this_offset, offset; unsigned long long base; if (vxtime.mode == VXTIME_HPET) { do { seq = read_seqbegin(&xtime_lock); last_offset = vxtime.last; base = monotonic_base; this_offset = hpet_readl(HPET_COUNTER); } while (read_seqretry(&xtime_lock, seq)); offset = (this_offset - last_offset); offset *=(NSEC_PER_SEC/HZ)/hpet_tick; return base + offset; }else{ do { seq = read_seqbegin(&xtime_lock); last_offset = vxtime.last_tsc; base = monotonic_base; } while (read_seqretry(&xtime_lock, seq)); sync_core(); rdtscll(this_offset); offset = (this_offset - last_offset)*1000/cpu_khz; return base + offset; }}EXPORT_SYMBOL(monotonic_clock);static noinline void handle_lost_ticks(int lost, struct pt_regs *regs){ static long lost_count; static int warned; if (report_lost_ticks) { printk(KERN_WARNING "time.c: Lost %d timer " "tick(s)! ", lost); print_symbol("rip %s)\n", regs->rip); } if (lost_count == 1000 && !warned) { printk(KERN_WARNING "warning: many lost ticks.\n" KERN_WARNING "Your time source seems to be instable or " "some driver is hogging interupts\n"); print_symbol("rip %s\n", regs->rip); if (vxtime.mode == VXTIME_TSC && vxtime.hpet_address) { printk(KERN_WARNING "Falling back to HPET\n"); vxtime.last = hpet_readl(HPET_T0_CMP) - hpet_tick; vxtime.mode = VXTIME_HPET; do_gettimeoffset = do_gettimeoffset_hpet; } /* else should fall back to PIT, but code missing. */ warned = 1; } else lost_count++;#ifdef CONFIG_CPU_FREQ /* In some cases the CPU can change frequency without us noticing (like going into thermal throttle) Give cpufreq a change to catch up. */ if ((lost_count+1) % 25 == 0) { cpufreq_delayed_get(); }#endif}static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs){ static unsigned long rtc_update = 0; unsigned long tsc; int delay, offset = 0, lost = 0;/* * Here we are in the timer irq handler. We have irqs locally disabled (so we * don't need spin_lock_irqsave()) but we don't know if the timer_bh is running * on the other CPU, so we need a lock. We also need to lock the vsyscall * variables, because both do_timer() and us change them -arca+vojtech */ write_seqlock(&xtime_lock); if (vxtime.hpet_address) offset = hpet_readl(HPET_COUNTER); if (hpet_use_timer) { /* if we're using the hpet timer functionality, * we can more accurately know the counter value * when the timer interrupt occured. */ offset = hpet_readl(HPET_T0_CMP) - hpet_tick; delay = hpet_readl(HPET_COUNTER) - offset; } else { spin_lock(&i8253_lock); outb_p(0x00, 0x43); delay = inb_p(0x40); delay |= inb(0x40) << 8; spin_unlock(&i8253_lock); delay = LATCH - 1 - delay; } rdtscll_sync(&tsc); if (vxtime.mode == VXTIME_HPET) { if (offset - vxtime.last > hpet_tick) { lost = (offset - vxtime.last) / hpet_tick - 1; } monotonic_base += (offset - vxtime.last)*(NSEC_PER_SEC/HZ) / hpet_tick; vxtime.last = offset;#ifdef CONFIG_X86_PM_TIMER } else if (vxtime.mode == VXTIME_PMTMR) { lost = pmtimer_mark_offset();#endif } else { offset = (((tsc - vxtime.last_tsc) * vxtime.tsc_quot) >> 32) - (USEC_PER_SEC / HZ); if (offset < 0) offset = 0; if (offset > (USEC_PER_SEC / HZ)) { lost = offset / (USEC_PER_SEC / HZ); offset %= (USEC_PER_SEC / HZ); } monotonic_base += (tsc - vxtime.last_tsc)*1000000/cpu_khz ; vxtime.last_tsc = tsc - vxtime.quot * delay / vxtime.tsc_quot; if ((((tsc - vxtime.last_tsc) * vxtime.tsc_quot) >> 32) < offset) vxtime.last_tsc = tsc - (((long) offset << 32) / vxtime.tsc_quot) - 1; } if (lost > 0) { handle_lost_ticks(lost, regs); jiffies += lost; }/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -