Subversion Repositories Kolibri OS

Rev

Rev 4429 | 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
 
568
        mov     ebx, [current_slot]
4430 Serge 569
        mov     ebx, [ebx+APPDATA.process]
570
        mov     eax, [ebx+PROC.heap_top]
4429 Serge 571
        test    eax, eax
572
        jz      @F
4430 Serge 573
        sub     eax, [ebx+PROC.heap_base]
574
        sub     eax, PAGE_SIZE
4429 Serge 575
        ret
576
@@:
4430 Serge 577
        mov     esi, [ebx+PROC.mem_used]
4429 Serge 578
        add     esi, 4095
579
        and     esi, not 4095
4430 Serge 580
        mov     [ebx+PROC.mem_used], esi
4429 Serge 581
        mov     eax, HEAP_TOP
4430 Serge 582
        mov     [ebx+PROC.heap_base], esi
583
        mov     [ebx+PROC.heap_top], eax
4429 Serge 584
 
585
        sub     eax, esi
586
        shr     esi, 10
587
        mov     ecx, eax
4430 Serge 588
        sub     eax, PAGE_SIZE
4429 Serge 589
        or      ecx, FREE_BLOCK
590
        mov     [page_tabs+esi], ecx
591
        ret
592
endp
593
 
594
align 4
595
proc user_alloc stdcall, alloc_size:dword
596
 
597
        push    ebx
598
        push    esi
599
        push    edi
600
 
601
        mov     ecx, [alloc_size]
4430 Serge 602
        add     ecx, (4095+PAGE_SIZE)
4429 Serge 603
        and     ecx, not 4095
604
 
605
        mov     ebx, [current_slot]
4430 Serge 606
        mov     ebx, [ebx+APPDATA.process]
607
        mov     esi, dword [ebx+PROC.heap_base] ; heap_base
608
        mov     edi, dword [ebx+PROC.heap_top]  ; heap_top
609
.scan:
4429 Serge 610
        cmp     esi, edi
4430 Serge 611
        jae     .m_exit
4429 Serge 612
 
613
        mov     ebx, esi
614
        shr     ebx, 12
615
        mov     eax, [page_tabs+ebx*4]
616
        test    al, FREE_BLOCK
4430 Serge 617
        jz      .test_used
4429 Serge 618
        and     eax, 0xFFFFF000
619
        cmp     eax, ecx   ;alloc_size
4430 Serge 620
        jb      .m_next
4429 Serge 621
        jz      @f
622
 
623
        lea     edx, [esi+ecx]
624
        sub     eax, ecx
625
        or      al, FREE_BLOCK
626
        shr     edx, 12
627
        mov     [page_tabs+edx*4], eax
628
@@:
629
        or      ecx, USED_BLOCK
630
        mov     [page_tabs+ebx*4], ecx
631
        shr     ecx, 12
632
        inc     ebx
633
        dec     ecx
634
        jz      .no
635
@@:
636
        mov     dword [page_tabs+ebx*4], 2
637
        inc     ebx
638
        dec     ecx
639
        jnz     @B
640
.no:
641
 
642
        mov     edx, [current_slot]
4430 Serge 643
        mov     edx, [edx+APPDATA.process]
644
 
4429 Serge 645
        mov     ebx, [alloc_size]
646
        add     ebx, 0xFFF
647
        and     ebx, not 0xFFF
4430 Serge 648
        add     [edx+PROC.mem_used], ebx
4429 Serge 649
 
650
        lea     eax, [esi+4096]
651
 
652
        pop     edi
653
        pop     esi
654
        pop     ebx
655
        ret
4430 Serge 656
.test_used:
4429 Serge 657
        test    al, USED_BLOCK
4430 Serge 658
        jz      .m_exit
4429 Serge 659
 
660
        and     eax, 0xFFFFF000
4430 Serge 661
.m_next:
4429 Serge 662
        add     esi, eax
4430 Serge 663
        jmp     .scan
664
.m_exit:
4429 Serge 665
        xor     eax, eax
666
        pop     edi
