Subversion Repositories Kolibri OS

Rev

Rev 3539 | Rev 4390 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3539 Rev 3732
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2
;;                                                              ;;
3
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
3
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
4
;; Distributed under terms of the GNU General Public License    ;;
4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
7
 
8
$Revision: 3539 $
8
$Revision: 3732 $
9
 
9
 
10
 
10
 
11
MEM_WB     equ 6               ;write-back memory
11
MEM_WB     equ 6               ;write-back memory
12
MEM_WC     equ 1               ;write combined memory
12
MEM_WC     equ 1               ;write combined memory
13
MEM_UC     equ 0               ;uncached memory
13
MEM_UC     equ 0               ;uncached memory
14
 
14
 
15
align 4
15
align 4
16
proc mem_test
16
proc mem_test
17
; if we have BIOS with fn E820, skip the test
17
; if we have BIOS with fn E820, skip the test
18
        cmp     dword [BOOT_VAR-OS_BASE + 0x9100], 0
18
        cmp     dword [BOOT_VARS-OS_BASE + 0x9100], 0
19
        jnz     .ret
19
        jnz     .ret
20
 
20
 
21
        mov     eax, cr0
21
        mov     eax, cr0
22
        and     eax, not (CR0_CD+CR0_NW)
22
        and     eax, not (CR0_CD+CR0_NW)
23
        or      eax, CR0_CD       ;disable caching
23
        or      eax, CR0_CD       ;disable caching
24
        mov     cr0, eax
24
        mov     cr0, eax
25
        wbinvd                    ;invalidate cache
25
        wbinvd                    ;invalidate cache
26
 
26
 
27
        xor     edi, edi
27
        xor     edi, edi
28
        mov     ebx, 'TEST'
28
        mov     ebx, 'TEST'
29
@@:
29
@@:
30
        add     edi, 0x100000
30
        add     edi, 0x100000
31
        xchg    ebx, dword [edi]
31
        xchg    ebx, dword [edi]
32
        cmp     dword [edi], 'TEST'
32
        cmp     dword [edi], 'TEST'
33
        xchg    ebx, dword [edi]
33
        xchg    ebx, dword [edi]
34
        je      @b
34
        je      @b
35
 
35
 
36
        and     eax, not (CR0_CD+CR0_NW) ;enable caching
36
        and     eax, not (CR0_CD+CR0_NW) ;enable caching
37
        mov     cr0, eax
37
        mov     cr0, eax
38
        inc     dword [BOOT_VAR-OS_BASE + 0x9100]
38
        inc     dword [BOOT_VARS-OS_BASE + 0x9100]
39
        xor     eax, eax
39
        xor     eax, eax
40
        mov     [BOOT_VAR-OS_BASE + 0x9104], eax
40
        mov     [BOOT_VARS-OS_BASE + 0x9104], eax
41
        mov     [BOOT_VAR-OS_BASE + 0x9108], eax
41
        mov     [BOOT_VARS-OS_BASE + 0x9108], eax
42
        mov     [BOOT_VAR-OS_BASE + 0x910C], edi
42
        mov     [BOOT_VARS-OS_BASE + 0x910C], edi
43
        mov     [BOOT_VAR-OS_BASE + 0x9110], eax
43
        mov     [BOOT_VARS-OS_BASE + 0x9110], eax
44
.ret:
44
.ret:
45
        ret
45
        ret
46
endp
46
endp
47
 
47
 
48
align 4
48
align 4
49
proc init_mem
49
proc init_mem
50
; calculate maximum allocatable address and number of allocatable pages
50
; calculate maximum allocatable address and number of allocatable pages
51
        mov     edi, BOOT_VAR-OS_BASE + 0x9104
51
        mov     edi, BOOT_VARS-OS_BASE + 0x9104
52
        mov     ecx, [edi-4]
52
        mov     ecx, [edi-4]
53
        xor     esi, esi; esi will hold total amount of memory
53
        xor     esi, esi; esi will hold total amount of memory
54
        xor     edx, edx; edx will hold maximum allocatable address
54
        xor     edx, edx; edx will hold maximum allocatable address
55
.calcmax:
55
.calcmax:
56
; round all to pages
56
; round all to pages
57
        mov     eax, [edi]
57
        mov     eax, [edi]
58
        cmp     [edi+16], byte 1
58
        cmp     [edi+16], byte 1
59
        jne     .unusable
59
        jne     .unusable
60
 
60
 
61
        test    eax, 0xFFF
61
        test    eax, 0xFFF
62
        jz      @f
62
        jz      @f
63
        neg     eax
63
        neg     eax
64
        and     eax, 0xFFF
64
        and     eax, 0xFFF
65
        add     [edi], eax
65
        add     [edi], eax
66
        adc     dword [edi+4], 0
66
        adc     dword [edi+4], 0
67
        sub     [edi+8], eax
67
        sub     [edi+8], eax
68
        sbb     dword [edi+12], 0
68
        sbb     dword [edi+12], 0
69
        jc      .unusable
69
        jc      .unusable
70
@@:
70
@@:
71
        and     dword [edi+8], not 0xFFF
71
        and     dword [edi+8], not 0xFFF
