此文原刊发于:《纯C论坛·电子杂志》2004.11(总第二期)上
Linux 核心(The Linux Kernel)——第一章
原著:David A Rusling
翻译:毕昕、胡宁宁、仲盛、赵振平、周笑波、李群、陈怀临
Chapter 1 Hardware Basics
An operating system has to work closely with the hardware system that acts as its foundations. The operating system needs certain services that can only be provided by the hardware. In order to fully understand the Linux operating system, you need to understand the basics of the underlying hardware. This chapter gives a brief introduction to that hardware: the modern PC.
When the “Popular Electronics” magazine for January 1975 was printed with an illustration of the Altair 8080 on its front cover, a revolution started. The Altair 8080, named after the destination of an early Star Trek episode, could be assembled by home electronics enthusiasts for a mere $397. With its Intel 8080 processor and 256 bytes of memory but no screen or keyboard it was puny by today's standards. Its inventor, Ed Roberts, coined the term “personal computer” to describe his new invention, but the term PC is now used to refer to almost any computer that you can pick up without needing help. By this definition, even some of the very powerful Alpha AXP systems are PCs.
Enthusiastic hackers saw the Altair's potential and started to write software and build hardware for it. To these early pioneers it represented freedom; the freedom from huge batch processing mainframe systems run and guarded by an elite priesthood. Overnight fortunes were made by college dropouts fascinated by this new phenomenon, a computer that you could have at home on your kitchen table. A lot of hardware appeared, all different to some degree and software hackers were happy to write software for these new machines. Paradoxically it was IBM who firmly cast the mould of the modern PC by announcing the IBM PC in 1981 and shipping it to customers early in 1982. With its Intel 8088 processor, 64K of memory (expandable to 256K), two floppy disks and an 80 character by 25 lines Color Graphics Adapter (CGA) it was not very powerful by today's standards but it sold well. It was followed, in 1983, by the IBM PC-XT which had the luxury of a 10Mbyte hard drive. It was not long before IBM PC clones were being produced by a host of companies such as Compaq and the architecture of the PC became a de-facto standard. This de-facto standard helped a multitude of hardware companies to compete together in a growing market which, happily for consumers, kept prices low. Many of the system architectural features of these early PCs have carried over into the modern PC. For example, even the most powerful Intel Pentium Pro based system starts running in the Intel 8086's addressing mode. When Linus Torvalds started writing what was to become Linux, he picked the most plentiful and reasonably priced hardware, an Intel 80386 PC.
第一章 硬件基础
一个操作系统必需紧密地和其支撑——硬件系统结合在一起。操作系统需要一些只能由硬件提供的一些服务。为了很好的理解Linux操作系统,读者需要明白一些低层的硬件知识。本章对当代PC系统的一些硬件作一个介绍。
1975年的1月,当“Popular Electronics”杂志在其封面上给出Altair 8080的照片后,一场革命就开始了。Altair 8080(这个名字源于一部早期电影--星际旅行--的目的地的),这个可以被家用电器爱好者组装出来的机器,当时的价格是397美金。对于今天而言,其Intel 8080的处理器,256字节的内存,没有屏幕和键盘的配置是微不足道的。它的发明者Ed Roberts缔造了一个新词,“个人计算机”来描述他的新发明,不过现在PC已经被用在所有你可以一个人搬走的计算机上了。按照这个定义来讲,即使一些性能更高的Alpha AXP系统也称之为PC。
充满激情的计算机黑客们看见了Altair的潜力,并开始为它写软件和在其上为它配置硬件。对这些先驱者来说,这是一种摆脱呆版的、基于批处理的大型机的自由。一些退学的在校生通过这些个人计算机(一个你可以放在家中厨房桌子上的计算机)一夜之间获得了巨大的财富。市场上出现了大量的硬件设备。软件hackers们非常高兴地为这些新机器编写软件。有趣的是,IBM公司坚定的制定了现代PC的模式,并在1981年制造了当代PC——IBM PC,并在1982年交给用户使用。当时IBM PC的配置是Intel 8088处理器,64K内存(可扩展到256K),两个软磁盘和一个80字符,25行的CGA显示适配器。按照今天的标准来看,它并不具有很好的性能,但它卖得很好。1983年,IBM推出了IBM PC-XT,含有一个昂贵的10M硬盘。不久之后,许多公司,如COMPAQ,推出了IBM PC的兼容机。PC的体系结构变成了一个事实上的工业标准。这个事实上的工业标准使得大量的硬件公司在一个增长的市场中进行竞争。从而使的PC价格变得越来越便宜,用户对此非常高兴。早期PC的许多结构特征被现代PC所继承。例如,即使先进的Intel Pentium Pro系统在开始时也运行在Intel 8086的地址模式下。当Linus Torvalds 开始写Linux时,他选择了当时最多的,价格也较合理的Intel 80386 PC。
(图1.1 一个典型的PC主板)
Looking at a PC from the outside, the most obvious components are a system box, a keyboard, a mouse and a video monitor. On the front of the system box are some buttons, a little display showing some numbers and a floppy drive. Most systems these days have a CD ROM and if you feel that you have to protect your data, then there will also be a tape drive for backups. These devices are collectively known as the peripherals.
Although the CPU is in overall control of the system, it is not the only intelligent device. All of the peripheral controllers, for example the IDE controller, have some level of intelligence. Inside the PC (Figure 1.1) you will see a motherboard containing the CPU or microprocessor, the memory and a number of slots for the ISA or PCI peripheral controllers. Some of the controllers, for example the IDE disk controller may be built directly onto the system board.
1.1 The CPU
The CPU, or rather microprocessor, is the heart of any computer system. The microprocessor calculates, performs logical operations and manages data flows by reading instructions from memory and then executing them. In the early days of computing the functional components of the microprocessor were separate (and physically large) units. This is when the term Central Processing Unit was coined. The modern microprocessor combines these components onto an integrated circuit etched onto a very small piece of silicon. The terms CPU, microprocessor and processor are all used interchangeably in this book.
Microprocessors operate on binary data; that is data composed of ones and zeros.
These ones and zeros correspond to electrical switches being either on or off. Just as 42 is a decimal number meaning ``4 10s and 2 units'', a binary number is a series of binary digits each one representing a power of 2. In this context, a power means the number of times that a number is multiplied by itself. 10 to the power 1 (101) is 10, 10 to the power 2 (102) is 10*10, 103 is 10*10*10 and so on. Binary 0001 is decimal 1, binary 0010 is decimal 2, binary 0011 is 3, binary 0100 is 4 and so on. So, 42 decimal is 101010 binary or (2 + 8 + 32 or 21 + 23 + 25 ). Rather than using binary to represent numbers in computer programs, another base, hexadecimal is usually used.
In this base, each digital represents a power of 16. As decimal numbers only go from 0 to 9 the numbers 10 to 15 are represented as a single digit by the letters A, B, C, D, E and F. For example, hexadecimal E is decimal 14 and hexadecimal 2A is decimal 42 (two 16s) + 10). Using the C programming language notation (as I do throughout this book) hexadecimal numbers are prefaced by “0x”; hexadecimal 2A is written as 0x2A.
Microprocessors can perform arithmetic operations such as add, multiply and divide and logical operations such as “is X greater than Y?”.
The processor's execution is governed by an external clock. This clock, the system clock, generates regular clock pulses to the processor and, at each clock pulse, the processor does some work. For example, a processor could execute an instruction every clock pulse. A processor's speed is described in terms of the rate of the system clock ticks. A 100Mhz processor will receive 100,000,000 clock ticks every second. It is misleading to describe the power of a CPU by its clock rate as different processors perform different amounts of work per clock tick. However, all things being equal, a faster clock speed means a more powerful processor. The instructions executed by the processor are very simple; for example ``read the contents of memory at location X into register Y''. Registers are the microprocessor's internal storage, used for storing data and performing operations on it. The operations performed may cause the processor to stop what it is doing and jump to another instruction somewhere else in memory. These tiny building blocks give the modern microprocessor almost limitless power as it can execute millions or even billions of instructions a second.
The instructions have to be fetched from memory as they are executed. Instructions may themselves reference data within memory and that data must be fetched from memory and saved there when appropriate.
The size, number and type of register within a microprocessor is entirely dependent on its type. An Intel 4086 processor has a different register set to an Alpha AXP processor; for a start, the Intel's are 32 bits wide and the Alpha AXP's are 64 bits wide. In general, though, any given processor will have a number of general purpose registers and a smaller number of dedicated registers. Most processors have the following special purpose, dedicated, registers:
Program Counter (PC)
This register contains the address of the next instruction to be executed. The contents of the PC are automatically incremented each time an instruction is fetched,
Stack Pointer (SP)
Processors have to have access to large amounts of external read/write random access memory (RAM) which facilitates temporary storage of data. The stack is a way of easily saving and restoring temporary values in external memory. Usually, processors have special instructions which allow you to push values onto the stack and to pop them off again later. The stack works on a last in first out (LIFO) basis. In other words, if you push two values, x and y, onto a stack and then pop a value off of the stack then you will get back the value of y.
Some processor's stacks grow upwards towards the top of memory whilst others grow downwards towards the bottom, or base, of memory. Some processor's support both types, for example ARM.
Processor Status (PS)
Instructions may yield results; for example “is the content of register X greater than the content of register Y?” will yield true or false as a result. The PS register holds this and other information about the current state of the processor. For example, most processors have at least two modes of operation, kernel (or supervisor) and user. The PS register would hold information identifying the current mode.
1.2 Memory
All systems have a memory hierarchy with memory at different speeds and sizes at different points in the hierarchy. The fastest memory is known as cache memory and is what it sounds like - memory that is used to temporarily hold, or cache, contents of the main memory. This sort of memory is very fast but expensive, therefore most processors have a small amount of on-chip cache memory and more system based (on-board) cache memory. Some processors have one cache to contain both instructions and data, but others have two, one for instructions and the other for data. The Alpha AXP processor has two internal memory caches; one for data (the D-Cache) and one for instructions (the I-Cache). The external cache (or B-Cache) mixes the two together. Finally there is the main memory which relative to the external cache memory is very slow. Relative to the on-CPU cache, main memory is positively crawling.
The cache and main memories must be kept in step (coherent). In other words, if a word of main memory is held in one or more locations in cache, then the system must make sure that the contents of cache and memory are the same. The job of cache coherency is done partially by the hardware and partially by the operating system. This is also true for a number of major system tasks where the hardware and software must cooperate closely to achieve their aims.
1.3 Buses
The individual components of the system board are interconnected by multiple connection systems known as buses. The system bus is divided into three logical functions; the address bus, the data bus and the control bus. The address bus specifies the memory locations (addresses) for the data transfers. The data bus holds the data transferred. The data bus is bidirectional; it allows data to be read into the CPU and written from the CPU. The control bus contains various lines used to route timing and control signals throughout the system. Many flavours of bus exist, for example ISA and PCI buses are popular ways of connecting peripherals to the system.
1.4 Controllers and Peripherals
Peripherals are real devices, such as graphics cards or disks controlled by controller chips on the system board or on cards plugged into it. The IDE disks are controlled by the IDE controller chip and the SCSI disks by the SCSI disk controller chips and so on. These controllers are connected to the CPU and to each other by a variety of buses. Most systems built now use PCI and ISA buses to connect together the main system components. The controllers are processors like the CPU itself, they can be viewed as intelligent helpers to the CPU. The CPU is in overall control of the system.
All controllers are different, but they usually have registers which control them. Software running on the CPU must be able to read and write those controlling registers. One register might contain status describing an error. Another might be used for control purposes; changing the mode of the controller. Each controller on a bus can be individually addressed by the CPU, this is so that the software device driver can write to its registers and thus control it. The IDE ribbon is a good example, as it gives you the ability to access each drive on the bus separately. Another good example is the PCI bus which allows each device (for example a graphics card) to be accessed independently.
1.5 Address Spaces
The system bus connects the CPU with the main memory and is separate from the buses connecting the CPU with the system's hardware peripherals. Collectively the memory space that the hardware peripherals exist in is known as I/O space. I/O space may itself be further subdivided, but we will not worry too much about that for the moment. The CPU can access both the system space memory and the I/O space memory, whereas the controllers themselves can only access system memory indirectly and then only with the help of the CPU. From the point of view of the device, say the floppy disk controller, it will see only the address space that its control registers are in (ISA), and not the system memory. Typically a CPU will have separate instructions for accessing the memory and I/O space. For example, there might be an instruction that means “read a byte from I/O address 0x3f0 into register X”. This is exactly how the CPU controls the system's hardware peripherals, by reading and writing to their registers in I/O space. Where in I/O space the common peripherals (IDE controller, serial port, floppy disk controller and so on) have their registers has been set by convention over the years as the PC architecture has developed. The I/O space address 0x3f0 just happens to be the address of one of the serial port's (COM1) control registers.
There are times when controllers need to read or write large amounts of data directly to or from system memory. For example when user data is being written to the hard disk. In this case, Direct Memory Access (DMA) controllers are used to allow hardware peripherals to directly access system memory but this access is under strict control and supervision of the CPU.
1.6 Timers
All operating systems need to know the time and so the modern PC includes a special peripheral called the Real Time Clock (RTC). This provides two things: a reliable time of day and an accurate timing interval. The RTC has its own battery so that it continues to run even when the PC is not powered on, this is how your PC always “knows” the correct date and time. The interval timer allows the operating system to accurately schedule essential work.
从外面观察一个PC,最明显的部件是一个系统主机,键盘,鼠标器和一个显示器。系统主机前面有几个按钮,一个小的可以显示几个数字的显示屏和一个软驱。今天的大多数系统还有CD ROM。如果你想要保护数据,还可以有一个磁带驱动器用来做数据备份。这些设备通称为外设。
虽然CPU是系统的主要控制部件,但它不是系统中唯一具有智能的设备。所有的外设控制器,例如,IDE控制器,都有一定的智能成份。在一个PC的内部(如图1.1所示),你可以看见一个含有CPU或微处理器的主板、内存以及一些ISA或PCI外设控制器插槽。有些控制器,如IDE磁盘控制器,有可能被直接作在系统板上。
1.1 CPU
CPU,或微处理器,是任何一个计算机系统的心脏。CPU通过读取并执行内存中的指令来进行计算,执行逻辑运算和管理数量流。在早期,微处理器的功能部件是分离元件(在外形上较大)。这就是中央处理单元这一术语的由来。现代的微处理器把这些部件都集成到一块很小的硅片上。CPU、中央处理器在本书中均可以交替使用。
(编者注:本节后面几段由编者补译)
微处理器处理的是二进制的数据,这些二进制的数据仅由1和0组合而成。
1和0与电路开关中开与关的状态是相对应的。正如十进制数42表示“4个10与2个1一样”,一个二进制数是一串二进制数字,每一个二进制数字都表示一个2的次幂。在这种上下文中,次幂表示一个数自乘的次数。10的一次幂(101)是10,10的2次幂(102)是10*10,10的3次幂(103)是10*10*10以此类推。在二进制中,0011是3,0100是4以此类推。因此,十进制数42用二进制表示是101010或者2 + 8 + 32 或者 21 + 23 + 25。除了在计算机程序中用二进制表示数,其它时候通常使用16进制。
在这个条件下,每一个数字表示一个16的次幂。由于十进制数字只能从0到9,所以10到15就用字母A、B、C、D、E及F来表示。例如,十六进制数E就是十进制数14,十六进制数2A就是十进制数42(2*16+10)。用C语言来表示(在本书就我都会这样做)十六进制数的前缀是“0x”,十六进制数2A被写作0x2A。
微处理器可以完成算术运术,比如:加、减、乘、除;以及逻辑运算,比如:“X是比Y大吗?”
处理器的执行受外部的一个时钟控制。这个时钟,即系统时钟,会有规律的向处理器发送脉冲,每个时钟脉冲,处理器都会完成一些工作。例如,一个处理器可以每个时钟脉冲执行一条指令。一个处理器的速度常用系统时钟的滴达速率来描述。一个100MHz的处理器每秒会收到100,000,000个时间滴达。不过,用时钟滴达速率来描述处理器的性能是不对的,因为不同的处理器每个时钟滴达所完成的工作量是不同的。不过,如果所有情况都相同,时钟速率越快,意味着处理器性能越强。处理器执行的指令都非常简单,比如“将内存地址为X处的内容读到寄存器Y里”。寄存器是微处理器内部的存储空间,用来存放数据,在上面执行一个操作。有些操作可能会引起处理器停止当前的工作而跳到内存中的另一条指令去执行。紧凑的blocks(编者注:抱歉,对于此处的确切意思实在无法把握)的设计使现代的微处理器几乎获得了无限的性能,它们可以每秒执行数百万条甚至数十亿条的指令。
这些指令必须从内存中取出来然后执行。指令自身可能会使用到内存中的数据,这些数据也必须从内存中取出来,以及在适当的时候保存在那里。
处理器中寄存器的大小,数量以及类型都取决于处理器的类型。一个Intel 4086处理器拥有一套同Alpha AXP不同的寄存器。首先,Intel的处理器是32位的,而Alpha AXP的处理器是64位的。不过,通常来讲,任何一个处理器都有一些通用寄存器以及少量的专用寄存器。许多处理器有下面一些专用寄存器:
程序计数器(PC)
这个寄存器保存有下一条将被执行的指令的地址。每次取出指令后,PC中的内容会自动增加。
堆栈指针(SP)
处理器经常需要访问外部的大量的可读写的随机存储器(RAM),它们非常适合临时存储数据。在处部存储器中,堆栈是一个很简单存储及恢复数据的方法。通常,处理器有专门的指令让你把数据推入栈中然后再将其弹出。堆栈遵循后进先出(LIFO)的原则。换句话说,如果你推入两个值,x与y到堆栈里,然后你从堆栈里弹出一个值,你弹出的将是y值。
有些处理器的堆栈朝着内存顶部向上增长,有些朝着内存底部向下增长,也有一些处理器两种增长方式都支持,比如说ARM。
处理器状态(PS)
指令可能会产生一些结果,比如“X寄存器中的内容比Y寄存器中的内容大吗”会产生真或假作为结果。PS寄存器保存了这样的信息以及其它一些有关处理器当前状态的信息。比如,许多处理器都至少拥有两种工作模式,内核模式(或者管理模式)以及用户模式。PS寄存器需要保存用来表示当前是在哪个模式下工作的信息。
1.2 存储器
所有的系统都有一个存储器的层次结构。每一层的速度和大小不一样。最快的存储器是缓冲(cache)存储器,用来暂时存放主存储器的内容。这种存储器非常快但价格很贵,因此大多数处理器在芯片中只含有较少的cache存储器。更多的则在系统板上。有些处理器在cache中混合存放数据和指令,有些则分开存放,一个cache为指令,其他一个cache为数据。Alpha处理器中含有两个内部存储cache,D-cache存放数据,I-cache存放指令。外部的cache(B-cache)混合存放指令与数据。最后,相对于外部cache而言非常慢的是主存。如果与CPU内部的cache比较,主存肯定就像在爬一样。
缓冲与主存之间必须保持一致性(coherent)。换句话说,如果主存中的一个字在cache中的一个或多个地方,系统必须保证cache中的内容与主存中的一致。cache一致性的工作一部份是由硬件完成的,一部份是由操作系统完成的。这一点对于大多数主要的系统任务来说都是一样的。系统中的软硬件必须互相合作完成功能。
1.3 总线
系统板上的独立的部件通过总线相连。系统总线按逻辑功能分成三种类型:地址总线,数据总线和控制总线。地址总线指定数据传送的地址。数据总线包含要传送的数据。数据总线是双向的,允许数据读进CPU和从
CPU写出。控制总线包含一些信号线,用来控制时序和系统中的其他控制信号。还有许多其它总线,比如ISA及PCI总线常常被系统用来连接外部设备。
1.4 控制器和外设
外设是真实的设备,例如显示卡或磁盘,它们被主板上的控制芯片或者那些插在主板上的板卡上的控制芯片所控制。IDE磁盘由IDE控制芯片控制,SCSI磁盘被SCSI磁盘控制芯片所控制,以此类推。控制器与CPU之间,控制器与控制器之间,通过总线相连。大多数系统通过PCI和ISA总线将主要的系统部件相连。控制器与CPU一样,本身就是一种处理器,它们可以看作是CPU的智能助手。CPU是系统的控制中心。
所有的控制器都不相同,但通常它们都有一些可以用来控制它们的寄存器。在CPU之上运行的软件必须能够读写这些控制寄存器。一个寄存器可能包含一个用来描述错误的状态,另一个寄存器可能被用来作为控制,例如改变控制器的模式。总线上的每个控制器都可以被CPU单独寻址,因而,设备驱动程序软件可以写上述寄存器以控制这些控制器。IDE扁平电缆就是一个很好的例子,它给了你可以单独访问总线上每一个设备的能力。另一个好的例子是PCI总线,它使每一个设备(例如一个图形显示卡)可以被独立访问。
1.5 地址空间
系统总线将CPU与主存相连,这与将CPU与系统硬件外设相连的总线是分开的。硬件外设所占据的存储空间叫做I/O空间。I/O空间自身还可以被细分,但我们不用在这一问题上深究。CPU可以既可以存取系统存储空间,也可以存取I/O空间。然而,控制器只能在CPU的帮助下间接地访问系统主存。从设备的观点看,例如软盘控制器,它只能看见其控制寄存器所在的(ISA)空间的地址,不能看到系统主存。一般而言,CPU使用不同的指令系统来存取系统存储器和I/O空间。例如,可能存在一个指令“从I/O地址0x3f0读一个字节到寄存器x”。CPU就是这样通过
读和写I/O空间中的外设的寄存器,来控制系统外设的。在I/O空间中,一些常用的硬件外设(IDE控制器、串行口、软盘驱动器等)的寄存器地址,在多年的PC体系结构发展中已经被固定下来了。例如,I/O地址空间中的地址0x3f0正好是串口一(com1)的一个控制寄存器的地址。
有时候,控制器需要在系统主存之间传送大量的数据。例如将数据写入硬盘。这种情况下,直接储存器访问(DMA)控制器被用来使得硬件外设直接存取系统内存。但是这个过程是在CPU的严格控制和监督之下的。
1.6 定时器
所有的操作系统都需要知道时间。所以现代PC中含有一个特殊的设备叫做实时时钟(Real Time Clock)。RTC提供两种功能:每天的可靠的时间和精确的时间间隔。RTC有其自己的电池,因此即使PC没开电源,RTC也在运行。这就是为什么你的PC知道正确的日期及日间的原因。时间间隔定时器使得操作系统可以准确的调度必要的工作。
【未完待续·责任编辑:iamxiaohan@hitbbs】
本文地址:http://com.8s8s.com/it/it33077.htm