Move mboot documentation to the doc/ directory
[profile/ivi/syslinux.git] / highmem.inc
1 ;; -----------------------------------------------------------------------
2 ;;
3 ;;   Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
4 ;;
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.
10 ;;
11 ;; -----------------------------------------------------------------------
12
13 ;;
14 ;; highmem.inc
15 ;;
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.
18 ;;
19
20                 section .text
21
22 ;
23 ; This is set up as a subroutine; it will set up the global variable
24 ; HighMemSize.  All registers are preserved.
25 ;
26 highmemsize:
27                 push es
28                 pushad
29
30                 push cs
31                 pop es
32
33 ;
34 ; First, try INT 15:E820 (get BIOS memory map)
35 ;
36 ; Note: we may have to scan this multiple times, because some (daft) BIOSes
37 ; report main memory as multiple contiguous ranges...
38 ;
39 get_e820:
40                 mov dword [E820Max],-(1 << 20)  ; Max amount of high memory
41                 mov dword [E820Mem],(1 << 20)   ; End of detected high memory
42 .start_over:
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
49                 xor ecx,ecx
50                 mov cl,20                       ; ECX <- 20
51                 mov di,E820Buf
52                 int 15h
53                 jnc .no_carry
54                 ; If carry, ebx == 0 means error, ebx != 0 means we're done
55                 and ebx,ebx
56                 jnz .e820_done
57                 jmp no_e820
58 .no_carry:
59                 cmp eax,534D4150h
60                 jne no_e820
61 ;
62 ; Look for a memory block starting at <= 1 MB and continuing upward
63 ;
64                 cmp dword [E820Buf+4], byte 0
65                 ja .int_loop                    ; Start >= 4 GB?
66                 mov eax, [E820Buf]
67                 cmp dword [E820Buf+16],1
68                 je .is_ram                      ; Is it memory?
69                 ;
70                 ; Non-memory range.  Remember this as a limit; some BIOSes get the length
71                 ; of primary RAM incorrect!
72                 ;
73                 cmp eax, (1 << 20)
74                 jb .int_loop                    ; Starts in lowmem region
75                 cmp eax,[E820Max]
76                 jae .int_loop                   ; Already above limit
77                 mov [E820Max],eax               ; Set limit
78                 jmp .int_loop
79
80 .is_ram:
81                 cmp eax,[E820Mem]
82                 ja .int_loop                    ; Not contiguous with our starting point
83                 add eax,[E820Buf+8]
84                 jc .overflow
85                 cmp dword [E820Buf+12],0
86                 je .nooverflow
87 .overflow:
88                 or eax,-1
89 .nooverflow:
90                 cmp eax,[E820Mem]
91                 jbe .int_loop                   ; All is below our baseline
92                 mov [E820Mem],eax
93                 jmp .start_over                 ; Start over in case we find an adjacent range
94
95 .e820_done:
96                 mov eax,[E820Mem]
97                 cmp eax,[E820Max]
98                 jna .not_limited
99                 mov eax,[E820Max]
100 .not_limited:
101                 cmp eax,(1 << 20)
102                 ja got_highmem                  ; Did we actually find memory?
103                 ; otherwise fall through
104
105 ;
106 ; INT 15:E820 failed.  Try INT 15:E801.
107 ;
108 no_e820:
109                 mov ax,0e801h                   ; Query high memory (semi-recent)
110                 int 15h
111                 jc no_e801
112                 cmp ax,3c00h
113                 ja no_e801                      ; > 3C00h something's wrong with this call
114                 jb e801_hole                    ; If memory hole we can only use low part
115
116                 mov ax,bx
117                 shl eax,16                      ; 64K chunks
118                 add eax,(16 << 20)              ; Add first 16M
119                 jmp short got_highmem
120
121 ;
122 ; INT 15:E801 failed.  Try INT 15:88.
123 ;
124 no_e801:
125                 mov ah,88h                      ; Query high memory (oldest)
126                 int 15h
127                 cmp ax,14*1024                  ; Don't trust memory >15M
128                 jna e801_hole
129                 mov ax,14*1024
130 e801_hole:
131                 and eax,0ffffh
132                 shl eax,10                      ; Convert from kilobytes
133                 add eax,(1 << 20)               ; First megabyte
134 got_highmem:
135 %if HIGHMEM_SLOP != 0
136                 sub eax,HIGHMEM_SLOP
137 %endif
138                 mov [HighMemSize],eax
139                 popad
140                 pop es
141                 ret                             ; Done!
142
143                 section .bss
144                 alignb 4
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)