72
        jz      .unusable
72
        jz      .unusable
73
; ignore memory after 4 Gb
73
; ignore memory after 4 Gb
74
        cmp     dword [edi+4], 0
74
        cmp     dword [edi+4], 0
75
        jnz     .unusable
75
        jnz     .unusable
76
        mov     eax, [edi]
76
        mov     eax, [edi]
77
        cmp     dword [edi+12], 0
77
        cmp     dword [edi+12], 0
78
        jnz     .overflow
78
        jnz     .overflow
79
        add     eax, [edi+8]
79
        add     eax, [edi+8]
80
        jnc     @f
80
        jnc     @f
81
.overflow:
81
.overflow:
82
        mov     eax, 0xFFFFF000
82
        mov     eax, 0xFFFFF000
83
@@:
83
@@:
84
        cmp     edx, eax
84
        cmp     edx, eax
85
        jae     @f
85
        jae     @f
86
        mov     edx, eax
86
        mov     edx, eax
87
@@:
87
@@:
88
        sub     eax, [edi]
88
        sub     eax, [edi]
89
        mov     [edi+8], eax
89
        mov     [edi+8], eax
90
        add     esi, eax
90
        add     esi, eax
91
        jmp     .usable
91
        jmp     .usable
92
.unusable:
92
.unusable:
93
;        and     dword [edi+8], 0
93
;        and     dword [edi+8], 0
94
.usable:
94
.usable:
95
        add     edi, 20
95
        add     edi, 20
96
        loop    .calcmax
96
        loop    .calcmax
97
.calculated:
97
.calculated:
98
        mov     [MEM_AMOUNT-OS_BASE], esi
98
        mov     [MEM_AMOUNT-OS_BASE], esi
99
        mov     [pg_data.mem_amount-OS_BASE], esi
99
        mov     [pg_data.mem_amount-OS_BASE], esi
100
        shr     esi, 12
100
        shr     esi, 12
101
        mov     [pg_data.pages_count-OS_BASE], esi
101
        mov     [pg_data.pages_count-OS_BASE], esi
102
 
102
 
103
        shr     edx, 12
103
        shr     edx, 12
104
        add     edx, 31
104
        add     edx, 31
105
        and     edx, not 31
105
        and     edx, not 31
106
        shr     edx, 3
106
        shr     edx, 3
107
        mov     [pg_data.pagemap_size-OS_BASE], edx
107
        mov     [pg_data.pagemap_size-OS_BASE], edx
108
 
108
 
109
        add     edx, (sys_pgmap-OS_BASE)+4095
109
        add     edx, (sys_pgmap-OS_BASE)+4095
110
        and     edx, not 4095
110
        and     edx, not 4095
111
        mov     [tmp_page_tabs], edx
111
        mov     [tmp_page_tabs], edx
112
 
112
 
113
        mov     edx, esi
113
        mov     edx, esi
114
        and     edx, -1024
114
        and     edx, -1024
115
        cmp     edx, (OS_BASE/4096)
115
        cmp     edx, (OS_BASE/4096)
116
        jbe     @F
116
        jbe     @F
117
        mov     edx, (OS_BASE/4096)
117
        mov     edx, (OS_BASE/4096)
118
        jmp     .set
118
        jmp     .set
119
@@:
119
@@:
120
        cmp     edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
120
        cmp     edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
121
        jae     .set
121
        jae     .set
122
        mov     edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
122
        mov     edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
123
.set:
123
.set:
124
        mov     [pg_data.kernel_pages-OS_BASE], edx
124
        mov     [pg_data.kernel_pages-OS_BASE], edx
125
        shr     edx, 10
125
        shr     edx, 10
126
        mov     [pg_data.kernel_tables-OS_BASE], edx
126
        mov     [pg_data.kernel_tables-OS_BASE], edx
127
 
127
 
128
        xor     eax, eax
128
        xor     eax, eax
129
        mov     edi, sys_pgdir-OS_BASE
129
        mov     edi, sys_pgdir-OS_BASE
130
        mov     ecx, 4096/4
130
        mov     ecx, 4096/4
131
        cld
131
        cld
132
        rep stosd
132
        rep stosd
133
 
133
 
134
        mov     edx, (sys_pgdir-OS_BASE)+ 0x800; (OS_BASE shr 20)
134
        mov     edx, (sys_pgdir-OS_BASE)+ 0x800; (OS_BASE shr 20)
135
        bt      [cpu_caps-OS_BASE], CAPS_PSE
135
        bt      [cpu_caps-OS_BASE], CAPS_PSE
136
        jnc     .no_PSE
136
        jnc     .no_PSE
137
 
137
 
138
        mov     ebx, cr4
138
        mov     ebx, cr4
139
        or      ebx, CR4_PSE
139
        or      ebx, CR4_PSE
140
        mov     eax, PG_LARGE+PG_SW
140
        mov     eax, PG_LARGE+PG_SW
141
        mov     cr4, ebx
141
        mov     cr4, ebx
142
        dec     [pg_data.kernel_tables-OS_BASE]
142
        dec     [pg_data.kernel_tables-OS_BASE]
143
 
143
 
