memory.asm
字号:
rep movsd
pop ds
popad
;
;Release current block.
;
pushad
shl esi,12
mov di,si
shr esi,16
call RawRelMemory
popad
;
;Use new block in place of origional.
;
mov esi,ebx
shr esi,12
;
mem2_RetNewAddr: ;Return possibly new address/handle to caller.
;
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 mem2_exit
;
mem2_error: stc
mem2_exit:
lss esp,f[esp]
pushf
add d[RawStackPos],RawStackDif
popf
pop es
pop ds
pop ebp
pop edx
pop eax
ret
assume ds:_cwDPMIEMU
RAWResMemory endp
;-------------------------------------------------------------------------------
;
;Release block of memory using RAW/VCPI.
;
;On Entry:-
;
;SI:DI - handle (Base linear address).
;
RAWRelMemory proc near
;
pushad
push ds
push 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
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
;
;Check for a legal address.
;
test esi,4095
jnz mem3_error
cmp esi,LinearBase ;inside logical memory map?
jc mem3_error
cmp esi,LinearLimit
jnc mem3_error
shr esi,12 ;Get page number.
mov eax,DWORD PTR es:[1024*4096*1022+esi*4]
and eax,MEM_MASK
cmp eax,MEM_START
jnz mem3_error
;
;Mark all this blocks pages as free.
;
inc FreePages
dec medAllocPages
;
and DWORD PTR es:[1024*4096*1022+esi*4],not MEM_MASK
or DWORD PTR es:[1024*4096*1022+esi*4],MEM_FREE
and DWORD PTR es:[1024*4096*1023+esi*4],not (1 shl 6)
inc esi
mov ecx,LinearLimit
shr ecx,12
sub ecx,esi ;Get pages remaining.
jz mem3_1
mem3_0: mov eax,DWORD PTR es:[1024*4096*1022+esi*4]
and eax,MEM_MASK
cmp eax,MEM_END
jnz mem3_1
inc FreePages
dec medAllocPages
and DWORD PTR es:[1024*4096*1022+esi*4],not MEM_MASK
or DWORD PTR es:[1024*4096*1022+esi*4],MEM_FREE
and DWORD PTR es:[1024*4096*1023+esi*4],not (1 shl 6)
inc esi
dec ecx
jnz mem3_0
;
mem3_1: call EMUCR3Flush
;
clc
jmp mem3_exit
;
mem3_error: stc
mem3_exit:
lss esp,f[esp]
pushf
add d[RawStackPos],RawStackDif
popf
pop es
pop ds
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
push ds
push 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 mem4_8
sub esi,eax
;
;Round length down a page.
;
and esi,not 4095
or esi,esi
jz mem4_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.
mem4_0: cmp edx,LinearBase
jc mem4_1
cmp edx,LinearLimit
jnc mem4_2
and DWORD PTR es:[esi+ebx*4],not ((1 shl 6)+(1 shl 11)) ;clear dirty & disk bits.
mem4_1: add edx,4096
inc ebx
dec ecx
jnz mem4_0
;
mem4_2: call EMUCR3Flush ;update page cache.
;
mem4_8: clc
popad
assume ds:_cwDPMIEMU
pop es
pop ds
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
push ds
push es
push eax
push edx
push esi
push edi
push 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 mem5_9
;
;Get length as number of pages.
;
shl esi,16
mov si,di
add esi,4095
shr esi,12
test esi,esi
jz mem5_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 mem5_8
;
;Find first un-used physical mapping space.
;
mov ebp,PageDirLinear
mov eax,1021
mem5_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 med2c ; no, continue as before
mov edi,eax
shl edi,22 ; convert index to 4M space
jmp mem5_8
med2c:
cmp DWORD PTR es:[ebp+eax*4],0
jz mem5_1
; dec eax
sub eax,1
jc mem5_9
jmp mem5_0
;
;Work out how many page tables we need and set first tables index.
;
mem5_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 mem5_9
;
;Put all the page tables into place.
;
lea edi,[ebp+eax*4]
push edi
mov ebp,ecx
mem5_2: call PhysicalGetPage
jnc mem5_3
call UnMapPhysical
jc mem5_10
mem5_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 DWORD PTR es:[edi],edx ;store this tables address.
push edi
sub edi,PageDIRLinear
add edi,PageAliasLinear ;get alias table address.
mov DWORD PTR es:[edi],edx ;setup in alias table as well.
pop edi
;
;Clear this page to 0.
;
push ecx
push 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
pop edi
pop ecx
;
add edi,4
dec ebp
jnz mem5_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
mem5_4: mov es:[edi],ebx
add edi,4
add ebx,4096
dec esi
jnz mem5_4
pop edi
;
;Return linear address to caller.
;
shl edi,12-2
mem5_8: xor ecx,ecx
xor ebx,ebx
mov cx,di
shr edi,16
mov bx,di
clc
jmp mem5_11
;
mem5_10: pop edi
mem5_9: stc
mem5_11:
pop ebp
pop edi
pop esi
pop edx
pop eax
assume ds:_cwDPMIEMU
pop es
pop ds
ret
RawMapPhys2Lin endp
;-------------------------------------------------------------------------------
;
;Un-Do a physical to linear address mapping.
;
RawUnMapPhys2Lin proc near
clc
ret
RawUnMapPhys2Lin endp
;-------------------------------------------------------------------------------
RAWLockMemory 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -