Copilot commented on code in PR #3696:
URL: https://github.com/apache/brpc/pull/3696#discussion_r4439980510


##########
test/bthread_context_unittest.cpp:
##########
@@ -110,3 +111,238 @@ TEST(BthreadContextTest, preserves_xmm6_through_xmm15) {
 }  // namespace
 
 #endif  // BTHREAD_CONTEXT_PLATFORM_linux_x86_64
+
+#if defined(BTHREAD_CONTEXT_PLATFORM_linux_ppc64)
+
+namespace {
+
+constexpr size_t kStackSize = 64 * 1024;
+// Non-volatile floating-point state of the ELFv2 ABI: f14-f31 and, when
+// AltiVec is enabled, v20-v31. The layout is what jump_with_fp_state loads
+// and stores.
+constexpr size_t kFprCount = 18;
+#ifdef __ALTIVEC__
+constexpr size_t kVrCount = 12;
+#endif
+
+struct alignas(16) FpState {
+    double fprs[kFprCount];           // f14-f31 at offsets 0..143
+#ifdef __ALTIVEC__
+    uint8_t vrs[kVrCount][16];        // v20-v31 at offsets 144..335
+#endif
+};
+
+bthread_fcontext_t g_main_context;
+bthread_fcontext_t g_test_context;
+FpState g_clobber_state;
+bool g_context_saw_its_state;
+int g_context_entry_rounding_mode;
+int g_context_rounding_mode;
+
+extern "C" intptr_t jump_with_fp_state(
+    bthread_fcontext_t* old_context, bthread_fcontext_t new_context,
+    intptr_t value, const FpState* input, FpState* output);
+
+// Load the registers from *input, switch to new_context and store them into
+// *output once this context is resumed, all in one assembly function so the
+// compiler cannot use any of them in between. The output pointer lives in
+// r31 across the switch, which exercises the GPR part of the frame too.
+__asm__(
+".text\n"
+".align 2\n"
+".type jump_with_fp_state, @function\n"
+"jump_with_fp_state:\n"
+"    addis  %r2, %r12, .TOC.-jump_with_fp_state@ha\n"
+"    addi   %r2, %r2, .TOC.-jump_with_fp_state@l\n"
+"    .localentry jump_with_fp_state, . - jump_with_fp_state\n"
+"    mflr  %r0\n"
+"    std  %r0, 16(%r1)\n"
+"    stdu  %r1, -48(%r1)\n"
+"    std  %r2, 24(%r1)\n"
+"    std  %r31, 32(%r1)\n"
+"    mr  %r31, %r7\n"
+"    lfd  %f14, 0(%r6)\n"
+"    lfd  %f15, 8(%r6)\n"
+"    lfd  %f16, 16(%r6)\n"
+"    lfd  %f17, 24(%r6)\n"
+"    lfd  %f18, 32(%r6)\n"
+"    lfd  %f19, 40(%r6)\n"
+"    lfd  %f20, 48(%r6)\n"
+"    lfd  %f21, 56(%r6)\n"
+"    lfd  %f22, 64(%r6)\n"
+"    lfd  %f23, 72(%r6)\n"
+"    lfd  %f24, 80(%r6)\n"
+"    lfd  %f25, 88(%r6)\n"
+"    lfd  %f26, 96(%r6)\n"
+"    lfd  %f27, 104(%r6)\n"
+"    lfd  %f28, 112(%r6)\n"
+"    lfd  %f29, 120(%r6)\n"
+"    lfd  %f30, 128(%r6)\n"
+"    lfd  %f31, 136(%r6)\n"
+#ifdef __ALTIVEC__
+"    li  %r11, 144\n"
+"    lvx  %v20, %r6, %r11\n"
+"    li  %r11, 160\n"
+"    lvx  %v21, %r6, %r11\n"
+"    li  %r11, 176\n"
+"    lvx  %v22, %r6, %r11\n"
+"    li  %r11, 192\n"
+"    lvx  %v23, %r6, %r11\n"
+"    li  %r11, 208\n"
+"    lvx  %v24, %r6, %r11\n"
+"    li  %r11, 224\n"
+"    lvx  %v25, %r6, %r11\n"
+"    li  %r11, 240\n"
+"    lvx  %v26, %r6, %r11\n"
+"    li  %r11, 256\n"
+"    lvx  %v27, %r6, %r11\n"
+"    li  %r11, 272\n"
+"    lvx  %v28, %r6, %r11\n"
+"    li  %r11, 288\n"
+"    lvx  %v29, %r6, %r11\n"
+"    li  %r11, 304\n"
+"    lvx  %v30, %r6, %r11\n"
+"    li  %r11, 320\n"
+"    lvx  %v31, %r6, %r11\n"
+#endif
+"    # preserve_fpu = false\n"
+"    li  %r6, 0\n"
+"    bl  bthread_jump_fcontext\n"
+"    nop\n"
+"    stfd  %f14, 0(%r31)\n"
+"    stfd  %f15, 8(%r31)\n"
+"    stfd  %f16, 16(%r31)\n"
+"    stfd  %f17, 24(%r31)\n"
+"    stfd  %f18, 32(%r31)\n"
+"    stfd  %f19, 40(%r31)\n"
+"    stfd  %f20, 48(%r31)\n"
+"    stfd  %f21, 56(%r31)\n"
+"    stfd  %f22, 64(%r31)\n"
+"    stfd  %f23, 72(%r31)\n"
+"    stfd  %f24, 80(%r31)\n"
+"    stfd  %f25, 88(%r31)\n"
+"    stfd  %f26, 96(%r31)\n"
+"    stfd  %f27, 104(%r31)\n"
+"    stfd  %f28, 112(%r31)\n"
+"    stfd  %f29, 120(%r31)\n"
+"    stfd  %f30, 128(%r31)\n"
+"    stfd  %f31, 136(%r31)\n"
+#ifdef __ALTIVEC__
+"    li  %r11, 144\n"
+"    stvx  %v20, %r31, %r11\n"
+"    li  %r11, 160\n"
+"    stvx  %v21, %r31, %r11\n"
+"    li  %r11, 176\n"
+"    stvx  %v22, %r31, %r11\n"
+"    li  %r11, 192\n"
+"    stvx  %v23, %r31, %r11\n"
+"    li  %r11, 208\n"
+"    stvx  %v24, %r31, %r11\n"
+"    li  %r11, 224\n"
+"    stvx  %v25, %r31, %r11\n"
+"    li  %r11, 240\n"
+"    stvx  %v26, %r31, %r11\n"
+"    li  %r11, 256\n"
+"    stvx  %v27, %r31, %r11\n"
+"    li  %r11, 272\n"
+"    stvx  %v28, %r31, %r11\n"
+"    li  %r11, 288\n"
+"    stvx  %v29, %r31, %r11\n"
+"    li  %r11, 304\n"
+"    stvx  %v30, %r31, %r11\n"
+"    li  %r11, 320\n"
+"    stvx  %v31, %r31, %r11\n"
+#endif
+"    ld  %r31, 32(%r1)\n"
+"    addi  %r1, %r1, 48\n"
+"    ld  %r0, 16(%r1)\n"
+"    mtlr  %r0\n"
+"    blr\n"

Review Comment:
   `jump_with_fp_state` overwrites f14–f31 and v20–v31 but restores only r31 
before returning. These registers are callee-saved in ELFv2, so the helper can 
corrupt values its C++ callers keep live across the call even when the context 
switch is correct. Save the incoming FP/vector registers before loading the 
test patterns, then restore them after capturing the output.



##########
src/bthread/context.cpp:
##########
@@ -1012,3 +1012,247 @@ __asm (
 "    call    _exit\n"
 );
 #endif
+
+#if defined(BTHREAD_CONTEXT_PLATFORM_linux_ppc64) && 
defined(BTHREAD_CONTEXT_COMPILER_gcc)
+__asm (
+".text\n"
+".align 2\n"
+".globl bthread_jump_fcontext\n"
+".type  bthread_jump_fcontext, @function\n"
+"bthread_jump_fcontext:\n"
+"    addis  %r2, %r12, .TOC.-bthread_jump_fcontext@ha\n"
+"    addi   %r2, %r2, .TOC.-bthread_jump_fcontext@l\n"
+"    .localentry bthread_jump_fcontext, . - bthread_jump_fcontext\n"
+"    # reserve space on stack\n"
+"    subi  %r1, %r1, 528\n"
+"    std  %r14, 352(%r1)\n"
+"    std  %r15, 360(%r1)\n"
+"    std  %r16, 368(%r1)\n"
+"    std  %r17, 376(%r1)\n"
+"    std  %r18, 384(%r1)\n"
+"    std  %r19, 392(%r1)\n"
+"    std  %r20, 400(%r1)\n"
+"    std  %r21, 408(%r1)\n"
+"    std  %r22, 416(%r1)\n"
+"    std  %r23, 424(%r1)\n"
+"    std  %r24, 432(%r1)\n"
+"    std  %r25, 440(%r1)\n"
+"    std  %r26, 448(%r1)\n"
+"    std  %r27, 456(%r1)\n"
+"    std  %r28, 464(%r1)\n"
+"    std  %r29, 472(%r1)\n"
+"    std  %r30, 480(%r1)\n"
+"    std  %r31, 488(%r1)\n"
+"    # save CR\n"
+"    mfcr  %r0\n"
+"    std  %r0, 496(%r1)\n"
+"    # save LR, and LR as PC\n"
+"    mflr  %r0\n"
+"    std  %r0, 504(%r1)\n"
+"    std  %r0, 512(%r1)\n"
+"    # the fourth argument (preserve_fpu) is ignored: the compiler may keep\n"
+"    # any value in the non-volatile FPRs/VRs across the call\n"
+"    stfd  %f14, 200(%r1)\n"
+"    stfd  %f15, 208(%r1)\n"
+"    stfd  %f16, 216(%r1)\n"
+"    stfd  %f17, 224(%r1)\n"
+"    stfd  %f18, 232(%r1)\n"
+"    stfd  %f19, 240(%r1)\n"
+"    stfd  %f20, 248(%r1)\n"
+"    stfd  %f21, 256(%r1)\n"
+"    stfd  %f22, 264(%r1)\n"
+"    stfd  %f23, 272(%r1)\n"
+"    stfd  %f24, 280(%r1)\n"
+"    stfd  %f25, 288(%r1)\n"
+"    stfd  %f26, 296(%r1)\n"
+"    stfd  %f27, 304(%r1)\n"
+"    stfd  %f28, 312(%r1)\n"
+"    stfd  %f29, 320(%r1)\n"
+"    stfd  %f30, 328(%r1)\n"
+"    stfd  %f31, 336(%r1)\n"
+"    mffs  %f0\n"
+"    stfd  %f0, 344(%r1)\n"
+#ifdef __ALTIVEC__
+"    mfspr  %r0, 256\n"
+"    std  %r0, 192(%r1)\n"
+"    li  %r11, 0\n"
+"    stvx  %v20, %r1, %r11\n"
+"    li  %r11, 16\n"
+"    stvx  %v21, %r1, %r11\n"
+"    li  %r11, 32\n"
+"    stvx  %v22, %r1, %r11\n"
+"    li  %r11, 48\n"
+"    stvx  %v23, %r1, %r11\n"
+"    li  %r11, 64\n"
+"    stvx  %v24, %r1, %r11\n"
+"    li  %r11, 80\n"
+"    stvx  %v25, %r1, %r11\n"
+"    li  %r11, 96\n"
+"    stvx  %v26, %r1, %r11\n"
+"    li  %r11, 112\n"
+"    stvx  %v27, %r1, %r11\n"
+"    li  %r11, 128\n"
+"    stvx  %v28, %r1, %r11\n"
+"    li  %r11, 144\n"
+"    stvx  %v29, %r1, %r11\n"
+"    li  %r11, 160\n"
+"    stvx  %v30, %r1, %r11\n"
+"    li  %r11, 176\n"
+"    stvx  %v31, %r1, %r11\n"
+#endif
+"    # store RSP (pointing to context-data) in R3\n"
+"    std  %r1, 0(%r3)\n"
+"    # restore RSP (pointing to context-data) from R4\n"
+"    mr  %r1, %r4\n"
+#ifdef __ALTIVEC__
+"    li  %r11, 0\n"
+"    lvx  %v20, %r1, %r11\n"
+"    li  %r11, 16\n"
+"    lvx  %v21, %r1, %r11\n"
+"    li  %r11, 32\n"
+"    lvx  %v22, %r1, %r11\n"
+"    li  %r11, 48\n"
+"    lvx  %v23, %r1, %r11\n"
+"    li  %r11, 64\n"
+"    lvx  %v24, %r1, %r11\n"
+"    li  %r11, 80\n"
+"    lvx  %v25, %r1, %r11\n"
+"    li  %r11, 96\n"
+"    lvx  %v26, %r1, %r11\n"
+"    li  %r11, 112\n"
+"    lvx  %v27, %r1, %r11\n"
+"    li  %r11, 128\n"
+"    lvx  %v28, %r1, %r11\n"
+"    li  %r11, 144\n"
+"    lvx  %v29, %r1, %r11\n"
+"    li  %r11, 160\n"
+"    lvx  %v30, %r1, %r11\n"
+"    li  %r11, 176\n"
+"    lvx  %v31, %r1, %r11\n"
+"    ld  %r0, 192(%r1)\n"
+"    mtspr  256, %r0\n"
+#endif
+"    lfd  %f0, 344(%r1)\n"
+"    mtfsf  0xff, %f0\n"
+"    lfd  %f14, 200(%r1)\n"
+"    lfd  %f15, 208(%r1)\n"
+"    lfd  %f16, 216(%r1)\n"
+"    lfd  %f17, 224(%r1)\n"
+"    lfd  %f18, 232(%r1)\n"
+"    lfd  %f19, 240(%r1)\n"
+"    lfd  %f20, 248(%r1)\n"
+"    lfd  %f21, 256(%r1)\n"
+"    lfd  %f22, 264(%r1)\n"
+"    lfd  %f23, 272(%r1)\n"
+"    lfd  %f24, 280(%r1)\n"
+"    lfd  %f25, 288(%r1)\n"
+"    lfd  %f26, 296(%r1)\n"
+"    lfd  %f27, 304(%r1)\n"
+"    lfd  %f28, 312(%r1)\n"
+"    lfd  %f29, 320(%r1)\n"
+"    lfd  %f30, 328(%r1)\n"
+"    lfd  %f31, 336(%r1)\n"
+"    ld  %r14, 352(%r1)\n"
+"    ld  %r15, 360(%r1)\n"
+"    ld  %r16, 368(%r1)\n"
+"    ld  %r17, 376(%r1)\n"
+"    ld  %r18, 384(%r1)\n"
+"    ld  %r19, 392(%r1)\n"
+"    ld  %r20, 400(%r1)\n"
+"    ld  %r21, 408(%r1)\n"
+"    ld  %r22, 416(%r1)\n"
+"    ld  %r23, 424(%r1)\n"
+"    ld  %r24, 432(%r1)\n"
+"    ld  %r25, 440(%r1)\n"
+"    ld  %r26, 448(%r1)\n"
+"    ld  %r27, 456(%r1)\n"
+"    ld  %r28, 464(%r1)\n"
+"    ld  %r29, 472(%r1)\n"
+"    ld  %r30, 480(%r1)\n"
+"    ld  %r31, 488(%r1)\n"
+"    # restore CR\n"
+"    ld  %r0, 496(%r1)\n"
+"    mtcr  %r0\n"
+"    # restore LR\n"
+"    ld  %r0, 504(%r1)\n"
+"    mtlr  %r0\n"
+"    # load PC into CTR (and R12 for the callee's TOC setup)\n"
+"    ld  %r12, 512(%r1)\n"
+"    mtctr  %r12\n"
+"    # adjust stack\n"
+"    addi  %r1, %r1, 528\n"
+"    # use third arg as return value after jump\n"
+"    # and as first arg in context function\n"
+"    mr  %r3, %r5\n"
+"    # jump to context\n"
+"    bctr\n"
+".size bthread_jump_fcontext, .-bthread_jump_fcontext\n"
+".section .note.GNU-stack,\"\",%progbits\n"
+);
+
+#endif
+
+#if defined(BTHREAD_CONTEXT_PLATFORM_linux_ppc64) && 
defined(BTHREAD_CONTEXT_COMPILER_gcc)
+__asm (
+".text\n"
+".align 2\n"
+".globl bthread_make_fcontext\n"
+".type  bthread_make_fcontext, @function\n"
+"bthread_make_fcontext:\n"
+"    addis  %r2, %r12, .TOC.-bthread_make_fcontext@ha\n"
+"    addi   %r2, %r2, .TOC.-bthread_make_fcontext@l\n"
+"    .localentry bthread_make_fcontext, . - bthread_make_fcontext\n"
+"    # save return address into R6\n"
+"    mflr  %r6\n"
+"    # first arg of bthread_make_fcontext() == top address of context-stack\n"
+"    # shift address in R3 to lower 16 byte boundary\n"
+"    clrrdi  %r3, %r3, 4\n"
+"    # reserve space for context-data on context-stack\n"
+"    # including 64 byte of linkage + parameter area (R1 % 16 == 0)\n"
+"    subi  %r3, %r3, 592\n"
+"    # third arg of bthread_make_fcontext() == address of context-function\n"
+"    # save address of context-function entry point\n"
+"    std  %r5, 512(%r3)\n"
+"    # initial CR, FPSCR and VRSAVE of the context: the current ones\n"
+"    mfcr  %r0\n"
+"    std  %r0, 496(%r3)\n"
+"    mffs  %f0\n"
+"    stfd  %f0, 344(%r3)\n"
+#ifdef __ALTIVEC__
+"    mfspr  %r0, 256\n"
+"    std  %r0, 192(%r3)\n"
+#endif
+"    # load LR\n"
+"    mflr  %r0\n"
+"    # jump to label 1\n"
+"    bl  1f\n"
+"1:\n"
+"    # load LR into R4\n"
+"    mflr  %r4\n"
+"    # compute abs address of label finish\n"
+"    addi  %r4, %r4, finish - 1b\n"
+"    # restore LR\n"
+"    mtlr  %r0\n"
+"    # save address of finish as return-address for context-function\n"
+"    # will be entered after context-function returns\n"
+"    std  %r4, 504(%r3)\n"
+"    # restore return address from R6\n"
+"    mtlr  %r6\n"
+"    blr  # return pointer to context-data\n"
+"finish:\n"
+"    # save return address into R0\n"
+"    mflr  %r0\n"

Review Comment:
   When libbrpc is shared and the context function resides in the executable or 
another DSO, that function's global entry establishes its own r2/TOC. Returning 
to `finish` does not restore libbrpc's TOC, so the following `_exit` PLT call 
uses the wrong table and can crash instead of exiting normally. Reestablish 
this module's TOC before making that call; LR already contains the address of 
`finish`.



##########
src/bthread/context.cpp:
##########
@@ -1012,3 +1012,247 @@ __asm (
 "    call    _exit\n"
 );
 #endif
+
+#if defined(BTHREAD_CONTEXT_PLATFORM_linux_ppc64) && 
defined(BTHREAD_CONTEXT_COMPILER_gcc)
+__asm (
+".text\n"
+".align 2\n"
+".globl bthread_jump_fcontext\n"
+".type  bthread_jump_fcontext, @function\n"
+"bthread_jump_fcontext:\n"
+"    addis  %r2, %r12, .TOC.-bthread_jump_fcontext@ha\n"
+"    addi   %r2, %r2, .TOC.-bthread_jump_fcontext@l\n"
+"    .localentry bthread_jump_fcontext, . - bthread_jump_fcontext\n"
+"    # reserve space on stack\n"
+"    subi  %r1, %r1, 528\n"
+"    std  %r14, 352(%r1)\n"
+"    std  %r15, 360(%r1)\n"
+"    std  %r16, 368(%r1)\n"
+"    std  %r17, 376(%r1)\n"
+"    std  %r18, 384(%r1)\n"
+"    std  %r19, 392(%r1)\n"
+"    std  %r20, 400(%r1)\n"
+"    std  %r21, 408(%r1)\n"
+"    std  %r22, 416(%r1)\n"
+"    std  %r23, 424(%r1)\n"
+"    std  %r24, 432(%r1)\n"
+"    std  %r25, 440(%r1)\n"
+"    std  %r26, 448(%r1)\n"
+"    std  %r27, 456(%r1)\n"
+"    std  %r28, 464(%r1)\n"
+"    std  %r29, 472(%r1)\n"
+"    std  %r30, 480(%r1)\n"
+"    std  %r31, 488(%r1)\n"
+"    # save CR\n"
+"    mfcr  %r0\n"
+"    std  %r0, 496(%r1)\n"
+"    # save LR, and LR as PC\n"
+"    mflr  %r0\n"
+"    std  %r0, 504(%r1)\n"
+"    std  %r0, 512(%r1)\n"
+"    # the fourth argument (preserve_fpu) is ignored: the compiler may keep\n"
+"    # any value in the non-volatile FPRs/VRs across the call\n"
+"    stfd  %f14, 200(%r1)\n"
+"    stfd  %f15, 208(%r1)\n"
+"    stfd  %f16, 216(%r1)\n"
+"    stfd  %f17, 224(%r1)\n"
+"    stfd  %f18, 232(%r1)\n"
+"    stfd  %f19, 240(%r1)\n"
+"    stfd  %f20, 248(%r1)\n"
+"    stfd  %f21, 256(%r1)\n"
+"    stfd  %f22, 264(%r1)\n"
+"    stfd  %f23, 272(%r1)\n"
+"    stfd  %f24, 280(%r1)\n"
+"    stfd  %f25, 288(%r1)\n"
+"    stfd  %f26, 296(%r1)\n"
+"    stfd  %f27, 304(%r1)\n"
+"    stfd  %f28, 312(%r1)\n"
+"    stfd  %f29, 320(%r1)\n"
+"    stfd  %f30, 328(%r1)\n"
+"    stfd  %f31, 336(%r1)\n"
+"    mffs  %f0\n"
+"    stfd  %f0, 344(%r1)\n"
+#ifdef __ALTIVEC__
+"    mfspr  %r0, 256\n"
+"    std  %r0, 192(%r1)\n"
+"    li  %r11, 0\n"
+"    stvx  %v20, %r1, %r11\n"
+"    li  %r11, 16\n"
+"    stvx  %v21, %r1, %r11\n"
+"    li  %r11, 32\n"
+"    stvx  %v22, %r1, %r11\n"
+"    li  %r11, 48\n"
+"    stvx  %v23, %r1, %r11\n"
+"    li  %r11, 64\n"
+"    stvx  %v24, %r1, %r11\n"
+"    li  %r11, 80\n"
+"    stvx  %v25, %r1, %r11\n"
+"    li  %r11, 96\n"
+"    stvx  %v26, %r1, %r11\n"
+"    li  %r11, 112\n"
+"    stvx  %v27, %r1, %r11\n"
+"    li  %r11, 128\n"
+"    stvx  %v28, %r1, %r11\n"
+"    li  %r11, 144\n"
+"    stvx  %v29, %r1, %r11\n"
+"    li  %r11, 160\n"
+"    stvx  %v30, %r1, %r11\n"
+"    li  %r11, 176\n"
+"    stvx  %v31, %r1, %r11\n"
+#endif
+"    # store RSP (pointing to context-data) in R3\n"
+"    std  %r1, 0(%r3)\n"
+"    # restore RSP (pointing to context-data) from R4\n"
+"    mr  %r1, %r4\n"
+#ifdef __ALTIVEC__
+"    li  %r11, 0\n"
+"    lvx  %v20, %r1, %r11\n"
+"    li  %r11, 16\n"
+"    lvx  %v21, %r1, %r11\n"
+"    li  %r11, 32\n"
+"    lvx  %v22, %r1, %r11\n"
+"    li  %r11, 48\n"
+"    lvx  %v23, %r1, %r11\n"
+"    li  %r11, 64\n"
+"    lvx  %v24, %r1, %r11\n"
+"    li  %r11, 80\n"
+"    lvx  %v25, %r1, %r11\n"
+"    li  %r11, 96\n"
+"    lvx  %v26, %r1, %r11\n"
+"    li  %r11, 112\n"
+"    lvx  %v27, %r1, %r11\n"
+"    li  %r11, 128\n"
+"    lvx  %v28, %r1, %r11\n"
+"    li  %r11, 144\n"
+"    lvx  %v29, %r1, %r11\n"
+"    li  %r11, 160\n"
+"    lvx  %v30, %r1, %r11\n"
+"    li  %r11, 176\n"
+"    lvx  %v31, %r1, %r11\n"
+"    ld  %r0, 192(%r1)\n"
+"    mtspr  256, %r0\n"
+#endif
+"    lfd  %f0, 344(%r1)\n"
+"    mtfsf  0xff, %f0\n"
+"    lfd  %f14, 200(%r1)\n"
+"    lfd  %f15, 208(%r1)\n"
+"    lfd  %f16, 216(%r1)\n"
+"    lfd  %f17, 224(%r1)\n"
+"    lfd  %f18, 232(%r1)\n"
+"    lfd  %f19, 240(%r1)\n"
+"    lfd  %f20, 248(%r1)\n"
+"    lfd  %f21, 256(%r1)\n"
+"    lfd  %f22, 264(%r1)\n"
+"    lfd  %f23, 272(%r1)\n"
+"    lfd  %f24, 280(%r1)\n"
+"    lfd  %f25, 288(%r1)\n"
+"    lfd  %f26, 296(%r1)\n"
+"    lfd  %f27, 304(%r1)\n"
+"    lfd  %f28, 312(%r1)\n"
+"    lfd  %f29, 320(%r1)\n"
+"    lfd  %f30, 328(%r1)\n"
+"    lfd  %f31, 336(%r1)\n"
+"    ld  %r14, 352(%r1)\n"
+"    ld  %r15, 360(%r1)\n"
+"    ld  %r16, 368(%r1)\n"
+"    ld  %r17, 376(%r1)\n"
+"    ld  %r18, 384(%r1)\n"
+"    ld  %r19, 392(%r1)\n"
+"    ld  %r20, 400(%r1)\n"
+"    ld  %r21, 408(%r1)\n"
+"    ld  %r22, 416(%r1)\n"
+"    ld  %r23, 424(%r1)\n"
+"    ld  %r24, 432(%r1)\n"
+"    ld  %r25, 440(%r1)\n"
+"    ld  %r26, 448(%r1)\n"
+"    ld  %r27, 456(%r1)\n"
+"    ld  %r28, 464(%r1)\n"
+"    ld  %r29, 472(%r1)\n"
+"    ld  %r30, 480(%r1)\n"
+"    ld  %r31, 488(%r1)\n"
+"    # restore CR\n"
+"    ld  %r0, 496(%r1)\n"
+"    mtcr  %r0\n"
+"    # restore LR\n"
+"    ld  %r0, 504(%r1)\n"
+"    mtlr  %r0\n"
+"    # load PC into CTR (and R12 for the callee's TOC setup)\n"
+"    ld  %r12, 512(%r1)\n"
+"    mtctr  %r12\n"
+"    # adjust stack\n"
+"    addi  %r1, %r1, 528\n"
+"    # use third arg as return value after jump\n"
+"    # and as first arg in context function\n"
+"    mr  %r3, %r5\n"
+"    # jump to context\n"
+"    bctr\n"
+".size bthread_jump_fcontext, .-bthread_jump_fcontext\n"
+".section .note.GNU-stack,\"\",%progbits\n"
+);
+
+#endif
+
+#if defined(BTHREAD_CONTEXT_PLATFORM_linux_ppc64) && 
defined(BTHREAD_CONTEXT_COMPILER_gcc)
+__asm (
+".text\n"
+".align 2\n"
+".globl bthread_make_fcontext\n"
+".type  bthread_make_fcontext, @function\n"
+"bthread_make_fcontext:\n"
+"    addis  %r2, %r12, .TOC.-bthread_make_fcontext@ha\n"
+"    addi   %r2, %r2, .TOC.-bthread_make_fcontext@l\n"
+"    .localentry bthread_make_fcontext, . - bthread_make_fcontext\n"
+"    # save return address into R6\n"
+"    mflr  %r6\n"
+"    # first arg of bthread_make_fcontext() == top address of context-stack\n"
+"    # shift address in R3 to lower 16 byte boundary\n"
+"    clrrdi  %r3, %r3, 4\n"
+"    # reserve space for context-data on context-stack\n"
+"    # including 64 byte of linkage + parameter area (R1 % 16 == 0)\n"
+"    subi  %r3, %r3, 592\n"

Review Comment:
   The first jump sets r1 to this context pointer plus 528, but the back-chain 
word at that address is never initialized. For a caller-supplied stack 
containing nonzero data, PPC64 stack walkers can follow a garbage 
previous-frame pointer past the context's root. Initialize the initial stack 
frame's back-chain to zero rather than relying on freshly zeroed stack storage.



##########
src/butil/atomicops_internals_gcc.h:
##########
@@ -99,6 +99,88 @@ inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
   return *ptr;
 }
 
+#ifdef ARCH_CPU_64_BITS
+
+inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
+                                         Atomic64 old_value,
+                                         Atomic64 new_value) {
+  Atomic64 prev_value;
+  do {
+    if (__sync_bool_compare_and_swap(ptr, old_value, new_value))
+      return old_value;
+    prev_value = *ptr;
+  } while (prev_value == old_value);
+  return prev_value;

Review Comment:
   After a failed CAS, this function returns a separate `*ptr` reload performed 
after the builtin's barrier, so `Acquire_CompareAndSwap` does not order 
subsequent memory accesses after the value it actually returns on PPC64. Return 
`__sync_val_compare_and_swap` directly so the observed value comes from the 
ordered atomic operation. Apply the same correction to the Atomic32 overload at 
lines 15–25, which PPC64 also uses.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to