6. 1 . Clock and Timer Circuits
On the 80x86 architecture, the kernel must explicitly interact with several kinds of clocks and timer circuits . The clock circuits are used both to keep track of the current time of day and to make precise time measurements. The timer circuits are programmed by the kernel, so that they issue interrupts at a fixed, predefined frequency; such periodic interrupts are crucial for implementing the software timers used by the kernel and the user programs. We'll now briefly describe the clock and hardware circuits that can be found in IBM-compatible PCs.
在80x86架构上,kernel 一定要精确地和一些时钟和定时器电路进行交互。时钟周期被同时用作来跟踪当前日期的时间和作出精确的时间测量。定时器电路可以被kernel编程,所以他们发出一个固定的预先定义的频率的中断;这些定时中断对于被内核和用户使用的软件计时器是至关重要。我们现在清楚的描述了可以在IBM兼容机中可以找到此时钟和硬件电路。
6.1.1. Real Time Clock (RTC)
All PCs include a clock called Real Time Clock (RTC), which is independent of the CPU and all other chips.
The RTC continues to tick even when the PC is switched off, because it is energized by a small battery. The CMOS RAM and RTC are integrated in a single chip (the Motorola 146818 or an equivalent).
实时时钟(RTC)
所有的个人计算机包含一个叫实时时钟的时钟,此时钟是独立于cpu和所有其他的芯片的。
这实时时钟当计算机断电仍然持续滴答。因为它是靠一块小的电池来供电的。这CMOS 随机存储器和 实时时钟在(MOTOROLA146818或者等类型)被整合到一个芯片上。
The RTC is capable of issuing periodic interrupts on IRQ 8 at frequencies ranging between 2 Hz and 8,192 Hz. It can also be programmed to activate the IRQ 8 line when the RTC reaches a specific value, thus working as an alarm clock.
这实时时钟有发出频率在 2HZ到 8192 HZ 之间任何频率的 定时中断到8号请求中断。它可以被编程来激活8号中断线,当实时时钟受到一个特别的值,来以闹钟的形式工作。
(引用)
IRQ8
IRQ Number: 8
16-Bit Priority: 3 /*优先级3*/
Bus Line: No
Typical Default Use: Real-time clock.
Other Common Uses: None; for system use only. 其他的通用使用:只为系统使用
Description: This is the reserved interrupt for the real-time clock timer. This
timer is used by software programs to manage events that must be calibrated to real-world
time; this is done by setting "alarms", which trigger this interrupt at a
specified time. For example, if you are using an electronic datebook and have it set to
pop up screen messages or beep the PC when it is time for a meeting, the software will set
a timer to count down to the appropriate time. When the timer finishes its countdown, an
interrupt will be generated on IRQ8.
描述:这是为实时时钟保留的中断。这个计数器被应用程序使用来管理必须被真实世界时间校准的事件。用设置时钟来完成的,这样来在一个特殊的时间上触发中断。比如,如果用一本电子行事历当是开会的时间,使它来在屏幕上显示信息或者让PC蜂鸣。这个软件将会设定计时器去递减到合适的时间。当计时器完成递减操作,将会产生IRQ8号中断。
Conflicts: This is a dedicated interrupt line; there should never be any conflicts.
If software indicates a conflict on this IRQ, there is a good possibility of a hardware
problem somewhere on your system board.
冲突: 这是一个专用的中断线;应该没有任何冲突。如果软件在此IRQ的中断,很有可能出现在你系统板上出现了硬件的问题。
Linux uses the RTC only to derive the time and date; however, it allows processes to program the RTC by acting on the /dev/rtc device file (see Chapter 13). The kernel accesses the RTC through the 0x70 and 0x71 I/O ports. The system administrator can read and write the RTC by executing the clock Unix system program that acts directly on these two I/O ports.
Linux 用实时时钟只用来获得时间和日期;但是,它允许程序对RTC编程通过在/dev/rtc设备文件中进行操作。这个kernel 通过0x70和0x71 i/o端口访问实时时钟。系统管理员可以通过在这两个端口执行时钟的unix系统程序直接读或者写实时时钟。
6.1.2 时间戳计数器
All 80x86 microprocessors include a CLK input pin, which receives the clock signal of an external oscillator. Starting with the Pentium, 80x86 microprocessors sport a counter that is increased at each clock signal. The counter is accessible through the 64-bit Time Stamp Counter(TSC) register, which can be read by means of the rdtsc assembly language instruction. When using this register, the kernel has to take into consideration the frequency of the clock signal: if, for instance, the clock ticks at 1 GHz, the Time Stamp Counter is increased once every nanosecond.
所有的80x86微处理器在输入针包含一个CLK,它接受时额外的是石英器时钟信号。从奔腾开始,80x86微处理器发布了一个在每一个时钟信号增加的计数器。这个计数器通过64-bit时间戳访问。时间戳计数器寄存器,可以被各种各样的汇编语言读。当使用寄存器时,kernel会考虑时钟信号的频率;例如,时钟滴答为1GHZ,这个时间戳每纳秒计数一次。
Linux may take advantage of this register to get much more accurate time measurements than those delivered by the Programmable Interval Timer. To do this, Linux must determine the clock signal frequency while initializing the system. In fact, because this frequency is not declared when compiling the kernel, the same kernel image may run on CPUs whose clocks may tick at any frequency.
Linux 或许是利用这个寄存器来比那些可编程间隔计数器获得更加精确的时间测量值。为了达到此,Linux必须在系统初始化时决定时钟的信号频率。实际上,因为这个频率不被公开当编译内核的时候,相同的内核映像在不同时钟滴答的CPUS上可能运行。
The task of figuring out the actual frequency of a CPU is accomplished during the system's boot. The calibrate_tsc( ) function computes the frequency by counting the number of clock signals that occur in a time interval of approximately 5 milliseconds. This time constant is produced by properly setting up one of the channels of the Programmable Interval Timer (see the next section).[*]
任务的目的是在system启动时计算出实际的cpu频率。这个calibrate_tsc()函数计算频率在一段时间约5毫秒的时钟信号内产生的时钟信号。这个时间常数是由正确地设置好一个的可编程间隔计时器得到*/
To avoid losing significant digits in the integer divisions, calibrate_tsc( ) returns the duration, in microseconds, of a clock tick multiplied by 2^32.
为了防止丢失在重要的整数部分,calibrate_tsc()以微秒来返回这段时间?。
6.1.3. Programmable Interval Timer (PIT) 可编程间隔计时器
见前面的对于clock tick的说明博客。
The PIT is initialized by setup_pit_timer( ) as follows: 初始化过程
spin_lock_irqsave(&i8253_lock, flags);
outb_p(0x34,0x43);
/*写入控制方式*/
udelay(10);
outb_p(LATCH & 0xff, 0x40);
udelay(10);
outb
(LATCH >> 8, 0x40); /*LATCH为16位数据*/
spin_unlock_irqrestore(&i8253_lock, flags);
6.1.4. CPU Local Timer CPU当地计时器
The CPU local timer is a device similar to the Programmable Interval Timer just described that can issue one-shot or periodic interrupts. There are, however, a few differences:
CPU的当地计时器是一个和可编程计数器相似的设备,描述定时中断或者一次中断。但是有些不同
The APIC's timer counter is 32 bits long, while the PIT's timer counter is 16 bits long; therefore, the local timer can be programmed to issue interrupts at very low frequencies (the counter stores the number of ticks that must elapse before the interrupt is issued).
APIC的计时器数是32位,PIT是16的。本地计时器可以被编程以工作在低频率下
The local APIC timer sends an interrupt only to its processor, while the PIT raises a global interrupt, which may be handled by any CPU in the system.
APIC给自己的处理器发送中断,而PIT产生全局中断,中断可能被任何一个CPU处理。
The APIC's timer is based on the bus clock signal (or the APIC bus signal, in older machines). It can be programmed in such a way to decrease the timer counter every 1, 2, 4, 8, 16, 32, 64, or 128 bus clock signals. Conversely, the PIT, which makes use of its own clock signals, can be programmed in a more flexible way.
APIC的计时器基于总线信号。可以被编程工作在每来一个总线信号计时器数就减去1,2,4,等。相反,PIT用自己的时钟信号,而且被编程也很复杂。
6.1.5. High Precision Event Timer (HPET) 高精度事件计时器
The High Precision Event Timer (HPET) is a new timer chip developed jointly by Intel and Microsoft. Although HPETs are not yet very common in end-user machines, Linux 2.6 already supports them, so we'll spend a few words describing their characteristics.
高精度实践计时器是由Intel 和 M 共同发明的计时器芯片。尽管高精度计时器在终端用户上还不常见,Linux已经支持它们了。所以我们就描述一下他们的特点。
The HPET provides a number of hardware timers that can be exploited by the kernel. Basically, the chip includes up to eight 32-bit or 64-bit independent counters . Each counter is driven by its own clock signal, whose frequency must be at least 10 MHz; therefore, the counter is increased at least once in 100 nanoseconds. Any counter is associated with at most 32 timers, each of which is composed by a comparator and a match register. The comparator is a circuit that checks the value in the counter against the value in the match register, and raises a hardware interrupt if a match is found. Some of the timers can be enabled to generate a periodic interrupt.
HPET提供了可以被kernel发掘的一些硬件计时器。一般芯片包含了8个32或者64位独立的计数器。每个计数器被自己的时钟信号驱动,它们的频率至少是10MHZ;因此这个计数器至少一次加100纳秒。每一个计数器最多和32计时器相关,每一个由比较和匹配寄存器组成。这个比较器是个电路用来和在匹配寄存器的值进行检查,产生一个中断如果发现了匹配值。一些计时器也可以产生定时中断。
6.1.6. ACPI Power Management Timer ACPI 电源管理计时器
The ACPI Power Management Timer (or ACPI PMT) is yet another clock device included in almost all ACPI-based motherboards. Its clock signal has a fixed frequency of roughly 3.58 MHz. The device is actually a simple counter increased at each clock tick; to read the current value of the counter, the kernel accesses an I/O port whose address is determined by the BIOS during the initialization phase (see Appendix A).
ACPI电源管理计时器是在所有基于ACPI母板上的另一个时钟设备。它的时钟信号固定为3.58MHZ,这个设备只是简单加上每一个时钟滴答;读取当前计时器数的值,kernel访问i/o端口,起地址就是由BIOS在初始化阶段完成的。
The ACPI Power Management Timer is preferable to the TSC if the operating system or the BIOS may dynamically lower the frequency or voltage of the CPU to save battery power. When this happens, the frequency of the TSC changes thus causing time warps and others unpleasant effects while the frequency of the ACPI PMT does not. On the other hand, the high-frequency of the TSC counter is quite handy for measuring very small time intervals.
ACPI电源管理计时器是更好的TSC。如果操作系统或者BIOS动态降低cpu的频率或者电压来保存电池的能量。当发生时,TSC的频率发生变化导致时间测量不准确,或者不愉快的后果,但是ACPI PMT 的频率却不变。TSC计数器高频率对于测量很小的时间间隔很适合。
However, if an HPET device is present, it should always be preferred to the other circuits because of its richer architecture. Table 6-2 later in this chapter illustrates how Linux takes advantage of the available timing circuits.
但是如果HPET设备存在了,对于其他的电路是取的,因为它更丰富的架构。 后面的6-2说明linux怎么来使用这些有用的时间电路。