diff --git a/kernel/branches/kolibri_pe/core/dll.inc b/kernel/branches/kolibri_pe/core/dll.inc index 4af98a1f3d..ddd595c0ea 100644 --- a/kernel/branches/kolibri_pe/core/dll.inc +++ b/kernel/branches/kolibri_pe/core/dll.inc @@ -17,78 +17,77 @@ PID_KERNEL equ 1 ;os_idle thread align 4 proc attach_int_handler stdcall, irq:dword, handler:dword, access_rights:dword - mov ebx, [irq] ;irq num - test ebx, ebx - jz .err - cmp ebx, 15 ; hidnplayr says: we only have 16 IRQ's - ja .err - mov eax, [handler] - test eax, eax - jz .err - cmp [irq_owner + 4 * ebx], 0 - je @f + mov ebx, [irq] ;irq num + test ebx, ebx + jz .err + cmp ebx, 15 ; hidnplayr says: we only have 16 IRQ's + ja .err + mov eax, [handler] + test eax, eax + jz .err + cmp [irq_owner + 4 * ebx], 0 + je @f - mov ecx, [irq_rights + 4 * ebx] ; Rights : 0 - full access, 1 - read only, 2 - forbidden - test ecx, ecx - jnz .err + mov ecx, [irq_rights + 4 * ebx] ; Rights : 0 - full access, 1 - read only, 2 - forbidden + test ecx, ecx + jnz .err @@: - mov [irq_tab+ebx*4], eax + mov [irq_tab+ebx*4], eax - mov eax, [access_rights] - mov [irq_rights + 4 * ebx], eax + mov eax, [access_rights] + mov [irq_rights + 4 * ebx], eax - mov [irq_owner + 4 * ebx], PID_KERNEL ; all handlers belong to a kernel + mov [irq_owner + 4 * ebx], PID_KERNEL ; all handlers belong to a kernel - stdcall enable_irq, [irq] - mov eax, 1 - ret + stdcall enable_irq, [irq] + mov eax, 1 + ret .err: - xor eax, eax - ret + xor eax, eax + ret endp uglobal - irq_rights rd 16 +irq_rights rd 16 endg proc get_int_handler stdcall, irq:dword - mov eax, [irq] + mov eax, [irq] - cmp [irq_rights + 4 * eax], dword 1 - ja .err + cmp [irq_rights + 4 * eax], dword 1 + ja .err - mov eax, [irq_tab + 4 * eax] - ret - - .err: - xor eax, eax - ret + mov eax, [irq_tab + 4 * eax] + ret +.err: + xor eax, eax + ret endp align 4 proc detach_int_handler - ret + ret endp align 4 proc enable_irq stdcall, irq_line:dword - mov ebx, [irq_line] - mov edx, 0x21 - cmp ebx, 8 - jb @F - mov edx, 0xA1 - sub ebx,8 + mov ebx, [irq_line] + mov edx, 0x21 + cmp ebx, 8 + jb @F + mov edx, 0xA1 + sub ebx,8 @@: - in al,dx - btr eax, ebx - out dx, al - ret + in al,dx + btr eax, ebx + out dx, al + ret endp align 16 @@ -97,200 +96,202 @@ align 16 irq_serv: .irq_1: - push 1 - jmp .main + push 1 + jmp .main align 4 .irq_2: - push 2 - jmp .main + push 2 + jmp .main align 4 .irq_3: - push 3 - jmp .main + push 3 + jmp .main align 4 .irq_4: - push 4 - jmp .main + push 4 + jmp .main align 4 .irq_5: - push 5 - jmp .main + push 5 + jmp .main align 4 .irq_6: - push 6 - jmp .main + push 6 + jmp .main align 4 .irq_7: - push 7 - jmp .main + push 7 + jmp .main align 4 .irq_8: - push 8 - jmp .main + push 8 + jmp .main align 4 .irq_9: - push 9 - jmp .main + push 9 + jmp .main align 4 .irq_10: - push 10 - jmp .main + push 10 + jmp .main align 4 .irq_11: - push 11 - jmp .main + push 11 + jmp .main align 4 .irq_12: - push 12 - jmp .main + push 12 + jmp .main align 4 .irq_13: - push 13 - jmp .main + push 13 + jmp .main align 4 .irq_14: - push 14 - jmp .main + push 14 + jmp .main align 4 .irq_15: - push 15 - jmp .main + push 15 + jmp .main align 16 .main: - save_ring3_context - mov eax, [esp + 32] - mov bx, app_data ;os_data - mov ds, bx - mov es, bx + save_ring3_context + mov eax, [esp + 32] + mov bx, app_data ;os_data + mov ds, bx + mov es, bx - cmp [v86_irqhooks+eax*8], 0 - jnz v86_irq + cmp [v86_irqhooks+eax*8], 0 + jnz v86_irq - mov ebx, [irq_tab+eax*4] - test ebx, ebx - jz .exit + mov ebx, [irq_tab+eax*4] + test ebx, ebx + jz .exit - call ebx - mov [check_idle_semaphore],5 + call ebx + mov [check_idle_semaphore],5 .exit: - cmp dword [esp + 32], 8 - mov al, 0x20 - jb @f - out 0xa0, al + cmp dword [esp + 32], 8 + mov al, 0x20 + jb @f + + out 0xa0, al @@: - out 0x20, al + out 0x20, al - restore_ring3_context - add esp, 4 + restore_ring3_context + add esp, 4 - iret + iret align 4 proc get_notify stdcall, p_ev:dword .wait: - mov ebx,[current_slot] - test dword [ebx+APPDATA.event_mask],EVENT_NOTIFY - jz @f - and dword [ebx+APPDATA.event_mask], not EVENT_NOTIFY - mov edi, [p_ev] - mov dword [edi], EV_INTR - mov eax, [ebx+APPDATA.event] - mov dword [edi+4], eax - ret + mov ebx,[current_slot] + test dword [ebx+APPDATA.event_mask],EVENT_NOTIFY + jz @f + + and dword [ebx+APPDATA.event_mask], not EVENT_NOTIFY + mov edi, [p_ev] + mov dword [edi], EV_INTR + mov eax, [ebx+APPDATA.event] + mov dword [edi+4], eax + ret @@: - call change_task - jmp .wait + call change_task + jmp .wait endp align 4 proc pci_read32 stdcall, bus:dword, devfn:dword, reg:dword - push ebx - xor eax, eax - xor ebx, ebx - mov ah, byte [bus] - mov al, 6 - mov bh, byte [devfn] - mov bl, byte [reg] - call pci_read_reg - pop ebx - ret + push ebx + xor eax, eax + xor ebx, ebx + mov ah, byte [bus] + mov al, 6 + mov bh, byte [devfn] + mov bl, byte [reg] + call pci_read_reg + pop ebx + ret endp align 4 proc pci_read16 stdcall, bus:dword, devfn:dword, reg:dword - push ebx - xor eax, eax - xor ebx, ebx - mov ah, byte [bus] - mov al, 5 - mov bh, byte [devfn] - mov bl, byte [reg] - call pci_read_reg - pop ebx - ret + push ebx + xor eax, eax + xor ebx, ebx + mov ah, byte [bus] + mov al, 5 + mov bh, byte [devfn] + mov bl, byte [reg] + call pci_read_reg + pop ebx + ret endp align 4 proc pci_read8 stdcall, bus:dword, devfn:dword, reg:dword - push ebx - xor eax, eax - xor ebx, ebx - mov ah, byte [bus] - mov al, 4 - mov bh, byte [devfn] - mov bl, byte [reg] - call pci_read_reg - pop ebx - ret + push ebx + xor eax, eax + xor ebx, ebx + mov ah, byte [bus] + mov al, 4 + mov bh, byte [devfn] + mov bl, byte [reg] + call pci_read_reg + pop ebx + ret endp align 4 proc pci_write8 stdcall, bus:dword, devfn:dword, reg:dword, val:dword - push ebx - xor eax, eax - xor ebx, ebx - mov ah, byte [bus] - mov al, 8 - mov bh, byte [devfn] - mov bl, byte [reg] - mov ecx, [val] - call pci_write_reg - pop ebx - ret + push ebx + xor eax, eax + xor ebx, ebx + mov ah, byte [bus] + mov al, 8 + mov bh, byte [devfn] + mov bl, byte [reg] + mov ecx, [val] + call pci_write_reg + pop ebx + ret endp align 4 proc pci_write16 stdcall, bus:dword, devfn:dword, reg:dword, val:dword - push ebx - xor eax, eax - xor ebx, ebx - mov ah, byte [bus] - mov al, 9 - mov bh, byte [devfn] - mov bl, byte [reg] - mov ecx, [val] - call pci_write_reg - pop ebx - ret + push ebx + xor eax, eax + xor ebx, ebx + mov ah, byte [bus] + mov al, 9 + mov bh, byte [devfn] + mov bl, byte [reg] + mov ecx, [val] + call pci_write_reg + pop ebx + ret endp align 4 proc pci_write32 stdcall, bus:dword, devfn:dword, reg:dword, val:dword - push ebx - xor eax, eax - xor ebx, ebx - mov ah, byte [bus] - mov al, 10 - mov bh, byte [devfn] - mov bl, byte [reg] - mov ecx, [val] - call pci_write_reg - pop ebx - ret + push ebx + xor eax, eax + xor ebx, ebx + mov ah, byte [bus] + mov al, 10 + mov bh, byte [devfn] + mov bl, byte [reg] + mov ecx, [val] + call pci_write_reg + pop ebx + ret endp handle equ IOCTL.handle @@ -303,29 +304,29 @@ out_size equ IOCTL.out_size align 4 proc srv_handler stdcall, ioctl:dword - mov esi, [ioctl] - test esi, esi - jz .err + mov esi, [ioctl] + test esi, esi + jz .err - mov edi, [esi+handle] - cmp [edi+SRV.magic], ' SRV' - jne .fail + mov edi, [esi+handle] + cmp [edi+SRV.magic], ' SRV' + jne .fail - cmp [edi+SRV.size], SRV_SIZE - jne .fail + cmp [edi+SRV.size], SRV_SIZE + jne .fail - stdcall [edi+SRV.srv_proc], esi - ret + stdcall [edi+SRV.srv_proc], esi + ret .fail: - xor eax, eax - not eax - mov [esi+output], eax - mov [esi+out_size], 4 - ret + xor eax, eax + not eax + mov [esi+output], eax + mov [esi+out_size], 4 + ret .err: - xor eax, eax - not eax - ret + xor eax, eax + not eax + ret endp ; param @@ -336,21 +337,21 @@ endp align 4 srv_handlerEx: - cmp ebx, OS_BASE - jae .fail + cmp ebx, OS_BASE + jae .fail - mov eax, [ebx+handle] - cmp [eax+SRV.magic], ' SRV' - jne .fail + mov eax, [ebx+handle] + cmp [eax+SRV.magic], ' SRV' + jne .fail - cmp [eax+SRV.size], SRV_SIZE - jne .fail + cmp [eax+SRV.size], SRV_SIZE + jne .fail - stdcall [eax+SRV.srv_proc], ebx - ret + stdcall [eax+SRV.srv_proc], ebx + ret .fail: - or eax, -1 - ret + or eax, -1 + ret restore handle restore io_code @@ -361,189 +362,189 @@ restore out_size align 4 proc get_service stdcall, sz_name:dword - mov eax, [sz_name] - test eax, eax - jnz @F - ret + mov eax, [sz_name] + test eax, eax + jnz @F + ret @@: - mov edx, [srv.fd] + mov edx, [srv.fd] @@: - cmp edx, srv.fd-SRV_FD_OFFSET - je .not_load + cmp edx, srv.fd-SRV_FD_OFFSET + je .not_load - stdcall strncmp, edx, [sz_name], 16 - test eax, eax - je .ok + stdcall strncmp, edx, [sz_name], 16 + test eax, eax + je .ok - mov edx, [edx+SRV.fd] - jmp @B + mov edx, [edx+SRV.fd] + jmp @B .not_load: - pop ebp - jmp load_driver + pop ebp + jmp load_driver .ok: - mov eax, edx - ret + mov eax, edx + ret endp align 4 proc reg_service stdcall, name:dword, handler:dword - push ebx + push ebx xor eax, eax - cmp [name], eax - je .fail + cmp [name], eax + je .fail - cmp [handler], eax - je .fail + cmp [handler], eax + je .fail - mov eax, SRV_SIZE - call malloc ;call alloc_service - test eax, eax - jz .fail + mov eax, SRV_SIZE + call malloc ;call alloc_service + test eax, eax + jz .fail - push esi - push edi - mov edi, eax - mov esi, [name] - mov ecx, 16/4 - rep movsd - pop edi - pop esi + push esi + push edi + mov edi, eax + mov esi, [name] + mov ecx, 16/4 + rep movsd + pop edi + pop esi - mov [eax+SRV.magic], ' SRV' - mov [eax+SRV.size], SRV_SIZE + mov [eax+SRV.magic], ' SRV' + mov [eax+SRV.size], SRV_SIZE - mov ebx, srv.fd-SRV_FD_OFFSET - mov edx, [ebx+SRV.fd] - mov [eax+SRV.fd], edx - mov [eax+SRV.bk], ebx - mov [ebx+SRV.fd], eax - mov [edx+SRV.bk], eax + mov ebx, srv.fd-SRV_FD_OFFSET + mov edx, [ebx+SRV.fd] + mov [eax+SRV.fd], edx + mov [eax+SRV.bk], ebx + mov [ebx+SRV.fd], eax + mov [edx+SRV.bk], eax - mov ecx, [handler] - mov [eax+SRV.srv_proc], ecx - pop ebx - ret -.fail: - xor eax, eax + mov ecx, [handler] + mov [eax+SRV.srv_proc], ecx pop ebx - ret + ret +.fail: + xor eax, eax + pop ebx + ret endp align 4 proc get_proc stdcall, exp:dword, sz_name:dword - mov edx, [exp] + mov edx, [exp] .next: - mov eax, [edx] - test eax, eax - jz .end + mov eax, [edx] + test eax, eax + jz .end - push edx - stdcall strncmp, eax, [sz_name], 16 - pop edx - test eax, eax - jz .ok + push edx + stdcall strncmp, eax, [sz_name], 16 + pop edx + test eax, eax + jz .ok - add edx,8 - jmp .next + add edx,8 + jmp .next .ok: - mov eax, [edx+4] + mov eax, [edx+4] .end: - ret + ret endp align 4 proc get_coff_sym stdcall, pSym:dword,count:dword, sz_sym:dword @@: - stdcall strncmp, [pSym], [sz_sym], 8 - test eax,eax - jz .ok - add [pSym], 18 - dec [count] - jnz @b - xor eax, eax - ret + stdcall strncmp, [pSym], [sz_sym], 8 + test eax,eax + jz .ok + add [pSym], 18 + dec [count] + jnz @b + + xor eax, eax + ret .ok: - mov ebx, [pSym] - mov eax, [ebx+8] - ret + mov ebx, [pSym] + mov eax, [ebx+8] + ret endp align 4 -proc get_curr_task - mov eax,[CURRENT_TASK] - shl eax, 8 - ret -endp +get_curr_task: + mov eax,[CURRENT_TASK] + shl eax, 8 + ret align 4 proc get_fileinfo stdcall, file_name:dword, info:dword - locals - cmd dd ? - offset dd ? - dd ? - count dd ? - buff dd ? - db ? - name dd ? - endl + locals + cmd dd ? + offset dd ? + dd ? + count dd ? + buff dd ? + db ? + name dd ? + endl - xor eax, eax - mov ebx, [file_name] - mov ecx, [info] + xor eax, eax + mov ebx, [file_name] + mov ecx, [info] - mov [cmd], 5 - mov [offset], eax - mov [offset+4], eax - mov [count], eax - mov [buff], ecx - mov byte [buff+4], al - mov [name], ebx + mov [cmd], 5 + mov [offset], eax + mov [offset+4], eax + mov [count], eax + mov [buff], ecx + mov byte [buff+4], al + mov [name], ebx - mov eax, 70 - lea ebx, [cmd] - int 0x40 - ret + mov eax, 70 + lea ebx, [cmd] + int 0x40 + ret endp align 4 proc read_file stdcall,file_name:dword, buffer:dword, off:dword,\ bytes:dword - locals - cmd dd ? - offset dd ? - dd ? - count dd ? - buff dd ? - db ? - name dd ? - endl + locals + cmd dd ? + offset dd ? + dd ? + count dd ? + buff dd ? + db ? + name dd ? + endl - xor eax, eax - mov ebx, [file_name] - mov ecx, [off] - mov edx, [bytes] - mov esi, [buffer] + xor eax, eax + mov ebx, [file_name] + mov ecx, [off] + mov edx, [bytes] + mov esi, [buffer] - mov [cmd], eax - mov [offset], ecx - mov [offset+4], eax - mov [count], edx - mov [buff], esi - mov byte [buff+4], al - mov [name], ebx + mov [cmd], eax + mov [offset], ecx + mov [offset+4], eax + mov [count], edx + mov [buff], esi + mov byte [buff+4], al + mov [name], ebx - pushad - push eax - lea eax, [cmd] - call file_system_lfn - pop eax - popad - ret + pushad + push eax + lea eax, [cmd] + call file_system_lfn + pop eax + popad + ret endp ; description @@ -562,488 +563,491 @@ endp align 4 proc load_file stdcall, file_name:dword - locals - attr dd ? - flags dd ? - cr_time dd ? - cr_date dd ? - acc_time dd ? - acc_date dd ? - mod_time dd ? - mod_date dd ? - file_size dd ? + locals + attr dd ? + flags dd ? + cr_time dd ? + cr_date dd ? + acc_time dd ? + acc_date dd ? + mod_time dd ? + mod_date dd ? + file_size dd ? - file dd ? - file2 dd ? - endl + file dd ? + file2 dd ? + endl - push esi - push edi + push esi + push edi - lea eax, [attr] - stdcall get_fileinfo, [file_name], eax - test eax, eax - jnz .fail + lea eax, [attr] + stdcall get_fileinfo, [file_name], eax + test eax, eax + jnz .fail mov ecx, [file_size] cmp ecx, 1024*1024*16 - ja .fail + ja .fail mov edx, PG_SW call @mem_alloc@8 - mov [file], eax + mov [file], eax test eax, eax jz .fail - stdcall read_file, [file_name], eax, dword 0, [file_size] - cmp ebx, [file_size] - jne .cleanup + stdcall read_file, [file_name], eax, dword 0, [file_size] + cmp ebx, [file_size] + jne .cleanup - mov eax, [file] - cmp dword [eax], 0x4B43504B - jne .exit + mov eax, [file] + cmp dword [eax], 0x4B43504B + jne .exit mov ecx, [eax+4] mov [file_size], ecx mov edx, PG_SW call @mem_alloc@8 - test eax, eax - jz .cleanup + test eax, eax + jz .cleanup - mov [file2], eax - stdcall unpack, [file], eax - stdcall kernel_free, [file] - mov eax, [file2] - mov ebx, [file_size] + mov [file2], eax + stdcall unpack, [file], eax + + mov ecx, [file] + call @mem_free@4 + + mov eax, [file2] + mov ebx, [file_size] .exit: - push eax - lea edi, [eax+ebx] ;cleanup remain space - mov ecx, 4096 ;from file end - and ebx, 4095 - jz @f - sub ecx, ebx - xor eax, eax - cld - rep stosb + push eax + lea edi, [eax+ebx] ;cleanup remain space + mov ecx, 4096 ;from file end + and ebx, 4095 + jz @f + sub ecx, ebx + xor eax, eax + cld + rep stosb @@: - mov ebx, [file_size] - pop eax - pop edi - pop esi - ret + mov ebx, [file_size] + pop eax + pop edi + pop esi + ret .cleanup: - stdcall kernel_free, [file] + stdcall kernel_free, [file] .fail: - xor eax, eax - xor ebx, ebx - pop edi - pop esi - ret + xor eax, eax + xor ebx, ebx + pop edi + pop esi + ret endp align 4 proc get_proc_ex stdcall, proc_name:dword, imports:dword .look_up: - mov edx, [imports] - test edx, edx - jz .end - mov edx, [edx] - test edx, edx - jz .end + mov edx, [imports] + test edx, edx + jz .end + mov edx, [edx] + test edx, edx + jz .end .next: - mov eax, [edx] - test eax, eax - jz .next_table + mov eax, [edx] + test eax, eax + jz .next_table - push edx - stdcall strncmp, eax, [proc_name], 16 - pop edx - test eax, eax - jz .ok + push edx + stdcall strncmp, eax, [proc_name], 16 + pop edx + test eax, eax + jz .ok - add edx,8 - jmp .next + add edx,8 + jmp .next .next_table: - add [imports], 4 - jmp .look_up + add [imports], 4 + jmp .look_up .ok: - mov eax, [edx+4] - ret + mov eax, [edx+4] + ret .end: - xor eax, eax - ret + xor eax, eax + ret endp align 4 proc fix_coff_symbols stdcall, sec:dword, symbols:dword,\ sym_count:dword, strings:dword, imports:dword - locals - retval dd ? - endl + locals + retval dd ? + endl - mov edi, [symbols] - mov [retval], 1 + mov edi, [symbols] + mov [retval], 1 .fix: - movzx ebx, [edi+CSYM.SectionNumber] - test ebx, ebx - jnz .internal - mov eax, dword [edi+CSYM.Name] - test eax, eax - jnz @F + movzx ebx, [edi+CSYM.SectionNumber] + test ebx, ebx + jnz .internal + mov eax, dword [edi+CSYM.Name] + test eax, eax + jnz @F - mov edi, [edi+4] - add edi, [strings] + mov edi, [edi+4] + add edi, [strings] @@: - push edi - stdcall get_proc_ex, edi,[imports] - pop edi + push edi + stdcall get_proc_ex, edi,[imports] + pop edi - xor ebx, ebx - test eax, eax - jnz @F + xor ebx, ebx + test eax, eax + jnz @F - mov esi, msg_unresolved - call sys_msg_board_str - mov esi, edi - call sys_msg_board_str - mov esi, msg_CR - call sys_msg_board_str + mov esi, msg_unresolved + call sys_msg_board_str + mov esi, edi + call sys_msg_board_str + mov esi, msg_CR + call sys_msg_board_str - mov [retval],0 + mov [retval],0 @@: - mov edi, [symbols] - mov [edi+CSYM.Value], eax - jmp .next + mov edi, [symbols] + mov [edi+CSYM.Value], eax + jmp .next .internal: - cmp bx, -1 - je .next - cmp bx, -2 - je .next + cmp bx, -1 + je .next + cmp bx, -2 + je .next - dec ebx - shl ebx, 3 - lea ebx, [ebx+ebx*4] - add ebx, [sec] + dec ebx + shl ebx, 3 + lea ebx, [ebx+ebx*4] + add ebx, [sec] - mov eax, [ebx+CFS.VirtualAddress] - add [edi+CSYM.Value], eax + mov eax, [ebx+CFS.VirtualAddress] + add [edi+CSYM.Value], eax .next: - add edi, CSYM_SIZE - mov [symbols], edi - dec [sym_count] - jnz .fix - mov eax, [retval] - ret + add edi, CSYM_SIZE + mov [symbols], edi + dec [sym_count] + jnz .fix + mov eax, [retval] + ret endp align 4 proc fix_coff_relocs stdcall, coff:dword, sec:dword, sym:dword - locals - n_sec dd ? - endl + locals + n_sec dd ? + endl - mov eax, [coff] - movzx ebx, [eax+CFH.nSections] - mov [n_sec], ebx + mov eax, [coff] + movzx ebx, [eax+CFH.nSections] + mov [n_sec], ebx .fix_sec: - mov esi, [sec] - mov edi, [esi+CFS.PtrReloc] - add edi, [coff] + mov esi, [sec] + mov edi, [esi+CFS.PtrReloc] + add edi, [coff] - movzx ecx, [esi+CFS.NumReloc] - test ecx, ecx - jz .next + movzx ecx, [esi+CFS.NumReloc] + test ecx, ecx + jz .next .next_reloc: - mov ebx, [edi+CRELOC.SymIndex] - add ebx,ebx - lea ebx,[ebx+ebx*8] - add ebx, [sym] + mov ebx, [edi+CRELOC.SymIndex] + add ebx,ebx + lea ebx,[ebx+ebx*8] + add ebx, [sym] - mov edx, [ebx+CSYM.Value] + mov edx, [ebx+CSYM.Value] - cmp [edi+CRELOC.Type], 6 - je .dir_32 + cmp [edi+CRELOC.Type], 6 + je .dir_32 - cmp [edi+CRELOC.Type], 20 - jne .next_reloc + cmp [edi+CRELOC.Type], 20 + jne .next_reloc .rel_32: - mov eax, [edi+CRELOC.VirtualAddress] - add eax, [esi+CFS.VirtualAddress] - sub edx, eax - sub edx, 4 - jmp .fix + mov eax, [edi+CRELOC.VirtualAddress] + add eax, [esi+CFS.VirtualAddress] + sub edx, eax + sub edx, 4 + jmp .fix .dir_32: - mov eax, [edi+CRELOC.VirtualAddress] - add eax, [esi+CFS.VirtualAddress] + mov eax, [edi+CRELOC.VirtualAddress] + add eax, [esi+CFS.VirtualAddress] .fix: - add [eax], edx - add edi, 10 - dec ecx - jnz .next_reloc + add [eax], edx + add edi, 10 + dec ecx + jnz .next_reloc .next: - add [sec], COFF_SECTION_SIZE - dec [n_sec] - jnz .fix_sec + add [sec], COFF_SECTION_SIZE + dec [n_sec] + jnz .fix_sec .exit: - ret + ret endp align 4 proc load_driver stdcall, driver_name:dword - locals - coff dd ? - sym dd ? - strings dd ? - img_size dd ? - img_base dd ? - start dd ? + locals + coff dd ? + sym dd ? + strings dd ? + img_size dd ? + img_base dd ? + start dd ? - exports dd ? ;fake exports table - dd ? - file_name rb 13+16+4+1 ; '/sys/drivers/.obj' - endl + exports dd ? ;fake exports table + dd ? + file_name rb 13+16+4+1 ; '/sys/drivers/.obj' + endl - lea edx, [file_name] - mov dword [edx], '/sys' - mov dword [edx+4], '/dri' - mov dword [edx+8], 'vers' - mov byte [edx+12], '/' - mov esi, [driver_name] - lea edi, [edx+13] - mov ecx, 16 + lea edx, [file_name] + mov dword [edx], '/sys' + mov dword [edx+4], '/dri' + mov dword [edx+8], 'vers' + mov byte [edx+12], '/' + mov esi, [driver_name] + lea edi, [edx+13] + mov ecx, 16 @@: - lodsb - test al, al - jz @f - stosb - loop @b + lodsb + test al, al + jz @f + stosb + loop @b @@: - mov dword [edi], '.obj' - mov byte [edi+4], 0 - stdcall load_file, edx + mov dword [edi], '.obj' + mov byte [edi+4], 0 + stdcall load_file, edx - test eax, eax - jz .exit + test eax, eax + jz .exit - mov [coff], eax + mov [coff], eax movzx ebx, [eax+CFH.nSections] - lea edx, [eax+20] + lea edx, [eax+20] xor ecx, ecx @@: add ecx, [edx+CFS.SizeOfRawData] add ecx, 15 and ecx, not 15 - add edx, COFF_SECTION_SIZE + add edx, COFF_SECTION_SIZE dec ebx - jnz @B + jnz @B mov [img_size], ecx mov edx, PG_SW call @mem_alloc@8 - test eax, eax - jz .fail - mov [img_base], eax + test eax, eax + jz .fail - mov edi, eax - xor eax, eax - mov ecx, [img_size] - add ecx, 4095 - and ecx, not 4095 - shr ecx, 2 - cld - rep stosd + mov [img_base], eax - mov edx, [coff] - movzx ebx, [edx+CFH.nSections] - mov edi, [img_base] - lea eax, [edx+20] + mov edi, eax + xor eax, eax + mov ecx, [img_size] + add ecx, 4095 + and ecx, not 4095 + shr ecx, 2 + cld + rep stosd + + mov edx, [coff] + movzx ebx, [edx+CFH.nSections] + mov edi, [img_base] + lea eax, [edx+20] @@: - mov [eax+CFS.VirtualAddress], edi - mov esi, [eax+CFS.PtrRawData] - test esi, esi - jnz .copy - add edi, [eax+CFS.SizeOfRawData] - jmp .next + mov [eax+CFS.VirtualAddress], edi + mov esi, [eax+CFS.PtrRawData] + test esi, esi + jnz .copy + add edi, [eax+CFS.SizeOfRawData] + jmp .next .copy: - add esi, edx - mov ecx, [eax+CFS.SizeOfRawData] - cld - rep movsb + add esi, edx + mov ecx, [eax+CFS.SizeOfRawData] + cld + rep movsb .next: - add edi, 15 - and edi, not 15 - add eax, COFF_SECTION_SIZE - dec ebx - jnz @B + add edi, 15 + and edi, not 15 + add eax, COFF_SECTION_SIZE + dec ebx + jnz @B - mov ebx, [edx+CFH.pSymTable] - add ebx, edx - mov [sym], ebx - mov ecx, [edx+CFH.nSymbols] - add ecx,ecx - lea ecx,[ecx+ecx*8] ;ecx*=18 = nSymbols*CSYM_SIZE - add ecx, [sym] - mov [strings], ecx + mov ebx, [edx+CFH.pSymTable] + add ebx, edx + mov [sym], ebx + mov ecx, [edx+CFH.nSymbols] + add ecx,ecx + lea ecx,[ecx+ecx*8] ;ecx*=18 = nSymbols*CSYM_SIZE + add ecx, [sym] + mov [strings], ecx - lea ebx, [exports] - mov dword [ebx], kernel_export - mov dword [ebx+4], 0 - lea eax, [edx+20] + lea ebx, [exports] + mov dword [ebx], kernel_export + mov dword [ebx+4], 0 + lea eax, [edx+20] - stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols],\ - [strings], ebx - test eax, eax - jz .link_fail + stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols],\ + [strings], ebx + test eax, eax + jz .link_fail - mov ebx, [coff] - add ebx, 20 - stdcall fix_coff_relocs, [coff], ebx, [sym] + mov ebx, [coff] + add ebx, 20 + stdcall fix_coff_relocs, [coff], ebx, [sym] - mov ebx, [coff] - stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szVersion - test eax, eax - jz .link_fail + mov ebx, [coff] + stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szVersion + test eax, eax + jz .link_fail - mov eax, [eax] - shr eax, 16 - cmp eax, DRV_COMPAT - jb .ver_fail + mov eax, [eax] + shr eax, 16 + cmp eax, DRV_COMPAT + jb .ver_fail - cmp eax, DRV_CURRENT - ja .ver_fail + cmp eax, DRV_CURRENT + ja .ver_fail - mov ebx, [coff] - stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szSTART - mov [start], eax + mov ebx, [coff] + stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szSTART + mov [start], eax - stdcall kernel_free, [coff] + stdcall kernel_free, [coff] - mov ebx, [start] - stdcall ebx, DRV_ENTRY - test eax, eax - jnz .ok + mov ebx, [start] + stdcall ebx, DRV_ENTRY + test eax, eax + jnz .ok - stdcall kernel_free, [img_base] - xor eax, eax - ret + stdcall kernel_free, [img_base] + xor eax, eax + ret .ok: - mov ebx, [img_base] - mov [eax+SRV.base], ebx - mov ecx, [start] - mov [eax+SRV.entry], ecx - ret + mov ebx, [img_base] + mov [eax+SRV.base], ebx + mov ecx, [start] + mov [eax+SRV.entry], ecx + ret .ver_fail: - mov esi, msg_CR - call sys_msg_board_str - mov esi, [driver_name] - call sys_msg_board_str - mov esi, msg_CR - call sys_msg_board_str - mov esi, msg_version - call sys_msg_board_str - mov esi, msg_www - call sys_msg_board_str - jmp .cleanup + mov esi, msg_CR + call sys_msg_board_str + mov esi, [driver_name] + call sys_msg_board_str + mov esi, msg_CR + call sys_msg_board_str + mov esi, msg_version + call sys_msg_board_str + mov esi, msg_www + call sys_msg_board_str + jmp .cleanup .link_fail: - mov esi, msg_module - call sys_msg_board_str - mov esi, [driver_name] - call sys_msg_board_str - mov esi, msg_CR - call sys_msg_board_str + mov esi, msg_module + call sys_msg_board_str + mov esi, [driver_name] + call sys_msg_board_str + mov esi, msg_CR + call sys_msg_board_str .cleanup: - stdcall kernel_free,[img_base] + stdcall kernel_free,[img_base] .fail: - stdcall kernel_free, [coff] + stdcall kernel_free, [coff] .exit: - xor eax, eax - ret + xor eax, eax + ret endp align 4 proc load_library stdcall, file_name:dword - locals - coff dd ? - sym dd ? - strings dd ? - img_size dd ? - img_base dd ? - exports dd ? - endl + locals + coff dd ? + sym dd ? + strings dd ? + img_size dd ? + img_base dd ? + exports dd ? + endl - cli + cli - stdcall load_file, [file_name] - test eax, eax - jz .fail + stdcall load_file, [file_name] + test eax, eax + jz .fail - mov [coff], eax - movzx ecx, [eax+CFH.nSections] - xor ebx, ebx + mov [coff], eax + movzx ecx, [eax+CFH.nSections] + xor ebx, ebx - lea edx, [eax+20] + lea edx, [eax+20] @@: - add ebx, [edx+CFS.SizeOfRawData] - add ebx, 15 - and ebx, not 15 - add edx, COFF_SECTION_SIZE - dec ecx - jnz @B - mov [img_size], ebx + add ebx, [edx+CFS.SizeOfRawData] + add ebx, 15 + and ebx, not 15 + add edx, COFF_SECTION_SIZE + dec ecx + jnz @B + mov [img_size], ebx - call init_heap - stdcall user_alloc, [img_size] + call init_heap + stdcall user_alloc, [img_size] - test eax, eax - jz .fail - mov [img_base], eax + test eax, eax + jz .fail + mov [img_base], eax - mov edx, [coff] - movzx ebx, [edx+CFH.nSections] - mov edi, [img_base] - lea eax, [edx+20] + mov edx, [coff] + movzx ebx, [edx+CFH.nSections] + mov edi, [img_base] + lea eax, [edx+20] @@: - mov [eax+CFS.VirtualAddress], edi - mov esi, [eax+CFS.PtrRawData] - test esi, esi - jnz .copy - add edi, [eax+CFS.SizeOfRawData] - jmp .next + mov [eax+CFS.VirtualAddress], edi + mov esi, [eax+CFS.PtrRawData] + test esi, esi + jnz .copy + add edi, [eax+CFS.SizeOfRawData] + jmp .next .copy: - add esi, edx - ; add edi, new_app_base - mov ecx, [eax+CFS.SizeOfRawData] - cld - rep movsb + add esi, edx + mov ecx, [eax+CFS.SizeOfRawData] + cld + rep movsb .next: - add edi, 15 ;-new_app_base - and edi, -16 - add eax, COFF_SECTION_SIZE - dec ebx - jnz @B + add edi, 15 ;-new_app_base + and edi, -16 + add eax, COFF_SECTION_SIZE + dec ebx + jnz @B - mov ebx, [edx+CFH.pSymTable] - add ebx, edx - mov [sym], ebx - mov ecx, [edx+CFH.nSymbols] - add ecx,ecx - lea ecx,[ecx+ecx*8] ;ecx*=18 = nSymbols*CSYM_SIZE - add ecx, [sym] - mov [strings], ecx + mov ebx, [edx+CFH.pSymTable] + add ebx, edx + mov [sym], ebx + mov ecx, [edx+CFH.nSymbols] + add ecx,ecx + lea ecx,[ecx+ecx*8] ;ecx*=18 = nSymbols*CSYM_SIZE + add ecx, [sym] + mov [strings], ecx - lea eax, [edx+20] + lea eax, [edx+20] - stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols],\ - [strings], dword 0 - test eax, eax - jnz @F + stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols],\ + [strings], dword 0 + test eax, eax + jnz @F @@: - mov edx, [coff] + mov edx, [coff] ; movzx ebx, [edx+CFH.nSections] ; xor edi, edi @@ -1054,44 +1058,44 @@ proc load_library stdcall, file_name:dword ; dec ebx ; jnz @B - add edx, 20 - stdcall fix_coff_relocs, [coff], edx, [sym] + add edx, 20 + stdcall fix_coff_relocs, [coff], edx, [sym] - mov ebx, [coff] - stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szEXPORTS - mov [exports], eax + mov ebx, [coff] + stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szEXPORTS + mov [exports], eax - stdcall kernel_free, [coff] - mov eax, [exports] - ret + stdcall kernel_free, [coff] + mov eax, [exports] + ret .fail: - xor eax, eax - ret + xor eax, eax + ret endp align 4 proc stop_all_services - mov edx, [srv.fd] + mov edx, [srv.fd] .next: - cmp edx, srv.fd-SRV_FD_OFFSET - je .done - cmp [edx+SRV.magic], ' SRV' - jne .next - cmp [edx+SRV.size], SRV_SIZE - jne .next + cmp edx, srv.fd-SRV_FD_OFFSET + je .done + cmp [edx+SRV.magic], ' SRV' + jne .next + cmp [edx+SRV.size], SRV_SIZE + jne .next - mov ebx, [edx+SRV.entry] - mov edx, [edx+SRV.fd] - test ebx, ebx - jz .next + mov ebx, [edx+SRV.entry] + mov edx, [edx+SRV.fd] + test ebx, ebx + jz .next - push edx - stdcall ebx, dword -1 - pop edx - jmp .next + push edx + stdcall ebx, dword -1 + pop edx + jmp .next .done: - ret + ret endp ; param @@ -1101,27 +1105,27 @@ endp align 4 create_kernel_object: - push ebx - call malloc - pop ebx - test eax, eax - jz .fail + push ebx + call malloc + pop ebx + test eax, eax + jz .fail - mov ecx,[current_slot] - add ecx, APP_OBJ_OFFSET + mov ecx,[current_slot] + add ecx, APP_OBJ_OFFSET - pushfd - cli - mov edx, [ecx+APPOBJ.fd] - mov [eax+APPOBJ.fd], edx - mov [eax+APPOBJ.bk], ecx - mov [eax+APPOBJ.pid], ebx + pushfd + cli + mov edx, [ecx+APPOBJ.fd] + mov [eax+APPOBJ.fd], edx + mov [eax+APPOBJ.bk], ecx + mov [eax+APPOBJ.pid], ebx - mov [ecx+APPOBJ.fd], eax - mov [edx+APPOBJ.bk], eax - popfd + mov [ecx+APPOBJ.fd], eax + mov [edx+APPOBJ.bk], eax + popfd .fail: - ret + ret ; param ; eax= object @@ -1129,23 +1133,23 @@ create_kernel_object: align 4 destroy_kernel_object: - pushfd - cli - mov ebx, [eax+APPOBJ.fd] - mov ecx, [eax+APPOBJ.bk] - mov [ebx+APPOBJ.bk], ecx - mov [ecx+APPOBJ.fd], ebx - popfd + pushfd + cli + mov ebx, [eax+APPOBJ.fd] + mov ecx, [eax+APPOBJ.bk] + mov [ebx+APPOBJ.bk], ecx + mov [ecx+APPOBJ.fd], ebx + popfd - xor edx, edx ;clear common header - mov [eax], edx - mov [eax+4], edx - mov [eax+8], edx - mov [eax+12], edx - mov [eax+16], edx + xor edx, edx ;clear common header + mov [eax], edx + mov [eax+4], edx + mov [eax+8], edx + mov [eax+12], edx + mov [eax+16], edx - call free ;release object memory - ret + call free ;release object memory + ret diff --git a/kernel/branches/kolibri_pe/core/exports.inc b/kernel/branches/kolibri_pe/core/exports.inc index c7bb10d3e5..8038ad6163 100644 --- a/kernel/branches/kolibri_pe/core/exports.inc +++ b/kernel/branches/kolibri_pe/core/exports.inc @@ -112,7 +112,7 @@ kernel_export: dd szReleasePages , release_pages dd szFreeKernelSpace , free_kernel_space ;stdcall - dd szHeapAlloc , @heap_alloc@8 ;fastcall + dd szHeapAlloc , @mem_alloc@8 ;fastcall dd szKernelFree , kernel_free ;stdcall dd szUserAlloc , user_alloc ;stdcall dd szUserFree , user_free ;stdcall diff --git a/kernel/branches/kolibri_pe/core/heap.c b/kernel/branches/kolibri_pe/core/heap.c index 75a688e1df..c306d68628 100644 --- a/kernel/branches/kolibri_pe/core/heap.c +++ b/kernel/branches/kolibri_pe/core/heap.c @@ -13,33 +13,42 @@ typedef struct addr_t base; size_t size; void* parent; - u32_t reserved; + u32_t state; }md_t; -typedef struct { - SPINLOCK_DECLARE(lock); /**< this lock protects everything below */ +#define MD_FREE 1 +#define MD_USED 2 - u32_t availmask; - link_t list[32]; +typedef struct { + SPINLOCK_DECLARE(lock); /**< this lock protects everything below */ + + u32_t availmask; + link_t free[32]; + + link_t used; }heap_t; + slab_cache_t *md_slab; slab_cache_t *phm_slab; -heap_t lheap; -heap_t sheap; + +heap_t lheap; +heap_t sheap; + + static inline void _set_lmask(count_t idx) -{ asm volatile ("bts DWORD PTR [_lheap], %0"::"r"(idx):"cc"); } +{ asm volatile ("bts %0, _lheap"::"r"(idx):"cc"); } static inline void _reset_lmask(count_t idx) -{ asm volatile ("btr DWORD PTR [_lheap], %0"::"r"(idx):"cc"); } +{ asm volatile ("btr %0, _lheap"::"r"(idx):"cc"); } static inline void _set_smask(count_t idx) -{ asm volatile ("bts DWORD PTR [_sheap], %0"::"r"(idx):"cc"); } +{ asm volatile ("bts %0, _sheap"::"r"(idx):"cc"); } static inline void _reset_smask(count_t idx) -{ asm volatile ("btr DWORD PTR [_sheap], %0"::"r"(idx):"cc"); } +{ asm volatile ("btr %0, _sheap"::"r"(idx):"cc"); } int __fastcall init_heap(addr_t base, size_t size) @@ -54,10 +63,14 @@ int __fastcall init_heap(addr_t base, size_t size) for (i = 0; i < 32; i++) { - list_initialize(&lheap.list[i]); - list_initialize(&sheap.list[i]); + list_initialize(&lheap.free[i]); + list_initialize(&sheap.free[i]); }; + list_initialize(&lheap.used); + list_initialize(&sheap.used); + + md_slab = slab_cache_create(sizeof(md_t), 32,NULL,NULL,SLAB_CACHE_MAGDEFERRED); md = (md_t*)slab_alloc(md_slab,0); @@ -66,9 +79,9 @@ int __fastcall init_heap(addr_t base, size_t size) md->base = base; md->size = size; md->parent = NULL; - md->reserved = 0; + md->state = MD_FREE; - list_prepend(&md->link, &lheap.list[31]); + list_prepend(&md->link, &lheap.free[31]); lheap.availmask = 0x80000000; sheap.availmask = 0x00000000; @@ -93,8 +106,8 @@ md_t* __fastcall find_large_md(size_t size) { if(idx0 == 31) { - md_t *tmp = (md_t*)lheap.list[31].next; - while((link_t*)tmp != &lheap.list[31]) + md_t *tmp = (md_t*)lheap.free[31].next; + while((link_t*)tmp != &lheap.free[31]) { if(tmp->size >= size) { @@ -110,163 +123,258 @@ md_t* __fastcall find_large_md(size_t size) { idx0 = _bsf(mask); - ASSERT( !list_empty(&lheap.list[idx0])) + ASSERT( !list_empty(&lheap.free[idx0])) - md = (md_t*)lheap.list[idx0].next; + md = (md_t*)lheap.free[idx0].next; }; } else return NULL; + ASSERT(md->state == MD_FREE); + list_remove((link_t*)md); - if(list_empty(&lheap.list[idx0])) + if(list_empty(&lheap.free[idx0])) _reset_lmask(idx0); if(md->size > size) { count_t idx1; - md_t *new_md = (md_t*)slab_alloc(md_slab,0); + md_t *new_md = (md_t*)slab_alloc(md_slab,0); /* FIXME check */ link_initialize(&new_md->link); list_insert(&new_md->adj, &md->adj); new_md->base = md->base; new_md->size = size; + new_md->state = MD_USED; md->base+= size; md->size-= size; idx1 = (md->size>>22) - 1 < 32 ? (md->size>>22) - 1 : 31; - list_prepend(&md->link, &lheap.list[idx1]); + list_prepend(&md->link, &lheap.free[idx1]); _set_lmask(idx1); return new_md; - } + }; + md->state = MD_USED; + return md; } md_t* __fastcall find_small_md(size_t size) { - eflags_t efl; + eflags_t efl; - md_t *md = NULL; + md_t *md = NULL; - count_t idx0; - u32_t mask; + count_t idx0; + u32_t mask; - ASSERT((size & 0xFFF) == 0); + ASSERT((size & 0xFFF) == 0); - efl = safe_cli(); + efl = safe_cli(); - idx0 = (size>>12) - 1 < 32 ? (size>>12) - 1 : 31; - mask = sheap.availmask & ( -1<>12) - 1 < 32 ? (size>>12) - 1 : 31; + mask = sheap.availmask & ( -1<size >= size) - { - md = tmp; - break; - }; - tmp = (md_t*)tmp->link.next; + ASSERT( !list_empty(&sheap.free[31])); + + md_t *tmp = (md_t*)sheap.free[31].next; + while((link_t*)tmp != &sheap.free[31]) + { + if(tmp->size >= size) + { + md = tmp; + break; + }; + tmp = (md_t*)tmp->link.next; + }; + } + else + { + idx0 = _bsf(mask); + + ASSERT( !list_empty(&sheap.free[idx0])); + + md = (md_t*)sheap.free[idx0].next; + } + }; + + if(md) + { + DBG("remove md %x\n", md); + + ASSERT(md->state==MD_FREE); + + list_remove((link_t*)md); + if(list_empty(&sheap.free[idx0])) + _reset_smask(idx0); + } + else + { + md_t *lmd; + lmd = find_large_md((size+0x3FFFFF)&~0x3FFFFF); + + DBG("get large md %x\n", lmd); + + if( !lmd) + { + safe_sti(efl); + return NULL; }; - } - else - { - idx0 = _bsf(mask); - ASSERT( !list_empty(&sheap.list[idx0])) - md = (md_t*)sheap.list[idx0].next; - } - }; - if(md) - { - DBG("remove md %x\n", md); + md = (md_t*)slab_alloc(md_slab,0); /* FIXME check */ - list_remove((link_t*)md); - if(list_empty(&sheap.list[idx0])) - _reset_smask(idx0); - } - else - { - md_t *lmd; - lmd = find_large_md((size+0x3FFFFF)&~0x3FFFFF); + link_initialize(&md->link); + list_initialize(&md->adj); + md->base = lmd->base; + md->size = lmd->size; + md->parent = lmd; + md->state = MD_USED; + }; - DBG("get large md %x\n", lmd); + if(md->size > size) + { + count_t idx1; + md_t *new_md = (md_t*)slab_alloc(md_slab,0); /* FIXME check */ - if( !lmd) - { - safe_sti(efl); - return NULL; - }; + link_initialize(&new_md->link); + list_insert(&new_md->adj, &md->adj); - md = (md_t*)slab_alloc(md_slab,0); - link_initialize(&md->link); - list_initialize(&md->adj); - md->base = lmd->base; - md->size = lmd->size; - md->parent = lmd; - md->reserved = 0; - }; + new_md->base = md->base; + new_md->size = size; + new_md->parent = md->parent; + new_md->state = MD_USED; - if(md->size > size) - { - count_t idx1; - md_t *new_md = (md_t*)slab_alloc(md_slab,0); + md->base+= size; + md->size-= size; + md->state = MD_FREE; - link_initialize(&new_md->link); - list_insert(&new_md->adj, &md->adj); + idx1 = (md->size>>12) - 1 < 32 ? (md->size>>12) - 1 : 31; - new_md->base = md->base; - new_md->size = size; - new_md->parent = md->parent; - new_md->reserved = 0; + DBG("insert md %x, base %x size %x idx %x\n", md,md->base, md->size,idx1); - md->base+= size; - md->size-= size; + if( idx1 < 31) + list_prepend(&md->link, &sheap.free[idx1]); + else + { + if( list_empty(&sheap.free[31])) + list_prepend(&md->link, &sheap.free[31]); + else + { + md_t *tmp = (md_t*)sheap.free[31].next; - idx1 = (md->size>>12) - 1 < 32 ? (md->size>>12) - 1 : 31; + while((link_t*)tmp != &sheap.free[31]) + { + if(md->base < tmp->base) + break; + tmp = (md_t*)tmp->link.next; + } + list_insert(&md->link, &tmp->link); + }; + }; - DBG("insert md %x, base %x size %x idx %x\n", md,md->base, md->size,idx1); + _set_smask(idx1); - if( idx1 < 31) - list_prepend(&md->link, &sheap.list[idx1]); - else - { - if( list_empty(&sheap.list[31])) - list_prepend(&md->link, &sheap.list[31]); - else - { - md_t *tmp = (md_t*)sheap.list[31].next; + safe_sti(efl); - while((link_t*)tmp != &sheap.list[31]) - { - if(md->base < tmp->base) - break; - tmp = (md_t*)tmp->link.next; - } - list_insert(&md->link, &tmp->link); - }; - }; + return new_md; + }; - _set_smask(idx1); + md->state = MD_USED; - safe_sti(efl); + safe_sti(efl); - return new_md; - } - safe_sti(efl); - return md; + return md; } +void __fastcall free_small_md(md_t *md) +{ + eflags_t efl ; + md_t *fd; + md_t *bk; + count_t idx; + + efl = safe_cli(); + spinlock_lock(&sheap.lock); + + if( !list_empty(&md->adj)) + { + bk = (md_t*)md->adj.prev; + fd = (md_t*)md->adj.next; + + if(fd->state == MD_FREE) + { + idx = (fd->size>>12) - 1 < 32 ? (fd->size>>12) - 1 : 31; + + list_remove((link_t*)fd); + if(list_empty(&sheap.free[idx])) + _reset_smask(idx); + + md->size+= fd->size; + md->adj.next = fd->adj.next; + md->adj.next->prev = (link_t*)md; + slab_free(md_slab, fd); + }; + if(bk->state == MD_FREE) + { + idx = (bk->size>>12) - 1 < 32 ? (bk->size>>12) - 1 : 31; + + list_remove((link_t*)bk); + if(list_empty(&sheap.free[idx])) + _reset_smask(idx); + + bk->size+= md->size; + bk->adj.next = md->adj.next; + bk->adj.next->prev = (link_t*)bk; + slab_free(md_slab, md); + md = fd; + }; + }; + + md->state = MD_FREE; + + idx = (md->size>>12) - 1 < 32 ? (md->size>>12) - 1 : 31; + + _set_smask(idx); + + if( idx < 31) + list_prepend(&md->link, &sheap.free[idx]); + else + { + if( list_empty(&sheap.free[31])) + list_prepend(&md->link, &sheap.free[31]); + else + { + md_t *tmp = (md_t*)sheap.free[31].next; + + while((link_t*)tmp != &sheap.free[31]) + { + if(md->base < tmp->base) + break; + tmp = (md_t*)tmp->link.next; + } + list_insert(&md->link, &tmp->link); + }; + }; + spinlock_unlock(&sheap.lock); + safe_sti(efl); + +}; + + +#define page_tabs 0xDF800000 + +/* phismem_t* __fastcall phis_alloc(count_t count) { phismem_t *phm; @@ -289,8 +397,6 @@ phismem_t* __fastcall phis_alloc(count_t count) return phm; } -#define page_tabs 0xDF800000 - void map_phm(addr_t base, phismem_t *phm, u32_t mapflags) { count_t count; @@ -317,62 +423,130 @@ void map_phm(addr_t base, phismem_t *phm, u32_t mapflags) } } }; +*/ -void* __fastcall mem_alloc(size_t size, u32_t flags) +void * __fastcall mem_alloc(size_t size, u32_t flags) { - md_t *md; - phismem_t *phm; + eflags_t efl; - size = (size+4095)&~4095; + md_t *md; - md = find_small_md(size); - if( md ) - { - phm = phis_alloc(size>>12); - map_phm(md->base , phm, flags); - return (void*)md->base; - } - return NULL; + DBG("\nmem_alloc: %x bytes\n", size); + + ASSERT(size != 0); + + size = (size+4095)&~4095; + + md = find_small_md(size); + + if( md ) + { + ASSERT(md->state == MD_USED); + + if( flags & PG_MAP ) + { + count_t tmp = size >> 12; + addr_t *pte = &((addr_t*)page_tabs)[md->base>>12]; + + while(tmp) + { + u32_t order; + addr_t frame; + size_t size; + + asm volatile ("bsr %1, %0":"=&r"(order):"r"(tmp):"cc"); + asm volatile ("btr %1, %0" :"=r"(tmp):"r"(order):"cc"); + + frame = core_alloc(order) | flags; /* FIXME check */ + + size = (1 << order); + while(size--) + { + *pte++ = frame; + frame+= 4096; + }; + }; + }; + + efl = safe_cli(); + spinlock_lock(&sheap.lock); + + if( list_empty(&sheap.used) ) + list_prepend(&md->link, &sheap.used); + else + { + md_t *tmp = (md_t*)sheap.used.next; + + while((link_t*)tmp != &sheap.used) + { + if(md->base < tmp->base) + break; + tmp = (md_t*)tmp->link.next; + } + list_insert(&md->link, &tmp->link); + }; + + spinlock_unlock(&sheap.lock); + safe_sti(efl); + + DBG("allocate: %x size %x\n\n",md->base, size); + return (void*)md->base; + }; + return NULL; }; -void * __fastcall heap_alloc(size_t size, u32_t flags) +void __fastcall mem_free(void *mem) { - md_t *md; + eflags_t efl; - size = (size+4095)&~4095; + md_t *tmp; + md_t *md = NULL; - md = find_small_md(size); + DBG("mem_free: %x\n",mem); - if( md ) - { - if( flags & PG_MAP ) - { - count_t tmp = size >> 12; - addr_t *pte = &((addr_t*)page_tabs)[md->base>>12]; + ASSERT( mem != 0 ); + ASSERT( ((addr_t)mem & 0xFFF) == 0 ); + ASSERT( ! list_empty(&sheap.used)); - while(tmp) - { - u32_t order; - addr_t frame; - size_t size; + efl = safe_cli(); - asm volatile ("bsr %0, %1":"=&r"(order):"r"(tmp):"cc"); - asm volatile ("btr %0, %1" :"=r"(tmp):"r"(order):"cc"); + tmp = (md_t*)sheap.used.next; - frame = core_alloc(order) | flags; + while((link_t*)tmp != &sheap.used) + { + if( tmp->base == (addr_t)mem ) + { + md = tmp; + break; + }; + tmp = (md_t*)tmp->link.next; + } - size = (1 << order); - while(size--) - { - *pte++ = frame; - frame+= 4096; - }; - }; - }; - DBG("alloc_heap: %x size %x\n\n",md->base, size); - return (void*)md->base; - }; - return NULL; + if( md ) + { + DBG("\tmd: %x base: %x size: %x\n",md, md->base, md->size); + + ASSERT(md->state == MD_USED); + + count_t tmp = md->size >> 12; + addr_t *pte = &((addr_t*)page_tabs)[md->base>>12]; + + while(tmp--) + { + *pte++ = 0; + asm volatile ( + "invlpg (%0)" + : + :"r" (mem) ); + mem+= 4096; + }; + list_remove((link_t*)md); + free_small_md( md ); + } + else + { + DBG("\tERROR: invalid base address: %x\n", mem); + }; + + safe_sti(efl); }; - - diff --git a/kernel/branches/kolibri_pe/core/mm.c b/kernel/branches/kolibri_pe/core/mm.c index a392f2ebb5..1655794e69 100644 --- a/kernel/branches/kolibri_pe/core/mm.c +++ b/kernel/branches/kolibri_pe/core/mm.c @@ -16,7 +16,7 @@ zone_t z_core; static inline u32_t save_edx(void) { u32_t val; - asm volatile ("mov %0, edx":"=r"(val)); + asm volatile ("movl %%edx, %0":"=r"(val)); return val; }; @@ -524,9 +524,9 @@ static inline int to_order(count_t arg) { int n; asm volatile ( - "xor eax, eax \n\t" - "bsr eax, edx \n\t" - "inc eax" + "xorl %eax, %eax \n\t" + "bsr %edx, %eax \n\t" + "incl %eax" :"=a" (n) :"d"(arg) ); diff --git a/kernel/branches/kolibri_pe/core/slab.c b/kernel/branches/kolibri_pe/core/slab.c index 6d3774eec2..d9b118888e 100644 --- a/kernel/branches/kolibri_pe/core/slab.c +++ b/kernel/branches/kolibri_pe/core/slab.c @@ -20,8 +20,6 @@ static slab_t *slab_create(); static slab_cache_t * slab_cache_alloc(); -void slab_free(slab_cache_t *cache, void *obj); - /** * Allocate frames for slab space and initialize @@ -313,22 +311,22 @@ static count_t slab_obj_destroy(slab_cache_t *cache, void *obj, /** Return object to cache, use slab if known */ static void _slab_free(slab_cache_t *cache, void *obj, slab_t *slab) { -// ipl_t ipl; + eflags_t efl; -// ipl = interrupts_disable(); + efl = safe_cli(); // if ((cache->flags & SLAB_CACHE_NOMAGAZINE) \ - || magazine_obj_put(cache, obj)) { +// || magazine_obj_put(cache, obj)) { slab_obj_destroy(cache, obj, slab); // } -// interrupts_restore(ipl); -// atomic_dec(&cache->allocated_objs); + safe_sti(efl); + atomic_dec(&cache->allocated_objs); } /** Return slab object to cache */ -void slab_free(slab_cache_t *cache, void *obj) +void __fastcall slab_free(slab_cache_t *cache, void *obj) { _slab_free(cache, obj, NULL); } diff --git a/kernel/branches/kolibri_pe/core/taskman.inc b/kernel/branches/kolibri_pe/core/taskman.inc index d5ca2af8e7..7c267f21c3 100644 --- a/kernel/branches/kolibri_pe/core/taskman.inc +++ b/kernel/branches/kolibri_pe/core/taskman.inc @@ -61,187 +61,186 @@ proc fs_execute ; ebp - full filename ; [esp+4] = procedure DoRead, [esp+8] = filesize & [esp+12]... - arguments for it - locals - cmdline rd 64 ;256/4 - filename rd 256 ;1024/4 - flags dd ? + locals + cmdline rd 64 ;256/4 + filename rd 256 ;1024/4 + flags dd ? - save_cr3 dd ? - slot dd ? - slot_base dd ? - file_base dd ? - file_size dd ? - ;app header data - hdr_cmdline dd ? ;0x00 - hdr_path dd ? ;0x04 - hdr_eip dd ? ;0x08 - hdr_esp dd ? ;0x0C - hdr_mem dd ? ;0x10 - hdr_i_end dd ? ;0x14 - endl + save_cr3 dd ? + slot dd ? + slot_base dd ? + file_base dd ? + file_size dd ? + ;app header data + hdr_cmdline dd ? ;0x00 + hdr_path dd ? ;0x04 + hdr_eip dd ? ;0x08 + hdr_esp dd ? ;0x0C + hdr_mem dd ? ;0x10 + hdr_i_end dd ? ;0x14 + endl - pushad + pushad - mov [flags], edx + mov [flags], edx ; [ebp] pointer to filename - lea edi, [filename] - lea ecx, [edi+1024] - mov al, '/' - stosb + lea edi, [filename] + lea ecx, [edi+1024] + mov al, '/' + stosb @@: - cmp edi, ecx - jae .bigfilename - lodsb - stosb - test al, al - jnz @b - mov esi, [ebp] - test esi, esi - jz .namecopied - mov byte [edi-1], '/' + cmp edi, ecx + jae .bigfilename + lodsb + stosb + test al, al + jnz @b + mov esi, [ebp] + test esi, esi + jz .namecopied + mov byte [edi-1], '/' @@: - cmp edi, ecx - jae .bigfilename - lodsb - stosb - test al, al - jnz @b - jmp .namecopied + cmp edi, ecx + jae .bigfilename + lodsb + stosb + test al, al + jnz @b + jmp .namecopied .bigfilename: - popad - mov eax, -ERROR_FILE_NOT_FOUND - ret + popad + mov eax, -ERROR_FILE_NOT_FOUND + ret .namecopied: - mov [cmdline], ebx - test ebx, ebx - jz @F + mov [cmdline], ebx + test ebx, ebx + jz @F - lea eax, [cmdline] - mov dword [eax+252], 0 - stdcall strncpy, eax, ebx, 255 + lea eax, [cmdline] + mov dword [eax+252], 0 + stdcall strncpy, eax, ebx, 255 @@: - lea eax, [filename] - stdcall load_file, eax - mov ecx, -ERROR_FILE_NOT_FOUND - test eax, eax - jz .err_file + lea eax, [filename] + stdcall load_file, eax + mov ecx, -ERROR_FILE_NOT_FOUND + test eax, eax + jz .err_file - mov [file_base], eax - mov [file_size], ebx + mov [file_base], eax + mov [file_size], ebx - lea ebx, [hdr_cmdline] - call test_app_header - mov ecx, -0x1F - test eax, eax - jz .err_hdr + lea ebx, [hdr_cmdline] + call test_app_header + mov ecx, -0x1F + test eax, eax + jz .err_hdr - DEBUGF 1,"%s",new_process_loading + DEBUGF 1,"%s",new_process_loading .wait_lock: - cmp [application_table_status],0 - je .get_lock - call change_task - jmp .wait_lock + cmp [application_table_status],0 + je .get_lock + call change_task + jmp .wait_lock .get_lock: - mov eax, 1 - xchg eax, [application_table_status] - cmp eax, 0 - jne .wait_lock + mov eax, 1 + xchg eax, [application_table_status] + cmp eax, 0 + jne .wait_lock - call set_application_table_status + call set_application_table_status - call get_new_process_place - test eax, eax - mov ecx, -0x20 ; too many processes - jz .err + call get_new_process_place + test eax, eax + mov ecx, -0x20 ; too many processes + jz .err - mov [slot], eax - shl eax, 8 - add eax, SLOT_BASE - mov [slot_base], eax - mov edi, eax - _clear_ 256 ;clean extended information about process + mov [slot], eax + shl eax, 8 + add eax, SLOT_BASE + mov [slot_base], eax + mov edi, eax + _clear_ 256 ;clean extended information about process ; write application name - lea eax, [filename] - stdcall strrchr, eax, '/' ; now eax points to name without path + lea eax, [filename] + stdcall strrchr, eax, '/' ; now eax points to name without path - lea esi, [eax+1] - test eax, eax - jnz @F - lea esi, [filename] + lea esi, [eax+1] + test eax, eax + jnz @F + lea esi, [filename] @@: - mov ecx, 8 ; 8 chars for name - mov edi, [slot_base] + mov ecx, 8 ; 8 chars for name + mov edi, [slot_base] .copy_process_name_loop: - lodsb - cmp al, '.' - jz .copy_process_name_done - test al, al - jz .copy_process_name_done - stosb - loop .copy_process_name_loop + lodsb + cmp al, '.' + jz .copy_process_name_done + test al, al + jz .copy_process_name_done + stosb + loop .copy_process_name_loop .copy_process_name_done: - - mov ebx, cr3 - mov [save_cr3], ebx + mov ebx, cr3 + mov [save_cr3], ebx stdcall create_app_space,[hdr_mem],[file_base],[file_size] mov ecx, -30 ; no memory - test eax, eax - jz .failed + test eax, eax + jz .failed - mov ebx,[slot_base] - mov [ebx+APPDATA.dir_table],eax - mov eax,[hdr_mem] - mov [ebx+APPDATA.mem_size],eax + mov ebx,[slot_base] + mov [ebx+APPDATA.dir_table],eax + mov eax,[hdr_mem] + mov [ebx+APPDATA.mem_size],eax if GREEDY_KERNEL else - mov ecx, [hdr_mem] - mov edi, [file_size] - add edi, 4095 - and edi, not 4095 - sub ecx, edi - jna @F + mov ecx, [hdr_mem] + mov edi, [file_size] + add edi, 4095 + and edi, not 4095 + sub ecx, edi + jna @F - xor eax, eax - cld - rep stosb + xor eax, eax + cld + rep stosb @@: end if ; release only virtual space, not phisical memory - stdcall free_kernel_space, [file_base] - lea eax, [hdr_cmdline] - lea ebx, [cmdline] - lea ecx, [filename] - stdcall set_app_params ,[slot],eax,ebx,ecx,[flags] + stdcall free_kernel_space, [file_base] ; + lea eax, [hdr_cmdline] + lea ebx, [cmdline] + lea ecx, [filename] + stdcall set_app_params ,[slot],eax,ebx,ecx,[flags] - mov eax, [save_cr3] - call set_cr3 + mov eax, [save_cr3] + call set_cr3 - xor ebx, ebx - mov [application_table_status],ebx ;unlock application_table_status mutex - mov eax,[process_number] ;set result - ret + xor ebx, ebx + mov [application_table_status],ebx ;unlock application_table_status mutex + mov eax,[process_number] ;set result + ret .failed: - mov eax, [save_cr3] - call set_cr3 + mov eax, [save_cr3] + call set_cr3 .err: .err_hdr: - stdcall kernel_free,[file_base] + stdcall kernel_free,[file_base] .err_file: - xor eax, eax - mov [application_table_status],eax - mov eax, ecx - ret + xor eax, eax + mov [application_table_status],eax + mov eax, ecx + ret endp align 4 @@ -341,39 +340,39 @@ endp align 4 proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword - locals - app_pages dd ? - img_pages dd ? - dir_addr dd ? - app_tabs dd ? - endl + locals + app_pages dd ? + img_pages dd ? + dir_addr dd ? + app_tabs dd ? + endl - mov ebx, pg_data.pg_mutex - call wait_mutex ;ebx + mov ebx, pg_data.pg_mutex + call wait_mutex ;ebx - xor eax, eax - mov [dir_addr], eax + xor eax, eax + mov [dir_addr], eax - mov eax, [app_size] - add eax, 4095 + mov eax, [app_size] + add eax, 4095 and eax, not 4095 - mov [app_size], eax - mov ebx, eax - shr eax, 12 - mov [app_pages], eax + mov [app_size], eax + mov ebx, eax + shr eax, 12 + mov [app_pages], eax - add ebx, 0x3FFFFF + add ebx, 0x3FFFFF and ebx, not 0x3FFFFF - shr ebx, 22 - mov [app_tabs], ebx + shr ebx, 22 + mov [app_tabs], ebx - mov ecx, [img_size] - add ecx, 4095 + mov ecx, [img_size] + add ecx, 4095 and ecx, not 4095 - mov [img_size], ecx - shr ecx, 12 - mov [img_pages], ecx + mov [img_size], ecx + shr ecx, 12 + mov [img_pages], ecx ; if GREEDY_KERNEL ; lea eax, [ecx+ebx+2] ;only image size @@ -384,9 +383,9 @@ proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword ; ja .fail call _alloc_page - test eax, eax - mov [dir_addr], eax - jz .fail + test eax, eax + mov [dir_addr], eax + jz .fail ;lea edi, [eax + OS_BASE] ;mov ecx, (OS_BASE shr 20)/4 @@ -413,15 +412,14 @@ proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword mov [edi+OS_BASE+(page_tabs shr 20)], eax mov eax, edi - call set_cr3 - + call set_cr3 mov edx, [app_tabs] mov edi, master_tab @@: call _alloc_page - test eax, eax - jz .fail + test eax, eax + jz .fail or eax, PG_UW stosd @@ -430,155 +428,159 @@ proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword mov edi, page_tabs - mov ecx, [app_tabs] - shl ecx, 10 - xor eax, eax - rep stosd + mov ecx, [app_tabs] + shl ecx, 10 + xor eax, eax + rep stosd - mov ecx, [img_pages] - mov ebx, PG_UW - mov esi, [img_base] - shr esi, 10 - add esi, page_tabs + mov ecx, [img_pages] + mov ebx, PG_UW + mov esi, [img_base] + shr esi, 10 + add esi, page_tabs xor edx, edx mov edi, page_tabs .remap: - lodsd - or eax, ebx ; force user level r/w access - stosd - add edx, 0x1000 - dec [app_pages] - dec ecx - jnz .remap + lodsd + or eax, ebx ; force user level r/w access + stosd + add edx, 0x1000 + dec [app_pages] + dec ecx + jnz .remap - mov ecx, [app_pages] - test ecx, ecx - jz .done + mov ecx, [app_pages] + test ecx, ecx + jz .done if GREEDY_KERNEL - mov eax, 0x02 + mov eax, 0x02 rep stosd else .alloc: call _alloc_page - test eax, eax - jz .fail + test eax, eax + jz .fail - stdcall map_page,edx,eax,dword PG_UW - add edx, 0x1000 - dec [app_pages] - jnz .alloc + stdcall map_page,edx,eax,dword PG_UW + add edx, 0x1000 + dec [app_pages] + jnz .alloc end if .done: dec [pg_data.pg_mutex] - mov eax, [dir_addr] - ret + mov eax, [dir_addr] + ret .fail: - dec [pg_data.pg_mutex] - cmp [dir_addr], 0 - je @f - stdcall destroy_app_space, [dir_addr] + dec [pg_data.pg_mutex] + cmp [dir_addr], 0 + je @f + stdcall destroy_app_space, [dir_addr] @@: - xor eax, eax - ret + xor eax, eax + ret endp align 4 set_cr3: - mov ebx, [current_slot] - mov [ebx+APPDATA.dir_table], eax - mov cr3, eax - ret + mov ebx, [current_slot] + mov [ebx+APPDATA.dir_table], eax + mov cr3, eax + ret align 4 proc destroy_page_table stdcall, pg_tab:dword - push esi + push esi - mov esi, [pg_tab] - mov ecx, 1024 + mov esi, [pg_tab] + mov ecx, 1024 .free: - mov eax, [esi] - test eax, 1 - jz .next + mov eax, [esi] + test eax, 1 + jz .next + test eax, 1 shl 9 jnz .next ;skip shared pages - call free_page + + call free_page .next: - add esi, 4 - dec ecx - jnz .free - pop esi - ret + add esi, 4 + dec ecx + jnz .free + pop esi + ret endp align 4 proc destroy_app_space stdcall, pg_dir:dword - mov ebx, pg_data.pg_mutex - call wait_mutex ;ebx + mov ebx, pg_data.pg_mutex + call wait_mutex ;ebx - xor edx,edx - mov eax,0x2 - mov ebx, [pg_dir] + xor edx,edx + mov eax,0x2 + mov ebx, [pg_dir] .loop: ;eax = current slot of process - mov ecx,eax - shl ecx,5 - cmp byte [CURRENT_TASK+ecx+0xa],9 ;if process running? - jz @f ;skip empty slots - shl ecx,3 - cmp [SLOT_BASE+ecx+0xB8],ebx ;compare page directory addresses - jnz @f - inc edx ;thread found + mov ecx,eax + shl ecx,5 + cmp byte [CURRENT_TASK+ecx+0xa],9 ;if process running? + jz @f ;skip empty slots + + shl ecx,3 + cmp [SLOT_BASE+ecx+0xB8],ebx ;compare page directory addresses + jnz @f + + inc edx ;thread found @@: - inc eax - cmp eax,[TASK_COUNT] ;exit loop if we look through all processes - jle .loop + inc eax + cmp eax,[TASK_COUNT] ;exit loop if we look through all processes + jle .loop ;edx = number of threads ;our process is zombi so it isn't counted - cmp edx,1 - jg .exit + cmp edx,1 + jg .exit ;if there isn't threads then clear memory. - mov eax, [pg_dir] + mov eax, [pg_dir] and eax, -4096 add eax, OS_BASE mov [tmp_task_pdir], eax mov esi, eax mov edi, (HEAP_BASE shr 20)/4 .destroy: - mov eax, [esi] - test eax, 1 - jz .next - and eax, not 0xFFF + mov eax, [esi] + test eax, 1 + jz .next + and eax, not 0xFFF add eax, OS_BASE stdcall destroy_page_table, eax mov eax, [esi] - call free_page + call free_page .next: - add esi, 4 - dec edi - jnz .destroy + add esi, 4 + dec edi + jnz .destroy - mov eax, [pg_dir] - call free_page + mov eax, [pg_dir] + call free_page .exit: - dec [pg_data.pg_mutex] - ret + dec [pg_data.pg_mutex] + ret endp align 4 get_pid: - mov eax, [TASK_BASE] + mov eax, [TASK_BASE] mov eax, [eax+TASKDATA.pid] - ret + ret pid_to_slot: ;Input: @@ -834,90 +836,91 @@ endp align 4 proc new_sys_threads - locals - slot dd ? - app_cmdline dd ? ;0x00 - app_path dd ? ;0x04 - app_eip dd ? ;0x08 - app_esp dd ? ;0x0C - app_mem dd ? ;0x10 - endl + locals + slot dd ? + app_cmdline dd ? ;0x00 + app_path dd ? ;0x04 + app_eip dd ? ;0x08 + app_esp dd ? ;0x0C + app_mem dd ? ;0x10 + endl - cmp eax,1 - jne .failed ;other subfunctions + cmp eax,1 + jne .failed ;other subfunctions - xor eax,eax - mov [app_cmdline], eax - mov [app_path], eax - mov [app_eip], ebx - mov [app_esp], ecx + xor eax,eax + mov [app_cmdline], eax + mov [app_path], eax + mov [app_eip], ebx + mov [app_esp], ecx - ;mov esi,new_process_loading - ;call sys_msg_board_str - DEBUGF 1,"%s",new_process_loading + ;mov esi,new_process_loading + ;call sys_msg_board_str + DEBUGF 1,"%s",new_process_loading .wait_lock: - cmp [application_table_status],0 - je .get_lock - call change_task - jmp .wait_lock + cmp [application_table_status],0 + je .get_lock + + call change_task + jmp .wait_lock .get_lock: - mov eax, 1 - xchg eax, [application_table_status] - cmp eax, 0 - jne .wait_lock + mov eax, 1 + xchg eax, [application_table_status] + cmp eax, 0 + jne .wait_lock - call set_application_table_status + call set_application_table_status - call get_new_process_place - test eax, eax - jz .failed + call get_new_process_place + test eax, eax + jz .failed - mov [slot], eax + mov [slot], eax - mov esi,[current_slot] - mov ebx,esi ;ebx=esi - pointer to extended information about current thread + mov esi,[current_slot] + mov ebx,esi ;ebx=esi - pointer to extended information about current thread - mov edi, eax - shl edi,8 - add edi,SLOT_BASE - mov edx,edi ;edx=edi - pointer to extended infomation about new thread - mov ecx,256/4 - xor eax, eax - cld - rep stosd ;clean extended information about new thread - mov esi,ebx - mov edi,edx - mov ecx,11 - rep movsb ;copy process name + mov edi, eax + shl edi,8 + add edi,SLOT_BASE + mov edx,edi ;edx=edi - pointer to extended infomation about new thread + mov ecx,256/4 + xor eax, eax + cld + rep stosd ;clean extended information about new thread + mov esi,ebx + mov edi,edx + mov ecx,11 + rep movsb ;copy process name - mov eax,[ebx+APPDATA.heap_base] - mov [edx+APPDATA.heap_base], eax + mov eax,[ebx+APPDATA.heap_base] + mov [edx+APPDATA.heap_base], eax - mov ecx,[ebx+APPDATA.heap_top] - mov [edx+APPDATA.heap_top], ecx + 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 eax,[ebx+APPDATA.mem_size] + mov [edx+APPDATA.mem_size], eax - mov ecx,[ebx+APPDATA.dir_table] - mov [edx+APPDATA.dir_table],ecx ;copy page directory + mov ecx,[ebx+APPDATA.dir_table] + mov [edx+APPDATA.dir_table],ecx ;copy page directory - lea eax, [app_cmdline] - stdcall set_app_params ,[slot],eax,dword 0,\ - dword 0,dword 0 + lea eax, [app_cmdline] + stdcall set_app_params ,[slot],eax,dword 0,\ + dword 0,dword 0 - ;mov esi,new_process_running - ;call sys_msg_board_str ;output information about succefull startup - DEBUGF 1,"%s",new_process_running + ;mov esi,new_process_running + ;call sys_msg_board_str ;output information about succefull startup + DEBUGF 1,"%s",new_process_running - mov [application_table_status],0 ;unlock application_table_status mutex - mov eax,[process_number] ;set result - ret + mov [application_table_status],0 ;unlock application_table_status mutex + mov eax,[process_number] ;set result + ret .failed: - mov [application_table_status],0 - mov eax,-1 - ret + mov [application_table_status],0 + mov eax,-1 + ret endp ; param @@ -925,21 +928,23 @@ endp align 4 wait_mutex: - push eax - push ebx + push eax + push ebx .do_wait: - cmp dword [ebx],0 - je .get_lock - call change_task - jmp .do_wait + cmp dword [ebx],0 + je .get_lock + + call change_task + jmp .do_wait .get_lock: - mov eax, 1 - xchg eax, [ebx] - test eax, eax - jnz .do_wait - pop ebx - pop eax - ret + mov eax, 1 + xchg eax, [ebx] + test eax, eax + jnz .do_wait + + pop ebx + pop eax + ret EFL_IF equ 0x0200 EFL_IOPL1 equ 0x1000 @@ -951,165 +956,164 @@ align 4 proc set_app_params stdcall,slot:dword, params:dword,\ cmd_line:dword, app_path:dword, flags:dword - locals - pl0_stack dd ? - endl + locals + pl0_stack dd ? + endl mov ecx, 1 ;(RING0_STACK_SIZE+512) shr 12 call @core_alloc@4 add eax, OS_BASE - mov [pl0_stack], eax + mov [pl0_stack], eax - lea edi, [eax+RING0_STACK_SIZE] + lea edi, [eax+RING0_STACK_SIZE] - mov eax, [slot] - mov ebx, eax + mov eax, [slot] + mov ebx, eax - shl eax, 8 - mov [eax+SLOT_BASE+APPDATA.fpu_state], edi - mov [eax+SLOT_BASE+APPDATA.fpu_handler], 0 - mov [eax+SLOT_BASE+APPDATA.sse_handler], 0 + shl eax, 8 + mov [eax+SLOT_BASE+APPDATA.fpu_state], edi + mov [eax+SLOT_BASE+APPDATA.fpu_handler], 0 + mov [eax+SLOT_BASE+APPDATA.sse_handler], 0 ;set default io permission map - mov [eax+SLOT_BASE+APPDATA.io_map],\ - (tss._io_map_0-OS_BASE+PG_MAP) - mov [eax+SLOT_BASE+APPDATA.io_map+4],\ - (tss._io_map_1-OS_BASE+PG_MAP) + mov [eax+SLOT_BASE+APPDATA.io_map],\ + (tss._io_map_0-OS_BASE+PG_MAP) + mov [eax+SLOT_BASE+APPDATA.io_map+4],\ + (tss._io_map_1-OS_BASE+PG_MAP) - mov esi, fpu_data - mov ecx, 512/4 - rep movsd + mov esi, fpu_data + mov ecx, 512/4 + rep movsd - cmp ebx,[TASK_COUNT] - jle .noinc - inc dword [TASK_COUNT] ;update number of processes + cmp ebx,[TASK_COUNT] + jle .noinc + inc dword [TASK_COUNT] ;update number of processes .noinc: - shl ebx,8 - lea edx, [ebx+SLOT_BASE+APP_EV_OFFSET] - mov [SLOT_BASE+APPDATA.fd_ev+ebx],edx - mov [SLOT_BASE+APPDATA.bk_ev+ebx],edx + shl ebx,8 + lea edx, [ebx+SLOT_BASE+APP_EV_OFFSET] + mov [SLOT_BASE+APPDATA.fd_ev+ebx],edx + mov [SLOT_BASE+APPDATA.bk_ev+ebx],edx - add edx, APP_OBJ_OFFSET-APP_EV_OFFSET - mov [SLOT_BASE+APPDATA.fd_obj+ebx],edx - mov [SLOT_BASE+APPDATA.bk_obj+ebx],edx + add edx, APP_OBJ_OFFSET-APP_EV_OFFSET + mov [SLOT_BASE+APPDATA.fd_obj+ebx],edx + mov [SLOT_BASE+APPDATA.bk_obj+ebx],edx - mov ecx, [def_cursor] - mov [SLOT_BASE+APPDATA.cursor+ebx],ecx - mov eax, [pl0_stack] - mov [SLOT_BASE+APPDATA.pl0_stack+ebx],eax - add eax, RING0_STACK_SIZE - mov [SLOT_BASE+APPDATA.saved_esp0+ebx], eax + mov ecx, [def_cursor] + mov [SLOT_BASE+APPDATA.cursor+ebx],ecx + mov eax, [pl0_stack] + mov [SLOT_BASE+APPDATA.pl0_stack+ebx],eax + add eax, RING0_STACK_SIZE + mov [SLOT_BASE+APPDATA.saved_esp0+ebx], eax call _alloc_page add eax, OS_BASE - mov esi,[current_slot] - mov esi,[esi+APPDATA.cur_dir] - mov ecx,0x1000/4 - mov edi,eax - mov [ebx+SLOT_BASE+APPDATA.cur_dir],eax - rep movsd + mov esi,[current_slot] + mov esi,[esi+APPDATA.cur_dir] + mov ecx,0x1000/4 + mov edi,eax + mov [ebx+SLOT_BASE+APPDATA.cur_dir],eax + rep movsd - shr ebx,3 + shr ebx,3 mov dword [CURRENT_TASK+ebx+0x10], 0 .add_command_line: - mov edx,[params] - mov edx,[edx] ;app_cmdline - test edx,edx - jz @f ;application doesn't need parameters + mov edx,[params] + mov edx,[edx] ;app_cmdline + test edx,edx + jz @f ;application doesn't need parameters - mov eax, edx - add eax, 256 - jc @f + mov eax, edx + add eax, 256 + jc @f - cmp eax, [SLOT_BASE+APPDATA.mem_size+ebx*8] - ja @f + cmp eax, [SLOT_BASE+APPDATA.mem_size+ebx*8] + ja @f - mov byte [edx], 0 ;force empty string if no cmdline given - mov eax, [cmd_line] - test eax, eax - jz @f - stdcall strncpy, edx, eax, 256 + mov byte [edx], 0 ;force empty string if no cmdline given + mov eax, [cmd_line] + test eax, eax + jz @f + stdcall strncpy, edx, eax, 256 @@: - mov edx,[params] - mov edx, [edx+4] ;app_path - test edx,edx - jz @F ;application don't need path of file - mov eax, edx - add eax, 1024 - jc @f - cmp eax, [SLOT_BASE+APPDATA.mem_size+ebx*8] - ja @f - stdcall strncpy, edx, [app_path], 1024 + mov edx,[params] + mov edx, [edx+4] ;app_path + test edx,edx + jz @F ;application don't need path of file + mov eax, edx + add eax, 1024 + jc @f + cmp eax, [SLOT_BASE+APPDATA.mem_size+ebx*8] + ja @f + stdcall strncpy, edx, [app_path], 1024 @@: - mov ebx,[slot] - mov eax,ebx - shl ebx,5 - lea ecx,[draw_data+ebx] ;ecx - pointer to draw data + mov ebx,[slot] + mov eax,ebx + shl ebx,5 + lea ecx,[draw_data+ebx] ;ecx - pointer to draw data ; set window state to 'normal' (non-minimized/maximized/rolled-up) state - mov [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL - mov [ebx+window_data+WDATA.fl_redraw], 1 - add ebx,CURRENT_TASK ;ebx - pointer to information about process - mov [ebx+TASKDATA.wnd_number],al;set window number on screen = process slot + mov [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL + mov [ebx+window_data+WDATA.fl_redraw], 1 + add ebx,CURRENT_TASK ;ebx - pointer to information about process + mov [ebx+TASKDATA.wnd_number],al;set window number on screen = process slot - mov [ebx+TASKDATA.event_mask],dword 1+2+4 ;set default event flags (see 40 function) + mov [ebx+TASKDATA.event_mask],dword 1+2+4 ;set default event flags (see 40 function) - inc dword [process_number] - mov eax,[process_number] - mov [ebx+4],eax ;set PID + inc dword [process_number] + mov eax,[process_number] + mov [ebx+4],eax ;set PID ;set draw data to full screen - mov [ecx+0],dword 0 - mov [ecx+4],dword 0 - mov eax,[Screen_Max_X] - mov [ecx+8],eax - mov eax,[Screen_Max_Y] - mov [ecx+12],eax + mov [ecx+0],dword 0 + mov [ecx+4],dword 0 + mov eax,[Screen_Max_X] + mov [ecx+8],eax + mov eax,[Screen_Max_Y] + mov [ecx+12],eax - mov ebx, [pl0_stack] - mov esi,[params] - lea ecx, [ebx+REG_EIP] - xor eax, eax + mov ebx, [pl0_stack] + mov esi,[params] + lea ecx, [ebx+REG_EIP] + xor eax, eax - mov [ebx+REG_RET], dword irq0.return - mov [ebx+REG_EDI], eax - mov [ebx+REG_ESI], eax - mov [ebx+REG_EBP], eax - mov [ebx+REG_ESP], ecx ;ebx+REG_EIP - mov [ebx+REG_EBX], eax - mov [ebx+REG_EDX], eax - mov [ebx+REG_ECX], eax - mov [ebx+REG_EAX], eax + mov [ebx+REG_RET], dword irq0.return + mov [ebx+REG_EDI], eax + mov [ebx+REG_ESI], eax + mov [ebx+REG_EBP], eax + mov [ebx+REG_ESP], ecx ;ebx+REG_EIP + mov [ebx+REG_EBX], eax + mov [ebx+REG_EDX], eax + mov [ebx+REG_ECX], eax + mov [ebx+REG_EAX], eax - mov eax, [esi+0x08] ;app_eip - mov [ebx+REG_EIP], eax ;app_entry - mov [ebx+REG_CS], dword app_code - mov [ebx+REG_EFLAGS], dword EFL_IOPL3+EFL_IF + mov eax, [esi+0x08] ;app_eip + mov [ebx+REG_EIP], eax ;app_entry + mov [ebx+REG_CS], dword app_code + mov [ebx+REG_EFLAGS], dword EFL_IOPL3+EFL_IF - mov eax, [esi+0x0C] ;app_esp - mov [ebx+REG_APP_ESP], eax ;app_stack - mov [ebx+REG_SS], dword app_data + mov eax, [esi+0x0C] ;app_esp + mov [ebx+REG_APP_ESP], eax ;app_stack + mov [ebx+REG_SS], dword app_data - lea ecx, [ebx+REG_RET] - mov ebx, [slot] - shl ebx, 5 - mov [ebx*8+SLOT_BASE+APPDATA.saved_esp], ecx + lea ecx, [ebx+REG_RET] + mov ebx, [slot] + shl ebx, 5 + mov [ebx*8+SLOT_BASE+APPDATA.saved_esp], ecx - xor ecx, ecx ; process state - running + xor ecx, ecx ; process state - running ; set if debuggee - test byte [flags], 1 - jz .no_debug - inc ecx ; process state - suspended - mov eax,[CURRENT_TASK] - mov [SLOT_BASE+ebx*8+APPDATA.debugger_slot],eax + test byte [flags], 1 + jz .no_debug + + inc ecx ; process state - suspended + mov eax,[CURRENT_TASK] + mov [SLOT_BASE+ebx*8+APPDATA.debugger_slot],eax .no_debug: - mov [CURRENT_TASK+ebx+TASKDATA.state], cl - ;mov esi,new_process_running - ;call sys_msg_board_str ;output information about succefull startup - DEBUGF 1,"%s",new_process_running - ret + mov [CURRENT_TASK+ebx+TASKDATA.state], cl + DEBUGF 1,"%s",new_process_running + ret endp include "debug.inc" diff --git a/kernel/branches/kolibri_pe/include/atomic.h b/kernel/branches/kolibri_pe/include/atomic.h index d271a85caa..65d6097077 100644 --- a/kernel/branches/kolibri_pe/include/atomic.h +++ b/kernel/branches/kolibri_pe/include/atomic.h @@ -36,17 +36,17 @@ typedef struct atomic { static inline void atomic_inc(atomic_t *val) { #ifdef USE_SMP - asm volatile ("lock inc %0\n" : "+m" (val->count)); + asm volatile ("lock incl %0\n" : "+m" (val->count)); #else - asm volatile ("inc %0\n" : "+m" (val->count)); + asm volatile ("incl %0\n" : "+m" (val->count)); #endif /* USE_SMP */ } static inline void atomic_dec(atomic_t *val) { #ifdef USE_SMP - asm volatile ("lock dec %0\n" : "+m" (val->count)); + asm volatile ("lock decl %0\n" : "+m" (val->count)); #else - asm volatile ("dec %0\n" : "+m" (val->count)); + asm volatile ("decl %0\n" : "+m" (val->count)); #endif /* USE_SMP */ } @@ -98,22 +98,20 @@ static inline void atomic_lock_arch(atomic_t *val) u32_t tmp; // preemption_disable(); - asm volatile ( - "0:\n" - "pause\n\t" /* Pentium 4's HT love this instruction */ - "mov %1, [%0]\n\t" - "test %1, %1\n\t" - "jnz 0b\n\t" /* lightweight looping on locked spinlock */ - "inc %1\n\t" /* now use the atomic operation */ - "xchg [%0], %1\n\t" - "test %1, %1\n\t" - "jnz 0b\n\t" - : "+m" (val->count), "=r"(tmp) - ); - /* - * Prevent critical section code from bleeding out this way up. - */ + asm volatile ( + "0:\n" + "pause\n" /* Pentium 4's HT love this instruction */ + "mov %0, %1\n" + "testl %1, %1\n" + "jnz 0b\n" /* lightweight looping on locked spinlock */ + + "incl %1\n" /* now use the atomic operation */ + "xchgl %0, %1\n" + "testl %1, %1\n" + "jnz 0b\n" + : "+m" (val->count), "=&r"(tmp) + ); // CS_ENTER_BARRIER(); } diff --git a/kernel/branches/kolibri_pe/include/core.h b/kernel/branches/kolibri_pe/include/core.h index b8c4832daf..1826e7bf17 100644 --- a/kernel/branches/kolibri_pe/include/core.h +++ b/kernel/branches/kolibri_pe/include/core.h @@ -37,8 +37,8 @@ static inline eflags_t safe_cli(void) { eflags_t tmp; asm volatile ( - "pushf\n\t" - "pop %0\n\t" + "pushfl\n\t" + "popl %0\n\t" "cli\n" : "=r" (tmp) ); @@ -48,8 +48,8 @@ static inline eflags_t safe_cli(void) static inline void safe_sti(eflags_t efl) { asm volatile ( - "push %0\n\t" - "popf\n" + "pushl %0\n\t" + "popfl\n" : : "r" (efl) ); } @@ -57,8 +57,8 @@ static inline void safe_sti(eflags_t efl) static inline count_t fnzb(u32_t arg) { count_t n; - asm volatile ("xor %0, %0 \n\t" - "bsr %0, %1" + asm volatile ("xorl %0, %0 \n\t" + "bsr %1, %0" :"=&r" (n) :"r"(arg) ); @@ -68,8 +68,8 @@ static inline count_t fnzb(u32_t arg) static inline count_t _bsf(u32_t arg) { count_t n; - asm volatile ("xor %0, %0 \n\t" - "bsf %0, %1" + asm volatile ("xorl %0, %0 \n\t" + "bsf %1, %0" :"=&r" (n) :"r"(arg) ); diff --git a/kernel/branches/kolibri_pe/include/slab.h b/kernel/branches/kolibri_pe/include/slab.h index b3e388618f..f1f36dcc22 100644 --- a/kernel/branches/kolibri_pe/include/slab.h +++ b/kernel/branches/kolibri_pe/include/slab.h @@ -78,3 +78,5 @@ slab_cache_t * slab_cache_create( int flags); void* __fastcall slab_alloc(slab_cache_t *cache, int flags); +void __fastcall slab_free(slab_cache_t *cache, void *obj); + diff --git a/kernel/branches/kolibri_pe/kernel.asm b/kernel/branches/kolibri_pe/kernel.asm index 79f8463dfb..fa171f4261 100644 --- a/kernel/branches/kolibri_pe/kernel.asm +++ b/kernel/branches/kolibri_pe/kernel.asm @@ -147,9 +147,9 @@ extrn @init_heap@8 extrn @find_large_md@4 extrn @find_small_md@4 extrn @phis_alloc@4 -extrn @mem_alloc@8 -extrn @heap_alloc@8 +extrn @mem_alloc@8 +extrn @mem_free@4 extrn _slab_cache_init extrn _alloc_page @@ -159,9 +159,6 @@ extrn _get_free_mem extrn _bx_from_load -@mem_alloc@8 equ @heap_alloc@8 - - section '.flat' code readable align 4096 use32 diff --git a/kernel/branches/kolibri_pe/makefile b/kernel/branches/kolibri_pe/makefile index 3313ab9733..e70ecd4548 100644 --- a/kernel/branches/kolibri_pe/makefile +++ b/kernel/branches/kolibri_pe/makefile @@ -6,7 +6,7 @@ INCLUDE = include/ DEFS = -DUSE_SMP -DCONFIG_DEBUG -CFLAGS = -c -O2 -I $(INCLUDE) -fomit-frame-pointer -fno-builtin-printf -masm=intel +CFLAGS = -c -O2 -I $(INCLUDE) -fomit-frame-pointer -fno-builtin-printf LDFLAGS = -shared -s -Map kernel.map --image-base 0x100000 --file-alignment 32 KERNEL_SRC:= \