From 3093e6bc8244419fd13e32200c28702def21bc0e Mon Sep 17 00:00:00 2001 From: d0p1 Date: Mon, 30 Dec 2024 15:14:04 +0100 Subject: [PATCH] chore: fix shellcheck errors, and some wip stuff --- kernel/mm/bootstrap.inc | 2 +- kernel/mm/mm.new.inc | 75 ++++++++------------ kernel/mm/mm.old.inc | 2 +- kernel/mm/pmm.inc | 131 +++++++++++++++++++++++++++++++++++ kernel/mm/pmm.new.inc | 23 ------ kernel/mm/pmm.old.inc | 2 +- kernel/mm/vm.inc | 80 +++++++++++++++++++++ releasetools/image.functions | 2 +- 8 files changed, 245 insertions(+), 72 deletions(-) create mode 100644 kernel/mm/pmm.inc delete mode 100644 kernel/mm/pmm.new.inc create mode 100644 kernel/mm/vm.inc diff --git a/kernel/mm/bootstrap.inc b/kernel/mm/bootstrap.inc index db4d560..6584257 100644 --- a/kernel/mm/bootstrap.inc +++ b/kernel/mm/bootstrap.inc @@ -38,7 +38,7 @@ mm_bootstrap: mov eax, 0x400000 + (1023 * PAGE_SIZE) mov cr3, eax - + ret szMsgMmBootstrap db "MM: boostrap", 0 diff --git a/kernel/mm/mm.new.inc b/kernel/mm/mm.new.inc index 1472027..4e85f31 100644 --- a/kernel/mm/mm.new.inc +++ b/kernel/mm/mm.new.inc @@ -1,49 +1,34 @@ - ;; Based on -VM_BESTFIT = (1 shl 0) -VM_INSTANTFIT = (1 shl 1) -VM_NEXTFIT = (1 shl 2) -VM_SLEEP = (1 shl 3) -VM_NOSLEEP = (1 shl 4) -VM_BOOTSTRAP = (1 shl 5) +MM_PTE_BASE = 0xFFC00000 +MM_VIRT_TEMP = 0xD0000000 -FREELISTS_N = 4 * 8 -HASHTABLE_N = 16 + ;; EAX - Physical address to map +mm_map_temp: + mov ecx, MM_PTE_BASE + (MM_VIRT_TEMP shr 12) + or eax, 0x3 + mov [ecx], eax -SEGMENT_ALLOCATED = 0 -SEGMENT_FREE = 1 -SEGMENT_SPAN = 2 +if COMPAT_I386 + mov eax, 0x40000 + (1023 * PAGE_SIZE) + mov cr3, eax +else + mov eax, MM_VIRT_TEMP + invlpg [eax] +end if -struc VmSegment { - .type db ? - .imported db ? - .base dd ? - .size dd ? -} - -struc VmObject { - .tmp dd ? -} - -struc VmPager { - .tmp dd ? -} - -struc Vmem { - .name db 32 dup(0) - .base dd ? - .size dd ? - .quantum dd ? - .alloc dd ? - .free dd ? - .source dd ? - .qcache_max dd ? - .vmflag dd ? ;; db ? - - .segqueue dd ? - .freelist dd FREELISTS_N dup(0) - .hashtable dd HASHTABLE_N dup(0) - .spanlist dd ? -} - -vmem_init: + ret + +mm_unmap_temp: + mov ecx, MM_PTE_BASE + (MM_VIRT_TEMP shr 12) + mov dword [ecx], 0 + +if COMPAT_I386 + mov eax, 0x40000 + (1023 * PAGE_SIZE) + mov cr3, eax +else + mov eax, MM_VIRT_TEMP + invlpg [eax] +end if + ret + +mm_init: ret diff --git a/kernel/mm/mm.old.inc b/kernel/mm/mm.old.inc index 22151bb..039469d 100644 --- a/kernel/mm/mm.old.inc +++ b/kernel/mm/mm.old.inc @@ -1,4 +1,4 @@ - ;; File: mm.inc + ;; File: mm.old.inc ;; StupidOS Memory Manager ;; ;; About: Memory Layout diff --git a/kernel/mm/pmm.inc b/kernel/mm/pmm.inc new file mode 100644 index 0000000..3ef20d2 --- /dev/null +++ b/kernel/mm/pmm.inc @@ -0,0 +1,131 @@ + ;; File: pmm.inc + ;; + ;; Our PMM is just a linked list of page-aligned size blocks. + ;; + ;; > ┌─────┐ ┌─────┐ + ;; > │size │ ┌──►│size │ + ;; > │next ├──┘ │next ├────► 0 + ;; > └─────┘ └─────┘ + ;; + + ;; Macro: PAGE_ALIGN_UP reg +macro PAGE_ALIGN_UP reg { + add reg, PAGE_SIZE - 1 + and reg, -PAGE_SIZE +} + + ;; Macro: PAGE_ALIGN_DOWN reg +macro PAGE_ALIGN_DOWN reg { + and reg, -PAGE_SIZE +} + + ;; Struc: PMMFreeRange +struc PMMFreeRange { + .size dd ? + .next dd ? +} +DEFN PMMFreeRange + + ;; Function: _pmm_map_temp_page + ;; +_pmm_map_temp_page: + ret + +_pmm_unmap_temp_page: + ret + +_pmm_init_region: + push ebp + mov ebp, esp + + push eax + call _pmm_map_temp_page + pop eax + + xor ecx, ecx + mov [MM_VIRT_TEMP], edx + mov [MM_VIRT_TEMP + PMMFreeRange.next], ecx + mov ecx, [pPMMFreeListHead] + or ecx, ecx + jz @f + mov [MM_VIRT_TEMP + PMMFreeRange.next], ecx +@@: + mov [pPMMFreeListHead], eax + + call _pmm_unmap_temp_page + + leave + ret + + ;; Function: pmm_alloc + ;; + ;; In: + ;; EAX - size in page + ;; + ;; Out: + ;; EAX - first page physical address, 0 on error +pmm_alloc: + ret + + ;; Function: pmm_alloc_page + ;; + ;; Out: + ;; EAX - page physical address, 0 on error +pmm_alloc_page: + mov eax, 1 + call pmm_alloc + ret + + ;; Function: pmm_free + ;; + ;; In: + ;; EAX - Start + ;; EDX - End +pmm_free: + ret + + ;; Function: _pmm_init_region + ;; + ;; Add new memory region to the linked list. + ;; + ;; In: + ;; EAX - Start + ;; EDX - End + ;; + + ;; Function: pmm_init + ;; + ;; Out: + ;; EAX - return -1 on error +pmm_init: + mov esi, szMsgMmInit + call klog + + mov eax, kend + PAGE_ALIGN_UP eax + + mov edx, 0x400000 + sub edx, eax + jle @f + cmp edx, PAGE_SIZE + jle @f + + mov edx, 0x400000 + call _pmm_init_region + +@@: + mov eax, 0x800000 + mov edx, [stBootInfo.high_mem] + PAGE_ALIGN_DOWN edx + call _pmm_init_region + + xor eax, eax ; TODO: check if enough memory and so on + ret + + ;; Variable: pPMMFreeListHead + ;; Hold first free list entry physical address +pPMMFreeListHead dd 0 + +szMsgPmmInit db "PMM: initialize", 0 +szMsgPmmFreeRange db "PMM: add free memory region %x - %x", 0 +szErrorNoMemLeft db "Error(PMM): no free memory left", 0 diff --git a/kernel/mm/pmm.new.inc b/kernel/mm/pmm.new.inc deleted file mode 100644 index 9a4d8d6..0000000 --- a/kernel/mm/pmm.new.inc +++ /dev/null @@ -1,23 +0,0 @@ - ;; Struc: PMMFreeRange -struc PMMFreeRange { - .size dd ? - .next dd ? -} - - ;; -pPMMFreeListHead dd 0 - -pmm_alloc: - ret - -pmm_alloc_page: - ret - -pmm_free_page: - ret - - ;; - ;; kend - 4Mb - ;; 8Mb - max ram -pmm_init: - ret diff --git a/kernel/mm/pmm.old.inc b/kernel/mm/pmm.old.inc index a52da87..a615d08 100644 --- a/kernel/mm/pmm.old.inc +++ b/kernel/mm/pmm.old.inc @@ -1,4 +1,4 @@ - ;; File: pmm.inc + ;; File: pmm.old.inc macro ALIGN reg { local ..end diff --git a/kernel/mm/vm.inc b/kernel/mm/vm.inc new file mode 100644 index 0000000..4f337b1 --- /dev/null +++ b/kernel/mm/vm.inc @@ -0,0 +1,80 @@ + ;; File: vm.inc + ;; Based on + +VM_BESTFIT = (1 shl 0) +VM_INSTANTFIT = (1 shl 1) +VM_NEXTFIT = (1 shl 2) +VM_SLEEP = (1 shl 3) +VM_NOSLEEP = (1 shl 4) +VM_BOOTSTRAP = (1 shl 5) + +FREELISTS_N = 4 * 8 +HASHTABLE_N = 16 + +SEGMENT_ALLOCATED = 0 +SEGMENT_FREE = 1 +SEGMENT_SPAN = 2 + +struc VmSegment { + .type db ? + .imported db ? + .base dd ? + .size dd ? +} + +struc VmObject { + .tmp dd ? +} + +struc VmPager { + .tmp dd ? +} + +struc Vmem { + .name db 32 dup(0) + .base dd ? + .size dd ? + .quantum dd ? + .alloc dd ? + .free dd ? + .source dd ? + .qcache_max dd ? + .vmflag dd ? ;; db ? + + .segqueue dd ? + .freelist dd FREELISTS_N dup(0) + .hashtable dd HASHTABLE_N dup(0) + .spanlist dd ? +} + + ;; Subroutine: _murmur32 + ;; + ;; In: + ;; EAX - Address + ;; + ;; Out: + ;; EAX - Hash + ;; +_murmur32: + ; hash ← hash XOR (hash >> 16) + mov ecx, eax + shr ecx, 16 + xor eax, ecx + ; hash ← hash × 0x85ebca6b + mov ecx, 0x85ebca6b + mul ecx + ; hash ← hash XOR (hash >> 13) + mov ecx, eax + shr ecx, 13 + xor eax, ecx + ; hash ← hash × 0xc2b2ae35 + mov ecx, 0xc2b2ae35 + mul ecx + ; hash ← hash XOR (hash >> 16) + mov ecx, eax + shr ecx, 16 + xor eax, ecx + ret + +vmem_init: + ret diff --git a/releasetools/image.functions b/releasetools/image.functions index 0500472..337fc81 100644 --- a/releasetools/image.functions +++ b/releasetools/image.functions @@ -55,5 +55,5 @@ EOF create_hd_image() { - + echo }