2 ;; -----------------------------------------------------------------------
4 ;; Copyright 1994-2004 H. Peter Anvin - All Rights Reserved
6 ;; This program is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 ;; Boston MA 02111-1307, USA; either version 2 of the License, or
10 ;; (at your option) any later version; incorporated herein by reference.
12 ;; -----------------------------------------------------------------------
17 ;; Probe for the size of high memory. This can be overridden by a
18 ;; mem= command on the command line while booting a new kernel.
24 ; This is set up as a subroutine; it will set up the global variable
25 ; HighMemSize. All registers are preserved. Assumes DS == CS.
32 ; First, try INT 15:E820 (get BIOS memory map)
35 xor ebx,ebx ; Start with first record
36 mov dword [E820Max],-(1 << 20) ; Max amount of high memory
37 mov dword [E820Mem],ebx ; Detected amount of high memory
38 mov es,bx ; Need ES = DS = 0 for now
39 jmp short .do_e820 ; Skip "at end" check first time!
40 .int_loop: and ebx,ebx ; If we're back at beginning...
41 jz .e820_done ; ... we're done
42 .do_e820: mov eax,0000E820h
43 mov edx,534D4150h ; "SMAP" backwards
49 ; If carry, ebx == 0 means error, ebx != 0 means we're done
57 ; Look for a memory block starting at <= 1 MB and continuing upward
59 cmp dword [E820Buf+4], byte 0
60 ja .int_loop ; Start >= 4 GB?
63 jnb .ram_range ; Start >= 1 MB?
64 ; If we get here, it starts > 1 MB but < 4 GB; if this is a
65 ; *non*-memory range, remember this as unusable; some BIOSes
66 ; get the length of primary RAM wrong!
67 cmp dword [E820Buf+16], byte 1
68 je .int_loop ; If it's memory, don't worry about it
69 neg edx ; This means what for memory limit?
70 cmp edx,[E820Max] ; Better or worse
77 sbb eax,eax ; eax <- 0xFFFFFFFF
78 cmp dword [E820Buf+12], byte 0
79 ja .huge ; Size >= 4 GB
81 .huge: sub eax, edx ; Adjust size to start at 1 MB
82 jbe .int_loop ; Completely below 1 MB?
84 ; Now EAX contains the size of memory 1 MB...up
85 cmp dword [E820Buf+16], byte 1
86 jne .int_loop ; High memory isn't usable memory!!!!
90 jmp .int_loop ; Still need to add low 1 MB
95 jz no_e820 ; Nothing found by E820?
96 cmp eax,[E820Max] ; Make sure we're not limited
97 jna got_highmem_add1mb
99 jmp got_highmem_add1mb
102 ; INT 15:E820 failed. Try INT 15:E801.
105 mov ax,0e801h ; Query high memory (semi-recent)
109 ja no_e801 ; > 3C00h something's wrong with this call
110 jb e801_hole ; If memory hole we can only use low part
113 shl eax,16 ; 64K chunks
114 add eax,(16 << 20) ; Add first 16M
115 jmp short got_highmem
118 ; INT 15:E801 failed. Try INT 15:88.
121 mov ah,88h ; Query high memory (oldest)
123 cmp ax,14*1024 ; Don't trust memory >15M
128 shl eax,10 ; Convert from kilobytes
130 add eax,(1 << 20) ; First megabyte
132 %if HIGHMEM_SLOP != 0
135 mov [HighMemSize],eax
142 E820Buf resd 5 ; INT 15:E820 data buffer
143 E820Mem resd 1 ; Memory detected by E820
144 E820Max resd 1 ; Is E820 memory capped?
145 HighMemSize resd 1 ; End of memory pointer (bytes)