kolibri-process:user heap

git-svn-id: svn://kolibrios.org@4430 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Sergey Semyonov (Serge) 2014-01-08 07:56:21 +00:00
parent ede303b245
commit fad1af692b
7 changed files with 106 additions and 185 deletions

View File

@ -273,6 +273,8 @@ REG_EDI equ (RING0_STACK_SIZE-52)
REG_RET equ (RING0_STACK_SIZE-56) ;irq0.return REG_RET equ (RING0_STACK_SIZE-56) ;irq0.return
PAGE_SIZE equ 4096
PG_UNMAP equ 0x000 PG_UNMAP equ 0x000
PG_MAP equ 0x001 PG_MAP equ 0x001
PG_WRITE equ 0x002 PG_WRITE equ 0x002

View File

@ -566,25 +566,26 @@ align 4
proc init_heap proc init_heap
mov ebx, [current_slot] mov ebx, [current_slot]
mov eax, [ebx+APPDATA.heap_top] mov ebx, [ebx+APPDATA.process]
mov eax, [ebx+PROC.heap_top]
test eax, eax test eax, eax
jz @F jz @F
sub eax, [ebx+APPDATA.heap_base] sub eax, [ebx+PROC.heap_base]
sub eax, 4096 sub eax, PAGE_SIZE
ret ret
@@: @@:
mov esi, [ebx+APPDATA.mem_size] mov esi, [ebx+PROC.mem_used]
add esi, 4095 add esi, 4095
and esi, not 4095 and esi, not 4095
mov [ebx+APPDATA.mem_size], esi mov [ebx+PROC.mem_used], esi
mov eax, HEAP_TOP mov eax, HEAP_TOP
mov [ebx+APPDATA.heap_base], esi mov [ebx+PROC.heap_base], esi
mov [ebx+APPDATA.heap_top], eax mov [ebx+PROC.heap_top], eax
sub eax, esi sub eax, esi
shr esi, 10 shr esi, 10
mov ecx, eax mov ecx, eax
sub eax, 4096 sub eax, PAGE_SIZE
or ecx, FREE_BLOCK or ecx, FREE_BLOCK
mov [page_tabs+esi], ecx mov [page_tabs+esi], ecx
ret ret
@ -598,24 +599,25 @@ proc user_alloc stdcall, alloc_size:dword
push edi push edi
mov ecx, [alloc_size] mov ecx, [alloc_size]
add ecx, (4095+4096) add ecx, (4095+PAGE_SIZE)
and ecx, not 4095 and ecx, not 4095
mov ebx, [current_slot] mov ebx, [current_slot]
mov esi, dword [ebx+APPDATA.heap_base] ; heap_base mov ebx, [ebx+APPDATA.process]
mov edi, dword [ebx+APPDATA.heap_top] ; heap_top mov esi, dword [ebx+PROC.heap_base] ; heap_base
l_0: mov edi, dword [ebx+PROC.heap_top] ; heap_top
.scan:
cmp esi, edi cmp esi, edi
jae m_exit jae .m_exit
mov ebx, esi mov ebx, esi
shr ebx, 12 shr ebx, 12
mov eax, [page_tabs+ebx*4] mov eax, [page_tabs+ebx*4]
test al, FREE_BLOCK test al, FREE_BLOCK
jz test_used jz .test_used
and eax, 0xFFFFF000 and eax, 0xFFFFF000
cmp eax, ecx ;alloc_size cmp eax, ecx ;alloc_size
jb m_next jb .m_next
jz @f jz @f
lea edx, [esi+ecx] lea edx, [esi+ecx]
@ -638,11 +640,12 @@ l_0:
.no: .no:
mov edx, [current_slot] mov edx, [current_slot]
mov edx, [edx+APPDATA.process]
mov ebx, [alloc_size] mov ebx, [alloc_size]
add ebx, 0xFFF add ebx, 0xFFF
and ebx, not 0xFFF and ebx, not 0xFFF
add ebx, [edx+APPDATA.mem_size] add [edx+PROC.mem_used], ebx
call update_mem_size
lea eax, [esi+4096] lea eax, [esi+4096]
@ -650,15 +653,15 @@ l_0:
pop esi pop esi
pop ebx pop ebx
ret ret
test_used: .test_used:
test al, USED_BLOCK test al, USED_BLOCK
jz m_exit jz .m_exit
and eax, 0xFFFFF000 and eax, 0xFFFFF000
m_next: .m_next:
add esi, eax add esi, eax
jmp l_0 jmp .scan
m_exit: .m_exit:
xor eax, eax xor eax, eax
pop edi pop edi
pop esi pop esi
@ -674,13 +677,15 @@ proc user_alloc_at stdcall, address:dword, alloc_size:dword
push edi push edi
mov ebx, [current_slot] mov ebx, [current_slot]
mov ebx, [ebx+APPDATA.process]
mov edx, [address] mov edx, [address]
and edx, not 0xFFF and edx, not 0xFFF
mov [address], edx mov [address], edx
sub edx, 0x1000 sub edx, 0x1000
jb .error jb .error
mov esi, [ebx+APPDATA.heap_base] mov esi, [ebx+PROC.heap_base]
mov edi, [ebx+APPDATA.heap_top] mov edi, [ebx+PROC.heap_top]
cmp edx, esi cmp edx, esi
jb .error jb .error
.scan: .scan:
@ -748,13 +753,13 @@ proc user_alloc_at stdcall, address:dword, alloc_size:dword
mov [page_tabs+ebx*4], ecx mov [page_tabs+ebx*4], ecx
.nothird: .nothird:
mov edx, [current_slot] mov edx, [current_slot]
mov edx, [edx+APPDATA.process]
mov ebx, [alloc_size] mov ebx, [alloc_size]
add ebx, 0xFFF add ebx, 0xFFF
and ebx, not 0xFFF and ebx, not 0xFFF
add ebx, [edx+APPDATA.mem_size] add [edx+PROC.mem_used], ebx
call update_mem_size
mov eax, [address] mov eax, [address]
@ -811,11 +816,12 @@ proc user_free stdcall, base:dword
push edi push edi
mov edx, [current_slot] mov edx, [current_slot]
mov esi, dword [edx+APPDATA.heap_base] mov edx, [edx+APPDATA.process]
mov edi, dword [edx+APPDATA.heap_top] mov esi, dword [edx+PROC.heap_base]
sub ebx, [edx+APPDATA.mem_size] mov edi, dword [edx+PROC.heap_top]
sub ebx, [edx+PROC.mem_used]
neg ebx neg ebx
call update_mem_size mov [edx+PROC.mem_used], ebx
call user_normalize call user_normalize
pop edi pop edi
pop ebx pop ebx
@ -999,15 +1005,16 @@ user_realloc:
mov eax, [page_tabs+ecx*4] mov eax, [page_tabs+ecx*4]
and eax, not 0xFFF and eax, not 0xFFF
mov edx, [current_slot] mov edx, [current_slot]
mov ebx, [APPDATA.mem_size+edx] mov edx, [edx+APPDATA.process]
mov ebx, [edx+PROC.mem_used]
sub ebx, eax sub ebx, eax
add ebx, 0x1000 add ebx, 0x1000
or al, FREE_BLOCK or al, FREE_BLOCK
mov [page_tabs+ecx*4], eax mov [page_tabs+ecx*4], eax
push esi edi push esi edi
mov esi, [APPDATA.heap_base+edx] mov esi, [edx+PROC.heap_base]
mov edi, [APPDATA.heap_top+edx] mov edi, [edx+PROC.heap_top]
call update_mem_size mov [edx+PROC.mem_used], ebx
call user_normalize call user_normalize
pop edi esi pop edi esi
jmp .ret0 ; all freed jmp .ret0 ; all freed
@ -1020,10 +1027,11 @@ user_realloc:
sub ebx, edx sub ebx, edx
push ebx ecx edx push ebx ecx edx
mov edx, [current_slot] mov edx, [current_slot]
mov edx, [edx+APPDATA.process]
shl ebx, 12 shl ebx, 12
sub ebx, [APPDATA.mem_size+edx] sub ebx, [edx+PROC.mem_used]
neg ebx neg ebx
call update_mem_size mov [edx+PROC.mem_used], ebx
pop edx ecx ebx pop edx ecx ebx
lea eax, [ecx+1] lea eax, [ecx+1]
shl eax, 12 shl eax, 12
@ -1034,7 +1042,8 @@ user_realloc:
jz .ret jz .ret
push esi push esi
mov esi, [current_slot] mov esi, [current_slot]
mov esi, [APPDATA.heap_top+esi] mov esi, [esi+APPDATA.process]
mov esi, [esi+PROC.heap_top]
shr esi, 12 shr esi, 12
@@: @@:
cmp edx, esi cmp edx, esi
@ -1058,7 +1067,8 @@ user_realloc:
.realloc_add: .realloc_add:
; get some additional memory ; get some additional memory
mov eax, [current_slot] mov eax, [current_slot]
mov eax, [APPDATA.heap_top+eax] mov eax, [eax+APPDATA.process]
mov eax, [eax+PROC.heap_top]
shr eax, 12 shr eax, 12
cmp edx, eax cmp edx, eax
jae .cant_inplace jae .cant_inplace
@ -1091,16 +1101,17 @@ user_realloc:
rep stosd rep stosd
pop edi pop edi
mov edx, [current_slot] mov edx, [current_slot]
mov edx, [edx+APPDATA.process]
shl ebx, 12 shl ebx, 12
add ebx, [APPDATA.mem_size+edx] add [edx+PROC.mem_used], ebx
call update_mem_size
pop eax edx ecx pop eax edx ecx
ret ret
.cant_inplace: .cant_inplace:
push esi edi push esi edi
mov eax, [current_slot] mov eax, [current_slot]
mov esi, [APPDATA.heap_base+eax] mov eax, [eax+APPDATA.process]
mov edi, [APPDATA.heap_top+eax] mov esi, [eax+PROC.heap_base]
mov edi, [eax+PROC.heap_top]
shr esi, 12 shr esi, 12
shr edi, 12 shr edi, 12
sub ebx, ecx sub ebx, ecx
@ -1164,9 +1175,9 @@ user_realloc:
.no: .no:
push ebx push ebx
mov edx, [current_slot] mov edx, [current_slot]
mov edx, [eax+APPDATA.process]
shl ebx, 12 shl ebx, 12
add ebx, [APPDATA.mem_size+edx] add [edx+PROC.mem_used], ebx
call update_mem_size
pop ebx pop ebx
@@: @@:
mov dword [page_tabs+esi*4], 2 mov dword [page_tabs+esi*4], 2
@ -1176,45 +1187,9 @@ user_realloc:
pop eax edi esi edx ecx pop eax edi esi edx ecx
ret ret
if 0
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
.find:
btr [srv_map], eax
popf
shl eax, 0x02
lea eax, [srv_tab+eax+eax*8] ;srv_tab+eax*36
ret
endp
end if
;;;;;;;;;;;;;; SHARED ;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;; SHARED MEMORY ;;;;;;;;;;;;;;;;;
; param ; param