667
        pop     esi
668
        pop     ebx
669
        ret
670
endp
671
 
672
align 4
673
proc user_alloc_at stdcall, address:dword, alloc_size:dword
674
 
675
        push    ebx
676
        push    esi
677
        push    edi
678
 
679
        mov     ebx, [current_slot]
4430 Serge 680
        mov     ebx, [ebx+APPDATA.process]
681
 
4429 Serge 682
        mov     edx, [address]
683
        and     edx, not 0xFFF
684
        mov     [address], edx
685
        sub     edx, 0x1000
686
        jb      .error
4430 Serge 687
        mov     esi, [ebx+PROC.heap_base]
688
        mov     edi, [ebx+PROC.heap_top]
4429 Serge 689
        cmp     edx, esi
690
        jb      .error
691
.scan:
692
        cmp     esi, edi
693
        jae     .error
694
        mov     ebx, esi
695
        shr     ebx, 12
696
        mov     eax, [page_tabs+ebx*4]
697
        mov     ecx, eax
698
        and     ecx, 0xFFFFF000
699
        add     ecx, esi
700
        cmp     edx, ecx
701
        jb      .found
702
        mov     esi, ecx
703
        jmp     .scan
704
.error:
705
        xor     eax, eax
706
        pop     edi
707
        pop     esi
708
        pop     ebx
709
        ret
710
.found:
711
        test    al, FREE_BLOCK
712
        jz      .error
713
        mov     eax, ecx
714
        sub     eax, edx
715
        sub     eax, 0x1000
716
        cmp     eax, [alloc_size]
717
        jb      .error
718
 
719
; Here we have 1 big free block which includes requested area.
720
; In general, 3 other blocks must be created instead:
721
; free at [esi, edx);
722
; busy at [edx, edx+0x1000+ALIGN_UP(alloc_size,0x1000));
723
; free at [edx+0x1000+ALIGN_UP(alloc_size,0x1000), ecx)
724
; First or third block (or both) may be absent.
725
        mov     eax, edx
726
        sub     eax, esi
727
        jz      .nofirst
728
        or      al, FREE_BLOCK
729
        mov     [page_tabs+ebx*4], eax
730
.nofirst:
731
        mov     eax, [alloc_size]
732
        add     eax, 0x1FFF
733
        and     eax, not 0xFFF
734
        mov     ebx, edx
735
        add     edx, eax
736
        shr     ebx, 12
737
        or      al, USED_BLOCK
738
        mov     [page_tabs+ebx*4], eax
739
        shr     eax, 12
740
        dec     eax
741
        jz      .second_nofill
742
        inc     ebx
743
.fill:
744
        mov     dword [page_tabs+ebx*4], 2
745
        inc     ebx
746
        dec     eax
747
        jnz     .fill
748
 
749
.second_nofill:
750
        sub     ecx, edx
751
        jz      .nothird
752
        or      cl, FREE_BLOCK
753
        mov     [page_tabs+ebx*4], ecx
754
 
755
.nothird:
4430 Serge 756
        mov     edx, [current_slot]
757
        mov     edx, [edx+APPDATA.process]
4429 Serge 758
 
759
        mov     ebx, [alloc_size]
760
        add     ebx, 0xFFF
761
        and     ebx, not 0xFFF
4430 Serge 762
        add     [edx+PROC.mem_used], ebx
4429 Serge 763
 
764
        mov     eax, [address]
765
 
766
        pop     edi
767
        pop     esi
768
        pop     ebx
769
        ret
770
endp
771
 
772
align 4
773
proc user_free stdcall, base:dword
774
 
775
        push    esi
776
 
777
        mov     esi, [base]
778
        test    esi, esi
779
        jz      .exit
780
 
781
        push    ebx
782
 
783
        xor     ebx, ebx
784
        shr     esi, 12
785
        mov     eax, [page_tabs+(esi-1)*4]
786
        test    al, USED_BLOCK
787
        jz      .cantfree
788
        test    al, DONT_FREE_BLOCK
