Subversion Repositories Kolibri OS

Rev

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