View File

@ -447,7 +447,9 @@ proc new_mem_resize stdcall, new_size:dword
push edi push edi
mov edx, [current_slot] mov edx, [current_slot]
cmp [edx+APPDATA.heap_base], 0 mov ebx, [edx+APPDATA.process]
cmp [ebx+PROC.heap_base], 0
jne .exit jne .exit
mov edi, [new_size] mov edi, [new_size]
@ -455,7 +457,7 @@ proc new_mem_resize stdcall, new_size:dword
and edi, not 4095 and edi, not 4095
mov [new_size], edi mov [new_size], edi
mov esi, [edx+APPDATA.mem_size] mov esi, [ebx+PROC.mem_used]
add esi, 4095 add esi, 4095
and esi, not 4095 and esi, not 4095
@ -490,7 +492,8 @@ proc new_mem_resize stdcall, new_size:dword
.update_size: .update_size:
mov edx, [current_slot] mov edx, [current_slot]
mov ebx, [new_size] mov ebx, [new_size]
call update_mem_size mov edx, [edx+APPDATA.process]
mov [edx+PROC.mem_used], ebx
.exit: .exit:
pop edi pop edi
pop esi pop esi
@ -566,38 +569,6 @@ proc new_mem_resize stdcall, new_size:dword
endp endp
align 4
update_mem_size:
; in: edx = slot base
; ebx = new memory size
; destroys eax,ecx,edx
mov [APPDATA.mem_size+edx], ebx
;search threads and update
;application memory size infomation
mov ecx, [APPDATA.process+edx]
mov eax, 2
.search_threads:
;eax = current slot
;ebx = new memory size
;ecx = page directory
cmp eax, [TASK_COUNT]
jg .search_threads_end
mov edx, eax
shl edx, 5
cmp word [CURRENT_TASK+edx+TASKDATA.state], 9 ;if slot empty?
jz .search_threads_next
shl edx, 3
cmp [SLOT_BASE+edx+APPDATA.process], ecx ;if it is our thread?
jnz .search_threads_next
mov [SLOT_BASE+edx+APPDATA.mem_size], ebx ;update memory size
.search_threads_next:
inc eax
jmp .search_threads
.search_threads_end:
ret
; param ; param
; eax= linear address ; eax= linear address
; ;
@ -776,59 +747,62 @@ end if
endp endp
; returns number of mapped bytes ; returns number of mapped bytes
proc map_mem stdcall, lin_addr:dword,slot:dword,\ proc map_mem_ipc stdcall, lin_addr:dword,slot:dword,\
ofs:dword,buf_size:dword,req_access:dword ofs:dword,buf_size:dword,req_access:dword
push 0 ; initialize number of mapped bytes locals
count dd ?
process dd ?
endl
mov [count], 0
cmp [buf_size], 0 cmp [buf_size], 0
jz .exit jz .exit
mov eax, [slot] mov eax, [slot]
shl eax, 8 shl eax, 8
mov eax, [SLOT_BASE+eax+APPDATA.process] mov eax, [SLOT_BASE+eax+APPDATA.process]
mov eax, [eax+PROC.pdt_0_phys] test eax, eax
and eax, 0xFFFFF000 jz .exit
stdcall map_page, [ipc_pdir], eax, PG_UW mov [process], eax
mov ebx, [ofs] mov ebx, [ofs]
shr ebx, 22 shr ebx, 22
mov esi, [ipc_pdir] mov eax, [eax+PROC.pdt_0+ebx*4] ;get page table
mov edi, [ipc_ptab] mov esi, [ipc_ptab]
mov eax, [esi+ebx*4]
and eax, 0xFFFFF000 and eax, 0xFFFFF000
jz .exit jz .exit
stdcall map_page, edi, eax, PG_UW stdcall map_page, esi, eax, PG_SW
@@: @@:
mov edi, [lin_addr] mov edi, [lin_addr]
and edi, 0xFFFFF000 and edi, 0xFFFFF000
mov ecx, [buf_size] mov ecx, [buf_size]
add ecx, 4095 add ecx, 4095
shr ecx, 12 shr ecx, 12
inc ecx inc ecx ; ???????????
mov edx, [ofs] mov edx, [ofs]
shr edx, 12 shr edx, 12
and edx, 0x3FF and edx, 0x3FF
mov esi, [ipc_ptab]
.map: .map:
stdcall safe_map_page, [slot], [req_access], [ofs] stdcall safe_map_page, [slot], [req_access], [ofs]
jnc .exit jnc .exit
add dword [ebp-4], 4096 add dword [ebp-4], PAGE_SIZE
add [ofs], 4096 add [ofs], PAGE_SIZE
dec ecx dec ecx
jz .exit jz .exit
add edi, 0x1000
add edi, PAGE_SIZE
inc edx inc edx
cmp edx, 0x400 cmp edx, 1024
jnz .map jnz .map
inc ebx inc ebx
mov eax, [ipc_pdir] mov eax, [process]
mov eax, [eax+ebx*4] mov eax, [eax+PROC.pdt_0+ebx*4]
and eax, 0xFFFFF000 and eax, 0xFFFFF000
jz .exit jz .exit
stdcall map_page, esi, eax, PG_UW
stdcall map_page, esi, eax, PG_SW
xor edx, edx xor edx, edx
jmp .map jmp .map
@ -1017,29 +991,6 @@ sys_IPC:
mov [esp+32], eax mov [esp+32], eax
ret ret
;align 4
;proc set_ipc_buff
; mov eax,[current_slot]
; pushf
; cli
; mov [eax+APPDATA.ipc_start],ebx ;set fields in extended information area
; mov [eax+APPDATA.ipc_size],ecx
;
; add ecx, ebx
; add ecx, 4095
; and ecx, not 4095
;
;.touch: mov eax, [ebx]
; add ebx, 0x1000
; cmp ebx, ecx
; jb .touch
;
; popf
; xor eax, eax
; ret
;endp
proc sys_ipc_send stdcall, PID:dword, msg_addr:dword, msg_size:dword proc sys_ipc_send stdcall, PID:dword, msg_addr:dword, msg_size:dword
locals locals
dst_slot dd ? dst_slot dd ?
@ -1058,7 +1009,7 @@ proc sys_ipc_send stdcall, PID:dword, msg_addr:dword, msg_size:dword
mov [dst_slot], eax mov [dst_slot], eax
shl eax, 8 shl eax, 8
mov edi, [eax+SLOT_BASE+0xa0] ;is ipc area defined? mov edi, [eax+SLOT_BASE+APPDATA.ipc_start] ;is ipc area defined?
test edi, edi test edi, edi
jz .no_ipc_area jz .no_ipc_area
@ -1066,7 +1017,7 @@ proc sys_ipc_send stdcall, PID:dword, msg_addr:dword, msg_size:dword
and ebx, 0xFFF and ebx, 0xFFF
mov [dst_offset], ebx mov [dst_offset], ebx
mov esi, [eax+SLOT_BASE+0xa4] mov esi, [eax+SLOT_BASE+APPDATA.ipc_size]
mov [buf_size], esi mov [buf_size], esi
mov ecx, [ipc_tmp] mov ecx, [ipc_tmp]
@ -1079,7 +1030,7 @@ proc sys_ipc_send stdcall, PID:dword, msg_addr:dword, msg_size:dword
pop edi esi pop edi esi
@@: @@:
mov [used_buf], ecx mov [used_buf], ecx
stdcall map_mem, ecx, [dst_slot], \ stdcall map_mem_ipc, ecx, [dst_slot], \
edi, esi, PG_SW edi, esi, PG_SW
mov edi, [dst_offset] mov edi, [dst_offset]
@ -1150,7 +1101,7 @@ proc sys_ipc_send stdcall, PID:dword, msg_addr:dword, msg_size:dword
.ret: .ret:
mov eax, [used_buf] mov eax, [used_buf]
cmp eax, [ipc_tmp] cmp eax, [ipc_tmp]
jz @f je @f
stdcall free_kernel_space, eax stdcall free_kernel_space, eax
@@: @@:
pop eax pop eax

View File

@ -257,10 +257,11 @@ proc fs_execute
test eax, eax test eax, eax
jz .failed jz .failed
mov ebx, [hdr_mem]
mov [eax+PROC.mem_used], ebx
mov ebx, [slot_base] mov ebx, [slot_base]
mov [ebx+APPDATA.process], eax mov [ebx+APPDATA.process], eax
mov eax, [hdr_mem]
mov [ebx+APPDATA.mem_size], eax
xor edx, edx xor edx, edx
cmp word [6], '02' cmp word [6], '02'
@ -976,17 +977,8 @@ proc new_sys_threads
mov ecx, 11 mov ecx, 11
rep movsb ;copy process name rep movsb ;copy process name
mov eax, [ebx+APPDATA.heap_base] mov eax, [ebx+APPDATA.process]
mov [edx+APPDATA.heap_base], eax mov [edx+APPDATA.process], eax
mov ecx, [ebx+APPDATA.heap_top]
mov [edx+APPDATA.heap_top], ecx
mov eax, [ebx+APPDATA.mem_size]
mov [edx+APPDATA.mem_size], eax
mov ecx, [ebx+APPDATA.process]
mov [edx+APPDATA.process], ecx;copy page directory
mov eax, [ebx+APPDATA.dlls_list_ptr] mov eax, [ebx+APPDATA.dlls_list_ptr]
mov [edx+APPDATA.dlls_list_ptr], eax mov [edx+APPDATA.dlls_list_ptr], eax
@ -1118,8 +1110,8 @@ proc set_app_params stdcall,slot:dword, params:dword,\
add eax, 256 add eax, 256
jc @f jc @f
cmp eax, [SLOT_BASE+APPDATA.mem_size+ebx*8] ; cmp eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
ja @f ; ja @f
mov eax, [cmd_line] mov eax, [cmd_line]
@ -1158,8 +1150,8 @@ proc set_app_params stdcall,slot:dword, params:dword,\
mov eax, edx mov eax, edx
add eax, 1024 add eax, 1024
jc @f jc @f
cmp eax, [SLOT_BASE+APPDATA.mem_size+ebx*8] ; cmp eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
ja @f ; ja @f
stdcall strncpy, edx, [app_path], 1024 stdcall strncpy, edx, [app_path], 1024
@@: @@:
mov ebx, [slot] mov ebx, [slot]

