2024-07-07 09:24:51 +00:00
|
|
|
;; File: pmm.inc
|
2024-07-06 13:19:35 +00:00
|
|
|
|
2024-07-09 08:35:47 +00:00
|
|
|
macro ALIGN reg {
|
2024-07-10 06:04:40 +00:00
|
|
|
local ..end
|
2024-07-09 08:35:47 +00:00
|
|
|
|
|
|
|
push reg
|
|
|
|
and reg, 0xFFF
|
|
|
|
pop reg
|
2024-07-10 06:04:40 +00:00
|
|
|
jz ..end
|
2024-07-09 08:35:47 +00:00
|
|
|
and reg, 0xFFFFF000
|
|
|
|
add reg, 0x1000
|
2024-07-10 06:04:40 +00:00
|
|
|
..end:
|
2024-07-09 08:35:47 +00:00
|
|
|
}
|
|
|
|
|
2024-07-07 09:24:51 +00:00
|
|
|
;; Function: pmm_init
|
|
|
|
;;
|
|
|
|
;; In:
|
|
|
|
;; EAX - Start
|
|
|
|
;; EBX - End
|
2024-07-06 13:19:35 +00:00
|
|
|
pmm_init:
|
2024-07-07 09:24:51 +00:00
|
|
|
push eax
|
|
|
|
mov esi, szMsgPmmInit
|
|
|
|
call klog
|
|
|
|
pop eax
|
|
|
|
|
|
|
|
call pmm_free_range
|
2024-07-06 13:19:35 +00:00
|
|
|
ret
|
|
|
|
|
2024-07-07 09:44:51 +00:00
|
|
|
;; Function: pmm_alloc_page
|
|
|
|
;;
|
|
|
|
;; Out:
|
|
|
|
;; EAX - page address (return zero on error)
|
2024-07-06 13:19:35 +00:00
|
|
|
pmm_alloc_page:
|
2024-07-10 10:56:53 +00:00
|
|
|
mov eax, uPmmLock
|
|
|
|
call lock_acquire
|
|
|
|
|
2024-07-07 09:24:51 +00:00
|
|
|
cmp [pFreeList], 0
|
|
|
|
je .error
|
|
|
|
mov eax, [pFreeList]
|
|
|
|
mov edx, [eax]
|
|
|
|
mov [pFreeList], edx
|
2024-07-10 06:04:40 +00:00
|
|
|
|
|
|
|
inc [cUsedPage]
|
|
|
|
dec [cFreePage]
|
|
|
|
|
2024-07-10 10:56:53 +00:00
|
|
|
|
|
|
|
push eax
|
|
|
|
mov eax, uPmmLock
|
|
|
|
call lock_release
|
|
|
|
pop eax
|
|
|
|
|
2024-07-06 13:19:35 +00:00
|
|
|
ret
|
2024-07-07 09:24:51 +00:00
|
|
|
.error:
|
|
|
|
mov esi, szErrorNoMemLeft
|
|
|
|
call klog
|
2024-07-10 10:56:53 +00:00
|
|
|
|
|
|
|
mov eax, uPmmLock
|
|
|
|
call lock_release
|
2024-07-07 09:24:51 +00:00
|
|
|
xor eax, eax
|
2024-07-10 10:56:53 +00:00
|
|
|
|
2024-07-06 13:19:35 +00:00
|
|
|
ret
|
|
|
|
|
2024-07-07 09:44:51 +00:00
|
|
|
;; Function: pmm_free_page
|
|
|
|
;; push page back to free list
|
|
|
|
;;
|
|
|
|
;; In:
|
|
|
|
;; EAX - page to be freed
|
2024-07-06 13:19:35 +00:00
|
|
|
pmm_free_page:
|
2024-07-10 10:56:53 +00:00
|
|
|
mov eax, uPmmLock
|
|
|
|
call lock_acquire
|
2024-07-07 09:24:51 +00:00
|
|
|
or eax, eax
|
|
|
|
jz @f
|
|
|
|
mov edx, [pFreeList]
|
|
|
|
mov [eax], edx
|
|
|
|
mov [pFreeList], eax
|
2024-07-10 06:04:40 +00:00
|
|
|
|
|
|
|
inc [cFreePage]
|
|
|
|
dec [cUsedPage]
|
2024-07-07 09:24:51 +00:00
|
|
|
@@:
|
2024-07-10 10:56:53 +00:00
|
|
|
mov eax, uPmmLock
|
|
|
|
call lock_release
|
2024-07-06 13:19:35 +00:00
|
|
|
ret
|
2024-07-07 09:24:51 +00:00
|
|
|
|
|
|
|
;; Function: pmm_free_range
|
2024-07-07 09:44:51 +00:00
|
|
|
;;
|
2024-07-07 09:24:51 +00:00
|
|
|
;; In:
|
|
|
|
;; EAX - Start
|
|
|
|
;; EBX - End
|
|
|
|
pmm_free_range:
|
|
|
|
push ebp
|
|
|
|
mov ebp, esp
|
|
|
|
sub esp, 0x10
|
2024-07-09 08:35:47 +00:00
|
|
|
ALIGN eax
|
|
|
|
ALIGN ebx
|
2024-07-07 09:24:51 +00:00
|
|
|
mov [ebp-4], eax
|
|
|
|
mov [ebp-8], ebx
|
|
|
|
|
|
|
|
push ebx
|
|
|
|
push eax
|
|
|
|
mov esi, szMsgPmmFreeRange
|
|
|
|
call klog
|
|
|
|
|
|
|
|
mov esi, [ebp-4]
|
|
|
|
.loop:
|
|
|
|
mov eax, [pFreeList]
|
|
|
|
mov [esi], eax
|
|
|
|
mov [pFreeList], esi
|
|
|
|
|
2024-07-10 06:04:40 +00:00
|
|
|
inc [cTotalPage]
|
|
|
|
inc [cFreePage]
|
|
|
|
|
2024-07-07 09:24:51 +00:00
|
|
|
add esi, 4096
|
|
|
|
cmp esi, [ebp-8]
|
|
|
|
jb .loop
|
|
|
|
|
2024-07-10 06:17:00 +00:00
|
|
|
call pmm_stats
|
2024-07-07 09:24:51 +00:00
|
|
|
|
2024-07-10 06:17:00 +00:00
|
|
|
leave
|
|
|
|
ret
|
|
|
|
|
|
|
|
;; Function: pmm_stats
|
|
|
|
pmm_stats:
|
2024-07-10 06:04:40 +00:00
|
|
|
push dword [cFreePage]
|
|
|
|
push dword [cUsedPage]
|
|
|
|
push dword [cTotalPage]
|
|
|
|
mov esi, szMsgPmmStats
|
|
|
|
call klog
|
2024-07-07 09:24:51 +00:00
|
|
|
ret
|
|
|
|
|
|
|
|
pFreeList dd 0
|
|
|
|
szMsgPmmInit db "PMM: initialize", 0
|
|
|
|
szMsgPmmFreeRange db "PMM: add free memory range %x - %x", 0
|
|
|
|
szErrorNoMemLeft db "Error: no free memory left", 0
|
2024-07-10 06:04:40 +00:00
|
|
|
szMsgPmmStats db "PMM: Total page: %x | Used page: %x | Free page: %x", 0
|
2024-07-10 10:56:53 +00:00
|
|
|
uPmmLock dd 0
|
2024-07-10 06:04:40 +00:00
|
|
|
|
|
|
|
; Some stats
|
|
|
|
cFreePage dd 0
|
|
|
|
cUsedPage dd 0
|
|
|
|
cTotalPage dd 0
|