1 ;; -----------------------------------------------------------------------
3 ;; Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 ;; Boston MA 02111-1307, USA; either version 2 of the License, or
9 ;; (at your option) any later version; incorporated herein by reference.
11 ;; -----------------------------------------------------------------------
16 ;; Probe for the size of high memory. This can be overridden by a
17 ;; mem= command on the command line while booting a new kernel.
23 ; This is set up as a subroutine; it will set up the global variable
24 ; HighMemSize. All registers are preserved.
34 ; First, try INT 15:E820 (get BIOS memory map)
36 ; Note: we may have to scan this multiple times, because some (daft) BIOSes
37 ; report main memory as multiple contiguous ranges...
40 mov dword [E820Max],-(1 << 20) ; Max amount of high memory
41 mov dword [E820Mem],(1 << 20) ; End of detected high memory
43 xor ebx,ebx ; Start with first record
44 jmp short .do_e820 ; Skip "at end" check first time!
45 .int_loop: and ebx,ebx ; If we're back at beginning...
46 jz .e820_done ; ... we're done
47 .do_e820: mov eax,0000E820h
48 mov edx,534D4150h ; "SMAP" backwards
54 ; If carry, ebx == 0 means error, ebx != 0 means we're done
62 ; Look for a memory block starting at <= 1 MB and continuing upward
64 cmp dword [E820Buf+4], byte 0
65 ja .int_loop ; Start >= 4 GB?
67 cmp dword [E820Buf+16],1
68 je .is_ram ; Is it memory?
70 ; Non-memory range. Remember this as a limit; some BIOSes get the length
71 ; of primary RAM incorrect!
74 jb .int_loop ; Starts in lowmem region
76 jae .int_loop ; Already above limit
77 mov [E820Max],eax ; Set limit
82 ja .int_loop ; Not contiguous with our starting point
85 cmp dword [E820Buf+12],0
91 jbe .int_loop ; All is below our baseline
93 jmp .start_over ; Start over in case we find an adjacent range
102 ja got_highmem ; Did we actually find memory?
103 ; otherwise fall through
106 ; INT 15:E820 failed. Try INT 15:E801.
109 mov ax,0e801h ; Query high memory (semi-recent)
113 ja no_e801 ; > 3C00h something's wrong with this call
114 jb e801_hole ; If memory hole we can only use low part
117 shl eax,16 ; 64K chunks
118 add eax,(16 << 20) ; Add first 16M
119 jmp short got_highmem
122 ; INT 15:E801 failed. Try INT 15:88.
125 mov ah,88h ; Query high memory (oldest)
127 cmp ax,14*1024 ; Don't trust memory >15M
132 shl eax,10 ; Convert from kilobytes
133 add eax,(1 << 20) ; First megabyte
135 %if HIGHMEM_SLOP != 0
138 mov [HighMemSize],eax
145 E820Buf resd 5 ; INT 15:E820 data buffer
146 E820Mem resd 1 ; Memory detected by E820
147 E820Max resd 1 ; Is E820 memory capped?
148 HighMemSize resd 1 ; End of memory pointer (bytes)