789
        jnz     .cantfree
790
 
791
        and     eax, not 4095
792
        mov     ecx, eax
793
        or      al, FREE_BLOCK
794
        mov     [page_tabs+(esi-1)*4], eax
795
        sub     ecx, 4096
796
        mov     ebx, ecx
797
        shr     ecx, 12
798
        jz      .released
799
.release:
800
        xor     eax, eax
801
        xchg    eax, [page_tabs+esi*4]
802
        test    al, 1
803
        jz      @F
804
        test    eax, PG_SHARED
805
        jnz     @F
806
        call    free_page
807
        mov     eax, esi
808
        shl     eax, 12
809
        invlpg  [eax]
810
@@:
811
        inc     esi
812
        dec     ecx
813
        jnz     .release
814
 
815
.released:
816
        push    edi
817
 
818
        mov     edx, [current_slot]
4430 Serge 819
        mov     edx, [edx+APPDATA.process]
820
        mov     esi, dword [edx+PROC.heap_base]
821
        mov     edi, dword [edx+PROC.heap_top]
822
        sub     ebx, [edx+PROC.mem_used]
4429 Serge 823
        neg     ebx
4430 Serge 824
        mov     [edx+PROC.mem_used], ebx
4429 Serge 825
        call    user_normalize
826
        pop     edi
827
        pop     ebx
828
        pop     esi
829
        ret
830
.exit:
831
        xor     eax, eax
832
        inc     eax
833
        pop     esi
834
        ret
835
.cantfree:
836
        xor     eax, eax
837
        pop     ebx
838
        pop     esi
839
        ret
840
endp
841
 
842
 
843
align 4
844
proc user_unmap stdcall, base:dword, offset:dword, size:dword
845
 
846
        push    ebx
847
 
848
        mov     ebx, [base]             ; must be valid pointer
849
        test    ebx, ebx
850
        jz      .error
851
 
852
        mov     edx, [offset]           ; check offset
853
        add     edx, ebx                ; must be below 2Gb app limit
854
        js      .error
855
 
856
        shr     ebx, 12                 ; chek block attributes
857
        lea     ebx, [page_tabs+ebx*4]
858
        mov     eax, [ebx-4]            ; block attributes
859
        test    al, USED_BLOCK
860
        jz      .error
861
        test    al, DONT_FREE_BLOCK
862
        jnz     .error
863
 
864
        shr     edx, 12
865
        lea     edx, [page_tabs+edx*4]  ; unmap offset
866
 
867
        mov     ecx, [size]
868
        add     ecx, 4095
869
        shr     ecx, 12                 ; unmap size in pages
870
 
871
        shr     eax, 12                 ; block size + 1 page
872
        lea     ebx, [ebx+eax*4-4]      ; block end ptr
873
        lea     eax, [edx+ecx*4]        ; unmap end ptr
874
 
875
        cmp     eax, ebx                ; check for overflow
876
        ja      .error
877
 
878
        mov     ebx, [offset]
879
        and     ebx, not 4095           ; is it required ?
880
        add     ebx, [base]
881
 
882
.unmap:
883
        mov     eax, [edx]              ; get page addres
884
        test    al, 1                   ; page mapped ?
885
        jz      @F
886
        test    eax, PG_SHARED          ; page shared ?
887
        jnz     @F
888
        mov     [edx], dword 2
889
                                        ; mark page as reserved
890
        invlpg  [ebx]                   ; when we start using
891
        call    free_page               ; empty c-o-w page instead this ?
892
@@:
893
        add     ebx, 4096
894
        add     edx, 4
895
        dec     ecx
896
        jnz     .unmap
897
 
898
        pop     ebx
899
        or      al, 1                   ; return non zero on success
900
        ret
901
.error:
902
        pop     ebx
903
        xor     eax, eax                ; something wrong
904
        ret
905
endp
906
 
907
align 4
908
user_normalize:
909
; in: esi=heap_base, edi=heap_top
910
; out: eax=0 <=> OK
911
; destroys: ebx,edx,esi,edi
912
        shr     esi, 12
