Subversion Repositories Kolibri OS

Rev

Rev 9828 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9828 Rev 9900
Line 3... Line 3...
3
;; Copyright (C) KolibriOS team 2004-2022. All rights reserved. ;;
3
;; Copyright (C) KolibriOS team 2004-2022. All rights reserved. ;;
4
;; Distributed under terms of the GNU General Public License    ;;
4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 7... Line 7...
7
 
7
 
Line 8... Line 8...
8
$Revision: 9828 $
8
$Revision: 9900 $
9
 
9
 
10
 
10
 
Line 135... Line 135...
135
        mov     [edi + MEM_BLOCK.next_block], ebx
135
        mov     [edi + MEM_BLOCK.next_block], ebx
136
        mov     [edi + MEM_BLOCK.prev_block], eax
136
        mov     [edi + MEM_BLOCK.prev_block], eax
137
        mov     [edi + MEM_BLOCK.list.next], eax
137
        mov     [edi + MEM_BLOCK.list.next], eax
138
        mov     [edi + MEM_BLOCK.list.prev], eax
138
        mov     [edi + MEM_BLOCK.list.prev], eax
139
        mov     [edi + MEM_BLOCK.base], HEAP_BASE
139
        mov     [edi + MEM_BLOCK.base], HEAP_BASE
140
        mov     [edi + MEM_BLOCK.size], 4096*sizeof.MEM_BLOCK
140
        mov     [edi + MEM_BLOCK.size], PAGE_SIZE * sizeof.MEM_BLOCK
141
        mov     [edi + MEM_BLOCK.flags], MEM_BLOCK_USED
141
        mov     [edi + MEM_BLOCK.flags], MEM_BLOCK_USED
Line 142... Line 142...
142
 
142
 
143
        mov     [ecx + MEM_BLOCK.next_block], eax
143
        mov     [ecx + MEM_BLOCK.next_block], eax
144
        mov     [ecx + MEM_BLOCK.prev_block], ebx
144
        mov     [ecx + MEM_BLOCK.prev_block], ebx
Line 148... Line 148...
148
        mov     [ecx + MEM_BLOCK.size], eax
148
        mov     [ecx + MEM_BLOCK.size], eax
149
        mov     [ecx + MEM_BLOCK.flags], MEM_BLOCK_USED
149
        mov     [ecx + MEM_BLOCK.flags], MEM_BLOCK_USED
Line 150... Line 150...
150
 
150
 
151
        mov     [ebx + MEM_BLOCK.next_block], ecx
151
        mov     [ebx + MEM_BLOCK.next_block], ecx
152
        mov     [ebx + MEM_BLOCK.prev_block], edi
152
        mov     [ebx + MEM_BLOCK.prev_block], edi
Line 153... Line 153...
153
        mov     [ebx + MEM_BLOCK.base], HEAP_BASE + 4096*sizeof.MEM_BLOCK
153
        mov     [ebx + MEM_BLOCK.base], HEAP_BASE + PAGE_SIZE * sizeof.MEM_BLOCK
154
 
154
 
155
        mov     ecx, [pg_data.kernel_pages]
155
        mov     ecx, [pg_data.kernel_pages]
156
        shl     ecx, 12
156
        shl     ecx, 12
157
        sub     ecx, HEAP_BASE-OS_BASE + 4096*sizeof.MEM_BLOCK
157
        sub     ecx, HEAP_BASE-OS_BASE + PAGE_SIZE * sizeof.MEM_BLOCK
158
        mov     [heap_size], ecx
158
        mov     [heap_size], ecx
159
        mov     [heap_free], ecx
159
        mov     [heap_free], ecx
Line 164... Line 164...
164
        mov     [mem_block_mask + 4], 0x80000000
164
        mov     [mem_block_mask + 4], 0x80000000
Line 165... Line 165...
165
 
165
 
166
        mov     ecx, mem_block_list + 63*8
