Subversion Repositories Kolibri OS

Rev

Rev 1952 | Rev 2350 | 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: 2047 $
9
 
593 mikedld 10
 
1683 art_zh 11
MEM_WB	   equ 6	       ;write-back memory
12
MEM_WC	   equ 1	       ;write combined memory
13
MEM_UC	   equ 0	       ;uncached memory
465 serge 14
 
1683 art_zh 15
; ======================================================================
16
align 4
17
preinit_mem:
465 serge 18
 
1952 art_zh 19
; clear [CLEAN_ZONE..HEAP_BASE]
1683 art_zh 20
	   xor	 eax,eax
2047 art_zh 21
	   mov	 edi,CLEAN_ZONE 		; 0x280000 = ramdisk FAT ?
1941 art_zh 22
	   mov	 ecx,(HEAP_BASE-OS_BASE-CLEAN_ZONE) / 4
1683 art_zh 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
2047 art_zh 38
	   mov	 edi, (BOOT_VAR - OS_BASE)		; low mem storage area
1683 art_zh 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
47
	   mov edi, sys_pgdir-OS_BASE
48
	   mov ecx, 4096/4
49
	   rep stosd
50
	ret
51
 
52
; ======================================================================
465 serge 53
align 4
54
proc init_mem
55
 
2047 art_zh 56
	   mov	ecx, 0xC001001A 			; Top of Memory MSR
57
	   xor	edi, edi
58
	   rdmsr
59
	   mov	esi, eax				; esi = total amount of memory
60
	   mov	ecx, 0x0200
61
.read_mtrr:
62
	   rdmsr
63
	   and	eax, 0xFFF00000 			; not just bitcleaning
64
	   jz	.next_mtrr				; ignore the main memory and free MTRRs
65
	   cmp	esi, eax
66
	   jb	.next_mtrr				; ignore MMIO blocks
67
	   mov	esi, eax
68
.next_mtrr:
69
	   add	cl, 2
70
	   cmp	cl, 0x10
71
	   jb	.read_mtrr
72
 
73
	   mov	eax, USER_DMA_SIZE
74
	   sub	esi, eax				; exclude the Global DMA block...
75
	   and	esi, 0xFF800000 			; ...and the hole above it
76
	   mov	eax, esi
77
	   mov	[MEM_AMOUNT-OS_BASE], eax
78
	   mov	[pg_data.mem_amount-OS_BASE], eax	; the true MEMTOP
79
	   mov	[UserDMAaddr-OS_BASE], eax
80
 
1683 art_zh 81
	   mov edx, esi 				; edx will hold maximum allocatable address
82
	   shr esi, 12
83
	   mov [pg_data.pages_count-OS_BASE], esi	; max number of PTEs   ?
513 serge 84
 
1683 art_zh 85
	   shr edx, 12
86
	   add edx, 31
87
	   and edx, not 31
88
	   shr edx, 3
89
	   mov [pg_data.pagemap_size-OS_BASE], edx	; size of sys_pgmap structure
465 serge 90
 
1683 art_zh 91
	   add edx, (sys_pgmap-OS_BASE)+4095
92
	   and edx, not 4095
93
	   mov [tmp_page_tabs], edx			; free zone to build PTEs
465 serge 94
 
1683 art_zh 95
	   mov edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
96
	   mov [pg_data.kernel_pages -OS_BASE], edx
97
	   shr edx, 10
98
	   mov [pg_data.kernel_tables-OS_BASE], edx
465 serge 99
 
1683 art_zh 100
	   mov edx, (sys_pgdir-OS_BASE)+ 0x800		; (0x800 = OS_BASE shr 20)
465 serge 101
 
1683 art_zh 102
	   mov ebx, cr4
103
	   or  ebx, CR4_PSE
104
	   mov eax, PG_LARGE+PG_SW
105
	   mov cr4, ebx
106
	   dec [pg_data.kernel_tables-OS_BASE]
465 serge 107
 
1683 art_zh 108
	   mov [edx], eax				; map first (physical) 4M bytes
109
	   add edx, 4
465 serge 110
 
1683 art_zh 111
	   mov edi, [tmp_page_tabs]
1952 art_zh 112
	   mov ecx, [pg_data.kernel_pages -OS_BASE]	; safety cleaning of already-zeroed space
113
	   xor eax, eax
114
	   rep stosd
1683 art_zh 115
 
116
	   mov ecx, [pg_data.kernel_tables-OS_BASE]	; build some PDEs to hold empty PTEs
117
	   mov eax, [tmp_page_tabs]
118
	   or  eax, PG_SW
119
	   mov edi, edx 		; edi = sys_pgdir+0x804
1952 art_zh 120
 
465 serge 121
.map_kernel_tabs:
1683 art_zh 122
	   stosd
123
	   add eax, 0x1000
124
	   dec ecx
125
	   jnz .map_kernel_tabs
465 serge 126
 
1683 art_zh 127
; map pagetables to linear space
128
	   mov dword [sys_pgdir-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE
465 serge 129
 
1683 art_zh 130
	   mov edi, (sys_pgdir-OS_BASE)
131
	   lea esi, [edi+(OS_BASE shr 20)]
132
	   movsd
133
	   movsd
134
	   ret
465 serge 135
endp
136
 
137
align 4
138
proc init_page_map
1103 diamond 139
; mark all memory as unavailable
1683 art_zh 140
	   mov edi, sys_pgmap-OS_BASE
141
	   mov ecx, [pg_data.pagemap_size-OS_BASE]
142
	   shr ecx, 2
143
	   xor eax, eax
144
	   cld
145
	   rep stosd
465 serge 146
 
1103 diamond 147
; scan through memory map and mark free areas as available
1683 art_zh 148
	   mov ebx, BOOT_VAR-OS_BASE + 0x9104
149
	   mov edx, [ebx-4]
1103 diamond 150
.scanmap:
1683 art_zh 151
	   mov ecx, [ebx+8]
152
	   shr ecx, 12 ; ecx = number of pages
153
	   jz .next
154
	   mov edi, [ebx]
155
	   shr edi, 12 ; edi = first page
156
	   mov eax, edi
157
	   shr edi, 5
158
	   shl edi, 2
159
	   add edi, sys_pgmap-OS_BASE
160
	   and eax, 31
161
	   jz .startok
162
	   add ecx, eax
163
	   sub ecx, 32
164
	   jbe .onedword
165
	   push ecx
166
	   mov ecx, eax
167
	   or eax, -1
168
	   shl eax, cl
169
	   or [edi], eax
170
	   add edi, 4
171
	   pop ecx
1103 diamond 172
.startok:
1683 art_zh 173
	   push ecx
174
	   shr ecx, 5
175
	   or eax, -1
176
	   rep stosd
177
	   pop ecx
178
	   and ecx, 31
179
	   neg eax
180
	   shl eax, cl
181
	   dec eax
182
	   or [edi], eax
183
	   jmp .next
1103 diamond 184
.onedword:
1683 art_zh 185
	   add ecx, 32
186
	   sub ecx, eax
1103 diamond 187
@@:
1683 art_zh 188
	   bts [edi], eax
189
	   inc eax
190
	   loop @b
1103 diamond 191
.next:
1683 art_zh 192
	   add ebx, 20
193
	   dec edx
194
	   jnz .scanmap
1103 diamond 195
 
196
; mark kernel memory as allocated (unavailable)
1683 art_zh 197
	   mov ecx, [tmp_page_tabs]
198
	   mov edx, [pg_data.pages_count-OS_BASE]
199
	   shr ecx, 12
200
	   add ecx, [pg_data.kernel_tables-OS_BASE]
201
	   sub edx, ecx
202
	   mov [pg_data.pages_free-OS_BASE], edx
465 serge 203
 
1683 art_zh 204
	   mov edi, sys_pgmap-OS_BASE
205
	   mov ebx, ecx
206
	   shr ecx, 5
207
	   xor eax, eax
208
	   rep stosd
465 serge 209
 
1683 art_zh 210
	   not eax
211
	   mov ecx, ebx
212
	   and ecx, 31
213
	   shl eax, cl
214
	   and [edi], eax
215
	   add edi, OS_BASE
216
	   mov [page_start-OS_BASE], edi;
465 serge 217
 
1683 art_zh 218
	   mov ebx, sys_pgmap
219
	   add ebx, [pg_data.pagemap_size-OS_BASE]
220
	   mov [page_end-OS_BASE], ebx
465 serge 221
 
1683 art_zh 222
	   mov [pg_data.pg_mutex-OS_BASE], 0
223
	   ret
465 serge 224
endp
225
 
226
align 4
586 serge 227
 
228
init_BIOS32:
1683 art_zh 229
	   mov edi, 0xE0000
586 serge 230
.pcibios_nxt:
1683 art_zh 231
	   cmp dword[edi], '_32_' ; "magic" word
232
	   je .BIOS32_found
586 serge 233
.pcibios_nxt2:
1683 art_zh 234
	   add edi, 0x10
235
	   cmp edi, 0xFFFF0
236
	   je .BIOS32_not_found
237
	   jmp .pcibios_nxt
586 serge 238
.BIOS32_found:			; magic word found, check control summ
239
 
1683 art_zh 240
	   movzx ecx, byte[edi + 9]
241
	   shl ecx, 4
242
	   mov esi, edi
243
	   xor eax, eax
244
	   cld	 ; paranoia
586 serge 245
@@:	lodsb
1683 art_zh 246
	   add ah, al
247
	   loop @b
248
	   jnz .pcibios_nxt2 ; control summ must be zero
586 serge 249
    ; BIOS32 service found !
1683 art_zh 250
	   mov ebp, [edi + 4]
251
	   mov [bios32_entry], ebp
586 serge 252
    ; check PCI BIOS present
1683 art_zh 253
	   mov eax, '$PCI'
254
	   xor ebx, ebx
255
	   push cs  ; special for 'ret far' from  BIOS
256
	   call ebp
257
	   test al, al
258
	   jnz .PCI_BIOS32_not_found
586 serge 259
 
260
 ; здесь создаются дискрипторы для PCI BIOS
261
 
1683 art_zh 262
	   add ebx, OS_BASE
263
	   dec ecx
264
	   mov [(pci_code_32-OS_BASE)], cx    ;limit 0-15
265
	   mov [(pci_data_32-OS_BASE)], cx    ;limit 0-15
586 serge 266
 
1683 art_zh 267
	   mov [(pci_code_32-OS_BASE)+2], bx  ;base  0-15
268
	   mov [(pci_data_32-OS_BASE)+2], bx  ;base  0-15
586 serge 269
 
1683 art_zh 270
	   shr ebx, 16
271
	   mov [(pci_code_32-OS_BASE)+4], bl  ;base  16-23
272
	   mov [(pci_data_32-OS_BASE)+4], bl  ;base  16-23
586 serge 273
 
1683 art_zh 274
	   shr ecx, 16
275
	   and cl, 0x0F
276
	   mov ch, bh
277
	   add cx, D32
278
	   mov [(pci_code_32-OS_BASE)+6], cx  ;lim   16-19 &
279
	   mov [(pci_data_32-OS_BASE)+6], cx  ;base  24-31
586 serge 280
 
1683 art_zh 281
	   mov [(pci_bios_entry-OS_BASE)], edx
282
	 ; jmp .end
586 serge 283
.PCI_BIOS32_not_found:
284
	; здесь должна заполнятся pci_emu_dat
285
.BIOS32_not_found:
286
.end:
1683 art_zh 287
	   ret
586 serge 288
 
289
align 4
1683 art_zh 290
test_cpu:	; only AMD machines supported
465 serge 291
 
1683 art_zh 292
	   xor eax, eax
293
	   mov [cpu_caps-OS_BASE], eax
294
	   mov [cpu_caps+4-OS_BASE], eax
465 serge 295
 
1683 art_zh 296
	   pushfd
297
	   pop eax
298
	   mov ecx, eax
299
	   xor eax, 0x40000
300
	   push eax
301
	   popfd
302
	   pushfd
303
	   pop eax
304
	   xor eax, ecx
305
	   jz $ 		; 386
306
	   push ecx
307
	   popfd
465 serge 308
 
1683 art_zh 309
	   mov eax, ecx
310
	   xor eax, 0x200000
311
	   push eax
312
	   popfd
313
	   pushfd
314
	   pop eax
315
	   xor eax, ecx
316
	   je $ 		; 486
465 serge 317
 
1683 art_zh 318
	   xor eax, eax
319
	   cpuid
465 serge 320
 
1683 art_zh 321
	   mov [cpu_vendor-OS_BASE],   ebx
322
	   mov [cpu_vendor+4-OS_BASE], edx
323
	   mov [cpu_vendor+8-OS_BASE], ecx
465 serge 324
 
1683 art_zh 325
	   cmp ebx, dword [AMD_str-OS_BASE]
326
	   jne $
327
	   cmp edx, dword [AMD_str+4-OS_BASE]
328
	   jne $
329
	   cmp ecx, dword [AMD_str+8-OS_BASE]
330
	   jne $
331
	   cmp eax, 1
332
	   jl $
333
	   mov eax, 1
334
	   cpuid
335
	   mov [cpu_sign-OS_BASE],  eax
336
	   mov [cpu_info-OS_BASE],  ebx
337
	   mov [cpu_caps-OS_BASE],  edx
338
	   mov [cpu_caps+4-OS_BASE],ecx
339
	   shr eax, 8
340
	   and eax, 0x0f
341
	   ret
465 serge 342