21 novembre
零、前言杂说
linux某老师说毛德操的《Linux源代码情景分析》是好书,我承认;但他又说,该书是最好的并且只看那一本就够了,我不苟同。原因如下:
1、对于我们这些准菜鸟,光看那一本是看不懂的,必须看些(或者说温习一些)“准备材料”,如操作系统、计算机组成原理(其中内存管理章节)等等等等,甚至intel的官方文档也要涉及;
2、他(指老师)说该书分析的很透彻,是站在他自己的高度看问题,并没有从学生的角度考虑,他已经会当林绝顶了,自然一览众山小。鄙人认为,没有最好的书,只有在特定的时空最适合你的书。我们现在处于什么时空?苏大研究生。什么书好?自己去找。哪本爽看哪本,从看得懂的看起。当然这也要有策略,可以利用google来解决你的疑惑。
3、毛书是倪先生做的序。倪光南院士先生说这本书通俗易懂。这句话在他而言没有问题。可是这位老前辈也没有跳出时代的框框,还停留在以自己的坐标看世界的层次。至少鄙人,作为一个普通读者,普通学生认为,真正的大师该是说出来的话既能让计算机高手听得懂也能让普通老百姓听的明白。别说很难办到,有书为证,曼昆的《经济学原理》即是很好的案例,我们国内的计算机作者们,如果都看一遍曼昆的《经济学原理》,我想会对你们的写作能力大有帮助的,或者甚至升华你们的写作态度。
4、其实关于linux内核确实还有很多好书,我们不妨比对着看。推荐o'reilly的《understanding the linux kernel》中译本(第二版),《linux kernel primer》等(当然包括毛书,鄙人不否认毛书本身确实是优秀的)。方法举例:看A书中的内存管理一章;再看B书中的内存管理这一章;再看……。你会发现,你自己会找到孰优孰劣。并且看同样一章的速度逐渐加快。最后你真的理解了内存管理。并且能找出这几本书中不适合自己的若干本,剔除。再看下一章。循环下去。鄙人认为,这是一种啃“难书”的方法,与吕老师的独尊一书相比,至少对我本人更好。欢迎指正。
转入正题》》》
一、由果探因
今天看《深入理解linux内核》遇到三个词语:逻辑地址,线性地址和物理地址。乍一看似乎都理解了,可仔细想想似乎理解的并不透彻,书上说的既非长而详细也不短而深刻。大体上线性地址与物理地址有啥区别我倒是能说出一二,可逻辑地址与线性地址又有啥联系和区别,便模模糊糊了,似懂非懂,说服不了自己。
于是翻阅资料,求助google,wiki。阅读资料的时候发现,各有各的观点,似乎人人都说了问题的一个方面,但又不至于全面,同时每个人理解的都会多多少少有些错误,并不能全信。当了这么多年学生,现在了解到要真正懂得一个东西,必须完完全全的搞懂,要有立体的认识,要把握这个知识的来龙去脉,再用自己的语言进行总结,去粗存精。这样走过一遭,才能把一页页文献转化为自己的知识并储备起来,能够随时随地的脱口而出,讲给其他人听,并且让人听懂。于是产生了研究intel官方文档的冲动,但还是打住了,决定整理各路资料,去粗存精编辑一下。循环往复地看各路豪杰的观点,也许能提取出个中真谛。再杂取种种,揉成一个,不知道能不能还原成intel官方文档的雏形。
【所谓:看尽红颜果,遂解红颜因】OK let's go...
二、果如丛林
各路资料整理如下:
1. 截取中国linux论坛的一篇帖子(http://www.linuxforum.net)
386架构里,cpu可以处于实模式和保护模式。
实模式下,cpu指令访问的地址就是物理地址,形式为:段寄存器:偏移
在保护模式下,cpu可以使用分段机制和分页机制。 分段机制下使用的地址就是逻辑地址,形式为:
段选择子:偏移 。分页机制下使用的地址就是线性地址,形式为:0xXXXXXXXX 。无论是逻辑地址还是线
性地址,都要被cpu映射成物理地址。保护模式下必须采用分段机制。在此基础上可采用分页机制。
逻辑地址被转化为线性地址,如果采用分页机制,则该线性地址通过分页机制被映射成物理地址。如
果不采用分页机制,则该线性地址就是物理地址。
实模式下的物理地址只能访问1M以下空间,而保护模式下的物理地址可以访问所有32位空间。并且要
注意,物理内存空间只是物理地址空间的一个部分而已。
2. 来自intel的两张图

3. 来自http://www.idcnews.net/html/edu/linux/20080425/302315.html的文字解说
逻辑地址(LogicalAddress) 是指由程式产生的和段相关的偏移地址部分。例如,您在进行C语言指针编程中,能够读取指针变量本身值(&操作),实际上这个值就是逻辑地址,他是相对于您当前进程数据段的地址,不和绝对物理地址相干。只有在Intel实模式下,逻辑地址才和物理地址相等(因为实模式没有分段或分页机制,Cpu不进行自动地址转换);逻辑也就是在Intel保护模式下程式执行代码段限长内的偏移地址(假定代码段、数据段假如完全相同)。应用程式员仅需和逻辑地址打交道,而分段和分页机制对您来说是完全透明的,仅由系统编程人员涉及。应用程式员虽然自己能够直接操作内存,那也只能在操作系统给您分配的内存段操作。
线性地址(LinearAddress)是逻辑地址到物理地址变换之间的中间层。程式代码会产生逻辑地址,或说是段中的偏移地址,加上相应段的基地址就生成了一个线性地址。假如启用了分页机制,那么线性地址能够再经变换以产生一个物理地址。若没有启用分页机制,那么线性地址直接就是物理地址。Intel80386的线性地址空间容量为4G(2的32次方即32根地址总线寻址)。
物理地址(PhysicalAddress) 是指出现在CPU外部地址总线上的寻址物理内存的地址信号,是地址变换的最终结果地址。假如启用了分页机制,那么线性地址会使用页目录和页表中的项变换成物理地址。假如没有启用分页机制,那么线性地址就直接成为物理地址了。
在Linux0.11内核中,给每个程式(进程)都划分了总容量为64MB的虚拟内存空间。因此程式的逻辑地址范围是0x0000000到0x4000000。有时我们也把逻辑地址称为虚拟地址。因为和虚拟内存空间的概念类似,逻辑地址也是和实际物理内存容量无关的。逻辑地址和物理地址的“差距”是0xC0000000,是由于虚拟地址->线性地址->物理地址映射正好差这个值。这个值是由操作系统指定的。逻辑地址(或称为虚拟地址)到线性地址是由CPU的段机制自动转换的。假如没有开启分页管理,则线性地址就是物理地址。假如开启了分页管理,那么系统程式需要参和线性地址到物理地址的转换过程。具体是通过配置页目录表和页表项进行的。
4. chinaUnix论坛上一个高手的帖子。(http://linux.chinaunix.net/bbs/thread-919019-1-1.html)
5. 让我们来看看wiki的说法(好像在绕圈子,我并没完全看懂,所以没办法总结)
logical address
In computer architectures, a logical address is the address at which a memory location appears to reside from the perspective of an executing application program. This may be different from the physical address due to the operation of a memory management unit (MMU) between the CPU and the memory bus. Physical memory may be mapped to different logical addresses for various purposes. For example, the same physical memory may appear at two logical addresses and if accessed by the program at one address, data will pass through the processor cache whereas if it is accesed at the other address, it will bypass the cache.
In a system supporting virtual memory, there may actually not be any physical memory mapped to a logical address until an access is attempted. The access triggers special functions of the operating system which reprogram the MMU to map the address to some physical memory, perhaps writing the old contents of that memory to disk and reading back from disk what the memory should contain at the new logical address. In this case, the logical address may be referred to as a virtual address.
Physical Address
In computing, a physical address, also real address, or binary address, is the memory address that is electronically (in the form of binary number) presented on the computer address bus circuitry in order to enable the data bus to access a particular storage cell of main memory.
In a computer with virtual memory, the term physical address is used mostly to differentiate from a virtual address. In particular, in computers utilizing memory management unit (MMU) to translate memory addresses, the virtual and physical address refer to address before and after MMU translation, respectively.
In networking, physical address is sometimes a synonym of MAC address. The address is actually used on network's data link layer, not on physical layer, as the name would suggest.
Note: There are two basic types of physical addresses when referencing Ethernet which are large and fixed physical addresses and proNET, which has small relatively easy to configure addresses.
linear address
【wiki中搜不到linear address记录,用linear address space搜索韦氏字典(www.webster-dictionary.org),结果如下】
linear address space - A memory addressing scheme used in processors where the whole memory can be accessed using a single address that fits in a single register or instruction. This contrasts with a segmented memory architecture, such as that used on the Intel 8086, where an address is given by an offset from a base address held in one of the "segment registers". Linear addressing greatly simplifies programming at the assembly language level but requires more instruction word bits to be allocated for an address.
相关知识补充(所有定义来自wiki)
【Real mode】
Real mode, also called real address mode, is an operating mode of 80286 and later x86-
compatible CPUs. Real mode is characterized by a 20 bit segmented memory address space
(meaning that a maximum of 1 MB of memory can be addressed), direct software access to BIOS
routines and peripheral hardware, and no concept of memory protection or multitasking at the
hardware level. All x86 CPUs in the 80286 series and later start in real mode at power-on;
80186 CPUs and earlier had only one operational mode, which is equivalent to real mode in
later chips.
【Protected Mode】
In computing, protected mode, also called protected virtual address mode,[1] is an
operational mode of x86-compatible central processing units (CPU). It was first added to the
x86 architecture in 1982,[2] with the release of Intel's 80286 (286) processor and later
extended with the release of the 80386 (386) in 1985.[3] Protected mode allows system
software to utilize features such as virtual memory, paging, safe multi-tasking, and other
features designed to increase an operating system's control over application software.[4][5]
When a processor that supports x86 protected mode is powered on, it begins executing
instructions in real mode, in order to maintain backwards compatibility with earlier x86
processors.[6] Protected mode may only be entered after the system software sets up several
descriptor tables and enables the Protection Enable (PE) bit in the Control Register 0
(CR0).[7]
Due to the enhancements added by protected mode, it has become widely adopted and has become
the foundation for all subsequent enhancements onto the x86 architecture.[8]
【内存管理单元:MMU】
記憶體管理单元(英语:memory management unit,缩写为MMU),有时称作分页内存管理单元(英语:
paged memory management unit,缩写为PMMU)。它是一种负责处理中央处理器(CPU)的内存访问请求
的计算机硬件。它的功能包括虚拟地址到物理地址的转换(即虚拟内存管理)、内存保护、中央处理器高
速缓存的控制,在较为简单的计算机体系结构中,負責总线的仲裁以及存储体切换(bank switching,尤
其是在8位的系统上)。
工作机制
现代的内存管理单元是以页的方式,分割虚拟地址空间(处理器使用的地址范围)的;页的大小是2的n次
方,通常为几KiB。地址尾部的n位(页大小的2的次方数)作为页内的偏移量保持不变。其余的地址位
(address)为(虚拟)页号。内存管理单元通常借助一种叫做轉譯旁觀緩衝區(Translation Lookaside
Buffer,缩写为TLB)的相联高速缓存(associative cache)来将虚拟页号转换为物理页号。当后备缓冲
器中没有转换记录时,则使用一种较慢的机制,其中将涉及硬件相关(hardware-specific)的数据结构
(Data structure)或软件辅助手段。这个数据结构称为分页表,页表中的数据就叫做页表项(page table
entry,缩写为PTE)。物理页号结合页偏移量便提供出了完整的物理地址。
页表或转换后备缓冲器中数据项包括的信息有:一、“脏位”(dirty bit)——表示该页是否被写过。
二、“存取位”(accessed bit)——表示该页最后使用于何时,以便于最近最少使用页面置换算法
(least recently used page replacement algorithm)的实现。三、哪种进程可以读写该页的信息,例
如用户模式(user mode)进程还是特权模式(supervisor mode)进程。四、该页是否应被高速缓冲的信
息。
A memory management unit (MMU), sometimes called paged memory management unit (PMMU), is a computer hardware component responsible for handling accesses to memory requested by the central processing unit (CPU). Its functions include translation of virtual addresses to physical addresses (i.e., virtual memory management), memory protection, cache control, bus arbitration, and, in simpler computer architectures (especially 8-bit systems), bank switching.
24 ottobre
I'm still in the world...
-- A notice to my friends...
I'm still in the world,
I'm still here, in your heart
though seldom, i come up with smile,
I'm still loving. the lovely friends,
you ,it's you, who are watching my words.
I'm still loving...
though seldom, i say i love you.
I'm still in the world
I'm still here, in your heart