changeset 62:dd3e60aeae26

Middle of fixing for -O3.. works -O0 still -- hard-code field offsets in assbly
author Me
date Fri, 12 Nov 2010 07:36:01 -0800
parents 7b799a46cc87
children a6c442d52590 5cb919ac890f
files CoreLoop.c MasterLoop.c SwitchAnimators.h VMS.c VMS.h
diffstat 5 files changed, 203 insertions(+), 30 deletions(-) [+]
line diff
     1.1 --- a/CoreLoop.c	Mon Nov 08 03:57:46 2010 -0800
     1.2 +++ b/CoreLoop.c	Fri Nov 12 07:36:01 2010 -0800
     1.3 @@ -139,9 +139,32 @@
     1.4        tries++;      //if too many, means master on other core taking too long
     1.5        if( tries > MASTERLOCK_RETRIES ) { tries = 0; pthread_yield(); }
     1.6      }
     1.7 -   
     1.8  
     1.9 -   SwitchToVP( currPr )
    1.10 +/* VirtProcr  offsets:
    1.11 + * 0xc  stackPtr
    1.12 + * 0x10 framePtr
    1.13 + * 0x14 nextInstrPt
    1.14 + * 0x1c coreLoopFramePtr
    1.15 + * 0x20 coreLoopStackPtr
    1.16 + *
    1.17 + * _VMSMasterEnv  offsets:
    1.18 + * 0x24 coreLoopStartPt
    1.19 + * 0x28 coreLoopEndPt
    1.20 + * 0x30 masterLock
    1.21 + */
    1.22 +//   SwitchToVP( currPr )
    1.23 +   asm volatile("movl         %0,      %%ebx;  \
    1.24 +                 movl      %%esp, 0x20(%%ebx); \
    1.25 +                 movl      %%ebp, 0x1c(%%ebx); \
    1.26 +                 movl 0x14(%%ebx),     %%eax;  \
    1.27 +                 movl 0x0c(%%ebx),     %%esp;  \
    1.28 +                 movl 0x10(%%ebx),     %%ebp;  \
    1.29 +                 jmp                  *%%eax"  \
    1.30 +   /* outputs */ :                             \
    1.31 +   /* inputs  */ : "g"(currPr)                 \
    1.32 +   /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
    1.33 +                );
    1.34 +
    1.35  
    1.36     //=========== jmp to here when want to shut down the VMS system ==========
    1.37     CoreLoopEndPt:
     2.1 --- a/MasterLoop.c	Mon Nov 08 03:57:46 2010 -0800
     2.2 +++ b/MasterLoop.c	Fri Nov 12 07:36:01 2010 -0800
     2.3 @@ -170,8 +170,35 @@
     2.4     saveLowTimeStampCountInto( masterPr->endMasterTSCLow );
     2.5     #endif
     2.6  
     2.7 -   
     2.8 -   masterSwitchToCoreLoop( masterPr )
     2.9 +
    2.10 +
    2.11 +/* VirtProcr  offsets:
    2.12 + * 0xc  stackPtr
    2.13 + * 0x10 framePtr
    2.14 + * 0x14 nextInstrPt
    2.15 + * 0x1c coreLoopFramePtr
    2.16 + * 0x20 coreLoopStackPtr
    2.17 + *
    2.18 + * _VMSMasterEnv  offsets:
    2.19 + * 0x24 coreLoopStartPt
    2.20 + * 0x28 coreLoopEndPt
    2.21 + * 0x30 masterLock
    2.22 + */
    2.23 +//   masterSwitchToCoreLoop( masterPr )
    2.24 +   asm volatile("movl         %0,       %%ebx;  \
    2.25 +                 movl         %1,       %%ecx;  \
    2.26 +                 movl      %%esp,  0x0c(%%ecx); \
    2.27 +                 movl      %%ebp,  0x10(%%ecx); \
    2.28 +                 movl 0x24(%%ebx),      %%eax;  \
    2.29 +                 movl 0x20(%%ecx),      %%esp;  \
    2.30 +                 movl 0x1c(%%ecx),      %%ebp;  \
    2.31 +                 movl $0x0,        0x30(%%ebx); \
    2.32 +                 jmp                    %%eax"  \
    2.33 +   /* outputs */ :                              \
    2.34 +   /* inputs  */ : "g"(_VMSMasterEnv), "g"(masterPr)                        \
    2.35 +   /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
    2.36 +                );
    2.37 +
    2.38   }
    2.39  
    2.40  
     3.1 --- a/SwitchAnimators.h	Mon Nov 08 03:57:46 2010 -0800
     3.2 +++ b/SwitchAnimators.h	Fri Nov 12 07:36:01 2010 -0800
     3.3 @@ -18,6 +18,51 @@
     3.4   *And it cleans the code up, having the ugly assembly out of the way
     3.5   */
     3.6  
     3.7 +/*Originally, let GCC handle input and output variables, which
     3.8 + * worked fine with -O0, but with -O3, it eliminated what it
     3.9 + * thought was un-used stores -- to fix this, am now hard-coding
    3.10 + * the offsets of the fields of VirtProcr and VMSMasterEnv data
    3.11 + * structs into the assembly for switching between VPs.
    3.12 + *To see what the offsets are, copy the following code to
    3.13 + * someplace, set compile to -O0 so it doesn't optimize, and
    3.14 + * set a break-point on the first line.  In DDD or Eclipse, look
    3.15 + * at the disassembly to see what it compiled for the offsets.
    3.16 + *
    3.17 +   void *foo;
    3.18 +   foo = currPr->stackPtr; \
    3.19 +   foo = currPr->framePtr; \
    3.20 +   foo = currPr->nextInstrPt; \
    3.21 +   foo = currPr->coreLoopStackPtr;
    3.22 +   foo = currPr->coreLoopFramePtr;
    3.23 +   foo = _VMSMasterEnv->coreLoopStartPt;
    3.24 +   foo = _VMSMasterEnv->coreLoopEndPt;
    3.25 +   foo = _VMSMasterEnv->masterLock;
    3.26 +
    3.27 + * VirtProcr  offsets:
    3.28 + * 0xc  stackPtr
    3.29 + * 0x10 framePtr
    3.30 + * 0x14 nextInstrPt
    3.31 + * 0x1c coreLoopFramePtr
    3.32 + * 0x20 coreLoopStackPtr
    3.33 + *
    3.34 + * _VMSMasterEnv  offsets:
    3.35 + * 0x24 coreLoopStartPt
    3.36 + * 0x28 coreLoopEndPt
    3.37 + * 0x30 masterLock
    3.38 + *
    3.39 + *For reference on the switch-VP assembly, the %%eax, %%ebx, %%ecx are
    3.40 + * general purpose registers -- the "clobber" at the end tells GCC that the
    3.41 + * values in the listed registers are overwritten inside the assembly, so
    3.42 + * that GCC doesn't rely on keeping values in registers across the assembly.
    3.43 + *The "input" tells GCC to generate the assembly form of the variable name.
    3.44 + *The "%0" and "%1" mean the first and second items in the "input" list at
    3.45 + * the bottom, respectively.  So, where %0 appears, GCC looks at the bottom,
    3.46 + * gets the first item it sees, generates the assembly for accessing that
    3.47 + * variable, and replaces %0 with that.
    3.48 + *
    3.49 + *%%ebp is the frame-ptr register and %%esp is the stack-ptr register
    3.50 + */
    3.51 +
    3.52  //=========================== MasterVP to CoreLoop ==========================
    3.53  //
    3.54        //Save stack ptr and frame, restore CoreLoop's stack and frame,
    3.55 @@ -25,18 +70,36 @@
    3.56        //GCC's -O3 messes with this -- go through generated -- protect somehow
    3.57        //
    3.58  #define masterSwitchToCoreLoop( masterPr )   \
    3.59 -   void           *stackPtrAddr, *framePtrAddr, *masterLockAddr; \
    3.60 +   void           *stackPtrAddr, *framePtrAddr; \
    3.61 +   volatile void *masterLockAddr; \
    3.62     void           *jmpPt, *coreLoopFramePtr, *coreLoopStackPtr;  \
    3.63  \
    3.64 -   stackPtrAddr      = &(masterPr->stackPtr); \
    3.65 -   framePtrAddr      = &(masterPr->framePtr); \
    3.66     masterLockAddr    = &(_VMSMasterEnv->masterLock); \
    3.67  \
    3.68     jmpPt             = _VMSMasterEnv->coreLoopStartPt; \
    3.69 +   coreLoopStackPtr  = masterPr->coreLoopStackPtr; \
    3.70     coreLoopFramePtr  = masterPr->coreLoopFramePtr; \
    3.71 -   coreLoopStackPtr  = masterPr->coreLoopStackPtr; \
    3.72  \
    3.73 -   asm volatile("movl %0,     %%eax;  \
    3.74 +   asm volatile("mov  %0,     %%ecx;  \
    3.75 +                 mov  %1,     %%ebx;  \
    3.76 +                 mov  %%ebx,  %%eax;  \
    3.77 +                 add  $0x10,  %%eax;  \
    3.78 +                 movl %%esp, (%%eax); \
    3.79 +                 mov  %%ebx,  %%eax;  \
    3.80 +                 add  $0x14,  %%eax;  \
    3.81 +                 movl %%ebp, (%%eax); \
    3.82 +                 movl %2, %%eax;      \
    3.83 +                 movl %3, %%esp;      \
    3.84 +                 movl %4, %%ebp;      \
    3.85 +                 movl $0x0, (%%ecx);  \
    3.86 +                 jmp  %%eax"          \
    3.87 +   /* outputs */ : "=g"(masterLockAddr)                \
    3.88 +   /* inputs  */ : "g"(masterPr), "g" (jmpPt), "g" (coreLoopStackPtr), \
    3.89 +                   "g" (coreLoopFramePtr) \
    3.90 +   /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
    3.91 +                );
    3.92 +
    3.93 +//   asm volatile("movl %0,     %%eax;  \
    3.94                   movl %%esp, (%%eax); \
    3.95                   movl %1,     %%eax;  \
    3.96                   movl %%ebp, (%%eax); \
    3.97 @@ -57,18 +120,32 @@
    3.98  //
    3.99  
   3.100  #define    SwitchToCoreLoop( animatingPr ) \
   3.101 -   void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr; \
   3.102 +   void *jmpPt, *coreLoopStackPtr; \
   3.103     void *coreLoopFramePtr; \
   3.104  \
   3.105 -   stackPtrAddr      = &(animatingPr->stackPtr); \
   3.106 -   framePtrAddr      = &(animatingPr->framePtr); \
   3.107 +   jmpPt             = _VMSMasterEnv->coreLoopStartPt; \
   3.108 +   coreLoopStackPtr  = animatingPr->coreLoopStackPtr; \
   3.109 +   coreLoopFramePtr  = animatingPr->coreLoopFramePtr; \
   3.110  \
   3.111 -   jmpPt             = _VMSMasterEnv->coreLoopStartPt; \
   3.112 -   coreLoopFramePtr  = animatingPr->coreLoopFramePtr; \
   3.113 -   coreLoopStackPtr  = animatingPr->coreLoopStackPtr; \
   3.114 -\
   3.115 +   asm volatile("mov  %0,%%ebx;       \
   3.116 +                 mov  %%ebx, %%eax;   \
   3.117 +                 add  $0x10, %%eax;   \
   3.118 +                 movl %%esp, (%%eax); \
   3.119 +                 mov  %%ebx, %%eax;   \
   3.120 +                 add  $0x14, %%eax;   \
   3.121 +                 movl %%ebp, (%%eax); \
   3.122 +                 movl %1, %%eax;      \
   3.123 +                 movl %2, %%esp;      \
   3.124 +                 movl %3, %%ebp;      \
   3.125 +                 jmp  %%eax"          \
   3.126 +   /* outputs */ :                    \
   3.127 +   /* inputs  */ : "g"(animatingPr), "g" (jmpPt), "g" (coreLoopStackPtr), \
   3.128 +                   "g" (coreLoopFramePtr) \
   3.129 +   /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
   3.130 +                );
   3.131 +
   3.132        /*Save the virt procr's stack and frame ptrs*/ \
   3.133 -   asm volatile("movl %0,     %%eax;  \
   3.134 +//   asm volatile("movl %0,     %%eax;  \
   3.135                   movl %%esp, (%%eax); \
   3.136                   movl %1,     %%eax;  \
   3.137                   movl %%ebp, (%%eax) "\
   3.138 @@ -110,26 +187,25 @@
   3.139        //switch to virt procr's stack and frame ptr then jump to virt procr fn
   3.140  
   3.141  #define SwitchToVP( currPr ) \
   3.142 -   void *stackPtr, *framePtr, *jmpPt, *coreLoopFramePtrAddr, \
   3.143 -        *coreLoopStackPtrAddr; \
   3.144 +   void *stackPtr, *framePtr, *jmpPt; \
   3.145  \
   3.146     stackPtr = currPr->stackPtr; \
   3.147     framePtr = currPr->framePtr; \
   3.148     jmpPt    = currPr->nextInstrPt; \
   3.149 -   coreLoopFramePtrAddr = &(currPr->coreLoopFramePtr); \
   3.150 -   coreLoopStackPtrAddr = &(currPr->coreLoopStackPtr); \
   3.151  \
   3.152 -   asm volatile("movl %0, %%eax;      \
   3.153 +   asm volatile("mov  %0,%%ebx;       \
   3.154 +                 mov  %%ebx, %%eax;   \
   3.155 +                 add  $0x1c, %%eax;   \
   3.156                   movl %%esp, (%%eax); \
   3.157 +                 mov  %%ebx, %%eax;   \
   3.158 +                 add  $0x20, %%eax;   \
   3.159 +                 movl %%ebp, (%%eax); \
   3.160                   movl %1, %%eax;      \
   3.161 -                 movl %%ebp, (%%eax); \
   3.162 -                 movl %2, %%eax;      \
   3.163 -                 movl %3, %%esp;      \
   3.164 -                 movl %4, %%ebp;      \
   3.165 +                 movl %2, %%esp;      \
   3.166 +                 movl %3, %%ebp;      \
   3.167                   jmp  %%eax"          \
   3.168 -   /* outputs */ : "=g"(coreLoopStackPtrAddr),                 \
   3.169 -                   "=g"(coreLoopFramePtrAddr)                  \
   3.170 -   /* inputs  */ : "g" (jmpPt), "g" (stackPtr), "g" (framePtr) \
   3.171 +   /* outputs */ :                    \
   3.172 +   /* inputs  */ : "g"(currPr), "g" (jmpPt), "g" (stackPtr), "g" (framePtr) \
   3.173     /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
   3.174                  );
   3.175  
     4.1 --- a/VMS.c	Mon Nov 08 03:57:46 2010 -0800
     4.2 +++ b/VMS.c	Fri Nov 12 07:36:01 2010 -0800
     4.3 @@ -358,8 +358,48 @@
     4.4     #endif
     4.5     //=======================================================================
     4.6  
     4.7 +/* VirtProcr  offsets:
     4.8 + * 0xc  stackPtr
     4.9 + * 0x10 framePtr
    4.10 + * 0x14 nextInstrPt
    4.11 + * 0x1c coreLoopFramePtr
    4.12 + * 0x20 coreLoopStackPtr
    4.13 + *
    4.14 + * _VMSMasterEnv  offsets:
    4.15 + * 0x24 coreLoopStartPt
    4.16 + * 0x28 coreLoopEndPt
    4.17 + * 0x30 masterLock
    4.18 + */
    4.19 +//   SwitchToCoreLoop( animatingPr )
    4.20 +   asm volatile("movl         %0,       %%ebx;  \
    4.21 +                 movl         %1,       %%ecx;  \
    4.22 +                 movl      %%esp,  0x0c(%%ecx); \
    4.23 +                 movl      %%ebp,  0x10(%%ecx); \
    4.24 +                 movl 0x24(%%ebx),      %%eax;  \
    4.25 +                 movl 0x20(%%ecx),      %%esp;  \
    4.26 +                 movl 0x1c(%%ecx),      %%ebp;  \
    4.27 +                 jmp                    %%eax"  \
    4.28 +   /* outputs */ :                              \
    4.29 +   /* inputs  */ : "g"(_VMSMasterEnv), "g"(animatingPr)                     \
    4.30 +   /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
    4.31 +                );
    4.32  
    4.33 -   SwitchToCoreLoop( animatingPr )
    4.34 +//  asm volatile("mov  %0,%%ebx;       \
    4.35 +                 mov  %%ebx, %%eax;   \
    4.36 +                 add  $0xc,  %%eax;   \
    4.37 +                 movl %%esp, (%%eax); \
    4.38 +                 mov  %%ebx, %%eax;   \
    4.39 +                 add  $0x10, %%eax;   \
    4.40 +                 movl %%ebp, (%%eax); \
    4.41 +                 movl %1, %%eax;      \
    4.42 +                 movl %2, %%esp;      \
    4.43 +                 movl %3, %%ebp;      \
    4.44 +                 jmp  %%eax"          \
    4.45 +   /* outputs */ :                    \
    4.46 +   /* inputs  */ : "g"(animatingPr), "g" (jmpPt), "g" (coreLoopStackPtr), \
    4.47 +                   "g" (coreLoopFramePtr) \
    4.48 +   /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
    4.49 +                );
    4.50  
    4.51     //=======================================================================
    4.52  ResumePt:
     5.1 --- a/VMS.h	Mon Nov 08 03:57:46 2010 -0800
     5.2 +++ b/VMS.h	Fri Nov 12 07:36:01 2010 -0800
     5.3 @@ -164,6 +164,9 @@
     5.4   };
     5.5  //SchedSlot
     5.6  
     5.7 +/*WARNING: re-arranging this data structure could cause VP switching
     5.8 + *         assembly code to fail -- hard-codes offsets of fields
     5.9 + */
    5.10  struct _VirtProcr
    5.11   { int         procrID;  //for debugging -- count up each time create
    5.12     int         coreAnimatedBy;
    5.13 @@ -199,6 +202,10 @@
    5.14  //VirtProcr
    5.15  
    5.16  
    5.17 +/*WARNING: re-arranging this data structure could cause VP-switching
    5.18 + *         assembly code to fail -- hard-codes offsets of fields
    5.19 + *         (because -O3 messes with things otherwise)
    5.20 + */
    5.21  typedef struct
    5.22   {
    5.23     SlaveScheduler   slaveScheduler;