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.