docs: documents memory management

This commit is contained in:
d0p1 🏳️‍⚧️ 2023-07-12 11:33:08 +02:00
parent 5cccfa22d4
commit 72f4ef1f00
7 changed files with 239 additions and 3 deletions

View file

@ -81,6 +81,8 @@ Group: Kernel {
} # Group: Sys
File: vm.inc (kernel/vm/vm.inc)
File: pmap.s (kernel/vm/pmap.s)
} # Group: Kernel
Group: Lib {

View file

@ -0,0 +1,89 @@
---
title: pmap_bootstrap
---
flowchart TD
R_BEGIN("Begin")
R_GETMMAP["Get Memory Map entry"]
R_MMAP_LAST{"Last Item ?"}
R_END("End")
subgraph "entry to pmap_block"
C_BEGIN("Begin Proc")
C_IS_FREE{"Is
entry free?"}
C_END("End Proc")
C_OVERLAP_KERNEL{"entry
overlap
kernel?"}
C_REMOVE_KRESERVED["Remove Kernel
Memory from entry"]
C_MEMORY_SIZE_EXCEED{"Is entry size
larger than
0x0x7f80000 ?"}
C_MEMORY_SPLIT["Split entry"]
C_MEMORY_LAST["Is last ?"]
C_BEGIN-->C_IS_FREE
C_IS_FREE--No-->C_END
C_IS_FREE--Yes-->C_OVERLAP_KERNEL
C_OVERLAP_KERNEL--Yes-->C_REMOVE_KRESERVED
C_REMOVE_KRESERVED-->C_MEMORY_SIZE_EXCEED
C_OVERLAP_KERNEL--No-->C_MEMORY_SIZE_EXCEED
C_MEMORY_SIZE_EXCEED--Yes-->C_MEMORY_SPLIT
C_MEMORY_SPLIT-->C_MEMORY_LAST
C_MEMORY_LAST--Yes-->C_END
end
subgraph "create & store pmap_block"
D_BEGIN("Begin Proc")
D_BOOSTRAP_EXIST{"Bootstrap
pmap_block
exist?"}
D_SETUP_BOOTSTRAP["Setup bootstrap pmap"]
D_END("End Proc")
D_SET_HEAD_BOOTSTRAP["Set bootstrap as Head"]
D_SEARCH_FREE_PAGE["Search free page in Head->bitmap"]
D_FREE_PAGE_FOUND{"Free Page found ?"}
D_HEAD_NEXT_NULL{"Head->next == NULL?"}
D_HEAD_NEXT["Set Head to Head->next"]
D_CREATE_PMAP["Create new pmap_block
in free page"]
D_APPEND_PMAP["Append pmap_block
at end of
linked list"]
D_BEGIN-->D_BOOSTRAP_EXIST
D_BOOSTRAP_EXIST--No-->D_SETUP_BOOTSTRAP
D_SETUP_BOOTSTRAP-->D_END
D_BOOSTRAP_EXIST--Yes-->D_SET_HEAD_BOOTSTRAP
D_SET_HEAD_BOOTSTRAP-->D_SEARCH_FREE_PAGE
D_SEARCH_FREE_PAGE-->D_FREE_PAGE_FOUND
D_FREE_PAGE_FOUND--No-->D_HEAD_NEXT_NULL
D_HEAD_NEXT_NULL--No-->D_HEAD_NEXT
D_HEAD_NEXT-->D_SEARCH_FREE_PAGE
D_FREE_PAGE_FOUND--Yes-->D_CREATE_PMAP
D_CREATE_PMAP-->D_APPEND_PMAP
D_APPEND_PMAP-->D_END
end
R_BEGIN-->R_GETMMAP
R_GETMMAP-->R_MMAP_LAST
R_MMAP_LAST--Yes-->R_END
R_MMAP_LAST--No-->C_BEGIN
C_MEMORY_SIZE_EXCEED--No-->D_BEGIN
D_END--Try get next-->C_MEMORY_LAST
C_MEMORY_LAST--No-->D_BEGIN
C_END--Try get next entry-->R_MMAP_LAST
style R_BEGIN fill:#6CA300
style R_END fill:#A30000
style C_BEGIN fill:#6CA300
style C_END fill:#A30000
style D_BEGIN fill:#6CA300
style D_END fill:#A30000

BIN
docs/img/pmap_bootstrap.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

View file

@ -7,4 +7,4 @@ INCS = sys/multiboot.inc \
sys/i386/registers.inc
include $(TOPDIR)/share/mk/stupid.kernel.mk
include $(TOPDIR)/share/mk/stupid.inc.mk
#include $(TOPDIR)/share/mk/stupid.inc.mk

View file

@ -1,7 +1,46 @@
;; File: mmu.inc
;;
;; About: Address Translation
;;
;; Since we don't use segmentation, this is how address translation works
;; > 31 21 11 0
;; > +--------+--------+--------+
;; > | DIR | PAGE | OFFSET |
;; > +--------+--------+--------+
;; > |
;; > v
;; > Page Translation
;; > |
;; > 31 v 0
;; > +--------------------------+
;; > | Pyshical address |
;; > +--------------------------+
;;
;; About: Page Translation
;;
;; > +--------+--------+--------+ +--------+
;; > | DIR | PAGE | OFFSET |-----+ | Phys |
;; > +--------+--------+--------+ +------>| addr |
;; > | | | |
;; > | +----+ +--------+
;; > | Page Dir | +---> Page Frame
;; > | +---------+ | +--------+ |
;; > | | | | | | |
;; > +->| PDE |-+ +->| PTE |-+
;; > | | | | |
;; > +---------+ | +--------+
;; > +----> Page Table
;; >
;;
;; Macro: V2P(addr)
;; Convert Kernel Address to Physical Address
%define V2P(addr) (addr - KERNBASE)
;; Macro: P2V(addr)
;; Convert Physical Address to Kernel Address
%define P2V(addr) (addr + KERNBASE)
%define P2PDE(addr) ((addr >> 22) & 0x3FF)
%define PDE2P(addr) (addr << 22)
%define P2PTE(addr) ((addr >> 12) & 0x3FF)
@ -26,8 +65,8 @@ PDE_G equ 1 << 8
;; PTE_PCD - Cache Disable
;; PTE_A - Accessed
;; PTE_D - Dirty
;; PTE_PAT -
;; PTE_G -
;; PTE_PAT - TODO
;; PTE_G - TODO
PTE_P equ 1 << 0
PTE_W equ 1 << 1
PTE_U equ 1 << 2

46
kernel/vm/pmap.s Normal file
View file

@ -0,0 +1,46 @@
;; File: pmap.s
;; Physical Memory Map
;; Structure: pmap_block
;;
;; .next - pointer to next pmap_block struct
;; .phys_start - start physical address of memory range
;; .phys_end - stop address of memory range
;; .bitmap_size - bitmap size (max: 0xFF0)
;; .last_alloc - last allocated frame in bitmap
;; .bitmap - bitmap array
;;
;; > pmap_block
;; > 0x0000 +-------------+
;; > | next |-----> pmap_block
;; > 0x0004 +-------------+ +----------+
;; > | phys start | | next |----> NULL (0x0)
;; > 0x0008 +-------------+ +----------+
;; > | phys end | | ..... |
;; > 0x000C +-------------+ | ..... |
;; > | bitmap size | | ..... |
;; > 0x000e +-------------+ +----------+
;; > | last alloc |
;; > 0x0010 +-------------+
;; > | bitmap |
;; > 0x???? +-------------+ (bitmap size * 4)
;;
struct pmap_block
.next: resd 1
.phys_start: resd 1
.phys_end: resd 1
.bitmap_size: resw 1
.last_alloc: resw 1
.bitmap:
endstruc
section .text
;; Function: pmap_bootstrap
;;
;; (see pmap_bootstrap.png)
pmap_bootstrap:
push esp
mov esp, ebp
leave
ret

60
kernel/vm/vm.inc Normal file
View file

@ -0,0 +1,60 @@
;; File: vm.inc
;; Stupid OS virtual Memory manager
;;
;; About: Memory Management
;;
;; Terminology:
;; VA - Virtual Address
;; PA - Physical Address
;; PDE - Page Directory Entry
;; PTE - Page Table Entry
;;
;; Memory Layout:
;;
;; Virtual Memory above `0xC0000000` is mapped as kernel only <PTE_U> flags
;;
;; > 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 +---------------+
;;
;; Page Frame Allocator
;;
%define K2P(addr) (addr - KERNBASE)
%define P2K(addr) (addr + KERNBASE)
struc vm_kmap
.virt: resd 1
.phys_start: resd 1
.phys_stop: resd 1
.access: resb 1
endstruc
struc vm_map
.pmap: resd 1
endstruc
struc
.directory: resd 1
.ref_count: resd 1
endstruc