witch yoo hee
i’m in love!
… but she’s married (in real life). this “koreanovela” is soo addicting!
i’m in love!
… but she’s married (in real life). this “koreanovela” is soo addicting!
apparently, there are some issues with linux 2.6 and pearpc.. so i still cant
get the gentoo ppc to start properly. i thought i’d do some studying in the
meantime..
typedef struct {
volatile unsigned int slock;
} raw_spinlock_t;
static inline void __raw_spin_lock(raw_spinlock_t *lock)
{ asm volatile(\"\n1:\t\"
LOCK_PREFIX \" ; decb %0\n\t\"
\"jns 3f\n\"
\"2:\t\"
\"rep;nop\n\t\"
\"cmpb $0,%0\n\t\"
\"jle 2b\n\t\"
\"jmp 1b\n\"
\"3:\n\t\"
: \"+m\" (lock->slock) : : \"memory\");
}
interpretation of the extended inline asm:
1) : “+m” (locl->slock)
“+m” means that the memory operand ‘lock->slock’ is both input and output.
2) : “memory”
from gcc manual:
If your assembler instructions access memory in an unpredictable
fashion, add `memory’ to the list of clobbered registers. This will
cause GCC to not keep memory values cached in registers across the
assembler instruction and not optimize stores or loads to that memory.
3) LOCK_PREFIX is defined at /usr/src/linux/include/asm/alternative.h:
#define LOCK_PREFIX \
\".section .smp_locks,\\"a\\"\n\" \ ; push current section and change section to .smp_locks
\" .align 4\n\" \ ; align to 4 bytes
\" .long 661f\n\" /* address */ \ ; create an entry in the up/smp alternative locking table
\".previous\n\" \ ; pop back the old section
\"661:\n\tlock; \" ; assert processor LOCK# signal
LOCK_PREFIX is supposed to be called within the context of a procedure (executable) which finds its way to a .text section.
i haven’t digged down deep into the internals of ‘alternatives’ yet, but a quick look at kernel/alternative.c :: alternatives_smp_unlock() shows that it’s employing a self modifying code that patches the “lock” instruction with an architecture specific NOP opcode.
without all the extended asm semantics above, the asm roughly translates to:
1: lock ; decrement atomically
decb %0 ; really decrement now..
jns 3f ; successful in acquiring lock?
2: rep ; nothing
nop ; nothing
cmpb $0, %0 ; someone must release the lock first.
jle 2b ; busy wait...
jmp 1b ; lock available! try acquiring lock again.
3: ; exit __raw_spin_lock()
and then comes the trylock variant
static inline int __raw_spin_trylock(raw_spinlock_t *lock)
{
char oldval;
asm volatile(
\"xchgb %b0,%1\"
:\"=q\" (oldval), \"+m\" (lock->slock)
:\"0\" (0) : \"memory\");
return oldval > 0;
}
interpretation of the extended inline asm:
1) : “=q” (oldval)
output oldval in any a,b,c or d register.
2) : “+m” (locl->slock)
“+m” means that the memory operand ‘lock->slock’ is both input and output.
3) : “0″ (0)
%0 matches the output register for oldval. initialize that with the value 0.
4) : “memory”
from gcc manual:
If your assembler instructions access memory in an unpredictable
fashion, add `memory’ to the list of clobbered registers. This will
cause GCC to not keep memory values cached in registers across the
assembler instruction and not optimize stores or loads to that memory.
__raw_spin_trylock() basically exchanges the current value in raw_spinlock_t->slock with zero and returns true if locking was successful and false if acquiring of lock didn’t succeed.
lastly, we have the raw unlock procedure:
static inline void __raw_spin_unlock(raw_spinlock_t *lock)
{
char oldval = 1;
asm volatile(\"xchgb %b0, %1\"
: \"=q\" (oldval), \"+m\" (lock->slock)
: \"0\" (oldval) : \"memory\");
}
i haven’t been able to understand the significance of the “%b” in the assembler template but looking at the asm listing of the template with and without the %b yielded the same intructions, so could anyone shed some light on this one?
basically looks like __raw_spin_trylock(), but the difference is that oldval is now initialized to 1 instead of 0.
there are many variants stemming from the idea of this implementation. raw reader/writer locks which allows multiple readers but only one writer (with no concurrent readers). and then there’s the spinlock layer above the raw spinlock implementation.. but those are for another post.
’til then.
i’ve been puting off a lot of things lately. at the top of my list is to give some just attention to this little web space of mine. its not because i don’t have time anymore. its just that, i felt the need to explore some de-stressing activities other than writing my thoughts and rants down. i always found writing rather emotionally helpful, but during those carefree college days, i rarely
did anything to the point of exhaustion.
but im bringing my hiatus onto a peaceful end. good day! gentoo powerpc land!
today… june 10, marks the first anniversary of my emancipation. it has been a challenging past year…
here’s hoping i can last another…
Get free blog up and running in minutes with Blogsome | Theme designs available here