From c9b82feb34dd0e92b9a6aa0d928880bb7fea23dc Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sat, 23 Nov 2019 15:32:04 +0900 Subject: [PATCH] Add spinlock implementation (should have been in commit aa0e4586dd3efe434b34ae7956a4c27220cb798d) --- kernel/klibc/spinlock.S | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 kernel/klibc/spinlock.S diff --git a/kernel/klibc/spinlock.S b/kernel/klibc/spinlock.S new file mode 100644 index 0000000..a82c2a5 --- /dev/null +++ b/kernel/klibc/spinlock.S @@ -0,0 +1,54 @@ + .section .text + + .global spinlock_lock + .global spinlock_trylock + .global spinlock_unlock + +spinlock_lock: + movl 4(%esp), %edx +0: xorl %eax, %eax + movl $1, %ecx + lock + cmpxchgl %ecx, (%edx) + jnz 0b /* lock failed */ + ret /* lock succeeded */ + +spinlock_trylock: + movl 4(%esp), %edx + xorl %eax, %eax + movl $1, %ecx + lock + cmpxchgl %ecx, (%edx) + ret + +spinlock_unlock: + movl 4(%esp), %edx + xorl %eax, %eax + lock + xchgl %eax, (%edx) + ret + + /* 序でにamd64の実装 + +spinlock_lock: + xorq %rax, %rax + movl $1, %ecx + lock + cmpxchgl %ecx, (%rdi) + jnz spinlock_lock + ret + +spinlock_trylock: + xorl %rax, %rax + movl $1, %ecx + lock + cmpxchgl %ecx, (%rdi) + ret + +spinlock_unlock: + xorq %rax, %rax + lock + xchgl %eax, (%rdi) + ret + + */ -- 2.47.3