144
        mov     [edx], eax
144
        mov     [edx], eax
145
        add     edx, 4
145
        add     edx, 4
146
 
146
 
147
        mov     edi, [tmp_page_tabs]
147
        mov     edi, [tmp_page_tabs]
148
        jmp     .map_kernel_heap        ; new kernel fits to the first 4Mb - nothing to do with ".map_low"
148
        jmp     .map_kernel_heap        ; new kernel fits to the first 4Mb - nothing to do with ".map_low"
149
.no_PSE:
149
.no_PSE:
150
        mov     eax, PG_SW
150
        mov     eax, PG_SW
151
        mov     ecx, [tmp_page_tabs]
151
        mov     ecx, [tmp_page_tabs]
152
        shr     ecx, 12
152
        shr     ecx, 12
153
.map_low:
153
.map_low:
154
        mov     edi, [tmp_page_tabs]
154
        mov     edi, [tmp_page_tabs]
155
@@:                                   ;
155
@@:                                   ;
156
        stosd
156
        stosd
157
        add     eax, 0x1000
157
        add     eax, 0x1000
158
        dec     ecx
158
        dec     ecx
159
        jnz     @B
159
        jnz     @B
160
 
160
 
161
.map_kernel_heap:
161
.map_kernel_heap:
162
        mov     ecx, [pg_data.kernel_tables-OS_BASE]
162
        mov     ecx, [pg_data.kernel_tables-OS_BASE]
163
        shl     ecx, 10
163
        shl     ecx, 10
164
        xor     eax, eax
164
        xor     eax, eax
165
        rep stosd
165
        rep stosd
166
 
166
 
167
        mov     ecx, [pg_data.kernel_tables-OS_BASE]
167
        mov     ecx, [pg_data.kernel_tables-OS_BASE]
168
        mov     eax, [tmp_page_tabs]
168
        mov     eax, [tmp_page_tabs]
169
        or      eax, PG_SW
169
        or      eax, PG_SW
170
        mov     edi, edx
170
        mov     edi, edx
171
 
171
 
172
.map_kernel_tabs:
172
.map_kernel_tabs:
173
        stosd
173
        stosd
174
        add     eax, 0x1000
174
        add     eax, 0x1000
175
        dec     ecx
175
        dec     ecx
176
        jnz     .map_kernel_tabs
176
        jnz     .map_kernel_tabs
177
 
177
 
