1

First : This Question have a duplicate here :

How does a modern operating system like Windows or Linux know the chipset specific memory map?

But the answer in this Question is speaking about device tree and ACPI (for legacy PCs) without the details I need to write an assembly or c code to utilize the information in ACPI tables, I am now trying to learn about legacy pc first and how to decode the tables of the ACPI , I tried doing some search and I found that the most important table is the DSDT , Now my questions How to decode the information in the table to get a detailed memory map (ranges) and also to get which devices are connected to the CPU and how to get the address of the DSDT in memory ? , I tried doing some search but I couldn't understand the AML language which i think is related to this subject .. I will be helpful if any one elaborate and provide good material for understanding ACPI tables and decoding them for beginners , The problem for me I couldn't have a standard fixed memory map in mind because i want to know how the same os version run on different chipsets , there must be a dynamic way to detect the whole map , so a suggested process for getting the whole map is also another question of me

please note also this is my first step toward learning how to communicate directly with bare metal hardware devices each individually (which is the second step)

Thanks

12
  • 2
    @Joshua, not all the physical address space is backed by RAM. Some of it is mapped to device control registers, video memory etc. Basically there are to types of memory mappings, the virtual memory mappings set up by the OS which maps process memory to physical memory (or swap). And and the chipset which maps addresses on the data-bus to RAM or devices.
    – HAL9000
    Commented Dec 2, 2020 at 1:10
  • 1
    start with one specific bit of hardware without the bios in between, pick one. like video but start with an x86 in sim with legacy non-gpu video, good old video modes and work your way forward...that and hard drive controlllers, usb, file systems, etc will take a very long time then work foreward to current hardware where maybe a handful of people at one company know how it works and basically nobody else. and they wont share the knowledge you have to reverse engineer it if you can.
    – old_timer
    Commented Dec 2, 2020 at 2:00
  • 2
    you will want to rely on the bios just like an operating system does until it takes over so look at linux and bsd sources, mimic that.
    – old_timer
    Commented Dec 2, 2020 at 2:02
  • 2
    x86 motherboards can have some H/W that is semi-secret, that only the bios knows about. The bios is mated [by the motherboard manufacturer] to the exact H/W on the board. This is considered proprietary. Also, PC devices have a mechanism whereby various controllers provide extensions to the bios code that are attached during bios bootup. They allow the device to work via the bios int calls. So, don't blow off the bios. Without its help, it may not be possible to discover and/or use certain H/W until much later in the boot chain: bios to boot to OS. Commented Dec 2, 2020 at 2:33
  • 3
    Although a lot of H/W is discoverable by probing PciE slots, and presents itself via the PnP mechanism, some H/W configuration info is hardwired into the bios. That H/W is discoverable [only] by the ACPI interface. Commented Dec 2, 2020 at 2:38

1 Answer 1

5

Let's split this into 3 different problems.

How to get the address of the DSDT in memory?

The firmware provides a pointer to (one or 2) ACPI tables that contain an index of all other tables. These tables are the RSDT (Root System Descriptor Table) and XSDT (Extended Root System Descriptor Table); and the only real difference between them is that the XSDT supports 64-bit addresses (and should be used if possible) while the older RSDT does not (and should be considered "deprecated" and only used as a fallback for modern 64-bit operating systems). These tables mostly provide the identifier and address of all other tables; so if you want to find a specific table (e.g. the DSDT) you can search the index for the identifier (e.g. the 4 ASCII characters "DSDT") and find the entry that contains the table's physical address.

The pointer/s to the index are contained in a special structure called the RSDP (Root System Description Pointer); which is found in different ways for different types of firmware. For BIOS you have to search a few specific areas of physical memory looking for a special structure (with a special signature, and a valid checksum); and for UEFI you simply ask the firmware (and avoid a "cache pounding" search).

This is all (relatively clearly) described by the ACPI specification, including how to find the RSDT (e.g. the section "Root System Description Pointer (RSDP)"), and including the format of all structures and tables, and the meaning and purpose of all fields.

How to decode the information in the DSDT?