913
        shr     edi, 12
914
@@:
915
        mov     eax, [page_tabs+esi*4]
916
        test    al, USED_BLOCK
917
        jz      .test_free
918
        shr     eax, 12
919
        add     esi, eax
920
        jmp     @B
921
.test_free:
922
        test    al, FREE_BLOCK
923
        jz      .err
924
        mov     edx, eax
925
        shr     edx, 12
926
        add     edx, esi
927
        cmp     edx, edi
928
        jae     .exit
929
 
930
        mov     ebx, [page_tabs+edx*4]
931
        test    bl, USED_BLOCK
932
        jz      .next_free
933
 
934
        shr     ebx, 12
935
        add     edx, ebx
936
        mov     esi, edx
937
        jmp     @B
938
.next_free:
939
        test    bl, FREE_BLOCK
940
        jz      .err
941
        and     dword [page_tabs+edx*4], 0
942
        add     eax, ebx
943
        and     eax, not 4095
944
        or      eax, FREE_BLOCK
945
        mov     [page_tabs+esi*4], eax
946
        jmp     @B
947
.exit:
948
        xor     eax, eax
949
        inc     eax
950
        ret
951
.err:
952
        xor     eax, eax
953
        ret
954
 
955
user_realloc:
956
; in: eax = pointer, ebx = new size
957
; out: eax = new pointer or NULL
958
        test    eax, eax
959
        jnz     @f
960
; realloc(NULL,sz) - same as malloc(sz)
961
        push    ebx
962
        call    user_alloc
963
        ret
964
@@:
965
        push    ecx edx
966
        lea     ecx, [eax - 0x1000]
967
        shr     ecx, 12
968
        mov     edx, [page_tabs+ecx*4]
969
        test    dl, USED_BLOCK
970
        jnz     @f
971
; attempt to realloc invalid pointer
972
.ret0:
973
        pop     edx ecx
974
        xor     eax, eax
975
        ret
976
@@:
977
        test    dl, DONT_FREE_BLOCK
978
        jnz     .ret0
979
        add     ebx, 0x1FFF
980
        shr     edx, 12
981
        shr     ebx, 12
982
; edx = allocated size, ebx = new size
983
        add     edx, ecx
984
        add     ebx, ecx
985
        cmp     edx, ebx
986
        jb      .realloc_add
987
; release part of allocated memory
988
.loop:
989
        cmp     edx, ebx
990
        jz      .release_done
991
        dec     edx
992
        xor     eax, eax
993
        xchg    eax, [page_tabs+edx*4]
994
        test    al, 1
995
        jz      .loop
996
        call    free_page
997
        mov     eax, edx
998
        shl     eax, 12
999
        invlpg  [eax]
1000
        jmp     .loop
1001
.release_done:
1002
        sub     ebx, ecx
1003
        cmp     ebx, 1
1004
        jnz     .nofreeall
1005
        mov     eax, [page_tabs+ecx*4]
1006
        and     eax, not 0xFFF
1007
        mov     edx, [current_slot]
4430 Serge 1008
        mov     edx, [edx+APPDATA.process]
1009
        mov     ebx, [edx+PROC.mem_used]
4429 Serge 1010
        sub     ebx, eax
1011
        add     ebx, 0x1000
1012
        or      al, FREE_BLOCK
1013
        mov     [page_tabs+ecx*4], eax
1014
        push    esi edi
4430 Serge 1015
        mov     esi, [edx+PROC.heap_base]
1016
        mov     edi, [edx+PROC.heap_top]
1017
        mov     [edx+PROC.mem_used], ebx
4429 Serge 1018
        call    user_normalize
1019
        pop     edi esi
1020
        jmp     .ret0   ; all freed
1021
.nofreeall:
1022
        sub     edx, ecx
1023
        shl     ebx, 12
1024
        or      ebx, USED_BLOCK
1025
        xchg    [page_tabs+ecx*4], ebx
1026
        shr     ebx, 12
1027
        sub     ebx, edx
