Improved memory management
[reactos.git] / freeldr / freeldr / arch / i386 / mem.S
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
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; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 .text
21 .code16
22
23 #define ASM
24 #include "arch.h"
25 #include "multiboot.h"
26
27
28
29 .code32
30 EXTERN(_GetExtendedMemorySize)
31
32 //
33 // get extended memory size in KB
34 //
35 pushl %edx
36 pushl %ecx
37 pushl %ebx
38
39 call switch_to_real
40 .code16
41
42 movw $0xe801,%ax
43 int $0x15
44 jc .oldstylemem
45
46 cmpw $0,%ax
47 je .cmem
48
49 movzwl %bx,%ebx
50 shll $6,%ebx
51 movzwl %ax,%eax
52 addl %ebx,%eax
53 jmp .done_mem
54
55 .cmem:
56 cmpw $0,%cx
57 je .oldstylemem
58
59 movzwl %dx,%edx
60 shll $6,%edx
61 movzwl %cx,%ecx
62 addl %ecx,%edx
63 movl %edx,%eax
64 jmp .done_mem
65
66 .oldstylemem:
67 // int 15h opt e801 don't work , try int 15h, option 88h
68 movb $0x88,%ah
69 int $0x15
70 cmp $0,%ax
71 je .cmosmem
72 movzwl %ax,%eax
73 jmp .done_mem
74
75 .cmosmem:
76 // int 15h opt 88h don't work , try read cmos
77 xorl %eax,%eax
78 movb $0x31,%al
79 outb %al,$0x70
80 inb $0x71,%al
81 andl $0xffff,%eax // clear carry
82 shll $8,%eax
83
84 .done_mem:
85
86
87 /* Save return value */
88 movl %eax,%edx
89
90 call switch_to_prot
91
92 .code32
93
94 /* Restore return value */
95 movl %edx,%eax
96
97 popl %ebx
98 popl %ecx
99 popl %edx
100
101 ret
102
103
104
105 .code32
106 EXTERN(_GetConventionalMemorySize)
107
108 //
109 // Get conventional memory size in KB
110 //
111 pushl %edx
112
113 call switch_to_real
114 .code16
115
116 xorl %eax,%eax
117 int $0x12
118
119 /*xorl %eax,%eax
120 movb $0x30,%al
121 outb %al,$0x70
122 inb $0x71,%al
123 andl $0xffff,%eax*/ // clear carry
124
125 /* Save return value */
126 movzwl %ax,%edx
127
128 call switch_to_prot
129
130 .code32
131
132 /* Restore return value */
133 movl %edx,%eax
134
135 popl %edx
136
137 ret
138
139
140
141 .code32
142 _gbmm_mem_map_length:
143 .long 0
144 _gbmm_memory_map_addr:
145 .long 0
146 EXTERN(_GetBiosMemoryMap)
147 //
148 // Retrieve BIOS memory map if available
149 //
150 pushl %edx
151 pushl %ecx
152 pushl %ebx
153 pushl %edi
154
155 movl $0,_gbmm_mem_map_length
156
157 /* Get memory map address off stack */
158 movl 0x10(%esp),%eax
159 movl %eax,_gbmm_memory_map_addr
160
161 call switch_to_real
162 .code16
163
164 xorl %ebx,%ebx
165 movl _gbmm_memory_map_addr,%edi
166
167 .mmap_next:
168
169 movl $0x534D4150,%edx // 'SMAP'
170 movl $/*sizeof(memory_map_t)*/24,%ecx
171 movl 0xE820,%eax
172 int $0x15
173 jc .done_mmap
174
175 cmpl $0x534D4150,%eax // 'SMAP'
176 jne .done_mmap
177
178 addl $/*sizeof(memory_map_t)*/24,%edi
179 addl $/*sizeof(memory_map_t)*/24,_gbmm_mem_map_length
180
181 cmpl $0,%ebx
182 jne .mmap_next
183
184 .done_mmap:
185
186 call switch_to_prot
187
188 .code32
189
190 /* Get return value */
191 movl _gbmm_mem_map_length,%eax
192
193 popl %edi
194 popl %ebx
195 popl %ecx
196 popl %edx
197
198 ret