"Self-written operating system" Nineth Chapter a simple hard drive implementation

Source: Internet
Author: User
Tags prefetch

Absrtact: We all know that the disk consists of a controller, a magnetic prime component. When we edit a file at the terminal, how does the file content get to the disk from memory? What the disk drive is primarily doing. How the hard drive interrupts are triggered. And so on, these are the fundamentals of hard disk work. This article mainly from "self-write operating system", by implementing a simple hard drive, to explain the basic principle of hard disk work.


1. Program execution Flow: 1) TASK_HD () as a daemon, initialize the hard disk, and then run, waiting to receive messages from other processes
2) If you receive a message to open the device, then identify (0) will be executed; otherwise, an error message prompts
3) Identify (0): Want hard drive to send identify command, wait for interrupt arrival, after interrupt arrives, read hard disk information from data register
4) The hard drive information obtained in the Print Step 3)

5) The contents of the interrupt handler: reads the busy state of the hard disk; notifies the driver

*6) file system sends an "open device" command to the driver


2. Questions and Answers

2.1. Which commands can be received by the hard drive. What kind of processing results.

Hard drives can accept many commands (both hardware-defined and system-independent). Here, we use the IDENTIFY Device command, explained below: Reference manual: At Attachment with packet Interface document p113:the IDENTIFY device command enables the Host to receive parameter information from the device.

Some devices may has to read the media in order to complete this command.

When the command was issued, the device sets the BSY bit to one, prepares to transfer the words of device
Identification data to the host, sets the DRQ bit-to-one, clears the BSY bit to zero, and asserts INTRQ if Nien
is cleared to zero. The host May and then transfer the data by reading the data register. Table defines the
Arrangement and meaning of the parameter words in the buffer. All reserved bits or words shall is zero.

Some parameters is defined as a 16-bit value. A word that's defined as a 16-bit value places the most
Significant bit of the value on signal line DD15 and the least significant bit on signal line DD0 (see 3.2.9).

Some parameters is defined as 32-bit values (e.g., words (61:60)). Such Fields is transferred using
Successive word transfers. The device shall first transfer the least significant bits, bits (15:0) of the value, on
Signal lines DD (15:0) respectively. After the least significant bits has been transferred, the most significant
Bits, bits (31:16) of the value, shall is transferred on DD (15:0) respectively (see 3.2.9).

Some parameters is defined as a string of acsii characters. Such fields is transferred as defined in 3.2.9.

2.2. How to notify the driver.
See Inform_int () Related codes

3. Other matters to be aware ofWhat resources a process has. Where you need to register.
The relevant information in the PCB. After we add a task, we need to register with task_table or somewhere else.
3.1 Don't forget about updates in Proc_tabel etc.

4. About Port_read: Non-prefetch typeThere are two types of IO operations, the pre-fetch type and the non-prefetch type, which is mainly RAM, the latter being the device register. Have you ever wondered about this port_read function? Why read multiple times, can read different information. -This is because the device register itself is the interface of the FIFO queue and is a non-prefetch type.
Where the INSW instruction is used as follows:
INS M8, DX
Input bytes into ES: (E) DI specified memory location from DX specified I/O port
INS M16, DX
Input the word into ES: (E) DI specified memory location from DX specified I/O port
INS M32, DX
Double-word input ES: (E) DI specified memory location from DX specified I/O port
INSB
Input bytes into ES: (E) DI specified memory location from DX specified I/O port
Insw
Input the word into ES: (E) DI specified memory location from DX specified I/O port
Insd
Double-word input ES: (E) DI specified memory location from DX specified I/O port

Here, if we must remember to export the Port_read function, this function cannot be used by external modules.


5. How does a hard drive interrupt interact with the driver? here is a very important function: Hd_identify (), the key process inside is as follows:
Hd_cmd_out ();
Interrup_wait ();
Port_read ();

Send command to hard disk--wait for interrupt arrival--read data port 6. Hard drive Construction and working principle the internal drive structure consists of a fixed panel, a control circuit and a board, a magnetic head, a disc, a spindle, a motor, an interface and other accessories, wherein the head disc component is the core of the hard disk, It is encapsulated in the purge chamber of the hard drive, including parts of the floating head assembly, the head drive mechanism, the disc, the spindle drive and the pre-read and write control circuitry.
After we send a command to the hard drive, the hard disk controller accepts the command and starts execution, and after the command is executed, the hard drive is interrupted, the driver is notified, and the data is ready.

7. Summary Now, there is no clear picture of hard disk work in your mind. The BIOS detects the hard drive, reads the basic information of the hard disk to a specific location in the memory, assigns the corresponding port number to the hard disk register through the PCIe configuration, and the operating system interrupts the hard drive to bind the specific interrupt handler (mainly the driver), the driver runs, the file system runs, The file system sends a request to the driver to read the hard disk information, the hard drive sends the corresponding operation command to the hard drive and waits for the drive to interrupt the response; After receiving the drive interrupt response, the driver reads the port data into the buffer and gives it to the file system for processing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.