简体   繁体   English

CPU处于保护模式时如何读/写硬盘?

[英]How to read/write a hard disk when CPU is in Protected Mode?

I am doing an OS experiment. 我正在做一个OS实验。 Until now, all my code utilized the real mode BIOS interrupts to manipulate the hard disk and floppy. 到目前为止,我的所有代码都使用实模式BIOS中断来操作硬盘和软盘。 But once my code enables Protected Mode, all the real mode BIOS interrupt service routines won't be available. 但是,一旦我的代码启用保护模式,所有实模式BIOS中断服务程序将不可用。

I have a feeling that I need to do some hardware drivers now. 我觉得我现在需要做一些硬件驱动程序。 Am I right? 我对吗? Is this why an OS is so difficult to develop? 这就是操作系统如此难以开发的原因吗?

I know that hardware is controlled by reading from and writing to certain control or data registers. 我知道通过读取和写入某些控制或数据寄存器来控制硬件。 For example, I know that the "Command Block Registers" of a hard disk range from 0x1F0 to 0x1F7 . 例如,我知道硬盘的“命令块寄存器”范围从0x1F00x1F7 I am wondering whether the register addresses of so many different hardware devices are consistent on different platforms? 我想知道这么多不同硬件设备的寄存器地址在不同平台上是否一致? Or do I have to detect that before using them? 或者我必须在使用之前检测到它? How would I do that? 我该怎么办?

Since I am not sure about how to read/write a floppy or a hard disk in Protected Mode, I have to use BIOS interrupts to load all my necessary kernel files from the floppy before entering protected mode. 由于我不确定如何在保护模式下读/写软盘或硬盘,因此在进入保护模式之前,我必须使用BIOS中断从软盘加载所有必需的内核文件。 What could I do if my kernel file exceeds the real mode 1M space limit? 如果我的内核文件超过实际模式1M空间限制,我该怎么办?

How do I read/write a hard disk when the CPU is in Protected Mode? 当CPU处于保护模式时,如何读/写硬盘?

Programming ATA-PIO Mode http://wiki.osdev.org/ATA_PIO_Mode IDE Devices: http://wiki.osdev.org/IDE 编程ATA-PIO模式http://wiki.osdev.org/ATA_PIO_Mode IDE设备: http//wiki.osdev.org/IDE

Floppy Drives: http://wiki.osdev.org/Floppy_Disk_Controller 软盘驱动器: http//wiki.osdev.org/Floppy_Disk_Controller

I encourage you to register at OSDEV forum, the community is very helpful and polite, even for newbies. 我鼓励您在OSDEV论坛注册,社区非常有帮助和礼貌,即使是新手。

I have a feeling that I need to do some hardware drivers now. 我觉得我现在需要做一些硬件驱动程序。 Am I right? 我对吗?

Strictly speaking; 严格来说; (and depending on your requirements) "need" may be too strong - in theory you can switch back to real mode to use BIOS functions, or use a virtual8086 monitor, or write an interpreter that interprets the firmware's instructions instead of executing them directly. (并且根据您的要求)“需要”可能太强 - 理论上您可以切换回实模式以使用BIOS功能,或使用virtual8086监视器,或者编写解释器来解释固件的指令而不是直接执行它们。

However, the BIOS is awful (designed for an "only one thing can happen at a time" environment that is completely unsuitable for modern systems where its expected that all devices are able to do useful work at the same time), and the BIOS is deprecated (replaced by UEFI), and it's hard to call something an OS when it doesn't have control over the hardware (because the firmware still has control of the hardware). 然而,BIOS很糟糕(设计用于“一次只能发生一件事”的环境,完全不适合现代系统,它预期所有设备能够同时做有用的工作),而BIOS则是不推荐使用(由UEFI取代),当它无法控制硬件时(因为固件仍然可以控制硬件),很难将操作系统称为操作系统。

Note that if you do continue using BIOS functions; 请注意,如果您继续使用BIOS功能; the state of various pieces of hardware (interrupt controller, PCI configuration space for various devices, any PCI bridges, timer/s, etc) has to match the expectations of the BIOS. 各种硬件(中断控制器,各种设备的PCI配置空间,任何PCI桥,定时器等)的状态必须符合BIOS的期望。 What this means is that you will either be forced to accept huge limitations (eg never being able to use IO APICs, etc. properly) because it will break BIOS functions used by other pre-existing code, or you will be forced to do a huge amount of work to make the BIOS happy (emulating various pieces of hardware so the BIOS thinks the hardware is still in the state it expects even though it's not). 这意味着您将被迫接受巨大的限制(例如,永远无法正确使用IO APIC等),因为它会破坏其他预先存在的代码所使用的BIOS功能,或者您将被迫执行大量的工作让BIOS开心(模拟各种硬件,以便BIOS认为硬件仍处于预期的状态,即使它不是)。

In other words; 换一种说法; if you want an OS that's good then you do need to write drivers; 如果你想要一个好的操作系统那么你需要编写驱动程序; but if you only want an OS that doesn't work on modern computers (UEFI), has severe performance problems ("only one thing can happen at a time"), is significantly harder to improve, doesn't support any devices that the BIOS doesn't support (eg sound cards), and doesn't support any kind of "hot-plug" (eg plugging in a USB device), then you don't need to write drivers. 但是如果你只想要一台在现代计算机(UEFI)上不起作用的操作系统,那么会出现严重的性能问题(“一次只能发生一件事”),难以改进,不支持任何设备BIOS不支持(例如声卡),并且不支持任何类型的“热插拔”(例如插入USB设备),那么您不需要编写驱动程序。

Is this why an OS is so difficult to develop? 这就是操作系统如此难以开发的原因吗?

A bad OS is easy to develop. 糟糕的操作系统很容易开发。 For example, something that is as horrible as MS-DOS (but not compatible with MS-DOS) could probably be slapped together in 1 month. 例如,像MS-DOS一样可怕的东西(但与MS-DOS不兼容)可能会在1个月内被打成一片。

What makes an OS difficult to develop is making it good. 使操作系统难以开发的原因是使其变得更好。 Things like caring about security, trying to get acceptable performance, supporting multi-CPU, providing fault tolerance, trying to make it more future-proof/extensible, providing a nice GUI, creating well thought-out standards (for APIs, etc), and power management - these are what makes an OS difficult. 关心安全性,尝试获得可接受的性能,支持多CPU,提供容错,尝试使其更具未来性/可扩展性,提供良好的GUI,创建经过深思熟虑的标准(用于API等),和电源管理 - 这些使操作系统变得困难。

Device drivers add to the difficulty. 设备驱动程序增加了难度。 Before you can write drivers you'll need support for things that drivers depend on (memory management, IRQ handling, etc - possibly including scheduler and some kind of communication); 在您编写驱动程序之前,您需要支持驱动程序所依赖的内容(内存管理,IRQ处理等 - 可能包括调度程序和某种通信); then something to auto-detect devices (eg to scan PCI configuration space) and try to start the drivers for whatever was detected (possibly/hopefully from file system or initial RAM disk, with the ability to add/unload/replace drivers without rebooting); 然后自动检测设备的东西(例如扫描PCI配置空间)并尝试为检测到的任何东西启动驱动程序(可能/希望从文件系统或初始RAM磁盘,能够添加/卸载/替换驱动程序而无需重新启动) ; and something to manage the tree of devices - eg so that you know which "child devices" will be effected when you put a "parent device" to sleep (or the "parent device" has hardware faults, or its driver crashes, or the device is unplugged). 以及管理设备树的东西 - 例如,当您将“父设备”置于睡眠状态时(或“父设备”出现硬件故障,或其驱动程序崩溃,或者设备已拔下插头)。 Of course then you'd need to write the device drivers, where the difficulty depends on the device itself (eg a device driver for a NVidia GPU is probably harder to write than a device driver for a RS232 serial port controller). 当然,您需要编写设备驱动程序,其中难度取决于设备本身(例如,NVidia GPU的设备驱动程序可能比RS232串行端口控制器的设备驱动程序更难编写)。

For storage devices themselves (assuming "80x86 PC") there's about 8 standards that matter (ATA/ATAPI, AHCI and NVMe; then OHCI, UHCI, eHCI and xHCI for USB controllers, then the USB mass storage device spec). 对于存储设备本身(假设“80x86 PC”),大约有8个标准(ATA / ATAPI,AHCI和NVMe;然后是OHCI,UHCI,eHCI和xHCI用于USB控制器,然后是USB大容量存储设备规范)。 However, there is also various RAID controllers and/or SCSI controllers where there's no standard (each of these controllers need their own driver), and some obsolete stuff (floppy controller, tape drives that plugged into floppy controller or parallel port, three proprietary CD-ROM interfaces that were built into sound cards). 然而,还有各种RAID控制器和/或SCSI控制器,其中没有标准(每个控制器都需要自己的驱动程序),以及一些过时的东西(软盘控制器,插入软盘控制器或并行端口的磁带驱动器,三个专有CD -ROM接口内置于声卡中)。

Please understand that supporting all of this isn't the goal. 请理解支持所有这些不是目标。 The goal should be to provide things device drivers depend on (described above), then provide specifications that describe the device driver interfaces (possibly/hopefully including things like IO priorities and synchronization, and notifications for device/media removal, error handling, etc) so that other people can write device drivers for you. 目标应该是提供设备驱动程序依赖的东西(如上所述),然后提供描述设备驱动程序接口的规范(可能/希望包括IO优先级和同步,以及设备/介质删除,错误处理等通知)以便其他人可以为您编写设备驱动程序。 Once that's done you might implement a few specific device drivers yourself (eg maybe just AHCI initially - everything else could be left until much later or until someone else writes it). 一旦完成,您可以自己实现一些特定的设备驱动程序(例如,最初可能只是AHCI - 其他所有内容都可以保留到很久以后或直到其他人编写它)。

You don't necessarily HAVE to write drivers. 你不一定要编写驱动程序。 You could drop back into real mode to call the BIOS service, and then hop back into protected mode when you're done. 您可以退回到实模式以调用BIOS服务,然后在完成后跳回到保护模式。 This is essentially how DPMI DOS extenders (DOS4GW, Causeway, etc) work. 这基本上是DPMI DOS扩展器(DOS4GW,铜锣等)的工作原理。

The source code for the Causeway DOS extender is public domain, you can look at that for a reference. Causeway DOS扩展程序的源代码是公共域,您可以查看它以供参考。 http://www.devoresoftware.com/freesource/cwsrc.htm http://www.devoresoftware.com/freesource/cwsrc.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM