chore: fix shellcheck errors, and some wip stuff

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-12-30 15:14:04 +01:00
parent 0c4a3e29f6
commit 3093e6bc82
8 changed files with 245 additions and 72 deletions

View file

@ -38,7 +38,7 @@ mm_bootstrap:
mov eax, 0x400000 + (1023 * PAGE_SIZE) mov eax, 0x400000 + (1023 * PAGE_SIZE)
mov cr3, eax mov cr3, eax
ret ret
szMsgMmBootstrap db "MM: boostrap", 0 szMsgMmBootstrap db "MM: boostrap", 0

View file

@ -1,49 +1,34 @@
;; Based on <https://github.com/rdmsr/tinyvmem at tinyvm> MM_PTE_BASE = 0xFFC00000
VM_BESTFIT = (1 shl 0) MM_VIRT_TEMP = 0xD0000000
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 ;; EAX - Physical address to map
HASHTABLE_N = 16 mm_map_temp:
mov ecx, MM_PTE_BASE + (MM_VIRT_TEMP shr 12)
or eax, 0x3
mov [ecx], eax
SEGMENT_ALLOCATED = 0 if COMPAT_I386
SEGMENT_FREE = 1 mov eax, 0x40000 + (1023 * PAGE_SIZE)
SEGMENT_SPAN = 2 mov cr3, eax
else
mov eax, MM_VIRT_TEMP
invlpg [eax]
end if
struc VmSegment { ret
.type db ?
.imported db ? mm_unmap_temp:
.base dd ? mov ecx, MM_PTE_BASE + (MM_VIRT_TEMP shr 12)
.size dd ? mov dword [ecx], 0
}
if COMPAT_I386
struc VmObject { mov eax, 0x40000 + (1023 * PAGE_SIZE)
.tmp dd ? mov cr3, eax
} else
mov eax, MM_VIRT_TEMP
struc VmPager { invlpg [eax]
.tmp dd ? end if
} ret
struc Vmem { mm_init:
.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 ret

View file

@ -1,4 +1,4 @@
;; File: mm.inc ;; File: mm.old.inc
;; StupidOS Memory Manager ;; StupidOS Memory Manager
;; ;;
;; About: Memory Layout ;; About: Memory Layout

131
kernel/mm/pmm.inc Normal file
View file

@ -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

View file

@ -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

View file

@ -1,4 +1,4 @@
;; File: pmm.inc ;; File: pmm.old.inc
macro ALIGN reg { macro ALIGN reg {
local ..end local ..end

80
kernel/mm/vm.inc Normal file
View file

@ -0,0 +1,80 @@
;; File: vm.inc
;; Based on <https://github.com/rdmsr/tinyvmem at tinyvm>
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

View file

@ -55,5 +55,5 @@ EOF
create_hd_image() create_hd_image()
{ {
echo
} }