# HG changeset patch # User Merten Sach # Date 1308751947 -7200 # Node ID fe5ec83f1bafb4db5ff5b7178c6d7192a62e51b1 # Parent 9ddbb071142df55aa6e3f11a481b32a40f67165f separated hardware dependent code diff -r 9ddbb071142d -r fe5ec83f1baf CoreLoop.c --- a/CoreLoop.c Thu Jun 16 14:41:15 2011 +0200 +++ b/CoreLoop.c Wed Jun 22 16:12:27 2011 +0200 @@ -7,7 +7,7 @@ #include "VMS.h" #include "Queue_impl/BlockingQueue.h" -#include "SwitchAnimators.h" +#include "ProcrContext.h" #include #include diff -r 9ddbb071142d -r fe5ec83f1baf MasterLoop.c --- a/MasterLoop.c Thu Jun 16 14:41:15 2011 +0200 +++ b/MasterLoop.c Wed Jun 22 16:12:27 2011 +0200 @@ -10,7 +10,7 @@ #include #include "VMS.h" -#include "SwitchAnimators.h" +#include "ProcrContext.h" //=========================================================================== diff -r 9ddbb071142d -r fe5ec83f1baf ProcrContext.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ProcrContext.c Wed Jun 22 16:12:27 2011 +0200 @@ -0,0 +1,65 @@ +/* + * This File contains all hardware dependent C code. + */ + + +#include "VMS.h" + +/*Create stack, then create __cdecl structure on it and put initialData and + * pointer to the new structure instance into the parameter positions on + * the stack + *Then put function pointer into nextInstrPt -- the stack is setup in std + * call structure, so jumping to function ptr is same as a GCC generated + * function call + *No need to save registers on old stack frame, because there's no old + * animator state to return to -- + * + */ +inline VirtProcr * +create_procr_helper( VirtProcr *newPr, VirtProcrFnPtr fnPtr, + void *initialData, void *stackLocs ) + { + void *stackPtr; + + newPr->startOfStack = stackLocs; + newPr->procrID = _VMSMasterEnv->numProcrsCreated++; + newPr->initialData = initialData; + newPr->requests = NULL; + newPr->schedSlot = NULL; + + /* + * Hardware dependent part + */ + //instead of calling the function directly, call a wrapper function to fetch + //arguments from stack + newPr->nextInstrPt = (VirtProcrFnPtr)&startVirtProcrFn; + + //fnPtr takes two params -- void *initData & void *animProcr + //alloc stack locations, make stackPtr be the highest addr minus room + // for 2 params + return addr. Return addr (NULL) is in loc pointed to + // by stackPtr, initData at stackPtr + 8 bytes, animatingPr just above + stackPtr = ( (void *)stackLocs + VIRT_PROCR_STACK_SIZE - 4*sizeof(void*)); + + //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp + *((VirtProcr**)stackPtr + 2 ) = newPr; //rightmost param + *((void**)stackPtr + 1 ) = initialData; //next param to left + *((void**)stackPtr) = (void*)fnPtr; + + /* + * end of Hardware dependent part + */ + + newPr->stackPtr = stackPtr; //core loop will switch to this, then + newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr + + //============================= MEASUREMENT STUFF ======================== + #ifdef STATS__TURN_ON_PROBES + struct timeval timeStamp; + gettimeofday( &(timeStamp), NULL); + newPr->createPtInSecs = timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0) - + _VMSMasterEnv->createPtInSecs; + #endif + //======================================================================== + + return newPr; + } \ No newline at end of file diff -r 9ddbb071142d -r fe5ec83f1baf ProcrContext.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ProcrContext.h Wed Jun 22 16:12:27 2011 +0200 @@ -0,0 +1,31 @@ +/* + * Copyright 2009 OpenSourceStewardshipFoundation.org + * Licensed under GNU General Public License version 2 + * + * Author: seanhalle@yahoo.com + * + */ + +#ifndef _ProcrContext_H +#define _ProcrContext_H +#define _GNU_SOURCE + +void saveCoreLoopReturnAddr(void **returnAddress); + +void switchToVP(VirtProcr *nextProcr); + +void switchToCoreLoop(VirtProcr *nextProcr); + +void masterSwitchToCoreLoop(VirtProcr *nextProcr); + +void startVirtProcrFn(); + +#define flushRegisters() \ + asm volatile ("":::"%rbx", "%r12", "%r13","%r14","%r15") + +inline VirtProcr * +create_procr_helper( VirtProcr *newPr, VirtProcrFnPtr fnPtr, + void *initialData, void *stackLocs ); + +#endif /* _ProcrContext_H */ + diff -r 9ddbb071142d -r fe5ec83f1baf SwitchAnimators.h --- a/SwitchAnimators.h Thu Jun 16 14:41:15 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,101 +0,0 @@ -/* - * Copyright 2009 OpenSourceStewardshipFoundation.org - * Licensed under GNU General Public License version 2 - * - * Author: seanhalle@yahoo.com - * - */ - -#ifndef _SwitchAnimators_H -#define _SwitchAnimators_H -#define _GNU_SOURCE - -void saveCoreLoopReturnAddr(void **returnAddress); - -void switchToVP(VirtProcr *nextProcr); - -void switchToCoreLoop(VirtProcr *nextProcr); - -void masterSwitchToCoreLoop(VirtProcr *nextProcr); - -void startVirtProcrFn(); - -#define flushRegisters() \ - asm volatile ("":::"%rbx", "%r12", "%r13","%r14","%r15") - -/*Isolating code for switching between animators within these macros -- at - * some point will make switches to compile for 32 bit or for 64 bit, which - * having these isolated will make cleaner - * - *This also makes it easier to change architectures, at some point - *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 - */ - - - -//=========================== SlaveVP to CoreLoop =========================== -// - - -//============================== CoreLoop to VP ============================= -// - //Save the core loop's stack and frame pointers into virt procr struct - // then switch to stack ptr and frame ptr of virt procr & jmp to it - //This was a pain to get right because GCC converts the "(jmpPt)" to - // frame-relative mem-op -- so generated machine code first changed the - // frame pointer, then tried to jump to an addr stored on stack, which - // it accessed as an offset from frame-ptr! (wrong frame-ptr now) - //Explicitly loading into eax before changing frame-ptr fixed it - //Also, it turns "(currPr->coreLoopFramePtr)" into a temporary on the - // stack, so "movl %%ebp, %0" saves to the temp, NOT the data-struc! - - - - -#endif /* _SwitchAnimators_H */ - diff -r 9ddbb071142d -r fe5ec83f1baf VMS.c --- a/VMS.c Thu Jun 16 14:41:15 2011 +0200 +++ b/VMS.c Wed Jun 22 16:12:27 2011 +0200 @@ -12,7 +12,7 @@ #include #include "VMS.h" -#include "SwitchAnimators.h" +#include "ProcrContext.h" #include "Queue_impl/BlockingQueue.h" #include "Histogram/Histogram.h" @@ -282,65 +282,6 @@ } #endif -/*Create stack, then create __cdecl structure on it and put initialData and - * pointer to the new structure instance into the parameter positions on - * the stack - *Then put function pointer into nextInstrPt -- the stack is setup in std - * call structure, so jumping to function ptr is same as a GCC generated - * function call - *No need to save registers on old stack frame, because there's no old - * animator state to return to -- - * - */ -inline VirtProcr * -create_procr_helper( VirtProcr *newPr, VirtProcrFnPtr fnPtr, - void *initialData, void *stackLocs ) - { - void *stackPtr; - - newPr->startOfStack = stackLocs; - newPr->procrID = _VMSMasterEnv->numProcrsCreated++; - newPr->initialData = initialData; - newPr->requests = NULL; - newPr->schedSlot = NULL; - - /* - * Hardware dependent part - */ - //instead of calling the function directly, call a wrapper function to fetch - //arguments from stack - newPr->nextInstrPt = (VirtProcrFnPtr)&startVirtProcrFn; - - //fnPtr takes two params -- void *initData & void *animProcr - //alloc stack locations, make stackPtr be the highest addr minus room - // for 2 params + return addr. Return addr (NULL) is in loc pointed to - // by stackPtr, initData at stackPtr + 8 bytes, animatingPr just above - stackPtr = ( (void *)stackLocs + VIRT_PROCR_STACK_SIZE - 4*sizeof(void*)); - - //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp - *((VirtProcr**)stackPtr + 2 ) = newPr; //rightmost param - *((void**)stackPtr + 1 ) = initialData; //next param to left - *((void**)stackPtr) = (void*)fnPtr; - - /* - * end of Hardware dependent part - */ - - newPr->stackPtr = stackPtr; //core loop will switch to this, then - newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr - - //============================= MEASUREMENT STUFF ======================== - #ifdef STATS__TURN_ON_PROBES - struct timeval timeStamp; - gettimeofday( &(timeStamp), NULL); - newPr->createPtInSecs = timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0) - - _VMSMasterEnv->createPtInSecs; - #endif - //======================================================================== - - return newPr; - } - inline VirtProcr * VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData ) { VirtProcr *newPr; diff -r 9ddbb071142d -r fe5ec83f1baf VMS.h --- a/VMS.h Thu Jun 16 14:41:15 2011 +0200 +++ b/VMS.h Wed Jun 22 16:12:27 2011 +0200 @@ -564,7 +564,7 @@ //===== -#include "SwitchAnimators.h" +#include "ProcrContext.h" #include "probes.h" #include "vutilities.h"