+/*
+ * Routines for mutex lock debugging.
+ */
+
+/*
+ * Gets lock check flags in CR6: CR bits 24-27
+ */
+#define CHECK_SETUP(rg) \
+ lbz rg,lglcksWork(0) __ASMNL__ \
+ mtcrf 2,rg __ASMNL__
+
+
+/*
+ * Checks for expected lock type.
+ */
+#define CHECK_MUTEX_TYPE() \
+ bf MUTEX_ATTR_DEBUGb,1f __ASMNL__ \
+ bt 24+disLktypeb,1f __ASMNL__ \
+ lwz r10,MUTEX_TYPE(r3) __ASMNL__ \
+ cmpwi r10,MUTEX_TAG __ASMNL__ \
+ beq++ 1f __ASMNL__ \
+ PROLOG(0) __ASMNL__ \
+ mr r4,r11 __ASMNL__ \
+ mr r5,r10 __ASMNL__ \
+ lis r3,hi16(not_a_mutex) __ASMNL__ \
+ ori r3,r3,lo16(not_a_mutex) __ASMNL__ \
+ bl EXT(panic) __ASMNL__ \
+ BREAKPOINT_TRAP __ASMNL__ \
+1:
+
+ .data
+not_a_mutex:
+ STRINGD "mutex (0x%08X) not a mutex type (0x%08X)\n\000"
+ .text
+
+/*
+ * Verifies return to the correct thread in "unlock" situations.
+ */
+#define CHECK_THREAD(thread_offset) \
+ bf MUTEX_ATTR_DEBUGb,3f __ASMNL__ \
+ bt 24+disLkThreadb,3f __ASMNL__ \
+ mfsprg r10,1 __ASMNL__ \
+ lwz r5,MUTEX_DATA(r3) __ASMNL__ \
+ rlwinm. r9,r5,0,0,29 __ASMNL__ \
+ bne++ 1f __ASMNL__ \
+ lis r3,hi16(not_held) __ASMNL__ \
+ ori r3,r3,lo16(not_held) __ASMNL__ \
+ b 2f __ASMNL__ \
+1: __ASMNL__ \
+ cmpw r9,r10 __ASMNL__ \
+ beq++ 3f __ASMNL__ \
+ mr r5,r10 __ASMNL__ \
+ mr r6,r9 __ASMNL__ \
+ lis r3,hi16(wrong_thread) __ASMNL__ \
+ ori r3,r3,lo16(wrong_thread) __ASMNL__ \
+2: __ASMNL__ \
+ mr r4,r11 __ASMNL__ \
+ PROLOG(0) __ASMNL__ \
+ bl EXT(panic) __ASMNL__ \
+ BREAKPOINT_TRAP __ASMNL__ \
+3:
+
+ .data
+not_held:
+ STRINGD "mutex (0x%08X) not held\n\000"
+wrong_thread:
+ STRINGD "mutex (0x%08X) unlocked by non-owner(0x%08X), current owner(0x%08X)\n\000"
+ .text
+
+#define CHECK_MYLOCK() \
+ bf MUTEX_ATTR_DEBUGb,1f __ASMNL__ \
+ bt 24+disLkMyLckb,1f __ASMNL__ \
+ mfsprg r10,1 __ASMNL__ \
+ lwz r9,MUTEX_DATA(r3) __ASMNL__ \
+ rlwinm r9,r9,0,0,29 __ASMNL__ \
+ cmpw r9,r10 __ASMNL__ \
+ bne++ 1f __ASMNL__ \
+ mr r4,r11 __ASMNL__ \
+ lis r3, hi16(mylock_attempt) __ASMNL__ \
+ ori r3,r3,lo16(mylock_attempt) __ASMNL__ \
+ bl EXT(panic) __ASMNL__ \
+ BREAKPOINT_TRAP __ASMNL__ \
+1:
+
+ .data
+mylock_attempt:
+ STRINGD "mutex (0x%08X) recursive lock attempt\n\000"
+ .text
+
+#define LCK_STACK(lck, stack, lck_stack, frame_cnt, lr_save, tmp) \
+ bf 24+enaLkExtStckb,3f __ASMNL__ \
+ addi lck_stack,lck,MUTEX_STACK __ASMNL__ \
+ li frame_cnt,MUTEX_FRAMES-1 __ASMNL__ \
+1: __ASMNL__ \
+ mr tmp,stack __ASMNL__ \
+ lwz stack,0(stack) __ASMNL__ \
+ xor tmp,stack,tmp __ASMNL__ \
+ cmplwi tmp,8192 __ASMNL__ \
+ bge-- 2f __ASMNL__ \
+ lwz lr_save,FM_LR_SAVE(stack) __ASMNL__ \
+ stwu lr_save,4(lck_stack) __ASMNL__ \
+ subi frame_cnt,frame_cnt,1 __ASMNL__ \
+ cmpi cr0,frame_cnt,0 __ASMNL__ \
+ bne 1b __ASMNL__ \
+ b 3f __ASMNL__ \
+2: __ASMNL__ \
+ li tmp,0 __ASMNL__ \
+ stwu tmp,4(lck_stack) __ASMNL__ \
+ subi frame_cnt,frame_cnt,1 __ASMNL__ \
+ cmpi cr0,frame_cnt,0 __ASMNL__ \
+ bne 2b __ASMNL__ \
+3:
+