📄 memory.asm
字号:
;
shl esi,12 ;Get a real address again and
mov di,si ;use it as both the memory
mov cx,si ;address to return and the handle.
shr esi,16
mov bx,si
clc
jmp @@exit
;
@@error: stc
@@exit:
lss esp,f[esp]
pushf
add d[RawStackPos],RawStackDif
popf
popm eax,edx,ebp,ds,es
ret
assume ds:_cwDPMIEMU
RAWResMemory endp
;-------------------------------------------------------------------------------
;
;Release block of memory using RAW/VCPI.
;
;On Entry:-
;
;SI:DI - handle (Base linear address).
;
RAWRelMemory proc near
call RAWCopyCheck
;
pushad
pushm ds,es
;
shl esi,16 ;Get block base address.
mov si,di
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
pushm ebx,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
;
;Check for a legal address.
;
test esi,4095
jnz @@error
cmp esi,LinearBase ;inside logical memory map?
jc @@error
cmp esi,LinearLimit
jnc @@error
shr esi,12 ;Get page number.
mov eax,es:d[1024*4096*1022+esi*4]
and eax,MEM_MASK
cmp eax,MEM_START
jnz @@error
;
;Mark all this blocks pages as free.
;
inc FreePages
dec medAllocPages
;
and es:d[1024*4096*1022+esi*4],not MEM_MASK
or es:d[1024*4096*1022+esi*4],MEM_FREE
and es:d[1024*4096*1023+esi*4],not (1 shl 6)
inc esi
mov ecx,LinearLimit
shr ecx,12
sub ecx,esi ;Get pages remaining.
jz @@1
@@0: mov eax,es:d[1024*4096*1022+esi*4]
and eax,MEM_MASK
cmp eax,MEM_END
jnz @@1
inc FreePages
dec medAllocPages
and es:d[1024*4096*1022+esi*4],not MEM_MASK
or es:d[1024*4096*1022+esi*4],MEM_FREE
and es:d[1024*4096*1023+esi*4],not (1 shl 6)
inc esi
dec ecx
jnz @@0
;
@@1: call EMUCR3Flush
;
clc
jmp @@exit
;
@@error: stc
@@exit:
lss esp,f[esp]
pushf
add d[RawStackPos],RawStackDif
popf
popm ds,es
popad
ret
assume ds:_cwDPMIEMU
RAWRelMemory endp
;-------------------------------------------------------------------------------
;
;Reset dirty bit for specified memory.
;
;On Entry:
;
;BX:CX Starting linear address of pages to discard
;SI:DI Number of bytes to discard
;
;On Exit:
;
;Carry clear, all other registers preserved.
;
;-------------------------------------------------------------------------------
RawDiscardPages proc near
pushm ds,es
pushad
mov ax,KernalDS
mov ds,ax
assume ds:_cwRaw
mov ax,KernalZero
mov es,ax
;
;Get base address.
;
shl ebx,16
mov bx,cx
;
;Get length.
;
shl esi,16
mov si,di
;
;Round start up a page.
;
mov eax,ebx
add ebx,4095
and ebx,not 4095
sub eax,ebx
neg eax
cmp eax,esi
jnc @@8
sub esi,eax
;
;Round length down a page.
;
and esi,not 4095
or esi,esi
jz @@8
;
;Get page values.
;
mov edx,ebx
shr ebx,12
shr esi,12
mov ecx,esi
;
;Modify all page tables.
;
mov esi,4096*1024*1023 ;base of page alias memory.
@@0: cmp edx,LinearBase
jc @@1
cmp edx,LinearLimit
jnc @@2
and es:d[esi+ebx*4],not ((1 shl 6)+(1 shl 11)) ;clear dirty & disk bits.
@@1: add edx,4096
inc ebx
dec ecx
jnz @@0
;
@@2: call EMUCR3Flush ;update page cache.
;
@@8: clc
popad
assume ds:_cwDPMIEMU
popm ds,es
ret
RawDiscardPages endp
;-------------------------------------------------------------------------------
;
;Map physical memory into address space and return linear access address.
;
;BX:CX Physical address of memory
;SI:DI Size of region to map in bytes
;
;Returns
;
;Carry set on error else,
;
;BX:CX Linear address that can be used to access the physical memory.
;
RawMapPhys2Lin proc near
pushm ds,es
pushm eax,edx,esi,edi,ebp
mov ax,KernalDS
mov ds,ax
assume ds:_cwRaw
mov ax,KernalZero
mov es,ax
;
;Get physical address as 32-bit & check for none-page boundary.
;
shl ebx,16
mov bx,cx
test ebx,4095
jnz @@9
;
;Get length as number of pages.
;
shl esi,16
mov si,di
add esi,4095
shr esi,12
test esi,esi
jz @@9
; MED 06/13/96, remove 4M restriction
;; MED 04/18/96, don't allow more than a 4M mapping (not supported by CW)
; cmp esi,400h
; ja @@9
;
;Deal with addresses below 1 Meg.
;
mov edi,ebx
mov eax,esi
shl eax,12
add eax,edi
dec eax
cmp eax,100000h+10000h
jc @@8
;
;Find first un-used physical mapping space.
;
mov ebp,PageDirLinear
mov eax,1021
@@0:
; MED 04/18/96
mov ecx,es:[ebp+eax*4]
and ecx,(NOT 4095) ; get linear address
cmp ecx,ebx ; see if matches desired linear address
jne med2 ; no, continue as before
mov edi,eax
shl edi,22 ; convert index to 4M space
jmp @@8
med2:
cmp es:d[ebp+eax*4],0
jz @@1
; dec eax
sub eax,1
jc @@9
jmp @@0
;
;Work out how many page tables we need and set first tables index.
;
@@1: mov ecx,esi
shr ecx,10
sub eax,ecx
inc ecx
;
;Make sure we can get enough memory for physical page tables.
;
call PhysicalGetPages
add edx,NoneLockedPages
cmp edx,ecx
jc @@9
;
;Put all the page tables into place.
;
lea edi,[ebp+eax*4]
push edi
mov ebp,ecx
@@2: call PhysicalGetPage
jnc @@3
call UnMapPhysical
jc @@10
@@3: and ecx,1 ;put user bits in useful place.
shl ecx,10
and edx,0FFFFFFFFh-4095 ;lose user bits.
or edx,111b ;present+user+write.
or edx,ecx ;set use flags.
mov es:d[edi],edx ;store this tables address.
push edi
sub edi,PageDIRLinear
add edi,PageAliasLinear ;get alias table address.
mov es:d[edi],edx ;setup in alias table as well.
pop edi
;
;Clear this page to 0.
;
pushm ecx,edi
sub edi,PageDIRLinear
shl edi,10
add edi,1024*4096*1023 ;base of page alias's.
mov ecx,4096/4
xor eax,eax
cld
rep stosd
popm ecx,edi
;
add edi,4
dec ebp
jnz @@2
pop edi
;
;Now map specified physical address range into place.
;
sub edi,PageDIRLinear
shl edi,10 ;start of first page table
push edi
add edi,1024*4096*1023
or ebx,111b
@@4: mov es:[edi],ebx
add edi,4
add ebx,4096
dec esi
jnz @@4
pop edi
;
;Return linear address to caller.
;
shl edi,12-2
@@8: xor ecx,ecx
xor ebx,ebx
mov cx,di
shr edi,16
mov bx,di
clc
jmp @@11
;
@@10: pop edi
@@9: stc
@@11: popm eax,edx,esi,edi,ebp
assume ds:_cwDPMIEMU
popm ds,es
ret
RawMapPhys2Lin endp
;-------------------------------------------------------------------------------
;
;Un-Do a physical to linear address mapping.
;
RawUnMapPhys2Lin proc near
clc
ret
RawUnMapPhys2Lin endp
;-------------------------------------------------------------------------------
RAWLockMemory proc near
call RAWCopyCheck
;
pushm eax,ebx,ecx,edx,esi,edi,ebp,ds,es,fs,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
pushm ebx,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 @@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]
@@04: cmp eax,LinearBase ;must be in our memory pool.
jc @@05
cmp eax,LinearLimit
jnc @@05
push eax
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -