Subversion Repositories Kolibri OS

Rev

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