Detecting amount of memory

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
posman
Posts: 19
Joined: Fri Sep 05, 2008 12:55 pm

Detecting amount of memory

Post by posman »

Hi

I just implemented the function to know the amount of memory. (I use int 15h/AX=E820h) But I have a question.
This are the entries I get

Image

The last entry has its base at 0xFFFC0000. I think this is, well, not wrong, but this entry should be discarded. right?

How can I know if the last entry should be discarded or not?
I got a similar output from VirtualBox
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: Detecting amount of memory

Post by CodeCat »

It depends on whether you need it. The type is 2, so it's reserved whether you ignore it or not because you should treat undefined regions as reserved. However, the BIOS wouldn't give you that area if there wasn't something special there, so maybe you could try finding out what's actually mapped at that address and use it somehow.
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Detecting amount of memory

Post by bewing »

That is the memory for your video card. You will need to know that it is there, when you are mapping memory.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Detecting amount of memory

Post by Brendan »

Hi,
bewing wrote:That is the memory for your video card. You will need to know that it is there, when you are mapping memory.
Um, no. The 256 KiB area from 0xFFFC0000 to 0xFFFFFFFF would be the BIOS ROM.

For modern 80x86, when the CPU starts it's first instruction is at 0xFFFFFFF0, which needs to be ROM because RAM doesn't work yet (and wouldn't contain anything if it did). During boot the BIOS copies part of itself into RAM at the legacy BIOS area (e.g. between 0x000F0000 to 0x000FFFFF) and then tells the memory controller to ignore writes to this area of RAM (more correctly, it usually tells the memory controller to forward writes in this area to the PCI bus instead of to the RAM chips, and the PCI bus ignores the writes).

A similar thing happens for PCI device ROMs (e.g. video card's ROM, SCSI controller's ROM, etc). In this case the BIOS uses the device's BAR in PCI configuration space to map the ROM somewhere in the physical address space, then copies data from the ROM into RAM, calls the ROM's entry point, then disables the ROM using the BAR, and then tells the memory controller to ignore writes to the area of RAM.

Basically, none of the "ROMs" in the area from 0x000C0000 to 0x000FFFFF are actually ROMs anymore...


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Re: Detecting amount of memory

Post by ~ »

The memory detection algorithm I have always used involves manual reading/writing to the memory. It seems to never have failed, although I have never used it in an old system (1996) with ISA devices attached.

In the newest system I have, from 2005, I just do this, several times in a loop, with "random" values:
  • Take as accounted the memory amount from the position the kernel was copied to.
  • Save the original value temporarily.
  • Write a value every 1048576 bytes.
  • Do some push's/pop's, another memory reads, etc., to try to flush cache and read actual memory next
  • Read back the memory position to see if it has the expected value.
  • Restore the original value.
  • If the value was preserved, account one more Megabyte. If not, stop accounting.

It seems to work fine. It gives me a plain memory amount up to where I know I really installed RAM.

For example, I have 512Mb installed, and my integrated video card has 64Mb assigned to it. So, when I run this code I properly account 448Mb, as if the chipset was configured to make that part of RAM "invisible".

However, I don't know how much reliable this method is. I guess it is very reliable indeed in the newest systems (the way they handle RAM could be more "standardized"). And for old systems, if there's some way to detect the memory usage of ISA devices, along with some further PCI scanning, I see no problem.

A problem could be defective RAM with bad areas in the middle, in which case the best approach could be to just change the memory modules to put new ones.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Detecting amount of memory

Post by Brendan »

Hi,
~ wrote:The memory detection algorithm I have always used involves manual reading/writing to the memory. It seems to never have failed, although I have never used it in an old system (1996) with ISA devices attached.
Let me guess:
  • You've never tried it on a system with memory mapped ISA devices.
  • You've never tried it on older systems (especially older systems that remap the part of RAM that's underneath the ROM to the top of RAM, so that with 4 MiB of RAM installed there's 3072+64 KiB of RAM starting at 0x000100000).
  • You've never tried it on older systems, where "bus float" can make unused addresses seem like RAM even though you flush caches.
  • You've never tried it on a newer system with 3 GiB of RAM (or more) and lots of memory mapped PCI devices, where there is no unused space above the RAM.
  • You've never tried it on an OS that supports multi-CPU/SMP (trashing the MP specification tables and/or ACPI tables wouldn't help).
  • You've never tried it on an OS that supports sleep states (standby, hybernate, etc).
  • You've never tried it on an OS that supports ACPI for anything else.
  • You've never tried it on an OS that configures PCI BARs properly (because for that you needs a full physical address space map to know which areas of the physical address space are unused).