View File

@ -3264,7 +3264,8 @@ sys_cpuusage:
mov edx, 0x100000*16 mov edx, 0x100000*16
cmp ecx, 1 shl 5 cmp ecx, 1 shl 5
je .os_mem je .os_mem
mov edx, [SLOT_BASE+ecx*8+APPDATA.mem_size] mov edx, [SLOT_BASE+ecx*8+APPDATA.process]
mov edx, [edx+PROC.mem_used]
mov eax, std_application_base_address mov eax, std_application_base_address
.os_mem: .os_mem:
stosd stosd

View File

@ -123,8 +123,8 @@ struct APPDATA
exc_handler dd ? ;+24 exc_handler dd ? ;+24
except_mask dd ? ;+28 except_mask dd ? ;+28
pl0_stack dd ? ;+32 pl0_stack dd ? ;+32
heap_base dd ? ;+36 dd ? ;+36
heap_top dd ? ;+40 dd ? ;+40
cursor dd ? ;+44 cursor dd ? ;+44
fd_ev dd ? ;+48 fd_ev dd ? ;+48
bk_ev dd ? ;+52 bk_ev dd ? ;+52
@ -149,7 +149,7 @@ struct APPDATA
wnd_shape dd ? ;+128 wnd_shape dd ? ;+128
wnd_shape_scale dd ? ;+132 wnd_shape_scale dd ? ;+132
dd ? ;+136 dd ? ;+136
mem_size dd ? ;+140 dd ? ;+140
saved_box BOX ;+144 saved_box BOX ;+144
ipc_start dd ? ;+160 ipc_start dd ? ;+160
ipc_size dd ? ;+164 ipc_size dd ? ;+164