BIOS读取硬盘内容 (OS-DEV.PDF)

As we will see a little later on,specific devices require specific routines to be written to use them,so,for example ,a floppy disk device requires us to explicitly turn on and off the motor that spins the disk under the read-and-write head before we can use it,
whereas most hard disk devices have more functionality automated on local chips,but again the bus technologies with which such devices connect to the cpu (e.g. ATA/IDE,SATA,SCSI,USB,etc.) affect how we access them.Thankfully,BIOS can offer a few disk routines that abstract all of these differences for common disk devices.
The specific BIOS routine we are interested in here is accessed by raising interrupt 0x13 after setting the register al to 0x02.This BIOS routine expects us to set up a few other registers with details of which disk device to use , which blocks we wish to read from the disk,and where to store the blocks in memory. The most difficult part of using this routine is that we must specify the first block to be read using a CHS addressing scheme;otherwise,it is just a case of filling in the expected registers,as detailed in next code snippet

GDT

The main differences in 32-bit protected mode are:
Registers are extended to 32 bits,with their full capacity being accessed by prefixing an e to the register name , for example:mov ebx, 0x274fe8fe
For convenience, there are two additional general purpose segment registers, fs and gs
32-bit memory offsets are available , so an offset can reference a whopping 4 GB of memory (0xffffffff)
The CPU supports a more sophisticated --- though slightly more complex --- means of memory segmentation , which offers two big advantages:
----Code in one segment can be prohibited from executing code in a more privilidged segment , so you can protect your kernel code from user applications
----The CPU can implement virtual memory for user processes,such that pages (i.e. fixed-sized chunks)of a process's memory can be swapped transparently between the disk and memory on an as-needed basis.This ensure main memory is used efficiently , in that code or data that is rarely executed needn't hog valuable memory.
Interrupt handling is also more sophisticated.

Understanding the Global Descriptor Table

It is important to understand the main point of this GDT, that is so fundamental to
the operation of protected mode, before we delve into the details. Recall from Section
XXX that the design rationale of segment-based addressing in the classical 16-bit real
mode was to allow the programmer to access (albeit slightly, by today's standards) more
memory than a 16-bit offset would allow. As an example of this, suppose that the
programmer wanted to store the value of ax at the address 0x4fe56. Without segment-based addressing, the best the programmer could do is this:
理解GDT的主要思想非常重要,当我们深入细节时,它是操作系统安全模式的基础。
回想一下,在经典的16位实模式中,基于分段寻址的设计原理是允许程序员访问比16位偏移量所允许的内存更多的内存(尽管以今天的标准来看是很小的)
例如,假设程序员希望将ax的值存储在地址0x4fe56处。没有基于分段的寻址,程序员访问的最大地址为:
mov [0xffff ], ax
which falls way short of the intended address. Whereby, using a segment register, the
task could be achieved as follows:
这与预计要访问的最大地址相差甚远,然而如果使用段地址的方式,这个任务可以这样完成。
mov bx , 0x4000
mov es , bx
mov [es :0 xfe56 ], ax
Although the general idea of segmenting memory and using offsets to reach into those segments has remained the same, the way that it is implented in protected mode has completely changed, primarily to afford more exibility. Once the CPU has been switched into 32-bit protected mode, the process by which it translates logical addresses (i.e. the combination of a segment register and an offset) to physical address is completely different: rather than multiply the value of a segment register by 16 and then add to it the offset, a segment register becomes an index to a particular segment descriptor (SD) in the GDT.
虽然分割内存和使用操作集来访问这些段的一般思想保持不变,但是在受保护模式下执行它的方式已经完全改变,主要是为了提供更多的存在性。一旦转换成32位CPU保护模式,它将逻辑地址转换的过程(即段寄存器和一个偏移量)的结合物理地址是完全不同的: 与将段寄存器的值乘以16然后向其添加偏移量不同,段寄存器将成为GDT中特定段描述符(SD)的索引
在GDT。

A segment descriptor is an 8-byte structure that denes the following properties of a protected-mode segment:

Base address (32 bits), which denes where the segment begins in physical memory
基址(32位),指定在物理内存中的起始位置。

Segment Limit (20 bits), which denes the size of the segment
段限制(20位),定义段的大小。

Various flags, which affect how the CPU interprets the segment, such as the privilige level of code that runs within it or whether it is read- or write-only.
影响CPU如何解释段的各种标志,比如运行的代码的权限,或者标记是只读或写。

Figure 4.2 shows the actual structure of the segment descriptor. Notice how, just to add to the confusion, the structure fragments the base address and segment limit throughout the structure, so, for example, the lower 16 bits of the segment limit are in the first two bytes of the structure but the higher 4-bits are at the start of the seventh byte of the structure. Perhaps this was done as some kind of joke, or more likley it has historic roots or was in uenced by the CPU's hardware design.
图4.2显示了段描述符的实际结构。注意这结构是如何分割基地址与段内地址,低16bit 。也许这是一个玩笑,或者更有可能它有历史根源,或者是CPU的硬件设计造成的。

We will not concern ourselves with details of all of the possible congurations of segment descriptors, a full explanation of which is given in Intel's Developer Manual [?], but we will learn what we have to in order to get our code running in 32-bit protected
mode. The simplest workable conguration of segment registers is described by Intel as the basic at model, whereby two overlapping segments are dened that cover the full 4 GB of addressable memory, one for code and the other for data. The fact that in this model these two segments overlap means that there is no attempt to protect one segment from the other, nor is there any attempt to use the paging features for virtual memory. It pays to keep things simple early on, especially since later we may alter the segment descriptors more easily once we have booted into a higher-level language.

In addition to the code and data segments, the CPU requires that the rst entry in the GDT purposely be an invalid null descriptor (i.e. a structure of 8 zero bytes). The null descriptor is a simple mechanism to catch mistakes where we forget to set a particular segment register before accessing an address, which is easily done if we had some segment registers set to 0x0 and forgot to update them to the appropriate segment descriptors after switching to protected mode. If an addressing attempt is made with the null descriptor, then the CPU will raise an exception, which essentially is an interrupt ---

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,260评论 0 10
  • 什么想法都没有,我该怎么填啊啊啊啊啊 睡觉可能来得更真实 今天还算ok的啦,哟嚯嚯嚯
    樑生阅读 125评论 0 0
  • Bertrand Paradox 在理论模型中,一个完全竞争的市场,信息透明的情况下,同质化产品的生产商是无法获得...
    安妮李斯特阅读 160评论 0 0
  • hello worldprint ("hello world) //python3.xprint "hello w...
    丫丫姑娘_b55e阅读 212评论 0 0
  • 起床后才知道,邮政宾馆原来就在布达拉宫脚下,如此近!! 清晨,格桑给大家献哈达,说,一定要快乐。这素朴简单的祝福,...
    Miya拉姆阅读 370评论 0 1