166
        mov     ecx, mem_block_list + 63*8
Line 167... Line 167...
167
        list_add ebx, ecx
167
        list_add ebx, ecx
168
 
168
 
Line 169... Line 169...
169
        mov     ecx, 4096-3-1
169
        mov     ecx, PAGE_SIZE -3-1
170
        mov     eax, HEAP_BASE + sizeof.MEM_BLOCK*4
170
        mov     eax, HEAP_BASE + sizeof.MEM_BLOCK*4
171
 
171
 
Line 266... Line 266...
266
        push    ebx
266
        push    ebx
267
        push    esi
267
        push    esi
268
        push    edi
268
        push    edi
Line 269... Line 269...
269
 
269
 
270
        mov     eax, [size]
270
        mov     eax, [size]
271
        add     eax, 4095
271
        add     eax, PAGE_SIZE-1
272
        and     eax, not 4095
272
        and     eax, -PAGE_SIZE
Line 273... Line 273...
273
        mov     [size], eax
273
        mov     [size], eax
274
 
274
 
Line 459... Line 459...
459
 
459
 
460
        push    ebx
460
        push    ebx
Line 461... Line 461...
461
        push    edi
461
        push    edi
462
 
462
 
463
        mov     eax, [size]
463
        mov     eax, [size]
464
        add     eax, 4095
464
        add     eax, PAGE_SIZE-1
465
        and     eax, not 4095;
465
        and     eax, -PAGE_SIZE;
466
        mov     [size], eax
466
        mov     [size], eax
467
        and     eax, eax
467
        and     eax, eax
468
        jz      .err
468
        jz      .err
Line 500... Line 500...
500
        call    alloc_page
500
        call    alloc_page
501
        test    eax, eax
501
        test    eax, eax
502
        jz      .err
502
        jz      .err
Line 503... Line 503...
503
 
503
 
504
        stdcall map_page, edx, eax, dword (PG_GLOBAL + PG_SWR)
504
        stdcall map_page, edx, eax, dword (PG_GLOBAL + PG_SWR)
505
        add     edx, 0x1000
505
        add     edx, PAGE_SIZE
506
        dec     ebx
506
        dec     ebx
507
        jnz     @B
507
        jnz     @B
508
.end:
508
.end:
509
        mov     eax, [lin_addr]
509
        mov     eax, [lin_addr]
Line 563... Line 563...
563
@@:
563
@@:
564
        lea     ecx, [ebx + PROC.heap_lock]
564
        lea     ecx, [ebx + PROC.heap_lock]
565
        call    mutex_init
565
        call    mutex_init
Line 566... Line 566...
566
 
566
 
567
        mov     esi, [ebx + PROC.mem_used]
567
        mov     esi, [ebx + PROC.mem_used]
568
        add     esi, 4095
568
        add     esi, PAGE_SIZE-1
569
        and     esi, not 4095
569
        and     esi, -PAGE_SIZE
570
        mov     [ebx + PROC.mem_used], esi
570
        mov     [ebx + PROC.mem_used], esi
571
        mov     eax, HEAP_TOP
571
        mov     eax, HEAP_TOP
572
        mov     [ebx + PROC.heap_base], esi
572
        mov     [ebx + PROC.heap_base], esi
Line 590... Line 590...
590
        lea     ecx, [ebx + PROC.heap_lock]
590
        lea     ecx, [ebx + PROC.heap_lock]
591
        call    mutex_lock
591
        call    mutex_lock
Line 592... Line 592...
592
 
592
 
593
        mov     ecx, [alloc_size]
593
        mov     ecx, [alloc_size]
594
        add     ecx, (4095 + PAGE_SIZE)
594
        add     ecx, (4095 + PAGE_SIZE)
595
        and     ecx, not 4095
595
        and     ecx, -PAGE_SIZE
596
        mov     esi, [ebx + PROC.heap_base]
