Subversion Repositories Kolibri OS

Rev

Rev 7136 | Rev 8052 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
7733 dunkaist 3
;; Copyright (C) KolibriOS team 2004-2020. All rights reserved. ;;
2288 clevermous 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
$Revision: 7733 $
9
 
10
align 4
11
proc mem_test
12
; if we have BIOS with fn E820, skip the test
7132 dunkaist 13
        cmp     dword [BOOT_LO.memmap_block_cnt], 0
2288 clevermous 14
        jnz     .ret
15
 
16
        mov     eax, cr0
17
        and     eax, not (CR0_CD+CR0_NW)
18
        or      eax, CR0_CD       ;disable caching
19
        mov     cr0, eax
20
        wbinvd                    ;invalidate cache
21
 
22
        xor     edi, edi
23
        mov     ebx, 'TEST'
24
@@:
25
        add     edi, 0x100000
26
        xchg    ebx, dword [edi]
27
        cmp     dword [edi], 'TEST'
28
        xchg    ebx, dword [edi]
29
        je      @b
30
 
31
        and     eax, not (CR0_CD+CR0_NW) ;enable caching
32
        mov     cr0, eax
7132 dunkaist 33
        inc     dword [BOOT_LO.memmap_block_cnt]
2288 clevermous 34
        xor     eax, eax
7132 dunkaist 35
        mov     [BOOT_LO.memmap_blocks + e820entry.addr.lo], eax
36
        mov     [BOOT_LO.memmap_blocks + e820entry.addr.hi], eax
37
        mov     [BOOT_LO.memmap_blocks + e820entry.size.lo], edi
38
        mov     [BOOT_LO.memmap_blocks + e820entry.size.hi], eax
4390 clevermous 39
        inc     eax
7132 dunkaist 40
        mov     [BOOT_LO.memmap_blocks + e820entry.type], eax
2288 clevermous 41
.ret:
42
        ret
43
endp
44
 
45
align 4
46
proc init_mem
47
; calculate maximum allocatable address and number of allocatable pages
7132 dunkaist 48
        mov     edi, BOOT_LO.memmap_blocks
2288 clevermous 49
        mov     ecx, [edi-4]
50
        xor     esi, esi; esi will hold total amount of memory
51
        xor     edx, edx; edx will hold maximum allocatable address
52
.calcmax:
53
; round all to pages
54
        mov     eax, [edi]
2466 Serge 55
        cmp     [edi+16], byte 1
56
        jne     .unusable
57
 
2288 clevermous 58
        test    eax, 0xFFF
59
        jz      @f
60
        neg     eax
61
        and     eax, 0xFFF
62
        add     [edi], eax
63
        adc     dword [edi+4], 0
64
        sub     [edi+8], eax
65
        sbb     dword [edi+12], 0
66
        jc      .unusable
67
@@:
68
        and     dword [edi+8], not 0xFFF
69
        jz      .unusable
70
; ignore memory after 4 Gb
71
        cmp     dword [edi+4], 0
72
        jnz     .unusable
73
        mov     eax, [edi]
74
        cmp     dword [edi+12], 0
75
        jnz     .overflow
76
        add     eax, [edi+8]
77
        jnc     @f
78
.overflow:
79
        mov     eax, 0xFFFFF000
80
@@:
81
        cmp     edx, eax
82
        jae     @f
83
        mov     edx, eax
84
@@:
85
        sub     eax, [edi]
86
        mov     [edi+8], eax
87
        add     esi, eax
88
        jmp     .usable
89
.unusable:
2466 Serge 90
;        and     dword [edi+8], 0
2288 clevermous 91
.usable:
92
        add     edi, 20
93
        loop    .calcmax
94
.calculated:
95
        mov     [MEM_AMOUNT-OS_BASE], esi
96
        mov     [pg_data.mem_amount-OS_BASE], esi
97
        shr     esi, 12
98
        mov     [pg_data.pages_count-OS_BASE], esi
99
 
100
        shr     edx, 12
101
        add     edx, 31
102
        and     edx, not 31
103
        shr     edx, 3
104
        mov     [pg_data.pagemap_size-OS_BASE], edx
105
 
106
        add     edx, (sys_pgmap-OS_BASE)+4095
107
        and     edx, not 4095
108
        mov     [tmp_page_tabs], edx
109
 
110
        mov     edx, esi
111
        and     edx, -1024
112
        cmp     edx, (OS_BASE/4096)
113
        jbe     @F
114
        mov     edx, (OS_BASE/4096)
115
        jmp     .set
116
@@:
117
        cmp     edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
118
        jae     .set
119
        mov     edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
120
.set:
121
        mov     [pg_data.kernel_pages-OS_BASE], edx