178
        mov     dword [sys_pgdir-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE
178
        mov     dword [sys_pgdir-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE
179
 
179
 
180
        mov     edi, (sys_pgdir-OS_BASE)
180
        mov     edi, (sys_pgdir-OS_BASE)
181
        lea     esi, [edi+(OS_BASE shr 20)]
181
        lea     esi, [edi+(OS_BASE shr 20)]
182
        movsd
182
        movsd
183
        movsd
183
        movsd
184
        ret
184
        ret
185
endp
185
endp
186
 
186
 
187
align 4
187
align 4
188
proc init_page_map
188
proc init_page_map
189
; mark all memory as unavailable
189
; mark all memory as unavailable
190
        mov     edi, sys_pgmap-OS_BASE
190
        mov     edi, sys_pgmap-OS_BASE
191
        mov     ecx, [pg_data.pagemap_size-OS_BASE]
191
        mov     ecx, [pg_data.pagemap_size-OS_BASE]
192
        shr     ecx, 2
192
        shr     ecx, 2
193
        xor     eax, eax
193
        xor     eax, eax
194
        cld
194
        cld
195
        rep stosd
195
        rep stosd
196
 
196
 
197
; scan through memory map and mark free areas as available
197
; scan through memory map and mark free areas as available
198
        mov     ebx, BOOT_VAR-OS_BASE + 0x9104
198
        mov     ebx, BOOT_VARS-OS_BASE + 0x9104
199
        mov     edx, [ebx-4]
199
        mov     edx, [ebx-4]
200
.scanmap:
200
.scanmap:
201
        cmp     [ebx+16], byte 1
201
        cmp     [ebx+16], byte 1
202
        jne     .next
202
        jne     .next
203
 
203
 
204
        mov     ecx, [ebx+8]
204
        mov     ecx, [ebx+8]
205
        shr     ecx, 12; ecx = number of pages
205
        shr     ecx, 12; ecx = number of pages
206
        jz      .next
206
        jz      .next
207
        mov     edi, [ebx]
207
        mov     edi, [ebx]
208
        shr     edi, 12; edi = first page
208
        shr     edi, 12; edi = first page
209
        mov     eax, edi
209
        mov     eax, edi
210
        shr     edi, 5
210
        shr     edi, 5
211
        shl     edi, 2
211
        shl     edi, 2
212
        add     edi, sys_pgmap-OS_BASE
212
        add     edi, sys_pgmap-OS_BASE
213
        and     eax, 31
213
        and     eax, 31
214
        jz      .startok
214
        jz      .startok
215
        add     ecx, eax
215
        add     ecx, eax
216
        sub     ecx, 32
216
        sub     ecx, 32
217
        jbe     .onedword
217
        jbe     .onedword
218
        push    ecx
218
        push    ecx
219
        mov     ecx, eax
219
        mov     ecx, eax
220
        or      eax, -1
220
        or      eax, -1
221
        shl     eax, cl
221
        shl     eax, cl
222
        or      [edi], eax
222
        or      [edi], eax
223
        add     edi, 4
223
        add     edi, 4
224
        pop     ecx
224
        pop     ecx
225
.startok:
225
.startok:
226
        push    ecx
226
        push    ecx
227
        shr     ecx, 5
227
        shr     ecx, 5
228
        or      eax, -1
228
        or      eax, -1
229
        rep stosd
229
        rep stosd
230
        pop     ecx
230
        pop     ecx
231
        and     ecx, 31
231
        and     ecx, 31
232
        neg     eax
232
        neg     eax
233
        shl     eax, cl
233
        shl     eax, cl
234
        dec     eax
234
        dec     eax
235
        or      [edi], eax
235
        or      [edi], eax
236
        jmp     .next
236
        jmp     .next
237
.onedword:
237
.onedword:
238
        add     ecx, 32
238
        add     ecx, 32
239
        sub     ecx, eax
239
        sub     ecx, eax
240
@@:
240
@@:
241
        bts     [edi], eax
241
        bts     [edi], eax
242
        inc     eax
242
        inc     eax
243
        loop    @b
243
        loop    @b
244
.next:
244
.next:
245
        add     ebx, 20
245
        add     ebx, 20
246
        dec     edx
246
        dec     edx
247
        jnz     .scanmap
247
        jnz     .scanmap
248
 
248
 
249
; mark kernel memory as allocated (unavailable)
249
; mark kernel memory as allocated (unavailable)
250
        mov     ecx, [tmp_page_tabs]
250
        mov     ecx, [tmp_page_tabs]
251
        mov     edx, [pg_data.pages_count-OS_BASE]
251
        mov     edx, [pg_data.pages_count-OS_BASE]
252
        shr     ecx, 12
252
        shr     ecx, 12
253
        add     ecx, [pg_data.kernel_tables-OS_BASE]
253
        add     ecx, [pg_data.kernel_tables-OS_BASE]
254
        sub     edx, ecx
254
        sub     edx, ecx
255
        mov     [pg_data.pages_free-OS_BASE], edx
255
        mov     [pg_data.pages_free-OS_BASE], edx
256
 
256
 
257
        mov     edi, sys_pgmap-OS_BASE
257
        mov     edi, sys_pgmap-OS_BASE
258
        mov     ebx, ecx
258
        mov     ebx, ecx
259
        shr     ecx, 5
259
        shr     ecx, 5
260
        xor     eax, eax
260
        xor     eax, eax
261
        rep stosd
261
        rep stosd
262
 
262
 
263
        not     eax
263
        not     eax
264
        mov     ecx, ebx
264
        mov     ecx, ebx
265
        and     ecx, 31
265
        and     ecx, 31
266
        shl     eax, cl
266
        shl     eax, cl
267
        and     [edi], eax
267
        and     [edi], eax
268
        add     edi, OS_BASE
268
        add     edi, OS_BASE
269
        mov     [page_start-OS_BASE], edi;
269
        mov     [page_start-OS_BASE], edi;
270
 
270
 
271
        mov     ebx, sys_pgmap
271
        mov     ebx, sys_pgmap
272
        add     ebx, [pg_data.pagemap_size-OS_BASE]
272
        add     ebx, [pg_data.pagemap_size-OS_BASE]
273
        mov     [page_end-OS_BASE], ebx
273
        mov     [page_end-OS_BASE], ebx
274
 
274
 
275
        ret
275
        ret
276
endp
276
endp
277
 
277
 
278
align 4
278
align 4
279
 
279
 
280
init_BIOS32:
280
init_BIOS32:
281
        mov     edi, 0xE0000
281
        mov     edi, 0xE0000
282
.pcibios_nxt:
282
.pcibios_nxt:
283
        cmp     dword[edi], '_32_'; "magic" word
283
        cmp     dword[edi], '_32_'; "magic" word
284
        je      .BIOS32_found
284
        je      .BIOS32_found
285
.pcibios_nxt2:
285
.pcibios_nxt2:
286
        add     edi, 0x10
286
        add     edi, 0x10
287
        cmp     edi, 0xFFFF0
287
        cmp     edi, 0xFFFF0
288
        je      .BIOS32_not_found
288
        je      .BIOS32_not_found
289
        jmp     .pcibios_nxt
289
        jmp     .pcibios_nxt
290
.BIOS32_found:                  ; magic word found, check control summ
290
.BIOS32_found:                  ; magic word found, check control summ
291
 
291
 
292
        movzx   ecx, byte[edi + 9]
292
        movzx   ecx, byte[edi + 9]
293
        shl     ecx, 4
293
        shl     ecx, 4
294
        mov     esi, edi
294
        mov     esi, edi
295
        xor     eax, eax
295
        xor     eax, eax
296
        cld      ; paranoia
296
        cld      ; paranoia
297
@@:
297
@@:
298
        lodsb
298
        lodsb
299
        add     ah, al
299
        add     ah, al
300
        loop    @b
300
        loop    @b
301
        jnz     .pcibios_nxt2; control summ must be zero
301
        jnz     .pcibios_nxt2; control summ must be zero
302
    ; BIOS32 service found !
302
    ; BIOS32 service found !
303
        mov     ebp, [edi + 4]
303
        mov     ebp, [edi + 4]
304
        mov     [bios32_entry], ebp
304
        mov     [bios32_entry], ebp
305
    ; check PCI BIOS present
305
    ; check PCI BIOS present
306
        mov     eax, '$PCI'
306
        mov     eax, '$PCI'
307
        xor     ebx, ebx
307
        xor     ebx, ebx
308
        push    cs  ; special for 'ret far' from  BIOS
308
        push    cs  ; special for 'ret far' from  BIOS
309
        call    ebp
309
        call    ebp
310
        test    al, al
310
        test    al, al
311
        jnz     .PCI_BIOS32_not_found
311
        jnz     .PCI_BIOS32_not_found
312
 
312
 
313
 ; здесь создаются дискрипторы для PCI BIOS
313
 ; здесь создаются дискрипторы для PCI BIOS
314
 
314
 
315
        add     ebx, OS_BASE
315
        add     ebx, OS_BASE
316
        dec     ecx
316
        dec     ecx
317
        mov     [(pci_code_32-OS_BASE)], cx   ;limit 0-15
317
        mov     [(pci_code_32-OS_BASE)], cx   ;limit 0-15
318
        mov     [(pci_data_32-OS_BASE)], cx   ;limit 0-15
318
        mov     [(pci_data_32-OS_BASE)], cx   ;limit 0-15
319
 
319
 
320
        mov     [(pci_code_32-OS_BASE)+2], bx ;base  0-15
320
        mov     [(pci_code_32-OS_BASE)+2], bx ;base  0-15
321
        mov     [(pci_data_32-OS_BASE)+2], bx ;base  0-15
321
        mov     [(pci_data_32-OS_BASE)+2], bx ;base  0-15
322
 
322
 
323
        shr     ebx, 16
323
        shr     ebx, 16
324
        mov     [(pci_code_32-OS_BASE)+4], bl ;base  16-23
324
        mov     [(pci_code_32-OS_BASE)+4], bl ;base  16-23
325
        mov     [(pci_data_32-OS_BASE)+4], bl ;base  16-23
325
        mov     [(pci_data_32-OS_BASE)+4], bl ;base  16-23
326
 
326
 
327
        shr     ecx, 16
327
        shr     ecx, 16
328
        and     cl, 0x0F
328
        and     cl, 0x0F
329
        mov     ch, bh
329
        mov     ch, bh
330
        add     cx, D32
330
        add     cx, D32
331
        mov     [(pci_code_32-OS_BASE)+6], cx ;lim   16-19 &
331
        mov     [(pci_code_32-OS_BASE)+6], cx ;lim   16-19 &
332
        mov     [(pci_data_32-OS_BASE)+6], cx ;base  24-31
332
        mov     [(pci_data_32-OS_BASE)+6], cx ;base  24-31
333
 
333
 
334
        mov     [(pci_bios_entry-OS_BASE)], edx
334
        mov     [(pci_bios_entry-OS_BASE)], edx
335
         ; jmp .end
335
         ; jmp .end
336
.PCI_BIOS32_not_found:
336
.PCI_BIOS32_not_found:
337
        ; здесь должна заполнятся pci_emu_dat
337
        ; здесь должна заполнятся pci_emu_dat
338
.BIOS32_not_found:
338
.BIOS32_not_found:
339
.end:
339
.end:
340
        ret
340
        ret
341
 
341
 
342
align 4
342
align 4
343
proc test_cpu
343
proc test_cpu
344
           locals
344
           locals
345
              cpu_type   dd ?
345
              cpu_type   dd ?
346
              cpu_id     dd ?
346
              cpu_id     dd ?
347
              cpu_Intel  dd ?
347
              cpu_Intel  dd ?
348
              cpu_AMD    dd ?
348
              cpu_AMD    dd ?
349
           endl
349
           endl
350
 
350
 
351
        xor     eax, eax
351
        xor     eax, eax
352
        mov     [cpu_type], eax
352
        mov     [cpu_type], eax
353
        mov     [cpu_caps-OS_BASE], eax
353
        mov     [cpu_caps-OS_BASE], eax
354
        mov     [cpu_caps+4-OS_BASE], eax
354
        mov     [cpu_caps+4-OS_BASE], eax
355
 
355
 
356
        pushfd
356
        pushfd
357
        pop     eax
357
        pop     eax
358
        mov     ecx, eax
358
        mov     ecx, eax
359
        xor     eax, 0x40000
359
        xor     eax, 0x40000
360
        push    eax
360
        push    eax
361
        popfd
361
        popfd
362
        pushfd
362
        pushfd
363
        pop     eax
363
        pop     eax
364
        xor     eax, ecx
364
        xor     eax, ecx
365
        mov     [cpu_type], CPU_386
365
        mov     [cpu_type], CPU_386
366
        jz      .end_cpuid
366
        jz      .end_cpuid
367
        push    ecx
367
        push    ecx
368
        popfd
368
        popfd
369
 
369
 
370
        mov     [cpu_type], CPU_486
370
        mov     [cpu_type], CPU_486
371
        mov     eax, ecx
371
        mov     eax, ecx
372
        xor     eax, 0x200000
372
        xor     eax, 0x200000
373
        push    eax
373
        push    eax
374
        popfd
374
        popfd
375
        pushfd
375
        pushfd
376
        pop     eax
376
        pop     eax
377
        xor     eax, ecx
377
        xor     eax, ecx
378
        je      .end_cpuid
378
        je      .end_cpuid
379
        mov     [cpu_id], 1
379
        mov     [cpu_id], 1
380
 
380
 
381
        xor     eax, eax
381
        xor     eax, eax
382
        cpuid
382
        cpuid
383
 
383
 
384
        mov     [cpu_vendor-OS_BASE], ebx
384
        mov     [cpu_vendor-OS_BASE], ebx
385
        mov     [cpu_vendor+4-OS_BASE], edx
385
        mov     [cpu_vendor+4-OS_BASE], edx
386
        mov     [cpu_vendor+8-OS_BASE], ecx
386
        mov     [cpu_vendor+8-OS_BASE], ecx
387
        cmp     ebx, dword [intel_str-OS_BASE]
387
        cmp     ebx, dword [intel_str-OS_BASE]
388
        jne     .check_AMD
388
        jne     .check_AMD
389
        cmp     edx, dword [intel_str+4-OS_BASE]
389
        cmp     edx, dword [intel_str+4-OS_BASE]
390
        jne     .check_AMD
390
        jne     .check_AMD
391
        cmp     ecx, dword [intel_str+8-OS_BASE]
391
        cmp     ecx, dword [intel_str+8-OS_BASE]
392
        jne     .check_AMD
392
        jne     .check_AMD
393
        mov     [cpu_Intel], 1
393
        mov     [cpu_Intel], 1
394
        cmp     eax, 1
394
        cmp     eax, 1
395
        jl      .end_cpuid
395
        jl      .end_cpuid
396
        mov     eax, 1
396
        mov     eax, 1
397
        cpuid
397
        cpuid
398
        mov     [cpu_sign-OS_BASE], eax
398
        mov     [cpu_sign-OS_BASE], eax
399
        mov     [cpu_info-OS_BASE], ebx
399
        mov     [cpu_info-OS_BASE], ebx
400
        mov     [cpu_caps-OS_BASE], edx
400
        mov     [cpu_caps-OS_BASE], edx
401
        mov     [cpu_caps+4-OS_BASE], ecx
401
        mov     [cpu_caps+4-OS_BASE], ecx
402
 
402
 
403
        shr     eax, 8
403
        shr     eax, 8
404
        and     eax, 0x0f
404
        and     eax, 0x0f
405
        ret
405
        ret
406
.end_cpuid:
406
.end_cpuid:
407
        mov     eax, [cpu_type]
407
        mov     eax, [cpu_type]
408
        ret
408
        ret
409
 
409
 
410
.check_AMD:
410
.check_AMD:
411
        cmp     ebx, dword [AMD_str-OS_BASE]
411
        cmp     ebx, dword [AMD_str-OS_BASE]
412
        jne     .unknown
412
        jne     .unknown
413
        cmp     edx, dword [AMD_str+4-OS_BASE]
413
        cmp     edx, dword [AMD_str+4-OS_BASE]
414
        jne     .unknown
414
        jne     .unknown
415
        cmp     ecx, dword [AMD_str+8-OS_BASE]
415
        cmp     ecx, dword [AMD_str+8-OS_BASE]
416
        jne     .unknown
416
        jne     .unknown
417
        mov     [cpu_AMD], 1
417
        mov     [cpu_AMD], 1
418
        cmp     eax, 1
418
        cmp     eax, 1
419
        jl      .unknown
419
        jl      .unknown
420
        mov     eax, 1
420
        mov     eax, 1
421
        cpuid
421
        cpuid
422
        mov     [cpu_sign-OS_BASE], eax
422
        mov     [cpu_sign-OS_BASE], eax
423
        mov     [cpu_info-OS_BASE], ebx
423
        mov     [cpu_info-OS_BASE], ebx
424
        mov     [cpu_caps-OS_BASE], edx
424
        mov     [cpu_caps-OS_BASE], edx
425
        mov     [cpu_caps+4-OS_BASE], ecx
425
        mov     [cpu_caps+4-OS_BASE], ecx
426
        shr     eax, 8
426
        shr     eax, 8
427
        and     eax, 0x0f
427
        and     eax, 0x0f
428
        ret
428
        ret
429
.unknown:
429
.unknown:
430
        mov     eax, 1
430
        mov     eax, 1
431
        cpuid
431
        cpuid
432
        mov     [cpu_sign-OS_BASE], eax
432
        mov     [cpu_sign-OS_BASE], eax
433
        mov     [cpu_info-OS_BASE], ebx
433
        mov     [cpu_info-OS_BASE], ebx
434
        mov     [cpu_caps-OS_BASE], edx
434
        mov     [cpu_caps-OS_BASE], edx
435
        mov     [cpu_caps+4-OS_BASE], ecx
435
        mov     [cpu_caps+4-OS_BASE], ecx
436
        shr     eax, 8
436
        shr     eax, 8
437
        and     eax, 0x0f
437
        and     eax, 0x0f
438
        ret
438
        ret
439
endp
439
endp
440
 
440
 
441
iglobal
441
iglobal
442
align 4
442
align 4
443
acpi_lapic_base   dd 0xfee00000   ; default local apic base
443
acpi_lapic_base   dd 0xfee00000   ; default local apic base
444
endg
444
endg
445
 
445
 
446
uglobal
446
uglobal
447
align 4
447
align 4
448
acpi_rsdp         rd 1
448
acpi_rsdp         rd 1
449
acpi_rsdt         rd 1
449
acpi_rsdt         rd 1
450
acpi_madt         rd 1
450
acpi_madt         rd 1
451
 
451
 
452
acpi_dev_data     rd 1
452
acpi_dev_data     rd 1
453
acpi_dev_size     rd 1
453
acpi_dev_size     rd 1
454
 
454
 
455
acpi_rsdt_base    rd 1
455
acpi_rsdt_base    rd 1
-
 
456
acpi_fadt_base    rd 1
-
 
457
acpi_dsdt_base    rd 1
-
 
458
acpi_dsdt_size    rd 1
456
acpi_madt_base    rd 1
459
acpi_madt_base    rd 1
457
acpi_ioapic_base  rd 1
460
acpi_ioapic_base  rd 1
458
 
461
 
459
cpu_count         rd 1
462
cpu_count         rd 1
460
smpt              rd 16
463
smpt              rd 16
461
endg
464
endg
462
 
465
 
463
ACPI_HI_RSDP_WINDOW_START  equ 0x000E0000
466
ACPI_HI_RSDP_WINDOW_START  equ 0x000E0000
464
ACPI_HI_RSDP_WINDOW_END    equ 0x00100000
467
ACPI_HI_RSDP_WINDOW_END    equ 0x00100000
465
ACPI_RSDP_CHECKSUM_LENGTH  equ 20
468
ACPI_RSDP_CHECKSUM_LENGTH  equ 20
466
ACPI_MADT_SIGN             equ 0x43495041
469
ACPI_MADT_SIGN             equ 0x43495041
-
 
470
ACPI_FADT_SIGN             equ 0x50434146
467
 
471
 
468
 
472
 
469
acpi_locate:
473
acpi_locate:
470
        push    ebx
474
        push    ebx
471
        mov     ebx, ACPI_HI_RSDP_WINDOW_START
475
        mov     ebx, ACPI_HI_RSDP_WINDOW_START
472
.check:
476
.check:
473
        cmp     [ebx], dword 0x20445352
477
        cmp     [ebx], dword 0x20445352
474
        jne     .next
478
        jne     .next
475
        cmp     [ebx+4], dword 0x20525450
479
        cmp     [ebx+4], dword 0x20525450
476
        jne     .next
480
        jne     .next
477
 
481
 
478
        mov     edx, ebx
482
        mov     edx, ebx
479
        mov     ecx, ACPI_RSDP_CHECKSUM_LENGTH
483
        mov     ecx, ACPI_RSDP_CHECKSUM_LENGTH
480
        xor     eax, eax
484
        xor     eax, eax
481
.sum:
485
.sum:
482
        add     al, [edx]
486
        add     al, [edx]
483
        inc     edx
487
        inc     edx
484
        loop    .sum
488
        loop    .sum
485
 
489
 
486
        test    al, al
490
        test    al, al
487
        jnz     .next
491
        jnz     .next
488
 
492
 
489
        mov     eax, ebx
493
        mov     eax, ebx
490
        pop     ebx
494
        pop     ebx
491
        ret
495
        ret
492
.next:
496
.next:
493
        add     ebx, 16
497
        add     ebx, 16
494
        cmp     ebx, ACPI_HI_RSDP_WINDOW_END
498
        cmp     ebx, ACPI_HI_RSDP_WINDOW_END
495
        jb      .check
499
        jb      .check
496
 
500
 
497
        pop     ebx
501
        pop     ebx
498
        xor     eax, eax
502
        xor     eax, eax
499
        ret
503
        ret
500
 
504
 
501
align 4
505
align 4
502
rsdt_find:           ;ecx= rsdt edx= SIG
506
rsdt_find:           ;ecx= rsdt edx= SIG
503
        push    ebx
507
        push    ebx
504
        push    esi
508
        push    esi
505
 
509
 
506
        lea     ebx, [ecx+36]
510
        lea     ebx, [ecx+36]
507
        mov     esi, [ecx+4]
511
        mov     esi, [ecx+4]
508
        add     esi, ecx
512
        add     esi, ecx
509
align 4
513
align 4
510
.next:
514
.next:
511
        mov     eax, [ebx]
515
        mov     eax, [ebx]
512
        cmp     [eax], edx
516
        cmp     [eax], edx
513
        je      .done
517
        je      .done
514
 
518
 
515
        add     ebx, 4
519
        add     ebx, 4
516
        cmp     ebx, esi
520
        cmp     ebx, esi
517
        jb      .next
521
        jb      .next
518
 
522
 
519
        xor     eax, eax
523
        xor     eax, eax
520
        pop     esi
524
        pop     esi
521
        pop     ebx
525
        pop     ebx
522
        ret
526
        ret
523
 
527
 
524
.done:
528
.done:
525
        mov     eax, [ebx]
529
        mov     eax, [ebx]
526
        pop     esi
530
        pop     esi
527
        pop     ebx
531
        pop     ebx
528
        ret
532
        ret
529
 
533
 
530
align 4
534
align 4
531
check_acpi:
535
check_acpi:
532
 
536
 
533
        call    acpi_locate
537
        call    acpi_locate
534
        test    eax, eax
538
        test    eax, eax
535
        jz      .done
539
        jz      .done
536
 
540
 
537
        mov     ecx, [eax+16]
541
        mov     ecx, [eax+16]
538
        mov     edx, ACPI_MADT_SIGN
542
        mov     edx, 0x50434146
539
        mov     [acpi_rsdt_base-OS_BASE], ecx
543
        mov     [acpi_rsdt_base-OS_BASE], ecx
540
        call    rsdt_find
544
        call    rsdt_find
-
 
545
        mov     [acpi_fadt_base-OS_BASE], eax
-
 
546
        test    eax, eax
-
 
547
        jz      @f
-
 
548
 
-
 
549
        mov     eax, [eax+40]
-
 
550
        mov     [acpi_dsdt_base-OS_BASE], eax
-
 
551
        mov     eax, [eax+4]
-
 
552
        mov     [acpi_dsdt_size-OS_BASE], eax
-
 
553
 
-
 
554
@@:
-
 
555
        mov     edx, ACPI_MADT_SIGN
-
 
556
        mov     ecx, [acpi_rsdt_base-OS_BASE]
-
 
557
        call    rsdt_find
541
        test    eax, eax
558
        test    eax, eax
542
        jz      .done
559
        jz      .done
543
 
560
 
544
        mov     [acpi_madt_base-OS_BASE], eax
561
        mov     [acpi_madt_base-OS_BASE], eax
545
        mov     ecx, [eax+36]
562
        mov     ecx, [eax+36]
546
        mov     [acpi_lapic_base-OS_BASE], ecx
563
        mov     [acpi_lapic_base-OS_BASE], ecx
547
 
564
 
548
        mov     edi, smpt-OS_BASE
565
        mov     edi, smpt-OS_BASE
549
        mov     ebx, [ecx+0x20]
566
        mov     ebx, [ecx+0x20]
550
        shr     ebx, 24              ; read APIC ID
567
        shr     ebx, 24              ; read APIC ID
551
 
568
 
552
        mov     [edi], ebx           ; bootstrap always first
569
        mov     [edi], ebx           ; bootstrap always first
553
        inc     [cpu_count-OS_BASE]
570
        inc     [cpu_count-OS_BASE]
554
        add     edi, 4
571
        add     edi, 4
555
 
572
 
556
        lea     edx, [eax+44]
573
        lea     edx, [eax+44]
557
        mov     ecx, [eax+4]
574
        mov     ecx, [eax+4]
558
        add     ecx, eax
575
        add     ecx, eax
559
.check:
576
.check:
560
        mov     eax, [edx]
577
        mov     eax, [edx]
561
        cmp     al, 0
578
        cmp     al, 0
562
        jne     .io_apic
579
        jne     .io_apic
563
 
580
 
564
        shr     eax, 24              ; get APIC ID
581
        shr     eax, 24              ; get APIC ID
565
        cmp     eax, ebx             ; skip self
582
        cmp     eax, ebx             ; skip self
566
        je      .next
583
        je      .next
567
 
584
 
568
        test    [edx+4], byte 1      ; is enabled ?
585
        test    [edx+4], byte 1      ; is enabled ?
569
        jz      .next
586
        jz      .next
570
 
587
 
571
        cmp     [cpu_count-OS_BASE], 16
588
        cmp     [cpu_count-OS_BASE], 16
572
        jae     .next
589
        jae     .next
573
 
590
 
574
        stosd                        ; store APIC ID
591
        stosd                        ; store APIC ID
575
        inc     [cpu_count-OS_BASE]
592
        inc     [cpu_count-OS_BASE]
576
.next:
593
.next:
577
        mov     eax, [edx]
594
        mov     eax, [edx]
578
        movzx   eax, ah
595
        movzx   eax, ah
579
        add     edx, eax
596
        add     edx, eax
580
        cmp     edx, ecx
597
        cmp     edx, ecx
581
        jb      .check
598
        jb      .check
582
.done:
599
.done:
583
        ret
600
        ret
584
 
601
 
585
.io_apic:
602
.io_apic:
586
        cmp     al, 1
603
        cmp     al, 1
587
        jne     .next
604
        jne     .next
588
 
605
 
589
        mov     eax, [edx+4]
606
        mov     eax, [edx+4]
590
        mov     [acpi_ioapic_base-OS_BASE], eax
607
        mov     [acpi_ioapic_base-OS_BASE], eax
591
        jmp     .next
608
        jmp     .next