596
        mov     esi, [ebx + PROC.heap_base]
597
        mov     edi, [ebx + PROC.heap_top]
597
        mov     edi, [ebx + PROC.heap_top]
598
  .scan:
598
  .scan:
599
        cmp     esi, edi
599
        cmp     esi, edi
Line 602... Line 602...
602
        mov     ebx, esi
602
        mov     ebx, esi
603
        shr     ebx, 12
603
        shr     ebx, 12
604
        mov     eax, [page_tabs + ebx*4]
604
        mov     eax, [page_tabs + ebx*4]
605
        test    al, MEM_BLOCK_FREE
605
        test    al, MEM_BLOCK_FREE
606
        jz      .test_used
606
        jz      .test_used
607
        and     eax, 0xFFFFF000
607
        and     eax, -PAGE_SIZE
608
        cmp     eax, ecx   ;alloc_size
608
        cmp     eax, ecx   ;alloc_size
609
        jb      .m_next
609
        jb      .m_next
610
        jz      @f
610
        jz      @f
Line 611... Line 611...
611
 
611
 
Line 628... Line 628...
628
        jnz     @B
628
        jnz     @B
629
  .no:
629
  .no:
Line 630... Line 630...
630
 
630
 
631
        mov     edx, [current_process]
631
        mov     edx, [current_process]
632
        mov     ebx, [alloc_size]
632
        mov     ebx, [alloc_size]
633
        add     ebx, 0xFFF
633
        add     ebx, PAGE_SIZE-1
634
        and     ebx, not 0xFFF
634
        and     ebx, -PAGE_SIZE
Line 635... Line 635...
635
        add     [edx + PROC.mem_used], ebx
635
        add     [edx + PROC.mem_used], ebx
636
 
636
 
Line 637... Line 637...
637
        lea     ecx, [edx + PROC.heap_lock]
637
        lea     ecx, [edx + PROC.heap_lock]
Line 638... Line 638...
638
        call    mutex_unlock
638
        call    mutex_unlock
639
 
639
 
640
        lea     eax, [esi + 4096]
640
        lea     eax, [esi + PAGE_SIZE]
641
 
641
 
642
        pop     edi
642
        pop     edi
643
        pop     esi
643
        pop     esi
644
        pop     ebx
644
        pop     ebx
Line 645... Line 645...
645
        ret
645
        ret
646
.test_used:
646
.test_used:
647
        test    al, MEM_BLOCK_USED
647
        test    al, MEM_BLOCK_USED
648
        jz      .m_exit
648
        jz      .m_exit
649
 
649
 
650
        and     eax, 0xFFFFF000 ; not PAGESIZE
650
        and     eax, -PAGE_SIZE
Line 673... Line 673...
673
        mov     ebx, [current_process]
673
        mov     ebx, [current_process]
674
        lea     ecx, [ebx + PROC.heap_lock]
674
        lea     ecx, [ebx + PROC.heap_lock]
675
        call    mutex_lock
675
        call    mutex_lock
Line 676... Line 676...
676
 
676
 
677
        mov     edx, [address]
677
        mov     edx, [address]
678
        and     edx, not 0xFFF
678
        and     edx, -PAGE_SIZE
679
        mov     [address], edx
679
        mov     [address], edx
680
        sub     edx, 0x1000
680
        sub     edx, PAGE_SIZE
681
        jb      .error
681
        jb      .error
682
        mov     esi, [ebx + PROC.heap_base]
682
        mov     esi, [ebx + PROC.heap_base]
683
        mov     edi, [ebx + PROC.heap_top]
683
        mov     edi, [ebx + PROC.heap_top]
684
        cmp     edx, esi
684
        cmp     edx, esi
Line 688... Line 688...
688
        jae     .error
688
        jae     .error
689
        mov     ebx, esi
689
        mov     ebx, esi
690
        shr     ebx, 12
690
        shr     ebx, 12