1028
        push    ebx ecx edx
1029
        mov     edx, [current_slot]
4430 Serge 1030
        mov     edx, [edx+APPDATA.process]
4429 Serge 1031
        shl     ebx, 12
4430 Serge 1032
        sub     ebx, [edx+PROC.mem_used]
4429 Serge 1033
        neg     ebx
4430 Serge 1034
        mov     [edx+PROC.mem_used], ebx
4429 Serge 1035
        pop     edx ecx ebx
1036
        lea     eax, [ecx+1]
1037
        shl     eax, 12
1038
        push    eax
1039
        add     ecx, edx
1040
        lea     edx, [ecx+ebx]
1041
        shl     ebx, 12
1042
        jz      .ret
1043
        push    esi
1044
        mov     esi, [current_slot]
4430 Serge 1045
        mov     esi, [esi+APPDATA.process]
1046
        mov     esi, [esi+PROC.heap_top]
4429 Serge 1047
        shr     esi, 12
1048
@@:
1049
        cmp     edx, esi
1050
        jae     .merge_done
1051
        mov     eax, [page_tabs+edx*4]
1052
        test    al, USED_BLOCK
1053
        jnz     .merge_done
1054
        and     dword [page_tabs+edx*4], 0
1055
        shr     eax, 12
1056
        add     edx, eax
1057
        shl     eax, 12
1058
        add     ebx, eax
1059
        jmp     @b
1060
.merge_done:
1061
        pop     esi
1062
        or      ebx, FREE_BLOCK
1063
        mov     [page_tabs+ecx*4], ebx
1064
.ret:
1065
        pop     eax edx ecx
1066
        ret
1067
.realloc_add:
1068
; get some additional memory
1069
        mov     eax, [current_slot]
4430 Serge 1070
        mov     eax, [eax+APPDATA.process]
1071
        mov     eax, [eax+PROC.heap_top]
4429 Serge 1072
        shr     eax, 12
1073
        cmp     edx, eax
1074
        jae     .cant_inplace
1075
        mov     eax, [page_tabs+edx*4]
1076
        test    al, FREE_BLOCK
1077
        jz      .cant_inplace
1078
        shr     eax, 12
1079
        add     eax, edx
1080
        sub     eax, ebx
1081
        jb      .cant_inplace
1082
        jz      @f
1083
        shl     eax, 12
1084
        or      al, FREE_BLOCK
1085
        mov     [page_tabs+ebx*4], eax
1086
@@:
1087
        mov     eax, ebx
1088
        sub     eax, ecx
1089
        shl     eax, 12
1090
        or      al, USED_BLOCK
1091
        mov     [page_tabs+ecx*4], eax
1092
        lea     eax, [ecx+1]
1093
        shl     eax, 12
1094
        push    eax
1095
        push    edi
1096
        lea     edi, [page_tabs+edx*4]
1097
        mov     eax, 2
1098
        sub     ebx, edx
1099
        mov     ecx, ebx
1100
        cld
1101
        rep stosd
1102
        pop     edi
1103
        mov     edx, [current_slot]
4430 Serge 1104
        mov     edx, [edx+APPDATA.process]
4429 Serge 1105
        shl     ebx, 12
4430 Serge 1106
        add     [edx+PROC.mem_used], ebx
4429 Serge 1107
        pop     eax edx ecx
1108
        ret
1109
.cant_inplace:
1110
        push    esi edi
1111
        mov     eax, [current_slot]
4430 Serge 1112
        mov     eax, [eax+APPDATA.process]
1113
        mov     esi, [eax+PROC.heap_base]
1114
        mov     edi, [eax+PROC.heap_top]
4429 Serge 1115
        shr     esi, 12
1116
        shr     edi, 12
1117
        sub     ebx, ecx
1118
.find_place:
1119
        cmp     esi, edi
1120
        jae     .place_not_found
1121
        mov     eax, [page_tabs+esi*4]
1122
        test    al, FREE_BLOCK
1123
        jz      .next_place
1124
        shr     eax, 12
1125
        cmp     eax, ebx
1126
        jae     .place_found
1127
        add     esi, eax
1128
        jmp     .find_place
1129
.next_place:
1130
        shr     eax, 12
1131
        add     esi, eax
1132
        jmp     .find_place
1133
.place_not_found:
1134
        pop     edi esi
1135
        jmp     .ret0
1136
.place_found:
1137
        sub     eax, ebx
1138
        jz      @f
1139
        push    esi
1140
        add     esi, ebx
1141
        shl     eax, 12
1142
        or      al, FREE_BLOCK
1143
        mov     [page_tabs+esi*4], eax
1144
        pop     esi
1145
@@:
1146
        mov     eax, ebx
1147
        shl     eax, 12
1148
        or      al, USED_BLOCK
1149
        mov     [page_tabs+esi*4], eax
1150
        inc     esi
1151
        mov     eax, esi
1152
        shl     eax, 12
1153
        push    eax
1154
        mov     eax, [page_tabs+ecx*4]
1155
        and     eax, not 0xFFF
1156
        or      al, FREE_BLOCK
1157
        sub     edx, ecx
1158
        mov     [page_tabs+ecx*4], eax
1159
        inc     ecx
1160
        dec     ebx
1161
        dec     edx
1162
        jz      .no
1163
@@:
1164
        xor     eax, eax
1165
        xchg    eax, [page_tabs+ecx*4]
1166
        mov     [page_tabs+esi*4], eax
1167
        mov     eax, ecx
1168
        shl     eax, 12
1169
        invlpg  [eax]
1170
        inc     esi
1171
        inc     ecx
1172
        dec     ebx
1173
        dec     edx
1174
        jnz     @b
1175
.no:
1176
        push    ebx
1177
        mov     edx, [current_slot]
4430 Serge 1178
        mov     edx, [eax+APPDATA.process]
4429 Serge 1179
        shl     ebx, 12
4430 Serge 1180
        add     [edx+PROC.mem_used], ebx
4429 Serge 1181
        pop     ebx
1182
@@:
1183
        mov     dword [page_tabs+esi*4], 2
1184
        inc     esi
1185
        dec     ebx
1186
        jnz     @b
1187
        pop     eax edi esi edx ecx
1188
        ret
1189
 
1190
 
1191
 
4430 Serge 1192
;;;;;;;;;;;;;;      SHARED MEMORY     ;;;;;;;;;;;;;;;;;
4429 Serge 1193
 
1194
 
1195
; param
1196
;  eax= shm_map object
1197
 
1198
align 4
1199
destroy_smap:
1200
 
1201
        pushfd
1202
        cli
1203
 
1204
        push    esi
1205
        push    edi
1206
 
1207
        mov     edi, eax
1208
        mov     esi, [eax+SMAP.parent]
1209
        test    esi, esi
1210
        jz      .done
1211
 
1212
        lock dec [esi+SMEM.refcount]
1213
        jnz     .done
1214
 
1215
        mov     ecx, [esi+SMEM.bk]
1216
        mov     edx, [esi+SMEM.fd]
1217
 
1218
        mov     [ecx+SMEM.fd], edx
1219
        mov     [edx+SMEM.bk], ecx
1220
 
1221
        stdcall kernel_free, [esi+SMEM.base]
1222
        mov     eax, esi
1223
        call    free
1224
.done:
1225
        mov     eax, edi
1226
        call    destroy_kernel_object
1227
 
1228
        pop     edi
1229
        pop     esi
1230
        popfd
1231
 
1232
        ret
1233
 
1234
E_NOTFOUND      equ  5
1235
E_ACCESS        equ 10
1236
E_NOMEM         equ 30
1237
E_PARAM         equ 33
1238
 
1239
SHM_READ        equ 0
1240
SHM_WRITE       equ 1
1241
 
1242
SHM_ACCESS_MASK equ 3
1243
 
1244
SHM_OPEN        equ (0 shl 2)
1245
SHM_OPEN_ALWAYS equ (1 shl 2)
1246
SHM_CREATE      equ (2 shl 2)
1247
 