Because things can change after boot (due to hot-plug support, etc); static tables can't be used for some things and ACPI solves this problem (and creates more problems) by defining a special language called ASL (ACPI Assembly Language) that is compiled into a portable byte-code called AML (ACPI Machine Language).

The DSDT contains this AML.

To make any sense of it you need an AML interpreter, to execute the AML that is contained in the DSDT.

This is "very challenging" - to do it yourself you'll probably need to spend months studying the ACPI specification (and years working around bugs in different computers). Most people port an open source implementation (originally created by Intel) that is called ACPICA (see https://acpica.org/ ).

Sadly; being able to execute AML is only the first step. You also need to understand ACPI's namespaces, which functions/methods are provided by the AML and what they're supposed to do. To make this worse; ACPI's AML expects to be informed of what the OS is, and then enables/disables various features and changes its behavior to suit the OS (depending on what it was told the OS is); and often the only operating systems that are recognized by AML are versions of Windows and if you tell it something else it disables various features, so most operating systems just lie and say they are a version of Windows so that AML doesn't provide a crippled subset of its capabilities. However; "what each version of Windows does" (and how AML behaves for each specific version of Windows) is a horrible undocumented (by ACPI specs) disaster. Fortunately; ACPICA also hides the majority of this pain (for people that port ACPICA).

How to get a detailed memory map (ranges) and also to get which devices are connected to the CPU?

Mostly you don't. Specifically, you don't just "get a detailed memory map" in a nice single step.

Instead; you start by getting some minimal information about the physical memory from firmware (from int 0x15, eax=0xE820 for BIOS, or from GetMemoryMap() on UEFI). Then you use a variety of different sources to add more details to that minimal information, including but not limited to the CPUID instruction (for how many bits a physical address actually has), the ACPI "APIC/MADT" table (for IO APIC and local APIC addresses), the ACPI "EDT/HPET" table (for HPET addresses), the ACPI "MCFG" table (for the addresses of PCI Express memory mapped configuration space areas), the ACPI "SRAT" table (for NUMA information for memory areas and "hot-plug RAM" information), and possibly (optionally) the SMBIOS tables (if you care about things like what type of RAM is installed, etc).

After obtaining all the information you want from static/unchanging sources; you switch to "phase 2", which involves continually managing the memory map and trying to keep it up to date as information from various devices is found (and modified via. hot-plug events). This is where being able to execute AML (from DSDT, using your AML interpreter) becomes important. It also involves bus specific approaches (e.g. scanning PCI buses and extracting information from each PCI device's BARs/Base Address Registers).

Note that this isn't purely about filling in details for the memory map. It's better to think about it as discovering the resources that devices use, which includes IO ports, IRQ lines, DMA channels (and not just areas of the physical address space alone).

Also note that you only really need information about devices if/when you have a device driver that is capable of using the information to "drive" the device. If you don't, then the memory map you have will just say "reserved" and you won't know why, but you probably won't have a reason to care why anyway.

Final Note

This is "very daunting" at first glance. Don't be worried - you can (and should) start small and ignore most of it until much much later. You can do a lot with the minimal information about the physical memory from firmware alone; and add code to do almost everything else if/when it actually matters for your OS one day.

8
  • Good, but where is the link to ACPI specification? What you are telling here is good, but specification shows it in details with pictures and ASL examples.
    – 0andriy
    Commented Dec 2, 2020 at 20:00
  • @0andriy You can find all the ACPI specs and related UEFI specs at: uefi.org/specifications Commented Jan 23, 2023 at 1:44
  • @smwikipedia, Thank you, I know that. My point is to make Brendan to update the post with what spec already has provided.
    – 0andriy
    Commented Jan 25, 2023 at 9:38
  • @0andriy I found the ACPI spec is awkwardly long. So here is a very concise ASL tutorial: cdrdv2-public.intel.com/772722/asl-tutorial-v20190625.pdf Commented yesterday
  • 1
    @smwikipedia, the full spec is indeed long and most of it is the description of the ASL operators. The second biggest part is the ACPI hardware. None of this are being referred to. The rest is the small subset of the sections of the selected chapters, which can be studied in a day by non-prepared reader. Those links are still missing in the answer. I would love to +1 to it, but not in the current form.
    – 0andriy
    Commented 6 hours ago

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