691
        mov     eax, [page_tabs + ebx*4]
691
        mov     eax, [page_tabs + ebx*4]
692
        mov     ecx, eax
692
        mov     ecx, eax
693
        and     ecx, 0xFFFFF000
693
        and     ecx, -PAGE_SIZE
694
        add     ecx, esi
694
        add     ecx, esi
695
        cmp     edx, ecx
695
        cmp     edx, ecx
696
        jb      .found
696
        jb      .found
697
        mov     esi, ecx
697
        mov     esi, ecx
698
        jmp     .scan
698
        jmp     .scan
Line 709... Line 709...
709
.found:
709
.found:
710
        test    al, MEM_BLOCK_FREE
710
        test    al, MEM_BLOCK_FREE
711
        jz      .error
711
        jz      .error
712
        mov     eax, ecx
712
        mov     eax, ecx
713
        sub     eax, edx
713
        sub     eax, edx
714
        sub     eax, 0x1000
714
        sub     eax, PAGE_SIZE
715
        cmp     eax, [alloc_size]
715
        cmp     eax, [alloc_size]
716
        jb      .error
716
        jb      .error
Line 717... Line 717...
717
 
717
 
718
; Here we have 1 big free block which includes requested area.
718
; Here we have 1 big free block which includes requested area.
Line 727... Line 727...
727
        or      al, MEM_BLOCK_FREE
727
        or      al, MEM_BLOCK_FREE
728
        mov     [page_tabs + ebx*4], eax
728
        mov     [page_tabs + ebx*4], eax
729
  .nofirst:
729
  .nofirst:
730
        mov     eax, [alloc_size]
730
        mov     eax, [alloc_size]
731
        add     eax, 0x1FFF
731
        add     eax, 0x1FFF
732
        and     eax, not 0xFFF
732
        and     eax, -PAGE_SIZE
733
        mov     ebx, edx
733
        mov     ebx, edx
734
        add     edx, eax
734
        add     edx, eax
735
        shr     ebx, 12
735
        shr     ebx, 12
736
        or      al, MEM_BLOCK_USED
736
        or      al, MEM_BLOCK_USED
737
        mov     [page_tabs + ebx*4], eax
737
        mov     [page_tabs + ebx*4], eax
Line 752... Line 752...
752
        mov     [page_tabs + ebx*4], ecx
752
        mov     [page_tabs + ebx*4], ecx
Line 753... Line 753...
753
 
753
 
754
  .nothird:
754
  .nothird:
755
        mov     edx, [current_process]
755
        mov     edx, [current_process]
756
        mov     ebx, [alloc_size]
756
        mov     ebx, [alloc_size]
757
        add     ebx, 0xFFF
757
        add     ebx, PAGE_SIZE-1
758
        and     ebx, not 0xFFF
758
        and     ebx, -PAGE_SIZE
Line 759... Line 759...
759
        add     [edx + PROC.mem_used], ebx
759
        add     [edx + PROC.mem_used], ebx
760
 
760
 
Line 790... Line 790...
790
        test    al, MEM_BLOCK_USED
790
        test    al, MEM_BLOCK_USED
791
        jz      .cantfree
791
        jz      .cantfree
792
        test    al, MEM_BLOCK_DONT_FREE
792
        test    al, MEM_BLOCK_DONT_FREE
793
        jnz     .cantfree
793
        jnz     .cantfree
Line 794... Line 794...
794
 
794
 
795
        and     eax, not 4095
795
        and     eax, -PAGE_SIZE
796
        mov     ecx, eax
796
        mov     ecx, eax
797
        or      al, MEM_BLOCK_FREE
797
        or      al, MEM_BLOCK_FREE
798
        mov     [page_tabs + (esi-1)*4], eax
798
        mov     [page_tabs + (esi-1)*4], eax
799
        sub     ecx, 4096
799
        sub     ecx, PAGE_SIZE
800
        mov     ebx, ecx
