Operating system hardware hard disk

The world's first disk storage device came from IBM. That's right, the blue giant, the vane of technology decades ago. This device uses magnetic heads to read and write data, and uses disks to store data. Future hard disks will be based on this model development. This magnetic head can be moved directly to any storage unit on the disk, thus realizing random storage. Although its total capacity was only 5MB, limited to the level of manufacturing technology at the time, a total of 50 disks with a diameter of 24 inches were used, and the stacked volume was equivalent to the size of a refrigerator. The surfaces of these disks are coated with magnetic substances for storing data. They are stacked and fixed together, and driven by the motor, they rotate around the same axis.

The "Winchester/Winchester" technology was first proposed by IBM in 1968. The essence of this technology is that "the plated disk rotates at high speed in a sealed space, and the magnetic head is suspended above the disk and fixed on the head arm along the diameter of the disk. to move". The magnetic head does not touch the platter and should not be in contact. Long-term friction will destroy all data. The high-speed rotation of the platter will generate airflow. The magnetic head floats like a flying saucer under this airflow, which ensures that there will be no friction with the platter. The magnetic head is fixed on the magnetic head arm, which can move radially along the disk. Due to the respective movements of the magnetic head and the disk, coupled with such a close distance, even a little dust can cause damage to the disk. Thus, the magnetic head and platter were sealed in a box. Today's hard disk is still such a structure.

At that time, such a structure was just an idea, which was realized by IBM Big Blue in 1973. IBM 3340, with a capacity of 60MB, was composed of two 30MB storage units. Since then, the development of hard disk technology has formed a structural basis.

With the development of hard disks to this day, it has survived by defeating competitors through continuous improvement in capacity over the past few decades. But speed has always been its biggest enemy. Ten years ago, the mainstream speed of hard drives in home computers was 7200 rpm, and it is still the same today. The random access of the hard disk is realized by the continuous movement of the magnetic head arm. The time for the magnetic head arm to move to the target position is called the seek time. If the stored data is not continuous, the magnetic head has to constantly adjust its position for one piece or another. This is Mechanical hard disks are inevitable, and this is the bottleneck of hard disks. Therefore, general hard disks use seek time as an important parameter.

Among the many competitors, there is also one that has survived tenaciously. It is the SSD solid state drive. Only 128G or 256G solid-state drives are used as boot disks to increase the speed of operation, and Samsung, a big manufacturer of solid-state drives, runs out of fire from time to time. When electronic products such as solid-state drives and memory sticks increase in price, they are jokingly called wealth management products. However, after domestic manufacturers such as Yangtze River Storage have conquered storage technology, solid-state drives and memory have become cabbage prices. arrogant. Some core technologies are not in their own hands, and they are always lambs at the mercy of others. Some of them ran far away and came back. We only talk about hard disks in this chapter.

How Hard Disks Work

Please add a picture description

Talking about the hard disk is inseparable from this picture. The current speed of the mainstream personal computer hard disk is 7200 rpm (this number has been boundless for decades). Each disk is divided into upper and lower sides, each side stores data, and each disk surface is read by a magnetic head, so one disk corresponds to 2 magnetic heads. Since there is a one-to-one correspondence between the disk surface and the magnetic head, the disk surface is represented by the head number. The head number starts from 0 from top to bottom, so use head 0 to represent the first disk. The magnetic head will not move on the disk by itself. It needs to be fixed on the right head arm. Driven by the head arm, it will swing back and forth along the edge of the disk toward the center of the circle. Note that the swing track is an arc, not absolute. Radially straight to and fro. On the one hand, this is because the head arm is driven by a stepping motor, one end of the head arm is the spindle of the stepping motor, and the other end is the magnetic head. The stepper motor rotates an angle every time, so it drives the head arm to "draw a circle", and the head is located at the other end of the head arm, so it also moves like a pendulum. The trajectory is an arc, not a straight line. Therefore, the "similar" radial movement is marked in the head arm in Figure 3-28, which is the reason for "similar". On the other hand, the magnetic head does not need to move straight to read data. Whether the data can be found is only related to its final landing point, and has nothing to do with the shape of the intermediate path. Therefore, on the one hand, the rotation of the disk, and on the other hand, the swing of the magnetic head, the combination of these two actions enables the magnetic head to read data at any position on the disk.