1248
SHM_OPEN_MASK   equ (3 shl 2)
1249
 
1250
align 4
1251
proc shmem_open stdcall name:dword, size:dword, access:dword
1252
        locals
1253
           action         dd ?
1254
           owner_access   dd ?
1255
           mapped         dd ?
1256
        endl
1257
 
1258
        push    ebx
1259
        push    esi
1260
        push    edi
1261
 
1262
        mov     [mapped], 0
1263
        mov     [owner_access], 0
1264
 
1265
        pushfd                         ;mutex required
1266
        cli
1267
 
1268
        mov     eax, [access]
1269
        and     eax, SHM_OPEN_MASK
1270
        mov     [action], eax
1271
 
1272
        mov     ebx, [name]
1273
        test    ebx, ebx
1274
        mov     edx, E_PARAM
1275
        jz      .fail
1276
 
1277
        mov     esi, [shmem_list.fd]
1278
align 4
1279
@@:
1280
        cmp     esi, shmem_list
1281
        je      .not_found
1282
 
1283
        lea     edx, [esi+SMEM.name]; link , base, size
1284
        stdcall strncmp, edx, ebx, 32
1285
        test    eax, eax
1286
        je      .found
1287
 
1288
        mov     esi, [esi+SMEM.fd]
1289
        jmp     @B
1290
 
1291
.not_found:
1292
        mov     eax, [action]
1293
 
1294
        cmp     eax, SHM_OPEN
1295
        mov     edx, E_NOTFOUND
1296
        je      .fail
1297
 
1298
        cmp     eax, SHM_CREATE
1299
        mov     edx, E_PARAM
1300
        je      .create_shm
1301
 
1302
        cmp     eax, SHM_OPEN_ALWAYS
1303
        jne     .fail
1304
 
1305
.create_shm:
1306
 
1307
        mov     ecx, [size]
1308
        test    ecx, ecx
1309
        jz      .fail
1310
 
1311
        add     ecx, 4095
1312
        and     ecx, -4096
1313
        mov     [size], ecx
1314
 
1315
        mov     eax, sizeof.SMEM
1316
        call    malloc
1317
        test    eax, eax
1318
        mov     esi, eax
1319
        mov     edx, E_NOMEM
1320
        jz      .fail
1321
 
1322
        stdcall kernel_alloc, [size]
1323
        test    eax, eax
1324
        mov     [mapped], eax
1325
        mov     edx, E_NOMEM
1326
        jz      .cleanup
1327
 
1328
        mov     ecx, [size]
1329
        mov     edx, [access]
1330
        and     edx, SHM_ACCESS_MASK
1331
 
1332
        mov     [esi+SMEM.base], eax
1333
        mov     [esi+SMEM.size], ecx
1334
        mov     [esi+SMEM.access], edx
1335
        mov     [esi+SMEM.refcount], 0
1336
        mov     [esi+SMEM.name+28], 0
1337
 
1338
        lea     eax, [esi+SMEM.name]
1339
        stdcall strncpy, eax, [name], 31
1340
 
1341
        mov     eax, [shmem_list.fd]
1342
        mov     [esi+SMEM.bk], shmem_list
1343
        mov     [esi+SMEM.fd], eax
1344
 
1345
        mov     [eax+SMEM.bk], esi
1346
        mov     [shmem_list.fd], esi
1347
 
1348
        mov     [action], SHM_OPEN
1349
        mov     [owner_access], SHM_WRITE
1350
 
1351
.found:
1352
        mov     eax, [action]
1353
 
1354
        cmp     eax, SHM_CREATE
1355
        mov     edx, E_ACCESS
1356
        je      .exit
1357
 
1358
        cmp     eax, SHM_OPEN
1359
        mov     edx, E_PARAM
1360
        je      .create_map
1361
 
1362
        cmp     eax, SHM_OPEN_ALWAYS
1363
        jne     .fail
1364
 
1365
.create_map:
1366
 
1367
        mov     eax, [access]