800
        mov     ebx, ecx
801
        shr     ecx, 12
801
        shr     ecx, 12
802
        jz      .released
802
        jz      .released
803
  .release:
803
  .release:
Line 871... Line 871...
871
 
871
 
872
        shr     edx, 12
872
        shr     edx, 12
Line 873... Line 873...
873
        lea     edx, [page_tabs + edx*4]  ; unmap offset
873
        lea     edx, [page_tabs + edx*4]  ; unmap offset
874
 
874
 
875
        mov     ecx, [size]
875
        mov     ecx, [size]
Line 876... Line 876...
876
        add     ecx, 4095
876
        add     ecx, PAGE_SIZE-1
877
        shr     ecx, 12                 ; unmap size in pages
877
        shr     ecx, 12                 ; unmap size in pages
878
 
878
 
Line 879... Line 879...
879
        shr     eax, 12                 ; block size  +  1 page
879
        shr     eax, 12                 ; block size  +  1 page
880
        lea     ebx, [ebx + eax*4-4]      ; block end ptr
880
        lea     ebx, [ebx + eax*4-4]      ; block end ptr
Line 881... Line 881...
881
        lea     eax, [edx + ecx*4]        ; unmap end ptr
881
        lea     eax, [edx + ecx*4]        ; unmap end ptr
882
 
882
 
883
        cmp     eax, ebx                ; check for overflow
883
        cmp     eax, ebx                ; check for overflow
Line 884... Line 884...
884
        ja      .error
884
        ja      .error
885
 
885
 
886
        mov     ebx, [offset]
886
        mov     ebx, [offset]
Line 896... Line 896...
896
        mov     dword[edx], MEM_BLOCK_RESERVED
896
        mov     dword[edx], MEM_BLOCK_RESERVED
897
                                        ; mark page as reserved
897
                                        ; mark page as reserved
898
        invlpg  [ebx]                   ; when we start using
898
        invlpg  [ebx]                   ; when we start using
899
        call    free_page               ; empty c-o-w page instead this ?
899
        call    free_page               ; empty c-o-w page instead this ?
900
  @@:
900
  @@:
901
        add     ebx, 4096       ; PAGESIZE?
901
        add     ebx, PAGE_SIZE
902
        add     edx, 4
902
        add     edx, 4
903
        dec     ecx
903
        dec     ecx
904
        jnz     .unmap
904
        jnz     .unmap
Line 905... Line 905...
905
 
905
 
Line 946... Line 946...
946
.next_free:
946
.next_free:
947
        test    bl, MEM_BLOCK_FREE
947
        test    bl, MEM_BLOCK_FREE
948
        jz      .err
948
        jz      .err
949
        and     dword[page_tabs + edx*4], 0
949
        and     dword[page_tabs + edx*4], 0
950
        add     eax, ebx
950
        add     eax, ebx
951
        and     eax, not 4095           ; not (PAGESIZE - 1)    ?
951
        and     eax, -PAGE_SIZE
952
        or      eax, MEM_BLOCK_FREE
952
        or      eax, MEM_BLOCK_FREE
953
        mov     [page_tabs + esi*4], eax
953
        mov     [page_tabs + esi*4], eax
954
        jmp     @B
954
        jmp     @B
955
.exit:
955
.exit:
956
        xor     eax, eax
956
        xor     eax, eax
Line 976... Line 976...
976
        mov     ecx, [current_process]
976
        mov     ecx, [current_process]
977
        lea     ecx, [ecx + PROC.heap_lock]
977
        lea     ecx, [ecx + PROC.heap_lock]
978
        call    mutex_lock
978
        call    mutex_lock
979
        pop     eax
979
        pop     eax
Line 980... Line 980...
980
 
980
 
981
        lea     ecx, [eax - 0x1000]
981
        lea     ecx, [eax - PAGE_SIZE]
982
        shr     ecx, 12
