2007-03-21 23:58:33 +01:00
|
|
|
$Revision$
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
struc MEM_BLOCK
|
|
|
|
{ .next_block dd ?
|
|
|
|
.prev_block dd ? ;+4
|
2007-02-17 11:09:23 +01:00
|
|
|
.list_fd dd ? ;+8
|
|
|
|
.list_bk dd ? ;+12
|
2006-10-06 08:09:41 +02:00
|
|
|
.base dd ? ;+16
|
|
|
|
.size dd ? ;+20
|
|
|
|
.flags dd ? ;+24
|
|
|
|
.handle dd ? ;+28
|
|
|
|
}
|
|
|
|
|
2007-02-17 11:09:23 +01:00
|
|
|
MEM_LIST_OFFSET equ 8
|
2006-10-06 08:09:41 +02:00
|
|
|
FREE_BLOCK equ 4
|
|
|
|
USED_BLOCK equ 8
|
|
|
|
|
|
|
|
virtual at 0
|
|
|
|
MEM_BLOCK MEM_BLOCK
|
|
|
|
end virtual
|
|
|
|
|
|
|
|
MEM_BLOCK_SIZE equ 8*4
|
|
|
|
|
|
|
|
block_next equ MEM_BLOCK.next_block
|
|
|
|
block_prev equ MEM_BLOCK.prev_block
|
2007-02-17 11:09:23 +01:00
|
|
|
list_fd equ MEM_BLOCK.list_fd
|
|
|
|
list_bk equ MEM_BLOCK.list_bk
|
2006-10-06 08:09:41 +02:00
|
|
|
block_base equ MEM_BLOCK.base
|
|
|
|
block_size equ MEM_BLOCK.size
|
|
|
|
block_flags equ MEM_BLOCK.flags
|
|
|
|
|
|
|
|
macro calc_index op
|
|
|
|
{ shr op, 12
|
|
|
|
dec op
|
|
|
|
cmp op, 63
|
|
|
|
jna @f
|
|
|
|
mov op, 63
|
|
|
|
@@:
|
|
|
|
}
|
|
|
|
|
|
|
|
macro remove_from_list op
|
2007-02-17 11:09:23 +01:00
|
|
|
{ mov edx, [op+list_fd]
|
|
|
|
mov ecx, [op+list_bk]
|
2006-10-06 08:09:41 +02:00
|
|
|
test edx, edx
|
|
|
|
jz @f
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edx+list_bk], ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
|
|
|
test ecx, ecx
|
|
|
|
jz @f
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [ecx+list_fd], edx
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [op+list_fd],0
|
|
|
|
mov [op+list_bk],0
|
2006-10-06 08:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
macro remove_from_free op
|
|
|
|
{
|
|
|
|
remove_from_list op
|
|
|
|
|
|
|
|
mov eax, [op+block_size]
|
|
|
|
calc_index eax
|
|
|
|
cmp [mem_block_list+eax*4], op
|
|
|
|
jne @f
|
|
|
|
mov [mem_block_list+eax*4], edx
|
|
|
|
@@:
|
|
|
|
cmp [mem_block_list+eax*4], 0
|
|
|
|
jne @f
|
|
|
|
btr [mem_block_mask], eax
|
|
|
|
@@:
|
|
|
|
}
|
|
|
|
|
|
|
|
macro remove_from_used op
|
|
|
|
{
|
2007-02-17 11:09:23 +01:00
|
|
|
mov edx, [op+list_fd]
|
|
|
|
mov ecx, [op+list_bk]
|
|
|
|
mov [edx+list_bk], ecx
|
|
|
|
mov [ecx+list_fd], edx
|
|
|
|
mov [op+list_fd], 0
|
|
|
|
mov [op+list_bk], 0
|
2006-10-06 08:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc init_kernel_heap
|
|
|
|
|
|
|
|
mov ecx, 64/4
|
|
|
|
mov edi, mem_block_list
|
|
|
|
xor eax, eax
|
|
|
|
cld
|
|
|
|
rep stosd
|
|
|
|
|
|
|
|
mov ecx, 512/4
|
|
|
|
mov edi, mem_block_map
|
|
|
|
not eax
|
|
|
|
rep stosd
|
|
|
|
|
|
|
|
mov [mem_block_start], mem_block_map
|
|
|
|
mov [mem_block_end], mem_block_map+512
|
|
|
|
mov [mem_block_arr], HEAP_BASE
|
|
|
|
|
2007-02-17 11:09:23 +01:00
|
|
|
mov eax, mem_used.fd-MEM_LIST_OFFSET
|
|
|
|
mov [mem_used.fd], eax
|
|
|
|
mov [mem_used.bk], eax
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
stdcall alloc_pages, dword 32
|
|
|
|
mov ecx, 32
|
|
|
|
mov edx, eax
|
|
|
|
mov edi, HEAP_BASE
|
|
|
|
.l1:
|
|
|
|
stdcall map_page,edi,edx,PG_SW
|
|
|
|
add edi, 0x1000
|
|
|
|
add edx, 0x1000
|
|
|
|
dec ecx
|
|
|
|
jnz .l1
|
|
|
|
|
|
|
|
mov edi, HEAP_BASE
|
2007-02-17 11:09:23 +01:00
|
|
|
mov ebx, HEAP_BASE+MEM_BLOCK_SIZE
|
2006-10-06 08:09:41 +02:00
|
|
|
xor eax, eax
|
|
|
|
mov [edi+block_next], ebx
|
|
|
|
mov [edi+block_prev], eax
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edi+list_fd], eax
|
|
|
|
mov [edi+list_bk], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [edi+block_base], HEAP_BASE
|
|
|
|
mov [edi+block_size], 4096*MEM_BLOCK_SIZE
|
|
|
|
mov [edi+block_flags], USED_BLOCK
|
|
|
|
|
|
|
|
mov [ebx+block_next], eax
|
|
|
|
mov [ebx+block_prev], eax
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [ebx+list_fd], eax
|
|
|
|
mov [ebx+list_bk], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [ebx+block_base], HEAP_BASE+4096*MEM_BLOCK_SIZE
|
|
|
|
|
|
|
|
mov ecx, [MEM_AMOUNT]
|
2006-11-16 11:59:49 +01:00
|
|
|
sub ecx, HEAP_BASE + 4096*MEM_BLOCK_SIZE
|
2006-10-08 09:48:38 +02:00
|
|
|
mov [heap_size], ecx
|
|
|
|
mov [heap_free], ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [ebx+block_size], ecx
|
|
|
|
mov [ebx+block_flags], FREE_BLOCK
|
|
|
|
|
|
|
|
mov [mem_block_mask], eax
|
|
|
|
mov [mem_block_mask+4],0x80000000
|
|
|
|
|
|
|
|
mov [mem_block_list+63*4], ebx
|
|
|
|
mov byte [mem_block_map], 0xFC
|
2007-01-18 08:31:56 +01:00
|
|
|
and [heap_mutex], 0
|
2006-10-08 09:48:38 +02:00
|
|
|
mov [heap_blocks], 4095
|
|
|
|
mov [free_blocks], 4095
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
2007-02-22 07:41:46 +01:00
|
|
|
; param
|
|
|
|
; eax= required size
|
|
|
|
;
|
|
|
|
; retval
|
|
|
|
; edi= memory block descriptor
|
|
|
|
; ebx= descriptor index
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2007-02-22 07:41:46 +01:00
|
|
|
align 4
|
|
|
|
get_block:
|
2006-10-06 08:09:41 +02:00
|
|
|
mov ecx, eax
|
2007-02-22 07:41:46 +01:00
|
|
|
shr ecx, 12
|
|
|
|
dec ecx
|
|
|
|
cmp ecx, 63
|
|
|
|
jle .get_index
|
|
|
|
mov ecx, 63
|
|
|
|
.get_index:
|
|
|
|
lea esi, [mem_block_mask]
|
2006-10-06 08:09:41 +02:00
|
|
|
xor ebx, ebx
|
2007-02-22 07:41:46 +01:00
|
|
|
or edx, -1
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
cmp ecx, 32
|
|
|
|
jb .bit_test
|
|
|
|
|
|
|
|
sub ecx, 32
|
|
|
|
add ebx, 32
|
|
|
|
add esi, 4
|
|
|
|
.bit_test:
|
|
|
|
shl edx, cl
|
2007-02-22 07:41:46 +01:00
|
|
|
and edx, [esi]
|
|
|
|
.find:
|
|
|
|
bsf edi, edx
|
2006-10-06 08:09:41 +02:00
|
|
|
jz .high_mask
|
2007-02-22 07:41:46 +01:00
|
|
|
add ebx, edi
|
|
|
|
mov edi, [mem_block_list+ebx*4]
|
|
|
|
.check_size:
|
|
|
|
cmp eax, [edi+block_size]
|
|
|
|
ja .next
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.high_mask:
|
|
|
|
add esi, 4
|
2007-02-22 07:41:46 +01:00
|
|
|
cmp esi, mem_block_mask+8
|
|
|
|
jae .err
|
2006-10-06 08:09:41 +02:00
|
|
|
add ebx, 32
|
2007-02-22 07:41:46 +01:00
|
|
|
mov edx, [esi]
|
|
|
|
jmp .find
|
|
|
|
.next:
|
|
|
|
mov edi, [edi+list_fd]
|
|
|
|
test edi, edi
|
|
|
|
jnz .check_size
|
|
|
|
.err:
|
|
|
|
xor edi, edi
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc alloc_mem_block
|
|
|
|
|
|
|
|
mov ebx, [mem_block_start]
|
|
|
|
mov ecx, [mem_block_end]
|
|
|
|
.l1:
|
|
|
|
bsf eax,[ebx];
|
|
|
|
jnz found
|
|
|
|
add ebx,4
|
|
|
|
cmp ebx, ecx
|
|
|
|
jb .l1
|
|
|
|
xor eax,eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
found:
|
|
|
|
btr [ebx], eax
|
|
|
|
mov [mem_block_start],ebx
|
|
|
|
sub ebx, mem_block_map
|
2006-12-29 08:12:27 +01:00
|
|
|
lea eax,[eax+ebx*8]
|
2006-10-06 08:09:41 +02:00
|
|
|
shl eax, 5
|
|
|
|
add eax, [mem_block_arr]
|
2006-10-08 09:48:38 +02:00
|
|
|
dec [free_blocks]
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
proc free_mem_block
|
2007-02-17 11:09:23 +01:00
|
|
|
mov dword [eax], 0
|
|
|
|
mov dword [eax+4], 0
|
|
|
|
mov dword [eax+8], 0
|
|
|
|
mov dword [eax+12], 0
|
|
|
|
mov dword [eax+16], 0
|
|
|
|
; mov dword [eax+20], 0
|
|
|
|
mov dword [eax+24], 0
|
|
|
|
mov dword [eax+28], 0
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
sub eax, [mem_block_arr]
|
|
|
|
shr eax, 5
|
|
|
|
|
|
|
|
mov ebx, mem_block_map
|
|
|
|
bts [ebx], eax
|
2006-10-08 09:48:38 +02:00
|
|
|
inc [free_blocks]
|
2006-10-06 08:09:41 +02:00
|
|
|
shr eax, 3
|
|
|
|
and eax, not 3
|
|
|
|
add eax, ebx
|
|
|
|
cmp [mem_block_start], eax
|
|
|
|
ja @f
|
|
|
|
ret
|
|
|
|
@@:
|
|
|
|
mov [mem_block_start], eax
|
|
|
|
ret
|
|
|
|
.err:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc alloc_kernel_space stdcall, size:dword
|
|
|
|
local block_ind:DWORD
|
|
|
|
|
|
|
|
mov eax, [size]
|
2006-11-03 16:19:15 +01:00
|
|
|
add eax, 4095
|
|
|
|
and eax, not 4095
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [size], eax
|
2007-01-18 08:31:56 +01:00
|
|
|
|
|
|
|
mov ebx, heap_mutex
|
|
|
|
call wait_mutex ;ebx
|
|
|
|
|
2006-10-08 09:48:38 +02:00
|
|
|
cmp eax, [heap_free]
|
|
|
|
ja .error
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2007-02-22 07:41:46 +01:00
|
|
|
call get_block ; eax
|
|
|
|
test edi, edi
|
2006-10-06 08:09:41 +02:00
|
|
|
jz .error
|
|
|
|
|
|
|
|
cmp [edi+block_flags], FREE_BLOCK
|
|
|
|
jne .error
|
|
|
|
|
|
|
|
mov [block_ind], ebx ;index of allocated block
|
|
|
|
|
|
|
|
mov eax, [edi+block_size]
|
|
|
|
cmp eax, [size]
|
|
|
|
je .m_eq_size
|
|
|
|
|
|
|
|
call alloc_mem_block
|
|
|
|
and eax, eax
|
|
|
|
jz .error
|
|
|
|
|
|
|
|
mov esi, eax ;esi - splitted block
|
|
|
|
|
|
|
|
mov [esi+block_next], edi
|
|
|
|
mov eax, [edi+block_prev]
|
|
|
|
mov [esi+block_prev], eax
|
|
|
|
mov [edi+block_prev], esi
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [esi+list_fd], 0
|
|
|
|
mov [esi+list_bk], 0
|
2006-10-06 08:09:41 +02:00
|
|
|
and eax, eax
|
|
|
|
jz @f
|
|
|
|
mov [eax+block_next], esi
|
|
|
|
@@:
|
|
|
|
mov ebx, [edi+block_base]
|
|
|
|
mov [esi+block_base], ebx
|
|
|
|
mov edx, [size]
|
|
|
|
mov [esi+block_size], edx
|
|
|
|
add [edi+block_base], edx
|
|
|
|
sub [edi+block_size], edx
|
|
|
|
|
|
|
|
mov eax, [edi+block_size]
|
|
|
|
shr eax, 12
|
|
|
|
sub eax, 1
|
|
|
|
cmp eax, 63
|
|
|
|
jna @f
|
|
|
|
mov eax, 63
|
|
|
|
@@:
|
|
|
|
cmp eax, [block_ind]
|
|
|
|
je .m_eq_ind
|
|
|
|
|
2006-10-23 22:38:29 +02:00
|
|
|
remove_from_list edi
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
mov ecx, [block_ind]
|
2006-11-16 06:47:31 +01:00
|
|
|
mov [mem_block_list+ecx*4], edx
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2006-11-16 06:47:31 +01:00
|
|
|
test edx, edx
|
2006-10-06 08:09:41 +02:00
|
|
|
jnz @f
|
|
|
|
btr [mem_block_mask], ecx
|
|
|
|
@@:
|
|
|
|
mov edx, [mem_block_list+eax*4]
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edi+list_fd], edx
|
2006-10-06 08:09:41 +02:00
|
|
|
test edx, edx
|
|
|
|
jz @f
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edx+list_bk], edi
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
|
|
|
mov [mem_block_list+eax*4], edi
|
|
|
|
bts [mem_block_mask], eax
|
|
|
|
.m_eq_ind:
|
2007-02-17 11:09:23 +01:00
|
|
|
mov ecx, mem_used.fd-MEM_LIST_OFFSET
|
|
|
|
mov edx, [ecx+list_fd]
|
|
|
|
mov [esi+list_fd], edx
|
|
|
|
mov [esi+list_bk], ecx
|
|
|
|
mov [ecx+list_fd], esi
|
|
|
|
mov [edx+list_bk], esi
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [esi+block_flags], USED_BLOCK
|
|
|
|
mov eax, [esi+block_base]
|
2006-10-08 09:48:38 +02:00
|
|
|
mov ebx, [size]
|
|
|
|
sub [heap_free], ebx
|
2007-01-18 08:31:56 +01:00
|
|
|
and [heap_mutex], 0
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
.m_eq_size:
|
|
|
|
remove_from_list edi
|
2006-10-23 22:38:29 +02:00
|
|
|
mov [mem_block_list+ebx*4], edx
|
2006-10-06 08:09:41 +02:00
|
|
|
and edx, edx
|
|
|
|
jnz @f
|
2006-10-23 22:38:29 +02:00
|
|
|
btr [mem_block_mask], ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
2007-02-17 11:09:23 +01:00
|
|
|
mov ecx, mem_used.fd-MEM_LIST_OFFSET
|
|
|
|
mov edx, [ecx+list_fd]
|
|
|
|
mov [edi+list_fd], edx
|
|
|
|
mov [edi+list_bk], ecx
|
|
|
|
mov [ecx+list_fd], edi
|
|
|
|
mov [edx+list_bk], edi
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [edi+block_flags], USED_BLOCK
|
|
|
|
mov eax, [edi+block_base]
|
2006-10-08 09:48:38 +02:00
|
|
|
mov ebx, [size]
|
|
|
|
sub [heap_free], ebx
|
2007-01-18 08:31:56 +01:00
|
|
|
and [heap_mutex], 0
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
.error:
|
|
|
|
xor eax, eax
|
2007-01-18 08:31:56 +01:00
|
|
|
mov [heap_mutex], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
2007-02-05 15:20:36 +01:00
|
|
|
proc free_kernel_space stdcall uses ebx ecx edx esi edi, base:dword
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2007-01-18 08:31:56 +01:00
|
|
|
mov ebx, heap_mutex
|
|
|
|
call wait_mutex ;ebx
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov eax, [base]
|
2007-02-17 11:09:23 +01:00
|
|
|
mov esi, [mem_used.fd]
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
2007-02-17 11:09:23 +01:00
|
|
|
cmp esi, mem_used.fd-MEM_LIST_OFFSET
|
|
|
|
je .fail
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
cmp [esi+block_base], eax
|
|
|
|
je .found
|
2007-02-17 11:09:23 +01:00
|
|
|
mov esi, [esi+list_fd]
|
2006-10-06 08:09:41 +02:00
|
|
|
jmp @b
|
|
|
|
.found:
|
|
|
|
cmp [esi+block_flags], USED_BLOCK
|
|
|
|
jne .fail
|
|
|
|
|
2006-10-08 09:48:38 +02:00
|
|
|
mov eax, [esi+block_size]
|
|
|
|
add [heap_free], eax
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov edi, [esi+block_next]
|
|
|
|
test edi, edi
|
|
|
|
jz .prev
|
|
|
|
|
|
|
|
cmp [edi+block_flags], FREE_BLOCK
|
|
|
|
jne .prev
|
|
|
|
|
|
|
|
remove_from_free edi
|
|
|
|
|
|
|
|
mov edx, [edi+block_next]
|
|
|
|
mov [esi+block_next], edx
|
|
|
|
test edx, edx
|
|
|
|
jz @f
|
|
|
|
|
|
|
|
mov [edx+block_prev], esi
|
|
|
|
@@:
|
|
|
|
mov ecx, [edi+block_size]
|
|
|
|
add [esi+block_size], ecx
|
|
|
|
|
|
|
|
mov eax, edi
|
|
|
|
call free_mem_block
|
|
|
|
.prev:
|
|
|
|
mov edi, [esi+block_prev]
|
|
|
|
test edi, edi
|
|
|
|
jz .insert
|
|
|
|
|
|
|
|
cmp [edi+block_flags], FREE_BLOCK
|
|
|
|
jne .insert
|
|
|
|
|
|
|
|
remove_from_used esi
|
|
|
|
|
|
|
|
mov edx, [esi+block_next]
|
|
|
|
mov [edi+block_next], edx
|
|
|
|
test edx, edx
|
|
|
|
jz @f
|
|
|
|
mov [edx+block_prev], edi
|
|
|
|
@@:
|
|
|
|
mov eax, esi
|
|
|
|
call free_mem_block
|
|
|
|
|
|
|
|
mov ecx, [edi+block_size]
|
|
|
|
mov eax, [esi+block_size]
|
|
|
|
add eax, ecx
|
|
|
|
mov [edi+block_size], eax
|
|
|
|
|
|
|
|
calc_index eax
|
|
|
|
calc_index ecx
|
|
|
|
cmp eax, ecx
|
|
|
|
je .m_eq
|
|
|
|
|
|
|
|
push ecx
|
|
|
|
remove_from_list edi
|
|
|
|
pop ecx
|
|
|
|
|
|
|
|
cmp [mem_block_list+ecx*4], edi
|
|
|
|
jne @f
|
|
|
|
mov [mem_block_list+ecx*4], edx
|
|
|
|
@@:
|
|
|
|
cmp [mem_block_list+ecx*4], 0
|
|
|
|
jne @f
|
|
|
|
btr [mem_block_mask], ecx
|
|
|
|
@@:
|
|
|
|
mov esi, [mem_block_list+eax*4]
|
|
|
|
mov [mem_block_list+eax*4], edi
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edi+list_fd], esi
|
2006-10-06 08:09:41 +02:00
|
|
|
test esi, esi
|
|
|
|
jz @f
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [esi+list_bk], edi
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
|
|
|
bts [mem_block_mask], eax
|
|
|
|
.m_eq:
|
|
|
|
xor eax, eax
|
2007-01-18 08:31:56 +01:00
|
|
|
mov [heap_mutex], eax
|
2007-02-05 15:20:36 +01:00
|
|
|
dec eax
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
.insert:
|
|
|
|
remove_from_used esi
|
|
|
|
|
|
|
|
mov eax, [esi+block_size]
|
|
|
|
calc_index eax
|
|
|
|
|
|
|
|
mov edi, [mem_block_list+eax*4]
|
|
|
|
mov [mem_block_list+eax*4], esi
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [esi+list_fd], edi
|
2006-10-06 08:09:41 +02:00
|
|
|
test edi, edi
|
|
|
|
jz @f
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edi+list_bk], esi
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
|
|
|
bts [mem_block_mask], eax
|
|
|
|
mov [esi+block_flags],FREE_BLOCK
|
|
|
|
xor eax, eax
|
2007-01-18 08:31:56 +01:00
|
|
|
mov [heap_mutex], eax
|
2007-02-05 15:20:36 +01:00
|
|
|
dec eax
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
.fail:
|
|
|
|
xor eax, eax
|
2007-01-18 08:31:56 +01:00
|
|
|
mov [heap_mutex], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc kernel_alloc stdcall, size:dword
|
|
|
|
locals
|
|
|
|
lin_addr dd ?
|
|
|
|
pages_count dd ?
|
|
|
|
endl
|
|
|
|
|
|
|
|
mov eax, [size]
|
2006-11-03 16:19:15 +01:00
|
|
|
add eax, 4095
|
|
|
|
and eax, not 4095;
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [size], eax
|
|
|
|
and eax, eax
|
2007-02-17 11:09:23 +01:00
|
|
|
jz .err
|
2006-10-06 08:09:41 +02:00
|
|
|
mov ebx, eax
|
|
|
|
shr ebx, 12
|
|
|
|
mov [pages_count], ebx
|
|
|
|
|
|
|
|
stdcall alloc_kernel_space, eax
|
2007-02-17 11:09:23 +01:00
|
|
|
test eax, eax
|
|
|
|
jz .err
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [lin_addr], eax
|
|
|
|
|
|
|
|
mov ecx, [pages_count]
|
|
|
|
mov edx, eax
|
|
|
|
mov ebx, ecx
|
|
|
|
|
|
|
|
shr ecx, 3
|
|
|
|
jz .next
|
|
|
|
|
|
|
|
and ebx, not 7
|
|
|
|
push ebx
|
|
|
|
stdcall alloc_pages, ebx
|
|
|
|
pop ecx ; yes ecx!!!
|
|
|
|
and eax, eax
|
2007-02-17 11:09:23 +01:00
|
|
|
jz .err
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
mov edi, eax
|
|
|
|
mov edx, [lin_addr]
|
|
|
|
@@:
|
|
|
|
stdcall map_page,edx,edi,dword PG_SW
|
|
|
|
add edx, 0x1000
|
|
|
|
add edi, 0x1000
|
|
|
|
dec ecx
|
|
|
|
jnz @B
|
|
|
|
.next:
|
|
|
|
mov ecx, [pages_count]
|
|
|
|
and ecx, 7
|
|
|
|
jz .end
|
2007-02-17 11:09:23 +01:00
|
|
|
@@:
|
|
|
|
push ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
call alloc_page
|
|
|
|
pop ecx
|
|
|
|
test eax, eax
|
2007-02-17 11:09:23 +01:00
|
|
|
jz .err
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
stdcall map_page,edx,eax,dword PG_SW
|
|
|
|
add edx, 0x1000
|
|
|
|
dec ecx
|
|
|
|
jnz @B
|
|
|
|
.end:
|
|
|
|
mov eax, [lin_addr]
|
|
|
|
ret
|
2007-02-17 11:09:23 +01:00
|
|
|
.err:
|
2006-10-06 08:09:41 +02:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc kernel_free stdcall, base:dword
|
2007-02-05 15:20:36 +01:00
|
|
|
push ebx esi
|
2007-01-18 08:31:56 +01:00
|
|
|
|
|
|
|
mov ebx, heap_mutex
|
|
|
|
call wait_mutex ;ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
mov eax, [base]
|
2007-02-17 11:09:23 +01:00
|
|
|
mov esi, [mem_used.fd]
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
2007-02-17 11:09:23 +01:00
|
|
|
cmp esi, mem_used.fd-MEM_LIST_OFFSET
|
|
|
|
je .fail
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
cmp [esi+block_base], eax
|
|
|
|
je .found
|
2007-02-17 11:09:23 +01:00
|
|
|
mov esi, [esi+list_fd]
|
2006-10-06 08:09:41 +02:00
|
|
|
jmp @b
|
|
|
|
.found:
|
|
|
|
cmp [esi+block_flags], USED_BLOCK
|
|
|
|
jne .fail
|
|
|
|
|
2007-01-18 08:31:56 +01:00
|
|
|
and [heap_mutex], 0
|
|
|
|
|
2007-02-05 15:20:36 +01:00
|
|
|
push ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
mov ecx, [esi+block_size];
|
2007-01-19 04:58:46 +01:00
|
|
|
shr ecx, 12
|
2007-01-18 08:31:56 +01:00
|
|
|
call release_pages ;eax, ecx
|
2007-02-05 15:20:36 +01:00
|
|
|
pop ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
stdcall free_kernel_space, [base]
|
2007-02-05 15:20:36 +01:00
|
|
|
pop esi ebx
|
2007-01-18 08:31:56 +01:00
|
|
|
ret
|
2006-10-06 08:09:41 +02:00
|
|
|
.fail:
|
2007-01-18 08:31:56 +01:00
|
|
|
and [heap_mutex], 0
|
2007-02-05 15:20:36 +01:00
|
|
|
pop esi ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
restore block_next
|
|
|
|
restore block_prev
|
|
|
|
restore block_list
|
|
|
|
restore block_base
|
|
|
|
restore block_size
|
|
|
|
restore block_flags
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;; USER ;;;;;;;;;;;;;;;;;
|
|
|
|
|
2006-10-20 16:02:26 +02:00
|
|
|
HEAP_TOP equ 0x5FC00000
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
align 4
|
2006-10-20 16:02:26 +02:00
|
|
|
proc init_heap
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2006-10-10 07:11:30 +02:00
|
|
|
mov ebx,[CURRENT_TASK]
|
|
|
|
shl ebx,8
|
2007-02-28 11:51:05 +01:00
|
|
|
mov eax, [SLOT_BASE+APPDATA.heap_top+ebx]
|
2006-10-10 07:11:30 +02:00
|
|
|
test eax, eax
|
|
|
|
jz @F
|
2007-02-28 11:51:05 +01:00
|
|
|
sub eax,[SLOT_BASE+APPDATA.heap_base+ebx]
|
2006-10-10 07:11:30 +02:00
|
|
|
sub eax, 4096
|
|
|
|
ret
|
|
|
|
@@:
|
2007-02-28 11:51:05 +01:00
|
|
|
mov esi, [SLOT_BASE+APPDATA.mem_size+ebx]
|
2006-10-20 16:02:26 +02:00
|
|
|
add esi, 4095
|
|
|
|
and esi, not 4095
|
2007-02-28 11:51:05 +01:00
|
|
|
mov [SLOT_BASE+APPDATA.mem_size+ebx], esi
|
2006-10-20 16:02:26 +02:00
|
|
|
mov eax, HEAP_TOP
|
2007-02-28 11:51:05 +01:00
|
|
|
mov [SLOT_BASE+APPDATA.heap_base+ebx], esi
|
|
|
|
mov [SLOT_BASE+APPDATA.heap_top+ebx], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2006-10-20 16:02:26 +02:00
|
|
|
sub eax, esi
|
|
|
|
add esi, new_app_base
|
|
|
|
shr esi, 10
|
|
|
|
mov ecx, eax
|
2006-10-06 08:09:41 +02:00
|
|
|
sub eax, 4096
|
2006-10-20 16:02:26 +02:00
|
|
|
or ecx, FREE_BLOCK
|
2007-02-20 13:05:24 +01:00
|
|
|
mov [page_tabs+esi], ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
.exit:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc user_alloc stdcall, alloc_size:dword
|
|
|
|
|
|
|
|
mov ecx, [alloc_size]
|
|
|
|
add ecx, (4095+4096)
|
|
|
|
and ecx, not 4095
|
|
|
|
|
|
|
|
mov ebx, [CURRENT_TASK]
|
|
|
|
shl ebx, 8
|
2007-02-28 11:51:05 +01:00
|
|
|
mov esi, dword [ebx+SLOT_BASE+APPDATA.heap_base]; heap_base
|
|
|
|
mov edi, dword [ebx+SLOT_BASE+APPDATA.heap_top]; heap_top
|
2006-10-06 08:09:41 +02:00
|
|
|
add esi, new_app_base
|
|
|
|
add edi, new_app_base
|
|
|
|
l_0:
|
|
|
|
cmp esi, edi
|
|
|
|
jae m_exit
|
|
|
|
|
|
|
|
mov ebx, esi
|
|
|
|
shr ebx, 12
|
2007-02-20 13:05:24 +01:00
|
|
|
mov eax, [page_tabs+ebx*4]
|
2006-10-06 08:09:41 +02:00
|
|
|
test eax, FREE_BLOCK
|
|
|
|
jz test_used
|
|
|
|
and eax, 0xFFFFF000
|
|
|
|
cmp eax, ecx ;alloc_size
|
|
|
|
jb m_next
|
2007-01-11 15:43:51 +01:00
|
|
|
jz @f
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
mov edx, esi
|
|
|
|
add edx, ecx
|
|
|
|
sub eax, ecx;
|
|
|
|
or eax, FREE_BLOCK
|
|
|
|
shr edx, 12
|
2007-02-20 13:05:24 +01:00
|
|
|
mov [page_tabs+edx*4], eax
|
2006-11-20 08:06:00 +01:00
|
|
|
|
2007-01-25 14:42:23 +01:00
|
|
|
@@:
|
2006-10-06 08:09:41 +02:00
|
|
|
or ecx, USED_BLOCK
|
2007-02-20 13:05:24 +01:00
|
|
|
mov [page_tabs+ebx*4], ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
shr ecx, 12
|
|
|
|
dec ecx
|
|
|
|
inc ebx
|
|
|
|
@@:
|
2007-02-20 13:05:24 +01:00
|
|
|
mov dword [page_tabs+ebx*4], 2
|
2006-10-06 08:09:41 +02:00
|
|
|
inc ebx
|
|
|
|
dec ecx
|
|
|
|
jnz @B
|
|
|
|
|
2007-01-25 14:42:23 +01:00
|
|
|
mov edx, [CURRENT_TASK]
|
|
|
|
shl edx, 8
|
|
|
|
mov ebx, [alloc_size]
|
|
|
|
add ebx, 0xFFF
|
|
|
|
and ebx, not 0xFFF
|
2007-02-28 11:51:05 +01:00
|
|
|
add ebx, [SLOT_BASE+APPDATA.mem_size+edx]
|
2007-01-25 14:42:23 +01:00
|
|
|
call update_mem_size
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov eax, esi
|
|
|
|
add eax, 4096
|
|
|
|
sub eax, new_app_base
|
|
|
|
ret
|
|
|
|
m_next:
|
|
|
|
add esi, eax
|
|
|
|
jmp l_0
|
|
|
|
test_used:
|
|
|
|
test eax, USED_BLOCK
|
|
|
|
jz m_exit
|
|
|
|
|
|
|
|
and eax, 0xFFFFF000
|
|
|
|
add esi, eax
|
|
|
|
jmp l_0
|
|
|
|
m_exit:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc user_free stdcall, base:dword
|
|
|
|
|
|
|
|
mov esi, [base]
|
|
|
|
test esi, esi
|
|
|
|
jz .exit
|
|
|
|
|
2007-01-25 14:42:23 +01:00
|
|
|
xor ebx, ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
sub esi, 4096
|
|
|
|
shr esi, 12
|
2007-02-20 13:05:24 +01:00
|
|
|
mov eax, [page_tabs+esi*4]
|
2006-10-06 08:09:41 +02:00
|
|
|
test eax, USED_BLOCK
|
2006-10-20 16:02:26 +02:00
|
|
|
jz .not_used
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
and eax, not 4095
|
|
|
|
mov ecx, eax
|
|
|
|
or eax, FREE_BLOCK
|
2007-02-20 13:05:24 +01:00
|
|
|
mov [page_tabs+esi*4], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
inc esi
|
|
|
|
sub ecx, 4096
|
|
|
|
shr ecx, 12
|
2007-01-25 14:42:23 +01:00
|
|
|
mov ebx, ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
.release:
|
2006-10-20 16:02:26 +02:00
|
|
|
xor eax, eax
|
2007-02-20 13:05:24 +01:00
|
|
|
xchg eax, [page_tabs+esi*4]
|
2006-10-20 16:02:26 +02:00
|
|
|
test eax, 1
|
|
|
|
jz @F
|
2006-10-06 08:09:41 +02:00
|
|
|
call free_page
|
2006-10-20 16:02:26 +02:00
|
|
|
@@:
|
2006-10-06 08:09:41 +02:00
|
|
|
inc esi
|
|
|
|
dec ecx
|
|
|
|
jnz .release
|
2006-10-20 16:02:26 +02:00
|
|
|
.not_used:
|
2007-01-25 14:42:23 +01:00
|
|
|
mov edx, [CURRENT_TASK]
|
|
|
|
shl edx, 8
|
2007-02-28 11:51:05 +01:00
|
|
|
mov esi, dword [edx+SLOT_BASE+APPDATA.heap_base]; heap_base
|
|
|
|
mov edi, dword [edx+SLOT_BASE+APPDATA.heap_top]; heap_top
|
|
|
|
sub ebx, [edx+SLOT_BASE+APPDATA.mem_size]
|
2007-01-25 14:42:23 +01:00
|
|
|
neg ebx
|
|
|
|
call update_mem_size
|
2007-01-09 19:30:35 +01:00
|
|
|
add esi, new_app_base
|
|
|
|
add edi, new_app_base
|
2006-10-06 08:09:41 +02:00
|
|
|
shr esi, 12
|
|
|
|
shr edi, 12
|
|
|
|
@@:
|
2007-02-20 13:05:24 +01:00
|
|
|
mov eax, [page_tabs+esi*4]
|
2006-10-06 08:09:41 +02:00
|
|
|
test eax, USED_BLOCK
|
|
|
|
jz .test_free
|
|
|
|
shr eax, 12
|
|
|
|
add esi, eax
|
|
|
|
jmp @B
|
|
|
|
.test_free:
|
|
|
|
test eax, FREE_BLOCK
|
|
|
|
jz .err
|
|
|
|
mov edx, eax
|
|
|
|
shr edx, 12
|
|
|
|
add edx, esi
|
|
|
|
cmp edx, edi
|
|
|
|
jae .exit
|
|
|
|
|
2007-02-20 13:05:24 +01:00
|
|
|
mov ebx, [page_tabs+edx*4]
|
2006-10-06 08:09:41 +02:00
|
|
|
test ebx, USED_BLOCK
|
|
|
|
jz .next_free
|
|
|
|
|
|
|
|
shr ebx, 12
|
|
|
|
add edx, ebx
|
|
|
|
mov esi, edx
|
|
|
|
jmp @B
|
|
|
|
.next_free:
|
|
|
|
test ebx, FREE_BLOCK
|
|
|
|
jz .err
|
2007-02-20 13:05:24 +01:00
|
|
|
and dword [page_tabs+edx*4], 0
|
2006-10-06 08:09:41 +02:00
|
|
|
add eax, ebx
|
|
|
|
and eax, not 4095
|
|
|
|
or eax, FREE_BLOCK
|
2007-02-20 13:05:24 +01:00
|
|
|
mov [page_tabs+esi*4], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
jmp @B
|
|
|
|
.exit:
|
|
|
|
xor eax, eax
|
|
|
|
inc eax
|
|
|
|
ret
|
|
|
|
.err:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
2007-01-18 03:06:00 +01:00
|
|
|
if 0
|
2006-10-06 08:09:41 +02:00
|
|
|
align 4
|
|
|
|
proc alloc_dll
|
|
|
|
pushf
|
|
|
|
cli
|
|
|
|
bsf eax, [dll_map]
|
|
|
|
jnz .find
|
|
|
|
popf
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
.find:
|
|
|
|
btr [dll_map], eax
|
|
|
|
popf
|
|
|
|
shl eax, 5
|
|
|
|
add eax, dll_tab
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc alloc_service
|
|
|
|
pushf
|
|
|
|
cli
|
|
|
|
bsf eax, [srv_map]
|
|
|
|
jnz .find
|
|
|
|
popf
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
2006-11-20 08:06:00 +01:00
|
|
|
.find:
|
|
|
|
btr [srv_map], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
popf
|
2006-11-20 08:06:00 +01:00
|
|
|
shl eax,0x02
|
|
|
|
lea eax,[srv_tab+eax+eax*8] ;srv_tab+eax*36
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
2007-01-18 03:06:00 +01:00
|
|
|
|
|
|
|
end if
|