kolibri-process:protect user heap with the mutex

git-svn-id: svn://kolibrios.org@4434 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Sergey Semyonov (Serge) 2014-01-10 03:59:17 +00:00
parent 5647b8ca46
commit a0e9094c8d
3 changed files with 104 additions and 51 deletions

View File

@ -159,7 +159,7 @@ FirstDataSector dw 0 ; begin of data
; ;
;========================================================================= ;=========================================================================
include 'bootvesa.inc' ;Include source for boot vesa include 'bootvesa.inc' ;Include source for boot vesa
if defined extended_primary_loader if defined extended_primary_loader
include 'parsers.inc' include 'parsers.inc'
end if end if
@ -649,7 +649,7 @@ end if
cmp byte [di+preboot_dma-preboot_device], 1 cmp byte [di+preboot_dma-preboot_device], 1
adc byte [di+preboot_dma-preboot_device], 0 adc byte [di+preboot_dma-preboot_device], 0
cmp byte [di+preboot_launcher-preboot_device], 1 ; Start LAUNCHER by default cmp byte [di+preboot_launcher-preboot_device], 1 ; Start LAUNCHER by default
adc byte [di+preboot_launcher-preboot_device], 0 adc byte [di+preboot_launcher-preboot_device], 0
; cmp byte [di+preboot_biosdisk-preboot_device], 1 ; cmp byte [di+preboot_biosdisk-preboot_device], 1
; adc byte [di+preboot_biosdisk-preboot_device], 0 ; adc byte [di+preboot_biosdisk-preboot_device], 0
;; default value for VRR is OFF ;; default value for VRR is OFF
@ -793,7 +793,7 @@ if defined extended_primary_loader
_ask_question bdev,'12',preboot_device ; range accepted for answer: 1-2 _ask_question bdev,'12',preboot_device ; range accepted for answer: 1-2
else else
_ask_question bdev,'14',preboot_device ; range accepted for answer: 1-4 _ask_question bdev,'14',preboot_device ; range accepted for answer: 1-4
end if end if
_setcursor 14,0 _setcursor 14,0
.d: .d:
@ -891,7 +891,7 @@ end if
push word [es:bp+6] push word [es:bp+6]
pop word [number_vm] pop word [number_vm]
mov word [preboot_graph], bp ;save choose mov word [preboot_graph], bp ;save choose
jmp .d jmp .d
.change_b: ; b) preboot_biosdisk = use BIOS disks through V86 emulation? .change_b: ; b) preboot_biosdisk = use BIOS disks through V86 emulation?
@ -1101,7 +1101,7 @@ end if
; Start the first app (right now it's LAUNCHER) after kernel is loaded? ; Start the first app (right now it's LAUNCHER) after kernel is loaded?
mov al, [preboot_launcher] mov al, [preboot_launcher]
mov [es:BOOT_LAUNCHER_START], al ;// 0x901D mov [es:BOOT_LAUNCHER_START], al ;// 0x901D
; BOOT DEVICE ; BOOT DEVICE
@ -1148,7 +1148,7 @@ include '../detect/biosmem.inc'
; TODO: !!!! read only first sector and set variables !!!!! ; TODO: !!!! read only first sector and set variables !!!!!
; ... ; ...
; TODO: !!! then read flippy image track by track ; TODO: !!! then read flippy image track by track
mov cx, 0x0001 ; startcyl,startsector mov cx, 0x0001 ; startcyl,startsector
.a1: .a1:
push cx dx push cx dx
@ -1240,7 +1240,7 @@ include '../detect/biosmem.inc'
; and copy them to extended memory ; and copy them to extended memory
mov si, movedesc mov si, movedesc
mov [si+8*2+3], bh ; from mov [si+8*2+3], bh ; from
mov ax, word [BPB_BytsPerSec] mov ax, word [BPB_BytsPerSec]
shr ax, 1 ; words per sector shr ax, 1 ; words per sector
mov cx, word [BPB_RsvdSecCnt] mov cx, word [BPB_RsvdSecCnt]
@ -1248,7 +1248,7 @@ include '../detect/biosmem.inc'
mul cx mul cx
push ax ; save to stack count of words in boot+FAT push ax ; save to stack count of words in boot+FAT
xchg ax, cx xchg ax, cx
push es push es
push ds push ds
pop es pop es
@ -1274,7 +1274,7 @@ sayerr_memmove:
; TODO: BPB_NumFATs !!!!! ; TODO: BPB_NumFATs !!!!!
add bx, word [BPB_BytsPerSec] ; !!! TODO: may be need multiply by BPB_RsvdSecCnt !!! add bx, word [BPB_BytsPerSec] ; !!! TODO: may be need multiply by BPB_RsvdSecCnt !!!
mov byte [si+8*2+3], bh ; bx - begin of FAT mov byte [si+8*2+3], bh ; bx - begin of FAT
mov ax, word [BPB_BytsPerSec] mov ax, word [BPB_BytsPerSec]
shr ax, 1 ; words per sector shr ax, 1 ; words per sector
mov cx, word [BPB_FATSz16] mov cx, word [BPB_FATSz16]
@ -1289,12 +1289,12 @@ sayerr_memmove:
pop es pop es
test ah, ah test ah, ah
jnz sayerr_floppy jnz sayerr_floppy
mov ax, cx mov ax, cx
shl ax, 1 shl ax, 1
and eax, 0ffffh ; ax - count of bytes in FAT and eax, 0ffffh ; ax - count of bytes in FAT
add dword [si+8*3+2], eax add dword [si+8*3+2], eax
; reading RootDir ; reading RootDir
; TODO: BPB_NumFATs ; TODO: BPB_NumFATs
add bx, ax add bx, ax
@ -1319,7 +1319,7 @@ sayerr_memmove:
pop bx pop bx
push bx push bx
add word [FirstDataSector], bx ; Begin of data region of floppy add word [FirstDataSector], bx ; Begin of data region of floppy
; read RootDir ; read RootDir
call conv_abs_to_THS call conv_abs_to_THS
pop ax pop ax
@ -1518,3 +1518,29 @@ setgr:
gmok2: gmok2:
push ds push ds
pop es pop es
sidt [cs:old_ints_h]
cli ; disable all irqs
mov al, 255 ; mask all irqs
out 0xa1, al
out 0x21, al
l.5:
in al, 0x64 ; Enable A20
test al, 2
jnz l.5
mov al, 0xD1
out 0x64, al
l.6:
in al, 0x64
test al, 2
jnz l.6
mov al, 0xDF
out 0x60, al
l.7:
in al, 0x64
test al, 2
jnz l.7
mov al, 0xFF
out 0x64, al

