1.0.41.28: ppc: Fix race conditions in use of pseudo-atomic in ppc-assem.S.
[sbcl.git] / src / runtime / ppc-assem.S
index 53e8cad..ec19ad5 100644 (file)
@@ -7,6 +7,7 @@
 #include "genesis/simple-fun.h"
 #include "genesis/fdefn.h"
 #include "genesis/closure.h"
+#include "genesis/funcallable-instance.h"
 #include "genesis/static-symbols.h"
 
 #ifdef LISP_FEATURE_DARWIN
@@ -20,7 +21,7 @@
                        .align 3 @ \
 _##x:
 
-#define GFUNCDEF(x)    .globl _/**/x @ \
+#define GFUNCDEF(x)    .globl _ ## x @ \
        FUNCDEF(x)
 #else
 #define FUNCDEF(x)     .text ; \
@@ -72,7 +73,7 @@ x:
 (NFPR_SAVE_BYTES(first_f)+ NGPR_SAVE_BYTES(first_g)+ FRAME_ARG_BYTES(out_arg_words))
 #define SAVE_FPR(n) stfd f##n,-8*(32- n)(r11)
 #define SAVE_GPR(n) stw r##n,-4*(32- n)(r11)
-#define FULL_FRAME_SIZE FRAME_SIZE(FIRST_SAVE_GPR,FIRST_SAVE_FPR,8,1)
+#define FULL_FRAME_SIZE (FRAME_SIZE(FIRST_SAVE_GPR,FIRST_SAVE_FPR,8,1)+15&~15)
 #define RESTORE_FPR(n) lfd f##n,-8*(32- n)(r11)
 #define RESTORE_GPR(n) lwz r##n,-4*(32- n)(r11)
 #else
@@ -311,7 +312,6 @@ x:
 #endif
        /* Turn on pseudo-atomic */
 
-       li reg_NL3,-4
        li reg_ALLOC,4
        store(reg_ZERO,reg_NL4,CSYMBOL(foreign_function_call_active))
        load(reg_NL4,CSYMBOL(dynamic_space_free_pointer))
@@ -321,8 +321,9 @@ x:
        load(reg_OCFP,CSYMBOL(current_control_frame_pointer))
 
        /* No longer atomic, and check for interrupt */
-       add reg_ALLOC,reg_ALLOC,reg_NL3
-       twlti reg_ALLOC,0
+       subi reg_ALLOC,reg_ALLOC,4
+       andi. reg_NL3, reg_ALLOC, 1
+       twnei reg_NL3, 0
 
        /* Pass in the arguments */
 
@@ -339,13 +340,13 @@ x:
        addi reg_LRA,reg_LRA,lo16(lra)
 #else
        lis reg_LRA,lra@h
-       addi reg_LRA,reg_LRA,lra@l
+       ori reg_LRA,reg_LRA,lra@l
 #endif
        addi reg_LRA,reg_LRA,OTHER_POINTER_LOWTAG
 
        /* Function is an indirect closure */
        lwz reg_CODE,SIMPLE_FUN_SELF_OFFSET(reg_LEXENV)
-       addi reg_LIP,reg_CODE,6*4-FUN_POINTER_LOWTAG
+       addi reg_LIP,reg_CODE,SIMPLE_FUN_CODE_OFFSET
        mtctr reg_LIP
        slwi reg_NARGS,reg_NL2,2
        bctr                    
@@ -363,7 +364,6 @@ lra:
        mr REG(3),reg_A0
 
        /* Turn on  pseudo-atomic */
-       li reg_NL3,-4
        la reg_ALLOC,4(reg_ALLOC)
 
        /* Store lisp state */
@@ -380,9 +380,10 @@ lra:
        store(reg_NL1,reg_NL2,CSYMBOL(foreign_function_call_active))
 
        /* Check for interrupt */
-       add reg_ALLOC,reg_ALLOC,reg_NL3
-       twlti reg_ALLOC,0
-
+       subi reg_ALLOC, reg_ALLOC, 4
+       andi. reg_NL3, reg_ALLOC, 1
+       twnei reg_NL3,0
+       
        /* Back to C */
        C_FULL_EPILOG
        blr
@@ -411,7 +412,6 @@ lra:
        mr reg_NARGS,reg_NL3
 
        /* Turn on pseudo-atomic */
