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 \
dev/vga_console.inc \
mm/mm.inc \
mm/pmm.inc
mm/pmm.inc \
lock.inc
.PHONY: all
all: $(KERNEL)

View file

@ -1,13 +1,16 @@
macro ISR_ERROR num {
cli
macro ISR [name,error] {
isr_table:
forward
dd isr_#name
forward
isr_#name#:
cli
if error eq 0
push 0
end if
push error
jmp isr_common
jmp isr_common
}
macro ISR_NOERROR num {
cli
jmp isr_common
}
isr_common:
@ -32,3 +35,37 @@ isr_common:
add esp, 8
sti
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 'dev/vga_console.inc'
include 'mm/mm.inc'
include 'lock.inc'
szMsgKernelAlive db "Kernel is alive", 0
szErrorBootProtocol db "Error: wrong magic number", 0

View file

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

View file

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