memory.asm
字号:
pushf
cli
mov edx,d[RawStackPos]
sub d[RawStackPos],RawStackDif
popf
mov ax,KernalSS
mov ss,ax
mov esp,edx
add ecx,4+4+4
push ebx
push ecx
sub ecx,4+4+4
push es
mov es,bx
mov edx,es:[ecx]
mov ebx,es:[ecx+4+4]
mov ecx,es:[ecx+4]
pop es
;
shl ebx,16
mov bx,cx
shl esi,16
mov si,di
add esi,ebx
and ebx,not 4095 ;round down to nearest page.
mov d[_LM_BlockBase],ebx
add esi,4095
and esi,not 4095 ;round up to next page.
dec esi
mov d[_LM_BlockEnd],esi ;store address of last page.
;
;Check if start above end now!
;
mov eax,d[_LM_BlockBase]
cmp eax,d[_LM_BlockEnd]
jnc mem7_10
;
;Count number of pages we need for this range.
;
mov d[_LM_Needed],0
mov eax,NoneLockedPages
mov d[_LM_Got],eax
mov eax,d[_LM_BlockBase]
mem7_04: cmp eax,LinearBase ;must be in our memory pool.
jc mem7_05
cmp eax,LinearLimit
jnc mem7_05
push eax
call GetPageStatus
pop eax
jc mem7_15 ;ignore not present tables.
test edx,1
jz mem7_005 ;already present.
call RawPageLocked ;locked page?
jnz mem7_05
dec d[_LM_Got] ;reduce available pages.
jmp mem7_05
mem7_005: inc d[_LM_Needed]
mem7_05: add eax,4096
cmp eax,d[_LM_BlockEnd] ;done them all yet?
jc mem7_04
;
;Check if we actually need any more pages to lock this region.
;
cmp d[_LM_Needed],0
jz mem7_OK
;
;If VMM isn't active then pages can always be locked assumeing they exist.
;
cmp VMMHandle,0
jnz mem7_VMM
mov eax,d[_LM_Needed]
cmp eax,d[_LM_Got]
jc mem7_OK
jz mem7_OK
jmp mem7_15
;
;VMM is active and pages are required so we need to make sure enough pages are
;left for swapping.
;
mem7_VMM: mov eax,d[_LM_Needed]
add eax,16 ;arbitrary safety buffer.
cmp eax,d[_LM_Got]
jc mem7_OK
jz mem7_OK
jmp mem7_15
;
;Enough free pages so lock the region.
;
mem7_OK: mov eax,d[_LM_BlockBase]
mem7_4: cmp eax,LinearBase ;must be in our memory pool.
jc mem7_5
cmp eax,LinearLimit
jnc mem7_5
push eax
call GetPageStatus
pop eax
jc mem7_15 ;ignore not present tables.
test edx,1 ;is it present?
jnz mem7_6
;
mem7_11: ;Need to allocate a physical page first.
;
push eax
call UnMapPhysical
pop eax
jc mem7_15 ;this shouldn't happen.
mov LinearEntry,eax
shr LinearEntry,12 ;store page number to allocate at.
push eax
call MapPhysical ;map this page in.
pop eax
;
mem7_6: ;Now mark this page as locked.
;
call RawLockPage
;
mem7_5: add eax,4096
cmp eax,d[_LM_BlockEnd] ;done them all yet?
jc mem7_4
;
mem7_10: clc
jmp mem7_1
;
mem7_15: stc
;
mem7_1:
lss esp,f[esp]
pushf
add d[RawStackPos],RawStackDif
popf
pop gs
pop fs
pop es
pop ds
pop ebp
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
ret
assume ds:_cwDPMIEMU
RAWLockMemory endp
;-------------------------------------------------------------------------------
RAWUnLockMemory proc near
;
push eax
push ebx
push ecx
push edx
push esi
push edi
push ebp
push ds
push es
push fs
push gs
mov ax,KernalDS
mov ds,ax
assume ds:_cwRaw
;
push ebx
push ecx
push edx
xor ebx,ebx
mov bx,ss
mov ecx,esp
pushf
cli
mov edx,d[RawStackPos]
sub d[RawStackPos],RawStackDif
popf
mov ax,KernalSS
mov ss,ax
mov esp,edx
add ecx,4+4+4
push ebx
push ecx
sub ecx,4+4+4
push es
mov es,bx
mov edx,es:[ecx]
mov ebx,es:[ecx+4+4]
mov ecx,es:[ecx+4]
pop es
shl ebx,16
mov bx,cx
shl esi,16
mov si,di
add esi,ebx
and ebx,0FFFFFFFFh-4095 ;round down to nearest page.
mov d[_LM_BlockBase],ebx
add esi,4095
and esi,0FFFFFFFFh-4095 ;round up to next page.
dec esi
mov d[_LM_BlockEnd],esi ;store address of last page.
;
;Now run through all pages in this range un-locking them.
;
mov eax,d[_LM_BlockBase]
mem8_4: cmp eax,LinearBase ;must be in our memory pool.
jc mem8_5
cmp eax,LinearLimit
jnc mem8_5
push eax
call GetPageStatus
pop eax
jc mem8_5 ;ignore not present tables.
test edx,1 ;is it present?
jz mem8_5
call RawUnLockPage ;unlock the page.
mem8_5: add eax,4096
cmp eax,d[_LM_BlockEnd] ;done them all yet?
jc mem8_4
clc
mem8_1:
lss esp,f[esp]
pushf
add d[RawStackPos],RawStackDif
popf
pop gs
pop fs
pop es
pop ds
pop ebp
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
ret
assume ds:_cwDPMIEMU
RAWUnLockMemory endp
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
RAWGetMemoryMax proc near
;
;Work out biggest memory block remaining.
;
;
push eax
push ecx
push edx
push esi
push edi
push ebp
push ds
push es
mov ax,KernalDS ;make data addresable.
mov ds,ax
assume ds:_cwRaw
push ebx
push ecx
push edx
xor ebx,ebx
mov bx,ss
mov ecx,esp
pushf
cli
mov edx,d[RawStackPos]
sub d[RawStackPos],RawStackDif
popf
mov ax,KernalSS
mov ss,ax
mov esp,edx
add ecx,4+4+4
push ebx
push ecx
sub ecx,4+4+4
push es
mov es,bx
mov edx,es:[ecx]
mov ebx,es:[ecx+4+4]
mov ecx,es:[ecx+4]
pop es
mov ax,KernalZero
mov es,ax
;
;Look in existing memory first.
;
mov edx,LinearBase ;Get starting point.
mov ecx,LinearLimit
sub ecx,edx ;Get memory present.
shr ecx,12 ;Get pages.
shr edx,12 ;Get page number.
xor edi,edi ;Clear flag.
xor ebp,ebp ;Clear biggest so far.
;
mem9_l0: ;Look for a bigest block of free memory.
;
mov eax,DWORD PTR es:[(1024*4096*1022)+edx*4] ;Get page details.
and eax,MEM_MASK
cmp eax,MEM_FREE ;Free block?
jnz mem9_l2
or edi,edi ;Got any yet?
jnz mem9_l1
mov esi,edx ;Get base page number.
mem9_l1: inc edi
cmp edi,ebp ;Biggest yet?
jc mem9_l3
mov ebx,esi ;Get base.
mov ebp,edi ;Get size.
jmp mem9_l3
mem9_l2: xor edi,edi
mem9_l3: inc edx ;Next page.
dec ecx
jnz mem9_l0
;
;See if biggest block found is the last block in the map.
;
xor edx,edx ;reset end of chain value.
or edi,edi ;last block free?
jz mem9_l4
cmp ebx,esi ;same base?
jnz mem9_l4
mov edx,ebp ;setup extra block size.
mov ebp,0 ;reset normal block size.
jmp mem9_l5
;
mem9_l4: ;Get size of the last block in the memory map.
;
mov eax,LinearBase
mov esi,LinearLimit
shr esi,12
dec esi
shr eax,12
mov ecx,esi
sub ecx,eax
mem9_l6: jecxz mem9_l5
mov eax,DWORD PTR es:[(1024*4096*1022)+esi*4] ;Get page details.
and eax,MEM_MASK
cmp eax,MEM_FREE ;Free block?
jnz mem9_l5
dec esi
inc edx
dec ecx
jmp mem9_l6
;
mem9_l5: ;See what extra memory we can get hold of.
;
mov ebx,edx
call PhysicalGetPages ;Get extra memory available.
mov ecx,ebx ;Save origional value.
add ebx,edx ;update extra memory value.
;
;See how many pages of real memory would be lost to page tables.
;
mov eax,LinearLimit
shr eax,12
add eax,edx
shr eax,10
mov edx,LinearLimit
shr edx,12
dec edx
shr edx,10
sub eax,edx
add eax,eax ;Page + Det
sub ebx,eax
;
;See what extra memory the VMM can get hold of.
;
cmp VMMHandle,0
jz mem9_l8
mov ebx,ecx
push ebx
push ebp
;
;Get free disk space remaining.
;
mov dl,VMMName ;get drive letter for this media.
sub dl,'A' ;make it real.
inc dl ;adjust for current type select.
mov ah,36h ;get free space.
int 21h ;/
xor edx,edx
cmp ax,-1 ;invalid drive?
jz mem9_l7
mul cx ;Get bytes per cluster.
mul bx ;Get bytes available.
shl edx,16
mov dx,ax
mem9_l7: ;
;Get current swap file size.
;
push edx
mov bx,VMMHandle
mov ax,4202h
xor cx,cx
mov dx,cx
int 21h
shl edx,16
mov dx,ax
pop eax
add edx,eax
and edx,not 65535
shr edx,12
pop ebp
pop ebx
;
;Work out how much of the VMM space is extra.
;
mov eax,LinearLimit
sub eax,LinearBase
shr eax,12
sub edx,eax
add ebx,edx
;
mem9_l8: ;Check which block is bigger and exit.
;
push ecx
mov eax,ebx
shl eax,12
mov ecx,LinearLimit
sub ecx,LinearBase
sub eax,ecx
js mem9_l89
cmp eax,MaxMemLin
jc mem9_l89
mov ebx,MaxMemLin
sub ebx,ecx
shr ebx,12
mem9_l89: pop ecx
cmp ebx,ebp
jnc mem9_l9
mov ebx,ebp
mem9_l9: shl ebx,12
clc
lss esp,f[esp]
pushf
add d[RawStackPos],RawStackDif
popf
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -