fix: spinlock

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-07-10 12:56:53 +02:00
parent 5379d0e924
commit 536f18f8cb
5 changed files with 77 additions and 23 deletions

View file

@ -8,7 +8,8 @@ SRCS = kernel.asm \
klog.inc \ klog.inc \
dev/vga_console.inc \ dev/vga_console.inc \
mm/mm.inc \ mm/mm.inc \
mm/pmm.inc mm/pmm.inc \
lock.inc
.PHONY: all .PHONY: all
all: $(KERNEL) all: $(KERNEL)

View file

@ -1,13 +1,16 @@
macro ISR_ERROR num { macro ISR [name,error] {
cli isr_table:
forward
jmp isr_common dd isr_#name
} forward
isr_#name#:
macro ISR_NOERROR num { cli
cli if error eq 0
push 0
jmp isr_common end if
push error
jmp isr_common
} }
isr_common: isr_common:
@ -32,3 +35,37 @@ isr_common:
add esp, 8 add esp, 8
sti sti
iret iret
ISR \
division_by_zero, 0, \
debug, 0, \
nmi, 0, \
breakpoint, 0, \
overflow, 0, \
bound_range_exceeded, 0, \
invalid_opcode, 0, \
device_not_available, 0, \
double_fault, 1, \
coproc_seg_overrun, 0, \
invalid_tss, 1, \
seg_not_present, 1, \
stack_seg_fault, 1, \
general_protection_fault, 1, \
page_fault, 1, \
reserved15, 0, \
x87_exception, 0, \
alignment_check, 0, \
machine_check, 0, \
simd_exception, 0, \
virt_exception, 0, \
reserved21, 0, \
reserved22, 0, \
reserved23, 0, \
reserved24, 0, \
reserved25, 0, \
reserved26, 0, \
reserved27, 0, \
reserved28, 0, \
reserved29, 0, \
security_exception, 0, \
reserved31, 0

View file

@ -68,6 +68,7 @@ kmain:
include 'klog.inc' include 'klog.inc'
include 'dev/vga_console.inc' include 'dev/vga_console.inc'
include 'mm/mm.inc' include 'mm/mm.inc'
include 'lock.inc'
szMsgKernelAlive db "Kernel is alive", 0 szMsgKernelAlive db "Kernel is alive", 0
szErrorBootProtocol db "Error: wrong magic number", 0 szErrorBootProtocol db "Error: wrong magic number", 0

View file

@ -1,17 +1,13 @@
;; ;;
lock_acquire: lock_acquire:
lock bts [eax], 0 mov edx, 1
jnc .end xchg edx, [eax]
@@: or edx, edx
pause jnz lock_acquire
test [eax], 1
jne @b
lock bts [eax], 0
jc @b
.end:
ret ret
release: lock_release:
mov [eax], 0 xor edx, edx
xchg [eax], edx
ret

View file

@ -31,6 +31,10 @@ pmm_init:
;; Out: ;; Out:
;; EAX - page address (return zero on error) ;; EAX - page address (return zero on error)
pmm_alloc_page: pmm_alloc_page:
xchg bx, bx
mov eax, uPmmLock
call lock_acquire
cmp [pFreeList], 0 cmp [pFreeList], 0
je .error je .error
mov eax, [pFreeList] mov eax, [pFreeList]
@ -40,11 +44,21 @@ pmm_alloc_page:
inc [cUsedPage] inc [cUsedPage]
dec [cFreePage] dec [cFreePage]
push eax
mov eax, uPmmLock
call lock_release
pop eax
ret ret
.error: .error:
mov esi, szErrorNoMemLeft mov esi, szErrorNoMemLeft
call klog call klog
mov eax, uPmmLock
call lock_release
xor eax, eax xor eax, eax
ret ret
;; Function: pmm_free_page ;; Function: pmm_free_page
@ -53,6 +67,8 @@ pmm_alloc_page:
;; In: ;; In:
;; EAX - page to be freed ;; EAX - page to be freed
pmm_free_page: pmm_free_page:
mov eax, uPmmLock
call lock_acquire
or eax, eax or eax, eax
jz @f jz @f
mov edx, [pFreeList] mov edx, [pFreeList]
@ -62,6 +78,8 @@ pmm_free_page:
inc [cFreePage] inc [cFreePage]
dec [cUsedPage] dec [cUsedPage]
@@: @@:
mov eax, uPmmLock
call lock_release
ret ret
;; Function: pmm_free_range ;; Function: pmm_free_range
@ -115,6 +133,7 @@ szMsgPmmInit db "PMM: initialize", 0
szMsgPmmFreeRange db "PMM: add free memory range %x - %x", 0 szMsgPmmFreeRange db "PMM: add free memory range %x - %x", 0
szErrorNoMemLeft db "Error: no free memory left", 0 szErrorNoMemLeft db "Error: no free memory left", 0
szMsgPmmStats db "PMM: Total page: %x | Used page: %x | Free page: %x", 0 szMsgPmmStats db "PMM: Total page: %x | Used page: %x | Free page: %x", 0
uPmmLock dd 0
; Some stats ; Some stats
cFreePage dd 0 cFreePage dd 0