982
        shr     ecx, 12
983
        mov     edx, [page_tabs + ecx*4]
983
        mov     edx, [page_tabs + ecx*4]
984
        test    dl, MEM_BLOCK_USED
984
        test    dl, MEM_BLOCK_USED
985
        jnz     @f
985
        jnz     @f
Line 1020... Line 1020...
1020
.release_done:
1020
.release_done:
1021
        sub     ebx, ecx
1021
        sub     ebx, ecx
1022
        cmp     ebx, 1
1022
        cmp     ebx, 1
1023
        jnz     .nofreeall
1023
        jnz     .nofreeall
1024
        mov     eax, [page_tabs + ecx*4]
1024
        mov     eax, [page_tabs + ecx*4]
1025
        and     eax, not 0xFFF
1025
        and     eax, -PAGE_SIZE
1026
        mov     edx, [current_process]
1026
        mov     edx, [current_process]
1027
        mov     ebx, [edx + PROC.mem_used]
1027
        mov     ebx, [edx + PROC.mem_used]
1028
        sub     ebx, eax
1028
        sub     ebx, eax
1029
        add     ebx, 0x1000
1029
        add     ebx, PAGE_SIZE
1030
        or      al, MEM_BLOCK_FREE
1030
        or      al, MEM_BLOCK_FREE
1031
        mov     [page_tabs + ecx*4], eax
1031
        mov     [page_tabs + ecx*4], eax
1032
        push    esi edi
1032
        push    esi edi
1033
        mov     esi, [edx + PROC.heap_base]
1033
        mov     esi, [edx + PROC.heap_base]
1034
        mov     edi, [edx + PROC.heap_top]
1034
        mov     edi, [edx + PROC.heap_top]
Line 1172... Line 1172...
1172
        inc     esi
1172
        inc     esi
1173
        mov     eax, esi
1173
        mov     eax, esi
1174
        shl     eax, 12
1174
        shl     eax, 12
1175
        push    eax
1175
        push    eax
1176
        mov     eax, [page_tabs + ecx*4]
1176
        mov     eax, [page_tabs + ecx*4]
1177
        and     eax, not 0xFFF
1177
        and     eax, -PAGE_SIZE
1178
        or      al, MEM_BLOCK_FREE
1178
        or      al, MEM_BLOCK_FREE
1179
        sub     edx, ecx
1179
        sub     edx, ecx
1180
        mov     [page_tabs + ecx*4], eax
1180
        mov     [page_tabs + ecx*4], eax
1181
        inc     ecx
1181
        inc     ecx
1182
        dec     ebx
1182
        dec     ebx
Line 1331... Line 1331...
1331
 
1331
 
1332
        mov     ecx, [size]
1332
        mov     ecx, [size]
1333
        test    ecx, ecx
1333
        test    ecx, ecx
Line 1334... Line 1334...
1334
        jz      .fail
1334
        jz      .fail
1335
 
1335
 
1336
        add     ecx, 4095
1336
        add     ecx, PAGE_SIZE-1
Line 1337... Line 1337...
1337
        and     ecx, -4096
1337
        and     ecx, -PAGE_SIZE
1338
        mov     [size], ecx
1338
        mov     [size], ecx
1339
 
1339
 
Line 1436... Line 1436...
1436
        or      edx, [owner_access]
1436
        or      edx, [owner_access]
1437
        shl     edx, 1
1437
        shl     edx, 1
1438
        or      edx, PG_SHARED + PG_UR
1438
        or      edx, PG_SHARED + PG_UR
1439
@@:
1439
@@:
1440
        lodsd
1440
        lodsd
1441
        and     eax, 0xFFFFF000
1441
        and     eax, -PAGE_SIZE
1442
        or      eax, edx
1442
        or      eax, edx
1443
        stosd
1443
        stosd
1444
        loop    @B
1444
        loop    @B
Line 1445... Line 1445...
1445
 
1445