Rev 2382 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
431 | serge | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
1289 | diamond | 3 | ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;; |
431 | serge | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
164 | serge | 7 | |
2385 | hidnplayr | 8 | $Revision: 2385 $ |
9 | |||
10 | |||
11 | struct MEM_BLOCK |
||
12 | list LHEAD |
||
13 | next_block dd ? ;+8 |
||
14 | prev_block dd ? ;+4 |
||
15 | base dd ? ;+16 |
||
16 | size dd ? ;+20 |
||
17 | flags dd ? ;+24 |
||
18 | handle dd ? ;+28 |
||
19 | ends |
||
20 | |||
21 | FREE_BLOCK equ 4 |
||
22 | USED_BLOCK equ 8 |
||
23 | DONT_FREE_BLOCK equ 10h |
||
24 | |||
25 | |||
26 | block_next equ MEM_BLOCK.next_block |
||
27 | block_prev equ MEM_BLOCK.prev_block |
||
28 | list_fd equ MEM_BLOCK.list.next |
||
2382 | hidnplayr | 29 | list_bk equ MEM_BLOCK.list.prev |
164 | serge | 30 | block_base equ MEM_BLOCK.base |
31 | block_size equ MEM_BLOCK.size |
||
32 | block_flags equ MEM_BLOCK.flags |
||
33 | |||
34 | macro calc_index op |
||
35 | { shr op, 12 |
||
2382 | hidnplayr | 36 | dec op |
37 | cmp op, 63 |
||
38 | jna @f |
||
39 | mov op, 63 |
||
164 | serge | 40 | @@: |
41 | } |
||
42 | |||
2382 | hidnplayr | 43 | align 4 |
44 | md: |
||
45 | .add_to_used: |
||
46 | mov eax, [esi+block_base] |
||
47 | mov ebx, [esi+block_base] |
||
48 | shr ebx, 6 |
||
49 | add eax, ebx |
||
50 | shr ebx, 6 |
||
51 | add eax, ebx |
||
52 | shr eax, 12 |
||
53 | and eax, 63 |
||
54 | inc [mem_hash_cnt+eax*4] |
||
164 | serge | 55 | |
2382 | hidnplayr | 56 | lea ecx, [mem_used_list+eax*8] |
57 | list_add esi, ecx |
||
58 | mov [esi+block_flags], USED_BLOCK |
||
59 | mov eax, [esi+block_size] |
||
60 | sub [heap_free], eax |
||
61 | ret |
||
62 | align 4 |
||
63 | .find_used: |
||
64 | mov ecx, eax |
||
65 | mov ebx, eax |
||
66 | shr ebx, 6 |
||
67 | add ecx, ebx |
||
68 | shr ebx, 6 |
||
69 | add ecx, ebx |
||
70 | shr ecx, 12 |
||
71 | and ecx, 63 |
||
164 | serge | 72 | |
2382 | hidnplayr | 73 | lea ebx, [mem_used_list+ecx*8] |
74 | mov esi, ebx |
||
75 | .next: |
||
76 | mov esi, [esi+list_fd] |
||
77 | cmp esi, ebx |
||
78 | je .fail |
||
164 | serge | 79 | |
2382 | hidnplayr | 80 | cmp eax, [esi+block_base] |
81 | jne .next |
||
164 | serge | 82 | |
2382 | hidnplayr | 83 | ret |
84 | .fail: |
||
85 | xor esi, esi |
||
86 | ret |
||
87 | |||
164 | serge | 88 | align 4 |
2382 | hidnplayr | 89 | .del_from_used: |
90 | call .find_used |
||
91 | test esi, esi |
||
92 | jz .done |
||
164 | serge | 93 | |
2382 | hidnplayr | 94 | cmp [esi+block_flags], USED_BLOCK |
95 | jne .fatal |
||
164 | serge | 96 | |
2382 | hidnplayr | 97 | dec [mem_hash_cnt+ecx*4] |
98 | list_del esi |
||
99 | .done: |
||
100 | ret |
||
101 | .fatal: ;FIXME panic here |
||
102 | xor esi, esi |
||
103 | ret |
||
164 | serge | 104 | |
2382 | hidnplayr | 105 | ;Initial heap state |
106 | ; |
||
107 | ;+heap_size terminator USED_BLOCK |
||
108 | ;+4096*MEM_BLOCK.sizeof free space FREE_BLOCK |
||
109 | ;HEAP_BASE heap_descriptors USED_BLOCK |
||
110 | ; |
||
164 | serge | 111 | |
2382 | hidnplayr | 112 | align 4 |
113 | proc init_kernel_heap |
||
357 | serge | 114 | |
2382 | hidnplayr | 115 | mov ecx, 64 |
116 | mov edi, mem_block_list |
||
117 | @@: |
||
118 | mov eax, edi |
||
119 | stosd |
||
120 | stosd |
||
121 | loop @B |
||
122 | |||
123 | mov ecx, 64 |
||
124 | mov edi, mem_used_list |
||
125 | @@: |
||
126 | mov eax, edi |
||
127 | stosd |
||
128 | stosd |
||
129 | loop @B |
||
130 | |||
131 | stdcall alloc_pages, dword 32 |
||
132 | mov ecx, 32 |
||
133 | mov edx, eax |
||
134 | mov edi, HEAP_BASE |
||
164 | serge | 135 | .l1: |
2382 | hidnplayr | 136 | stdcall map_page, edi, edx, PG_SW |
137 | add edi, 0x1000 |
||
138 | add edx, 0x1000 |
||
139 | dec ecx |
||
2385 | hidnplayr | 140 | jnz .l1 |
141 | |||
142 | mov edi, HEAP_BASE ;descriptors |
||
143 | mov ebx, HEAP_BASE+sizeof.MEM_BLOCK ;free space |
||
144 | mov ecx, HEAP_BASE+sizeof.MEM_BLOCK*2 ;terminator |
||
145 | |||
146 | xor eax, eax |
||
147 | mov [edi+block_next], ebx |
||
2382 | hidnplayr | 148 | mov [edi+block_prev], eax |
2385 | hidnplayr | 149 | mov [edi+list_fd], eax |
150 | mov [edi+list_bk], eax |
||
151 | mov [edi+block_base], HEAP_BASE |
||
152 | mov [edi+block_size], 4096*sizeof.MEM_BLOCK |
||
153 | mov [edi+block_flags], USED_BLOCK |
||
154 | |||
155 | mov [ecx+block_next], eax |
||
2382 | hidnplayr | 156 | mov [ecx+block_prev], ebx |
157 | mov [edi+list_fd], eax |
||
158 | mov [edi+list_bk], eax |
||
159 | mov [edi+block_base], eax |
||
160 | mov [edi+block_size], eax |
||
161 | mov [edi+block_flags], USED_BLOCK |
||
2385 | hidnplayr | 162 | |
163 | mov [ebx+block_next], ecx |
||
164 | mov [ebx+block_prev], edi |
||
165 | mov [ebx+block_base], HEAP_BASE+4096*sizeof.MEM_BLOCK |
||
166 | |||
167 | mov ecx, [pg_data.kernel_pages] |
||
168 | shl ecx, 12 |
||
169 | sub ecx, HEAP_BASE-OS_BASE+4096*sizeof.MEM_BLOCK |
||
170 | mov [heap_size], ecx |
||
171 | mov [heap_free], ecx |
||
172 | mov [ebx+block_size], ecx |
||
2382 | hidnplayr | 173 | mov [ebx+block_flags], FREE_BLOCK |
174 | |||
175 | mov [mem_block_mask], eax |
||
176 | mov [mem_block_mask+4], 0x80000000 |
||
177 | |||
178 | mov ecx, mem_block_list+63*8 |
||
2385 | hidnplayr | 179 | list_add ebx, ecx |
180 | |||
181 | mov ecx, 4096-3-1 |
||
182 | mov eax, HEAP_BASE+sizeof.MEM_BLOCK*4 |
||
183 | |||
184 | mov [next_memblock], HEAP_BASE+sizeof.MEM_BLOCK *3 |
||
185 | @@: |
||
186 | mov [eax-sizeof.MEM_BLOCK], eax |
||
187 | add eax, sizeof.MEM_BLOCK |
||
188 | loop @B |
||
189 | |||
190 | mov [eax-sizeof.MEM_BLOCK], dword 0 |
||
191 | |||
192 | mov ecx, heap_mutex |
||
193 | call mutex_init |
||
2382 | hidnplayr | 194 | mov [heap_blocks], 4094 |
195 | mov [free_blocks], 4093 |
||
196 | ret |
||
164 | serge | 197 | endp |
198 | |||
369 | serge | 199 | ; param |
200 | ; eax= required size |
||
201 | ; |
||
202 | ; retval |
||
203 | ; edi= memory block descriptor |
||
204 | ; ebx= descriptor index |
||
205 | |||
164 | serge | 206 | align 4 |
819 | serge | 207 | get_small_block: |
2382 | hidnplayr | 208 | mov ecx, eax |
209 | shr ecx, 12 |
||
210 | dec ecx |
||
211 | cmp ecx, 63 |
||
212 | jle .get_index |
||
213 | mov ecx, 63 |
||
369 | serge | 214 | .get_index: |
2382 | hidnplayr | 215 | lea esi, [mem_block_mask] |
216 | xor ebx, ebx |
||
217 | or edx, -1 |
||
164 | serge | 218 | |
2382 | hidnplayr | 219 | cmp ecx, 32 |
220 | jb .bit_test |
||
164 | serge | 221 | |
2382 | hidnplayr | 222 | sub ecx, 32 |
223 | add ebx, 32 |
||
224 | add esi, 4 |
||
164 | serge | 225 | .bit_test: |
2382 | hidnplayr | 226 | shl edx, cl |
227 | and edx, [esi] |
||
369 | serge | 228 | .find: |
2382 | hidnplayr | 229 | bsf edi, edx |
230 | jz .high_mask |
||
231 | add ebx, edi |
||
232 | lea ecx, [mem_block_list+ebx*8] |
||
233 | mov edi, ecx |
||
369 | serge | 234 | .next: |
2382 | hidnplayr | 235 | mov edi, [edi+list_fd] |
236 | cmp edi, ecx |
||
237 | je .err |
||
238 | cmp eax, [edi+block_size] |
||
239 | ja .next |
||
240 | ret |
||
369 | serge | 241 | .err: |
2382 | hidnplayr | 242 | xor edi, edi |
243 | ret |
||
164 | serge | 244 | |
2382 | hidnplayr | 245 | .high_mask: |
246 | add esi, 4 |
||
247 | cmp esi, mem_block_mask+8 |
||
248 | jae .err |
||
249 | add ebx, 32 |
||
250 | mov edx, [esi] |
||
251 | jmp .find |
||
164 | serge | 252 | |
253 | |||
819 | serge | 254 | align 4 |
255 | free_mem_block: |
||
2382 | hidnplayr | 256 | mov ebx, [next_memblock] |
257 | mov [eax], ebx |
||
258 | mov [next_memblock], eax |
||
259 | xor ebx, ebx |
||
357 | serge | 260 | |
2382 | hidnplayr | 261 | mov dword [eax+4], ebx |
262 | mov dword [eax+8], ebx |
||
263 | mov dword [eax+12], ebx |
||
264 | mov dword [eax+16], ebx |
||
265 | ; mov dword [eax+20], 0 ;don't clear block size |
||
266 | mov dword [eax+24], ebx |
||
267 | mov dword [eax+28], ebx |
||
268 | inc [free_blocks] |
||
269 | ret |
||
164 | serge | 270 | |
271 | align 4 |
||
272 | proc alloc_kernel_space stdcall, size:dword |
||
273 | local block_ind:DWORD |
||
274 | |||
2382 | hidnplayr | 275 | push ebx |
276 | push esi |
||
277 | push edi |
||
660 | serge | 278 | |
2382 | hidnplayr | 279 | mov eax, [size] |
280 | add eax, 4095 |
||
281 | and eax, not 4095 |
||
282 | mov [size], eax |
||
279 | serge | 283 | |
2382 | hidnplayr | 284 | cmp eax, [heap_free] |
285 | ja .error |
||
279 | serge | 286 | |
2382 | hidnplayr | 287 | mov ecx, heap_mutex |
288 | call mutex_lock |
||
164 | serge | 289 | |
2382 | hidnplayr | 290 | mov eax, [size] |
164 | serge | 291 | |
2382 | hidnplayr | 292 | call get_small_block ; eax |
293 | test edi, edi |
||
294 | jz .error_unlock |
||
164 | serge | 295 | |
2382 | hidnplayr | 296 | cmp [edi+block_flags], FREE_BLOCK |
297 | jne .error_unlock |
||
164 | serge | 298 | |
2382 | hidnplayr | 299 | mov [block_ind], ebx ;index of allocated block |
164 | serge | 300 | |
2382 | hidnplayr | 301 | mov eax, [edi+block_size] |
302 | cmp eax, [size] |
||
303 | je .m_eq_size |
||
164 | serge | 304 | |
2382 | hidnplayr | 305 | mov esi, [next_memblock] ;new memory block |
306 | test esi, esi |
||
307 | jz .error_unlock |
||
164 | serge | 308 | |
2382 | hidnplayr | 309 | dec [free_blocks] |
310 | mov eax, [esi] |
||
311 | mov [next_memblock], eax |
||
164 | serge | 312 | |
2382 | hidnplayr | 313 | mov [esi+block_next], edi |
314 | mov eax, [edi+block_prev] |
||
315 | mov [esi+block_prev], eax |
||
316 | mov [edi+block_prev], esi |
||
317 | mov [esi+list_fd], 0 |
||
318 | mov [esi+list_bk], 0 |
||
319 | mov [eax+block_next], esi |
||
164 | serge | 320 | |
2382 | hidnplayr | 321 | mov ebx, [edi+block_base] |
322 | mov [esi+block_base], ebx |
||
323 | mov edx, [size] |
||
324 | mov [esi+block_size], edx |
||
325 | add [edi+block_base], edx |
||
326 | sub [edi+block_size], edx |
||
164 | serge | 327 | |
2382 | hidnplayr | 328 | mov eax, [edi+block_size] |
329 | calc_index eax |
||
330 | cmp eax, [block_ind] |
||
331 | je .add_used |
||
164 | serge | 332 | |
2382 | hidnplayr | 333 | list_del edi |
334 | |||
335 | mov ecx, [block_ind] |
||
336 | lea edx, [mem_block_list+ecx*8] |
||
337 | cmp edx, [edx] |
||
338 | jnz @f |
||
339 | btr [mem_block_mask], ecx |
||
164 | serge | 340 | @@: |
2382 | hidnplayr | 341 | bts [mem_block_mask], eax |
342 | lea edx, [mem_block_list+eax*8] ;edx= list head |
||
343 | list_add edi, edx |
||
344 | .add_used: |
||
357 | serge | 345 | |
2382 | hidnplayr | 346 | call md.add_to_used |
347 | |||
348 | mov ecx, heap_mutex |
||
349 | call mutex_unlock |
||
350 | mov eax, [esi+block_base] |
||
351 | pop edi |
||
352 | pop esi |
||
353 | pop ebx |
||
354 | ret |
||
355 | |||
164 | serge | 356 | .m_eq_size: |
2382 | hidnplayr | 357 | list_del edi |
358 | lea edx, [mem_block_list+ebx*8] |
||
359 | cmp edx, [edx] |
||
360 | jnz @f |
||
361 | btr [mem_block_mask], ebx |
||
164 | serge | 362 | @@: |
2382 | hidnplayr | 363 | mov esi, edi |
364 | jmp .add_used |
||
357 | serge | 365 | |
2382 | hidnplayr | 366 | .error_unlock: |
367 | mov ecx, heap_mutex |
||
368 | call mutex_unlock |
||
164 | serge | 369 | .error: |
2382 | hidnplayr | 370 | xor eax, eax |
371 | pop edi |
||
372 | pop esi |
||
373 | pop ebx |
||
374 | ret |
||
164 | serge | 375 | endp |
376 | |||
377 | align 4 |
||
321 | diamond | 378 | proc free_kernel_space stdcall uses ebx ecx edx esi edi, base:dword |
279 | serge | 379 | |
2382 | hidnplayr | 380 | mov ecx, heap_mutex |
381 | call mutex_lock |
||
164 | serge | 382 | |
2382 | hidnplayr | 383 | mov eax, [base] |
164 | serge | 384 | |
2382 | hidnplayr | 385 | call md.del_from_used |
386 | test esi, esi |
||
387 | jz .fail |
||
170 | serge | 388 | |
2382 | hidnplayr | 389 | mov eax, [esi+block_size] |
390 | add [heap_free], eax |
||
164 | serge | 391 | |
2382 | hidnplayr | 392 | mov edi, [esi+block_next] |
393 | cmp [edi+block_flags], FREE_BLOCK |
||
394 | jne .prev |
||
164 | serge | 395 | |
2382 | hidnplayr | 396 | list_del edi |
164 | serge | 397 | |
2382 | hidnplayr | 398 | mov edx, [edi+block_next] |
399 | mov [esi+block_next], edx |
||
400 | mov [edx+block_prev], esi |
||
401 | mov ecx, [edi+block_size] |
||
402 | add [esi+block_size], ecx |
||
164 | serge | 403 | |
2382 | hidnplayr | 404 | calc_index ecx |
405 | |||
406 | lea edx, [mem_block_list+ecx*8] |
||
407 | cmp edx, [edx] |
||
408 | jne @F |
||
409 | btr [mem_block_mask], ecx |
||
164 | serge | 410 | @@: |
2382 | hidnplayr | 411 | mov eax, edi |
412 | call free_mem_block |
||
164 | serge | 413 | .prev: |
2382 | hidnplayr | 414 | mov edi, [esi+block_prev] |
415 | cmp [edi+block_flags], FREE_BLOCK |
||
416 | jne .insert |
||
164 | serge | 417 | |
2382 | hidnplayr | 418 | mov edx, [esi+block_next] |
419 | mov [edi+block_next], edx |
||
420 | mov [edx+block_prev], edi |
||
164 | serge | 421 | |
2382 | hidnplayr | 422 | mov eax, esi |
423 | call free_mem_block |
||
164 | serge | 424 | |
2382 | hidnplayr | 425 | mov ecx, [edi+block_size] |
426 | mov eax, [esi+block_size] |
||
427 | add eax, ecx |
||
428 | mov [edi+block_size], eax |
||
164 | serge | 429 | |
2382 | hidnplayr | 430 | calc_index eax ;new index |
431 | calc_index ecx ;old index |
||
432 | cmp eax, ecx |
||
433 | je .m_eq |
||
164 | serge | 434 | |
2382 | hidnplayr | 435 | push ecx |
436 | list_del edi |
||
437 | pop ecx |
||
164 | serge | 438 | |
2382 | hidnplayr | 439 | lea edx, [mem_block_list+ecx*8] |
440 | cmp edx, [edx] |
||
441 | jne .add_block |
||
442 | btr [mem_block_mask], ecx |
||
443 | .add_block: |
||
444 | bts [mem_block_mask], eax |
||
445 | lea edx, [mem_block_list+eax*8] |
||
446 | list_add edi, edx |
||
164 | serge | 447 | .m_eq: |
2382 | hidnplayr | 448 | mov ecx, heap_mutex |
449 | call mutex_unlock |
||
450 | xor eax, eax |
||
451 | not eax |
||
452 | ret |
||
164 | serge | 453 | .insert: |
2382 | hidnplayr | 454 | mov [esi+block_flags], FREE_BLOCK |
455 | mov eax, [esi+block_size] |
||
164 | serge | 456 | calc_index eax |
2382 | hidnplayr | 457 | mov edi, esi |
458 | jmp .add_block |
||
164 | serge | 459 | |
460 | .fail: |
||
2382 | hidnplayr | 461 | mov ecx, heap_mutex |
462 | call mutex_unlock |
||
463 | xor eax, eax |
||
464 | ret |
||
164 | serge | 465 | endp |
466 | |||
467 | align 4 |
||
468 | proc kernel_alloc stdcall, size:dword |
||
469 | locals |
||
470 | lin_addr dd ? |
||
471 | pages_count dd ? |
||
472 | endl |
||
473 | |||
2382 | hidnplayr | 474 | push ebx |
475 | push edi |
||
662 | serge | 476 | |
2382 | hidnplayr | 477 | mov eax, [size] |
478 | add eax, 4095 |
||
479 | and eax, not 4095; |
||
480 | mov [size], eax |
||
481 | and eax, eax |
||
482 | jz .err |
||
483 | mov ebx, eax |
||
484 | shr ebx, 12 |
||
485 | mov [pages_count], ebx |
||
164 | serge | 486 | |
2382 | hidnplayr | 487 | stdcall alloc_kernel_space, eax |
488 | test eax, eax |
||
489 | jz .err |
||
490 | mov [lin_addr], eax |
||
164 | serge | 491 | |
2382 | hidnplayr | 492 | mov ecx, [pages_count] |
493 | mov edx, eax |
||
494 | mov ebx, ecx |
||
164 | serge | 495 | |
2382 | hidnplayr | 496 | shr ecx, 3 |
497 | jz .next |
||
164 | serge | 498 | |
2382 | hidnplayr | 499 | and ebx, not 7 |
500 | push ebx |
||
501 | stdcall alloc_pages, ebx |
||
502 | pop ecx ; yes ecx!!! |
||
503 | and eax, eax |
||
504 | jz .err |
||
164 | serge | 505 | |
2382 | hidnplayr | 506 | mov edi, eax |
507 | mov edx, [lin_addr] |
||
164 | serge | 508 | @@: |
2382 | hidnplayr | 509 | stdcall map_page, edx, edi, dword PG_SW |
510 | add edx, 0x1000 |
||
511 | add edi, 0x1000 |
||
512 | dec ecx |
||
513 | jnz @B |
||
164 | serge | 514 | .next: |
2382 | hidnplayr | 515 | mov ecx, [pages_count] |
516 | and ecx, 7 |
||
517 | jz .end |
||
357 | serge | 518 | @@: |
2382 | hidnplayr | 519 | push ecx |
520 | call alloc_page |
||
521 | pop ecx |
||
522 | test eax, eax |
||
523 | jz .err |
||
164 | serge | 524 | |
2382 | hidnplayr | 525 | stdcall map_page, edx, eax, dword PG_SW |
526 | add edx, 0x1000 |
||
527 | dec ecx |
||
528 | jnz @B |
||
164 | serge | 529 | .end: |
2382 | hidnplayr | 530 | mov eax, [lin_addr] |
531 | pop edi |
||
532 | pop ebx |
||
533 | ret |
||
357 | serge | 534 | .err: |
2382 | hidnplayr | 535 | xor eax, eax |
536 | pop edi |
||
537 | pop ebx |
||
538 | ret |
||
164 | serge | 539 | endp |
540 | |||
541 | align 4 |
||
542 | proc kernel_free stdcall, base:dword |
||
543 | |||
2382 | hidnplayr | 544 | push ebx esi |
279 | serge | 545 | |
2382 | hidnplayr | 546 | mov ecx, heap_mutex |
547 | call mutex_lock |
||
164 | serge | 548 | |
2382 | hidnplayr | 549 | mov eax, [base] |
550 | call md.find_used |
||
164 | serge | 551 | |
2382 | hidnplayr | 552 | mov ecx, heap_mutex |
553 | cmp [esi+block_flags], USED_BLOCK |
||
554 | jne .fail |
||
279 | serge | 555 | |
2382 | hidnplayr | 556 | call mutex_unlock |
557 | |||
558 | mov eax, [esi+block_base] |
||
559 | mov ecx, [esi+block_size] |
||
560 | shr ecx, 12 |
||
561 | call release_pages ;eax, ecx |
||
562 | stdcall free_kernel_space, [base] |
||
563 | pop esi ebx |
||
564 | ret |
||
164 | serge | 565 | .fail: |
2382 | hidnplayr | 566 | call mutex_unlock |
567 | xor eax, eax |
||
568 | pop esi ebx |
||
569 | ret |
||
164 | serge | 570 | endp |
571 | |||
572 | restore block_next |
||
573 | restore block_prev |
||
574 | restore block_list |
||
575 | restore block_base |
||
576 | restore block_size |
||
577 | restore block_flags |
||
578 | |||
579 | ;;;;;;;;;;;;;; USER ;;;;;;;;;;;;;;;;; |
||
580 | |||
188 | serge | 581 | HEAP_TOP equ 0x5FC00000 |
582 | |||
164 | serge | 583 | align 4 |
188 | serge | 584 | proc init_heap |
164 | serge | 585 | |
2382 | hidnplayr | 586 | mov ebx, [current_slot] |
587 | mov eax, [ebx+APPDATA.heap_top] |
||
588 | test eax, eax |
||
589 | jz @F |
||
590 | sub eax, [ebx+APPDATA.heap_base] |
||
591 | sub eax, 4096 |
||
592 | ret |
||
172 | serge | 593 | @@: |
2382 | hidnplayr | 594 | mov esi, [ebx+APPDATA.mem_size] |
595 | add esi, 4095 |
||
596 | and esi, not 4095 |
||
597 | mov [ebx+APPDATA.mem_size], esi |
||
598 | mov eax, HEAP_TOP |
||
599 | mov [ebx+APPDATA.heap_base], esi |
||
600 | mov [ebx+APPDATA.heap_top], eax |
||
164 | serge | 601 | |
2382 | hidnplayr | 602 | sub eax, esi |
603 | shr esi, 10 |
||
604 | mov ecx, eax |
||
605 | sub eax, 4096 |
||
606 | or ecx, FREE_BLOCK |
||
607 | mov [page_tabs+esi], ecx |
||
608 | ret |
||
164 | serge | 609 | endp |
610 | |||
611 | align 4 |
||
612 | proc user_alloc stdcall, alloc_size:dword |
||
613 | |||
2382 | hidnplayr | 614 | push ebx |
615 | push esi |
||
616 | push edi |
||
662 | serge | 617 | |
2382 | hidnplayr | 618 | mov ecx, [alloc_size] |
619 | add ecx, (4095+4096) |
||
620 | and ecx, not 4095 |
||
164 | serge | 621 | |
2382 | hidnplayr | 622 | mov ebx, [current_slot] |
623 | mov esi, dword [ebx+APPDATA.heap_base] ; heap_base |
||
624 | mov edi, dword [ebx+APPDATA.heap_top] ; heap_top |
||
164 | serge | 625 | l_0: |
2382 | hidnplayr | 626 | cmp esi, edi |
627 | jae m_exit |
||
164 | serge | 628 | |
2382 | hidnplayr | 629 | mov ebx, esi |
630 | shr ebx, 12 |
||
631 | mov eax, [page_tabs+ebx*4] |
||
632 | test al, FREE_BLOCK |
||
633 | jz test_used |
||
634 | and eax, 0xFFFFF000 |
||
635 | cmp eax, ecx ;alloc_size |
||
636 | jb m_next |
||
637 | jz @f |
||
164 | serge | 638 | |
2382 | hidnplayr | 639 | lea edx, [esi+ecx] |
640 | sub eax, ecx |
||
641 | or al, FREE_BLOCK |
||
642 | shr edx, 12 |
||
643 | mov [page_tabs+edx*4], eax |
||
294 | diamond | 644 | @@: |
2382 | hidnplayr | 645 | or ecx, USED_BLOCK |
646 | mov [page_tabs+ebx*4], ecx |
||
647 | shr ecx, 12 |
||
648 | inc ebx |
||
649 | dec ecx |
||
650 | jz .no |
||
164 | serge | 651 | @@: |
2382 | hidnplayr | 652 | mov dword [page_tabs+ebx*4], 2 |
653 | inc ebx |
||
654 | dec ecx |
||
655 | jnz @B |
||
620 | diamond | 656 | .no: |
164 | serge | 657 | |
2382 | hidnplayr | 658 | mov edx, [current_slot] |
659 | mov ebx, [alloc_size] |
||
660 | add ebx, 0xFFF |
||
661 | and ebx, not 0xFFF |
||
662 | add ebx, [edx+APPDATA.mem_size] |
||
663 | call update_mem_size |
||
294 | diamond | 664 | |
2382 | hidnplayr | 665 | lea eax, [esi+4096] |
662 | serge | 666 | |
2382 | hidnplayr | 667 | pop edi |
668 | pop esi |
||
669 | pop ebx |
||
670 | ret |
||
164 | serge | 671 | test_used: |
2382 | hidnplayr | 672 | test al, USED_BLOCK |
673 | jz m_exit |
||
164 | serge | 674 | |
2382 | hidnplayr | 675 | and eax, 0xFFFFF000 |
620 | diamond | 676 | m_next: |
2382 | hidnplayr | 677 | add esi, eax |
678 | jmp l_0 |
||
164 | serge | 679 | m_exit: |
2382 | hidnplayr | 680 | xor eax, eax |
681 | pop edi |
||
682 | pop esi |
||
683 | pop ebx |
||
684 | ret |
||
164 | serge | 685 | endp |
686 | |||
687 | align 4 |
||
1289 | diamond | 688 | proc user_alloc_at stdcall, address:dword, alloc_size:dword |
689 | |||
2382 | hidnplayr | 690 | push ebx |
691 | push esi |
||
692 | push edi |
||
1289 | diamond | 693 | |
2382 | hidnplayr | 694 | mov ebx, [current_slot] |
695 | mov edx, [address] |
||
696 | and edx, not 0xFFF |
||
697 | mov [address], edx |
||
698 | sub edx, 0x1000 |
||
699 | jb .error |
||
700 | mov esi, [ebx+APPDATA.heap_base] |
||
701 | mov edi, [ebx+APPDATA.heap_top] |
||
702 | cmp edx, esi |
||
703 | jb .error |
||
1289 | diamond | 704 | .scan: |
2382 | hidnplayr | 705 | cmp esi, edi |
706 | jae .error |
||
707 | mov ebx, esi |
||
708 | shr ebx, 12 |
||
709 | mov eax, [page_tabs+ebx*4] |
||
710 | mov ecx, eax |
||
711 | and ecx, 0xFFFFF000 |
||
712 | add ecx, esi |
||
713 | cmp edx, ecx |
||
714 | jb .found |
||
715 | mov esi, ecx |
||
716 | jmp .scan |
||
1289 | diamond | 717 | .error: |
2382 | hidnplayr | 718 | xor eax, eax |
719 | pop edi |
||
720 | pop esi |
||
721 | pop ebx |
||
722 | ret |
||
1289 | diamond | 723 | .found: |
2382 | hidnplayr | 724 | test al, FREE_BLOCK |
725 | jz .error |
||
726 | mov eax, ecx |
||
727 | sub eax, edx |
||
728 | sub eax, 0x1000 |
||
729 | cmp eax, [alloc_size] |
||
730 | jb .error |
||
1289 | diamond | 731 | |
732 | ; Here we have 1 big free block which includes requested area. |
||
733 | ; In general, 3 other blocks must be created instead: |
||
734 | ; free at [esi, edx); |
||
735 | ; busy at [edx, edx+0x1000+ALIGN_UP(alloc_size,0x1000)); |
||
736 | ; free at [edx+0x1000+ALIGN_UP(alloc_size,0x1000), ecx) |
||
737 | ; First or third block (or both) may be absent. |
||
2382 | hidnplayr | 738 | mov eax, edx |
739 | sub eax, esi |
||
740 | jz .nofirst |
||
741 | or al, FREE_BLOCK |
||
742 | mov [page_tabs+ebx*4], eax |
||
1289 | diamond | 743 | .nofirst: |
2382 | hidnplayr | 744 | mov eax, [alloc_size] |
745 | add eax, 0x1FFF |
||
746 | and eax, not 0xFFF |
||
747 | mov ebx, edx |
||
748 | add edx, eax |
||
749 | shr ebx, 12 |
||
750 | or al, USED_BLOCK |
||
751 | mov [page_tabs+ebx*4], eax |
||
752 | shr eax, 12 |
||
753 | dec eax |
||
754 | jz .second_nofill |
||
755 | inc ebx |
||
1289 | diamond | 756 | .fill: |
2382 | hidnplayr | 757 | mov dword [page_tabs+ebx*4], 2 |
758 | inc ebx |
||
759 | dec eax |
||
760 | jnz .fill |
||
1289 | diamond | 761 | .second_nofill: |
2382 | hidnplayr | 762 | sub ecx, edx |
763 | jz .nothird |
||
764 | or cl, FREE_BLOCK |
||
765 | mov [page_tabs+ebx*4], ecx |
||
1289 | diamond | 766 | .nothird: |
767 | |||
2382 | hidnplayr | 768 | mov edx, [current_slot] |
769 | mov ebx, [alloc_size] |
||
770 | add ebx, 0xFFF |
||
771 | and ebx, not 0xFFF |
||
772 | add ebx, [edx+APPDATA.mem_size] |
||
773 | call update_mem_size |
||
1289 | diamond | 774 | |
2382 | hidnplayr | 775 | mov eax, [address] |
1289 | diamond | 776 | |
2382 | hidnplayr | 777 | pop edi |
778 | pop esi |
||
779 | pop ebx |
||
780 | ret |
||
1289 | diamond | 781 | endp |
782 | |||
783 | align 4 |
||
164 | serge | 784 | proc user_free stdcall, base:dword |
785 | |||
2382 | hidnplayr | 786 | push esi |
662 | serge | 787 | |
2382 | hidnplayr | 788 | mov esi, [base] |
789 | test esi, esi |
||
790 | jz .exit |
||
164 | serge | 791 | |
2382 | hidnplayr | 792 | push ebx |
662 | serge | 793 | |
2382 | hidnplayr | 794 | xor ebx, ebx |
795 | shr esi, 12 |
||
796 | mov eax, [page_tabs+(esi-1)*4] |
||
797 | test al, USED_BLOCK |
||
798 | jz .cantfree |
||
799 | test al, DONT_FREE_BLOCK |
||
800 | jnz .cantfree |
||
164 | serge | 801 | |
2382 | hidnplayr | 802 | and eax, not 4095 |
803 | mov ecx, eax |
||
804 | or al, FREE_BLOCK |
||
805 | mov [page_tabs+(esi-1)*4], eax |
||
806 | sub ecx, 4096 |
||
807 | mov ebx, ecx |
||
808 | shr ecx, 12 |
||
809 | jz .released |
||
164 | serge | 810 | .release: |
2382 | hidnplayr | 811 | xor eax, eax |
812 | xchg eax, [page_tabs+esi*4] |
||
813 | test al, 1 |
||
814 | jz @F |
||
815 | test eax, PG_SHARED |
||
816 | jnz @F |
||
817 | call free_page |
||
818 | mov eax, esi |
||
819 | shl eax, 12 |
||
820 | invlpg [eax] |
||
188 | serge | 821 | @@: |
2382 | hidnplayr | 822 | inc esi |
823 | dec ecx |
||
824 | jnz .release |
||
620 | diamond | 825 | .released: |
2382 | hidnplayr | 826 | push edi |
662 | serge | 827 | |
2382 | hidnplayr | 828 | mov edx, [current_slot] |
829 | mov esi, dword [edx+APPDATA.heap_base] |
||
830 | mov edi, dword [edx+APPDATA.heap_top] |
||
831 | sub ebx, [edx+APPDATA.mem_size] |
||
832 | neg ebx |
||
833 | call update_mem_size |
||
834 | call user_normalize |
||
835 | pop edi |
||
836 | pop ebx |
||
837 | pop esi |
||
838 | ret |
||
448 | diamond | 839 | .exit: |
2382 | hidnplayr | 840 | xor eax, eax |
841 | inc eax |
||
842 | pop esi |
||
843 | ret |
||
546 | diamond | 844 | .cantfree: |
2382 | hidnplayr | 845 | xor eax, eax |
846 | pop ebx |
||
847 | pop esi |
||
848 | ret |
||
448 | diamond | 849 | endp |
850 | |||
851 | user_normalize: |
||
852 | ; in: esi=heap_base, edi=heap_top |
||
853 | ; out: eax=0 <=> OK |
||
854 | ; destroys: ebx,edx,esi,edi |
||
2382 | hidnplayr | 855 | shr esi, 12 |
856 | shr edi, 12 |
||
164 | serge | 857 | @@: |
2382 | hidnplayr | 858 | mov eax, [page_tabs+esi*4] |
859 | test al, USED_BLOCK |
||
860 | jz .test_free |
||
861 | shr eax, 12 |
||
862 | add esi, eax |
||
863 | jmp @B |
||
164 | serge | 864 | .test_free: |
2382 | hidnplayr | 865 | test al, FREE_BLOCK |
866 | jz .err |
||
867 | mov edx, eax |
||
868 | shr edx, 12 |
||
869 | add edx, esi |
||
870 | cmp edx, edi |
||
871 | jae .exit |
||
164 | serge | 872 | |
2382 | hidnplayr | 873 | mov ebx, [page_tabs+edx*4] |
874 | test bl, USED_BLOCK |
||
875 | jz .next_free |
||
164 | serge | 876 | |
2382 | hidnplayr | 877 | shr ebx, 12 |
878 | add edx, ebx |
||
879 | mov esi, edx |
||
880 | jmp @B |
||
164 | serge | 881 | .next_free: |
2382 | hidnplayr | 882 | test bl, FREE_BLOCK |
883 | jz .err |
||
884 | and dword [page_tabs+edx*4], 0 |
||
885 | add eax, ebx |
||
886 | and eax, not 4095 |
||
887 | or eax, FREE_BLOCK |
||
888 | mov [page_tabs+esi*4], eax |
||
889 | jmp @B |
||
164 | serge | 890 | .exit: |
2382 | hidnplayr | 891 | xor eax, eax |
892 | inc eax |
||
893 | ret |
||
164 | serge | 894 | .err: |
2382 | hidnplayr | 895 | xor eax, eax |
896 | ret |
||
164 | serge | 897 | |
448 | diamond | 898 | user_realloc: |
899 | ; in: eax = pointer, ebx = new size |
||
900 | ; out: eax = new pointer or NULL |
||
901 | test eax, eax |
||
902 | jnz @f |
||
903 | ; realloc(NULL,sz) - same as malloc(sz) |
||
904 | push ebx |
||
905 | call user_alloc |
||
906 | ret |
||
907 | @@: |
||
908 | push ecx edx |
||
465 | serge | 909 | lea ecx, [eax - 0x1000] |
448 | diamond | 910 | shr ecx, 12 |
911 | mov edx, [page_tabs+ecx*4] |
||
620 | diamond | 912 | test dl, USED_BLOCK |
448 | diamond | 913 | jnz @f |
914 | ; attempt to realloc invalid pointer |
||
915 | .ret0: |
||
916 | pop edx ecx |
||
917 | xor eax, eax |
||
918 | ret |
||
919 | @@: |
||
620 | diamond | 920 | test dl, DONT_FREE_BLOCK |
546 | diamond | 921 | jnz .ret0 |
448 | diamond | 922 | add ebx, 0x1FFF |
923 | shr edx, 12 |
||
924 | shr ebx, 12 |
||
925 | ; edx = allocated size, ebx = new size |
||
926 | add edx, ecx |
||
927 | add ebx, ecx |
||
928 | cmp edx, ebx |
||
929 | jb .realloc_add |
||
930 | ; release part of allocated memory |
||
931 | .loop: |
||
932 | cmp edx, ebx |
||
933 | jz .release_done |
||
934 | dec edx |
||
935 | xor eax, eax |
||
936 | xchg eax, [page_tabs+edx*4] |
||
937 | test al, 1 |
||
938 | jz .loop |
||
939 | call free_page |
||
940 | mov eax, edx |
||
941 | shl eax, 12 |
||
942 | invlpg [eax] |
||
943 | jmp .loop |
||
944 | .release_done: |
||
945 | sub ebx, ecx |
||
946 | cmp ebx, 1 |
||
947 | jnz .nofreeall |
||
948 | mov eax, [page_tabs+ecx*4] |
||
949 | and eax, not 0xFFF |
||
465 | serge | 950 | mov edx, [current_slot] |
951 | mov ebx, [APPDATA.mem_size+edx] |
||
448 | diamond | 952 | sub ebx, eax |
953 | add ebx, 0x1000 |
||
954 | or al, FREE_BLOCK |
||
955 | mov [page_tabs+ecx*4], eax |
||
956 | push esi edi |
||
465 | serge | 957 | mov esi, [APPDATA.heap_base+edx] |
958 | mov edi, [APPDATA.heap_top+edx] |
||
448 | diamond | 959 | call update_mem_size |
960 | call user_normalize |
||
961 | pop edi esi |
||
962 | jmp .ret0 ; all freed |
||
963 | .nofreeall: |
||
964 | sub edx, ecx |
||
965 | shl ebx, 12 |
||
966 | or ebx, USED_BLOCK |
||
967 | xchg [page_tabs+ecx*4], ebx |
||
968 | shr ebx, 12 |
||
969 | sub ebx, edx |
||
970 | push ebx ecx edx |
||
465 | serge | 971 | mov edx, [current_slot] |
448 | diamond | 972 | shl ebx, 12 |
465 | serge | 973 | sub ebx, [APPDATA.mem_size+edx] |
448 | diamond | 974 | neg ebx |
975 | call update_mem_size |
||
976 | pop edx ecx ebx |
||
465 | serge | 977 | lea eax, [ecx+1] |
448 | diamond | 978 | shl eax, 12 |
979 | push eax |
||
823 | diamond | 980 | add ecx, edx |
981 | lea edx, [ecx+ebx] |
||
448 | diamond | 982 | shl ebx, 12 |
983 | jz .ret |
||
984 | push esi |
||
465 | serge | 985 | mov esi, [current_slot] |
986 | mov esi, [APPDATA.heap_top+esi] |
||
448 | diamond | 987 | shr esi, 12 |
988 | @@: |
||
989 | cmp edx, esi |
||
990 | jae .merge_done |
||
991 | mov eax, [page_tabs+edx*4] |
||
992 | test al, USED_BLOCK |
||
823 | diamond | 993 | jnz .merge_done |
448 | diamond | 994 | and dword [page_tabs+edx*4], 0 |
823 | diamond | 995 | shr eax, 12 |
996 | add edx, eax |
||
997 | shl eax, 12 |
||
448 | diamond | 998 | add ebx, eax |
999 | jmp @b |
||
1000 | .merge_done: |
||
1001 | pop esi |
||
1002 | or ebx, FREE_BLOCK |
||
1003 | mov [page_tabs+ecx*4], ebx |
||
1004 | .ret: |
||
1005 | pop eax edx ecx |
||
1006 | ret |
||
1007 | .realloc_add: |
||
1008 | ; get some additional memory |
||
465 | serge | 1009 | mov eax, [current_slot] |
1010 | mov eax, [APPDATA.heap_top+eax] |
||
448 | diamond | 1011 | shr eax, 12 |
1012 | cmp edx, eax |
||
1013 | jae .cant_inplace |
||
1014 | mov eax, [page_tabs+edx*4] |
||
620 | diamond | 1015 | test al, FREE_BLOCK |
1016 | jz .cant_inplace |
||
448 | diamond | 1017 | shr eax, 12 |
1018 | add eax, edx |
||
620 | diamond | 1019 | sub eax, ebx |
448 | diamond | 1020 | jb .cant_inplace |
1021 | jz @f |
||
1022 | shl eax, 12 |
||
1023 | or al, FREE_BLOCK |
||
1024 | mov [page_tabs+ebx*4], eax |
||
1025 | @@: |
||
1026 | mov eax, ebx |
||
1027 | sub eax, ecx |
||
1028 | shl eax, 12 |
||
1029 | or al, USED_BLOCK |
||
1030 | mov [page_tabs+ecx*4], eax |
||
465 | serge | 1031 | lea eax, [ecx+1] |
448 | diamond | 1032 | shl eax, 12 |
1033 | push eax |
||
1034 | push edi |
||
1035 | lea edi, [page_tabs+edx*4] |
||
1036 | mov eax, 2 |
||
1037 | sub ebx, edx |
||
1038 | mov ecx, ebx |
||
1039 | cld |
||
2382 | hidnplayr | 1040 | rep stosd |
448 | diamond | 1041 | pop edi |
465 | serge | 1042 | mov edx, [current_slot] |
448 | diamond | 1043 | shl ebx, 12 |
465 | serge | 1044 | add ebx, [APPDATA.mem_size+edx] |
448 | diamond | 1045 | call update_mem_size |
1046 | pop eax edx ecx |
||
1047 | ret |
||
1048 | .cant_inplace: |
||
1049 | push esi edi |
||
465 | serge | 1050 | mov eax, [current_slot] |
1051 | mov esi, [APPDATA.heap_base+eax] |
||
1052 | mov edi, [APPDATA.heap_top+eax] |
||
448 | diamond | 1053 | shr esi, 12 |
1054 | shr edi, 12 |
||
1055 | sub ebx, ecx |
||
1056 | .find_place: |
||
1057 | cmp esi, edi |
||
1058 | jae .place_not_found |
||
1059 | mov eax, [page_tabs+esi*4] |
||
1060 | test al, FREE_BLOCK |
||
1061 | jz .next_place |
||
1062 | shr eax, 12 |
||
1063 | cmp eax, ebx |
||
1064 | jae .place_found |
||
1065 | add esi, eax |
||
1066 | jmp .find_place |
||
1067 | .next_place: |
||
1068 | shr eax, 12 |
||
1069 | add esi, eax |
||
1070 | jmp .find_place |
||
1071 | .place_not_found: |
||
1072 | pop edi esi |
||
1073 | jmp .ret0 |
||
1074 | .place_found: |
||
1075 | sub eax, ebx |
||
1076 | jz @f |
||
1077 | push esi |
||
620 | diamond | 1078 | add esi, ebx |
448 | diamond | 1079 | shl eax, 12 |
1080 | or al, FREE_BLOCK |
||
1081 | mov [page_tabs+esi*4], eax |
||
1082 | pop esi |
||
1083 | @@: |
||
1084 | mov eax, ebx |
||
1085 | shl eax, 12 |
||
1086 | or al, USED_BLOCK |
||
1087 | mov [page_tabs+esi*4], eax |
||
1088 | inc esi |
||
1089 | mov eax, esi |
||
1090 | shl eax, 12 |
||
1091 | push eax |
||
1092 | mov eax, [page_tabs+ecx*4] |
||
1093 | and eax, not 0xFFF |
||
1094 | or al, FREE_BLOCK |
||
1095 | sub edx, ecx |
||
1096 | mov [page_tabs+ecx*4], eax |
||
1097 | inc ecx |
||
620 | diamond | 1098 | dec ebx |
1099 | dec edx |
||
1100 | jz .no |
||
448 | diamond | 1101 | @@: |
1102 | xor eax, eax |
||
1103 | xchg eax, [page_tabs+ecx*4] |
||
1104 | mov [page_tabs+esi*4], eax |
||
940 | serge | 1105 | mov eax, ecx |
1106 | shl eax, 12 |
||
1107 | invlpg [eax] |
||
620 | diamond | 1108 | inc esi |
448 | diamond | 1109 | inc ecx |
1110 | dec ebx |
||
1111 | dec edx |
||
1112 | jnz @b |
||
620 | diamond | 1113 | .no: |
448 | diamond | 1114 | push ebx |
465 | serge | 1115 | mov edx, [current_slot] |
448 | diamond | 1116 | shl ebx, 12 |
465 | serge | 1117 | add ebx, [APPDATA.mem_size+edx] |
448 | diamond | 1118 | call update_mem_size |
1119 | pop ebx |
||
1120 | @@: |
||
1121 | mov dword [page_tabs+esi*4], 2 |
||
1122 | inc esi |
||
1123 | dec ebx |
||
1124 | jnz @b |
||
1125 | pop eax edi esi edx ecx |
||
1126 | ret |
||
1127 | |||
278 | serge | 1128 | if 0 |
164 | serge | 1129 | align 4 |
1130 | proc alloc_dll |
||
2382 | hidnplayr | 1131 | pushf |
1132 | cli |
||
1133 | bsf eax, [dll_map] |
||
1134 | jnz .find |
||
1135 | popf |
||
1136 | xor eax, eax |
||
1137 | ret |
||
164 | serge | 1138 | .find: |
2382 | hidnplayr | 1139 | btr [dll_map], eax |
1140 | popf |
||
1141 | shl eax, 5 |
||
1142 | add eax, dll_tab |
||
1143 | ret |
||
164 | serge | 1144 | endp |
1145 | |||
1146 | align 4 |
||
1147 | proc alloc_service |
||
2382 | hidnplayr | 1148 | pushf |
1149 | cli |
||
1150 | bsf eax, [srv_map] |
||
1151 | jnz .find |
||
1152 | popf |
||
1153 | xor eax, eax |
||
1154 | ret |
||
214 | serge | 1155 | .find: |
2382 | hidnplayr | 1156 | btr [srv_map], eax |
1157 | popf |
||
1158 | shl eax, 0x02 |
||
1159 | lea eax, [srv_tab+eax+eax*8] ;srv_tab+eax*36 |
||
1160 | ret |
||
164 | serge | 1161 | endp |
278 | serge | 1162 | |
1163 | end if |
||
940 | serge | 1164 | |
1165 | |||
1166 | ;;;;;;;;;;;;;; SHARED ;;;;;;;;;;;;;;;;; |
||
1167 | |||
1168 | |||
1169 | ; param |
||
1170 | ; eax= shm_map object |
||
1171 | |||
1172 | align 4 |
||
1173 | destroy_smap: |
||
1174 | |||
2382 | hidnplayr | 1175 | pushfd |
1176 | cli |
||
940 | serge | 1177 | |
2382 | hidnplayr | 1178 | push esi |
1179 | push edi |
||
940 | serge | 1180 | |
2382 | hidnplayr | 1181 | mov edi, eax |
1182 | mov esi, [eax+SMAP.parent] |
||
1183 | test esi, esi |
||
1184 | jz .done |
||
940 | serge | 1185 | |
2382 | hidnplayr | 1186 | lock dec [esi+SMEM.refcount] |
1187 | jnz .done |
||
940 | serge | 1188 | |
2382 | hidnplayr | 1189 | mov ecx, [esi+SMEM.bk] |
1190 | mov edx, [esi+SMEM.fd] |
||
945 | serge | 1191 | |
2382 | hidnplayr | 1192 | mov [ecx+SMEM.fd], edx |
1193 | mov [edx+SMEM.bk], ecx |
||
945 | serge | 1194 | |
2382 | hidnplayr | 1195 | stdcall kernel_free, [esi+SMEM.base] |
1196 | mov eax, esi |
||
1197 | call free |
||
945 | serge | 1198 | .done: |
2382 | hidnplayr | 1199 | mov eax, edi |
1200 | call destroy_kernel_object |
||
940 | serge | 1201 | |
2382 | hidnplayr | 1202 | pop edi |
1203 | pop esi |
||
1204 | popfd |
||
940 | serge | 1205 | |
2382 | hidnplayr | 1206 | ret |
940 | serge | 1207 | |
1208 | E_NOTFOUND equ 5 |
||
1209 | E_ACCESS equ 10 |
||
1210 | E_NOMEM equ 30 |
||
1211 | E_PARAM equ 33 |
||
1212 | |||
1213 | SHM_READ equ 0 |
||
1214 | SHM_WRITE equ 1 |
||
1215 | |||
1216 | SHM_ACCESS_MASK equ 3 |
||
1217 | |||
1218 | SHM_OPEN equ (0 shl 2) |
||
1219 | SHM_OPEN_ALWAYS equ (1 shl 2) |
||
1220 | SHM_CREATE equ (2 shl 2) |
||
1221 | |||
1222 | SHM_OPEN_MASK equ (3 shl 2) |
||
1223 | |||
1224 | align 4 |
||
1225 | proc shmem_open stdcall name:dword, size:dword, access:dword |
||
1226 | locals |
||
1227 | action dd ? |
||
1228 | owner_access dd ? |
||
1229 | mapped dd ? |
||
1230 | endl |
||
1231 | |||
2382 | hidnplayr | 1232 | push ebx |
1233 | push esi |
||
1234 | push edi |
||
940 | serge | 1235 | |
2382 | hidnplayr | 1236 | mov [mapped], 0 |
1237 | mov [owner_access], 0 |
||
940 | serge | 1238 | |
2382 | hidnplayr | 1239 | pushfd ;mutex required |
1240 | cli |
||
940 | serge | 1241 | |
2382 | hidnplayr | 1242 | mov eax, [access] |
1243 | and eax, SHM_OPEN_MASK |
||
1244 | mov [action], eax |
||
940 | serge | 1245 | |
2382 | hidnplayr | 1246 | mov ebx, [name] |
1247 | test ebx, ebx |
||
1248 | mov edx, E_PARAM |
||
1249 | jz .fail |
||
940 | serge | 1250 | |
2382 | hidnplayr | 1251 | mov esi, [shmem_list.fd] |
940 | serge | 1252 | align 4 |
1253 | @@: |
||
2382 | hidnplayr | 1254 | cmp esi, shmem_list |
1255 | je .not_found |
||
940 | serge | 1256 | |
2382 | hidnplayr | 1257 | lea edx, [esi+SMEM.name]; link , base, size |
1258 | stdcall strncmp, edx, ebx, 32 |
||
1259 | test eax, eax |
||
1260 | je .found |
||
940 | serge | 1261 | |
2382 | hidnplayr | 1262 | mov esi, [esi+SMEM.fd] |
1263 | jmp @B |
||
940 | serge | 1264 | |
1265 | .not_found: |
||
2382 | hidnplayr | 1266 | mov eax, [action] |
940 | serge | 1267 | |
2382 | hidnplayr | 1268 | cmp eax, SHM_OPEN |
1269 | mov edx, E_NOTFOUND |
||
1270 | je .fail |
||
940 | serge | 1271 | |
2382 | hidnplayr | 1272 | cmp eax, SHM_CREATE |
1273 | mov edx, E_PARAM |
||
1274 | je .create_shm |
||
940 | serge | 1275 | |
2382 | hidnplayr | 1276 | cmp eax, SHM_OPEN_ALWAYS |
1277 | jne .fail |
||
940 | serge | 1278 | |
1279 | .create_shm: |
||
1280 | |||
2382 | hidnplayr | 1281 | mov ecx, [size] |
1282 | test ecx, ecx |
||
1283 | jz .fail |
||
940 | serge | 1284 | |
2382 | hidnplayr | 1285 | add ecx, 4095 |
2385 | hidnplayr | 1286 | and ecx, -4096 |
1287 | mov [size], ecx |
||
1288 | |||
1289 | mov eax, sizeof.SMEM |
||
1290 | call malloc |
||
1291 | test eax, eax |
||
1292 | mov esi, eax |
||
2382 | hidnplayr | 1293 | mov edx, E_NOMEM |
1294 | jz .fail |
||
940 | serge | 1295 | |
2382 | hidnplayr | 1296 | stdcall kernel_alloc, [size] |
1297 | test eax, eax |
||
1298 | mov [mapped], eax |
||
1299 | mov edx, E_NOMEM |
||
1300 | jz .cleanup |
||
940 | serge | 1301 | |
2382 | hidnplayr | 1302 | mov ecx, [size] |
1303 | mov edx, [access] |
||
1304 | and edx, SHM_ACCESS_MASK |
||
940 | serge | 1305 | |
2382 | hidnplayr | 1306 | mov [esi+SMEM.base], eax |
1307 | mov [esi+SMEM.size], ecx |
||
1308 | mov [esi+SMEM.access], edx |
||
1309 | mov [esi+SMEM.refcount], 0 |
||
1310 | mov [esi+SMEM.name+28], 0 |
||
940 | serge | 1311 | |
2382 | hidnplayr | 1312 | lea eax, [esi+SMEM.name] |
1313 | stdcall strncpy, eax, [name], 31 |
||
940 | serge | 1314 | |
2382 | hidnplayr | 1315 | mov eax, [shmem_list.fd] |
1316 | mov [esi+SMEM.bk], shmem_list |
||
1317 | mov [esi+SMEM.fd], eax |
||
940 | serge | 1318 | |
2382 | hidnplayr | 1319 | mov [eax+SMEM.bk], esi |
1320 | mov [shmem_list.fd], esi |
||
940 | serge | 1321 | |
2382 | hidnplayr | 1322 | mov [action], SHM_OPEN |
1323 | mov [owner_access], SHM_WRITE |
||
940 | serge | 1324 | |
1325 | .found: |
||
2382 | hidnplayr | 1326 | mov eax, [action] |
940 | serge | 1327 | |
2382 | hidnplayr | 1328 | cmp eax, SHM_CREATE |
1329 | mov edx, E_ACCESS |
||
1330 | je .exit |
||
940 | serge | 1331 | |
2382 | hidnplayr | 1332 | cmp eax, SHM_OPEN |
1333 | mov edx, E_PARAM |
||
1334 | je .create_map |
||
940 | serge | 1335 | |
2382 | hidnplayr | 1336 | cmp eax, SHM_OPEN_ALWAYS |
1337 | jne .fail |
||
940 | serge | 1338 | |
1339 | .create_map: |
||
1340 | |||
2382 | hidnplayr | 1341 | mov eax, [access] |
1342 | and eax, SHM_ACCESS_MASK |
||
1343 | cmp eax, [esi+SMEM.access] |
||
1344 | mov [access], eax |
||
1345 | mov edx, E_ACCESS |
||
1346 | ja .fail |
||
940 | serge | 1347 | |
2385 | hidnplayr | 1348 | mov ebx, [CURRENT_TASK] |
1349 | shl ebx, 5 |
||
1350 | mov ebx, [CURRENT_TASK+ebx+4] |
||
1351 | mov eax, sizeof.SMAP |
||
1352 | |||
1353 | call create_kernel_object |
||
1354 | test eax, eax |
||
2382 | hidnplayr | 1355 | mov edi, eax |
1356 | mov edx, E_NOMEM |
||
1357 | jz .fail |
||
940 | serge | 1358 | |
2382 | hidnplayr | 1359 | inc [esi+SMEM.refcount] |
1213 | serge | 1360 | |
2382 | hidnplayr | 1361 | mov [edi+SMAP.magic], 'SMAP' |
1362 | mov [edi+SMAP.destroy], destroy_smap |
||
1363 | mov [edi+SMAP.parent], esi |
||
1364 | mov [edi+SMAP.base], 0 |
||
940 | serge | 1365 | |
2382 | hidnplayr | 1366 | stdcall user_alloc, [esi+SMEM.size] |
1367 | test eax, eax |
||
1368 | mov [mapped], eax |
||
1369 | mov edx, E_NOMEM |
||
1370 | jz .cleanup2 |
||
940 | serge | 1371 | |
2382 | hidnplayr | 1372 | mov [edi+SMAP.base], eax |
940 | serge | 1373 | |
2382 | hidnplayr | 1374 | mov ecx, [esi+SMEM.size] |
1375 | mov [size], ecx |
||
940 | serge | 1376 | |
2382 | hidnplayr | 1377 | shr ecx, 12 |
1378 | shr eax, 10 |
||
940 | serge | 1379 | |
2382 | hidnplayr | 1380 | mov esi, [esi+SMEM.base] |
1381 | shr esi, 10 |
||
1382 | lea edi, [page_tabs+eax] |
||
1383 | add esi, page_tabs |
||
940 | serge | 1384 | |
2382 | hidnplayr | 1385 | mov edx, [access] |
1386 | or edx, [owner_access] |
||
1387 | shl edx, 1 |
||
1388 | or edx, PG_USER+PG_SHARED |
||
940 | serge | 1389 | @@: |
2382 | hidnplayr | 1390 | lodsd |
1391 | and eax, 0xFFFFF000 |
||
1392 | or eax, edx |
||
1393 | stosd |
||
1394 | loop @B |
||
940 | serge | 1395 | |
2382 | hidnplayr | 1396 | xor edx, edx |
940 | serge | 1397 | |
2382 | hidnplayr | 1398 | cmp [owner_access], 0 |
1399 | jne .fail |
||
1213 | serge | 1400 | .exit: |
2382 | hidnplayr | 1401 | mov edx, [size] |
1213 | serge | 1402 | .fail: |
2382 | hidnplayr | 1403 | mov eax, [mapped] |
940 | serge | 1404 | |
2382 | hidnplayr | 1405 | popfd |
1406 | pop edi |
||
1407 | pop esi |
||
1408 | pop ebx |
||
1409 | ret |
||
940 | serge | 1410 | .cleanup: |
2382 | hidnplayr | 1411 | mov [size], edx |
1412 | mov eax, esi |
||
1413 | call free |
||
1414 | jmp .exit |
||
940 | serge | 1415 | |
1416 | .cleanup2: |
||
2382 | hidnplayr | 1417 | mov [size], edx |
1418 | mov eax, edi |
||
1419 | call destroy_smap |
||
1420 | jmp .exit |
||
940 | serge | 1421 | endp |
943 | serge | 1422 | |
1423 | align 4 |
||
1424 | proc shmem_close stdcall, name:dword |
||
1425 | |||
2382 | hidnplayr | 1426 | mov eax, [name] |
1427 | test eax, eax |
||
1428 | jz .fail |
||
943 | serge | 1429 | |
2382 | hidnplayr | 1430 | push esi |
1431 | push edi |
||
1432 | pushfd |
||
1433 | cli |
||
943 | serge | 1434 | |
2382 | hidnplayr | 1435 | mov esi, [current_slot] |
1436 | add esi, APP_OBJ_OFFSET |
||
943 | serge | 1437 | .next: |
2382 | hidnplayr | 1438 | mov eax, [esi+APPOBJ.fd] |
1439 | test eax, eax |
||
1440 | jz @F |
||
943 | serge | 1441 | |
2382 | hidnplayr | 1442 | cmp eax, esi |
1443 | mov esi, eax |
||
1444 | je @F |
||
943 | serge | 1445 | |
2382 | hidnplayr | 1446 | cmp [eax+SMAP.magic], 'SMAP' |
1447 | jne .next |
||
943 | serge | 1448 | |
2382 | hidnplayr | 1449 | mov edi, [eax+SMAP.parent] |
1450 | test edi, edi |
||
1451 | jz .next |
||
943 | serge | 1452 | |
2382 | hidnplayr | 1453 | lea edi, [edi+SMEM.name] |
1454 | stdcall strncmp, [name], edi, 32 |
||
1455 | test eax, eax |
||
1456 | jne .next |
||
943 | serge | 1457 | |
2382 | hidnplayr | 1458 | stdcall user_free, [esi+SMAP.base] |
945 | serge | 1459 | |
2382 | hidnplayr | 1460 | mov eax, esi |
1461 | call [esi+APPOBJ.destroy] |
||
943 | serge | 1462 | @@: |
2382 | hidnplayr | 1463 | popfd |
1464 | pop edi |
||
1465 | pop esi |
||
943 | serge | 1466 | .fail: |
2382 | hidnplayr | 1467 | ret |
943 | serge | 1468 | endp=> |