2011-10-15 01:38:50 +04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2015-01-08 23:10:22 +03:00
|
|
|
;; Copyright (C) KolibriOS team 2011-2015. All rights reserved. ;;
|
2011-10-15 01:38:50 +04:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
$Revision$
|
|
|
|
|
2014-01-10 15:56:03 +04:00
|
|
|
; Read/write functions try to do large operations,
|
|
|
|
; it is significantly faster than several small operations.
|
|
|
|
; This requires large buffers.
|
|
|
|
; We can't use input/output buffers directly - they can be controlled
|
|
|
|
; by user-mode application, so they can be modified between the operation
|
|
|
|
; and copying to/from cache, giving invalid data in cache.
|
|
|
|
; It is unclear how to use cache directly, currently cache items are
|
|
|
|
; allocated/freed sector-wise, so items for sequential sectors can be
|
|
|
|
; scattered over all the cache.
|
|
|
|
; So read/write functions allocate a temporary buffer which is
|
|
|
|
; 1) not greater than half of free memory and
|
|
|
|
; 2) not greater than the following constant.
|
|
|
|
CACHE_MAX_ALLOC_SIZE = 4 shl 20
|
|
|
|
|
|
|
|
; Legacy interface for filesystems fs_{read,write}32_{sys,app}
|
|
|
|
; gives only one sector for FS. However, per-sector reading is inefficient,
|
|
|
|
; so internally fs_read32_{sys,app} reads to the cache several sequential
|
|
|
|
; sectors, hoping that they will be useful.
|
|
|
|
; Total number of sectors is given by the following constant.
|
|
|
|
CACHE_LEGACY_READ_SIZE = 16
|
|
|
|
|
|
|
|
; This structure describes one item in the cache.
|
|
|
|
struct CACHE_ITEM
|
|
|
|
SectorLo dd ? ; low 32 bits of sector
|
|
|
|
SectorHi dd ? ; high 32 bits of sector
|
|
|
|
Status dd ? ; one of CACHE_ITEM_*
|
|
|
|
ends
|
|
|
|
|
|
|
|
; Possible values for CACHE_ITEM_*
|
|
|
|
CACHE_ITEM_EMPTY = 0
|
|
|
|
CACHE_ITEM_COPY = 1
|
|
|
|
CACHE_ITEM_MODIFIED = 2
|
|
|
|
|
|
|
|
; Read several sequential sectors using cache #1.
|
|
|
|
; in: edx:eax = start sector, relative to start of partition
|
|
|
|
; in: ecx = number of sectors to read
|
|
|
|
; in: ebx -> buffer
|
|
|
|
; in: ebp -> PARTITION
|
|
|
|
; out: eax = error code, 0 = ok
|
|
|
|
; out: ecx = number of sectors that were read
|
|
|
|
fs_read64_sys:
|
|
|
|
; Save ebx, set ebx to SysCache and let the common part do its work.
|
2014-09-08 22:46:36 +04:00
|
|
|
push ebx ebx
|
2014-01-10 15:56:03 +04:00
|
|
|
mov ebx, [ebp+PARTITION.Disk]
|
|
|
|
add ebx, DISK.SysCache
|
|
|
|
jmp fs_read64_common
|
|
|
|
|
|
|
|
; Read several sequential sectors using cache #2.
|
|
|
|
; in: edx:eax = start sector, relative to start of partition
|
|
|
|
; in: ecx = number of sectors to read
|
|
|
|
; in: edi -> buffer
|
|
|
|
; in: ebp -> PARTITION
|
|
|
|
; out: eax = error code, 0 = ok
|
|
|
|
; out: ecx = number of sectors that were read
|
|
|
|
fs_read64_app:
|
|
|
|
; Save ebx, set ebx to AppCache and let the common part do its work.
|
2014-09-08 22:46:36 +04:00
|
|
|
push ebx ebx
|
2014-01-10 15:56:03 +04:00
|
|
|
mov ebx, [ebp+PARTITION.Disk]
|
|
|
|
add ebx, DISK.AppCache
|
|
|
|
|
|
|
|
; Common part of fs_read64_{app,sys}:
|
|
|
|
; read several sequential sectors using the given cache.
|
|
|
|
fs_read64_common:
|
|
|
|
; 1. Setup stack frame.
|
|
|
|
push esi edi ; save used registers to be stdcall
|
|
|
|
push 0 ; initialize .error_code
|
|
|
|
push ebx edx eax ecx ecx ; initialize stack variables
|
|
|
|
virtual at esp
|
|
|
|
.local_vars:
|
|
|
|
.num_sectors_orig dd ?
|
|
|
|
; Number of sectors that should be read. Used to generate output value of ecx.
|
|
|
|
.num_sectors dd ?
|
|
|
|
; Number of sectors that remain to be read. Decreases from .num_sectors_orig to 0.
|
|
|
|
.sector_lo dd ? ; low 32 bits of the current sector
|
|
|
|
.sector_hi dd ? ; high 32 bits of the current sector
|
|
|
|
.cache dd ? ; pointer to DISKCACHE
|
|
|
|
.error_code dd ? ; current status
|
|
|
|
.local_vars_size = $ - .local_vars
|
|
|
|
.saved_regs rd 2
|
|
|
|
.buffer dd ? ; filled by fs_read64_{sys,app}
|
|
|
|
end virtual
|
|
|
|
; 2. Validate parameters against partition length:
|
|
|
|
; immediately return error if edx:eax are beyond partition end,
|
|
|
|
; decrease .num_sectors and .num_sectors_orig, if needed,
|
|
|
|
; so that the entire operation fits in the partition limits.
|
|
|
|
mov eax, dword [ebp+PARTITION.Length]
|
|
|
|
mov edx, dword [ebp+PARTITION.Length+4]
|
|
|
|
sub eax, [.sector_lo]
|
|
|
|
sbb edx, [.sector_hi]
|
|
|
|
jb .end_of_media
|
|
|
|
jnz .no_end_of_media
|
|
|
|
cmp ecx, eax
|
|
|
|
jbe .no_end_of_media
|
|
|
|
; If .num_sectors got decreased, set status to DISK_STATUS_END_OF_MEDIA;
|
|
|
|
; if all subsequent operations would be successful, this would become the final
|
|
|
|
; status, otherwise this would be rewritten by failed operation.
|
|
|
|
mov [.num_sectors], eax
|
|
|
|
mov [.num_sectors_orig], eax
|
|
|
|
mov [.error_code], DISK_STATUS_END_OF_MEDIA
|
|
|
|
.no_end_of_media:
|
|
|
|
; 3. If number of sectors to read is zero, either because zero-sectors operation
|
|
|
|
; was requested or because it got decreased to zero due to partition limits,
|
|
|
|
; just return the current status.
|
|
|
|
cmp [.num_sectors], 0
|
|
|
|
jz .return
|
|
|
|
; 4. Shift sector from partition-relative to absolute.
|
|
|
|
mov eax, dword [ebp+PARTITION.FirstSector]
|
|
|
|
mov edx, dword [ebp+PARTITION.FirstSector+4]
|
|
|
|
add [.sector_lo], eax
|
|
|
|
adc [.sector_hi], edx
|
|
|
|
; 5. If the cache is disabled, pass the request directly to the driver.
|
|
|
|
cmp [ebx+DISKCACHE.pointer], 0
|
|
|
|
jz .nocache
|
|
|
|
; 6. Look for sectors in the cache, sequentially from the beginning.
|
|
|
|
; Stop at the first sector that is not in the cache
|
|
|
|
; or when all sectors were read from the cache.
|
|
|
|
; 6a. Acquire the lock.
|
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
|
|
|
call mutex_lock
|
|
|
|
.lookup_in_cache_loop:
|
|
|
|
; 6b. For each sector, call the lookup function without adding to the cache.
|
|
|
|
mov eax, [.sector_lo]
|
|
|
|
mov edx, [.sector_hi]
|
|
|
|
call cache_lookup_read
|
|
|
|
; 6c. If it has failed, the sector is not in cache;
|
|
|
|
; release the lock and go to 7.
|
|
|
|
jc .not_found_in_cache
|
|
|
|
; The sector is found in cache.
|
2014-09-03 16:11:19 +04:00
|
|
|
; 6d. Copy data for the caller, advance [.buffer].
|
|
|
|
mov esi, edi
|
|
|
|
mov edi, [.buffer]
|
|
|
|
mov eax, 1
|
|
|
|
shl eax, cl
|
|
|
|
mov ecx, eax
|
|
|
|
shr ecx, 2
|
2014-01-10 15:56:03 +04:00
|
|
|
rep movsd
|
2014-09-03 16:11:19 +04:00
|
|
|
mov [.buffer], edi
|
2014-01-10 15:56:03 +04:00
|
|
|
; 6e. Advance the sector.
|
|
|
|
add [.sector_lo], 1
|
|
|
|
adc [.sector_hi], 0
|
|
|
|
; 6f. Decrement number of sectors left.
|
|
|
|
; If all sectors were read, release the lock and return.
|
|
|
|
dec [.num_sectors]
|
|
|
|
jnz .lookup_in_cache_loop
|
|
|
|
; Release the lock acquired at 6a.
|
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
|
|
|
call mutex_unlock
|
|
|
|
.return:
|
|
|
|
mov eax, [.error_code]
|
|
|
|
mov ecx, [.num_sectors_orig]
|
|
|
|
sub ecx, [.num_sectors]
|
|
|
|
.nothing:
|
|
|
|
add esp, .local_vars_size
|
2014-09-08 22:46:36 +04:00
|
|
|
pop edi esi ebx ebx ; restore used registers to be stdcall
|
2014-01-10 15:56:03 +04:00
|
|
|
ret
|
|
|
|
.not_found_in_cache:
|
|
|
|
; Release the lock acquired at 6a.
|
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
|
|
|
call mutex_unlock
|
|
|
|
; The current sector is not present in the cache.
|
|
|
|
; Ask the driver to read all requested not-yet-read sectors,
|
|
|
|
; put results in the cache.
|
|
|
|
; Also, see the comment before the definition of CACHE_MAX_ALLOC_SIZE.
|
|
|
|
; 7. Allocate buffer for operations.
|
|
|
|
; Normally, create buffer that is sufficient for all remaining data.
|
|
|
|
; However, for extra-large requests make an upper limit:
|
|
|
|
; do not use more than half of the free memory
|
|
|
|
; or more than CACHE_MAX_ALLOC_SIZE bytes.
|
2014-09-03 16:11:19 +04:00
|
|
|
mov ecx, [ebx+DISKCACHE.sector_size_log]
|
2014-01-10 15:56:03 +04:00
|
|
|
mov ebx, [pg_data.pages_free]
|
|
|
|
shr ebx, 1
|
|
|
|
jz .nomemory
|
|
|
|
cmp ebx, CACHE_MAX_ALLOC_SIZE shr 12
|
|
|
|
jbe @f
|
|
|
|
mov ebx, CACHE_MAX_ALLOC_SIZE shr 12
|
|
|
|
@@:
|
2014-09-03 16:11:19 +04:00
|
|
|
shl ebx, 12
|
|
|
|
shr ebx, cl
|
|
|
|
jz .nomemory
|
2014-01-10 15:56:03 +04:00
|
|
|
cmp ebx, [.num_sectors]
|
|
|
|
jbe @f
|
|
|
|
mov ebx, [.num_sectors]
|
|
|
|
@@:
|
|
|
|
mov eax, ebx
|
2014-09-03 16:11:19 +04:00
|
|
|
shl eax, cl
|
2014-01-10 15:56:03 +04:00
|
|
|
stdcall kernel_alloc, eax
|
|
|
|
; If failed, return the appropriate error code.
|
|
|
|
test eax, eax
|
|
|
|
jz .nomemory
|
|
|
|
mov esi, eax
|
|
|
|
; Split the request to chunks that fit in the allocated buffer.
|
|
|
|
.read_loop:
|
|
|
|
; 8. Get iteration size: either size of allocated buffer in sectors
|
|
|
|
; or number of sectors left, select what is smaller.
|
|
|
|
cmp ebx, [.num_sectors]
|
|
|
|
jbe @f
|
|
|
|
mov ebx, [.num_sectors]
|
|
|
|
@@:
|
|
|
|
; 9. Create second portion of local variables.
|
|
|
|
; Note that variables here and above are esp-relative;
|
|
|
|
; it means that all addresses should be corrected when esp is changing.
|
|
|
|
push ebx esi esi
|
|
|
|
push ebx
|
|
|
|
; In particular, num_sectors is now [.num_sectors+.local_vars2_size].
|
|
|
|
virtual at esp
|
|
|
|
.local_vars2:
|
|
|
|
.current_num_sectors dd ? ; number of sectors that were read
|
|
|
|
.current_buffer dd ?
|
|
|
|
; pointer inside .allocated_buffer that points
|
|
|
|
; to the beginning of not-processed data
|
|
|
|
.allocated_buffer dd ? ; saved in safe place
|
|
|
|
.iteration_size dd ? ; saved in safe place
|
|
|
|
.local_vars2_size = $ - .local_vars2
|
|
|
|
end virtual
|
|
|
|
; 10. Call the driver, reading the next chunk.
|
|
|
|
push esp ; numsectors
|
|
|
|
push [.sector_hi+.local_vars2_size+4] ; startsector
|
|
|
|
push [.sector_lo+.local_vars2_size+8] ; startsector
|
|
|
|
push esi ; buffer
|
|
|
|
mov esi, [ebp+PARTITION.Disk]
|
|
|
|
mov al, DISKFUNC.read
|
|
|
|
call disk_call_driver
|
|
|
|
; If failed, save error code.
|
|
|
|
test eax, eax
|
|
|
|
jz @f
|
2014-01-13 15:06:54 +04:00
|
|
|
mov [.error_code+.local_vars2_size], eax
|
2014-01-10 15:56:03 +04:00
|
|
|
@@:
|
2014-09-03 16:11:19 +04:00
|
|
|
; 11. Copy data for the caller, advance .buffer.
|
2014-01-10 15:56:03 +04:00
|
|
|
cmp [.current_num_sectors], 0
|
|
|
|
jz .copy_done
|
2014-09-03 16:11:19 +04:00
|
|
|
mov ebx, [.cache+.local_vars2_size]
|
|
|
|
mov eax, [.current_num_sectors]
|
|
|
|
mov ecx, [ebx+DISKCACHE.sector_size_log]
|
|
|
|
shl eax, cl
|
2014-01-10 15:56:03 +04:00
|
|
|
mov esi, [.allocated_buffer]
|
2014-09-03 16:11:19 +04:00
|
|
|
mov edi, [.buffer+.local_vars2_size]
|
|
|
|
mov ecx, eax
|
|
|
|
shr ecx, 2
|
2014-01-10 15:56:03 +04:00
|
|
|
rep movsd
|
2014-09-03 16:11:19 +04:00
|
|
|
mov [.buffer+.local_vars2_size], edi
|
2014-01-10 15:56:03 +04:00
|
|
|
; 12. Copy data to the cache.
|
|
|
|
; 12a. Acquire the lock.
|
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
|
|
|
call mutex_lock
|
2014-09-03 16:11:19 +04:00
|
|
|
; 12b. Prepare for the loop: create a local variable that
|
2014-01-10 15:56:03 +04:00
|
|
|
; stores number of sectors to be copied.
|
2014-09-03 16:11:19 +04:00
|
|
|
push [.current_num_sectors]
|
2014-01-10 15:56:03 +04:00
|
|
|
.store_to_cache:
|
|
|
|
; 12c. For each sector, call the lookup function with adding to the cache, if not yet.
|
2014-09-03 16:11:19 +04:00
|
|
|
mov eax, [.sector_lo+.local_vars2_size+4]
|
|
|
|
mov edx, [.sector_hi+.local_vars2_size+4]
|
2014-01-10 15:56:03 +04:00
|
|
|
call cache_lookup_write
|
|
|
|
test eax, eax
|
|
|
|
jnz .cache_error
|
2014-01-17 14:27:05 +04:00
|
|
|
; 12d. If the sector was already present in the cache as modified,
|
|
|
|
; data that were read at step 10 for this sector are obsolete,
|
|
|
|
; so rewrite data for the caller from the cache.
|
|
|
|
cmp [esi+CACHE_ITEM.Status], CACHE_ITEM_MODIFIED
|
|
|
|
jnz .not_modified
|
2014-09-03 16:11:19 +04:00
|
|
|
mov esi, edi
|
|
|
|
mov edi, [.buffer+.local_vars2_size+4]
|
|
|
|
mov eax, [esp]
|
|
|
|
shl eax, cl
|
|
|
|
sub edi, eax
|
|
|
|
mov eax, 1
|
|
|
|
shl eax, cl
|
|
|
|
mov ecx, eax
|
|
|
|
shr ecx, 2
|
2014-01-17 14:27:05 +04:00
|
|
|
rep movsd
|
2014-09-03 16:11:19 +04:00
|
|
|
add [.current_buffer+4], eax
|
2014-01-17 14:27:05 +04:00
|
|
|
jmp .sector_done
|
|
|
|
.not_modified:
|
|
|
|
; 12e. For each not-modified sector,
|
|
|
|
; copy data, mark the item as not-modified copy of the disk,
|
2014-01-10 15:56:03 +04:00
|
|
|
; advance .current_buffer and .sector_hi:.sector_lo to the next sector.
|
|
|
|
mov [esi+CACHE_ITEM.Status], CACHE_ITEM_COPY
|
2014-09-03 16:11:19 +04:00
|
|
|
mov eax, 1
|
|
|
|
shl eax, cl
|
|
|
|
mov esi, [.current_buffer+4]
|
|
|
|
mov ecx, eax
|
|
|
|
shr ecx, 2
|
2014-01-10 15:56:03 +04:00
|
|
|
rep movsd
|
2014-09-03 16:11:19 +04:00
|
|
|
mov [.current_buffer+4], esi
|
2014-01-17 14:27:05 +04:00
|
|
|
.sector_done:
|
2014-09-03 16:11:19 +04:00
|
|
|
add [.sector_lo+.local_vars2_size+4], 1
|
|
|
|
adc [.sector_hi+.local_vars2_size+4], 0
|
2014-01-17 14:27:05 +04:00
|
|
|
; 12f. Continue the loop 12c-12e until all sectors are read.
|
2014-01-10 15:56:03 +04:00
|
|
|
dec dword [esp]
|
|
|
|
jnz .store_to_cache
|
|
|
|
.cache_error:
|
2014-09-03 16:11:19 +04:00
|
|
|
; 12g. Restore after the loop: pop the local variable.
|
2014-01-10 15:56:03 +04:00
|
|
|
pop ecx
|
2014-01-17 14:27:05 +04:00
|
|
|
; 12h. Release the lock.
|
2014-01-10 15:56:03 +04:00
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
|
|
|
call mutex_unlock
|
|
|
|
.copy_done:
|
|
|
|
; 13. Remove portion of local variables created at step 9.
|
|
|
|
pop ecx
|
|
|
|
pop esi esi ebx
|
|
|
|
; 14. Continue iterations while number of sectors read by the driver
|
|
|
|
; is equal to number of sectors requested and there are additional sectors.
|
|
|
|
cmp ecx, ebx
|
|
|
|
jnz @f
|
|
|
|
sub [.num_sectors], ebx
|
|
|
|
jnz .read_loop
|
|
|
|
@@:
|
|
|
|
; 15. Free the buffer allocated at step 7 and return.
|
|
|
|
stdcall kernel_free, esi
|
|
|
|
jmp .return
|
|
|
|
|
|
|
|
; Special branches:
|
|
|
|
.nomemory:
|
|
|
|
; memory allocation failed at step 7: return the corresponding error
|
|
|
|
mov [.error_code], DISK_STATUS_NO_MEMORY
|
|
|
|
jmp .return
|
|
|
|
.nocache:
|
|
|
|
; step 5, after correcting number of sectors to fit in partition limits
|
|
|
|
; and advancing partition-relative sector to absolute,
|
|
|
|
; sees that cache is disabled: pass corrected request to the driver
|
|
|
|
lea eax, [.num_sectors]
|
|
|
|
push eax ; numsectors
|
|
|
|
push [.sector_hi+4] ; startsector
|
|
|
|
push [.sector_lo+8] ; startsector
|
2014-09-03 16:11:19 +04:00
|
|
|
push [.buffer+12] ; buffer
|
2014-01-10 15:56:03 +04:00
|
|
|
mov esi, [ebp+PARTITION.Disk]
|
|
|
|
mov al, DISKFUNC.read
|
|
|
|
call disk_call_driver
|
|
|
|
test eax, eax
|
|
|
|
jnz @f
|
|
|
|
mov eax, [.error_code]
|
|
|
|
@@:
|
|
|
|
mov ecx, [.num_sectors]
|
|
|
|
jmp .nothing
|
|
|
|
.end_of_media:
|
|
|
|
; requested sector is beyond the partition end: return the corresponding error
|
|
|
|
mov [.error_code], DISK_STATUS_END_OF_MEDIA
|
|
|
|
jmp .return
|
|
|
|
|
|
|
|
; Write several sequential sectors using cache #1.
|
|
|
|
; in: edx:eax = start sector
|
|
|
|
; in: ecx = number of sectors to write
|
|
|
|
; in: ebx -> buffer
|
|
|
|
; in: ebp -> PARTITION
|
|
|
|
; out: eax = error code, 0 = ok
|
|
|
|
; out: ecx = number of sectors that were written
|
|
|
|
fs_write64_sys:
|
|
|
|
; Save ebx, set ebx to SysCache and let the common part do its work.
|
|
|
|
push ebx
|
|
|
|
mov ebx, [ebp+PARTITION.Disk]
|
|
|
|
add ebx, DISK.SysCache
|
|
|
|
jmp fs_write64_common
|
|
|
|
|
|
|
|
; Write several sequential sectors using cache #2.
|
|
|
|
; in: edx:eax = start sector
|
|
|
|
; in: ecx = number of sectors to write
|
|
|
|
; in: ebx -> buffer
|
|
|
|
; in: ebp -> PARTITION
|
|
|
|
; out: eax = error code, 0 = ok
|
|
|
|
; out: ecx = number of sectors that were written
|
|
|
|
fs_write64_app:
|
|
|
|
; Save ebx, set ebx to AppCache and let the common part do its work.
|
|
|
|
push ebx
|
|
|
|
mov ebx, [ebp+PARTITION.Disk]
|
|
|
|
add ebx, DISK.AppCache
|
|
|
|
|
|
|
|
; Common part of fs_write64_{app,sys}:
|
|
|
|
; write several sequential sectors using the given cache.
|
|
|
|
fs_write64_common:
|
|
|
|
; 1. Setup stack frame.
|
|
|
|
push esi edi ; save used registers to be stdcall
|
|
|
|
push 0 ; initialize .error_code
|
|
|
|
push edx eax ecx ecx ; initialize stack variables
|
|
|
|
push [.buffer-4] ; copy [.buffer] to [.cur_buffer]
|
|
|
|
; -4 is due to esp-relative addressing
|
|
|
|
virtual at esp
|
|
|
|
.local_vars:
|
|
|
|
.cur_buffer dd ? ; pointer to data that are currently copying
|
|
|
|
.num_sectors_orig dd ?
|
|
|
|
; Number of sectors that should be written. Used to generate output value of ecx.
|
|
|
|
.num_sectors dd ?
|
|
|
|
; Number of sectors that remain to be written.
|
|
|
|
.sector_lo dd ? ; low 32 bits of the current sector
|
|
|
|
.sector_hi dd ? ; high 32 bits of the current sector
|
|
|
|
.error_code dd ? ; current status
|
|
|
|
.local_vars_size = $ - .local_vars
|
|
|
|
.saved_regs rd 2
|
|
|
|
.buffer dd ? ; filled by fs_write64_{sys,app}
|
|
|
|
end virtual
|
|
|
|
; 2. Validate parameters against partition length:
|
|
|
|
; immediately return error if edx:eax are beyond partition end,
|
|
|
|
; decrease .num_sectors and .num_sectors_orig, if needed,
|
|
|
|
; so that the entire operation fits in the partition limits.
|
|
|
|
mov eax, dword [ebp+PARTITION.Length]
|
|
|
|
mov edx, dword [ebp+PARTITION.Length+4]
|
|
|
|
sub eax, [.sector_lo]
|
|
|
|
sbb edx, [.sector_hi]
|
|
|
|
jb .end_of_media
|
|
|
|
jnz .no_end_of_media
|
|
|
|
cmp ecx, eax
|
|
|
|
jbe .no_end_of_media
|
|
|
|
; If .num_sectors got decreased, set status to DISK_STATUS_END_OF_MEDIA;
|
|
|
|
; if all subsequent operations would be successful, this would become the final
|
|
|
|
; status, otherwise this would be rewritten by failed operation.
|
|
|
|
mov [.num_sectors], eax
|
|
|
|
mov [.num_sectors_orig], eax
|
|
|
|
mov [.error_code], DISK_STATUS_END_OF_MEDIA
|
|
|
|
.no_end_of_media:
|
|
|
|
; 3. If number of sectors to write is zero, either because zero-sectors operation
|
|
|
|
; was requested or because it got decreased to zero due to partition limits,
|
|
|
|
; just return the current status.
|
|
|
|
cmp [.num_sectors], 0
|
|
|
|
jz .return
|
|
|
|
; 4. Shift sector from partition-relative to absolute.
|
|
|
|
mov eax, dword [ebp+PARTITION.FirstSector]
|
|
|
|
mov edx, dword [ebp+PARTITION.FirstSector+4]
|
|
|
|
add [.sector_lo], eax
|
|
|
|
adc [.sector_hi], edx
|
|
|
|
; 5. If the cache is disabled, pass the request directly to the driver.
|
|
|
|
cmp [ebx+DISKCACHE.pointer], 0
|
|
|
|
jz .nocache
|
|
|
|
; 6. Store sectors in the cache, sequentially from the beginning.
|
|
|
|
; 6a. Acquire the lock.
|
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
|
|
|
call mutex_lock
|
|
|
|
.lookup_in_cache_loop:
|
|
|
|
; 6b. For each sector, call the lookup function with adding to the cache, if not yet.
|
|
|
|
mov eax, [.sector_lo]
|
|
|
|
mov edx, [.sector_hi]
|
|
|
|
call cache_lookup_write
|
|
|
|
test eax, eax
|
|
|
|
jnz .cache_error
|
|
|
|
; 6c. For each sector, copy data, mark the item as modified and not saved,
|
|
|
|
; advance .current_buffer to the next sector.
|
|
|
|
mov [esi+CACHE_ITEM.Status], CACHE_ITEM_MODIFIED
|
2014-09-03 16:11:19 +04:00
|
|
|
mov eax, 1
|
|
|
|
shl eax, cl
|
2014-01-10 15:56:03 +04:00
|
|
|
mov esi, [.cur_buffer]
|
2014-09-03 16:11:19 +04:00
|
|
|
mov ecx, eax
|
|
|
|
shr ecx, 2
|
2014-01-10 15:56:03 +04:00
|
|
|
rep movsd
|
|
|
|
mov [.cur_buffer], esi
|
|
|
|
; 6d. Remove the sector from the other cache.
|
|
|
|
; Normally it should not be there, but prefetching could put to the app cache
|
|
|
|
; data that normally should belong to the sys cache and vice versa.
|
|
|
|
; Note: this requires that both caches must be protected by the same lock.
|
|
|
|
mov eax, [.sector_lo]
|
|
|
|
mov edx, [.sector_hi]
|
|
|
|
push ebx
|
|
|
|
sub ebx, [ebp+PARTITION.Disk]
|
|
|
|
xor ebx, DISK.SysCache xor DISK.AppCache
|
|
|
|
add ebx, [ebp+PARTITION.Disk]
|
|
|
|
call cache_lookup_read
|
|
|
|
jc @f
|
|
|
|
mov [esi+CACHE_ITEM.Status], CACHE_ITEM_EMPTY
|
|
|
|
@@:
|
|
|
|
pop ebx
|
|
|
|
; 6e. Advance .sector_hi:.sector_lo to the next sector.
|
|
|
|
add [.sector_lo], 1
|
|
|
|
adc [.sector_hi], 0
|
|
|
|
; 6f. Continue the loop at 6b-6e until all sectors are processed.
|
|
|
|
dec [.num_sectors]
|
|
|
|
jnz .lookup_in_cache_loop
|
|
|
|
.unlock_return:
|
|
|
|
; 6g. Release the lock and return.
|
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
|
|
|
call mutex_unlock
|
|
|
|
.return:
|
|
|
|
mov eax, [.error_code]
|
|
|
|
mov ecx, [.num_sectors_orig]
|
|
|
|
sub ecx, [.num_sectors]
|
|
|
|
.nothing:
|
|
|
|
add esp, .local_vars_size
|
|
|
|
pop edi esi ebx
|
|
|
|
ret
|
|
|
|
|
|
|
|
; Special branches:
|
|
|
|
.cache_error:
|
|
|
|
; error at flushing the cache while adding sector to the cache:
|
|
|
|
; return the error from the lookup function
|
|
|
|
mov [.error_code], eax
|
|
|
|
jmp .unlock_return
|
|
|
|
.end_of_media:
|
|
|
|
; requested sector is beyond the partition end: return the corresponding error
|
|
|
|
mov eax, DISK_STATUS_END_OF_MEDIA
|
|
|
|
xor ecx, ecx
|
|
|
|
jmp .nothing
|
|
|
|
.nocache:
|
|
|
|
; step 5, after correcting number of sectors to fit in partition limits
|
|
|
|
; and advancing partition-relative sector to absolute,
|
|
|
|
; sees that cache is disabled: pass corrected request to the driver
|
|
|
|
lea eax, [.num_sectors]
|
|
|
|
push eax ; numsectors
|
|
|
|
push [.sector_hi+4] ; startsector
|
|
|
|
push [.sector_lo+8] ; startsector
|
|
|
|
push [.buffer+12] ; buffer
|
|
|
|
mov esi, [ebp+PARTITION.Disk]
|
|
|
|
mov al, DISKFUNC.write
|
|
|
|
call disk_call_driver
|
|
|
|
mov ecx, [.num_sectors]
|
|
|
|
jmp .nothing
|
|
|
|
|
|
|
|
; Legacy. Use fs_read64_sys instead.
|
2011-10-15 01:38:50 +04:00
|
|
|
; This function is intended to replace the old 'hd_read' function when
|
|
|
|
; [hdd_appl_data] = 0, so its input/output parameters are the same, except
|
|
|
|
; that it can't use the global variables 'hd_error' and 'hdd_appl_data'.
|
|
|
|
; in: eax = sector, ebx = buffer, ebp = pointer to PARTITION structure
|
|
|
|
; eax is relative to partition start
|
|
|
|
; out: eax = error code; 0 = ok
|
|
|
|
fs_read32_sys:
|
2014-01-10 15:56:03 +04:00
|
|
|
; Save ebx, set ebx to SysCache and let the common part do its work.
|
|
|
|
push ebx
|
|
|
|
mov ebx, [ebp+PARTITION.Disk]
|
|
|
|
add ebx, DISK.SysCache
|
2011-10-15 01:38:50 +04:00
|
|
|
jmp fs_read32_common
|
|
|
|
|
2014-01-10 15:56:03 +04:00
|
|
|
; Legacy. Use fs_read64_app instead.
|
2011-10-15 01:38:50 +04:00
|
|
|
; This function is intended to replace the old 'hd_read' function when
|
|
|
|
; [hdd_appl_data] = 1, so its input/output parameters are the same, except
|
|
|
|
; that it can't use the global variables 'hd_error' and 'hdd_appl_data'.
|
|
|
|
; in: eax = sector, ebx = buffer, ebp = pointer to PARTITION structure
|
|
|
|
; eax is relative to partition start
|
|
|
|
; out: eax = error code; 0 = ok
|
|
|
|
fs_read32_app:
|
2014-01-10 15:56:03 +04:00
|
|
|
; Save ebx, set ebx to AppCache and let the common part do its work.
|
|
|
|
push ebx
|
|
|
|
mov ebx, [ebp+PARTITION.Disk]
|
|
|
|
add ebx, DISK.AppCache
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
; This label is the common part of fs_read32_sys and fs_read32_app.
|
|
|
|
fs_read32_common:
|
|
|
|
; 1. Check that the required sector is inside the partition. If no, return
|
|
|
|
; DISK_STATUS_END_OF_MEDIA.
|
|
|
|
cmp dword [ebp+PARTITION.Length+4], 0
|
|
|
|
jnz @f
|
|
|
|
cmp dword [ebp+PARTITION.Length], eax
|
|
|
|
ja @f
|
|
|
|
mov eax, DISK_STATUS_END_OF_MEDIA
|
2014-01-10 15:56:03 +04:00
|
|
|
pop ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
@@:
|
|
|
|
; 2. Get the absolute sector on the disk.
|
2014-01-10 15:56:03 +04:00
|
|
|
push ecx edx esi edi
|
2011-10-15 01:38:50 +04:00
|
|
|
xor edx, edx
|
|
|
|
add eax, dword [ebp+PARTITION.FirstSector]
|
|
|
|
adc edx, dword [ebp+PARTITION.FirstSector+4]
|
|
|
|
; 3. If there is no cache for this disk, just pass the request to the driver.
|
2014-01-10 15:56:03 +04:00
|
|
|
cmp [ebx+DISKCACHE.pointer], 0
|
2011-10-15 01:38:50 +04:00
|
|
|
jnz .scancache
|
|
|
|
push 1
|
|
|
|
push esp ; numsectors
|
|
|
|
push edx ; startsector
|
|
|
|
push eax ; startsector
|
2014-01-10 15:56:03 +04:00
|
|
|
pushd [esp+32]; buffer
|
2012-04-23 13:19:34 +04:00
|
|
|
mov esi, [ebp+PARTITION.Disk]
|
2011-10-15 01:38:50 +04:00
|
|
|
mov al, DISKFUNC.read
|
|
|
|
call disk_call_driver
|
|
|
|
pop ecx
|
2014-01-10 15:56:03 +04:00
|
|
|
pop edi esi edx ecx
|
|
|
|
pop ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
.scancache:
|
2014-01-10 15:56:03 +04:00
|
|
|
push ebx edx eax
|
2011-10-15 01:38:50 +04:00
|
|
|
virtual at esp
|
2014-01-10 15:56:03 +04:00
|
|
|
.local_vars:
|
2011-10-15 01:38:50 +04:00
|
|
|
.sector_lo dd ?
|
|
|
|
.sector_hi dd ?
|
|
|
|
.cache dd ?
|
2014-01-10 15:56:03 +04:00
|
|
|
.local_vars_size = $ - .local_vars
|
|
|
|
.saved_regs rd 4
|
|
|
|
.buffer dd ?
|
2011-10-15 01:38:50 +04:00
|
|
|
end virtual
|
2014-01-10 15:56:03 +04:00
|
|
|
; 4. Scan for the requested sector in the cache.
|
|
|
|
; If found, copy the data and return.
|
|
|
|
; 4a. Acquire the lock.
|
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
2011-10-15 01:38:50 +04:00
|
|
|
call mutex_lock
|
2014-01-10 15:56:03 +04:00
|
|
|
; 4b. Call the lookup function without adding to the cache.
|
2011-10-15 01:38:50 +04:00
|
|
|
mov eax, [.sector_lo]
|
|
|
|
mov edx, [.sector_hi]
|
2014-01-10 15:56:03 +04:00
|
|
|
call cache_lookup_read
|
|
|
|
; If not found, go to 5.
|
|
|
|
jc .not_found_in_cache
|
|
|
|
.found_in_cache:
|
|
|
|
; 4c. Copy the data.
|
2014-09-03 16:11:19 +04:00
|
|
|
mov esi, edi
|
2014-01-10 15:56:03 +04:00
|
|
|
mov edi, [.buffer]
|
2014-09-03 16:11:19 +04:00
|
|
|
mov eax, 1
|
|
|
|
shl eax, cl
|
|
|
|
mov ecx, eax
|
|
|
|
shr ecx, 2
|
2014-01-10 15:56:03 +04:00
|
|
|
rep movsd
|
|
|
|
; 4d. Release the lock and return success.
|
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
|
|
|
call mutex_unlock
|
|
|
|
.return:
|
|
|
|
xor eax, eax
|
|
|
|
.return_eax:
|
|
|
|
add esp, .local_vars_size
|
|
|
|
pop edi esi edx ecx
|
|
|
|
pop ebx
|
|
|
|
ret
|
|
|
|
.not_found_in_cache:
|
|
|
|
; 5. Decide whether we need to prefetch further sectors.
|
|
|
|
; If so, advance to 6. If not, go to 13.
|
|
|
|
; Assume that devices < 3MB are floppies which are slow
|
|
|
|
; (ramdisk does not have a cache, so we don't even get here for ramdisk).
|
|
|
|
; This is a dirty hack, but the entire function is somewhat hacky. Use fs_read64*.
|
2014-11-28 23:19:34 +03:00
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
cmp dword [ecx+DISK.MediaInfo.Capacity+4], 0
|
2014-01-10 15:56:03 +04:00
|
|
|
jnz @f
|
2014-11-28 23:19:34 +03:00
|
|
|
cmp dword [ecx+DISK.MediaInfo.Capacity], 3 shl (20-9)
|
2014-01-10 15:56:03 +04:00
|
|
|
jb .floppy
|
|
|
|
@@:
|
|
|
|
; We want to prefetch CACHE_LEGACY_READ_SIZE sectors.
|
|
|
|
; 6. Release the lock acquired at step 4a.
|
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
|
|
|
call mutex_unlock
|
|
|
|
; 7. Allocate buffer for CACHE_LEGACY_READ_SIZE sectors.
|
2014-09-03 16:11:19 +04:00
|
|
|
mov eax, CACHE_LEGACY_READ_SIZE
|
|
|
|
mov ecx, [ebx+DISKCACHE.sector_size_log]
|
|
|
|
shl eax, cl
|
|
|
|
stdcall kernel_alloc, eax
|
2014-01-10 15:56:03 +04:00
|
|
|
; If failed, return the corresponding error code.
|
|
|
|
test eax, eax
|
|
|
|
jz .nomemory
|
|
|
|
; 8. Create second portion of local variables.
|
|
|
|
push eax eax
|
|
|
|
push CACHE_LEGACY_READ_SIZE
|
|
|
|
virtual at esp
|
|
|
|
.local_vars2:
|
|
|
|
.num_sectors dd ? ; number of sectors left
|
|
|
|
.current_buffer dd ? ; pointer to data that are currently copying
|
|
|
|
.allocated_buffer dd ? ; saved at safe place
|
|
|
|
.local_vars2_size = $ - .local_vars2
|
|
|
|
end virtual
|
|
|
|
; 9. Call the driver to read CACHE_LEGACY_READ_SIZE sectors.
|
|
|
|
push esp ; numsectors
|
|
|
|
push [.sector_hi+.local_vars2_size+4] ; startsector
|
|
|
|
push [.sector_lo+.local_vars2_size+8] ; startsector
|
|
|
|
push eax ; buffer
|
|
|
|
mov esi, [ebp+PARTITION.Disk]
|
|
|
|
mov al, DISKFUNC.read
|
|
|
|
call disk_call_driver
|
|
|
|
; Note: we're ok if at least one sector is read,
|
|
|
|
; read error somewhere after that just limits data to be put in cache.
|
|
|
|
cmp [.num_sectors], 0
|
|
|
|
jz .read_error
|
|
|
|
; 10. Copy data for the caller.
|
|
|
|
mov esi, [.allocated_buffer]
|
|
|
|
mov edi, [.buffer+.local_vars2_size]
|
2014-09-03 16:11:19 +04:00
|
|
|
mov ecx, [ebx+DISKCACHE.sector_size_log]
|
|
|
|
mov eax, 1
|
|
|
|
shl eax, cl
|
|
|
|
mov ecx, eax
|
|
|
|
shr ecx, 2
|
2014-01-10 15:56:03 +04:00
|
|
|
rep movsd
|
|
|
|
; 11. Store all sectors that were successfully read to the cache.
|
|
|
|
; 11a. Acquire the lock.
|
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
|
|
|
call mutex_lock
|
|
|
|
.store_to_cache:
|
|
|
|
; 11b. For each sector, call the lookup function with adding to the cache, if not yet.
|
|
|
|
mov eax, [.sector_lo+.local_vars2_size]
|
|
|
|
mov edx, [.sector_hi+.local_vars2_size]
|
|
|
|
call cache_lookup_write
|
|
|
|
test eax, eax
|
|
|
|
jnz .cache_error
|
2014-01-17 14:27:05 +04:00
|
|
|
; 11c. Ignore sectors marked as modified: for them the cache is more recent that disk data.
|
2014-09-03 16:11:19 +04:00
|
|
|
mov eax, 1
|
|
|
|
shl eax, cl
|
2014-01-17 14:27:05 +04:00
|
|
|
cmp [esi+CACHE_ITEM.Status], CACHE_ITEM_MODIFIED
|
|
|
|
jnz .not_modified
|
2014-09-03 16:11:19 +04:00
|
|
|
add [.current_buffer], eax
|
2014-01-17 14:27:05 +04:00
|
|
|
jmp .sector_done
|
|
|
|
.not_modified:
|
|
|
|
; 11d. For each sector, copy data, mark the item as not-modified copy of the disk,
|
2014-01-10 15:56:03 +04:00
|
|
|
; advance .current_buffer and .sector_hi:.sector_lo to the next sector.
|
|
|
|
mov [esi+CACHE_ITEM.Status], CACHE_ITEM_COPY
|
|
|
|
mov esi, [.current_buffer]
|
2014-09-03 16:11:19 +04:00
|
|
|
mov ecx, eax
|
|
|
|
shr ecx, 2
|
2014-01-10 15:56:03 +04:00
|
|
|
rep movsd
|
|
|
|
mov [.current_buffer], esi
|
2014-01-17 14:27:05 +04:00
|
|
|
.sector_done:
|
2014-01-10 15:56:03 +04:00
|
|
|
add [.sector_lo+.local_vars2_size], 1
|
|
|
|
adc [.sector_hi+.local_vars2_size], 0
|
2014-01-17 14:27:05 +04:00
|
|
|
; 11e. Continue the loop at 11b-11d until all sectors are processed.
|
2014-01-10 15:56:03 +04:00
|
|
|
dec [.num_sectors]
|
|
|
|
jnz .store_to_cache
|
|
|
|
.cache_error:
|
2014-01-17 14:27:05 +04:00
|
|
|
; 11f. Release the lock.
|
2014-01-10 15:56:03 +04:00
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
|
|
|
call mutex_unlock
|
|
|
|
.copy_done:
|
|
|
|
; 12. Remove portion of local variables created at step 8,
|
|
|
|
; free the buffer allocated at step 7 and return.
|
|
|
|
pop ecx ecx
|
|
|
|
stdcall kernel_free
|
|
|
|
jmp .return
|
|
|
|
.read_error:
|
|
|
|
; If no sectors were read, free the buffer allocated at step 7
|
|
|
|
; and pass the error to the caller.
|
|
|
|
push eax
|
|
|
|
stdcall kernel_free, [.allocated_buffer+4]
|
|
|
|
pop eax
|
|
|
|
add esp, .local_vars2_size
|
|
|
|
jmp .return_eax
|
|
|
|
.nomemory:
|
|
|
|
mov eax, DISK_STATUS_NO_MEMORY
|
|
|
|
jmp .return_eax
|
|
|
|
.floppy:
|
|
|
|
; We don't want to prefetch anything, just read one sector.
|
|
|
|
; We are still holding the lock acquired at step 4a.
|
|
|
|
; 13. Call the lookup function adding sector to the cache.
|
|
|
|
call cache_lookup_write
|
2011-10-15 01:38:50 +04:00
|
|
|
test eax, eax
|
2014-01-10 15:56:03 +04:00
|
|
|
jnz .floppy_cache_error
|
2014-09-03 16:11:19 +04:00
|
|
|
push esi
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-01-17 14:27:05 +04:00
|
|
|
; 14. Call the driver to read one sector.
|
2011-10-15 01:38:50 +04:00
|
|
|
push 1
|
|
|
|
push esp
|
|
|
|
push edx
|
2014-01-10 15:56:03 +04:00
|
|
|
push [.sector_lo+16]
|
2014-09-03 16:11:19 +04:00
|
|
|
push edi
|
2011-10-15 01:38:50 +04:00
|
|
|
mov esi, [ebp+PARTITION.Disk]
|
|
|
|
mov al, DISKFUNC.read
|
|
|
|
call disk_call_driver
|
|
|
|
pop ecx
|
|
|
|
dec ecx
|
2014-01-10 15:56:03 +04:00
|
|
|
jnz .floppy_read_error
|
2014-01-17 14:27:05 +04:00
|
|
|
; 15. Get the slot and pointer to the cache item,
|
2014-01-10 15:56:03 +04:00
|
|
|
; change the status to not-modified copy of the disk
|
|
|
|
; and go to 4c.
|
2014-09-03 16:11:19 +04:00
|
|
|
pop esi
|
2014-01-10 15:56:03 +04:00
|
|
|
mov [esi+CACHE_ITEM.Status], CACHE_ITEM_COPY
|
2014-11-28 23:19:34 +03:00
|
|
|
mov ecx, [ebx+DISKCACHE.sector_size_log]
|
2014-01-10 15:56:03 +04:00
|
|
|
jmp .found_in_cache
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-01-17 14:27:05 +04:00
|
|
|
; On error at steps 13-14, release the lock
|
2014-01-10 15:56:03 +04:00
|
|
|
; and pass the error to the caller.
|
|
|
|
.floppy_read_error:
|
|
|
|
pop ecx
|
|
|
|
.floppy_cache_error:
|
|
|
|
mov ecx, [ebp+PARTITION.Disk]
|
|
|
|
add ecx, DISK.CacheLock
|
2011-10-15 01:38:50 +04:00
|
|
|
push eax
|
|
|
|
call mutex_unlock
|
|
|
|
pop eax
|
2014-01-10 15:56:03 +04:00
|
|
|
jmp .return_eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
; This function is intended to replace the old 'hd_write' function when
|
|
|
|
; [hdd_appl_data] = 0, so its input/output parameters are the same, except
|
|
|
|
; that it can't use the global variables 'hd_error' and 'hdd_appl_data'.
|
|
|
|
; in: eax = sector, ebx = buffer, ebp = pointer to PARTITION structure
|
|
|
|
; eax is relative to partition start
|
|
|
|
; out: eax = error code; 0 = ok
|
|
|
|
fs_write32_sys:
|
2014-01-10 15:56:03 +04:00
|
|
|
; Just call the advanced function.
|
|
|
|
push ecx edx
|
|
|
|
xor edx, edx
|
|
|
|
mov ecx, 1
|
|
|
|
call fs_write64_sys
|
|
|
|
pop edx ecx
|
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
; This function is intended to replace the old 'hd_write' function when
|
|
|
|
; [hdd_appl_data] = 1, so its input/output parameters are the same, except
|
|
|
|
; that it can't use the global variables 'hd_error' and 'hdd_appl_data'.
|
|
|
|
; in: eax = sector, ebx = buffer, ebp = pointer to PARTITION structure
|
|
|
|
; eax is relative to partition start
|
|
|
|
; out: eax = error code; 0 = ok
|
|
|
|
fs_write32_app:
|
2014-01-10 15:56:03 +04:00
|
|
|
; Just call the advanced function.
|
|
|
|
push ecx edx
|
2011-10-15 01:38:50 +04:00
|
|
|
xor edx, edx
|
2014-01-10 15:56:03 +04:00
|
|
|
mov ecx, 1
|
|
|
|
call fs_write64_app
|
|
|
|
pop edx ecx
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
|
2014-01-10 15:56:03 +04:00
|
|
|
; Lookup for the given sector in the given cache.
|
|
|
|
; If the sector is not present, return error.
|
|
|
|
; The caller must acquire the cache lock.
|
|
|
|
; in: edx:eax = sector
|
|
|
|
; in: ebx -> DISKCACHE structure
|
|
|
|
; out: CF set if sector is not in cache
|
2014-09-03 16:11:19 +04:00
|
|
|
; out: ecx = sector_size_log
|
2014-01-10 15:56:03 +04:00
|
|
|
; out: esi -> sector:status
|
2014-09-03 16:11:19 +04:00
|
|
|
; out: edi -> sector data
|
2014-01-10 15:56:03 +04:00
|
|
|
proc cache_lookup_read
|
|
|
|
mov esi, [ebx+DISKCACHE.pointer]
|
|
|
|
add esi, sizeof.CACHE_ITEM
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-09-03 16:11:19 +04:00
|
|
|
mov edi, 1
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-01-10 15:56:03 +04:00
|
|
|
.hdreadcache:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-01-10 15:56:03 +04:00
|
|
|
cmp [esi+CACHE_ITEM.Status], CACHE_ITEM_EMPTY
|
|
|
|
je .nohdcache
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-01-10 15:56:03 +04:00
|
|
|
cmp [esi+CACHE_ITEM.SectorLo], eax
|
|
|
|
jne .nohdcache
|
|
|
|
cmp [esi+CACHE_ITEM.SectorHi], edx
|
|
|
|
jne .nohdcache
|
2014-09-03 16:11:19 +04:00
|
|
|
mov ecx, [ebx+DISKCACHE.sector_size_log]
|
|
|
|
shl edi, cl
|
|
|
|
add edi, [ebx+DISKCACHE.data]
|
2014-01-10 15:56:03 +04:00
|
|
|
clc
|
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-01-10 15:56:03 +04:00
|
|
|
.nohdcache:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-01-10 15:56:03 +04:00
|
|
|
add esi, sizeof.CACHE_ITEM
|
2014-09-03 16:11:19 +04:00
|
|
|
inc edi
|
|
|
|
cmp edi, [ebx+DISKCACHE.sad_size]
|
2014-01-10 15:56:03 +04:00
|
|
|
jbe .hdreadcache
|
|
|
|
stc
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
2014-01-10 15:56:03 +04:00
|
|
|
endp
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-01-10 15:56:03 +04:00
|
|
|
; Lookup for the given sector in the given cache.
|
|
|
|
; If the sector is not present, allocate space for it,
|
|
|
|
; possibly flushing data.
|
|
|
|
; in: edx:eax = sector
|
|
|
|
; in: ebx -> DISKCACHE structure
|
|
|
|
; in: ebp -> PARTITION structure
|
|
|
|
; out: eax = error code
|
|
|
|
; out: esi -> sector:status
|
2014-09-03 16:11:19 +04:00
|
|
|
; out: edi -> sector data
|
2014-01-10 15:56:03 +04:00
|
|
|
proc cache_lookup_write
|
|
|
|
call cache_lookup_read
|
|
|
|
jnc .return0
|
|
|
|
push edx eax
|
2011-10-15 01:38:50 +04:00
|
|
|
;-----------------------------------------------------------
|
|
|
|
; find empty or read slot, flush cache if next 12.5% is used by write
|
2014-01-10 15:56:03 +04:00
|
|
|
; output : ecx = cache slot
|
2011-10-15 01:38:50 +04:00
|
|
|
;-----------------------------------------------------------
|
2014-01-10 15:56:03 +04:00
|
|
|
; Note: the code is essentially inherited, so probably
|
|
|
|
; no analysis of efficiency were done.
|
|
|
|
; However, it works.
|
2011-10-15 01:38:50 +04:00
|
|
|
.search_again:
|
2014-01-10 15:56:03 +04:00
|
|
|
mov eax, [ebx+DISKCACHE.sad_size]
|
|
|
|
mov ecx, [ebx+DISKCACHE.search_start]
|
|
|
|
shr eax, 3
|
|
|
|
lea esi, [ecx*sizeof.CACHE_ITEM/4]
|
|
|
|
shl esi, 2
|
|
|
|
add esi, [ebx+DISKCACHE.pointer]
|
2011-10-15 01:38:50 +04:00
|
|
|
.search_for_empty:
|
2014-01-10 15:56:03 +04:00
|
|
|
inc ecx
|
|
|
|
add esi, sizeof.CACHE_ITEM
|
|
|
|
cmp ecx, [ebx+DISKCACHE.sad_size]
|
2011-10-15 01:38:50 +04:00
|
|
|
jbe .inside_cache
|
2014-01-10 15:56:03 +04:00
|
|
|
mov ecx, 1
|
|
|
|
mov esi, [ebx+DISKCACHE.pointer]
|
|
|
|
add esi, sizeof.CACHE_ITEM
|
2011-10-15 01:38:50 +04:00
|
|
|
.inside_cache:
|
2014-01-10 15:56:03 +04:00
|
|
|
cmp [esi+CACHE_ITEM.Status], CACHE_ITEM_MODIFIED
|
2011-10-15 01:38:50 +04:00
|
|
|
jb .found_slot ; it's empty or read
|
2014-01-10 15:56:03 +04:00
|
|
|
dec eax
|
2011-10-15 01:38:50 +04:00
|
|
|
jnz .search_for_empty
|
2012-04-23 13:19:34 +04:00
|
|
|
stdcall write_cache64, [ebp+PARTITION.Disk] ; no empty slots found, write all
|
2011-10-15 01:38:50 +04:00
|
|
|
test eax, eax
|
|
|
|
jne .found_slot_access_denied
|
|
|
|
jmp .search_again ; and start again
|
|
|
|
.found_slot:
|
2014-01-10 15:56:03 +04:00
|
|
|
mov [ebx+DISKCACHE.search_start], ecx
|
|
|
|
popd [esi+CACHE_ITEM.SectorLo]
|
|
|
|
popd [esi+CACHE_ITEM.SectorHi]
|
2014-01-17 14:27:05 +04:00
|
|
|
mov [esi+CACHE_ITEM.Status], CACHE_ITEM_EMPTY
|
2014-09-03 16:11:19 +04:00
|
|
|
mov edi, ecx
|
|
|
|
mov ecx, [ebx+DISKCACHE.sector_size_log]
|
|
|
|
shl edi, cl
|
|
|
|
add edi, [ebx+DISKCACHE.data]
|
2014-01-10 15:56:03 +04:00
|
|
|
.return0:
|
2011-10-15 01:38:50 +04:00
|
|
|
xor eax, eax ; success
|
2014-01-10 15:56:03 +04:00
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
.found_slot_access_denied:
|
2014-01-10 15:56:03 +04:00
|
|
|
add esp, 8
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
2014-01-10 15:56:03 +04:00
|
|
|
endp
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-01-10 15:56:03 +04:00
|
|
|
; Flush the given cache.
|
|
|
|
; The caller must acquire the cache lock.
|
|
|
|
; in: ebx -> DISKCACHE
|
|
|
|
; in: first argument in stdcall convention -> PARTITION
|
|
|
|
proc write_cache64
|
|
|
|
; 1. Setup stack frame.
|
|
|
|
push esi edi ; save used registers to be stdcall
|
|
|
|
sub esp, .local_vars_size ; reserve space for local vars
|
|
|
|
virtual at esp
|
|
|
|
.local_vars:
|
|
|
|
.cache_end dd ? ; item past the end of the cache
|
|
|
|
.size_left dd ? ; items left to scan
|
|
|
|
.current_ptr dd ? ; pointer to the current item
|
|
|
|
;
|
|
|
|
; Write operations are coalesced in chains,
|
|
|
|
; one chain describes a sequential interval of sectors,
|
|
|
|
; they can be sequential or scattered in the cache.
|
|
|
|
.sequential dd ?
|
|
|
|
; boolean variable, 1 if the current chain is sequential in the cache,
|
|
|
|
; 0 if additional buffer is needed to perform the operation
|
2014-09-03 16:11:19 +04:00
|
|
|
.chain_start_pos dd ? ; data of chain start item
|
2014-01-10 15:56:03 +04:00
|
|
|
.chain_start_ptr dd ? ; pointer to chain start item
|
|
|
|
.chain_size dd ? ; chain size (thanks, C.O.)
|
|
|
|
.iteration_size dd ?
|
|
|
|
; If the chain size is too large, split the operation to several iterations.
|
|
|
|
; This is size in sectors for one iterations.
|
|
|
|
.iteration_buffer dd ? ; temporary buffer for non-sequential chains
|
|
|
|
.local_vars_size = $ - .local_vars
|
|
|
|
rd 2 ; saved registers
|
|
|
|
dd ? ; return address
|
|
|
|
.disk dd ? ; first argument
|
|
|
|
end virtual
|
|
|
|
; 1. If there is no cache for this disk, nothing to do, just return zero.
|
|
|
|
cmp [ebx+DISKCACHE.pointer], 0
|
|
|
|
jz .return0
|
|
|
|
; 2. Prepare for the loop: initialize current pointer and .size_left,
|
|
|
|
; calculate .cache_end.
|
|
|
|
mov ecx, [ebx+DISKCACHE.sad_size]
|
|
|
|
mov [.size_left], ecx
|
|
|
|
lea ecx, [ecx*sizeof.CACHE_ITEM/4]
|
|
|
|
shl ecx, 2
|
|
|
|
mov esi, [ebx+DISKCACHE.pointer]
|
|
|
|
add esi, sizeof.CACHE_ITEM
|
|
|
|
add ecx, esi
|
|
|
|
mov [.cache_end], ecx
|
|
|
|
; 3. Main loop: go over all items, go to 5 for every modified item.
|
|
|
|
.look:
|
|
|
|
cmp [esi+CACHE_ITEM.Status], CACHE_ITEM_MODIFIED
|
|
|
|
jz .begin_write
|
|
|
|
.look_next:
|
|
|
|
add esi, sizeof.CACHE_ITEM
|
|
|
|
dec [.size_left]
|
|
|
|
jnz .look
|
|
|
|
; 4. Return success.
|
|
|
|
.return0:
|
|
|
|
xor eax, eax
|
|
|
|
.return:
|
|
|
|
add esp, .local_vars_size
|
|
|
|
pop edi esi ; restore used registers to be stdcall
|
|
|
|
ret 4 ; return popping one argument
|
|
|
|
.begin_write:
|
|
|
|
; We have found a modified item.
|
|
|
|
; 5. Prepare for chain finding: save the current item, initialize chain variables.
|
|
|
|
mov [.current_ptr], esi
|
|
|
|
; Initialize chain as sequential zero-length starting at the current item.
|
|
|
|
mov [.chain_start_ptr], esi
|
|
|
|
mov eax, [ebx+DISKCACHE.sad_size]
|
|
|
|
sub eax, [.size_left]
|
|
|
|
inc eax
|
2014-09-03 16:11:19 +04:00
|
|
|
mov ecx, [ebx+DISKCACHE.sector_size_log]
|
|
|
|
shl eax, cl
|
|
|
|
add eax, [ebx+DISKCACHE.data]
|
2014-01-10 15:56:03 +04:00
|
|
|
mov [.chain_start_pos], eax
|
|
|
|
mov [.chain_size], 0
|
|
|
|
mov [.sequential], 1
|
|
|
|
; 6. Expand the chain backward.
|
|
|
|
; Note: the main loop in step 2 looks for items sequentially,
|
|
|
|
; so the previous item is not modified. If the previous sector
|
|
|
|
; is present in the cache, it automatically makes the chain scattered.
|
|
|
|
; 6a. Calculate sector number: one before the sector for the current item.
|
|
|
|
mov eax, [esi+CACHE_ITEM.SectorLo]
|
|
|
|
mov edx, [esi+CACHE_ITEM.SectorHi]
|
|
|
|
sub eax, 1
|
|
|
|
sbb edx, 0
|
|
|
|
.find_chain_start:
|
|
|
|
; 6b. For each sector where the previous item does not expand the chain,
|
|
|
|
; call the lookup function without adding to the cache.
|
|
|
|
call cache_lookup_read
|
|
|
|
; 6c. If the sector is not found in cache or is not modified, stop expanding
|
|
|
|
; and advance to step 7.
|
|
|
|
jc .found_chain_start
|
|
|
|
cmp [esi+CACHE_ITEM.Status], CACHE_ITEM_MODIFIED
|
|
|
|
jnz .found_chain_start
|
|
|
|
; 6d. We have found a new block that expands the chain backwards.
|
|
|
|
; It makes the chain non-sequential.
|
|
|
|
; Normally, sectors come in sequential blocks, so try to look at previous items
|
|
|
|
; before returning to 6b; if there is a sequential block indeed, this saves some
|
|
|
|
; time instead of many full-fledged lookups.
|
|
|
|
mov [.sequential], 0
|
2014-09-03 16:11:19 +04:00
|
|
|
mov [.chain_start_pos], edi
|
2014-01-10 15:56:03 +04:00
|
|
|
.look_backward:
|
|
|
|
; 6e. For each sector, update chain start pos/ptr, decrement sector number,
|
|
|
|
; look at the previous item.
|
|
|
|
mov [.chain_start_ptr], esi
|
|
|
|
inc [.chain_size]
|
|
|
|
sub eax, 1
|
|
|
|
sbb edx, 0
|
|
|
|
sub esi, sizeof.CACHE_ITEM
|
|
|
|
; If the previous item exists...
|
|
|
|
cmp esi, [ebx+DISKCACHE.pointer]
|
|
|
|
jbe .find_chain_start
|
|
|
|
; ...describes the correct sector...
|
|
|
|
cmp [esi+CACHE_ITEM.SectorLo], eax
|
|
|
|
jnz .find_chain_start
|
|
|
|
cmp [esi+CACHE_ITEM.SectorHi], edx
|
|
|
|
jnz .find_chain_start
|
|
|
|
; ...and is modified...
|
|
|
|
cmp [esi+CACHE_ITEM.Status], CACHE_ITEM_MODIFIED
|
|
|
|
jnz .found_chain_start
|
|
|
|
; ...expand the chain one sector backwards and continue the loop at 6e.
|
|
|
|
; Otherwise, advance to step 7 if the previous item describes the correct sector
|
|
|
|
; but is not modified, and return to step 6b otherwise.
|
2014-09-03 16:11:19 +04:00
|
|
|
mov edi, 1
|
|
|
|
shl edi, cl
|
|
|
|
sub [.chain_start_pos], edi
|
2014-01-10 15:56:03 +04:00
|
|
|
jmp .look_backward
|
|
|
|
.found_chain_start:
|
|
|
|
; 7. Expand the chain forward.
|
|
|
|
; 7a. Prepare for the loop at 7b:
|
|
|
|
; set esi = pointer to current item, edx:eax = current sector.
|
|
|
|
mov esi, [.current_ptr]
|
|
|
|
mov eax, [esi+CACHE_ITEM.SectorLo]
|
|
|
|
mov edx, [esi+CACHE_ITEM.SectorHi]
|
|
|
|
.look_forward:
|
|
|
|
; 7b. First, look at the next item. If it describes the next sector:
|
|
|
|
; if it is modified, expand the chain with that sector and continue this step,
|
|
|
|
; if it is not modified, the chain is completed, so advance to step 8.
|
|
|
|
inc [.chain_size]
|
2011-10-15 01:38:50 +04:00
|
|
|
add eax, 1
|
|
|
|
adc edx, 0
|
2014-01-10 15:56:03 +04:00
|
|
|
add esi, sizeof.CACHE_ITEM
|
|
|
|
cmp esi, [.cache_end]
|
|
|
|
jae .find_chain_end
|
|
|
|
cmp [esi+CACHE_ITEM.SectorLo], eax
|
|
|
|
jnz .find_chain_end
|
|
|
|
cmp [esi+CACHE_ITEM.SectorHi], edx
|
|
|
|
jnz .find_chain_end
|
|
|
|
cmp [esi+CACHE_ITEM.Status], CACHE_ITEM_MODIFIED
|
|
|
|
jnz .found_chain_end
|
|
|
|
jmp .look_forward
|
|
|
|
.find_chain_end:
|
|
|
|
; 7c. Otherwise, call the lookup function.
|
|
|
|
call cache_lookup_read
|
|
|
|
; 7d. If the next sector is present in the cache and is modified,
|
|
|
|
; mark the chain as non-sequential and continue to step 7b.
|
|
|
|
jc .found_chain_end
|
|
|
|
cmp [esi+CACHE_ITEM.Status], CACHE_ITEM_MODIFIED
|
|
|
|
jnz .found_chain_end
|
|
|
|
mov [.sequential], 0
|
|
|
|
jmp .look_forward
|
|
|
|
.found_chain_end:
|
|
|
|
; 8. Decide whether the chain is sequential or scattered.
|
|
|
|
; Advance to step 9 for sequential chains, go to step 10 for scattered chains.
|
|
|
|
cmp [.sequential], 0
|
|
|
|
jz .write_non_sequential
|
|
|
|
.write_sequential:
|
|
|
|
; 9. Write a sequential chain to disk.
|
|
|
|
; 9a. Pass the entire chain to the driver.
|
|
|
|
mov eax, [.chain_start_ptr]
|
|
|
|
lea ecx, [.chain_size]
|
|
|
|
push ecx ; numsectors
|
|
|
|
pushd [eax+CACHE_ITEM.SectorHi] ; startsector
|
|
|
|
pushd [eax+CACHE_ITEM.SectorLo] ; startsector
|
2014-09-03 16:11:19 +04:00
|
|
|
push [.chain_start_pos+12] ; buffer
|
2014-01-10 15:56:03 +04:00
|
|
|
mov esi, [ebp+PARTITION.Disk]
|
|
|
|
mov al, DISKFUNC.write
|
|
|
|
call disk_call_driver
|
|
|
|
; 9b. If failed, pass the error code to the driver.
|
|
|
|
test eax, eax
|
|
|
|
jnz .return
|
|
|
|
; 9c. If succeeded, mark all sectors in the chain as not-modified,
|
|
|
|
; advance current item and number of items left to skip the chain.
|
|
|
|
mov esi, [.current_ptr]
|
|
|
|
mov eax, [.chain_size]
|
|
|
|
sub [.size_left], eax
|
2011-10-15 01:38:50 +04:00
|
|
|
@@:
|
2014-01-10 15:56:03 +04:00
|
|
|
mov [esi+CACHE_ITEM.Status], CACHE_ITEM_COPY
|
|
|
|
add esi, sizeof.CACHE_ITEM
|
|
|
|
dec eax
|
|
|
|
jnz @b
|
|
|
|
; 9d. Continue the main loop at step 2 if there are more sectors.
|
|
|
|
; Return success otherwise.
|
|
|
|
cmp [.size_left], 0
|
|
|
|
jnz .look
|
|
|
|
jmp .return0
|
|
|
|
.write_non_sequential:
|
|
|
|
; Write a non-sequential chain to the disk.
|
|
|
|
; 10. Allocate a temporary buffer.
|
|
|
|
; Use [.chain_size] sectors, but
|
|
|
|
; not greater than CACHE_MAX_ALLOC_SIZE bytes
|
|
|
|
; and not greater than half of free memory.
|
|
|
|
mov eax, [pg_data.pages_free]
|
|
|
|
shr eax, 1
|
|
|
|
jz .nomemory
|
|
|
|
cmp eax, CACHE_MAX_ALLOC_SIZE shr 12
|
|
|
|
jbe @f
|
|
|
|
mov eax, CACHE_MAX_ALLOC_SIZE shr 12
|
2011-10-15 01:38:50 +04:00
|
|
|
@@:
|
2014-09-03 16:11:19 +04:00
|
|
|
shl eax, 12
|
|
|
|
shr eax, cl
|
|
|
|
jz .nomemory
|
2014-01-10 15:56:03 +04:00
|
|
|
cmp eax, [.chain_size]
|
|
|
|
jbe @f
|
|
|
|
mov eax, [.chain_size]
|
|
|
|
@@:
|
|
|
|
mov [.iteration_size], eax
|
2014-09-03 16:11:19 +04:00
|
|
|
shl eax, cl
|
2014-01-10 15:56:03 +04:00
|
|
|
stdcall kernel_alloc, eax
|
2011-10-15 01:38:50 +04:00
|
|
|
test eax, eax
|
2014-01-10 15:56:03 +04:00
|
|
|
jz .nomemory
|
|
|
|
mov [.iteration_buffer], eax
|
|
|
|
.write_non_sequential_iteration:
|
|
|
|
; 11. Split the chain so that each iteration fits in the allocated buffer.
|
|
|
|
; Iteration size is the minimum of chain size and allocated size.
|
|
|
|
mov eax, [.chain_size]
|
|
|
|
cmp eax, [.iteration_size]
|
|
|
|
jae @f
|
|
|
|
mov [.iteration_size], eax
|
2011-10-15 01:38:50 +04:00
|
|
|
@@:
|
2014-01-10 15:56:03 +04:00
|
|
|
; 12. Prepare arguments for the driver.
|
|
|
|
mov esi, [.chain_start_ptr]
|
|
|
|
mov edi, [.iteration_buffer]
|
|
|
|
push [.iteration_size]
|
2011-10-15 01:38:50 +04:00
|
|
|
push esp ; numsectors
|
2014-01-10 15:56:03 +04:00
|
|
|
push [esi+CACHE_ITEM.SectorHi] ; startsector
|
|
|
|
push [esi+CACHE_ITEM.SectorLo] ; startsector
|
2011-10-15 01:38:50 +04:00
|
|
|
push edi ; buffer
|
2014-01-10 15:56:03 +04:00
|
|
|
; 13. Copy data from the cache to the temporary buffer,
|
|
|
|
; advancing chain_start pos/ptr and marking sectors as not-modified.
|
|
|
|
; 13a. Prepare for the loop: push number of sectors to process.
|
|
|
|
push [.iteration_size+20] ; temporary variable
|
|
|
|
.copy_loop:
|
|
|
|
; 13b. For each sector, copy the data.
|
|
|
|
; Note that edi is advanced automatically.
|
|
|
|
mov esi, [.chain_start_pos+24]
|
2014-09-03 16:11:19 +04:00
|
|
|
mov ecx, [ebx+DISKCACHE.sector_size_log]
|
|
|
|
mov eax, 1
|
|
|
|
shl eax, cl
|
|
|
|
mov ecx, eax
|
|
|
|
shr ecx, 2
|
2014-01-10 15:56:03 +04:00
|
|
|
rep movsd
|
2014-09-03 16:11:19 +04:00
|
|
|
mov ecx, eax ; keep for 13e
|
2014-01-10 15:56:03 +04:00
|
|
|
; 13c. Mark the item as not-modified.
|
|
|
|
mov esi, [.chain_start_ptr+24]
|
|
|
|
mov [esi+CACHE_ITEM.Status], CACHE_ITEM_COPY
|
|
|
|
; 13d. Check whether the next sector continues the chain.
|
|
|
|
; If so, advance to 13e. Otherwise, go to 13f.
|
|
|
|
mov eax, [esi+CACHE_ITEM.SectorLo]
|
|
|
|
mov edx, [esi+CACHE_ITEM.SectorHi]
|
|
|
|
add esi, sizeof.CACHE_ITEM
|
|
|
|
add eax, 1
|
|
|
|
adc edx, 0
|
|
|
|
cmp esi, [.cache_end+24]
|
|
|
|
jae .no_forward
|
|
|
|
cmp [esi+CACHE_ITEM.SectorLo], eax
|
|
|
|
jnz .no_forward
|
|
|
|
cmp [esi+CACHE_ITEM.SectorHi], edx
|
|
|
|
jnz .no_forward
|
|
|
|
; 13e. Increment position/pointer to the chain and
|
|
|
|
; continue the loop.
|
2014-09-03 16:11:19 +04:00
|
|
|
add [.chain_start_pos+24], ecx
|
2014-01-10 15:56:03 +04:00
|
|
|
mov [.chain_start_ptr+24], esi
|
|
|
|
dec dword [esp]
|
|
|
|
jnz .copy_loop
|
|
|
|
jmp .copy_done
|
|
|
|
.no_forward:
|
|
|
|
; 13f. Call the lookup function without adding to the cache.
|
|
|
|
; Update position/pointer with returned value.
|
2014-09-03 16:11:19 +04:00
|
|
|
; Note: for the last sector in the chain, edi/esi may contain
|
2014-01-10 15:56:03 +04:00
|
|
|
; garbage; we are not going to use them in this case.
|
2014-09-03 16:11:19 +04:00
|
|
|
push edi
|
2014-01-10 15:56:03 +04:00
|
|
|
call cache_lookup_read
|
2014-09-03 16:11:19 +04:00
|
|
|
mov [.chain_start_pos+28], edi
|
|
|
|
mov [.chain_start_ptr+28], esi
|
|
|
|
pop edi
|
2014-01-10 15:56:03 +04:00
|
|
|
dec dword [esp]
|
|
|
|
jnz .copy_loop
|
|
|
|
.copy_done:
|
|
|
|
; 13g. Restore the stack after 13a.
|
|
|
|
pop ecx
|
|
|
|
; 14. Call the driver.
|
|
|
|
mov esi, [ebp+PARTITION.Disk]
|
2011-10-15 01:38:50 +04:00
|
|
|
mov al, DISKFUNC.write
|
|
|
|
call disk_call_driver
|
2014-01-10 15:56:03 +04:00
|
|
|
pop ecx ; numsectors
|
|
|
|
; 15. If the driver has returned an error, free the buffer allocated at step 10
|
|
|
|
; and pass the error to the caller.
|
|
|
|
; Otherwise, remove the processed part from the chain and continue iterations
|
|
|
|
; starting in step 11 if there are more data to process.
|
|
|
|
test eax, eax
|
|
|
|
jnz .nonsequential_error
|
|
|
|
sub [.chain_size], ecx
|
|
|
|
jnz .write_non_sequential_iteration
|
|
|
|
; 16. The chain is written. Free the temporary buffer
|
|
|
|
; and continue the loop at step 2.
|
|
|
|
stdcall kernel_free, [.iteration_buffer]
|
|
|
|
mov esi, [.current_ptr]
|
|
|
|
jmp .look_next
|
|
|
|
.nonsequential_error:
|
|
|
|
push eax
|
|
|
|
stdcall kernel_free, [.iteration_buffer+4]
|
|
|
|
pop eax
|
|
|
|
jmp .return
|
|
|
|
.nomemory:
|
|
|
|
mov eax, DISK_STATUS_NO_MEMORY
|
|
|
|
jmp .return
|
2011-10-15 01:38:50 +04:00
|
|
|
endp
|
|
|
|
|
|
|
|
; This internal function is called from disk_add to initialize the caching for
|
|
|
|
; a new DISK.
|
|
|
|
; The algorithm is inherited from getcache.inc: take 1/32 part of the available
|
|
|
|
; physical memory, round down to 8 pages, limit by 128K from below and by 1M
|
|
|
|
; from above. Reserve 1/8 part of the cache for system data and 7/8 for app
|
|
|
|
; data.
|
|
|
|
; After the size is calculated, but before the cache is allocated, the device
|
|
|
|
; driver can adjust the size. In particular, setting size to zero disables
|
|
|
|
; caching: there is no sense in a cache for a ramdisk. In fact, such action
|
|
|
|
; is most useful example of a non-trivial adjustment.
|
|
|
|
; esi = pointer to DISK structure
|
|
|
|
disk_init_cache:
|
2014-09-03 16:11:19 +04:00
|
|
|
; 1. Verify sector size. The code requires it to be a power of 2 not less than 4.
|
|
|
|
; In the name of sanity check that sector size is not too small or too large.
|
|
|
|
bsf ecx, [esi+DISK.MediaInfo.SectorSize]
|
|
|
|
jz .invalid_sector_size
|
|
|
|
mov eax, 1
|
|
|
|
shl eax, cl
|
|
|
|
cmp eax, [esi+DISK.MediaInfo.SectorSize]
|
|
|
|
jnz .invalid_sector_size
|
|
|
|
cmp ecx, 6
|
|
|
|
jb .invalid_sector_size
|
|
|
|
cmp ecx, 14
|
|
|
|
jbe .normal_sector_size
|
|
|
|
.invalid_sector_size:
|
|
|
|
DEBUGF 1,'K : sector size %x is invalid\n',[esi+DISK.MediaInfo.SectorSize]
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
.normal_sector_size:
|
|
|
|
mov [esi+DISK.SysCache.sector_size_log], ecx
|
|
|
|
mov [esi+DISK.AppCache.sector_size_log], ecx
|
|
|
|
; 2. Calculate the suggested cache size.
|
|
|
|
; 2a. Get the size of free physical memory in pages.
|
2011-10-15 01:38:50 +04:00
|
|
|
mov eax, [pg_data.pages_free]
|
2014-09-03 16:11:19 +04:00
|
|
|
; 2b. Use the value to calculate the size.
|
2011-10-15 01:38:50 +04:00
|
|
|
shl eax, 12 - 5 ; 1/32 of it in bytes
|
|
|
|
and eax, -8*4096 ; round down to the multiple of 8 pages
|
2014-09-03 16:11:19 +04:00
|
|
|
; 2c. Force lower and upper limits.
|
2011-10-15 01:38:50 +04:00
|
|
|
cmp eax, 1024*1024
|
|
|
|
jb @f
|
|
|
|
mov eax, 1024*1024
|
|
|
|
@@:
|
|
|
|
cmp eax, 128*1024
|
|
|
|
ja @f
|
|
|
|
mov eax, 128*1024
|
|
|
|
@@:
|
2014-09-03 16:11:19 +04:00
|
|
|
; 2d. Give a chance to the driver to adjust the size.
|
2011-10-15 01:38:50 +04:00
|
|
|
push eax
|
|
|
|
mov al, DISKFUNC.adjust_cache_size
|
|
|
|
call disk_call_driver
|
|
|
|
; Cache size calculated.
|
|
|
|
mov [esi+DISK.cache_size], eax
|
|
|
|
test eax, eax
|
|
|
|
jz .nocache
|
2014-09-03 16:11:19 +04:00
|
|
|
; 3. Allocate memory for the cache.
|
|
|
|
; 3a. Call the allocator.
|
2011-10-15 01:38:50 +04:00
|
|
|
stdcall kernel_alloc, eax
|
|
|
|
test eax, eax
|
|
|
|
jnz @f
|
2014-09-03 16:11:19 +04:00
|
|
|
; 3b. If it failed, say a message and return with eax = 0.
|
2011-10-15 01:38:50 +04:00
|
|
|
dbgstr 'no memory for disk cache'
|
|
|
|
jmp .nothing
|
|
|
|
@@:
|
2014-09-03 16:11:19 +04:00
|
|
|
; 4. Fill two DISKCACHE structures.
|
2011-10-15 01:38:50 +04:00
|
|
|
mov [esi+DISK.SysCache.pointer], eax
|
2014-01-10 15:56:03 +04:00
|
|
|
lea ecx, [esi+DISK.CacheLock]
|
2011-10-15 01:38:50 +04:00
|
|
|
call mutex_init
|
|
|
|
; The following code is inherited from getcache.inc.
|
|
|
|
mov edx, [esi+DISK.SysCache.pointer]
|
|
|
|
and [esi+DISK.SysCache.search_start], 0
|
|
|
|
and [esi+DISK.AppCache.search_start], 0
|
|
|
|
mov eax, [esi+DISK.cache_size]
|
|
|
|
shr eax, 3
|
|
|
|
mov [esi+DISK.SysCache.data_size], eax
|
|
|
|
add edx, eax
|
|
|
|
imul eax, 7
|
|
|
|
mov [esi+DISK.AppCache.data_size], eax
|
|
|
|
mov [esi+DISK.AppCache.pointer], edx
|
|
|
|
|
|
|
|
mov eax, [esi+DISK.SysCache.data_size]
|
2014-09-03 16:11:19 +04:00
|
|
|
call calculate_cache_slots
|
2011-10-15 01:38:50 +04:00
|
|
|
add eax, [esi+DISK.SysCache.pointer]
|
|
|
|
mov [esi+DISK.SysCache.data], eax
|
|
|
|
mov [esi+DISK.SysCache.sad_size], ecx
|
|
|
|
|
|
|
|
push edi
|
|
|
|
mov edi, [esi+DISK.SysCache.pointer]
|
2013-10-29 21:45:24 +04:00
|
|
|
lea ecx, [(ecx+1)*3]
|
2011-10-15 01:38:50 +04:00
|
|
|
xor eax, eax
|
|
|
|
rep stosd
|
|
|
|
pop edi
|
|
|
|
|
|
|
|
mov eax, [esi+DISK.AppCache.data_size]
|
2014-09-03 16:11:19 +04:00
|
|
|
call calculate_cache_slots
|
2011-10-15 01:38:50 +04:00
|
|
|
add eax, [esi+DISK.AppCache.pointer]
|
|
|
|
mov [esi+DISK.AppCache.data], eax
|
|
|
|
mov [esi+DISK.AppCache.sad_size], ecx
|
|
|
|
|
|
|
|
push edi
|
|
|
|
mov edi, [esi+DISK.AppCache.pointer]
|
2013-10-29 21:45:24 +04:00
|
|
|
lea ecx, [(ecx+1)*3]
|
2011-10-15 01:38:50 +04:00
|
|
|
xor eax, eax
|
|
|
|
rep stosd
|
|
|
|
pop edi
|
|
|
|
|
2014-09-03 16:11:19 +04:00
|
|
|
; 5. Return with nonzero al.
|
2011-10-15 01:38:50 +04:00
|
|
|
mov al, 1
|
2014-09-03 16:11:19 +04:00
|
|
|
; 6. Return.
|
2011-10-15 01:38:50 +04:00
|
|
|
.nothing:
|
|
|
|
ret
|
|
|
|
; No caching is required for this driver. Zero cache pointers and return with
|
|
|
|
; nonzero al.
|
|
|
|
.nocache:
|
|
|
|
mov [esi+DISK.SysCache.pointer], eax
|
|
|
|
mov [esi+DISK.AppCache.pointer], eax
|
|
|
|
mov al, 1
|
|
|
|
ret
|
|
|
|
|
2014-09-03 16:11:19 +04:00
|
|
|
calculate_cache_slots:
|
2013-01-14 13:24:25 +04:00
|
|
|
push eax
|
2014-09-03 16:11:19 +04:00
|
|
|
mov ecx, [esi+DISK.MediaInfo.SectorSize]
|
|
|
|
add ecx, sizeof.CACHE_ITEM
|
|
|
|
xor edx, edx
|
|
|
|
div ecx
|
|
|
|
mov ecx, eax
|
|
|
|
imul eax, [esi+DISK.MediaInfo.SectorSize]
|
|
|
|
sub [esp], eax
|
2013-01-14 13:24:25 +04:00
|
|
|
pop eax
|
|
|
|
dec ecx
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2011-10-15 01:38:50 +04:00
|
|
|
; This internal function is called from disk_media_dereference to free the
|
|
|
|
; allocated cache, if there is one.
|
|
|
|
; esi = pointer to DISK structure
|
|
|
|
disk_free_cache:
|
|
|
|
; The algorithm is straightforward.
|
|
|
|
mov eax, [esi+DISK.SysCache.pointer]
|
|
|
|
test eax, eax
|
|
|
|
jz .nothing
|
|
|
|
stdcall kernel_free, eax
|
|
|
|
.nothing:
|
|
|
|
ret
|
2012-04-23 13:19:34 +04:00
|
|
|
|
|
|
|
; This function flushes all modified data from both caches for the given DISK.
|
|
|
|
; esi = pointer to DISK
|
|
|
|
disk_sync:
|
|
|
|
; The algorithm is straightforward.
|
2014-01-10 15:56:03 +04:00
|
|
|
cmp [esi+DISK.SysCache.pointer], 0
|
|
|
|
jz .nothing
|
|
|
|
lea ecx, [esi+DISK.CacheLock]
|
|
|
|
call mutex_lock
|
|
|
|
push ebx
|
2012-04-23 13:19:34 +04:00
|
|
|
push esi ; for second write_cache64
|
|
|
|
push esi ; for first write_cache64
|
2014-01-10 15:56:03 +04:00
|
|
|
lea ebx, [esi+DISK.SysCache]
|
2012-04-23 13:19:34 +04:00
|
|
|
call write_cache64
|
2014-01-10 15:56:03 +04:00
|
|
|
add ebx, DISK.AppCache - DISK.SysCache
|
2012-04-23 13:19:34 +04:00
|
|
|
call write_cache64
|
2014-01-10 15:56:03 +04:00
|
|
|
pop ebx
|
|
|
|
lea ecx, [esi+DISK.CacheLock]
|
|
|
|
call mutex_unlock
|
|
|
|
.nothing:
|
|
|
|
mov al, DISKFUNC.flush
|
|
|
|
call disk_call_driver
|
2012-04-23 13:19:34 +04:00
|
|
|
ret
|