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