Rev 1505 | Rev 1683 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
750 | victor | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
983 | diamond | 3 | ;; Copyright (C) KolibriOS team 2004-2008. All rights reserved. ;; |
750 | victor | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
593 | mikedld | 7 | |
465 | serge | 8 | $Revision: 1507 $ |
9 | |||
593 | mikedld | 10 | |
465 | serge | 11 | MEM_WB equ 6 ;write-back memory |
12 | MEM_WC equ 1 ;write combined memory |
||
13 | MEM_UC equ 0 ;uncached memory |
||
14 | |||
15 | |||
16 | align 4 |
||
17 | proc init_mem |
||
1103 | diamond | 18 | ; calculate maximum allocatable address and number of allocatable pages |
19 | mov edi, BOOT_VAR-OS_BASE + 0x9104 |
||
20 | mov ecx, [edi-4] |
||
21 | xor esi, esi ; esi will hold total amount of memory |
||
22 | xor edx, edx ; edx will hold maximum allocatable address |
||
23 | .calcmax: |
||
24 | ; round all to pages |
||
25 | mov eax, [edi] |
||
26 | test eax, 0xFFF |
||
27 | jz @f |
||
28 | neg eax |
||
29 | and eax, 0xFFF |
||
30 | add [edi], eax |
||
31 | adc dword [edi+4], 0 |
||
32 | sub [edi+8], eax |
||
33 | sbb dword [edi+12], 0 |
||
34 | jc .unusable |
||
35 | @@: |
||
36 | and dword [edi+8], not 0xFFF |
||
37 | jz .unusable |
||
38 | ; ignore memory after 4 Gb |
||
39 | cmp dword [edi+4], 0 |
||
40 | jnz .unusable |
||
41 | mov eax, [edi] |
||
42 | cmp dword [edi+12], 0 |
||
43 | jnz .overflow |
||
44 | add eax, [edi+8] |
||
45 | jnc @f |
||
46 | .overflow: |
||
47 | mov eax, 0xFFFFF000 |
||
48 | @@: |
||
49 | cmp edx, eax |
||
50 | jae @f |
||
51 | mov edx, eax |
||
52 | @@: |
||
53 | sub eax, [edi] |
||
54 | mov [edi+8], eax |
||
55 | add esi, eax |
||
56 | jmp .usable |
||
57 | .unusable: |
||
58 | and dword [edi+8], 0 |
||
59 | .usable: |
||
60 | add edi, 20 |
||
61 | loop .calcmax |
||
62 | .calculated: |
||
63 | mov [MEM_AMOUNT-OS_BASE], esi |
||
64 | mov [pg_data.mem_amount-OS_BASE], esi |
||
65 | shr esi, 12 |
||
66 | mov [pg_data.pages_count-OS_BASE], esi |
||
465 | serge | 67 | |
1103 | diamond | 68 | shr edx, 12 |
69 | add edx, 31 |
||
70 | and edx, not 31 |
||
71 | shr edx, 3 |
||
72 | mov [pg_data.pagemap_size-OS_BASE], edx |
||
465 | serge | 73 | |
1103 | diamond | 74 | add edx, (sys_pgmap-OS_BASE)+4095 |
75 | and edx, not 4095 |
||
76 | mov [tmp_page_tabs], edx |
||
513 | serge | 77 | |
1130 | diamond | 78 | mov edx, esi |
1131 | diamond | 79 | and edx, -1024 |
1130 | diamond | 80 | cmp edx, (OS_BASE/4096) |
81 | jbe @F |
||
82 | mov edx, (OS_BASE/4096) |
||
83 | jmp .set |
||
84 | @@: |
||
1332 | diamond | 85 | cmp edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096 |
1130 | diamond | 86 | jae .set |
1332 | diamond | 87 | mov edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096 |
1130 | diamond | 88 | .set: |
465 | serge | 89 | mov [pg_data.kernel_pages-OS_BASE], edx |
90 | shr edx, 10 |
||
91 | mov [pg_data.kernel_tables-OS_BASE], edx |
||
92 | |||
93 | xor eax, eax |
||
94 | mov edi, sys_pgdir-OS_BASE |
||
95 | mov ecx, 4096/4 |
||
96 | cld |
||
97 | rep stosd |
||
98 | |||
99 | mov edx, (sys_pgdir-OS_BASE)+ 0x800; (OS_BASE shr 20) |
||
100 | |||
101 | mov ebx, cr4 |
||
102 | or ebx, CR4_PSE |
||
103 | mov eax, PG_LARGE+PG_SW |
||
104 | mov cr4, ebx |
||
513 | serge | 105 | dec [pg_data.kernel_tables-OS_BASE] |
465 | serge | 106 | |
107 | mov [edx], eax |
||
108 | add edx, 4 |
||
109 | |||
1451 | art_zh | 110 | mov edi, [tmp_page_tabs] |
465 | serge | 111 | mov ecx, [pg_data.kernel_tables-OS_BASE] |
112 | shl ecx, 10 |
||
113 | xor eax, eax |
||
114 | rep stosd |
||
115 | |||
116 | mov ecx, [pg_data.kernel_tables-OS_BASE] |
||
513 | serge | 117 | mov eax, [tmp_page_tabs] |
1451 | art_zh | 118 | or eax, PG_SW |
465 | serge | 119 | mov edi, edx |
1455 | art_zh | 120 | |
465 | serge | 121 | .map_kernel_tabs: |
122 | stosd |
||
123 | add eax, 0x1000 |
||
124 | dec ecx |
||
125 | jnz .map_kernel_tabs |
||
126 | |||
127 | mov dword [sys_pgdir-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE |
||
128 | |||
129 | mov edi, (sys_pgdir-OS_BASE) |
||
130 | lea esi, [edi+(OS_BASE shr 20)] |
||
519 | serge | 131 | movsd |
132 | movsd |
||
465 | serge | 133 | ret |
134 | endp |
||
135 | |||
136 | align 4 |
||
137 | proc init_page_map |
||
1103 | diamond | 138 | ; mark all memory as unavailable |
465 | serge | 139 | mov edi, sys_pgmap-OS_BASE |
140 | mov ecx, [pg_data.pagemap_size-OS_BASE] |
||
141 | shr ecx, 2 |
||
1103 | diamond | 142 | xor eax, eax |
513 | serge | 143 | cld |
465 | serge | 144 | rep stosd |
145 | |||
1103 | diamond | 146 | ; scan through memory map and mark free areas as available |
147 | mov ebx, BOOT_VAR-OS_BASE + 0x9104 |
||
148 | mov edx, [ebx-4] |
||
149 | .scanmap: |
||
150 | mov ecx, [ebx+8] |
||
151 | shr ecx, 12 ; ecx = number of pages |
||
152 | jz .next |
||
153 | mov edi, [ebx] |
||
154 | shr edi, 12 ; edi = first page |
||
155 | mov eax, edi |
||
156 | shr edi, 5 |
||
1331 | diamond | 157 | shl edi, 2 |
1103 | diamond | 158 | add edi, sys_pgmap-OS_BASE |
159 | and eax, 31 |
||
160 | jz .startok |
||
1331 | diamond | 161 | add ecx, eax |
162 | sub ecx, 32 |
||
1103 | diamond | 163 | jbe .onedword |
164 | push ecx |
||
165 | mov ecx, eax |
||
1331 | diamond | 166 | or eax, -1 |
1103 | diamond | 167 | shl eax, cl |
168 | or [edi], eax |
||
169 | add edi, 4 |
||
170 | pop ecx |
||
171 | .startok: |
||
172 | push ecx |
||
173 | shr ecx, 5 |
||
174 | or eax, -1 |
||
175 | rep stosd |
||
176 | pop ecx |
||
177 | and ecx, 31 |
||
1331 | diamond | 178 | neg eax |
1103 | diamond | 179 | shl eax, cl |
1331 | diamond | 180 | dec eax |
1103 | diamond | 181 | or [edi], eax |
182 | jmp .next |
||
183 | .onedword: |
||
1331 | diamond | 184 | add ecx, 32 |
185 | sub ecx, eax |
||
1103 | diamond | 186 | @@: |
187 | bts [edi], eax |
||
1331 | diamond | 188 | inc eax |
1103 | diamond | 189 | loop @b |
190 | .next: |
||
191 | add ebx, 20 |
||
192 | dec edx |
||
193 | jnz .scanmap |
||
194 | |||
195 | ; mark kernel memory as allocated (unavailable) |
||
1455 | art_zh | 196 | mov ecx, [tmp_page_tabs] |
465 | serge | 197 | mov edx, [pg_data.pages_count-OS_BASE] |
1455 | art_zh | 198 | shr ecx, 12 |
199 | add ecx, [pg_data.kernel_tables-OS_BASE] |
||
465 | serge | 200 | sub edx, ecx |
201 | mov [pg_data.pages_free-OS_BASE], edx |
||
202 | |||
513 | serge | 203 | mov edi, sys_pgmap-OS_BASE |
465 | serge | 204 | mov ebx, ecx |
205 | shr ecx, 5 |
||
513 | serge | 206 | xor eax, eax |
465 | serge | 207 | rep stosd |
208 | |||
209 | not eax |
||
210 | mov ecx, ebx |
||
211 | and ecx, 31 |
||
212 | shl eax, cl |
||
1103 | diamond | 213 | and [edi], eax |
465 | serge | 214 | add edi, OS_BASE |
215 | mov [page_start-OS_BASE], edi; |
||
216 | |||
217 | mov ebx, sys_pgmap |
||
218 | add ebx, [pg_data.pagemap_size-OS_BASE] |
||
219 | mov [page_end-OS_BASE], ebx |
||
220 | |||
221 | mov [pg_data.pg_mutex-OS_BASE], 0 |
||
222 | ret |
||
223 | endp |
||
224 | |||
225 | align 4 |
||
586 | serge | 226 | |
227 | init_BIOS32: |
||
228 | mov edi, 0xE0000 |
||
229 | .pcibios_nxt: |
||
230 | cmp dword[edi], '_32_' ; "magic" word |
||
231 | je .BIOS32_found |
||
232 | .pcibios_nxt2: |
||
233 | add edi, 0x10 |
||
234 | cmp edi, 0xFFFF0 |
||
235 | je .BIOS32_not_found |
||
236 | jmp .pcibios_nxt |
||
237 | .BIOS32_found: ; magic word found, check control summ |
||
238 | |||
239 | movzx ecx, byte[edi + 9] |
||
240 | shl ecx, 4 |
||
241 | mov esi, edi |
||
242 | xor eax, eax |
||
243 | cld ; paranoia |
||
244 | @@: lodsb |
||
245 | add ah, al |
||
246 | loop @b |
||
247 | jnz .pcibios_nxt2 ; control summ must be zero |
||
248 | ; BIOS32 service found ! |
||
249 | mov ebp, [edi + 4] |
||
250 | mov [bios32_entry], ebp |
||
251 | ; check PCI BIOS present |
||
252 | mov eax, '$PCI' |
||
253 | xor ebx, ebx |
||
254 | push cs ; special for 'ret far' from BIOS |
||
255 | call ebp |
||
256 | test al, al |
||
257 | jnz .PCI_BIOS32_not_found |
||
258 | |||
259 | ; здесь создаются дискрипторы для PCI BIOS |
||
260 | |||
261 | add ebx, OS_BASE |
||
262 | dec ecx |
||
263 | mov [(pci_code_32-OS_BASE)], cx ;limit 0-15 |
||
264 | mov [(pci_data_32-OS_BASE)], cx ;limit 0-15 |
||
265 | |||
266 | mov [(pci_code_32-OS_BASE)+2], bx ;base 0-15 |
||
267 | mov [(pci_data_32-OS_BASE)+2], bx ;base 0-15 |
||
268 | |||
269 | shr ebx, 16 |
||
270 | mov [(pci_code_32-OS_BASE)+4], bl ;base 16-23 |
||
271 | mov [(pci_data_32-OS_BASE)+4], bl ;base 16-23 |
||
272 | |||
273 | shr ecx, 16 |
||
274 | and cl, 0x0F |
||
275 | mov ch, bh |
||
276 | add cx, D32 |
||
277 | mov [(pci_code_32-OS_BASE)+6], cx ;lim 16-19 & |
||
278 | mov [(pci_data_32-OS_BASE)+6], cx ;base 24-31 |
||
279 | |||
280 | mov [(pci_bios_entry-OS_BASE)], edx |
||
281 | ; jmp .end |
||
282 | .PCI_BIOS32_not_found: |
||
283 | ; здесь должна заполнятся pci_emu_dat |
||
284 | .BIOS32_not_found: |
||
285 | .end: |
||
756 | Ghost | 286 | ret |
586 | serge | 287 | |
288 | align 4 |
||
465 | serge | 289 | proc test_cpu |
290 | locals |
||
291 | cpu_type dd ? |
||
292 | cpu_id dd ? |
||
293 | cpu_Intel dd ? |
||
294 | cpu_AMD dd ? |
||
295 | endl |
||
296 | |||
297 | xor eax, eax |
||
942 | Lrz | 298 | mov [cpu_type], eax |
465 | serge | 299 | mov [cpu_caps-OS_BASE], eax |
300 | mov [cpu_caps+4-OS_BASE], eax |
||
301 | |||
302 | pushfd |
||
303 | pop eax |
||
304 | mov ecx, eax |
||
305 | xor eax, 0x40000 |
||
306 | push eax |
||
307 | popfd |
||
308 | pushfd |
||
309 | pop eax |
||
310 | xor eax, ecx |
||
311 | mov [cpu_type], CPU_386 |
||
312 | jz .end_cpuid |
||
313 | push ecx |
||
314 | popfd |
||
315 | |||
316 | mov [cpu_type], CPU_486 |
||
317 | mov eax, ecx |
||
318 | xor eax, 0x200000 |
||
319 | push eax |
||
320 | popfd |
||
321 | pushfd |
||
322 | pop eax |
||
323 | xor eax, ecx |
||
324 | je .end_cpuid |
||
325 | mov [cpu_id], 1 |
||
326 | |||
327 | xor eax, eax |
||
328 | cpuid |
||
329 | |||
330 | mov [cpu_vendor-OS_BASE], ebx |
||
331 | mov [cpu_vendor+4-OS_BASE], edx |
||
332 | mov [cpu_vendor+8-OS_BASE], ecx |
||
333 | cmp ebx, dword [intel_str-OS_BASE] |
||
334 | jne .check_AMD |
||
335 | cmp edx, dword [intel_str+4-OS_BASE] |
||
336 | jne .check_AMD |
||
337 | cmp ecx, dword [intel_str+8-OS_BASE] |
||
338 | jne .check_AMD |
||
339 | mov [cpu_Intel], 1 |
||
340 | cmp eax, 1 |
||
341 | jl .end_cpuid |
||
342 | mov eax, 1 |
||
343 | cpuid |
||
344 | mov [cpu_sign-OS_BASE], eax |
||
345 | mov [cpu_info-OS_BASE], ebx |
||
346 | mov [cpu_caps-OS_BASE], edx |
||
347 | mov [cpu_caps+4-OS_BASE],ecx |
||
348 | |||
349 | shr eax, 8 |
||
350 | and eax, 0x0f |
||
351 | ret |
||
352 | .end_cpuid: |
||
353 | mov eax, [cpu_type] |
||
354 | ret |
||
355 | |||
356 | .check_AMD: |
||
357 | cmp ebx, dword [AMD_str-OS_BASE] |
||
358 | jne .unknown |
||
359 | cmp edx, dword [AMD_str+4-OS_BASE] |
||
360 | jne .unknown |
||
361 | cmp ecx, dword [AMD_str+8-OS_BASE] |
||
362 | jne .unknown |
||
363 | mov [cpu_AMD], 1 |
||
364 | cmp eax, 1 |
||
365 | jl .unknown |
||
366 | mov eax, 1 |
||
367 | cpuid |
||
368 | mov [cpu_sign-OS_BASE], eax |
||
369 | mov [cpu_info-OS_BASE], ebx |
||
370 | mov [cpu_caps-OS_BASE], edx |
||
371 | mov [cpu_caps+4-OS_BASE],ecx |
||
372 | shr eax, 8 |
||
373 | and eax, 0x0f |
||
374 | ret |
||
375 | .unknown: |
||
376 | mov eax, 1 |
||
377 | cpuid |
||
378 | mov [cpu_sign-OS_BASE], eax |
||
379 | mov [cpu_info-OS_BASE], ebx |
||
380 | mov [cpu_caps-OS_BASE], edx |
||
381 | mov [cpu_caps+4-OS_BASE],ecx |
||
382 | shr eax, 8 |
||
383 | and eax, 0x0f |
||
384 | ret |
||
385 | endp |
||
386 |