# HG changeset patch # User Me # Date 1289576161 28800 # Node ID dd3e60aeae26dc93d7aeb2d763db3df3bbd4db78 # Parent 7b799a46cc8721572b30ffa19773b7a4b54bb582 Middle of fixing for -O3.. works -O0 still -- hard-code field offsets in assbly diff -r 7b799a46cc87 -r dd3e60aeae26 CoreLoop.c --- a/CoreLoop.c Mon Nov 08 03:57:46 2010 -0800 +++ b/CoreLoop.c Fri Nov 12 07:36:01 2010 -0800 @@ -139,9 +139,32 @@ tries++; //if too many, means master on other core taking too long if( tries > MASTERLOCK_RETRIES ) { tries = 0; pthread_yield(); } } - - SwitchToVP( currPr ) +/* VirtProcr offsets: + * 0xc stackPtr + * 0x10 framePtr + * 0x14 nextInstrPt + * 0x1c coreLoopFramePtr + * 0x20 coreLoopStackPtr + * + * _VMSMasterEnv offsets: + * 0x24 coreLoopStartPt + * 0x28 coreLoopEndPt + * 0x30 masterLock + */ +// SwitchToVP( currPr ) + asm volatile("movl %0, %%ebx; \ + movl %%esp, 0x20(%%ebx); \ + movl %%ebp, 0x1c(%%ebx); \ + movl 0x14(%%ebx), %%eax; \ + movl 0x0c(%%ebx), %%esp; \ + movl 0x10(%%ebx), %%ebp; \ + jmp *%%eax" \ + /* outputs */ : \ + /* inputs */ : "g"(currPr) \ + /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \ + ); + //=========== jmp to here when want to shut down the VMS system ========== CoreLoopEndPt: diff -r 7b799a46cc87 -r dd3e60aeae26 MasterLoop.c --- a/MasterLoop.c Mon Nov 08 03:57:46 2010 -0800 +++ b/MasterLoop.c Fri Nov 12 07:36:01 2010 -0800 @@ -170,8 +170,35 @@ saveLowTimeStampCountInto( masterPr->endMasterTSCLow ); #endif - - masterSwitchToCoreLoop( masterPr ) + + +/* VirtProcr offsets: + * 0xc stackPtr + * 0x10 framePtr + * 0x14 nextInstrPt + * 0x1c coreLoopFramePtr + * 0x20 coreLoopStackPtr + * + * _VMSMasterEnv offsets: + * 0x24 coreLoopStartPt + * 0x28 coreLoopEndPt + * 0x30 masterLock + */ +// masterSwitchToCoreLoop( masterPr ) + asm volatile("movl %0, %%ebx; \ + movl %1, %%ecx; \ + movl %%esp, 0x0c(%%ecx); \ + movl %%ebp, 0x10(%%ecx); \ + movl 0x24(%%ebx), %%eax; \ + movl 0x20(%%ecx), %%esp; \ + movl 0x1c(%%ecx), %%ebp; \ + movl $0x0, 0x30(%%ebx); \ + jmp %%eax" \ + /* outputs */ : \ + /* inputs */ : "g"(_VMSMasterEnv), "g"(masterPr) \ + /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \ + ); + } diff -r 7b799a46cc87 -r dd3e60aeae26 SwitchAnimators.h --- a/SwitchAnimators.h Mon Nov 08 03:57:46 2010 -0800 +++ b/SwitchAnimators.h Fri Nov 12 07:36:01 2010 -0800 @@ -18,6 +18,51 @@ *And it cleans the code up, having the ugly assembly out of the way */ +/*Originally, let GCC handle input and output variables, which + * worked fine with -O0, but with -O3, it eliminated what it + * thought was un-used stores -- to fix this, am now hard-coding + * the offsets of the fields of VirtProcr and VMSMasterEnv data + * structs into the assembly for switching between VPs. + *To see what the offsets are, copy the following code to + * someplace, set compile to -O0 so it doesn't optimize, and + * set a break-point on the first line. In DDD or Eclipse, look + * at the disassembly to see what it compiled for the offsets. + * + void *foo; + foo = currPr->stackPtr; \ + foo = currPr->framePtr; \ + foo = currPr->nextInstrPt; \ + foo = currPr->coreLoopStackPtr; + foo = currPr->coreLoopFramePtr; + foo = _VMSMasterEnv->coreLoopStartPt; + foo = _VMSMasterEnv->coreLoopEndPt; + foo = _VMSMasterEnv->masterLock; + + * VirtProcr offsets: + * 0xc stackPtr + * 0x10 framePtr + * 0x14 nextInstrPt + * 0x1c coreLoopFramePtr + * 0x20 coreLoopStackPtr + * + * _VMSMasterEnv offsets: + * 0x24 coreLoopStartPt + * 0x28 coreLoopEndPt + * 0x30 masterLock + * + *For reference on the switch-VP assembly, the %%eax, %%ebx, %%ecx are + * general purpose registers -- the "clobber" at the end tells GCC that the + * values in the listed registers are overwritten inside the assembly, so + * that GCC doesn't rely on keeping values in registers across the assembly. + *The "input" tells GCC to generate the assembly form of the variable name. + *The "%0" and "%1" mean the first and second items in the "input" list at + * the bottom, respectively. So, where %0 appears, GCC looks at the bottom, + * gets the first item it sees, generates the assembly for accessing that + * variable, and replaces %0 with that. + * + *%%ebp is the frame-ptr register and %%esp is the stack-ptr register + */ + //=========================== MasterVP to CoreLoop ========================== // //Save stack ptr and frame, restore CoreLoop's stack and frame, @@ -25,18 +70,36 @@ //GCC's -O3 messes with this -- go through generated -- protect somehow // #define masterSwitchToCoreLoop( masterPr ) \ - void *stackPtrAddr, *framePtrAddr, *masterLockAddr; \ + void *stackPtrAddr, *framePtrAddr; \ + volatile void *masterLockAddr; \ void *jmpPt, *coreLoopFramePtr, *coreLoopStackPtr; \ \ - stackPtrAddr = &(masterPr->stackPtr); \ - framePtrAddr = &(masterPr->framePtr); \ masterLockAddr = &(_VMSMasterEnv->masterLock); \ \ jmpPt = _VMSMasterEnv->coreLoopStartPt; \ + coreLoopStackPtr = masterPr->coreLoopStackPtr; \ coreLoopFramePtr = masterPr->coreLoopFramePtr; \ - coreLoopStackPtr = masterPr->coreLoopStackPtr; \ \ - asm volatile("movl %0, %%eax; \ + asm volatile("mov %0, %%ecx; \ + mov %1, %%ebx; \ + mov %%ebx, %%eax; \ + add $0x10, %%eax; \ + movl %%esp, (%%eax); \ + mov %%ebx, %%eax; \ + add $0x14, %%eax; \ + movl %%ebp, (%%eax); \ + movl %2, %%eax; \ + movl %3, %%esp; \ + movl %4, %%ebp; \ + movl $0x0, (%%ecx); \ + jmp %%eax" \ + /* outputs */ : "=g"(masterLockAddr) \ + /* inputs */ : "g"(masterPr), "g" (jmpPt), "g" (coreLoopStackPtr), \ + "g" (coreLoopFramePtr) \ + /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \ + ); + +// asm volatile("movl %0, %%eax; \ movl %%esp, (%%eax); \ movl %1, %%eax; \ movl %%ebp, (%%eax); \ @@ -57,18 +120,32 @@ // #define SwitchToCoreLoop( animatingPr ) \ - void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr; \ + void *jmpPt, *coreLoopStackPtr; \ void *coreLoopFramePtr; \ \ - stackPtrAddr = &(animatingPr->stackPtr); \ - framePtrAddr = &(animatingPr->framePtr); \ + jmpPt = _VMSMasterEnv->coreLoopStartPt; \ + coreLoopStackPtr = animatingPr->coreLoopStackPtr; \ + coreLoopFramePtr = animatingPr->coreLoopFramePtr; \ \ - jmpPt = _VMSMasterEnv->coreLoopStartPt; \ - coreLoopFramePtr = animatingPr->coreLoopFramePtr; \ - coreLoopStackPtr = animatingPr->coreLoopStackPtr; \ -\ + asm volatile("mov %0,%%ebx; \ + mov %%ebx, %%eax; \ + add $0x10, %%eax; \ + movl %%esp, (%%eax); \ + mov %%ebx, %%eax; \ + add $0x14, %%eax; \ + movl %%ebp, (%%eax); \ + movl %1, %%eax; \ + movl %2, %%esp; \ + movl %3, %%ebp; \ + jmp %%eax" \ + /* outputs */ : \ + /* inputs */ : "g"(animatingPr), "g" (jmpPt), "g" (coreLoopStackPtr), \ + "g" (coreLoopFramePtr) \ + /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \ + ); + /*Save the virt procr's stack and frame ptrs*/ \ - asm volatile("movl %0, %%eax; \ +// asm volatile("movl %0, %%eax; \ movl %%esp, (%%eax); \ movl %1, %%eax; \ movl %%ebp, (%%eax) "\ @@ -110,26 +187,25 @@ //switch to virt procr's stack and frame ptr then jump to virt procr fn #define SwitchToVP( currPr ) \ - void *stackPtr, *framePtr, *jmpPt, *coreLoopFramePtrAddr, \ - *coreLoopStackPtrAddr; \ + void *stackPtr, *framePtr, *jmpPt; \ \ stackPtr = currPr->stackPtr; \ framePtr = currPr->framePtr; \ jmpPt = currPr->nextInstrPt; \ - coreLoopFramePtrAddr = &(currPr->coreLoopFramePtr); \ - coreLoopStackPtrAddr = &(currPr->coreLoopStackPtr); \ \ - asm volatile("movl %0, %%eax; \ + asm volatile("mov %0,%%ebx; \ + mov %%ebx, %%eax; \ + add $0x1c, %%eax; \ movl %%esp, (%%eax); \ + mov %%ebx, %%eax; \ + add $0x20, %%eax; \ + movl %%ebp, (%%eax); \ movl %1, %%eax; \ - movl %%ebp, (%%eax); \ - movl %2, %%eax; \ - movl %3, %%esp; \ - movl %4, %%ebp; \ + movl %2, %%esp; \ + movl %3, %%ebp; \ jmp %%eax" \ - /* outputs */ : "=g"(coreLoopStackPtrAddr), \ - "=g"(coreLoopFramePtrAddr) \ - /* inputs */ : "g" (jmpPt), "g" (stackPtr), "g" (framePtr) \ + /* outputs */ : \ + /* inputs */ : "g"(currPr), "g" (jmpPt), "g" (stackPtr), "g" (framePtr) \ /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \ ); diff -r 7b799a46cc87 -r dd3e60aeae26 VMS.c --- a/VMS.c Mon Nov 08 03:57:46 2010 -0800 +++ b/VMS.c Fri Nov 12 07:36:01 2010 -0800 @@ -358,8 +358,48 @@ #endif //======================================================================= +/* VirtProcr offsets: + * 0xc stackPtr + * 0x10 framePtr + * 0x14 nextInstrPt + * 0x1c coreLoopFramePtr + * 0x20 coreLoopStackPtr + * + * _VMSMasterEnv offsets: + * 0x24 coreLoopStartPt + * 0x28 coreLoopEndPt + * 0x30 masterLock + */ +// SwitchToCoreLoop( animatingPr ) + asm volatile("movl %0, %%ebx; \ + movl %1, %%ecx; \ + movl %%esp, 0x0c(%%ecx); \ + movl %%ebp, 0x10(%%ecx); \ + movl 0x24(%%ebx), %%eax; \ + movl 0x20(%%ecx), %%esp; \ + movl 0x1c(%%ecx), %%ebp; \ + jmp %%eax" \ + /* outputs */ : \ + /* inputs */ : "g"(_VMSMasterEnv), "g"(animatingPr) \ + /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \ + ); - SwitchToCoreLoop( animatingPr ) +// asm volatile("mov %0,%%ebx; \ + mov %%ebx, %%eax; \ + add $0xc, %%eax; \ + movl %%esp, (%%eax); \ + mov %%ebx, %%eax; \ + add $0x10, %%eax; \ + movl %%ebp, (%%eax); \ + movl %1, %%eax; \ + movl %2, %%esp; \ + movl %3, %%ebp; \ + jmp %%eax" \ + /* outputs */ : \ + /* inputs */ : "g"(animatingPr), "g" (jmpPt), "g" (coreLoopStackPtr), \ + "g" (coreLoopFramePtr) \ + /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \ + ); //======================================================================= ResumePt: diff -r 7b799a46cc87 -r dd3e60aeae26 VMS.h --- a/VMS.h Mon Nov 08 03:57:46 2010 -0800 +++ b/VMS.h Fri Nov 12 07:36:01 2010 -0800 @@ -164,6 +164,9 @@ }; //SchedSlot +/*WARNING: re-arranging this data structure could cause VP switching + * assembly code to fail -- hard-codes offsets of fields + */ struct _VirtProcr { int procrID; //for debugging -- count up each time create int coreAnimatedBy; @@ -199,6 +202,10 @@ //VirtProcr +/*WARNING: re-arranging this data structure could cause VP-switching + * assembly code to fail -- hard-codes offsets of fields + * (because -O3 messes with things otherwise) + */ typedef struct { SlaveScheduler slaveScheduler;