Memory Detecting Routine

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
User avatar
d4n1l0d
Member
Member
Posts: 42
Joined: Sat Dec 02, 2006 4:12 pm

Memory Detecting Routine

Post 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 ? )
User avatar
nicesj
Member
Member
Posts: 27
Joined: Fri May 06, 2005 11:00 pm
Location: South Korea
Contact:

This is my code for detecting memory size.

Post 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
; }}}
http://nicesj.com
With the software, What You Think Is What You Get.(WYTIWYG)
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post 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
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
nicesj
Member
Member
Posts: 27
Joined: Fri May 06, 2005 11:00 pm
Location: South Korea
Contact:

Post 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...
User avatar
d4n1l0d
Member
Member
Posts: 42
Joined: Sat Dec 02, 2006 4:12 pm

Post by d4n1l0d »

Ok ....
I'll get the ram size from BIOS and save it somewhere.
Thanks!!!
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
d4n1l0d
Member
Member
Posts: 42
Joined: Sat Dec 02, 2006 4:12 pm

Post by d4n1l0d »

lol
Ok, I'll take a look at that page
thx
Post Reply