mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
x86 asm: move exchange instructions from x86-assembly-cheat
This commit is contained in:
28
userland/arch/x86_64/cmpxchg.S
Normal file
28
userland/arch/x86_64/cmpxchg.S
Normal file
@@ -0,0 +1,28 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#cmpxchg-instruction */
|
||||
|
||||
#include <lkmc.h>
|
||||
|
||||
LKMC_PROLOGUE
|
||||
/* rax != r13 */
|
||||
mov $0, %rax
|
||||
mov $1, %r13
|
||||
mov $2, %r14
|
||||
cmpxchg %r14, %r13
|
||||
mov %rax, %r12
|
||||
LKMC_ASSERT(jnz)
|
||||
LKMC_ASSERT_EQ(%rax, $1)
|
||||
LKMC_ASSERT_EQ(%r13, $1)
|
||||
LKMC_ASSERT_EQ(%r14, $2)
|
||||
|
||||
/* rax == r13 */
|
||||
mov $0, %rax
|
||||
mov $0, %r13
|
||||
mov $2, %r14
|
||||
cmpxchg %r14, %r13
|
||||
mov %rax, %r12
|
||||
LKMC_ASSERT(jz)
|
||||
LKMC_ASSERT_EQ(%rax, $0)
|
||||
LKMC_ASSERT_EQ(%r13, $2)
|
||||
LKMC_ASSERT_EQ(%r14, $2)
|
||||
|
||||
LKMC_EPILOGUE
|
||||
11
userland/arch/x86_64/xadd.S
Normal file
11
userland/arch/x86_64/xadd.S
Normal file
@@ -0,0 +1,11 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-exchange-instructions */
|
||||
|
||||
#include <lkmc.h>
|
||||
|
||||
LKMC_PROLOGUE
|
||||
mov $1, %rax
|
||||
mov $2, %rbx
|
||||
xadd %rbx, %rax
|
||||
LKMC_ASSERT_EQ(%rax, $3)
|
||||
LKMC_ASSERT_EQ(%rbx, $1)
|
||||
LKMC_EPILOGUE
|
||||
16
userland/arch/x86_64/xchg.S
Normal file
16
userland/arch/x86_64/xchg.S
Normal file
@@ -0,0 +1,16 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-exchange-instructions */
|
||||
|
||||
#include <lkmc.h>
|
||||
|
||||
LKMC_PROLOGUE
|
||||
mov $0, %rax
|
||||
mov $1, %rbx
|
||||
|
||||
xchg %rbx, %rax
|
||||
LKMC_ASSERT_EQ(%rax, $1)
|
||||
LKMC_ASSERT_EQ(%rbx, $0)
|
||||
|
||||
xchg %rbx, %rax
|
||||
LKMC_ASSERT_EQ(%rax, $0)
|
||||
LKMC_ASSERT_EQ(%rbx, $1)
|
||||
LKMC_EPILOGUE
|
||||
@@ -1,19 +1,27 @@
|
||||
// https://github.com/cirosantilli/linux-kernel-module-cheat#atomic
|
||||
// https://github.com/cirosantilli/linux-kernel-module-cheat#cpp
|
||||
// https://github.com/cirosantilli/linux-kernel-module-cheat#x86-lock-prefix
|
||||
//
|
||||
// More restricted than mutex as it can only protect a few operations on integers.
|
||||
// The non-atomic counters have undefined values which get printed:
|
||||
// they are extremely likely to be less than the correct value due to
|
||||
// race conditions on the data read and update of the ++.
|
||||
//
|
||||
// But if that is the use case, may be more efficient.
|
||||
// The atomic counters have defined values, and are asserted
|
||||
//
|
||||
// On GCC 4.8 x86-64, using atomic is a huge peformance improvement
|
||||
// over the same program with mutexes (5x).
|
||||
// Atomic operations are more restricted than mutex as they can
|
||||
// only protect a few operations on integers.
|
||||
//
|
||||
// But when they can be used, they can be much more efficient than mutees.
|
||||
//
|
||||
// On GCC 4.8 x86-64, using atomic offered a 5x peformance improvement
|
||||
// over the same program with mutexes.
|
||||
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
std::atomic_ulong my_atomic_ulong(0);
|
||||
unsigned long my_non_atomic_ulong = 0;
|
||||
#if defined(__x86_64__)
|
||||
|
||||
Reference in New Issue
Block a user