The disk surface is a magnetic medium used to store data. In order to manage the disk more effectively, these magnetic media are also "formatted" into an easy-to-manage pattern, that is, the entire disk surface is divided into multiple concentric rings, and a sector is drawn with the center of the circle. The arc-shaped area where each concentric ring intersects serves as the most basic data storage unit. This concentric ring is called a track, and the arc-shaped area on the concentric ring is a part of the sector, so it is called a sector. It is the most basic unit for us to store data on the hard disk, and its size is 512 bytes. The data we write ends up in sectors on the track. Pay attention, the magnetic track I mentioned above is a ring, not a line. Many textbooks simply draw a circle when introducing the magnetic track. This is easy to misunderstand that the magnetic track is a line. Data cannot be stored on the line. For cross-sections, the data is stored in these "areas". The magnetic head arm drives the magnetic head to move above the disk, that is, to find the position of the track, and the high-speed rotation of the disk is to locate the sector in the magnetic track.

Track numbers start from 0 just like heads. A tubular region of identically numbered tracks is called a cylinder. In Figure 3-28, the tracks with the same number on the two platters are connected by gray straight lines, which are very similar to the curved surface of a pillar, so they are called cylinders. If there are a lot of discs, the "cylindrical surface" is very vivid. Why is there a concept of cylinders? I can’t store them one by one. This is to reduce the seek time. The head jumps between tracks. The time required for the jump is called the seek time. If you wait The written data is less than the remaining capacity of a track, and when it is read out in the future, the magnetic head only needs to be positioned on the track. At this time, the seeking is only once. If the data to be written occupies multiple tracks, in addition to changing the head to a new track when writing, the track also needs to be changed when reading in the future, that is, multiple seeks are required to complete the complete reading and writing of data. Since seeks have a great influence on the speed of mechanical hard disks, in principle, the number of seeks should be reduced as much as possible. The tracks in the cylinder have the same number, and the same number means that the positions of the tracks on the disk are the same, and they must be located on the same cylinder. In the track, all the head positions are the same, so the head does not need to move any more. Isn't it great?

Write data according to this idea: When the space of a track on the 0 side is insufficient, other data are written to the track with the same number on the 1st side. If the space in the new track is still insufficient, write to the same numbered track on the second side until the tracks on the same cylinder (tracks with the same number on all disks) are not enough to write to the new cylinder. So, at this point, the more disks, the faster the hard drive.

Each track sector is numbered starting with 1, and it is only valid within this track, so the sector numbers between each track are the same, the lower limit starts from 0, and the upper limit is not necessarily, it depends on each manufacturer technology, but in most cases there are 63 sectors in a track.

How does the magnetic head find the sector, even if it is searched by the number, there must be a comparison. In fact, the sector has its own header, and the stored data is after the header. The header contains the sector itself. information, so the only sector can be located by using the 3 information of head number, track number and sector number.

A little history of hard drives

The relationship between the hard disk controller and the hard disk is the same as that of the graphics card and the monitor. They are all modular circuits that drive external devices. The CPU only talks to them, and they translate the words of the CPU to the external devices. This is what they have in common, but the difference is that the graphics card and the monitor are separated, and the hard disk controller and the hard disk are connected together, but in fact they were also separated a long time ago. Later, several leaders in the industry cooperated to develop a In this way, the hard disk and the hard disk controller are integrated together. In order to highlight the meaning of "integration", the hard disk controller and the hard disk are finally together. This interface is called Integrated Drive Electronics (IDE). ). As the influence of the IDE interface standard becomes more and more extensive, the global standardization agreement summarizes the technical specifications used by this interface into the global hard disk standard, thus resulting in ATA (Advanced Technology Attachment), the computer develops very fast, and the phenomenon of alternation between the old and the new They emerge in endlessly, so that the appearance of the younger generation often changes the names of the older generation. No, the hard disk serial interface (Serial ATA, SATA) that just debuted a few years ago, because it is serial, the previous ATA interface had to be called parallel ATA, that is, (Parallel ATA, PATA).

PATA interfaces are rarely used by us now. Each interface can mount two hard drives, one master and one slave. , such as the system will be installed on the main disk. Later, the compatibility of the system became better and better, so that the difference was not obvious. A motherboard supports such four IDE (PATA) hard disks, so two IDE slots are provided on the motherboard. These two interfaces are also numbered starting from 0, one is called IDE0 and the other is called IDE1. According to ATA, these two slots are called channels, IDE0 is called Primary channel, and IDE1 is called Secondary channel.

We use SATA interface a lot now. How many SATA hard disks can be connected to a host depends on the number of SATA interfaces on the motherboard. There is no such limitation as PATA. Even if a SATA hard disk is installed on the motherboard, it is also compatible with PATA programming. Interface, upward compatibility is the foundation for the continuous development of computer energy. Therefore, the port numbers given later will also be given in groups according to the two channels of PATA.

The Nvme interface is a high-speed interface protocol based on PCIe, which is specially used to connect solid-state drives. Compared with SATA and SAS, NVMe has a higher data transfer rate and lower latency, and can better utilize the performance advantages of solid-state drives. This protocol is relatively new, so the advantage is that the link speed is relatively fast, and the transmission speed of the current solid state drive is theoretically even 7000MB/s

HDD port

The controller of the hard disk belongs to the IO interface, so its control is also a read-write port. The port is the register on the IO controller. The port here refers to the register on the hard disk controller. We only talk about some things that may be used. In fact, the hard disk is very complicated. After all, it is a very precise component, and its manual is very long.
Please add a picture description

Ports can be divided into two groups, Command Block registers and Control Block registers. The registers in the second group have been simplified a lot and are basically not used, so we only talk about the first group.

The ports are given according to the channel. That is to say, don’t mistakenly think that the port is directly for a certain hard disk like I did at the beginning. This is not the case. Both the master and slave hard disks on a channel use these port numbers. If you want to operate a certain hard disk on a certain channel, you need to specify it separately. Look, there is a register called device on it. As the name suggests, it refers to the drive device, which is related to the hard disk. However, this register is 8 bits, and there are only two hard disks on one channel. Only one bit is enough to specify which hard disk. The register is a precious resource and cannot be wasted, so this register is a miscellaneous item, and many settings need to be concentrated In this register, the 4th bit is the master or slave hard disk on the specified channel, 0 is the master disk, and 1 is the slave disk.

The purpose of the port is different when reading and writing the hard disk. For example, take the 0x1F1 port on the Primary channel. If the read operation fails, the failure status information is stored in it, so it is called the error register, and the 0x1F2 port Stores the number of unread sectors in . When writing, it becomes the feature register, which is used to write the parameters of the command. All this is to save money, a register can be reused.

From the name of the Data register, we know that it is responsible for managing data. The faster the reading and writing of data, the better, so this register is wider than other registers, 16 bits (already very good, other registers in the table are 8 bits) of). When reading the hard disk, after the hard disk has prepared data, the hard disk controller puts it in the internal buffer, and continuously reading this register means reading all the data in the buffer. When writing to the hard disk, we need to continuously send data to this port, and the data will be stored in the buffer. When the hard disk controller finds that there is data in the buffer, it will write the data here to the corresponding sector. middle.

When reading the hard disk, the register name of the port 0x171 or 0x1F1 is the Error register, which is only useful when reading the hard disk fails, and the failure information will be recorded in it. When writing the hard disk, this register has other uses, so there is a new one. The name is called the Feature register. Some commands need to specify additional parameters, and these parameters are written in the Feature register.

The Sector count register is used to specify the number of sectors to be read or written. Every time the hard disk completes a sector, it will decrement the value of this register by 1, so if it fails in the middle, the value in this register is the unfinished sector. This is an 8-bit register with a maximum value of 255. If it is specified as 0, it means that 256 sectors are to be operated.

The sectors in the hard disk are physically positioned using cylinder-head-sector, referred to as CHS, but every time we have to figure out which cylinder, which sector, it is too troublesome, we hope that in the disk Sectors are numbered sequentially from 0, regardless of the physical structure of the sector. In fact, I have already mentioned the definition of LBA when describing the requirements. This is a logical sector address method, which is called Logical Block Address (Logical Block Address). There are two types of LBA, one is LBA28, which uses 28 bits to describe the address of a sector. The maximum addressing range is 2 to the 28th power equal to 268435456 sectors, each sector is 512 bytes, and the maximum support is 128GB. Here we use the LBA28 mode for simplicity. Since 128GB can no longer meet the growing storage demand, the hard disk is getting bigger and bigger, and there must be a matching addressing method to match it, so the other one to be introduced is LBA48, which uses 48 bits to describe a sector. Address, the maximum addressable range is 2 to the 48th power, which is equal to 281474976710656 sectors. After multiplying by 512 bytes, the maximum support is 131072TB, which is 128PB.

But we only have three LBA registers, a total of 24 bits, what to do if there are not enough 28 bits, borrow a little from the device, the lower 4 bits of the device register are used to store the 24th to 27th bits of the LBA address, and the 4th bit is used to specify the address on the channel Master or slave, 0 means master, 1 means slave. Bit 6 is used to set whether to enable the LBA mode, 1 means enable the LBA mode, and 0 means enable the CHS mode. The other two bits: bits 5 and 7 are fixed at 1, called MBS bits,

When reading the hard disk, the register name of the port 0x1F7 or 0x177 is Status, which is an 8-bit register and is used to give the status information of the hard disk. The 0th bit is the ERR bit. If this bit is 1, it means that the command is wrong. The specific reason can be seen in the error register. The third bit is the data request bit. If this bit is 1, it means that the hard disk has prepared the data, and the host can read the data now. The sixth bit is DRDY, which means that the hard disk is ready. This bit is used in the diagnosis of the hard disk, indicating that the hard disk is detected normally, and you can continue to execute some commands. The seventh bit is the BSY bit, indicating whether the hard disk is busy, if it is 1, it means the hard disk is busy, and other bits in this register are invalid. The other 4 are temporarily ignored.

When writing to the hard disk, the register name of port 0x1F7 or 0x177 is command, which is the same as the error and feature registers mentioned above, but the purpose has changed, so the name is changed to indicate a new purpose, and it is the same as the status register. This register is used to store the commands for the hard disk to execute. As long as the command is written into this register, the hard disk will start to work. 0xEC means hard disk identification. 0x20, read sector. 0x30, namely write sector.

Read and write hard disk through port

1. Select a channel first, and write the number of sectors to be operated into the sector count register of the channel.

2. Write the lower 24 bits of the sector start address to the three LBA registers on the channel

3. Write the 24-27 bits of the LBA address into the device register, and set the sixth bit to 1 to make it LBA mode, and set the fourth bit to select the master or slave

4. Write an operation command to the command register on the channel.

5. Read the status register on the channel to judge whether the hard disk work is completed.

6. If the above step is to read the hard disk, go to the next step. Otherwise, done.

7. Read out the hard disk data.

If the hard disk has completed its work, it has prepared the data. Generally, there are five commonly used data transmission methods:

1. Unconditional transmission

2. Query transmission

3. Interrupt transmission

4. Direct memory access (DMA)

5. IO processor transmission

For the first method, the data source device must be ready for data at any time, and the CPU can take it at any time. For example, registers and memory are similar devices, and the CPU does not need to call in advance when fetching data. Then the hard disk will not work, it is very slow.

For the second method, it means that before transmission, the program first checks the status of the device. The data source device can only transmit data under certain conditions. This type of device is usually a low-speed device, which is much slower than the CPU. When the CPU needs data, it first checks the status of the device, and if the status is "ready to send", the CPU then goes to get the data. The hard disk has a status register, which stores the working status, so this method can be used to obtain data from the hard disk.

For the third method, if the data source device notifies the CPU to fetch the data after it is ready, the efficiency will be high. Notifying the CPU can be interrupted. When the data source device is ready for data, it notifies the CPU to get the data by sending an interrupt, which avoids the CPU polling mechanism and is more efficient.

For the fourth method, it is also aimed at the third defect. The third interrupt processing method needs to save the scene and push the stack. The DMA controller can completely liberate the CPU and prevent the CPU from participating in the transmission. It is completely up to the data source device and Memory direct transfer. The CPU just goes directly to the memory to get the data.

For the fifth method, the CPU still needs to complete data exchange, combination, verification, etc., so it is better to introduce another piece of hardware without doing anything. Thus, the I/O processor was born. You can tell from the name that it is specially used to process IO, and it is actually a processor, but it uses another set of instruction systems that are good at IO, and can process data at any time. The IO processor will be integrated under the South Bridge chip before, but the latest processor will integrate the functions of the South Bridge and the North Bridge. It can only be said that the integration of the CPU is getting higher and higher.