122
        shr     edx, 10
123
        mov     [pg_data.kernel_tables-OS_BASE], edx
124
 
125
        xor     eax, eax
5130 serge 126
        mov     edi, sys_proc-OS_BASE
127
        mov     ecx, 8192/4
2288 clevermous 128
        cld
129
        rep stosd
130
 
5130 serge 131
        mov     edx, (sys_proc-OS_BASE+PROC.pdt_0)+ 0x800; (OS_BASE shr 20)
2288 clevermous 132
        bt      [cpu_caps-OS_BASE], CAPS_PSE
133
        jnc     .no_PSE
134
 
135
        mov     ebx, cr4
136
        or      ebx, CR4_PSE
5356 serge 137
        mov     eax, PDE_LARGE+PG_SWR
2288 clevermous 138
        mov     cr4, ebx
139
        dec     [pg_data.kernel_tables-OS_BASE]
140
 
141
        mov     [edx], eax
142
        add     edx, 4
143
 
144
        mov     edi, [tmp_page_tabs]
145
        jmp     .map_kernel_heap        ; new kernel fits to the first 4Mb - nothing to do with ".map_low"
146
.no_PSE:
5356 serge 147
        mov     eax, PG_SWR
2288 clevermous 148
        mov     ecx, [tmp_page_tabs]
149
        shr     ecx, 12
150
.map_low:
151
        mov     edi, [tmp_page_tabs]
152
@@:                                   ;
153
        stosd
154
        add     eax, 0x1000
155
        dec     ecx
156
        jnz     @B
157
 
158
.map_kernel_heap:
159
        mov     ecx, [pg_data.kernel_tables-OS_BASE]
160
        shl     ecx, 10
161
        xor     eax, eax
162
        rep stosd
163
 
164
        mov     ecx, [pg_data.kernel_tables-OS_BASE]
165
        mov     eax, [tmp_page_tabs]
5356 serge 166
        or      eax, PG_SWR
2288 clevermous 167
        mov     edi, edx
168
 
169
.map_kernel_tabs:
170
        stosd
171
        add     eax, 0x1000
172
        dec     ecx
173
        jnz     .map_kernel_tabs
174
 
5356 serge 175
        mov     dword [sys_proc-OS_BASE+PROC.pdt_0+(page_tabs shr 20)], sys_proc+PROC.pdt_0+PG_SWR-OS_BASE
2288 clevermous 176
 
5130 serge 177
        mov     edi, (sys_proc+PROC.pdt_0-OS_BASE)
2288 clevermous 178
        lea     esi, [edi+(OS_BASE shr 20)]
179
        movsd
180
        movsd
181
        ret
182
endp
183
 
184
align 4
185
proc init_page_map
186
; mark all memory as unavailable
187
        mov     edi, sys_pgmap-OS_BASE
188
        mov     ecx, [pg_data.pagemap_size-OS_BASE]
189
        shr     ecx, 2
190
        xor     eax, eax
191
        cld
192
        rep stosd
193
 
194
; scan through memory map and mark free areas as available
7132 dunkaist 195
        mov     ebx, BOOT_LO.memmap_blocks
2288 clevermous 196
        mov     edx, [ebx-4]
197
.scanmap:
2466 Serge 198
        cmp     [ebx+16], byte 1
199
        jne     .next
200
 
2288 clevermous 201
        mov     ecx, [ebx+8]
202
        shr     ecx, 12; ecx = number of pages
203
        jz      .next
204
        mov     edi, [ebx]
205
        shr     edi, 12; edi = first page
206
        mov     eax, edi
207
        shr     edi, 5
208
        shl     edi, 2
209
        add     edi, sys_pgmap-OS_BASE
210
        and     eax, 31
211
        jz      .startok
212
        add     ecx, eax
213
        sub     ecx, 32
214
        jbe     .onedword
215
        push    ecx
216
        mov     ecx, eax
217
        or      eax, -1
218
        shl     eax, cl
219
        or      [edi], eax
220
        add     edi, 4
221
        pop     ecx
222
.startok:
223
        push    ecx
224
        shr     ecx, 5
225
        or      eax, -1
226
        rep stosd
227
        pop     ecx
228
        and     ecx, 31
229
        neg     eax
230
        shl     eax, cl
231
        dec     eax
232
        or      [edi], eax
233
        jmp     .next
234
.onedword:
235
        add     ecx, 32
236
        sub     ecx, eax
237
@@:
238
        bts     [edi], eax
239
        inc     eax
240
        loop    @b
241
.next:
242
        add     ebx, 20
243
        dec     edx
244
        jnz     .scanmap
245
 
246
; mark kernel memory as allocated (unavaila