View File

@ -558,7 +558,7 @@ restore block_base
restore block_size restore block_size
restore block_flags restore block_flags
;;;;;;;;;;;;;; USER ;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;; USER HEAP ;;;;;;;;;;;;;;;;;
HEAP_TOP equ 0x80000000 HEAP_TOP equ 0x80000000
@ -573,6 +573,9 @@ proc init_heap
sub eax, PAGE_SIZE sub eax, PAGE_SIZE
ret ret
@@: @@:
lea ecx, [ebx+PROC.heap_lock]
call mutex_init
mov esi, [ebx+PROC.mem_used] mov esi, [ebx+PROC.mem_used]
add esi, 4095 add esi, 4095
and esi, not 4095 and esi, not 4095
@ -597,11 +600,14 @@ proc user_alloc stdcall, alloc_size:dword
push esi push esi
push edi push edi
mov ebx, [current_process]
lea ecx, [ebx+PROC.heap_lock]
call mutex_lock
mov ecx, [alloc_size] mov ecx, [alloc_size]
add ecx, (4095+PAGE_SIZE) add ecx, (4095+PAGE_SIZE)
and ecx, not 4095 and ecx, not 4095
mov ebx, [current_process]
mov esi, dword [ebx+PROC.heap_base] ; heap_base mov esi, dword [ebx+PROC.heap_base] ; heap_base
mov edi, dword [ebx+PROC.heap_top] ; heap_top mov edi, dword [ebx+PROC.heap_top] ; heap_top
.scan: .scan:
@ -643,6 +649,9 @@ proc user_alloc stdcall, alloc_size:dword
and ebx, not 0xFFF and ebx, not 0xFFF
add [edx+PROC.mem_used], ebx add [edx+PROC.mem_used], ebx
lea ecx, [edx+PROC.heap_lock]
call mutex_unlock
lea eax, [esi+4096] lea eax, [esi+4096]
pop edi pop edi
@ -658,6 +667,10 @@ proc user_alloc stdcall, alloc_size:dword
add esi, eax add esi, eax
jmp .scan jmp .scan
.m_exit: .m_exit:
mov ecx, [current_process]
lea ecx, [ecx+PROC.heap_lock]
call mutex_unlock
xor eax, eax xor eax, eax
pop edi pop edi
pop esi pop esi
@ -673,6 +686,8 @@ proc user_alloc_at stdcall, address:dword, alloc_size:dword
push edi push edi
mov ebx, [current_process] mov ebx, [current_process]
lea ecx, [ebx+PROC.heap_lock]
call mutex_lock
mov edx, [address] mov edx, [address]
and edx, not 0xFFF and edx, not 0xFFF
@ -697,6 +712,10 @@ proc user_alloc_at stdcall, address:dword, alloc_size:dword
mov esi, ecx mov esi, ecx
jmp .scan jmp .scan
.error: .error:
mov ecx, [current_process]
lea ecx, [ecx+PROC.heap_lock]
call mutex_unlock
xor eax, eax xor eax, eax
pop edi pop edi
pop esi pop esi
@ -754,6 +773,9 @@ proc user_alloc_at stdcall, address:dword, alloc_size:dword
and ebx, not 0xFFF and ebx, not 0xFFF
add [edx+PROC.mem_used], ebx add [edx+PROC.mem_used], ebx
lea ecx, [edx+PROC.heap_lock]
call mutex_unlock
mov eax, [address] mov eax, [address]
pop edi pop edi
@ -769,10 +791,14 @@ proc user_free stdcall, base:dword
mov esi, [base] mov esi, [base]
test esi, esi test esi, esi
jz .exit jz .fail
push ebx push ebx
mov ebx, [current_process]
lea ecx, [ebx+PROC.heap_lock]
call mutex_lock
xor ebx, ebx xor ebx, ebx
shr esi, 12 shr esi, 12
mov eax, [page_tabs+(esi-1)*4] mov eax, [page_tabs+(esi-1)*4]
@ -809,6 +835,7 @@ proc user_free stdcall, base:dword
push edi push edi
mov edx, [current_process] mov edx, [current_process]
lea ecx, [edx+PROC.heap_lock]
mov esi, dword [edx+PROC.heap_base] mov esi, dword [edx+PROC.heap_base]
mov edi, dword [edx+PROC.heap_top] mov edi, dword [edx+PROC.heap_top]
sub ebx, [edx+PROC.mem_used] sub ebx, [edx+PROC.mem_used]
@ -816,19 +843,23 @@ proc user_free stdcall, base:dword
mov [edx+PROC.mem_used], ebx mov [edx+PROC.mem_used], ebx
call user_normalize call user_normalize
pop edi pop edi
.exit:
call mutex_unlock
xor eax, eax
pop ebx pop ebx
pop esi pop esi
ret ret
.exit:
.cantfree:
mov ecx, [current_process]
lea ecx, [ecx+PROC.heap_lock]
jmp .exit
.fail:
xor eax, eax xor eax, eax
inc eax inc eax
pop esi pop esi
ret ret
.cantfree:
xor eax, eax
pop ebx
pop esi
ret
endp endp
@ -955,6 +986,13 @@ user_realloc:
ret ret
@@: @@:
push ecx edx push ecx edx
push eax
mov ecx, [current_process]
lea ecx, [ecx+PROC.heap_lock]
call mutex_lock
pop eax
lea ecx, [eax - 0x1000] lea ecx, [eax - 0x1000]
shr ecx, 12 shr ecx, 12
mov edx, [page_tabs+ecx*4] mov edx, [page_tabs+ecx*4]
@ -962,6 +1000,10 @@ user_realloc:
jnz @f jnz @f
; attempt to realloc invalid pointer ; attempt to realloc invalid pointer
.ret0: .ret0:
mov ecx, [current_process]
lea ecx, [ecx+PROC.heap_lock]
call mutex_unlock
pop edx ecx pop edx ecx
xor eax, eax xor eax, eax
ret ret
@ -1051,8 +1093,12 @@ user_realloc:
or ebx, FREE_BLOCK or ebx, FREE_BLOCK
mov [page_tabs+ecx*4], ebx mov [page_tabs+ecx*4], ebx
.ret: .ret:
mov ecx, [current_process]
lea ecx, [ecx+PROC.heap_lock]
call mutex_unlock
pop eax edx ecx pop eax edx ecx
ret ret
.realloc_add: .realloc_add:
; get some additional memory ; get some additional memory
mov eax, [current_process] mov eax, [current_process]
@ -1091,8 +1137,13 @@ user_realloc:
mov edx, [current_process] mov edx, [current_process]
shl ebx, 12 shl ebx, 12
add [edx+PROC.mem_used], ebx add [edx+PROC.mem_used], ebx
mov ecx, [current_process]
lea ecx, [ecx+PROC.heap_lock]
call mutex_unlock
pop eax edx ecx pop eax edx ecx
ret ret
.cant_inplace: .cant_inplace:
push esi edi push esi edi
mov eax, [current_process] mov eax, [current_process]
@ -1169,6 +1220,10 @@ user_realloc:
inc esi inc esi
dec ebx dec ebx
jnz @b jnz @b
mov ecx, [current_process]
lea ecx, [ecx+PROC.heap_lock]
call mutex_unlock
pop eax edi esi edx ecx pop eax edi esi edx ecx
ret ret

View File

@ -177,39 +177,11 @@ include "detect/biosdisk.inc"
;; ;; ;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
cld
; CR0 Flags - Protected mode and Paging
mov ecx, CR0_PE
; Enabling 32 bit protected mode ; Enabling 32 bit protected mode
sidt [cs:old_ints_h] mov ecx, CR0_PE ; CR0 Flags - Protected mode and Paging
cli ; disable all irqs
cld
mov al, 255 ; mask all irqs
out 0xa1, al
out 0x21, al
l.5:
in al, 0x64 ; Enable A20
test al, 2
jnz l.5
mov al, 0xD1
out 0x64, al
l.6:
in al, 0x64
test al, 2
jnz l.6
mov al, 0xDF
out 0x60, al
l.7:
in al, 0x64
test al, 2
jnz l.7
mov al, 0xFF
out 0x64, al
lgdt [cs:tmp_gdt] ; Load GDT lgdt [cs:tmp_gdt] ; Load GDT
mov eax, cr0 ; protected mode mov eax, cr0 ; protected mode
or eax, ecx or eax, ecx