Skip to content

Commit

Permalink
kern: Spinlocks must disable interrupts
Browse files Browse the repository at this point in the history
  • Loading branch information
mintsuki committed Feb 21, 2025
1 parent ce69212 commit 1a5dec5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
15 changes: 8 additions & 7 deletions kernel/modules/klock/klock.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
module klock

import katomic
import x86.cpu

pub struct Lock {
pub mut:
l bool
caller u64
l bool
ints bool
}

fn C.__builtin_return_address(int) voidptr

pub fn (mut l Lock) acquire() {
caller := u64(C.__builtin_return_address(0))

for {
if l.test_and_acquire() == true {
l.caller = caller
return
}
asm volatile amd64 {
Expand All @@ -31,14 +29,17 @@ pub fn (mut l Lock) acquire() {

pub fn (mut l Lock) release() {
katomic.store(mut &l.l, false)
cpu.interrupt_toggle(l.ints)
}

pub fn (mut l Lock) test_and_acquire() bool {
caller := u64(C.__builtin_return_address(0))
ints := cpu.interrupt_toggle(false)

ret := katomic.cas(mut &l.l, false, true)
if ret == true {
l.caller = caller
l.ints = ints
} else {
cpu.interrupt_toggle(ints)
}

return ret
Expand Down
3 changes: 2 additions & 1 deletion kernel/modules/lib/panic.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub fn kpanic(gpr_state &cpulocal.GPRState, message charptr) {

cpu_number := if smp_ready { cpulocal.current().cpu_number } else { 0 }

C.printf_panic(c'KERNEL PANIC: "%s" on CPU %d\n', message, cpu_number)
C.printf_panic(c'\n *** Vinix KERNEL PANIC on CPU %d ***\n\n', cpu_number)
C.printf_panic(c'Panic info: %s\n', message)
if gpr_state != unsafe { nil } {
C.printf_panic(c'Error code: 0x%016llx\n', gpr_state.err)
C.printf_panic(c'Register dump:\n')
Expand Down

0 comments on commit 1a5dec5

Please sign in to comment.