2011-10-14 23:38:50 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2022-02-12 18:27:41 +01:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2022. All rights reserved. ;;
|
2011-10-14 23:38:50 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
$Revision$
|
|
|
|
|
|
|
|
|
2012-02-23 19:52:13 +01:00
|
|
|
struct MEM_BLOCK
|
|
|
|
list LHEAD
|
|
|
|
next_block dd ? ;+8
|
|
|
|
prev_block dd ? ;+4
|
|
|
|
base dd ? ;+16
|
|
|
|
size dd ? ;+20
|
|
|
|
flags dd ? ;+24
|
|
|
|
handle dd ? ;+28
|
|
|
|
ends
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
MEM_BLOCK_RESERVED = 0x02 ; Will be allocated on first access (lazy allocation)
|
|
|
|
MEM_BLOCK_FREE = 0x04
|
|
|
|
MEM_BLOCK_USED = 0x08
|
|
|
|
MEM_BLOCK_DONT_FREE = 0x10
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
macro calc_index op
|
|
|
|
{ shr op, 12
|
|
|
|
dec op
|
|
|
|
cmp op, 63
|
|
|
|
jna @f
|
|
|
|
mov op, 63
|
|
|
|
@@:
|
|
|
|
}
|
|
|
|
|
|
|
|
align 4
|
|
|
|
md:
|
|
|
|
.add_to_used:
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [esi + MEM_BLOCK.base]
|
|
|
|
mov ebx, [esi + MEM_BLOCK.base]
|
2011-10-14 23:38:50 +02:00
|
|
|
shr ebx, 6
|
|
|
|
add eax, ebx
|
|
|
|
shr ebx, 6
|
|
|
|
add eax, ebx
|
|
|
|
shr eax, 12
|
|
|
|
and eax, 63
|
2020-05-21 15:55:36 +02:00
|
|
|
inc [mem_hash_cnt + eax*4]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [mem_used_list + eax*8]
|
2013-02-17 13:07:32 +01:00
|
|
|
list_add esi, ecx
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [esi + MEM_BLOCK.flags], MEM_BLOCK_USED
|
|
|
|
mov eax, [esi + MEM_BLOCK.size]
|
2011-10-14 23:38:50 +02:00
|
|
|
sub [heap_free], eax
|
|
|
|
ret
|
|
|
|
align 4
|
|
|
|
.find_used:
|
|
|
|
mov ecx, eax
|
|
|
|
mov ebx, eax
|
|
|
|
shr ebx, 6
|
|
|
|
add ecx, ebx
|
|
|
|
shr ebx, 6
|
|
|
|
add ecx, ebx
|
|
|
|
shr ecx, 12
|
|
|
|
and ecx, 63
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ebx, [mem_used_list + ecx*8]
|
2011-10-14 23:38:50 +02:00
|
|
|
mov esi, ebx
|
|
|
|
.next:
|
2020-05-21 15:55:36 +02:00
|
|
|
mov esi, [esi + MEM_BLOCK.list.next]
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp esi, ebx
|
|
|
|
je .fail
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
cmp eax, [esi + MEM_BLOCK.base]
|
2011-10-14 23:38:50 +02:00
|
|
|
jne .next
|
|
|
|
|
|
|
|
ret
|
|
|
|
.fail:
|
|
|
|
xor esi, esi
|
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
.del_from_used:
|
|
|
|
call .find_used
|
|
|
|
test esi, esi
|
|
|
|
jz .done
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
cmp [esi + MEM_BLOCK.flags], MEM_BLOCK_USED
|
2011-10-14 23:38:50 +02:00
|
|
|
jne .fatal
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
dec [mem_hash_cnt + ecx*4]
|
2013-02-17 13:07:32 +01:00
|
|
|
list_del esi
|
2011-10-14 23:38:50 +02:00
|
|
|
.done:
|
|
|
|
ret
|
|
|
|
.fatal: ;FIXME panic here
|
|
|
|
xor esi, esi
|
|
|
|
ret
|
|
|
|
|
|
|
|
;Initial heap state
|
|
|
|
;
|
2020-05-21 15:55:36 +02:00
|
|
|
; + heap_size terminator MEM_BLOCK_USED
|
|
|
|
; + 4096*MEM_BLOCK.sizeof free space MEM_BLOCK_FREE
|
|
|
|
;HEAP_BASE heap_descriptors MEM_BLOCK_USED
|
2011-10-14 23:38:50 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc init_kernel_heap
|
|
|
|
|
|
|
|
mov ecx, 64
|
|
|
|
mov edi, mem_block_list
|
2020-05-21 15:55:36 +02:00
|
|
|
@@:
|
2011-10-14 23:38:50 +02:00
|
|
|
mov eax, edi
|
|
|
|
stosd
|
|
|
|
stosd
|
|
|
|
loop @B
|
|
|
|
|
|
|
|
mov ecx, 64
|
|
|
|
mov edi, mem_used_list
|
2020-05-21 15:55:36 +02:00
|
|
|
@@:
|
2011-10-14 23:38:50 +02:00
|
|
|
mov eax, edi
|
|
|
|
stosd
|
|
|
|
stosd
|
|
|
|
loop @B
|
|
|
|
|
|
|
|
stdcall alloc_pages, dword 32
|
2014-01-02 20:43:51 +01:00
|
|
|
|
2015-01-06 15:53:54 +01:00
|
|
|
or eax, PG_SWR
|
2014-01-02 20:43:51 +01:00
|
|
|
mov ebx, HEAP_BASE
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ecx, 32
|
2014-01-02 20:43:51 +01:00
|
|
|
call commit_pages
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov edi, HEAP_BASE ;descriptors
|
2020-05-21 15:55:36 +02:00
|
|
|
mov ebx, HEAP_BASE + sizeof.MEM_BLOCK ;free space
|
|
|
|
mov ecx, HEAP_BASE + sizeof.MEM_BLOCK*2 ;terminator
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
xor eax, eax
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [edi + MEM_BLOCK.next_block], ebx
|
|
|
|
mov [edi + MEM_BLOCK.prev_block], eax
|
|
|
|
mov [edi + MEM_BLOCK.list.next], eax
|
|
|
|
mov [edi + MEM_BLOCK.list.prev], eax
|
|
|
|
mov [edi + MEM_BLOCK.base], HEAP_BASE
|
2023-02-15 00:26:59 +01:00
|
|
|
mov [edi + MEM_BLOCK.size], PAGE_SIZE * sizeof.MEM_BLOCK
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [edi + MEM_BLOCK.flags], MEM_BLOCK_USED
|
|
|
|
|
|
|
|
mov [ecx + MEM_BLOCK.next_block], eax
|
|
|
|
mov [ecx + MEM_BLOCK.prev_block], ebx
|
|
|
|
mov [ecx + MEM_BLOCK.list.next], eax
|
|
|
|
mov [ecx + MEM_BLOCK.list.prev], eax
|
|
|
|
mov [ecx + MEM_BLOCK.base], eax
|
|
|
|
mov [ecx + MEM_BLOCK.size], eax
|
|
|
|
mov [ecx + MEM_BLOCK.flags], MEM_BLOCK_USED
|
|
|
|
|
|
|
|
mov [ebx + MEM_BLOCK.next_block], ecx
|
|
|
|
mov [ebx + MEM_BLOCK.prev_block], edi
|
2023-02-15 00:26:59 +01:00
|
|
|
mov [ebx + MEM_BLOCK.base], HEAP_BASE + PAGE_SIZE * sizeof.MEM_BLOCK
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov ecx, [pg_data.kernel_pages]
|
|
|
|
shl ecx, 12
|
2023-02-15 00:26:59 +01:00
|
|
|
sub ecx, HEAP_BASE-OS_BASE + PAGE_SIZE * sizeof.MEM_BLOCK
|
2011-10-14 23:38:50 +02:00
|
|
|
mov [heap_size], ecx
|
|
|
|
mov [heap_free], ecx
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [ebx + MEM_BLOCK.size], ecx
|
|
|
|
mov [ebx + MEM_BLOCK.flags], MEM_BLOCK_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov [mem_block_mask], eax
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [mem_block_mask + 4], 0x80000000
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov ecx, mem_block_list + 63*8
|
2013-02-17 13:07:32 +01:00
|
|
|
list_add ebx, ecx
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2023-02-15 00:26:59 +01:00
|
|
|
mov ecx, PAGE_SIZE -3-1
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, HEAP_BASE + sizeof.MEM_BLOCK*4
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [next_memblock], HEAP_BASE + sizeof.MEM_BLOCK *3
|
|
|
|
@@:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [eax - sizeof.MEM_BLOCK], eax
|
2012-02-23 19:52:13 +01:00
|
|
|
add eax, sizeof.MEM_BLOCK
|
2011-10-14 23:38:50 +02:00
|
|
|
loop @B
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov dword[eax - sizeof.MEM_BLOCK], 0
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov ecx, heap_mutex
|
|
|
|
call mutex_init
|
|
|
|
mov [heap_blocks], 4094
|
|
|
|
mov [free_blocks], 4093
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
; param
|
|
|
|
; eax= required size
|
|
|
|
;
|
|
|
|
; retval
|
|
|
|
; edi= memory block descriptor
|
|
|
|
; ebx= descriptor index
|
|
|
|
|
|
|
|
align 4
|
|
|
|
get_small_block:
|
|
|
|
mov ecx, eax
|
|
|
|
shr ecx, 12
|
|
|
|
dec ecx
|
|
|
|
cmp ecx, 63
|
|
|
|
jle .get_index
|
|
|
|
mov ecx, 63
|
2020-05-21 15:55:36 +02:00
|
|
|
.get_index:
|
2011-10-14 23:38:50 +02:00
|
|
|
lea esi, [mem_block_mask]
|
|
|
|
xor ebx, ebx
|
|
|
|
or edx, -1
|
|
|
|
|
|
|
|
cmp ecx, 32
|
|
|
|
jb .bit_test
|
|
|
|
|
|
|
|
sub ecx, 32
|
|
|
|
add ebx, 32
|
|
|
|
add esi, 4
|
2020-05-21 15:55:36 +02:00
|
|
|
.bit_test:
|
2011-10-14 23:38:50 +02:00
|
|
|
shl edx, cl
|
|
|
|
and edx, [esi]
|
2020-05-21 15:55:36 +02:00
|
|
|
.find:
|
2011-10-14 23:38:50 +02:00
|
|
|
bsf edi, edx
|
|
|
|
jz .high_mask
|
|
|
|
add ebx, edi
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [mem_block_list + ebx*8]
|
2011-10-14 23:38:50 +02:00
|
|
|
mov edi, ecx
|
2020-05-21 15:55:36 +02:00
|
|
|
.next:
|
|
|
|
mov edi, [edi + MEM_BLOCK.list.next]
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp edi, ecx
|
|
|
|
je .err
|
2020-05-21 15:55:36 +02:00
|
|
|
cmp eax, [edi + MEM_BLOCK.size]
|
2011-10-14 23:38:50 +02:00
|
|
|
ja .next
|
|
|
|
ret
|
2020-05-21 15:55:36 +02:00
|
|
|
.err:
|
2011-10-14 23:38:50 +02:00
|
|
|
xor edi, edi
|
|
|
|
ret
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
.high_mask:
|
2011-10-14 23:38:50 +02:00
|
|
|
add esi, 4
|
2020-05-21 15:55:36 +02:00
|
|
|
cmp esi, mem_block_mask + 8
|
2011-10-14 23:38:50 +02:00
|
|
|
jae .err
|
|
|
|
add ebx, 32
|
|
|
|
mov edx, [esi]
|
|
|
|
jmp .find
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
free_mem_block:
|
2020-05-21 15:55:36 +02:00
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ebx, [next_memblock]
|
|
|
|
mov [eax], ebx
|
|
|
|
mov [next_memblock], eax
|
2020-05-21 15:55:36 +02:00
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
xor ebx, ebx
|
2020-05-21 15:55:36 +02:00
|
|
|
mov dword[eax + 4], ebx
|
|
|
|
mov dword[eax + 8], ebx
|
|
|
|
mov dword[eax + 12], ebx
|
|
|
|
mov dword[eax + 16], ebx
|
|
|
|
; mov dword[eax + 20], 0 ;don't clear block size
|
|
|
|
mov dword[eax + 24], ebx
|
|
|
|
mov dword[eax + 28], ebx
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
inc [free_blocks]
|
2020-05-21 15:55:36 +02:00
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc alloc_kernel_space stdcall, size:dword
|
|
|
|
local block_ind:DWORD
|
|
|
|
|
|
|
|
push ebx
|
|
|
|
push esi
|
|
|
|
push edi
|
|
|
|
|
|
|
|
mov eax, [size]
|
2023-02-15 00:26:59 +01:00
|
|
|
add eax, PAGE_SIZE-1
|
|
|
|
and eax, -PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
mov [size], eax
|
|
|
|
|
|
|
|
cmp eax, [heap_free]
|
|
|
|
ja .error
|
|
|
|
|
2013-12-20 18:38:01 +01:00
|
|
|
spin_lock_irqsave heap_mutex
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov eax, [size]
|
|
|
|
|
|
|
|
call get_small_block ; eax
|
|
|
|
test edi, edi
|
|
|
|
jz .error_unlock
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
cmp [edi + MEM_BLOCK.flags], MEM_BLOCK_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
jne .error_unlock
|
|
|
|
|
|
|
|
mov [block_ind], ebx ;index of allocated block
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [edi + MEM_BLOCK.size]
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp eax, [size]
|
|
|
|
je .m_eq_size
|
|
|
|
|
|
|
|
mov esi, [next_memblock] ;new memory block
|
|
|
|
test esi, esi
|
|
|
|
jz .error_unlock
|
|
|
|
|
|
|
|
dec [free_blocks]
|
|
|
|
mov eax, [esi]
|
|
|
|
mov [next_memblock], eax
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [esi + MEM_BLOCK.next_block], edi
|
|
|
|
mov eax, [edi + MEM_BLOCK.prev_block]
|
|
|
|
mov [esi + MEM_BLOCK.prev_block], eax
|
|
|
|
mov [edi + MEM_BLOCK.prev_block], esi
|
|
|
|
mov [esi + MEM_BLOCK.list.next], 0
|
|
|
|
mov [esi + MEM_BLOCK.list.prev], 0
|
|
|
|
mov [eax + MEM_BLOCK.next_block], esi
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov ebx, [edi + MEM_BLOCK.base]
|
|
|
|
mov [esi + MEM_BLOCK.base], ebx
|
2011-10-14 23:38:50 +02:00
|
|
|
mov edx, [size]
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [esi + MEM_BLOCK.size], edx
|
|
|
|
add [edi + MEM_BLOCK.base], edx
|
|
|
|
sub [edi + MEM_BLOCK.size], edx
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [edi + MEM_BLOCK.size]
|
2013-12-20 18:38:01 +01:00
|
|
|
calc_index eax
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp eax, [block_ind]
|
|
|
|
je .add_used
|
|
|
|
|
2013-12-20 18:38:01 +01:00
|
|
|
list_del edi
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov ecx, [block_ind]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edx, [mem_block_list + ecx*8]
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp edx, [edx]
|
|
|
|
jnz @f
|
|
|
|
btr [mem_block_mask], ecx
|
|
|
|
@@:
|
|
|
|
bts [mem_block_mask], eax
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edx, [mem_block_list + eax*8] ;edx= list head
|
2013-12-20 18:38:01 +01:00
|
|
|
list_add edi, edx
|
2011-10-14 23:38:50 +02:00
|
|
|
.add_used:
|
|
|
|
|
|
|
|
call md.add_to_used
|
|
|
|
|
2013-12-20 18:38:01 +01:00
|
|
|
spin_unlock_irqrestore heap_mutex
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [esi + MEM_BLOCK.base]
|
2011-10-14 23:38:50 +02:00
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
|
|
|
ret
|
|
|
|
|
|
|
|
.m_eq_size:
|
2013-12-20 18:38:01 +01:00
|
|
|
list_del edi
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edx, [mem_block_list + ebx*8]
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp edx, [edx]
|
|
|
|
jnz @f
|
|
|
|
btr [mem_block_mask], ebx
|
|
|
|
@@:
|
|
|
|
mov esi, edi
|
|
|
|
jmp .add_used
|
|
|
|
|
|
|
|
.error_unlock:
|
2013-12-20 18:38:01 +01:00
|
|
|
spin_unlock_irqrestore heap_mutex
|
2011-10-14 23:38:50 +02:00
|
|
|
.error:
|
|
|
|
xor eax, eax
|
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc free_kernel_space stdcall uses ebx ecx edx esi edi, base:dword
|
|
|
|
|
2013-12-20 18:38:01 +01:00
|
|
|
spin_lock_irqsave heap_mutex
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov eax, [base]
|
|
|
|
|
|
|
|
call md.del_from_used
|
|
|
|
test esi, esi
|
|
|
|
jz .fail
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [esi + MEM_BLOCK.size]
|
2011-10-14 23:38:50 +02:00
|
|
|
add [heap_free], eax
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov edi, [esi + MEM_BLOCK.next_block]
|
|
|
|
cmp [edi + MEM_BLOCK.flags], MEM_BLOCK_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
jne .prev
|
|
|
|
|
2013-02-17 13:07:32 +01:00
|
|
|
list_del edi
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov edx, [edi + MEM_BLOCK.next_block]
|
|
|
|
mov [esi + MEM_BLOCK.next_block], edx
|
|
|
|
mov [edx + MEM_BLOCK.prev_block], esi
|
|
|
|
mov ecx, [edi + MEM_BLOCK.size]
|
|
|
|
add [esi + MEM_BLOCK.size], ecx
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-02-17 13:07:32 +01:00
|
|
|
calc_index ecx
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edx, [mem_block_list + ecx*8]
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp edx, [edx]
|
|
|
|
jne @F
|
|
|
|
btr [mem_block_mask], ecx
|
|
|
|
@@:
|
|
|
|
mov eax, edi
|
|
|
|
call free_mem_block
|
|
|
|
.prev:
|
2020-05-21 15:55:36 +02:00
|
|
|
mov edi, [esi + MEM_BLOCK.prev_block]
|
|
|
|
cmp [edi + MEM_BLOCK.flags], MEM_BLOCK_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
jne .insert
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov edx, [esi + MEM_BLOCK.next_block]
|
|
|
|
mov [edi + MEM_BLOCK.next_block], edx
|
|
|
|
mov [edx + MEM_BLOCK.prev_block], edi
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov eax, esi
|
|
|
|
call free_mem_block
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov ecx, [edi + MEM_BLOCK.size]
|
|
|
|
mov eax, [esi + MEM_BLOCK.size]
|
2011-10-14 23:38:50 +02:00
|
|
|
add eax, ecx
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [edi + MEM_BLOCK.size], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-02-17 13:07:32 +01:00
|
|
|
calc_index eax ;new index
|
|
|
|
calc_index ecx ;old index
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp eax, ecx
|
|
|
|
je .m_eq
|
|
|
|
|
|
|
|
push ecx
|
2013-02-17 13:07:32 +01:00
|
|
|
list_del edi
|
2011-10-14 23:38:50 +02:00
|
|
|
pop ecx
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edx, [mem_block_list + ecx*8]
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp edx, [edx]
|
|
|
|
jne .add_block
|
|
|
|
btr [mem_block_mask], ecx
|
2013-02-17 13:07:32 +01:00
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
.add_block:
|
|
|
|
bts [mem_block_mask], eax
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edx, [mem_block_list + eax*8]
|
2013-02-17 13:07:32 +01:00
|
|
|
list_add edi, edx
|
2011-10-14 23:38:50 +02:00
|
|
|
.m_eq:
|
2013-12-20 18:38:01 +01:00
|
|
|
spin_unlock_irqrestore heap_mutex
|
2011-10-14 23:38:50 +02:00
|
|
|
xor eax, eax
|
|
|
|
not eax
|
|
|
|
ret
|
|
|
|
.insert:
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [esi + MEM_BLOCK.flags], MEM_BLOCK_FREE
|
|
|
|
mov eax, [esi + MEM_BLOCK.size]
|
2013-02-17 13:07:32 +01:00
|
|
|
calc_index eax
|
2011-10-14 23:38:50 +02:00
|
|
|
mov edi, esi
|
|
|
|
jmp .add_block
|
|
|
|
|
|
|
|
.fail:
|
2013-12-20 18:38:01 +01:00
|
|
|
spin_unlock_irqrestore heap_mutex
|
2011-10-14 23:38:50 +02:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc kernel_alloc stdcall, size:dword
|
2013-02-17 13:07:32 +01:00
|
|
|
locals
|
|
|
|
lin_addr dd ?
|
|
|
|
pages_count dd ?
|
|
|
|
endl
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
push ebx
|
|
|
|
push edi
|
|
|
|
|
|
|
|
mov eax, [size]
|
2023-02-15 00:26:59 +01:00
|
|
|
add eax, PAGE_SIZE-1
|
|
|
|
and eax, -PAGE_SIZE;
|
2011-10-14 23:38:50 +02:00
|
|
|
mov [size], eax
|
|
|
|
and eax, eax
|
|
|
|
jz .err
|
|
|
|
mov ebx, eax
|
|
|
|
shr ebx, 12
|
|
|
|
mov [pages_count], ebx
|
|
|
|
|
|
|
|
stdcall alloc_kernel_space, eax
|
2014-01-02 20:43:51 +01:00
|
|
|
mov [lin_addr], eax
|
|
|
|
mov ebx, [pages_count]
|
2011-10-14 23:38:50 +02:00
|
|
|
test eax, eax
|
|
|
|
jz .err
|
|
|
|
|
|
|
|
mov edx, eax
|
|
|
|
|
2014-01-02 20:43:51 +01:00
|
|
|
shr ebx, 3
|
|
|
|
jz .tail
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2014-01-02 20:43:51 +01:00
|
|
|
shl ebx, 3
|
2011-10-14 23:38:50 +02:00
|
|
|
stdcall alloc_pages, ebx
|
2014-01-02 20:43:51 +01:00
|
|
|
test eax, eax
|
2011-10-14 23:38:50 +02:00
|
|
|
jz .err
|
|
|
|
|
2014-01-02 20:43:51 +01:00
|
|
|
mov ecx, ebx
|
2020-05-21 15:55:36 +02:00
|
|
|
or eax, PG_GLOBAL + PG_SWR
|
2014-01-02 20:43:51 +01:00
|
|
|
mov ebx, [lin_addr]
|
|
|
|
call commit_pages
|
|
|
|
|
|
|
|
mov edx, ebx ; this dirty hack
|
|
|
|
.tail:
|
|
|
|
mov ebx, [pages_count]
|
|
|
|
and ebx, 7
|
2011-10-14 23:38:50 +02:00
|
|
|
jz .end
|
|
|
|
@@:
|
|
|
|
call alloc_page
|
|
|
|
test eax, eax
|
|
|
|
jz .err
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
stdcall map_page, edx, eax, dword (PG_GLOBAL + PG_SWR)
|
2023-02-15 00:26:59 +01:00
|
|
|
add edx, PAGE_SIZE
|
2014-01-02 20:43:51 +01:00
|
|
|
dec ebx
|
2011-10-14 23:38:50 +02:00
|
|
|
jnz @B
|
|
|
|
.end:
|
|
|
|
mov eax, [lin_addr]
|
|
|
|
pop edi
|
|
|
|
pop ebx
|
|
|
|
ret
|
|
|
|
.err:
|
|
|
|
xor eax, eax
|
|
|
|
pop edi
|
|
|
|
pop ebx
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc kernel_free stdcall, base:dword
|
|
|
|
|
|
|
|
push ebx esi
|
|
|
|
|
2013-12-20 18:38:01 +01:00
|
|
|
spin_lock_irqsave heap_mutex
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov eax, [base]
|
|
|
|
call md.find_used
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
cmp [esi + MEM_BLOCK.flags], MEM_BLOCK_USED
|
2011-10-14 23:38:50 +02:00
|
|
|
jne .fail
|
|
|
|
|
2013-12-20 18:38:01 +01:00
|
|
|
spin_unlock_irqrestore heap_mutex
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [esi + MEM_BLOCK.base]
|
|
|
|
mov ecx, [esi + MEM_BLOCK.size]
|
2011-10-14 23:38:50 +02:00
|
|
|
shr ecx, 12
|
|
|
|
call release_pages ;eax, ecx
|
|
|
|
stdcall free_kernel_space, [base]
|
|
|
|
pop esi ebx
|
|
|
|
ret
|
|
|
|
.fail:
|
2013-12-20 18:38:01 +01:00
|
|
|
spin_unlock_irqrestore heap_mutex
|
2011-10-14 23:38:50 +02:00
|
|
|
xor eax, eax
|
|
|
|
pop esi ebx
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
2014-09-18 09:58:56 +02:00
|
|
|
;;;;;;;;;;;;;; USER HEAP ;;;;;;;;;;;;;;;;;
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2017-12-20 02:07:10 +01:00
|
|
|
HEAP_TOP = 0x80000000
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
align 4
|
|
|
|
proc init_heap
|
|
|
|
|
2014-09-18 09:58:56 +02:00
|
|
|
mov ebx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [ebx + PROC.heap_top]
|
2011-10-14 23:38:50 +02:00
|
|
|
test eax, eax
|
|
|
|
jz @F
|
2020-05-21 15:55:36 +02:00
|
|
|
sub eax, [ebx + PROC.heap_base]
|
2014-09-18 09:58:56 +02:00
|
|
|
sub eax, PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
|
|
|
@@:
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ebx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_init
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov esi, [ebx + PROC.mem_used]
|
2023-02-15 00:26:59 +01:00
|
|
|
add esi, PAGE_SIZE-1
|
|
|
|
and esi, -PAGE_SIZE
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [ebx + PROC.mem_used], esi
|
2011-10-14 23:38:50 +02:00
|
|
|
mov eax, HEAP_TOP
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [ebx + PROC.heap_base], esi
|
|
|
|
mov [ebx + PROC.heap_top], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
sub eax, esi
|
|
|
|
shr esi, 10
|
|
|
|
mov ecx, eax
|
2014-09-18 09:58:56 +02:00
|
|
|
sub eax, PAGE_SIZE
|
2020-05-21 15:55:36 +02:00
|
|
|
or ecx, MEM_BLOCK_FREE
|
|
|
|
mov [page_tabs + esi], ecx
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc user_alloc stdcall, alloc_size:dword
|
2020-05-21 15:55:36 +02:00
|
|
|
|
2017-05-13 08:41:42 +02:00
|
|
|
push ebx esi edi
|
2020-05-21 15:55:36 +02:00
|
|
|
|
2017-09-13 12:53:39 +02:00
|
|
|
mov ebx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ebx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_lock
|
2020-05-21 15:55:36 +02:00
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ecx, [alloc_size]
|
2020-05-21 15:55:36 +02:00
|
|
|
add ecx, (4095 + PAGE_SIZE)
|
2023-02-15 00:26:59 +01:00
|
|
|
and ecx, -PAGE_SIZE
|
2020-05-21 15:55:36 +02:00
|
|
|
mov esi, [ebx + PROC.heap_base]
|
|
|
|
mov edi, [ebx + PROC.heap_top]
|
|
|
|
.scan:
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp esi, edi
|
2014-09-18 09:58:56 +02:00
|
|
|
jae .m_exit
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov ebx, esi
|
|
|
|
shr ebx, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [page_tabs + ebx*4]
|
|
|
|
test al, MEM_BLOCK_FREE
|
2014-09-18 09:58:56 +02:00
|
|
|
jz .test_used
|
2023-02-15 00:26:59 +01:00
|
|
|
and eax, -PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp eax, ecx ;alloc_size
|
2014-09-18 09:58:56 +02:00
|
|
|
jb .m_next
|
2011-10-14 23:38:50 +02:00
|
|
|
jz @f
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edx, [esi + ecx]
|
2011-10-14 23:38:50 +02:00
|
|
|
sub eax, ecx
|
2020-05-21 15:55:36 +02:00
|
|
|
or al, MEM_BLOCK_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
shr edx, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [page_tabs + edx*4], eax
|
|
|
|
@@:
|
|
|
|
or ecx, MEM_BLOCK_USED
|
|
|
|
mov [page_tabs + ebx*4], ecx
|
2011-10-14 23:38:50 +02:00
|
|
|
shr ecx, 12
|
|
|
|
inc ebx
|
|
|
|
dec ecx
|
|
|
|
jz .no
|
2020-05-21 15:55:36 +02:00
|
|
|
@@:
|
|
|
|
mov dword [page_tabs + ebx*4], MEM_BLOCK_RESERVED
|
2011-10-14 23:38:50 +02:00
|
|
|
inc ebx
|
|
|
|
dec ecx
|
|
|
|
jnz @B
|
2020-05-21 15:55:36 +02:00
|
|
|
.no:
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2014-09-18 09:58:56 +02:00
|
|
|
mov edx, [current_process]
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ebx, [alloc_size]
|
2023-02-15 00:26:59 +01:00
|
|
|
add ebx, PAGE_SIZE-1
|
|
|
|
and ebx, -PAGE_SIZE
|
2020-05-21 15:55:36 +02:00
|
|
|
add [edx + PROC.mem_used], ebx
|
2014-09-18 09:58:56 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [edx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_unlock
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2023-02-15 00:26:59 +01:00
|
|
|
lea eax, [esi + PAGE_SIZE]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
|
|
|
ret
|
2014-09-18 09:58:56 +02:00
|
|
|
.test_used:
|
2020-05-21 15:55:36 +02:00
|
|
|
test al, MEM_BLOCK_USED
|
2014-09-18 09:58:56 +02:00
|
|
|
jz .m_exit
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2023-02-15 00:26:59 +01:00
|
|
|
and eax, -PAGE_SIZE
|
2014-09-18 09:58:56 +02:00
|
|
|
.m_next:
|
2011-10-14 23:38:50 +02:00
|
|
|
add esi, eax
|
2014-09-18 09:58:56 +02:00
|
|
|
jmp .scan
|
|
|
|
.m_exit:
|
|
|
|
mov ecx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ecx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_unlock
|
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
xor eax, eax
|
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc user_alloc_at stdcall, address:dword, alloc_size:dword
|
|
|
|
|
|
|
|
push ebx
|
|
|
|
push esi
|
|
|
|
push edi
|
|
|
|
|
2014-09-18 09:58:56 +02:00
|
|
|
mov ebx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ebx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_lock
|
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
mov edx, [address]
|
2023-02-15 00:26:59 +01:00
|
|
|
and edx, -PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
mov [address], edx
|
2023-02-15 00:26:59 +01:00
|
|
|
sub edx, PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
jb .error
|
2020-05-21 15:55:36 +02:00
|
|
|
mov esi, [ebx + PROC.heap_base]
|
|
|
|
mov edi, [ebx + PROC.heap_top]
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp edx, esi
|
|
|
|
jb .error
|
|
|
|
.scan:
|
|
|
|
cmp esi, edi
|
|
|
|
jae .error
|
|
|
|
mov ebx, esi
|
|
|
|
shr ebx, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [page_tabs + ebx*4]
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ecx, eax
|
2023-02-15 00:26:59 +01:00
|
|
|
and ecx, -PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
add ecx, esi
|
|
|
|
cmp edx, ecx
|
|
|
|
jb .found
|
|
|
|
mov esi, ecx
|
|
|
|
jmp .scan
|
|
|
|
.error:
|
2014-09-18 09:58:56 +02:00
|
|
|
mov ecx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ecx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_unlock
|
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
xor eax, eax
|
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
|
|
|
ret
|
|
|
|
.found:
|
2020-05-21 15:55:36 +02:00
|
|
|
test al, MEM_BLOCK_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
jz .error
|
|
|
|
mov eax, ecx
|
|
|
|
sub eax, edx
|
2023-02-15 00:26:59 +01:00
|
|
|
sub eax, PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp eax, [alloc_size]
|
|
|
|
jb .error
|
|
|
|
|
|
|
|
; Here we have 1 big free block which includes requested area.
|
|
|
|
; In general, 3 other blocks must be created instead:
|
|
|
|
; free at [esi, edx);
|
2020-05-21 15:55:36 +02:00
|
|
|
; busy at [edx, edx + 0x1000 + ALIGN_UP(alloc_size,0x1000));
|
|
|
|
; free at [edx + 0x1000 + ALIGN_UP(alloc_size,0x1000), ecx)
|
2011-10-14 23:38:50 +02:00
|
|
|
; First or third block (or both) may be absent.
|
|
|
|
mov eax, edx
|
|
|
|
sub eax, esi
|
|
|
|
jz .nofirst
|
2020-05-21 15:55:36 +02:00
|
|
|
or al, MEM_BLOCK_FREE
|
|
|
|
mov [page_tabs + ebx*4], eax
|
|
|
|
.nofirst:
|
2011-10-14 23:38:50 +02:00
|
|
|
mov eax, [alloc_size]
|
|
|
|
add eax, 0x1FFF
|
2023-02-15 00:26:59 +01:00
|
|
|
and eax, -PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ebx, edx
|
|
|
|
add edx, eax
|
|
|
|
shr ebx, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
or al, MEM_BLOCK_USED
|
|
|
|
mov [page_tabs + ebx*4], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
shr eax, 12
|
|
|
|
dec eax
|
|
|
|
jz .second_nofill
|
|
|
|
inc ebx
|
2020-05-21 15:55:36 +02:00
|
|
|
.fill:
|
|
|
|
mov dword [page_tabs + ebx*4], MEM_BLOCK_RESERVED
|
2011-10-14 23:38:50 +02:00
|
|
|
inc ebx
|
|
|
|
dec eax
|
|
|
|
jnz .fill
|
2013-02-17 13:07:32 +01:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
.second_nofill:
|
2011-10-14 23:38:50 +02:00
|
|
|
sub ecx, edx
|
|
|
|
jz .nothird
|
2020-05-21 15:55:36 +02:00
|
|
|
or cl, MEM_BLOCK_FREE
|
|
|
|
mov [page_tabs + ebx*4], ecx
|
2013-02-17 13:07:32 +01:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
.nothird:
|
2014-09-18 09:58:56 +02:00
|
|
|
mov edx, [current_process]
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ebx, [alloc_size]
|
2023-02-15 00:26:59 +01:00
|
|
|
add ebx, PAGE_SIZE-1
|
|
|
|
and ebx, -PAGE_SIZE
|
2020-05-21 15:55:36 +02:00
|
|
|
add [edx + PROC.mem_used], ebx
|
2014-09-18 09:58:56 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [edx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_unlock
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov eax, [address]
|
|
|
|
|
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc user_free stdcall, base:dword
|
|
|
|
|
|
|
|
push esi
|
|
|
|
|
|
|
|
mov esi, [base]
|
|
|
|
test esi, esi
|
2014-09-18 09:58:56 +02:00
|
|
|
jz .fail
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
push ebx
|
|
|
|
|
2014-09-18 09:58:56 +02:00
|
|
|
mov ebx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ebx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_lock
|
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
xor ebx, ebx
|
|
|
|
shr esi, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [page_tabs + (esi-1)*4]
|
|
|
|
test al, MEM_BLOCK_USED
|
2011-10-14 23:38:50 +02:00
|
|
|
jz .cantfree
|
2020-05-21 15:55:36 +02:00
|
|
|
test al, MEM_BLOCK_DONT_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
jnz .cantfree
|
|
|
|
|
2023-02-15 00:26:59 +01:00
|
|
|
and eax, -PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ecx, eax
|
2020-05-21 15:55:36 +02:00
|
|
|
or al, MEM_BLOCK_FREE
|
|
|
|
mov [page_tabs + (esi-1)*4], eax
|
2023-02-15 00:26:59 +01:00
|
|
|
sub ecx, PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ebx, ecx
|
|
|
|
shr ecx, 12
|
|
|
|
jz .released
|
2020-05-21 15:55:36 +02:00
|
|
|
.release:
|
2011-10-14 23:38:50 +02:00
|
|
|
xor eax, eax
|
2020-05-21 15:55:36 +02:00
|
|
|
xchg eax, [page_tabs + esi*4]
|
2011-10-14 23:38:50 +02:00
|
|
|
test al, 1
|
|
|
|
jz @F
|
|
|
|
test eax, PG_SHARED
|
|
|
|
jnz @F
|
|
|
|
call free_page
|
|
|
|
mov eax, esi
|
|
|
|
shl eax, 12
|
|
|
|
invlpg [eax]
|
2020-05-21 15:55:36 +02:00
|
|
|
@@:
|
2011-10-14 23:38:50 +02:00
|
|
|
inc esi
|
|
|
|
dec ecx
|
|
|
|
jnz .release
|
2013-02-17 13:07:32 +01:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
.released:
|
2011-10-14 23:38:50 +02:00
|
|
|
push edi
|
|
|
|
|
2014-09-18 09:58:56 +02:00
|
|
|
mov edx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [edx + PROC.heap_lock]
|
|
|
|
mov esi, dword [edx + PROC.heap_base]
|
|
|
|
mov edi, dword [edx + PROC.heap_top]
|
|
|
|
sub ebx, [edx + PROC.mem_used]
|
2011-10-14 23:38:50 +02:00
|
|
|
neg ebx
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [edx + PROC.mem_used], ebx
|
2011-10-14 23:38:50 +02:00
|
|
|
call user_normalize
|
|
|
|
pop edi
|
2020-05-21 15:55:36 +02:00
|
|
|
.exit:
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_unlock
|
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
xor eax, eax
|
|
|
|
inc eax
|
2014-09-18 09:58:56 +02:00
|
|
|
pop ebx
|
2011-10-14 23:38:50 +02:00
|
|
|
pop esi
|
|
|
|
ret
|
2014-09-18 09:58:56 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
.cantfree:
|
2014-09-18 09:58:56 +02:00
|
|
|
mov ecx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ecx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
jmp .exit
|
2020-05-21 15:55:36 +02:00
|
|
|
.fail:
|
2011-10-14 23:38:50 +02:00
|
|
|
xor eax, eax
|
|
|
|
pop esi
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
2012-02-24 22:47:01 +01:00
|
|
|
|
|
|
|
align 4
|
|
|
|
proc user_unmap stdcall, base:dword, offset:dword, size:dword
|
|
|
|
|
|
|
|
push ebx
|
|
|
|
|
|
|
|
mov ebx, [base] ; must be valid pointer
|
|
|
|
test ebx, ebx
|
|
|
|
jz .error
|
|
|
|
|
|
|
|
mov edx, [offset] ; check offset
|
|
|
|
add edx, ebx ; must be below 2Gb app limit
|
|
|
|
js .error
|
|
|
|
|
|
|
|
shr ebx, 12 ; chek block attributes
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ebx, [page_tabs + ebx*4]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [ebx - 4] ; block attributes
|
2020-05-21 15:55:36 +02:00
|
|
|
test al, MEM_BLOCK_USED
|
2012-02-24 22:47:01 +01:00
|
|
|
jz .error
|
2020-05-21 15:55:36 +02:00
|
|
|
test al, MEM_BLOCK_DONT_FREE
|
2012-02-24 22:47:01 +01:00
|
|
|
jnz .error
|
|
|
|
|
|
|
|
shr edx, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edx, [page_tabs + edx*4] ; unmap offset
|
2012-02-24 22:47:01 +01:00
|
|
|
|
|
|
|
mov ecx, [size]
|
2023-02-15 00:26:59 +01:00
|
|
|
add ecx, PAGE_SIZE-1
|
2012-02-24 22:47:01 +01:00
|
|
|
shr ecx, 12 ; unmap size in pages
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
shr eax, 12 ; block size + 1 page
|
|
|
|
lea ebx, [ebx + eax*4-4] ; block end ptr
|
|
|
|
lea eax, [edx + ecx*4] ; unmap end ptr
|
2012-02-24 22:47:01 +01:00
|
|
|
|
|
|
|
cmp eax, ebx ; check for overflow
|
|
|
|
ja .error
|
|
|
|
|
|
|
|
mov ebx, [offset]
|
2023-02-15 00:26:59 +01:00
|
|
|
and ebx, -PAGE_SIZE ; is it required ?
|
2013-07-10 11:29:34 +02:00
|
|
|
add ebx, [base]
|
2012-02-24 22:47:01 +01:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
.unmap:
|
2012-02-24 22:47:01 +01:00
|
|
|
mov eax, [edx] ; get page addres
|
|
|
|
test al, 1 ; page mapped ?
|
|
|
|
jz @F
|
|
|
|
test eax, PG_SHARED ; page shared ?
|
|
|
|
jnz @F
|
2020-05-21 15:55:36 +02:00
|
|
|
mov dword[edx], MEM_BLOCK_RESERVED
|
2012-02-24 22:47:01 +01:00
|
|
|
; mark page as reserved
|
|
|
|
invlpg [ebx] ; when we start using
|
|
|
|
call free_page ; empty c-o-w page instead this ?
|
2020-05-21 15:55:36 +02:00
|
|
|
@@:
|
2023-02-15 00:26:59 +01:00
|
|
|
add ebx, PAGE_SIZE
|
2012-02-24 22:47:01 +01:00
|
|
|
add edx, 4
|
|
|
|
dec ecx
|
|
|
|
jnz .unmap
|
|
|
|
|
|
|
|
pop ebx
|
|
|
|
or al, 1 ; return non zero on success
|
|
|
|
ret
|
|
|
|
.error:
|
|
|
|
pop ebx
|
|
|
|
xor eax, eax ; something wrong
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
2011-10-14 23:38:50 +02:00
|
|
|
user_normalize:
|
|
|
|
; in: esi=heap_base, edi=heap_top
|
|
|
|
; out: eax=0 <=> OK
|
|
|
|
; destroys: ebx,edx,esi,edi
|
|
|
|
shr esi, 12
|
|
|
|
shr edi, 12
|
|
|
|
@@:
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [page_tabs + esi*4]
|
|
|
|
test al, MEM_BLOCK_USED
|
2011-10-14 23:38:50 +02:00
|
|
|
jz .test_free
|
|
|
|
shr eax, 12
|
|
|
|
add esi, eax
|
|
|
|
jmp @B
|
|
|
|
.test_free:
|
2020-05-21 15:55:36 +02:00
|
|
|
test al, MEM_BLOCK_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
jz .err
|
|
|
|
mov edx, eax
|
|
|
|
shr edx, 12
|
|
|
|
add edx, esi
|
|
|
|
cmp edx, edi
|
|
|
|
jae .exit
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov ebx, [page_tabs + edx*4]
|
|
|
|
test bl, MEM_BLOCK_USED
|
2011-10-14 23:38:50 +02:00
|
|
|
jz .next_free
|
|
|
|
|
|
|
|
shr ebx, 12
|
|
|
|
add edx, ebx
|
|
|
|
mov esi, edx
|
|
|
|
jmp @B
|
|
|
|
.next_free:
|
2020-05-21 15:55:36 +02:00
|
|
|
test bl, MEM_BLOCK_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
jz .err
|
2020-05-21 15:55:36 +02:00
|
|
|
and dword[page_tabs + edx*4], 0
|
2011-10-14 23:38:50 +02:00
|
|
|
add eax, ebx
|
2023-02-15 00:26:59 +01:00
|
|
|
and eax, -PAGE_SIZE
|
2020-05-21 15:55:36 +02:00
|
|
|
or eax, MEM_BLOCK_FREE
|
|
|
|
mov [page_tabs + esi*4], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
jmp @B
|
|
|
|
.exit:
|
|
|
|
xor eax, eax
|
|
|
|
inc eax
|
|
|
|
ret
|
|
|
|
.err:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
user_realloc:
|
|
|
|
; in: eax = pointer, ebx = new size
|
|
|
|
; out: eax = new pointer or NULL
|
|
|
|
test eax, eax
|
|
|
|
jnz @f
|
|
|
|
; realloc(NULL,sz) - same as malloc(sz)
|
|
|
|
push ebx
|
|
|
|
call user_alloc
|
|
|
|
ret
|
|
|
|
@@:
|
|
|
|
push ecx edx
|
2014-09-18 09:58:56 +02:00
|
|
|
|
|
|
|
push eax
|
|
|
|
mov ecx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ecx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_lock
|
|
|
|
pop eax
|
|
|
|
|
2023-02-15 00:26:59 +01:00
|
|
|
lea ecx, [eax - PAGE_SIZE]
|
2011-10-14 23:38:50 +02:00
|
|
|
shr ecx, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
mov edx, [page_tabs + ecx*4]
|
|
|
|
test dl, MEM_BLOCK_USED
|
2011-10-14 23:38:50 +02:00
|
|
|
jnz @f
|
|
|
|
; attempt to realloc invalid pointer
|
|
|
|
.ret0:
|
2014-09-18 09:58:56 +02:00
|
|
|
mov ecx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ecx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_unlock
|
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
pop edx ecx
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
@@:
|
2020-05-21 15:55:36 +02:00
|
|
|
test dl, MEM_BLOCK_DONT_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
jnz .ret0
|
|
|
|
add ebx, 0x1FFF
|
|
|
|
shr edx, 12
|
|
|
|
shr ebx, 12
|
|
|
|
; edx = allocated size, ebx = new size
|
|
|
|
add edx, ecx
|
|
|
|
add ebx, ecx
|
|
|
|
cmp edx, ebx
|
|
|
|
jb .realloc_add
|
|
|
|
; release part of allocated memory
|
|
|
|
.loop:
|
|
|
|
cmp edx, ebx
|
|
|
|
jz .release_done
|
|
|
|
dec edx
|
|
|
|
xor eax, eax
|
2020-05-21 15:55:36 +02:00
|
|
|
xchg eax, [page_tabs + edx*4]
|
2011-10-14 23:38:50 +02:00
|
|
|
test al, 1
|
|
|
|
jz .loop
|
|
|
|
call free_page
|
|
|
|
mov eax, edx
|
|
|
|
shl eax, 12
|
|
|
|
invlpg [eax]
|
|
|
|
jmp .loop
|
|
|
|
.release_done:
|
|
|
|
sub ebx, ecx
|
|
|
|
cmp ebx, 1
|
|
|
|
jnz .nofreeall
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [page_tabs + ecx*4]
|
2023-02-15 00:26:59 +01:00
|
|
|
and eax, -PAGE_SIZE
|
2014-09-18 09:58:56 +02:00
|
|
|
mov edx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
mov ebx, [edx + PROC.mem_used]
|
2011-10-14 23:38:50 +02:00
|
|
|
sub ebx, eax
|
2023-02-15 00:26:59 +01:00
|
|
|
add ebx, PAGE_SIZE
|
2020-05-21 15:55:36 +02:00
|
|
|
or al, MEM_BLOCK_FREE
|
|
|
|
mov [page_tabs + ecx*4], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
push esi edi
|
2020-05-21 15:55:36 +02:00
|
|
|
mov esi, [edx + PROC.heap_base]
|
|
|
|
mov edi, [edx + PROC.heap_top]
|
|
|
|
mov [edx + PROC.mem_used], ebx
|
2011-10-14 23:38:50 +02:00
|
|
|
call user_normalize
|
|
|
|
pop edi esi
|
|
|
|
jmp .ret0 ; all freed
|
|
|
|
.nofreeall:
|
|
|
|
sub edx, ecx
|
|
|
|
shl ebx, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
or ebx, MEM_BLOCK_USED
|
|
|
|
xchg [page_tabs + ecx*4], ebx
|
2011-10-14 23:38:50 +02:00
|
|
|
shr ebx, 12
|
|
|
|
sub ebx, edx
|
|
|
|
push ebx ecx edx
|
2014-09-18 09:58:56 +02:00
|
|
|
mov edx, [current_process]
|
2011-10-14 23:38:50 +02:00
|
|
|
shl ebx, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
sub ebx, [edx + PROC.mem_used]
|
2011-10-14 23:38:50 +02:00
|
|
|
neg ebx
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [edx + PROC.mem_used], ebx
|
2011-10-14 23:38:50 +02:00
|
|
|
pop edx ecx ebx
|
2020-05-21 15:55:36 +02:00
|
|
|
lea eax, [ecx + 1]
|
2011-10-14 23:38:50 +02:00
|
|
|
shl eax, 12
|
|
|
|
push eax
|
|
|
|
add ecx, edx
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edx, [ecx + ebx]
|
2011-10-14 23:38:50 +02:00
|
|
|
shl ebx, 12
|
|
|
|
jz .ret
|
|
|
|
push esi
|
2014-09-18 09:58:56 +02:00
|
|
|
mov esi, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
mov esi, [esi + PROC.heap_top]
|
2011-10-14 23:38:50 +02:00
|
|
|
shr esi, 12
|
|
|
|
@@:
|
|
|
|
cmp edx, esi
|
|
|
|
jae .merge_done
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [page_tabs + edx*4]
|
|
|
|
test al, MEM_BLOCK_USED
|
2011-10-14 23:38:50 +02:00
|
|
|
jnz .merge_done
|
2020-05-21 15:55:36 +02:00
|
|
|
and dword [page_tabs + edx*4], 0
|
2011-10-14 23:38:50 +02:00
|
|
|
shr eax, 12
|
|
|
|
add edx, eax
|
|
|
|
shl eax, 12
|
|
|
|
add ebx, eax
|
|
|
|
jmp @b
|
|
|
|
.merge_done:
|
|
|
|
pop esi
|
2020-05-21 15:55:36 +02:00
|
|
|
or ebx, MEM_BLOCK_FREE
|
|
|
|
mov [page_tabs + ecx*4], ebx
|
2011-10-14 23:38:50 +02:00
|
|
|
.ret:
|
2014-09-18 09:58:56 +02:00
|
|
|
mov ecx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ecx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_unlock
|
2011-10-14 23:38:50 +02:00
|
|
|
pop eax edx ecx
|
|
|
|
ret
|
2014-09-18 09:58:56 +02:00
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
.realloc_add:
|
|
|
|
; get some additional memory
|
2014-09-18 09:58:56 +02:00
|
|
|
mov eax, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [eax + PROC.heap_top]
|
2011-10-14 23:38:50 +02:00
|
|
|
shr eax, 12
|
|
|
|
cmp edx, eax
|
|
|
|
jae .cant_inplace
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [page_tabs + edx*4]
|
|
|
|
test al, MEM_BLOCK_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
jz .cant_inplace
|
|
|
|
shr eax, 12
|
|
|
|
add eax, edx
|
|
|
|
sub eax, ebx
|
|
|
|
jb .cant_inplace
|
|
|
|
jz @f
|
|
|
|
shl eax, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
or al, MEM_BLOCK_FREE
|
|
|
|
mov [page_tabs + ebx*4], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
@@:
|
|
|
|
mov eax, ebx
|
|
|
|
sub eax, ecx
|
|
|
|
shl eax, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
or al, MEM_BLOCK_USED
|
|
|
|
mov [page_tabs + ecx*4], eax
|
|
|
|
lea eax, [ecx + 1]
|
2011-10-14 23:38:50 +02:00
|
|
|
shl eax, 12
|
|
|
|
push eax
|
|
|
|
push edi
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edi, [page_tabs + edx*4]
|
2011-10-14 23:38:50 +02:00
|
|
|
mov eax, 2
|
|
|
|
sub ebx, edx
|
|
|
|
mov ecx, ebx
|
|
|
|
cld
|
|
|
|
rep stosd
|
|
|
|
pop edi
|
2014-09-18 09:58:56 +02:00
|
|
|
mov edx, [current_process]
|
2011-10-14 23:38:50 +02:00
|
|
|
shl ebx, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
add [edx + PROC.mem_used], ebx
|
2014-09-18 09:58:56 +02:00
|
|
|
|
|
|
|
mov ecx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ecx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_unlock
|
2011-10-14 23:38:50 +02:00
|
|
|
pop eax edx ecx
|
|
|
|
ret
|
2014-09-18 09:58:56 +02:00
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
.cant_inplace:
|
|
|
|
push esi edi
|
2014-09-18 09:58:56 +02:00
|
|
|
mov eax, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
mov esi, [eax + PROC.heap_base]
|
|
|
|
mov edi, [eax + PROC.heap_top]
|
2011-10-14 23:38:50 +02:00
|
|
|
shr esi, 12
|
|
|
|
shr edi, 12
|
|
|
|
sub ebx, ecx
|
|
|
|
.find_place:
|
|
|
|
cmp esi, edi
|
|
|
|
jae .place_not_found
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [page_tabs + esi*4]
|
|
|
|
test al, MEM_BLOCK_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
jz .next_place
|
|
|
|
shr eax, 12
|
|
|
|
cmp eax, ebx
|
|
|
|
jae .place_found
|
|
|
|
add esi, eax
|
|
|
|
jmp .find_place
|
|
|
|
.next_place:
|
|
|
|
shr eax, 12
|
|
|
|
add esi, eax
|
|
|
|
jmp .find_place
|
|
|
|
.place_not_found:
|
|
|
|
pop edi esi
|
|
|
|
jmp .ret0
|
|
|
|
.place_found:
|
|
|
|
sub eax, ebx
|
|
|
|
jz @f
|
|
|
|
push esi
|
|
|
|
add esi, ebx
|
|
|
|
shl eax, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
or al, MEM_BLOCK_FREE
|
|
|
|
mov [page_tabs + esi*4], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
pop esi
|
|
|
|
@@:
|
|
|
|
mov eax, ebx
|
|
|
|
shl eax, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
or al, MEM_BLOCK_USED
|
|
|
|
mov [page_tabs + esi*4], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
inc esi
|
|
|
|
mov eax, esi
|
|
|
|
shl eax, 12
|
|
|
|
push eax
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [page_tabs + ecx*4]
|
2023-02-15 00:26:59 +01:00
|
|
|
and eax, -PAGE_SIZE
|
2020-05-21 15:55:36 +02:00
|
|
|
or al, MEM_BLOCK_FREE
|
2011-10-14 23:38:50 +02:00
|
|
|
sub edx, ecx
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [page_tabs + ecx*4], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
inc ecx
|
|
|
|
dec ebx
|
|
|
|
dec edx
|
|
|
|
jz .no
|
|
|
|
@@:
|
|
|
|
xor eax, eax
|
2020-05-21 15:55:36 +02:00
|
|
|
xchg eax, [page_tabs + ecx*4]
|
|
|
|
mov [page_tabs + esi*4], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
mov eax, ecx
|
|
|
|
shl eax, 12
|
|
|
|
invlpg [eax]
|
|
|
|
inc esi
|
|
|
|
inc ecx
|
|
|
|
dec ebx
|
|
|
|
dec edx
|
|
|
|
jnz @b
|
|
|
|
.no:
|
|
|
|
push ebx
|
2014-09-18 09:58:56 +02:00
|
|
|
mov edx, [current_process]
|
2011-10-14 23:38:50 +02:00
|
|
|
shl ebx, 12
|
2020-05-21 15:55:36 +02:00
|
|
|
add [edx + PROC.mem_used], ebx
|
2011-10-14 23:38:50 +02:00
|
|
|
pop ebx
|
|
|
|
@@:
|
2020-05-21 15:55:36 +02:00
|
|
|
mov dword [page_tabs + esi*4], MEM_BLOCK_RESERVED
|
2011-10-14 23:38:50 +02:00
|
|
|
inc esi
|
|
|
|
dec ebx
|
|
|
|
jnz @b
|
|
|
|
|
2014-09-18 09:58:56 +02:00
|
|
|
mov ecx, [current_process]
|
2020-05-21 15:55:36 +02:00
|
|
|
lea ecx, [ecx + PROC.heap_lock]
|
2014-09-18 09:58:56 +02:00
|
|
|
call mutex_unlock
|
|
|
|
pop eax edi esi edx ecx
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-18 09:58:56 +02:00
|
|
|
;;;;;;;;;;;;;; SHARED MEMORY ;;;;;;;;;;;;;;;;;
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
; param
|
|
|
|
; eax= shm_map object
|
|
|
|
|
|
|
|
align 4
|
|
|
|
destroy_smap:
|
|
|
|
|
|
|
|
pushfd
|
|
|
|
cli
|
|
|
|
|
|
|
|
push esi
|
|
|
|
push edi
|
|
|
|
|
|
|
|
mov edi, eax
|
2020-05-21 15:55:36 +02:00
|
|
|
mov esi, [eax + SMAP.parent]
|
2011-10-14 23:38:50 +02:00
|
|
|
test esi, esi
|
|
|
|
jz .done
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
lock dec [esi + SMEM.refcount]
|
2011-10-14 23:38:50 +02:00
|
|
|
jnz .done
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov ecx, [esi + SMEM.bk]
|
|
|
|
mov edx, [esi + SMEM.fd]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [ecx + SMEM.fd], edx
|
|
|
|
mov [edx + SMEM.bk], ecx
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
stdcall kernel_free, [esi + SMEM.base]
|
2011-10-14 23:38:50 +02:00
|
|
|
mov eax, esi
|
|
|
|
call free
|
|
|
|
.done:
|
|
|
|
mov eax, edi
|
|
|
|
call destroy_kernel_object
|
|
|
|
|
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
popfd
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2017-12-20 02:07:10 +01:00
|
|
|
E_NOTFOUND = 5
|
|
|
|
E_ACCESS = 10
|
|
|
|
E_NOMEM = 30
|
|
|
|
E_PARAM = 33
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2017-12-20 02:07:10 +01:00
|
|
|
SHM_READ = 0
|
|
|
|
SHM_WRITE = 1
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2017-12-20 02:07:10 +01:00
|
|
|
SHM_ACCESS_MASK = 3
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2017-12-20 02:07:10 +01:00
|
|
|
SHM_OPEN = 0 shl 2
|
|
|
|
SHM_OPEN_ALWAYS = 1 shl 2
|
|
|
|
SHM_CREATE = 2 shl 2
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2017-12-20 02:07:10 +01:00
|
|
|
SHM_OPEN_MASK = 3 shl 2
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
align 4
|
|
|
|
proc shmem_open stdcall name:dword, size:dword, access:dword
|
2013-02-17 13:07:32 +01:00
|
|
|
locals
|
|
|
|
action dd ?
|
|
|
|
owner_access dd ?
|
|
|
|
mapped dd ?
|
|
|
|
endl
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
push ebx
|
|
|
|
push esi
|
|
|
|
push edi
|
|
|
|
|
|
|
|
mov [mapped], 0
|
|
|
|
mov [owner_access], 0
|
|
|
|
|
|
|
|
pushfd ;mutex required
|
|
|
|
cli
|
|
|
|
|
|
|
|
mov eax, [access]
|
|
|
|
and eax, SHM_OPEN_MASK
|
|
|
|
mov [action], eax
|
|
|
|
|
|
|
|
mov ebx, [name]
|
|
|
|
test ebx, ebx
|
|
|
|
mov edx, E_PARAM
|
|
|
|
jz .fail
|
|
|
|
|
|
|
|
mov esi, [shmem_list.fd]
|
|
|
|
align 4
|
|
|
|
@@:
|
|
|
|
cmp esi, shmem_list
|
|
|
|
je .not_found
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edx, [esi + SMEM.name]; link , base, size
|
2011-10-14 23:38:50 +02:00
|
|
|
stdcall strncmp, edx, ebx, 32
|
|
|
|
test eax, eax
|
|
|
|
je .found
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov esi, [esi + SMEM.fd]
|
2011-10-14 23:38:50 +02:00
|
|
|
jmp @B
|
|
|
|
|
|
|
|
.not_found:
|
|
|
|
mov eax, [action]
|
|
|
|
|
|
|
|
cmp eax, SHM_OPEN
|
|
|
|
mov edx, E_NOTFOUND
|
|
|
|
je .fail
|
|
|
|
|
|
|
|
cmp eax, SHM_CREATE
|
|
|
|
mov edx, E_PARAM
|
|
|
|
je .create_shm
|
|
|
|
|
|
|
|
cmp eax, SHM_OPEN_ALWAYS
|
|
|
|
jne .fail
|
|
|
|
|
|
|
|
.create_shm:
|
|
|
|
|
|
|
|
mov ecx, [size]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .fail
|
|
|
|
|
2023-02-15 00:26:59 +01:00
|
|
|
add ecx, PAGE_SIZE-1
|
|
|
|
and ecx, -PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
mov [size], ecx
|
|
|
|
|
2012-02-23 19:52:13 +01:00
|
|
|
mov eax, sizeof.SMEM
|
2011-10-14 23:38:50 +02:00
|
|
|
call malloc
|
|
|
|
test eax, eax
|
|
|
|
mov esi, eax
|
|
|
|
mov edx, E_NOMEM
|
|
|
|
jz .fail
|
|
|
|
|
|
|
|
stdcall kernel_alloc, [size]
|
|
|
|
test eax, eax
|
|
|
|
mov [mapped], eax
|
|
|
|
mov edx, E_NOMEM
|
|
|
|
jz .cleanup
|
|
|
|
|
|
|
|
mov ecx, [size]
|
|
|
|
mov edx, [access]
|
|
|
|
and edx, SHM_ACCESS_MASK
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [esi + SMEM.base], eax
|
|
|
|
mov [esi + SMEM.size], ecx
|
|
|
|
mov [esi + SMEM.access], edx
|
|
|
|
mov [esi + SMEM.refcount], 0
|
|
|
|
mov [esi + SMEM.name + 28], 0
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
lea eax, [esi + SMEM.name]
|
2011-10-14 23:38:50 +02:00
|
|
|
stdcall strncpy, eax, [name], 31
|
|
|
|
|
|
|
|
mov eax, [shmem_list.fd]
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [esi + SMEM.bk], shmem_list
|
|
|
|
mov [esi + SMEM.fd], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [eax + SMEM.bk], esi
|
2011-10-14 23:38:50 +02:00
|
|
|
mov [shmem_list.fd], esi
|
|
|
|
|
|
|
|
mov [action], SHM_OPEN
|
|
|
|
mov [owner_access], SHM_WRITE
|
|
|
|
|
|
|
|
.found:
|
|
|
|
mov eax, [action]
|
|
|
|
|
|
|
|
cmp eax, SHM_CREATE
|
|
|
|
mov edx, E_ACCESS
|
|
|
|
je .exit
|
|
|
|
|
|
|
|
cmp eax, SHM_OPEN
|
|
|
|
mov edx, E_PARAM
|
|
|
|
je .create_map
|
|
|
|
|
|
|
|
cmp eax, SHM_OPEN_ALWAYS
|
|
|
|
jne .fail
|
|
|
|
|
|
|
|
.create_map:
|
|
|
|
|
|
|
|
mov eax, [access]
|
|
|
|
and eax, SHM_ACCESS_MASK
|
2020-05-21 15:55:36 +02:00
|
|
|
cmp eax, [esi + SMEM.access]
|
2011-10-14 23:38:50 +02:00
|
|
|
mov [access], eax
|
|
|
|
mov edx, E_ACCESS
|
|
|
|
ja .fail
|
|
|
|
|
2022-06-04 01:34:04 +02:00
|
|
|
mov ebx, [current_slot]
|
|
|
|
mov ebx, [ebx + APPDATA.tid]
|
2012-02-23 19:52:13 +01:00
|
|
|
mov eax, sizeof.SMAP
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
call create_kernel_object
|
|
|
|
test eax, eax
|
|
|
|
mov edi, eax
|
|
|
|
mov edx, E_NOMEM
|
|
|
|
jz .fail
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
inc [esi + SMEM.refcount]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [edi + SMAP.magic], 'SMAP'
|
|
|
|
mov [edi + SMAP.destroy], destroy_smap
|
|
|
|
mov [edi + SMAP.parent], esi
|
|
|
|
mov [edi + SMAP.base], 0
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
stdcall user_alloc, [esi + SMEM.size]
|
2011-10-14 23:38:50 +02:00
|
|
|
test eax, eax
|
|
|
|
mov [mapped], eax
|
|
|
|
mov edx, E_NOMEM
|
|
|
|
jz .cleanup2
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov [edi + SMAP.base], eax
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov ecx, [esi + SMEM.size]
|
2011-10-14 23:38:50 +02:00
|
|
|
mov [size], ecx
|
|
|
|
|
|
|
|
shr ecx, 12
|
|
|
|
shr eax, 10
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov esi, [esi + SMEM.base]
|
2011-10-14 23:38:50 +02:00
|
|
|
shr esi, 10
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edi, [page_tabs + eax]
|
2011-10-14 23:38:50 +02:00
|
|
|
add esi, page_tabs
|
|
|
|
|
|
|
|
mov edx, [access]
|
|
|
|
or edx, [owner_access]
|
|
|
|
shl edx, 1
|
2020-05-21 15:55:36 +02:00
|
|
|
or edx, PG_SHARED + PG_UR
|
2011-10-14 23:38:50 +02:00
|
|
|
@@:
|
|
|
|
lodsd
|
2023-02-15 00:26:59 +01:00
|
|
|
and eax, -PAGE_SIZE
|
2011-10-14 23:38:50 +02:00
|
|
|
or eax, edx
|
|
|
|
stosd
|
|
|
|
loop @B
|
|
|
|
|
|
|
|
xor edx, edx
|
|
|
|
|
|
|
|
cmp [owner_access], 0
|
|
|
|
jne .fail
|
|
|
|
.exit:
|
|
|
|
mov edx, [size]
|
|
|
|
.fail:
|
|
|
|
mov eax, [mapped]
|
|
|
|
|
|
|
|
popfd
|
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
|
|
|
ret
|
|
|
|
.cleanup:
|
|
|
|
mov [size], edx
|
|
|
|
mov eax, esi
|
|
|
|
call free
|
|
|
|
jmp .exit
|
|
|
|
|
|
|
|
.cleanup2:
|
|
|
|
mov [size], edx
|
|
|
|
mov eax, edi
|
|
|
|
call destroy_smap
|
|
|
|
jmp .exit
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc shmem_close stdcall, name:dword
|
|
|
|
|
|
|
|
mov eax, [name]
|
|
|
|
test eax, eax
|
|
|
|
jz .fail
|
|
|
|
|
2021-12-06 17:57:50 +01:00
|
|
|
push ebx ; Added
|
2011-10-14 23:38:50 +02:00
|
|
|
push esi
|
|
|
|
push edi
|
|
|
|
pushfd
|
|
|
|
cli
|
|
|
|
|
|
|
|
mov esi, [current_slot]
|
|
|
|
add esi, APP_OBJ_OFFSET
|
2021-12-06 17:57:50 +01:00
|
|
|
mov ebx, esi ; Fixed endless loop bug with not existing name (part 1)
|
2011-10-14 23:38:50 +02:00
|
|
|
.next:
|
2020-05-21 15:55:36 +02:00
|
|
|
mov eax, [esi + APPOBJ.fd]
|
2011-10-14 23:38:50 +02:00
|
|
|
test eax, eax
|
|
|
|
jz @F
|
|
|
|
|
2021-12-06 17:57:50 +01:00
|
|
|
cmp eax, ebx ;esi ; Fixed endless loop bug with not existing name (part 2)
|
|
|
|
je @F ; Small optimization
|
2011-10-14 23:38:50 +02:00
|
|
|
mov esi, eax
|
2021-12-06 17:57:50 +01:00
|
|
|
;je @F
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
cmp [eax + SMAP.magic], 'SMAP'
|
2011-10-14 23:38:50 +02:00
|
|
|
jne .next
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
mov edi, [eax + SMAP.parent]
|
2011-10-14 23:38:50 +02:00
|
|
|
test edi, edi
|
|
|
|
jz .next
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
lea edi, [edi + SMEM.name]
|
2011-10-14 23:38:50 +02:00
|
|
|
stdcall strncmp, [name], edi, 32
|
|
|
|
test eax, eax
|
|
|
|
jne .next
|
|
|
|
|
2020-05-21 15:55:36 +02:00
|
|
|
stdcall user_free, [esi + SMAP.base]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov eax, esi
|
2020-05-21 15:55:36 +02:00
|
|
|
call [esi + APPOBJ.destroy]
|
2011-10-14 23:38:50 +02:00
|
|
|
@@:
|
|
|
|
popfd
|
|
|
|
pop edi
|
|
|
|
pop esi
|
2021-12-06 17:57:50 +01:00
|
|
|
pop ebx ; Added
|
2011-10-14 23:38:50 +02:00
|
|
|
.fail:
|
|
|
|
ret
|
|
|
|
endp
|
2020-05-21 15:55:36 +02:00
|
|
|
|
2020-05-22 17:38:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
proc user_ring stdcall, size:dword
|
|
|
|
|
|
|
|
locals
|
|
|
|
virt_ptr dd ?
|
|
|
|
phys_ptr dd ?
|
|
|
|
num_pages dd ?
|
|
|
|
endl
|
|
|
|
|
|
|
|
; Size must be an exact multiple of pagesize
|
2020-05-22 17:44:06 +02:00
|
|
|
mov eax, [size]
|
2020-05-22 17:38:59 +02:00
|
|
|
test eax, PAGE_SIZE-1
|
|
|
|
jnz .exit
|
|
|
|
|
|
|
|
; We must have at least one complete page
|
|
|
|
shr eax, 12
|
|
|
|
jz .exit
|
|
|
|
mov [num_pages], eax
|
|
|
|
|
|
|
|
; Allocate double the virtual memory
|
|
|
|
mov eax, [size]
|
|
|
|
shl eax, 1
|
|
|
|
jz .exit
|
|
|
|
stdcall user_alloc, eax
|
|
|
|
test eax, eax
|
|
|
|
jz .exit
|
|
|
|
mov [virt_ptr], eax
|
|
|
|
|
|
|
|
; Now allocate physical memory
|
|
|
|
stdcall alloc_pages, [num_pages]
|
|
|
|
test eax, eax
|
|
|
|
jz .exit_free_virt
|
|
|
|
mov [phys_ptr], eax
|
|
|
|
|
|
|
|
; Map first half of virtual memory to physical memory
|
|
|
|
push ecx esi edi
|
|
|
|
mov ecx, [num_pages]
|
|
|
|
mov esi, [virt_ptr]
|
|
|
|
mov edi, [phys_ptr]
|
|
|
|
.loop1:
|
|
|
|
stdcall map_page, esi, edi, PG_UWR
|
|
|
|
add esi, PAGE_SIZE
|
|
|
|
add edi, PAGE_SIZE
|
|
|
|
dec ecx
|
|
|
|
jnz .loop1
|
|
|
|
|
|
|
|
; Map second half of virtual memory to same physical memory
|
2020-05-22 17:44:06 +02:00
|
|
|
mov ecx, [num_pages]
|
2020-05-22 17:38:59 +02:00
|
|
|
mov edi, [phys_ptr]
|
|
|
|
.loop2:
|
|
|
|
stdcall map_page, esi, edi, PG_UWR
|
|
|
|
add esi, PAGE_SIZE
|
|
|
|
add edi, PAGE_SIZE
|
|
|
|
dec ecx
|
|
|
|
jnz .loop2
|
|
|
|
pop edi esi ecx
|
|
|
|
|
|
|
|
mov eax, [virt_ptr]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.exit_free_virt:
|
|
|
|
stdcall user_free, [virt_ptr]
|
|
|
|
|
|
|
|
.exit:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|