Subversion Repositories Kolibri OS

Rev

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