rd_disk_m_16:
	mov esi,eax                    ;备份eax,也就是LBA地址
	mov di,cx                      ;备份cx,也就是待读入的扇区数
	;1、设置要读取的扇区数
	mov dx,0x1f2                   ;要写入的端口号要放在dx中
	mov al,cl
	out dx,al
	mov eax,esi                    ;恢复eax
	;2、将LBA地址存入0x1f3-0x1f6
	mov dx,0x1f3                   ;LBA地址7-0位
	out dx,al
	
	mov cl,8                       ;右移操作数
	shr eax,cl                     ;右移8位
	mov dx,0x1f4                   ;写入端口 LBA 15-8
	out dx,al

	shr eax,cl                     ;写入端口 LBA 13-16
	mov dx,0x1f5
	out dx,al
	
	shr eax,cl                     ;只需要LBA地址的24-27位
	and al,0x0f                    ;将al的前四位清零
	or al,0xe0                     ;将al的前四位设置为1110,即表示LBA模式
	mov dx,0x1f6                   ;写入到端口
	out dx,al
	
	;3、向0x1f7端口写入读命令0x20
	mov dx,0x1f7      
	mov al,0x20
	out dx,al
	
	;4、检测硬盘状态
.not_ready:
	;统一端口,写时表示命令字,读时表示读入硬盘状态
	nop
	in al,dx
	and al,0x88            ; 第4位为1表示硬盘已经准备好数据传输
						   ; 第7位位1表示硬盘忙
						   ; 如果硬盘准备好了,那么al的结果是0x08,否则位0x80
	cmp al,0x08            ; 下面这两行汇编可以表示为,如果结果不是0x80那么跳转
		
	jnz .not_ready
	
	;5、从0x1f0端口读数据
	mov ax,di     ; di为要读取的扇区数,这里我们只读取一个扇区,
				  ;一个扇区512字节,每次读取一个子,所以需要256次读取 
	mov dx,256
	mul dx        ; 乘法指令,被乘数隐藏在ax中
	mov cx,ax     ; cx中是循环次数
	mov dx,0x1f0

.go_on_ready:
	in ax,dx                ; 数据寄存器是16位的,所以这里每次读取两个字节
	mov [bx],ax             ;
	add bx,2                ;
	loop .go_on_ready       ;
	ret

0x13Read and write hard disk through interrupt

The assembly code of this section is as follows:

_start:
    mov     edx, 0          ; 初始化 EDX 寄存器,用于指定设备号和扇区号
    mov     ecx, 0          ; 初始化 ECX 寄存器,用于指定磁道号
    mov     ebx, 0          ; 初始化 EBX 寄存器,用于指定目标地址

    mov     ah, 2           ; AH 寄存器设置为 2,表示读取指定扇区数据
    mov     al, 1           ; AL 寄存器设置为 1,表示读取一个扇区
    push    edx             ; 将扇区号压到栈中
    mov     dword [esp+4], ebx ; 将目标地址压到栈中
    mov     ecx, 0          ; 磁道号设置为 0
    mov     edx, 0          ; 设备号为 0 硬盘
    mov     ebx, 0x80       ; 通过设置 LBA 模式(7位),EBX 寄存器的高一位为 1

    int     0x13            ; 调用 BIOS 中断读取扇区数据

    jc      error           ; 如果 CF 标志被设置,表示读取失败,跳转到 error 标签处

    jmp     $               ; 跳转回当前位置

error:
    ; 处理错误

This code uses BIOS interrupt 0x13to read the data of SATA hard disk head 0, track 0 and sector, and then store the data into the target address (the memory address specified in the EBX register). If AHthe register is set to 2, it means to read the specified sector data, and if ALthe register is set to 1, it means to read only one sector. Push the sector number onto the stack, then push the target address onto the stack, and use the value 0x80of EBXto set the upper bit of the register 1to enable LBA mode. If the operation fails, it will jump to errorthe label to handle the error.

address (the memory address specified in the EBX register). If AHthe register is set to 2, it means to read the specified sector data, and if ALthe register is set to 1, it means to read only one sector. Push the sector number onto the stack, then push the target address onto the stack, and use the value 0x80of EBXto set the upper bit of the register 1to enable LBA mode. If the operation fails, it will jump to errorthe label to handle the error.

Guess you like

Origin blog.csdn.net/weixin_43903639/article/details/129834328