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

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Eng_Boody
  • 183
  • 8
  • Wait? What chipsets have nonlinear memory mapping in early boot? Thing is, I've done this. For main memory I just scribbled on memory after making the bios call returning the scalar value for how much there is. – Joshua Dec 01 '20 at 23:20
  • @Joshua could you please elaborate more about what you mean by nonlinear memory map .. am just a beginner and trying to understand those terms and how things are going on – Eng_Boody Dec 01 '20 at 23:25
  • I have just made a bare metal bootloader to print hello world using bios calls I am very happy by that , but iam trying to communicate directly with hardware without the bios in between thats my goal and what i am trying to learning about – Eng_Boody Dec 01 '20 at 23:26
  • 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 Dec 02 '20 at 01:10
  • I assume you are very well versed in bare metal somewhere else other than x86 before even contemplating a project like this? – old_timer Dec 02 '20 at 01:57
  • Full on raspberry pi and beaglebone black, etc support. – old_timer Dec 02 '20 at 01:57
  • 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 Dec 02 '20 at 02:00
  • 1
    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 Dec 02 '20 at 02:02
  • if you want to bypass the bios earlier then you will have to disassemble the bios which with x86 is a major project itself. then reverse engineer what you find. you wont know what any of the registers are or do, just that bits are poked into them. – old_timer Dec 02 '20 at 02:04
  • 1
    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. – Craig Estey Dec 02 '20 at 02:33
  • 2
    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. – Craig Estey Dec 02 '20 at 02:38
  • 1
    If you are familiar with U-Boot, you may look into its source code and at least few (x86) platform has been recreated to have ACPI tables from scratch. Very helpful is the latest ACPI specification (see https://uefi.org). – 0andriy Dec 02 '20 at 19:59

1 Answers1

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 thing 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 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.

Brendan
  • 35,656
  • 2
  • 39
  • 66
  • 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 Dec 02 '20 at 20:00
  • @0andriy You can find all the ACPI specs and related UEFI specs at: https://uefi.org/specifications – smwikipedia Jan 23 '23 at 01:44
  • @smwikipedia, Thank you, I know that. My point is to make Brendan to update the post with what spec already has provided. – 0andriy Jan 25 '23 at 09:38