Sunday, April 13, 2008

e820, detect_memory from BIOS

Yap. Here we are, the beginning of C code, in arch/x86/boot/main.c, what a relief :-).
Basically, the main() function call a bunch of other real mode C code to prepare itself for protected mode environment.
As usual, I will lazily and shamelessly skip some of the details.

Ok, so today we are going to skip all the other function calls in main() except the detect_memory() call.
Basically, detect_memory() just call another 3 functions to carry out the actual work for itself. They are detect_memory_e820(), detect_memory_e801() and detect_memory_88(). Hmm...the names are strange enough...what does the number means? We can clearly see that in detect_memory_e820(), it calls the inline assembly instruction INT 15h. After browsing around, here is what I found out about e820.

To see how this is more relevant those who doesn't want to read the source, I checked out dmesg after booting and found out this part:

Now compare that with the /proc/iomem entries:

Do you see any resemblance?
So, basically, my guess is that in arch/x86/boot/main.c and memory.c, detect_memory() do a BIOS interrupt call to get the basic physical memory layout to know how much RAM is really in the system as well as other details such as what range is assigned for what purposes, before it switch from real mode to protected mode (if I understand correctly, once we are in protected mode, we can't do BIOS interrupt call anymore as they are 16 bit real mode code). There are other relevant details to e820 in the kernel source, such as those found in arch/x86/mach-default/setup.c but I haven't look at them yet.


Jiang said...


S:oNNe said...

Can you give the source code of e 820 bios call. how can we do this after booting the computer??

weichong78 said...

Under arch/x86/boot/memory.c:
there is a static int detect_memory_e820(void) function.
Maybe it can help you.

yousafsajjad said...

where is arch/x86/boot/memory.c

can you please tell me the direct link or something

weichong78 said...

arch/x86/boot/memory.c is a sub directory of the Linux kernel source code.