2024-07-07 09:44:51 +00:00
|
|
|
;; File: mm.inc
|
2024-07-07 13:29:41 +00:00
|
|
|
;; StupidOS Memory Manager
|
2024-07-08 17:10:37 +00:00
|
|
|
;;
|
|
|
|
;; About: Memory Layout
|
|
|
|
;;
|
|
|
|
;; > Virtual
|
|
|
|
;; > 0xFFFFFFFF +---------------+
|
|
|
|
;; > | |
|
|
|
|
;; > | Device Memory |
|
|
|
|
;; > | |
|
|
|
|
;; > +---------------+
|
|
|
|
;; > | |
|
|
|
|
;; > | Kernel Heap |
|
|
|
|
;; > | |
|
|
|
|
;; > KERN_END +---------------+
|
|
|
|
;; > | |
|
|
|
|
;; > | Stupid Kernel |
|
|
|
|
;; > | |
|
|
|
|
;; > 0xC0100000 +---------------+
|
|
|
|
;; > | I/O Space and |
|
|
|
|
;; > | phys memory |
|
|
|
|
;; > | Lower than 1M | kernel mode only
|
|
|
|
;; > 0xC0000000 +---------------+
|
|
|
|
;; > | | user mode
|
|
|
|
;; > | userspace |
|
|
|
|
;; > | |
|
|
|
|
;; > 0x00000000 +---------------+
|
|
|
|
;;
|
2024-07-07 13:29:41 +00:00
|
|
|
|
2024-07-06 13:19:35 +00:00
|
|
|
include "pmm.inc"
|
2024-07-08 17:10:37 +00:00
|
|
|
include "../sys/mmu.inc"
|
2024-02-04 19:18:52 +00:00
|
|
|
|
2024-07-08 17:10:37 +00:00
|
|
|
;; Macro: KV2P
|
|
|
|
;; Convert virtual address to physical
|
2024-07-08 16:48:31 +00:00
|
|
|
macro KV2P reg {
|
|
|
|
sub reg, KERNEL_VIRT_BASE
|
|
|
|
}
|
|
|
|
|
2024-07-08 17:10:37 +00:00
|
|
|
;; Macro: KP2V
|
2024-07-09 08:35:47 +00:00
|
|
|
;; Convert physical address to virtual
|
2024-07-08 17:10:37 +00:00
|
|
|
macro KP2V reg {
|
|
|
|
add reg, KERNEL_VIRT_BASE
|
|
|
|
}
|
|
|
|
|
2024-07-09 08:35:47 +00:00
|
|
|
;; Function: mm_kmap
|
2024-07-08 17:10:37 +00:00
|
|
|
mm_kmap:
|
2024-07-09 08:35:47 +00:00
|
|
|
push esp
|
|
|
|
mov ebp, esp
|
|
|
|
sub esp, 0x10
|
2024-07-07 09:24:51 +00:00
|
|
|
|
|
|
|
call pmm_alloc_page
|
2024-07-09 08:35:47 +00:00
|
|
|
mov [ebp-4], eax
|
2024-07-07 09:24:51 +00:00
|
|
|
|
2024-07-07 13:29:41 +00:00
|
|
|
; clear page dir
|
2024-07-09 08:35:47 +00:00
|
|
|
mov edi, eax
|
2024-07-07 13:29:41 +00:00
|
|
|
mov ecx, 4096
|
|
|
|
xor al, al
|
|
|
|
rep stosb
|
|
|
|
|
|
|
|
call pmm_alloc_page
|
2024-07-08 16:48:31 +00:00
|
|
|
xor esi, esi
|
|
|
|
xor ecx, ecx
|
2024-07-07 13:29:41 +00:00
|
|
|
mov edi, eax
|
2024-07-08 16:48:31 +00:00
|
|
|
@@:
|
|
|
|
mov edx, esi
|
2024-07-08 17:10:37 +00:00
|
|
|
or edx, (PTE_P or PTE_W)
|
2024-07-08 16:48:31 +00:00
|
|
|
mov [edi], edx
|
|
|
|
add edi, 4
|
|
|
|
add esi, 4096
|
|
|
|
inc ecx
|
|
|
|
cmp ecx, 1024
|
|
|
|
jb @b
|
|
|
|
|
2024-07-08 17:10:37 +00:00
|
|
|
or eax, (PDE_P or PDE_W)
|
2024-07-09 08:35:47 +00:00
|
|
|
mov edx, [ebp-4]
|
2024-07-08 16:48:31 +00:00
|
|
|
add edx, (768 * 4)
|
|
|
|
KV2P eax
|
|
|
|
mov [edx], eax
|
2024-07-07 09:44:51 +00:00
|
|
|
|
2024-07-09 08:35:47 +00:00
|
|
|
mov eax, [ebp-4]
|
|
|
|
|
|
|
|
leave
|
|
|
|
ret
|
2024-07-07 09:44:51 +00:00
|
|
|
|
2024-07-10 06:17:00 +00:00
|
|
|
;; Function: mm_clone_pte
|
|
|
|
;;
|
|
|
|
;; In:
|
|
|
|
;; EAX - source page table
|
|
|
|
;;
|
|
|
|
;; Out:
|
|
|
|
;; EAX - cloned page table
|
|
|
|
;;
|
|
|
|
mm_clone_pte:
|
|
|
|
push esp
|
|
|
|
mov ebp, esp
|
|
|
|
sub esp, 0x10
|
|
|
|
|
|
|
|
leave
|
|
|
|
ret
|
|
|
|
|
|
|
|
;; Function: mm_clone_pgdir
|
|
|
|
;;
|
|
|
|
;; In:
|
|
|
|
;; EAX - source page dir
|
|
|
|
;;
|
|
|
|
;; Out:
|
|
|
|
;; EAX - cloned page dir
|
|
|
|
mm_clone_pgdir:
|
|
|
|
push esp
|
|
|
|
mov ebp, esp
|
|
|
|
sub esp, 0x10
|
|
|
|
|
|
|
|
mov [ebp-4], eax
|
|
|
|
call pmm_alloc_page
|
|
|
|
|
|
|
|
mov [ebp-8], eax
|
|
|
|
mov ecx, 4096
|
|
|
|
mov edi, eax
|
|
|
|
xor al, al
|
|
|
|
rep stosb
|
|
|
|
|
|
|
|
xor ecx, ecx
|
|
|
|
.loop:
|
|
|
|
mov eax, [ebp-4]
|
|
|
|
add eax, ecx
|
|
|
|
mov eax, [eax]
|
|
|
|
cmp eax, 0
|
|
|
|
je .next
|
|
|
|
cmp ecx, (768*4)
|
|
|
|
jb @f
|
|
|
|
mov edx, [ebp-8]
|
|
|
|
add edx, ecx
|
|
|
|
mov [edx], eax
|
|
|
|
jmp .next
|
|
|
|
@@:
|
|
|
|
mov edx, eax
|
|
|
|
call pmm_alloc_page
|
|
|
|
.next:
|
|
|
|
add ecx, 4
|
|
|
|
cmp ecx, 4096
|
|
|
|
jb .loop
|
|
|
|
|
|
|
|
leave
|
|
|
|
ret
|
|
|
|
|
2024-07-09 08:35:47 +00:00
|
|
|
;; Function: mm_init
|
|
|
|
mm_init:
|
|
|
|
mov esi, szMsgMmInit
|
|
|
|
call klog
|
|
|
|
|
|
|
|
call mm_kmap
|
|
|
|
mov [pKernelPgDir], eax
|
|
|
|
|
|
|
|
; map whole memory
|
|
|
|
mov esi, 0x400000
|
|
|
|
mov ebx, (769 * 4)
|
|
|
|
.loop:
|
|
|
|
call pmm_alloc_page
|
|
|
|
xor ecx, ecx
|
|
|
|
mov edi, eax
|
|
|
|
@@:
|
|
|
|
mov edx, esi
|
|
|
|
or edx, (PTE_P or PTE_W)
|
|
|
|
mov [edi], edx
|
|
|
|
add edi, 4
|
|
|
|
add esi, 4096
|
|
|
|
inc ecx
|
|
|
|
cmp ecx, 1024
|
|
|
|
jb @b
|
|
|
|
|
|
|
|
or eax, (PDE_P or PDE_W)
|
|
|
|
mov edx, [pKernelPgDir]
|
|
|
|
add edx, ebx
|
|
|
|
add ebx, 4
|
|
|
|
KV2P eax
|
|
|
|
mov [edx], eax
|
|
|
|
|
|
|
|
cmp esi, [boot_structure.high_mem]
|
|
|
|
jb .loop
|
|
|
|
|
|
|
|
; reload cr3
|
2024-07-08 16:48:31 +00:00
|
|
|
mov eax, [pKernelPgDir]
|
|
|
|
KV2P eax
|
|
|
|
mov cr3, eax
|
|
|
|
|
|
|
|
push eax
|
|
|
|
mov esi, szMsgMmKernelPgDir
|
|
|
|
call klog
|
2024-07-07 09:44:51 +00:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2024-07-09 08:35:47 +00:00
|
|
|
mm_alloc:
|
|
|
|
ret
|
|
|
|
|
|
|
|
mm_dealloc:
|
|
|
|
ret
|
|
|
|
|
2024-07-07 09:44:51 +00:00
|
|
|
mm_wallk_pagedir:
|
2024-04-28 06:41:36 +00:00
|
|
|
ret
|
2024-07-07 09:24:51 +00:00
|
|
|
|
|
|
|
szMsgMmInit db "MM: initialize", 0
|
2024-07-08 16:48:31 +00:00
|
|
|
szMsgMmKernelPgDir db "MM: Kernel page dir at %x phys", 0
|
|
|
|
pKernelPgDir dd 0
|