~ wrote:A problem could be defective RAM with bad areas in the middle, in which case the best approach could be to just change the memory modules to put new ones.
Would you replace this defective RAM before or after your OS has let faulty RAM (that the BIOS tried to tell the OS not to use) trash all your company's data on a server that nobody logs into because it's in a rack in the corner of a server room being used for mySQL or something? I'm guessing you'd replace this RAM after all the data has been trashed, because you'd have no way to know the RAM is faulty until then (and because your OS isn't smart enough to simply avoid using areas that the BIOS says is faulty).

Mostly, people who end up probing RAM (without an extremely good reason, like only probing RAM after every other possible alternative has already been tried) are people who didn't know what they were doing and just wanted to switch to protected mode, and haven't learned much since...


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Re: Detecting amount of memory

Post by ~ »

I am sure that I wouldn't alter the RAM contents in that way after an OS were really active. I would do it in the second stage bootloader.

So, I better write one more snippet (could be a good way of having that code evaluated and the memory detection issue solved once and for all), to implement a reliable memory testing routine, which I already did once using the information found at http://www.uruk.org/orig-grub/mem64mb.html about INT 15h, AX=E820h. I remember calling that interrupt in a loop, just like this (part of this code may malfunction by being taken apart here and being outdated for its original purpose, but the calling idea remains):

Code: Select all

 ;;INIT: Gather BIOS memory map
 ;;INIT: Gather BIOS memory map
 ;;INIT: Gather BIOS memory map
;jmp .lpend
  pusha
  push ds
  push es

   mov eax,0x700
   mov dword[eax],0       ;Size of structure

  xor edi,edi

   mov ebx,0    ;Continuation
   .lp:
    mov eax,0xE820    ;Service
    mov cx,0x0        ;16-bit Segment
    mov es,cx         ;16-bit Segment
    mov di,0x500      ;16-bit Offset
    mov ecx,20        ;Expected size of each descriptor part is 20 bytes
    mov edx,'PAMS'    ;Signature to acknowledge we want this service
     int 0x15          ;Execute Interrupt!
      jc .lpend        ;If carry, end

      cmp eax,'PAMS'   ;If not signature present, end
      jnz .lpend       ;If not signature present, end

      cmp ecx,20      ;Must be at least 20 bytes, or else end by error
      jb .lpend       ;Must be at least 20 bytes, or else end by error

;      cmp ecx,20      ;If bigger than the size of struct, end by error
;      ja .lpend       ;If bigger than the size of struct, end by error


      add edi,ecx  ;Advance pointer
;      cmp di,0     ;See if the address resetted
;      jnz .norR    ;If not, continue normally
       jnc .norR
         push ax
         ;;
         ;;;
          mov ax,es      ;If not... take old pointer
          inc ax         ;Increment it to advance segments
          mov es,ax      ;And store it again
         pop ax
      .norR:

     push eax
      mov eax,0x700
      add dword[eax],ecx     ;Register an increment in the map size 
     pop eax


   cmp ebx,0     ;Repeat while EBX!=0
   jne .lp

  .lpend:


  pop es
  pop ds
  popa

 ;;END:  Gather BIOS memory map
 ;;END:  Gather BIOS memory map
 ;;END:  Gather BIOS memory map
When I think on a reliable way of putting this memory map in a location above the first Megabyte used for Real Mode, I will, so it don't matter its size in case the memory is very "fragmented" in a system and the map could overwrite something.

If I remember well, the only thing it returned in my newer system was a free region from 0x100000 to the mark of 448Mb. But it returned only one entry, actually simpler than expected in this PC.

It's just that if one of the old PC's I own doesn't support that BIOS call (it would mean others anywhere wouldn't as well), then, what if a system doesn't support the E820h call and it would only provide the INT 15h, AH=88h for 64Mb or 16Mb, or just supports the INT 15h, AX=E801h, which are not optimal, and that system had, say 128Mb or more?

In this case, one is at risk of having to use the older and more limited call, avoiding any direct writing to anything for detection purposes.

After that, the best thing that occurs to me is to try to scan for PCI devices (and ISA) manually, and then try to do a software memory test taking into account the reserved areas without touching it and restoring the altered memory used for testing just before advancing to the next Megabyte. Unfortunately, I have also seen Windows XP fail when the memory modules were poorly connected and suddenly failing contact to pins due to dust, etc., as well as the graphics screen presented by BIOS before booting, partially trashed for the same reason, so I could figure out it could happen some time, being a practically impossible event to detect at that moment.

I attach here a part of the log I keep for my OS development, for the ones that may be interested, as well as several posts in this forum and other web links. Note however that it is just a bunch of my general descriptions, and discussions that were done here about software memory probing, using BIOS calls, the SMBus, etc.

So, let's see how to write a failsafe memory test module, I'd really like to solve this issue an put it in clear words and code so it can becomes one of the simplest issues to do when somebody else asks.
Attachments
amount0.txt
Just some brainstorm and posts around the Internet.
(8.25 KiB) Downloaded 85 times
Post Reply