0
$\begingroup$

I have basic understanding of file I/O. I know how to operate the read and write system calls in Linux. I know that there is blocking and non-blocking I/O. I want to know more about the underlying mechanism in OS for I/O. How is blocking and non-blocking I/O implemented? How do the kernel I/O buffers work? How big are they and when are they "erased"? For example if there is an open socket how much data will be kept in the kernel buffer before the data is overwritten? I prefer a book of some kind. And please don't tell me to go and read the source. I konw that if I REALLY want to know how those things work that is the best I can do, but I simply am unable to dedicate so much time to this subject.

$\endgroup$
2
$\begingroup$

It is pretty complex because there are several layers between the actual hard-disk and the software implementation of sockets. In today's x86 computers, the actual hard-disk is read using the Intel's AHCI controller which is a PCI device. All SATA plugs on your motherboard are directly connected to the AHCI. The AHCI will control the hard-disk and present a complex interface to software so that it can read/write to/from the hard-disk.

The best way to learn those things is to write an OS yourself. Get the basics of bootloading then write a minimal OS and this knowledge will come by itself over time. The MCFG ACPI table informs software of the address of the beginning of the PCI configuration space. Then you can scan PCI buses (more often only bus 0 for home computers). On bus 0 you can have up to 32 devices which can each have up to 8 functions. The function is what the device actually does. Each function of each device will have a configuration space which allows to get more information on the function. It will present class IDs and subclass IDs will allow to identify the different devices. As stated on osdev.org (https://wiki.osdev.org/AHCI).

An AHCI controller can be found by enumerating the PCI bus. It has a class id 0x01 (mass storage device) and normally a subclass id 0x06 (serial ATA). The vendor id and device id should also be checked to ensure it’s really an AHCI controller.

Once you found an AHCI on the PCI bus, you can look at the specification of the AHCI for implementing a driver: https://www.intel.com/content/www/us/en/io/serial-ata/serial-ata-ahci-spec-rev1-3-1.html. This spec really covers what you need to implement an AHCI driver.

Blocking versus non-blocking I/O is a software construct. Blocking I/O waits for the data buffer to be filed while non-blocking I/O will return immediately if there are no data ready. Non-blocking I/O expects you to call the read again so that you can get a full buffer. It isn't this complex for hard-disks while for network it is very complex. Basically, the AHCI is a DMA device (since it is PCI compliant and PCI devices are DMA in nature since they have access to RAM bypassing the CPU). I never implemented a driver for it but the AHCI will be triggered by a command and, once the command is completed, will trigger an interrupt on a certain vector using the MSI-X capability of PCI devices. The MSI-X capability allows to specify a vector to trigger on command completion.

I think this answer is a good starting point for you on what to read and what to do. I doubt there will be a good book on this matter for you to read. You should implement an AHCI driver and socket layer above this driver to really understand how it works under the hood. The socket layer is a POSIX specification that you can find on internet.

$\endgroup$
1
  • $\begingroup$ Also the AHCI spec is short compared to most Intel's specs. Maybe start by reading that and you'll have a very good understanding of how Linux or Windows do things. $\endgroup$
    – user123
    May 12 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.