1368
        and     eax, SHM_ACCESS_MASK
1369
        cmp     eax, [esi+SMEM.access]
1370
        mov     [access], eax
1371
        mov     edx, E_ACCESS
1372
        ja      .fail
1373
 
1374
        mov     ebx, [CURRENT_TASK]
1375
        shl     ebx, 5
1376
        mov     ebx, [CURRENT_TASK+ebx+4]
1377
        mov     eax, sizeof.SMAP
1378
 
1379
        call    create_kernel_object
1380
        test    eax, eax
1381
        mov     edi, eax
1382
        mov     edx, E_NOMEM
1383
        jz      .fail
1384
 
1385
        inc     [esi+SMEM.refcount]
1386
 
1387
        mov     [edi+SMAP.magic], 'SMAP'
1388
        mov     [edi+SMAP.destroy], destroy_smap
1389
        mov     [edi+SMAP.parent], esi
1390
        mov     [edi+SMAP.base], 0
1391
 
1392
        stdcall user_alloc, [esi+SMEM.size]
1393
        test    eax, eax
1394
        mov     [mapped], eax
1395
        mov     edx, E_NOMEM
1396
        jz      .cleanup2
1397
 
1398
        mov     [edi+SMAP.base], eax
1399
 
1400
        mov     ecx, [esi+SMEM.size]
1401
        mov     [size], ecx
1402
 
1403
        shr     ecx, 12
1404
        shr     eax, 10
1405
 
1406
        mov     esi, [esi+SMEM.base]
1407
        shr     esi, 10
1408
        lea     edi, [page_tabs+eax]
1409
        add     esi, page_tabs
1410
 
1411
        mov     edx, [access]
1412
        or      edx, [owner_access]
1413
        shl     edx, 1
1414
        or      edx, PG_USER+PG_SHARED
1415
@@:
1416
        lodsd
1417
        and     eax, 0xFFFFF000
1418
        or      eax, edx
1419
        stosd
1420
        loop    @B
1421
 
1422
        xor     edx, edx
1423
 
1424
        cmp     [owner_access], 0
1425
        jne     .fail
1426
.exit:
1427
        mov     edx, [size]
1428
.fail:
1429
        mov     eax, [mapped]
1430
 
1431
        popfd
1432
        pop     edi
1433
        pop     esi
1434
        pop     ebx
1435
        ret
1436
.cleanup:
1437
        mov     [size], edx
1438
        mov     eax, esi
1439
        call    free
1440
        jmp     .exit
1441
 
1442
.cleanup2:
1443
        mov     [size], edx
1444
        mov     eax, edi
1445
        call    destroy_smap
1446
        jmp     .exit
1447
endp
1448
 
1449
align 4
1450
proc shmem_close stdcall, name:dword
1451
 
1452
        mov     eax, [name]
1453
        test    eax, eax
1454
        jz      .fail
1455
 
1456
        push    esi
1457
        push    edi
1458
        pushfd
1459
        cli
1460
 
1461
        mov     esi, [current_slot]
1462
        add     esi, APP_OBJ_OFFSET
1463
.next:
1464
        mov     eax, [esi+APPOBJ.fd]
1465
        test    eax, eax
1466
        jz      @F
1467
 
1468
        cmp     eax, esi
1469
        mov     esi, eax
1470
        je      @F
1471
 
1472
        cmp     [eax+SMAP.magic], 'SMAP'
1473
        jne     .next
1474
 
1475
        mov     edi, [eax+SMAP.parent]
1476
        test    edi, edi
1477
        jz      .next
1478
 
1479
        lea     edi, [edi+SMEM.name]
1480
        stdcall strncmp, [name], edi, 32
1481
        test    eax, eax
1482
        jne     .next
1483
 
1484
        stdcall user_free, [esi+SMAP.base]
1485
 
1486
        mov     eax, esi
1487
        call    [esi+APPOBJ.destroy]
1488
@@:
1489
        popfd
1490
        pop     edi
1491
        pop     esi
1492
.fail:
1493
        ret
1494
endp