diff --git a/kernel/trunk/const.inc b/kernel/trunk/const.inc index 4fda41ed53..05cd449b1e 100644 --- a/kernel/trunk/const.inc +++ b/kernel/trunk/const.inc @@ -645,7 +645,7 @@ virtual at 0 end virtual struc MEM_STATE -{ .mutex rd 1 +{ .mutex MUTEX .smallmap rd 1 .treemap rd 1 .topsize rd 1 @@ -664,7 +664,7 @@ struc PG_DATA .kernel_pages dd ? .kernel_tables dd ? .sys_page_dir dd ? - .pg_mutex dd ? + .mutex MUTEX } ;struc LIB diff --git a/kernel/trunk/core/heap.inc b/kernel/trunk/core/heap.inc index 9f7021dc57..2d6aca2c0c 100644 --- a/kernel/trunk/core/heap.inc +++ b/kernel/trunk/core/heap.inc @@ -151,7 +151,8 @@ proc init_kernel_heap mov [mem_block_list+63*4], ebx mov byte [mem_block_map], 0xFC - and [heap_mutex], 0 + mov ecx, heap_mutex + call mutex_init mov [heap_blocks], 4095 mov [free_blocks], 4094 ret @@ -272,14 +273,14 @@ proc alloc_kernel_space stdcall, size:dword push esi push edi + mov ecx, heap_mutex + call mutex_lock + mov eax, [size] add eax, 4095 and eax, not 4095 mov [size], eax - mov ebx, heap_mutex - call wait_mutex ;ebx - cmp eax, [heap_free] ja .error @@ -355,10 +356,11 @@ proc alloc_kernel_space stdcall, size:dword mov [edx+list_bk], esi mov [esi+block_flags], USED_BLOCK - mov eax, [esi+block_base] mov ebx, [size] sub [heap_free], ebx - and [heap_mutex], 0 + mov ecx, heap_mutex + call mutex_unlock + mov eax, [esi+block_base] pop edi pop esi pop ebx @@ -378,17 +380,19 @@ proc alloc_kernel_space stdcall, size:dword mov [edx+list_bk], edi mov [edi+block_flags], USED_BLOCK - mov eax, [edi+block_base] mov ebx, [size] sub [heap_free], ebx - and [heap_mutex], 0 + mov ecx, heap_mutex + call mutex_unlock + mov eax, [edi+block_base] pop edi pop esi pop ebx ret .error: + mov ecx, heap_mutex + call mutex_unlock xor eax, eax - mov [heap_mutex], eax pop edi pop esi pop ebx @@ -400,8 +404,9 @@ proc free_kernel_space stdcall uses ebx ecx edx esi edi, base:dword push ebx push esi push edi - mov ebx, heap_mutex - call wait_mutex ;ebx + + mov ecx, heap_mutex + call mutex_lock mov eax, [base] mov esi, [mem_used.fd] @@ -491,9 +496,10 @@ proc free_kernel_space stdcall uses ebx ecx edx esi edi, base:dword @@: bts [mem_block_mask], eax .m_eq: + mov ecx, heap_mutex + call mutex_unlock xor eax, eax - mov [heap_mutex], eax - dec eax + not eax pop edi pop esi pop ebx @@ -513,16 +519,18 @@ proc free_kernel_space stdcall uses ebx ecx edx esi edi, base:dword @@: bts [mem_block_mask], eax mov [esi+block_flags],FREE_BLOCK + mov ecx, heap_mutex + call mutex_unlock xor eax, eax - mov [heap_mutex], eax - dec eax + not eax pop edi pop esi pop ebx ret .fail: + mov ecx, heap_mutex + call mutex_unlock xor eax, eax - mov [heap_mutex], eax pop edi pop esi pop ebx @@ -607,8 +615,8 @@ align 4 proc kernel_free stdcall, base:dword push ebx esi - mov ebx, heap_mutex - call wait_mutex ;ebx + mov ecx, heap_mutex + call mutex_lock mov eax, [base] mov esi, [mem_used.fd] @@ -624,19 +632,17 @@ proc kernel_free stdcall, base:dword cmp [esi+block_flags], USED_BLOCK jne .fail - and [heap_mutex], 0 + call mutex_unlock - push ecx mov ecx, [esi+block_size]; shr ecx, 12 call release_pages ;eax, ecx - pop ecx stdcall free_kernel_space, [base] pop esi ebx ret .fail: + call mutex_unlock xor eax, eax - mov [heap_mutex], eax pop esi ebx ret endp diff --git a/kernel/trunk/core/irq.inc b/kernel/trunk/core/irq.inc index cb9941becb..241314465b 100644 --- a/kernel/trunk/core/irq.inc +++ b/kernel/trunk/core/irq.inc @@ -72,8 +72,6 @@ proc attach_int_handler stdcall, irq:dword, handler:dword, user_data:dword .irqh dd ? endl - xchg bx, bx - and [.irqh], 0 push ebx @@ -100,17 +98,17 @@ proc attach_int_handler stdcall, irq:dword, handler:dword, user_data:dword mov eax, [ecx] mov [next_irqh], eax - mov [.irqh], ecx + mov [irq_failed+ebx*4], 0 ;clear counter + mov eax, [user_data] mov [ecx+IRQH.handler], edx mov [ecx+IRQH.data], eax lea edx, [irqh_tab+ebx*8] list_add_tail ecx, edx ;clobber eax - - stdcall enable_irq, [irq] + stdcall enable_irq, ebx .fail: popfd diff --git a/kernel/trunk/core/malloc.inc b/kernel/trunk/core/malloc.inc index edf3ee53eb..ad7f603ff4 100644 --- a/kernel/trunk/core/malloc.inc +++ b/kernel/trunk/core/malloc.inc @@ -20,7 +20,7 @@ $Revision$ ; esi= nb ; ebx= idx ; -align 16 +align 4 malloc: push esi @@ -31,8 +31,8 @@ malloc: and esi, -8 add esi, 8 - mov ebx, mst.mutex - call wait_mutex ;ebx + mov ecx, mst.mutex + call mutex_lock cmp esi, 256 jae .large @@ -92,9 +92,13 @@ malloc: pop edi pop ebp .done: + mov esi, eax + mov ecx, mst.mutex + call mutex_unlock + mov eax, esi pop esi - mov [mst.mutex], 0 ret + .split: lea ebx, [edx+8] ;ebx=mem @@ -133,10 +137,10 @@ malloc: mov [edx+12], eax ; F->bk = r; mov [eax+8], edx ; r->fd = F; mov [eax+12], ecx ; r->bk = B; + mov eax, ebx - pop esi - mov [mst.mutex], 0 - ret + jmp .done + .small: ; if (ms.treemap != 0 && (mem = malloc_small(nb)) != 0) @@ -150,9 +154,8 @@ malloc: call malloc_small test eax, eax jz .from_top - pop esi - and [mst.mutex], 0 - ret + jmp .done + .large: ; if (ms.treemap != 0 && (mem = malloc_large(nb)) != 0) @@ -189,18 +192,15 @@ malloc: mov [edx+4], eax mov [ecx+4], esi lea eax, [ecx+8] - pop esi - and [mst.mutex], 0 - ret + jmp .done + .fail: xor eax, eax - pop esi - and [mst.mutex], 0 - ret + jmp .done ; param ; eax= mem - +align 4 free: push edi mov edi, eax @@ -211,8 +211,8 @@ free: test byte [edi+4], 2 je .fail - mov ebx, mst.mutex - call wait_mutex ;ebx + mov ecx, mst.mutex + call mutex_lock ; psize = p->head & (~3); @@ -289,7 +289,10 @@ free: mov [mst.top], edi mov [edi+4], eax .fail2: - and [mst.mutex], 0 + mov esi, eax + mov ecx, mst.mutex + call mutex_unlock + mov eax, esi pop esi .fail: pop edi @@ -410,13 +413,15 @@ insert_chunk: mov [esi+8], edx ;P->fd = F mov [esi+12], eax ;P->bk = B pop esi - and [mst.mutex], 0 + mov ecx, mst.mutex + call mutex_unlock ret .large: mov ebx, eax call insert_large_chunk pop esi - and [mst.mutex], 0 + mov ecx, mst.mutex + call mutex_unlock ret @@ -591,21 +596,21 @@ unlink_large_chunk: btr [mst.treemap], ecx pop edi ret - + .l3: cmp [edi+16], edx jne @F mov [edi+16], eax jmp .l4 - + @@: mov [edi+20], eax - + .l4: test eax, eax je .done - + .l5: mov [eax+24], edi mov ecx, [edx+16] @@ -614,7 +619,7 @@ unlink_large_chunk: mov [eax+16], ecx mov [ecx+24], eax - + .l6: mov edx, [edx+20] test edx, edx @@ -622,7 +627,7 @@ unlink_large_chunk: mov [eax+20], edx mov [edx+24], eax - + .done: pop edi ret @@ -644,10 +649,10 @@ malloc_small: mov edi, [ecx+4] and edi, -4 sub edi, esi - + .loop: mov ebx, ecx - + .loop_1: ; while ((t = leftmost_child(t)) != 0) @@ -657,10 +662,10 @@ malloc_small: jz @F mov ecx, eax jmp .l1 - + @@: mov ecx, [ecx+20] - + .l1: test ecx, ecx jz .unlink @@ -711,7 +716,7 @@ malloc_small: lea eax, [ebx+8] ret - + .split: ; v->head = nb|PINUSE_BIT|CINUSE_BIT; @@ -744,7 +749,7 @@ malloc_small: pop ebp lea eax, [ebx+8] ret - + .large: lea eax, [ebx+8] push eax @@ -798,20 +803,20 @@ malloc_large: jne @F xor ecx, ecx jmp .l1 - + @@: mov edx, ecx shr edx, 1 mov ecx, 37 sub ecx, edx - + .l1: mov edx, ebx shl edx, cl ; rst = 0; mov [.rst], ebp - + .loop: ; trem = (t->head & ~INUSE_BITS) - nb; @@ -831,7 +836,7 @@ malloc_large: mov ebp, eax mov edi, ecx je .l2 - + @@: ; rt = t->child[1]; @@ -853,7 +858,7 @@ malloc_large: ; rst = rt; mov [.rst], ecx - + @@: ; if (t == 0) @@ -864,11 +869,11 @@ malloc_large: add edx, edx jmp .loop - + @@: ; t = rst; mov eax, [.rst] - + .l2: ; if (t == 0 && v == 0) @@ -877,7 +882,7 @@ malloc_large: test ebp, ebp jne .l7 mov ecx, [.idx] - + .l3: ; leftbits = (-1<head & ~INUSE_BITS) - nb; @@ -915,7 +920,7 @@ malloc_large: mov edi, ecx ; v = t; mov ebp, eax - + @@: ; t = leftmost_child(t); @@ -925,24 +930,24 @@ malloc_large: je @F mov eax, ecx jmp .l6 - + @@: mov eax, [eax+20] - + .l6: ; while (t != 0) test eax, eax jne .l4 - + .l5: ; if (v != 0) test ebp, ebp jz .done - + .l7: ; r = chunk_plus_offset((mchunkptr)v, nb); @@ -974,7 +979,7 @@ malloc_large: pop esi pop ebp ret - + .large: ; v->head = nb|PINUSE_BIT|CINUSE_BIT; @@ -1000,7 +1005,7 @@ malloc_large: pop esi pop ebp ret - + .done: add esp, 8 pop edi @@ -1017,7 +1022,7 @@ init_malloc: mov [mst.topsize], 128*1024 mov dword [eax+4], (128*1024) or 1 mov eax, mst.smallbins - + @@: mov [eax+8], eax mov [eax+12], eax @@ -1025,5 +1030,8 @@ init_malloc: cmp eax, mst.smallbins+512 jb @B + mov ecx, mst.mutex + call mutex_init + ret diff --git a/kernel/trunk/core/memory.inc b/kernel/trunk/core/memory.inc index 79dc7406f8..01f9aedd4c 100644 --- a/kernel/trunk/core/memory.inc +++ b/kernel/trunk/core/memory.inc @@ -214,30 +214,32 @@ endp align 4 commit_pages: - push edi test ecx, ecx jz .fail - mov edi, ebx - mov ebx, pg_data.pg_mutex - call wait_mutex ;ebx + push edi + push eax + push ecx + mov ecx, pg_data.mutex + call mutex_lock + pop ecx + pop eax - mov edx, 0x1000 - mov ebx, edi - shr ebx, 12 + mov edi, ebx + shr edi, 12 + lea edi, [page_tabs+edi*4] @@: - mov [page_tabs+ebx*4], eax - ; push eax - invlpg [edi] - ; pop eax - add edi, edx - add eax, edx - inc ebx - dec ecx - jnz @B - mov [pg_data.pg_mutex],ecx -.fail: + stosd + invlpg [ebx] + add eax, 0x1000 + add ebx, 0x1000 + loop @B + pop edi + + mov ecx, pg_data.mutex + call mutex_unlock +.fail: ret @@ -248,15 +250,21 @@ commit_pages: align 4 release_pages: - pushad - mov ebx, pg_data.pg_mutex - call wait_mutex ;ebx + push ebp + push esi + push edi + push ebx mov esi, eax mov edi, eax - shr esi, 10 - add esi, page_tabs + shr esi, 12 + lea esi, [page_tabs+esi*4] + + push ecx + mov ecx, pg_data.mutex + call mutex_lock + pop ecx mov ebp, [pg_data.pages_free] mov ebx, [page_start] @@ -264,9 +272,7 @@ release_pages: @@: xor eax, eax xchg eax, [esi] - push eax invlpg [edi] - pop eax test eax, 1 jz .next @@ -285,11 +291,16 @@ release_pages: .next: add edi, 0x1000 add esi, 4 - dec ecx - jnz @B + loop @B + mov [pg_data.pages_free], ebp - and [pg_data.pg_mutex],0 - popad + mov ecx, pg_data.mutex + call mutex_unlock + + pop ebx + pop edi + pop esi + pop ebp ret ; param @@ -423,8 +434,8 @@ endp align 4 proc new_mem_resize stdcall, new_size:dword - mov ebx, pg_data.pg_mutex - call wait_mutex ;ebx + mov ecx, pg_data.mutex + call mutex_lock mov edi, [new_size] add edi,4095 @@ -464,8 +475,10 @@ proc new_mem_resize stdcall, new_size:dword mov ebx, [new_size] call update_mem_size + mov ecx, pg_data.mutex + call mutex_unlock + xor eax, eax - dec [pg_data.pg_mutex] ret .expand: @@ -539,9 +552,11 @@ proc new_mem_resize stdcall, new_size:dword pop edi pop esi .exit: + mov ecx, pg_data.mutex + call mutex_unlock + xor eax, eax inc eax - dec [pg_data.pg_mutex] ret endp diff --git a/kernel/trunk/core/taskman.inc b/kernel/trunk/core/taskman.inc index 49ca4631f3..643424f1c3 100644 --- a/kernel/trunk/core/taskman.inc +++ b/kernel/trunk/core/taskman.inc @@ -360,8 +360,8 @@ proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword app_tabs dd ? endl - mov ebx, pg_data.pg_mutex - call wait_mutex ;ebx + mov ecx, pg_data.mutex + call mutex_lock xor eax, eax mov [dir_addr], eax @@ -387,11 +387,11 @@ proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword shr ecx, 12 mov [img_pages], ecx - if GREEDY_KERNEL +if GREEDY_KERNEL lea eax, [ecx+ebx+2] ;only image size - else +else lea eax, [eax+ebx+2] ;all requested memory - end if +end if cmp eax, [pg_data.pages_free] ja .fail @@ -480,11 +480,13 @@ end if .done: stdcall map_page,[tmp_task_pdir],dword 0,dword PG_UNMAP - dec [pg_data.pg_mutex] + mov ecx, pg_data.mutex + call mutex_unlock mov eax, [dir_addr] ret .fail: - dec [pg_data.pg_mutex] + mov ecx, pg_data.mutex + call mutex_unlock cmp [dir_addr], 0 je @f stdcall destroy_app_space, [dir_addr], 0 @@ -554,10 +556,10 @@ proc destroy_app_space stdcall, pg_dir:dword, dlls_list:dword jg .ret ;if there isn't threads then clear memory. mov esi, [dlls_list] - call destroy_all_hdlls + call destroy_all_hdlls ;ecx=APPDATA - mov ebx, pg_data.pg_mutex - call wait_mutex ;ebx + mov ecx, pg_data.mutex + call mutex_lock mov eax, [pg_dir] and eax, not 0xFFF @@ -583,7 +585,8 @@ proc destroy_app_space stdcall, pg_dir:dword, dlls_list:dword .exit: stdcall map_page,[tmp_task_ptab],0,PG_UNMAP stdcall map_page,[tmp_task_pdir],0,PG_UNMAP - dec [pg_data.pg_mutex] + mov ecx, pg_data.mutex + call mutex_unlock .ret: ret endp @@ -956,24 +959,6 @@ proc new_sys_threads ret endp -; param -; ebx=mutex - -align 4 -wait_mutex: -;;Maxis use atomic bts for mutex 4.4.2009 - push eax - push ebx -.do_wait: - bts dword [ebx],0 - jnc .locked - call change_task - jmp .do_wait -.locked: - pop ebx - pop eax - ret - align 4 tls_app_entry: diff --git a/kernel/trunk/data32.inc b/kernel/trunk/data32.inc index 0b177fd35c..c71374e25f 100644 --- a/kernel/trunk/data32.inc +++ b/kernel/trunk/data32.inc @@ -310,7 +310,7 @@ mem_block_arr rd 1 mem_block_start rd 1 mem_block_end rd 1 -heap_mutex rd 1 +heap_mutex MUTEX heap_size rd 1 heap_free rd 1 heap_blocks rd 1 diff --git a/kernel/trunk/fs/disk.inc b/kernel/trunk/fs/disk.inc index 4a57c7e9be..3dce6e3dc4 100644 --- a/kernel/trunk/fs/disk.inc +++ b/kernel/trunk/fs/disk.inc @@ -119,7 +119,7 @@ struct DISK ; The structure is destroyed when the reference count decrements to zero: ; this usually occurs in 'disk_del', but can be delayed to the end of last ; filesystem operation, if one is active. -.MediaLock dd ? +.MediaLock MUTEX ; Lock to protect the MEDIA structure. See the description after ; 'disk_list_mutex' for the locking strategy. ; Fields of media object @@ -150,7 +150,7 @@ ends ; This structure represents one partition for the kernel. This is a base ; template, the actual contents after common fields is determined by the -; file system code for this partition. +; file system code for this partition. struct PARTITION .FirstSector dq ? ; First sector of the partition. @@ -198,7 +198,7 @@ disk_list: endg uglobal ; This mutex guards all operations with the global list of DISK structures. -disk_list_mutex dd 0 +disk_list_mutex MUTEX ; * There are two dependent objects, a disk and a media. In the simplest case ; disk and media are both non-removable. However, in the general case both ; can be removed at any time, simultaneously or only media, this makes things @@ -345,27 +345,24 @@ disk_add: ; 4. Initialize other fields of the DISK structure. ; Media is not inserted, initialized state of mutex is zero, ; reference counter is 1. + lea ecx, [ebx+DISK.MediaLock] + call mutex_init xor eax, eax mov dword [ebx+DISK.MediaInserted], eax - mov [ebx+DISK.MediaLock], eax inc eax mov [ebx+DISK.RefCount], eax ; The DISK structure is initialized. ; 5. Insert the new structure to the global list. - xchg eax, ebx ; now eax = pointer to DISK ; 5a. Acquire the mutex. - mov ebx, disk_list_mutex - call wait_mutex + mov ecx, disk_list_mutex + call mutex_lock ; 5b. Insert item to the tail of double-linked list. mov edx, disk_list - mov ecx, [edx+DISK.Prev] - mov [eax+DISK.Prev], ecx - mov [eax+DISK.Next], edx - mov [edx+DISK.Prev], eax - mov [ecx+DISK.Next], eax + list_add_tail ebx, edx ;ebx= new edx= list head ; 5c. Release the mutex. - mov dword [ebx], 0 + call mutex_unlock ; 6. Return with eax = pointer to DISK. + xchg eax, ebx jmp .nothing .free: ; Memory allocation for DISK structure succeeded, but for disk name failed. @@ -388,28 +385,28 @@ disk_add: ; [esp+4] = handle of the disk, i.e. the pointer to the DISK structure. ; Return value: none. disk_del: - push ebx esi ; save used registers to be stdcall + push esi ; save used registers to be stdcall ; 1. Force media to be removed. If the media is already removed, the ; call does nothing. mov esi, [esp+4+8] ; esi = handle of the disk stdcall disk_media_changed, esi, 0 ; 2. Delete the structure from the global list. ; 2a. Acquire the mutex. - mov ebx, disk_list_mutex - call wait_mutex + mov ecx, disk_list_mutex + call mutex_lock ; 2b. Delete item from double-linked list. mov eax, [esi+DISK.Next] mov edx, [esi+DISK.Prev] mov [eax+DISK.Prev], edx mov [edx+DISK.Next], eax ; 2c. Release the mutex. - mov dword [ebx], 0 + call mutex_unlock ; 3. The structure still has one reference created in disk_add. Remove this ; reference. If there are no other references, disk_dereference will free the ; structure. call disk_dereference ; 4. Return. - pop esi ebx ; restore used registers to be stdcall + pop esi ; restore used registers to be stdcall ret 4 ; purge 1 dword argument to be stdcall ; This is an internal function which removes a previously obtained reference @@ -502,12 +499,12 @@ disk_media_changed: jz .noremove ; We really need to remove the media. ; 1b. Acquire mutex. - lea ebx, [esi+DISK.MediaLock] - call wait_mutex + lea ecx, [esi+DISK.MediaLock] + call mutex_lock ; 1c. Clear the flag. mov [esi+DISK.MediaInserted], 0 ; 1d. Release mutex. - mov dword [ebx], 0 + call mutex_unlock ; 1e. Remove the "lifetime" reference and possibly destroy the structure. call disk_media_dereference .noremove: @@ -968,19 +965,19 @@ end virtual dyndisk_handler: push ebx edi ; save registers used in file_system_lfn ; 1. Acquire the mutex. - mov ebx, disk_list_mutex - call wait_mutex + mov ecx, disk_list_mutex + call mutex_lock ; 2. Loop over the list of DISK structures. ; 2a. Initialize. - mov ecx, disk_list + mov ebx, disk_list .scan: ; 2b. Get the next item. - mov ecx, [ecx+DISK.Next] + mov ebx, [ebx+DISK.Next] ; 2c. Check whether the list is done. If so, go to 3. - cmp ecx, disk_list + cmp ebx, disk_list jz .notfound ; 2d. Compare names. If names match, go to 5. - mov edi, [ecx+DISK.Name] + mov edi, [ebx+DISK.Name] push esi @@: ; esi points to the name from fs operation; it is terminated by zero or slash. @@ -1005,7 +1002,7 @@ dyndisk_handler: .notfound: ; The loop is done and no name matches. ; 3. Release the mutex. - mov dword [ebx], 0 + call mutex_unlock ; 4. Return normally. pop edi ebx ; restore registers used in file_system_lfn ret @@ -1018,24 +1015,25 @@ dyndisk_handler: jnz .wrongname ; We found the addressed DISK structure. ; 5. Reference the disk. -lock inc [ecx+DISK.RefCount] +lock inc [ebx+DISK.RefCount] ; 6. Now we are sure that the DISK structure is not going to die at least ; while we are working with it, so release the global mutex. - mov dword [ebx], 0 + call mutex_unlock ; 7. Acquire the mutex for media object. pop edi ; restore edi - lea ebx, [ecx+DISK.MediaLock] - call wait_mutex + lea ecx, [ebx+DISK.MediaLock] + call mutex_lock ; 8. Get the media object. If it is not NULL, reference it. xor edx, edx - cmp [ecx+DISK.MediaInserted], dl + cmp [ebx+DISK.MediaInserted], dl jz @f - mov edx, ecx - inc [ecx+DISK.MediaRefCount] + mov edx, ebx + inc [ebx+DISK.MediaRefCount] @@: ; 9. Now we are sure that the media object, if it exists, is not going to die ; at least while we are working with it, so release the mutex for media object. - mov dword [ebx], 0 + call mutex_unlock + mov ecx, ebx pop ebx eax ; restore ebx, pop return address ; 10. Check whether the fs operation wants to enumerate partitions (go to 11) ; or work with some concrete partition (go to 12). @@ -1152,24 +1150,25 @@ fs_dyndisk: ; if the driver does not support insert notifications and we are the only fs ; operation with this disk, issue the fake insert notification; if media is ; still not inserted, 'disk_media_changed' will detect this and do nothing - push ebx - lea ebx, [edx+DISK.MediaLock] - call wait_mutex +;;; push ebx + lea ecx, [edx+DISK.MediaLock] + call mutex_lock cmp [edx+DISK.MediaRefCount], 1 jnz .noluck - mov dword [ebx], 0 + call mutex_unlock push edx stdcall disk_media_changed, edx, 1 pop edx - call wait_mutex + lea ecx, [edx+DISK.MediaLock] + call mutex_lock cmp [edx+DISK.MediaInserted], 0 jz .noluck lock inc [edx+DISK.MediaRefCount] - mov dword [ebx], 0 + call mutex_unlock xor ecx, ecx jmp .main .noluck: - mov dword [ebx], 0 + call mutex_unlock .deverror: mov dword [esp+32], ERROR_DEVICE mov esi, edx @@ -1185,11 +1184,11 @@ lock inc [edx+DISK.MediaRefCount] ; eax != 0 => buffer pointed to by edi contains name of item dyndisk_enum_root: push ebx ; save register used in file_system_lfn - mov ebx, disk_list_mutex ; it will be useful + mov ecx, disk_list_mutex ; it will be useful ; 1. If this is the first call, acquire the mutex and initialize. test eax, eax jnz .notfirst - call wait_mutex + call mutex_lock mov eax, disk_list .notfirst: ; 2. Get next item. @@ -1211,7 +1210,7 @@ dyndisk_enum_root: ret .last: ; 6. Release the mutex and return with eax = 0. + call mutex_unlock xor eax, eax - mov dword [ebx], eax pop ebx ; restore register used in file_system_lfn ret diff --git a/kernel/trunk/init.inc b/kernel/trunk/init.inc index 54ba0d6e40..bff3c0f049 100644 --- a/kernel/trunk/init.inc +++ b/kernel/trunk/init.inc @@ -141,7 +141,7 @@ proc init_mem mov [edx], eax add edx, 4 - mov edi, [tmp_page_tabs] + mov edi, [tmp_page_tabs] jmp .map_kernel_heap ; new kernel fits to the first 4Mb - nothing to do with ".map_low" .no_PSE: mov eax, PG_SW @@ -165,7 +165,7 @@ proc init_mem mov eax, [tmp_page_tabs] or eax, PG_SW mov edi, edx - + .map_kernel_tabs: stosd add eax, 0x1000 @@ -266,7 +266,6 @@ proc init_page_map add ebx, [pg_data.pagemap_size-OS_BASE] mov [page_end-OS_BASE], ebx - mov [pg_data.pg_mutex-OS_BASE], 0 ret endp @@ -331,7 +330,7 @@ init_BIOS32: ; здесь должна заполнятся pci_emu_dat .BIOS32_not_found: .end: - ret + ret align 4 proc test_cpu diff --git a/kernel/trunk/kernel.asm b/kernel/trunk/kernel.asm index 9a0dd7097c..8e6da88df3 100644 --- a/kernel/trunk/kernel.asm +++ b/kernel/trunk/kernel.asm @@ -321,6 +321,12 @@ high_code: mov eax, cr3 mov cr3, eax ; flush TLB + mov ecx, pg_data.mutex + call mutex_init + + mov ecx, disk_list_mutex + call mutex_init + ; SAVE REAL MODE VARIABLES mov ax, [BOOT_VAR + 0x9031] mov [IDEContrRegsBaseAddr], ax diff --git a/kernel/trunk/network/socket.inc b/kernel/trunk/network/socket.inc index 37e2b8b532..be2b689c28 100644 --- a/kernel/trunk/network/socket.inc +++ b/kernel/trunk/network/socket.inc @@ -54,7 +54,7 @@ struct SOCKET .SEG_LEN dd ? ; segment length .SEG_WND dd ? ; segment window .wndsizeTimer dd ? ; window size timer - .lock dd ? ; lock mutex + .lock MUTEX ; lock mutex .rxData dd ? ; receive data buffer here ends @@ -99,6 +99,11 @@ proc net_socket_alloc stdcall uses ebx ecx edx edi rep stosd pop eax + mov ebx, eax + lea ecx, [eax+SOCKET.lock] + call mutex_init + mov eax, ebx + ; add socket to the list by changing pointers mov ebx, net_sockets push [ebx + SOCKET.NextPtr] @@ -703,10 +708,10 @@ proc socket_read stdcall or eax, eax jz .error - lea ebx, [eax + SOCKET.lock] - call wait_mutex + mov ebx, eax + lea ecx, [eax + SOCKET.lock] + call mutex_lock - mov ebx, eax mov eax, [ebx + SOCKET.rxDataCount] ; get count of bytes test eax, eax jz .error_release @@ -727,15 +732,18 @@ proc socket_read stdcall and ecx, 3 rep movsb - mov [ebx + SOCKET.lock], 0 + lea ecx, [ebx + SOCKET.lock] mov ebx, eax - + call mutex_unlock + mov eax, ebx ret .error_release: - mov [ebx + SOCKET.lock], 0 + lea ecx, [ebx + SOCKET.lock] + call mutex_unlock .error: xor ebx, ebx + xor eax, eax ret endp @@ -756,10 +764,11 @@ proc socket_read_packet stdcall or eax, eax jz .error - lea ebx, [eax + SOCKET.lock] - call wait_mutex + mov ebx, eax + + lea ecx, [eax + SOCKET.lock] + call mutex_lock - mov ebx, eax mov eax, [ebx + SOCKET.rxDataCount] ; get count of bytes test eax, eax ; if count of bytes is zero.. jz .exit ; exit function (eax will be zero) @@ -789,8 +798,10 @@ proc socket_read_packet stdcall rep movsb ; copy remaining bytes .exit: - mov [ebx + SOCKET.lock], 0 - ret ; at last, exit + lea ecx, [ebx + SOCKET.lock] + call mutex_unlock + mov eax, edx + ret ; at last, exit .error: xor eax, eax @@ -800,7 +811,9 @@ proc socket_read_packet stdcall xor esi, esi mov [ebx + SOCKET.rxDataCount], esi ; store new count (zero) call .start_copy - mov [ebx + SOCKET.lock], 0 + lea ecx, [ebx + SOCKET.lock] + call mutex_unlock + mov eax, edx ret .start_copy: diff --git a/kernel/trunk/network/tcp.inc b/kernel/trunk/network/tcp.inc index ae01e2ba42..7a5983be90 100644 --- a/kernel/trunk/network/tcp.inc +++ b/kernel/trunk/network/tcp.inc @@ -697,7 +697,7 @@ endp ; ; Description ; Signals about network event to socket owner -; This is a kernel function, called from TCP handler +; This is a kernel function, called from TCP handler ; ; Socket/TCB address in ebx ;*************************************************************************** @@ -925,7 +925,7 @@ proc stateTCB_ESTABLISHED stdcall, sockAddr:DWORD test [edx + 20 + TCP_PACKET.Flags], TH_RST je @f mov [ebx + SOCKET.TCBState], TCB_CLOSED - @@: + @@: call signal_network_event lea esi, [ebx + SOCKET.RCV_NXT] mov eax, [esi] ; save original @@ -963,12 +963,10 @@ proc stateTCB_ESTABLISHED stdcall, sockAddr:DWORD jmp .exit .data: - push ebx - add ebx, SOCKET.lock - call wait_mutex - pop ebx + push ecx + lea ecx, [ebx+SOCKET.lock] + call mutex_lock - push ecx push ebx mov eax, [ebx + SOCKET.rxDataCount] add eax, ecx @@ -986,7 +984,9 @@ proc stateTCB_ESTABLISHED stdcall, sockAddr:DWORD cld rep movsb ; copy the data across - mov [ebx + SOCKET.lock], 0 ; release mutex + + lea ecx,[ebx + SOCKET.lock] + call mutex_unlock ; flag an event to the application pop ebx @@ -1031,9 +1031,10 @@ proc stateTCB_ESTABLISHED stdcall, sockAddr:DWORD .overflow: ; no place in buffer ; so simply restore stack and exit - pop eax ecx - mov [ebx + SOCKET.lock], 0 - ret + lea ecx, [ebx + SOCKET.lock] + call mutex_unlock + pop eax ecx + ret endp