Page 1 of 1

Memory Detecting Routine

Posted: Sat Jul 07, 2007 8:57 pm
by d4n1l0d
How could it be?? I want to know the size of my RAM memory ( without using BIOS interrupts or GRUB ). Theorically, I need to write to an address and read from it. (OK). But I should handle fault when data is written into not present memory address. ( HOW ? )

This is my code for detecting memory size.

Posted: Sun Jul 08, 2007 6:29 am
by nicesj
E820H bios function will help you to get the size of memory.

Code: Select all

; {{{ Now, try to get the size of memory
E820H:
; NOTICE
; we assume that the maximum index of MRD structure is 128
; if the MRD entry count is bigger than 128, it can makes problem..
; but most system didn't uses more than 128.. I believe that...
; just believing...
; If this makes some problem. try fix it.
; and I will check this and tell it to the KERNEL, to handle it.. :)
        and BYTE [CPUTYPE], MEM_BIT_CLEAR
        or BYTE [CPUTYPE], E820_USED
        xor ebx, ebx
        mov [MRD_CNT], BYTE 0
        mov di, MRD
e_mem_info:
        mov eax, 0x0000E820     ; function
        mov edx, SMAP           ; signature
        mov ecx, 0x14           ; size of record

        push    ds
        pop     es                              ; set es to zero, di -> MemoryRangeDescriptor

        int 0x15                        ; interrupt
        jc E820H_error          ; no-carry = success

        cmp eax, SMAP
        jne E801H                       ; check signature

        cmp ecx, 0x14
        jne E801H                       ; check the size

        add di, 0x14            ; add 20 to DI

        cmp ebx, 0
        je mem_ok                       ; check EOF (ebx == 0)

        cmp BYTE [MRD_CNT], MRD_MAX_CNT
        jae E820H_over_cnt
        inc BYTE [MRD_CNT]

        jmp e_mem_info          ; LOOP

E820H_over_cnt:
        and BYTE [CPUTYPE], MEM_BIT_CLEAR
        or BYTE [CPUTYPE], E820_OVER
        jmp             mem_ok

E820H_error:
E801H:
        mov di, MRD
        and BYTE [CPUTYPE], MEM_BIT_CLEAR
        or BYTE [CPUTYPE], E801_USED
        stc
        xor     cx, cx
        xor     dx, dx
        mov ax, 0xe801
        int 0x15
        jc E801H_error

        cmp cx, 0
        jne E801_USE_CX_DX
        cmp dx, 0
        jne E801_USE_CX_DX
        mov ax, cx
        mov bx, dx

E801_USE_CX_DX:
        and edx, 0xffff
        shl edx, 6
        mov DWORD [di], edx
        and ecx, 0xffff
        add DWORD [di], ecx
        add di, 4
        jmp mem_ok

E801H_error:
E88H:           ; real value of function is '88h'
        mov di, MRD
        and BYTE [CPUTYPE], MEM_BIT_CLEAR
        or BYTE [CPUTYPE], E88_USED
        mov ah, 0x88
        int 0x15
        mov WORD [di], ax
        add di, 2
mem_ok:
        int 0x12
        mov WORD [di], ax
; }}}

Posted: Sun Jul 08, 2007 7:32 am
by inflater
@sjpark: He doesn't want BIOS.

In that case you need to get memory from CMOS and that works clearly only with RAM < 64 MB. If I would be you, I get that value before switching to protected mode through BIOS like sjpark's example and store the value somewhere. Then, you switch to protected mode and you have the size of RAM in yer kernel ;)

BIOS is legacy and slow, but useful sometimes :)

inflater

Posted: Sun Jul 08, 2007 8:10 am
by nicesj
inflater wrote:@sjpark: He doesn't want BIOS.

In that case you need to get memory from CMOS and that works clearly only with RAM < 64 MB. If I would be you, I get that value before switching to protected mode through BIOS like sjpark's example and store the value somewhere. Then, you switch to protected mode and you have the size of RAM in yer kernel ;)

BIOS is legacy and slow, but useful sometimes :)

inflater
ah, sorry :) I forgot what he said...

Posted: Sun Jul 08, 2007 1:06 pm
by d4n1l0d
Ok ....
I'll get the ram size from BIOS and save it somewhere.
Thanks!!!

Posted: Sun Jul 08, 2007 3:23 pm
by Combuster
Well, asking the bios or grub are the common (and the only politically correct) approaches. You should really use one of those. If you so want, you can try to use v8086 mode and call the bios from protected mode.

If you are really desparate, there's a page in the wiki on (alternative) methods of detecting memory with the appropriate warnings :wink:

Posted: Sun Jul 08, 2007 6:10 pm
by d4n1l0d
lol
Ok, I'll take a look at that page
thx