Page 1 of 1

How is hardware mapped to memory?

Posted: Sat Sep 01, 2012 7:15 pm
by basharh
I've been trying to understand hardware programming for some time and I've reached a dead-end.

From what I know, hardware looks like memory addresses to processors.

I have several questions:

1. How are the memory addresses corresponding to the different hardware devices established?
2. How many memory addresses are allocated per device?
3. What do the memory addresses correspond to inside the device?
4. Where can I get information about how to talk to devices through their memory addresses?
5. What is the role of the BIOS in all of this?

I've read a lot of material on processors/hardware but nothing answered these questions. I basically want to know what hardware looks like to assembly and to eventually write a program that runs directly on bare metal without the OS.

Any book recommendations that you think will point me to the right direction would be highly appreciated.

Re: How is hardware mapped to memory?

Posted: Sat Sep 01, 2012 10:25 pm
by xenos
If you take, for example, a modern PC, most memory mapped devices (such as the I/O APIC, HPET...) are part of the mainboard chipset. Their addresses can usually be looked up through the programmer's manual of your mainboard chipset, if the manufacturer provides one. The CPU communicates with these devices through the memory bus. The chipset checks whether a memory address corresponds to some device or to actual RAM, and redirects the access accordingly.

Many embedded systems are constructed a bit differently. They are mostly based on SoCs (system on chip), so there is a single chip which contains not only the CPU, but also many other peripheral devices such as USB ports, camera interfaces, PCI hardware... In this case their memory addresses are mapped to their devices internally. For many SoCs, they are documented in some technical datasheets.

Actually I'm working on a project to document also those memory adresses / device locations which are not well documented / published by the manufacturers. Have a look at the link in my signature (Programmers Hardware Database).

Re: How is hardware mapped to memory?

Posted: Sat Sep 01, 2012 11:10 pm
by Brendan
Hi,


If you look at modern hardware, it's a bit like networking. An address is like an IP address, and various pieces of hardware are like routers. The CPU requests a read or write from an address, the routers route this request to the relevant piece of hardware, and the relevant piece of hardware performs the request (including sending a reply back to the CPU for reads).

The first "router" is the memory controller. This could be built into the CPU/s or part of the chipset. Its job is to determine if the read/write should got to a RAM chip or to the PCI bus. If the request is routed to the RAM chips there's more routers to determine which chips.

For PCI, each bridge (including the host bridge) is another little router that decides if a request should be forwarded from one bus to another (or, from the bridge's primary bus to the bridge's secondary bus). A request might pass through several PCI bridges (on several PCI buses) before it reaches the right bus. When a request reaches the right PCI bus, all devices on that bus check if they should handle the request (e.g. by seeing if the address in the request is within a range of addresses described by one of the device's BARs).
basharh wrote:1. How are the memory addresses corresponding to the different hardware devices established?
By configuring the memory controller/s and PCI bridges such that requests for addresses in that range make it to bus that the memory mapped device is connected to; and by configuring the device's BARs to respond to addresses in that range. This is normally meant to be done by firmware, sort of (firmware might configure some devices in "legacy" modes for backward compatibility and therefore might not configure memory mapped areas how an OS wants them).
basharh wrote:2. How many memory addresses are allocated per device?
Each device is different. One device might have 3 different areas that are all huge; and another device might only have one area that is tiny.
basharh wrote:3. What do the memory addresses correspond to inside the device?
Depends on the device. Typically they're used for registers that control the device. For example, writing to a certain address might set a register that causes the hardware to launch a weather balloon, or set a pixel, or adjust a sound channel's volume, or whatever.
basharh wrote:4. Where can I get information about how to talk to devices through their memory addresses?
For specific devices you'd need information for that specific device (e.g. manufacturer's datasheet). For general information about how devices communication, PCI specifications might be a good start.
basharh wrote:5. What is the role of the BIOS in all of this?
Firmware/BIOS is meant to configure things during boot; and firmware/BIOS might use some devices to boot; and firmware/BIOS might provide an API that software can use as a very basic/limited driver for some devices (until OS starts its own drivers).


Cheers,

Brendan

Re: How is hardware mapped to memory?

Posted: Sun Sep 02, 2012 8:45 am
by basharh
This is all great information. But things are still unclear to me.

I can probably go into datasheets, but I'm looking for some general knowledge about the BIOS and the devices programming first to get the bigger picture.

Which makes me think of more questions:

1. How does Linux(or any other OS) enumerate the devices on the machine?
2. Does it read this info from somewhere in the BIOS?
3. How can it tell which BIOS it's talking to and how is the info in the BIOS is arranged?
4. Is there a unified BIOS specification that is the same for all computers(motherboards)?
5. Are there books that particularly tries to address these questions?
6. Are there books out there about bare-bones programming and interfacing with the BIOS/hardware?

Most of the books I've read were either at the kernel level or at the Electronics(flops, gates, signals...) level which is not what I'm looking for.

My ultimate aim is to write a simple program that can run on a machine and enumerate its devices(model numbers?). Figuring out the addresses and model number, I could then go into the particular device datasheet and read those. And then maybe draw something on the screen based on data from a USB or HD. And then probably go further into writing drivers, a simple OS, etc...

Thanks for the help!

Re: How is hardware mapped to memory?

Posted: Sun Sep 02, 2012 9:05 am
by bluemoon
basharh wrote:1. How does Linux enumerate the devices on the machine?
There are standard procedures and protocols like ACPI tables, PCI bus scanning, USB, etc
For very ancient legacy device, it's done by direct probe(sending signal to specific port/memory address and see if there is response) or "told by user".
basharh wrote:2. Does it read this info from somewhere in the BIOS?
I'm not sure on Linux, but BIOS constructs some tables (eg. ACPI, legacy devices like serial/parallel port, etc)
basharh wrote:3. How can it tell which BIOS it's talking to and how is the info in the BIOS is arranged?
You can think there is only one BIOS, which can be hook and extended by various cards. However, the internal of BIOS is specific to implementation like a black box.
basharh wrote:4. Is there a unified BIOS specification that is the same for all computers(motherboards)?
There is a set of well-documented functions and structures that worked on most machines, see Ralf Brown Interrupt List.

Re: How is hardware mapped to memory?

Posted: Mon Sep 03, 2012 7:51 pm
by liaoo
1. How does Linux(or any other OS) enumerate the devices on the machine?
I didn't know how linux or other OS enumerate the devices...but I know how BIOS does.

AFAIK, BIOS will perform device scan in the POST sequence before handing over control to OS. Different devices are identified and detected at different stages. After scanning devices BIOS will construct specific tables and then these tables are referenced/used/maintained by OS. Take PCI device scan for example, BIOS will scan them based on PCI specification Ex. locate its (bus,device,function), check how much resource(IO,Memory,DMA) it needs, read base/sub class code, get the interrupt pin and setup its interrupt line, etc...
2. Does it read this info from somewhere in the BIOS?
Some tables built by BIOS will be used and maintained by OS Ex. ACPI tables(you can check ACPI specification...)
3. How can it tell which BIOS it's talking to and how is the info in the BIOS is arranged?
AFAIK there is only one BIOS in the system. Basically speaking BIOS is composed of many specifications(PCI,USB,CPU,AHCI,SATA,Processor,...), datasheets, and some underground docs. Thus the best way to know about these is to be a BIOS engineer :lol:

Some links might be helpful:
http://www.amazon.com/BIOS-Disassembly- ... 1931769605
http://www.amazon.com/Beyond-BIOS-Devel ... im_sbs_b_2
http://www.amazon.com/The-BIOS-Companio ... im_sbs_b_4
4. Is there a unified BIOS specification that is the same for all computers(motherboards)?
AFAIK there are 4 BIOS vendors and they are phoenix, AMI, AWARD, and Inside; they all have their own architecture... And the latest unified BIOS specification might be EFI(Extensible Firmware Interface) and you can find lots of information on internet.
5. Are there books that particularly tries to address these questions?
Please check above links and do more search on internet...
6. Are there books out there about bare-bones programming and interfacing with the BIOS/hardware?
As I said, BIOS is composed of many HW and SW specs thus you might need to schedule you time to study them...