Subversion Repositories Kolibri OS

Rev

Rev 1507 | Rev 1941 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1507 Rev 1683
Line 3... Line 3...
3
;; Copyright (C) KolibriOS team 2004-2008. All rights reserved. ;;
3
;; Copyright (C) KolibriOS team 2004-2008. All rights reserved. ;;
4
;; Distributed under terms of the GNU General Public License    ;;
4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 7... Line 7...
7
 
7
 
Line 8... Line 8...
8
$Revision: 1507 $
8
$Revision: 1683 $
9
 
9
 
10
 
10
 
Line -... Line 11...
-
 
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
preinit_mem:
-
 
18
 
-
 
19
; clear [0x280000..HEAP_BASE]
-
 
20
	   xor	 eax,eax
-
 
21
	   mov	 edi,0x280000			; 0x280000 = ramdisk FAT ?
-
 
22
	   mov	 ecx,(HEAP_BASE-OS_BASE-0x280000) / 4
-
 
23
	   cld
-
 
24
	   rep	 stosd
-
 
25
 
-
 
26
; clear [0x40000..0x90000]
-
 
27
	   mov	 edi,0x50000			; 0x50000 is somewhere inside kernel code?
-
 
28
	   mov	 ecx,(0x90000-0x50000)/4
-
 
29
	   rep	 stosd
-
 
30
 
-
 
31
; clear undefined kernel globals
-
 
32
	   mov	 edi, endofcode-OS_BASE
-
 
33
	   mov	 ecx, (uglobals_size/4)+4
-
 
34
	   rep	 stosd
-
 
35
 
-
 
36
; save [0..0xffff]
-
 
37
	   xor	 esi, esi
-
 
38
	   mov	 edi,0x2F0000			; low mem storage area
-
 
39
	   mov	 ecx, 0x10000 / 4
-
 
40
	   rep	 movsd
-
 
41
; clear [0x1000..0x0ffff]
-
 
42
	   mov	 edi,0x1000
-
 
43
	   mov	 ecx,0xf000 / 4
-
 
44
	   rep	 stosd
-
 
45
 
-
 
46
; clear  table
Line -... Line 47...
-
 
47
	   mov edi, sys_pgdir-OS_BASE
11
MEM_WB     equ 6               ;write-back memory
48
	   mov ecx, 4096/4
12
MEM_WC     equ 1               ;write combined memory
49
	   rep stosd
13
MEM_UC     equ 0               ;uncached memory
-
 
14
 
-
 
15
 
-
 
-
 
50
	ret
16
align 4
51
 
17
proc init_mem
52
; ======================================================================
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
53
align 4
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
54
proc init_mem
59
.usable:
55
 
60
           add edi, 20
56
	   mov esi, (PCIe_CONFIG_SPACE-OS_BASE) 	; esi will hold total amount of memory
61
           loop .calcmax
57
	   mov edx, esi 				; edx will hold maximum allocatable address
Line 62... Line 58...
62
.calculated:
58
 
63
           mov [MEM_AMOUNT-OS_BASE], esi
59
	   mov [MEM_AMOUNT-OS_BASE], esi
64
           mov [pg_data.mem_amount-OS_BASE], esi
60
	   mov [pg_data.mem_amount-OS_BASE], esi
65
           shr esi, 12
61
	   shr esi, 12
66
           mov [pg_data.pages_count-OS_BASE], esi
62
	   mov [pg_data.pages_count-OS_BASE], esi	; max number of PTEs   ?
Line 67... Line 63...
67
 
63
 
68
           shr edx, 12
64
	   shr edx, 12
69
           add edx, 31
65
	   add edx, 31
Line 70... Line -...
70
           and edx, not 31
-
 
71
           shr edx, 3
-
 
72
           mov [pg_data.pagemap_size-OS_BASE], edx
-
 
73
 
-
 
74
           add edx, (sys_pgmap-OS_BASE)+4095
-
 
75
           and edx, not 4095
-
 
76
           mov [tmp_page_tabs], edx
-
 
77
 
-
 
78
           mov edx, esi
-
 
79
           and edx, -1024
66
	   and edx, not 31
80
           cmp edx, (OS_BASE/4096)
-
 
81
           jbe @F
67
	   shr edx, 3
82
           mov edx, (OS_BASE/4096)
68
	   mov [pg_data.pagemap_size-OS_BASE], edx	; size of sys_pgmap structure
83
           jmp .set
69
 
Line 84... Line -...
84
@@:
-
 
85
           cmp edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
-
 
86
           jae .set
-
 
87
           mov edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
-
 
88
.set:
-
 
89
           mov [pg_data.kernel_pages-OS_BASE], edx
-
 
90
           shr edx, 10
70
	   add edx, (sys_pgmap-OS_BASE)+4095
Line 91... Line 71...
91
           mov [pg_data.kernel_tables-OS_BASE], edx
71
	   and edx, not 4095
92
 
72
	   mov [tmp_page_tabs], edx			; free zone to build PTEs
93
           xor eax, eax
73
 
94
           mov edi, sys_pgdir-OS_BASE
74
	   mov edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
95
           mov ecx, 4096/4
75
	   mov [pg_data.kernel_pages -OS_BASE], edx
Line 96... Line 76...
96
           cld
76
	   shr edx, 10
97
           rep stosd
77
	   mov [pg_data.kernel_tables-OS_BASE], edx
Line 98... Line 78...
98
 
78
 
99
           mov edx, (sys_pgdir-OS_BASE)+ 0x800; (OS_BASE shr 20)
79
	   mov edx, (sys_pgdir-OS_BASE)+ 0x800		; (0x800 = OS_BASE shr 20)
100
 
-
 
101
           mov ebx, cr4
80
 
102
           or ebx, CR4_PSE
81
	   mov ebx, cr4
Line 103... Line 82...
103
           mov eax, PG_LARGE+PG_SW
82
	   or  ebx, CR4_PSE
104
           mov cr4, ebx
83
	   mov eax, PG_LARGE+PG_SW
105
           dec [pg_data.kernel_tables-OS_BASE]
84
	   mov cr4, ebx
106
 
85
	   dec [pg_data.kernel_tables-OS_BASE]
Line 107... Line 86...
107
           mov [edx], eax
86
 
108
           add edx, 4
87
	   mov [edx], eax				; map first (physical) 4M bytes
109
 
88
	   add edx, 4
110
           mov edi, [tmp_page_tabs] 
89
 
111
           mov ecx, [pg_data.kernel_tables-OS_BASE]
90
	   mov edi, [tmp_page_tabs]
Line -... Line 91...
-
 
91
	   mov ecx, [pg_data.kernel_pages -OS_BASE]	; safety cleaning of already-zeroed space
112
           shl ecx, 10
92
	   xor eax, eax
Line 113... Line 93...
113
           xor eax, eax
93
	   rep stosd
114
           rep stosd
94
 
115
 
95
	   mov ecx, [pg_data.kernel_tables-OS_BASE]	; build some PDEs to hold empty PTEs
Line 284... Line 264...
284
.BIOS32_not_found:
264
.BIOS32_not_found:
285
.end:
265
.end:
286
           ret	
266
	   ret
Line 287... Line 267...
287
 
267
 
288
align 4
-
 
289
proc test_cpu
-
 
290
           locals
-
 
291
              cpu_type   dd ?
-
 
292
              cpu_id     dd ?
268
align 4
293
              cpu_Intel  dd ?
-
 
294
              cpu_AMD    dd ?
-
 
Line 295... Line 269...
295
           endl
269
test_cpu:	; only AMD machines supported
296
 
-
 
297
           xor eax, eax
270
 
298
           mov [cpu_type], eax
271
	   xor eax, eax
Line 299... Line 272...
299
           mov [cpu_caps-OS_BASE], eax
272
	   mov [cpu_caps-OS_BASE], eax
300
           mov [cpu_caps+4-OS_BASE], eax
273
	   mov [cpu_caps+4-OS_BASE], eax
Line 306... Line 279...
306
           push eax
279
	   push eax
307
           popfd
280
	   popfd
308
           pushfd
281
	   pushfd
309
           pop eax
282
	   pop eax
310
           xor eax, ecx
283
	   xor eax, ecx
311
           mov [cpu_type], CPU_386
-
 
312
           jz .end_cpuid
284
	   jz $ 		; 386
313
           push ecx
285
	   push ecx
314
           popfd
286
	   popfd
Line 315... Line -...
315
 
-
 
316
           mov [cpu_type], CPU_486
287
 
317
           mov eax, ecx
288
	   mov eax, ecx
318
           xor eax, 0x200000
289
	   xor eax, 0x200000
319
           push eax
290
	   push eax
320
           popfd
291
	   popfd
321
           pushfd
292
	   pushfd
322
           pop eax
293
	   pop eax
323
           xor eax, ecx
294
	   xor eax, ecx
324
           je .end_cpuid
-
 
Line 325... Line 295...
325
           mov [cpu_id], 1
295
	   je $ 		; 486
326
 
296
 
Line 327... Line 297...
327
           xor eax, eax
297
	   xor eax, eax
328
           cpuid
298
	   cpuid
329
 
299
 
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
-
 
Line 352... Line -...
352
.end_cpuid:
-
 
353
           mov eax, [cpu_type]
300
	   mov [cpu_vendor-OS_BASE],   ebx
354
           ret
301
	   mov [cpu_vendor+4-OS_BASE], edx
355
 
302
	   mov [cpu_vendor+8-OS_BASE], ecx
356
.check_AMD:
303
 
357
           cmp ebx, dword [AMD_str-OS_BASE]
304
	   cmp ebx, dword [AMD_str-OS_BASE]
358
           jne .unknown
305
	   jne $
359
           cmp edx, dword [AMD_str+4-OS_BASE]
-
 
360
           jne .unknown
306
	   cmp edx, dword [AMD_str+4-OS_BASE]
361
           cmp ecx, dword [AMD_str+8-OS_BASE]
307
	   jne $
362
           jne .unknown
308
	   cmp ecx, dword [AMD_str+8-OS_BASE]
363
           mov [cpu_AMD], 1
309
	   jne $
364
           cmp eax, 1
310
	   cmp eax, 1
365
           jl .unknown
311
	   jl $
366
           mov eax, 1
312
	   mov eax, 1
367
           cpuid
313
	   cpuid
368
           mov [cpu_sign-OS_BASE], eax
314
	   mov [cpu_sign-OS_BASE],  eax
369
           mov [cpu_info-OS_BASE],  ebx
315
	   mov [cpu_info-OS_BASE],  ebx
370
           mov [cpu_caps-OS_BASE],  edx
316
	   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
317
	   mov [cpu_caps+4-OS_BASE],ecx