Subversion Repositories Kolibri OS

Rev

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