-       li reg_NL3,-4
        la reg_ALLOC,4(reg_ALLOC)
 
        /* Convert the return address to an offset and save it on the stack. */
@@ -432,8 +432,10 @@ lra:
        store(reg_CSP,reg_CFUNC,CSYMBOL(foreign_function_call_active))
        /* load(reg_POLL,saver2) */
        /* Disable pseudo-atomic; check pending interrupt */
-       add reg_ALLOC,reg_ALLOC,reg_NL3
-       twlti reg_ALLOC,0
+       subi reg_ALLOC, reg_ALLOC, 4
+       andi. reg_NL3, reg_ALLOC, 1
+       twnei reg_NL3, 0
+
        mr reg_NL3,reg_NARGS
 
 #ifdef LISP_FEATURE_DARWIN
@@ -474,7 +476,7 @@ lra:
        li reg_LIP,0
 
        /* Atomic ... */
-       li reg_NL3,-4
+        li reg_NL3,-4        
        li reg_ALLOC,4
 
        /* No long in foreign function call. */
@@ -497,8 +499,10 @@ lra:
        la reg_LIP,-OTHER_POINTER_LOWTAG(reg_LIP)
 
        /* No longer atomic */
-       add reg_ALLOC,reg_ALLOC,reg_NL3
-       twlti reg_ALLOC,0
+       subi reg_ALLOC, reg_ALLOC, 4
+       andi. reg_NL3, reg_ALLOC, 1
+       twnei reg_NL3, 0
+
        mtlr reg_LIP
        
        /* Reset the lisp stack. */
@@ -512,18 +516,27 @@ lra:
 
        GFUNCDEF(xundefined_tramp)
        .globl CSYMBOL(undefined_tramp)
-       .byte 0,0,0,SIMPLE_FUN_HEADER_WIDETAG
-       .byte 18<<2
-CSYMBOL(undefined_tramp):      
-       .byte 0,0,48
-       .long CSYMBOL(undefined_tramp)
-       .long NIL
-       .long NIL
-       .long NIL
+       .long   SIMPLE_FUN_HEADER_WIDETAG                         /* header */
+       .long   CSYMBOL(undefined_tramp) - SIMPLE_FUN_CODE_OFFSET /* self */
+       .long   NIL                                               /* next */
+       .long   NIL                                               /* name */
+       .long   NIL                                               /* arglist */
+       .long   NIL                                               /* type */
+       .long   NIL                                               /* xref */
+CSYMBOL(undefined_tramp):
+       /* Point reg_CODE to the header and tag it as function, since
+          the debugger regards a function pointer in reg_CODE which
+          doesn't point to a code object as undefined function.  */
+       bcl 20,31,.+4                  /* get address of the next instruction */
+       mflr reg_CODE                  /* header 1 extra word back from here */
+       addi reg_CODE,reg_CODE,-(SIMPLE_FUN_CODE_OFFSET+4)
+       
        twllei reg_ZERO,trap_Cerror
        .byte 4
        .byte UNDEFINED_FUN_ERROR
-       .byte 254, sc_DescriptorReg+0x40, 1     /* 140?  sparc says sc_descriptorReg */
+       .byte 254, sc_DescriptorReg+0x40, 1 /* 140?  sparc says sc_descriptorReg */
+       /* This stuff is for the continuable error.  I don't think there's
+        * any support for it on the lisp side */
        .align 2
 1:     lwz reg_CODE,FDEFN_RAW_ADDR_OFFSET(reg_FDEFN)
        la reg_LIP,SIMPLE_FUN_CODE_OFFSET(reg_CODE)
@@ -536,15 +549,7 @@ CSYMBOL(undefined_tramp):
 
        GFUNCDEF(xclosure_tramp)
        .globl CSYMBOL(closure_tramp)
-       .byte 0,0,0,SIMPLE_FUN_HEADER_WIDETAG
-       .byte 18<<2
 CSYMBOL(closure_tramp):
-       .byte 0,0,24
-       .long CSYMBOL(closure_tramp)
-       .long NIL 
-       .long NIL
-       .long NIL
-       .long NIL
        lwz reg_LEXENV,FDEFN_FUN_OFFSET(reg_FDEFN)
        lwz reg_CODE,CLOSURE_FUN_OFFSET(reg_LEXENV)
        la reg_LIP,SIMPLE_FUN_CODE_OFFSET(reg_CODE)
@@ -553,22 +558,91 @@ CSYMBOL(closure_tramp):
 
        SET_SIZE(xclosure_tramp)
 
-       GFUNCDEF(fun_end_breakpoint_trap)
-       .long 0
-       SET_SIZE(fun_end_breakpoint_trap)
-
-       GFUNCDEF(fun_end_breakpoint)
-       .long 0
-       SET_SIZE(fun_end_breakpoint)
-
+       GFUNCDEF(xfuncallable_instance_tramp)
+       .globl CSYMBOL(funcallable_instance_tramp)
+       .long SIMPLE_FUN_HEADER_WIDETAG
+CSYMBOL(funcallable_instance_tramp) = . + 1
+       .long CSYMBOL(funcallable_instance_tramp)
+       .long NIL
+       .long NIL
+       .long NIL
+       .long NIL
+       .long NIL
+       lwz reg_LEXENV,FUNCALLABLE_INSTANCE_FUNCTION_OFFSET(reg_LEXENV)
+       lwz reg_FDEFN,CLOSURE_FUN_OFFSET(reg_LEXENV)
+       addi reg_LIP,reg_FDEFN,SIMPLE_FUN_CODE_OFFSET
+       mtctr reg_LIP
+       bctr
+       SET_SIZE(funcallable_instance_tramp)
+\f
+       /* The fun_end_breakpoint support here is considered by the
+       authors of the other $ARCH-assem.S files to be magic, and it
+       is.  It is a small fragment of code that is copied into a heap
+       code-object when needed, and contains an LRA object, code to
+       convert a single-value return to unknown-values format, and a
+       trap_FunEndBreakpoint. */
        GFUNCDEF(fun_end_breakpoint_guts)
-       .long 0
-       SET_SIZE(fun_end_breakpoint_guts)
-
-       GFUNCDEF(fun_end_breakpoint_end)
-       .long 0
-       SET_SIZE(fun_end_breakpoint_end)
+       .globl CSYMBOL(fun_end_breakpoint_trap)
+       .globl CSYMBOL(fun_end_breakpoint_end)
+
+       /* Due to pointer verification in MAKE-LISP-OBJ on GENCGC
+       targets, which includes PPC, this must include its header data
+       (the offset from the start of the code-object to the LRA).
+       The code-object header is five words, there are two words of
+       constants, and the instruction space is doubleword-aligned,
+       making an offset of eight.  This is header data for a widetag,
+       so shift left eight bits and add. */
+       .long RETURN_PC_HEADER_WIDETAG + 0x800
+
+       /* We are receiving unknown multiple values, thus must deal
+       with the single-value and multiple-value cases separately. */
+       b fun_end_breakpoint_multiple_values
+       nop
 
+       /* Compute the correct value for reg_CODE based on the LRA.
+       This is a "simple" matter of subtracting a constant from
+       reg_LRA (where the LRA is stored by the return sequence) to
+       obtain a tagged pointer to the enclosing code component.  Both
+       values are tagged OTHER_POINTER_LOWTAG, so we just have to
+       account for the eight words (see calculation for
+       RETURN_PC_HEADER_WIDETAG, above) between the two addresses.
+       Restoring reg_CODE doesn't appear to be strictly necessary
+       here, but let's observe the niceties.*/
+       addi reg_CODE, reg_LRA, -32
+
+       /* Multiple values are stored relative to reg_OCFP, which we
+       set to be the current top-of-stack. */
+       mr reg_OCFP, reg_CSP
+
+       /* Reserve a save location for the one value we have. */
+       addi reg_CSP, reg_CSP, 4
+
+       /* Record the number of values we have as a FIXNUM. */
+       li reg_NARGS, 4
+
+       /* Blank the remaining arg-passing registers. */
+       mr reg_A1, reg_NULL
+       mr reg_A2, reg_NULL
+       mr reg_A3, reg_NULL
+
+       /* And branch to our trap. */
+       b CSYMBOL(fun_end_breakpoint_trap)
+
+fun_end_breakpoint_multiple_values:
+       /* Compute the correct value for reg_CODE.  See the
+       explanation for the single-value case, above. */
+       addi reg_CODE, reg_LRA, -32
+
+       /* The actual magic trap. */
+CSYMBOL(fun_end_breakpoint_trap):
+       twllei  reg_ZERO, trap_FunEndBreakpoint
+
+       /* Finally, the debugger needs to know where the end of the
+       fun_end_breakpoint_guts are, so that it may calculate its size
+       in order to populate out a suitably-sized code object. */
+CSYMBOL(fun_end_breakpoint_end):
+       SET_SIZE(fun_end_breakpoint_guts)
+\f
 
        GFUNCDEF(ppc_flush_cache_line)
        dcbf 0,REG(3)
@@ -579,3 +653,82 @@ CSYMBOL(closure_tramp):
        blr
        SET_SIZE(ppc_flush_cache_line)
 
+        GFUNCDEF(do_pending_interrupt)
+       twllei  reg_ZERO, trap_PendingInterrupt
+       blr
+/* King Nato's branch has a nop here. Do we need this? */
+       SET_SIZE(do_pending_interrupt)
+       
+#if defined LISP_FEATURE_GENCGC
+
+        GFUNCDEF(fpu_save)
+        stfd   FREG(1), 0(REG(3))
+       stfd    FREG(2), 8(REG(3))
+       stfd    FREG(3), 16(REG(3))
+       stfd    FREG(4), 24(REG(3))
+       stfd    FREG(5), 32(REG(3))
+       stfd    FREG(6), 40(REG(3))
+       stfd    FREG(7), 48(REG(3))
+       stfd    FREG(8), 56(REG(3))
+       stfd    FREG(9), 64(REG(3))
+       stfd    FREG(10), 72(REG(3))
+       stfd    FREG(11), 80(REG(3))
+       stfd    FREG(12), 88(REG(3))
+       stfd    FREG(13), 96(REG(3))
+       stfd    FREG(14), 104(REG(3))
+       stfd    FREG(15), 112(REG(3))
+       stfd    FREG(16), 120(REG(3))
+       stfd    FREG(17), 128(REG(3))
+       stfd    FREG(18), 136(REG(3))
+       stfd    FREG(19), 144(REG(3))
+       stfd    FREG(20), 152(REG(3))
+       stfd    FREG(21), 160(REG(3))
+       stfd    FREG(22), 168(REG(3))
+       stfd    FREG(23), 176(REG(3))
+       stfd    FREG(24), 184(REG(3))
+       stfd    FREG(25), 192(REG(3))
+       stfd    FREG(26), 200(REG(3))
+       stfd    FREG(27), 208(REG(3))
+       stfd    FREG(28), 216(REG(3))
+       stfd    FREG(29), 224(REG(3))
+       stfd    FREG(30), 232(REG(3))
+       stfd    FREG(31), 240(REG(3))
+       blr
+       SET_SIZE(fpu_save)
+       
+       GFUNCDEF(fpu_restore)
+       lfd     FREG(1), 0(REG(3))
+       lfd     FREG(2), 8(REG(3))
+       lfd     FREG(3), 16(REG(3))
+       lfd     FREG(4), 24(REG(3))
+       lfd     FREG(5), 32(REG(3))
+       lfd     FREG(6), 40(REG(3))
+       lfd     FREG(7), 48(REG(3))
+       lfd     FREG(8), 56(REG(3))
+       lfd     FREG(9), 64(REG(3))
+       lfd     FREG(10), 72(REG(3))
+       lfd     FREG(11), 80(REG(3))
+       lfd     FREG(12), 88(REG(3))
+       lfd     FREG(13), 96(REG(3))
+       lfd     FREG(14), 104(REG(3))
+       lfd     FREG(15), 112(REG(3))
+       lfd     FREG(16), 120(REG(3))
+       lfd     FREG(17), 128(REG(3))
+       lfd     FREG(18), 136(REG(3))
+       lfd     FREG(19), 144(REG(3))
+       lfd     FREG(20), 152(REG(3))
+       lfd     FREG(21), 160(REG(3))
+       lfd     FREG(22), 168(REG(3))
+       lfd     FREG(23), 176(REG(3))
+       lfd     FREG(24), 184(REG(3))
+       lfd     FREG(25), 192(REG(3))
+       lfd     FREG(26), 200(REG(3))
+       lfd     FREG(27), 208(REG(3))
+       lfd     FREG(28), 216(REG(3))
+       lfd     FREG(29), 224(REG(3))
+       lfd     FREG(30), 232(REG(3))
+       lfd     FREG(31), 240(REG(3))
+        blr
+       SET_SIZE(fpu_restore)
+       
+#endif