Subversion Repositories Kolibri OS

Rev

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