changeset 77:fe5ec83f1baf

separated hardware dependent code
author Merten Sach <msach@mailbox.tu-berlin.de>
date Wed, 22 Jun 2011 16:12:27 +0200
parents 9ddbb071142d
children 521c75d64cef
files CoreLoop.c MasterLoop.c ProcrContext.c ProcrContext.h SwitchAnimators.h VMS.c VMS.h
diffstat 7 files changed, 100 insertions(+), 164 deletions(-) [+]
line diff
     1.1 --- a/CoreLoop.c	Thu Jun 16 14:41:15 2011 +0200
     1.2 +++ b/CoreLoop.c	Wed Jun 22 16:12:27 2011 +0200
     1.3 @@ -7,7 +7,7 @@
     1.4  
     1.5  #include "VMS.h"
     1.6  #include "Queue_impl/BlockingQueue.h"
     1.7 -#include "SwitchAnimators.h"
     1.8 +#include "ProcrContext.h"
     1.9  
    1.10  #include <stdlib.h>
    1.11  #include <stdio.h>
     2.1 --- a/MasterLoop.c	Thu Jun 16 14:41:15 2011 +0200
     2.2 +++ b/MasterLoop.c	Wed Jun 22 16:12:27 2011 +0200
     2.3 @@ -10,7 +10,7 @@
     2.4  #include <stddef.h>
     2.5  
     2.6  #include "VMS.h"
     2.7 -#include "SwitchAnimators.h"
     2.8 +#include "ProcrContext.h"
     2.9  
    2.10  
    2.11  //===========================================================================
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/ProcrContext.c	Wed Jun 22 16:12:27 2011 +0200
     3.3 @@ -0,0 +1,65 @@
     3.4 +/*
     3.5 + * This File contains all hardware dependent C code.
     3.6 + */
     3.7 +
     3.8 +
     3.9 +#include "VMS.h"
    3.10 +
    3.11 +/*Create stack, then create __cdecl structure on it and put initialData and
    3.12 + * pointer to the new structure instance into the parameter positions on
    3.13 + * the stack
    3.14 + *Then put function pointer into nextInstrPt -- the stack is setup in std
    3.15 + * call structure, so jumping to function ptr is same as a GCC generated
    3.16 + * function call
    3.17 + *No need to save registers on old stack frame, because there's no old
    3.18 + * animator state to return to --
    3.19 + *
    3.20 + */
    3.21 +inline VirtProcr *
    3.22 +create_procr_helper( VirtProcr *newPr,       VirtProcrFnPtr  fnPtr,
    3.23 +                     void      *initialData, void           *stackLocs )
    3.24 + {
    3.25 +   void  *stackPtr;
    3.26 +
    3.27 +   newPr->startOfStack = stackLocs;
    3.28 +   newPr->procrID      = _VMSMasterEnv->numProcrsCreated++;
    3.29 +   newPr->initialData  = initialData;
    3.30 +   newPr->requests     = NULL;
    3.31 +   newPr->schedSlot    = NULL;
    3.32 +
    3.33 +   /*
    3.34 +    * Hardware dependent part           
    3.35 +    */
    3.36 +   //instead of calling the function directly, call a wrapper function to fetch
    3.37 +   //arguments from stack
    3.38 +   newPr->nextInstrPt  = (VirtProcrFnPtr)&startVirtProcrFn;
    3.39 +   
    3.40 +    //fnPtr takes two params -- void *initData & void *animProcr
    3.41 +    //alloc stack locations, make stackPtr be the highest addr minus room
    3.42 +    // for 2 params + return addr.  Return addr (NULL) is in loc pointed to
    3.43 +    // by stackPtr, initData at stackPtr + 8 bytes, animatingPr just above
    3.44 +   stackPtr = ( (void *)stackLocs + VIRT_PROCR_STACK_SIZE - 4*sizeof(void*));
    3.45 +   
    3.46 +      //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
    3.47 +   *((VirtProcr**)stackPtr + 2 ) = newPr; //rightmost param
    3.48 +   *((void**)stackPtr + 1 ) = initialData;  //next  param to left
    3.49 +   *((void**)stackPtr) = (void*)fnPtr;
    3.50 +   
    3.51 +   /*
    3.52 +    * end of Hardware dependent part           
    3.53 +    */
    3.54 +   
    3.55 +   newPr->stackPtr = stackPtr; //core loop will switch to this, then
    3.56 +   newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
    3.57 +
    3.58 +   //============================= MEASUREMENT STUFF ========================
    3.59 +   #ifdef STATS__TURN_ON_PROBES
    3.60 +   struct timeval timeStamp;
    3.61 +   gettimeofday( &(timeStamp), NULL);
    3.62 +   newPr->createPtInSecs = timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0) -
    3.63 +                                               _VMSMasterEnv->createPtInSecs;
    3.64 +   #endif
    3.65 +   //========================================================================
    3.66 +
    3.67 +   return newPr;
    3.68 + }
    3.69 \ No newline at end of file
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/ProcrContext.h	Wed Jun 22 16:12:27 2011 +0200
     4.3 @@ -0,0 +1,31 @@
     4.4 +/*
     4.5 + *  Copyright 2009 OpenSourceStewardshipFoundation.org
     4.6 + *  Licensed under GNU General Public License version 2
     4.7 + *
     4.8 + * Author: seanhalle@yahoo.com
     4.9 + * 
    4.10 + */
    4.11 +
    4.12 +#ifndef _ProcrContext_H
    4.13 +#define	_ProcrContext_H
    4.14 +#define _GNU_SOURCE
    4.15 +
    4.16 +void saveCoreLoopReturnAddr(void **returnAddress);
    4.17 +
    4.18 +void switchToVP(VirtProcr *nextProcr);
    4.19 +
    4.20 +void switchToCoreLoop(VirtProcr *nextProcr);
    4.21 +
    4.22 +void masterSwitchToCoreLoop(VirtProcr *nextProcr);
    4.23 +
    4.24 +void startVirtProcrFn();
    4.25 +
    4.26 +#define flushRegisters() \
    4.27 +        asm volatile ("":::"%rbx", "%r12", "%r13","%r14","%r15")
    4.28 +
    4.29 +inline VirtProcr *
    4.30 +create_procr_helper( VirtProcr *newPr,       VirtProcrFnPtr  fnPtr,
    4.31 +                     void      *initialData, void           *stackLocs );
    4.32 +
    4.33 +#endif	/* _ProcrContext_H */
    4.34 +
     5.1 --- a/SwitchAnimators.h	Thu Jun 16 14:41:15 2011 +0200
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,101 +0,0 @@
     5.4 -/*
     5.5 - *  Copyright 2009 OpenSourceStewardshipFoundation.org
     5.6 - *  Licensed under GNU General Public License version 2
     5.7 - *
     5.8 - * Author: seanhalle@yahoo.com
     5.9 - * 
    5.10 - */
    5.11 -
    5.12 -#ifndef _SwitchAnimators_H
    5.13 -#define	_SwitchAnimators_H
    5.14 -#define _GNU_SOURCE
    5.15 -
    5.16 -void saveCoreLoopReturnAddr(void **returnAddress);
    5.17 -
    5.18 -void switchToVP(VirtProcr *nextProcr);
    5.19 -
    5.20 -void switchToCoreLoop(VirtProcr *nextProcr);
    5.21 -
    5.22 -void masterSwitchToCoreLoop(VirtProcr *nextProcr);
    5.23 -
    5.24 -void startVirtProcrFn();
    5.25 -
    5.26 -#define flushRegisters() \
    5.27 -        asm volatile ("":::"%rbx", "%r12", "%r13","%r14","%r15")
    5.28 -
    5.29 -/*Isolating code for switching between animators within these macros -- at
    5.30 - * some point will make switches to compile for 32 bit or for 64 bit, which
    5.31 - * having these isolated will make cleaner
    5.32 - *
    5.33 - *This also makes it easier to change architectures, at some point
    5.34 - *And it cleans the code up, having the ugly assembly out of the way
    5.35 - */
    5.36 -
    5.37 -/*Originally, let GCC handle input and output variables, which
    5.38 - * worked fine with -O0, but with -O3, it eliminated what it
    5.39 - * thought was un-used stores -- to fix this, am now hard-coding
    5.40 - * the offsets of the fields of VirtProcr and VMSMasterEnv data
    5.41 - * structs into the assembly for switching between VPs.
    5.42 - *To see what the offsets are, copy the following code to
    5.43 - * someplace, set compile to -O0 so it doesn't optimize, and
    5.44 - * set a break-point on the first line.  In DDD or Eclipse, look
    5.45 - * at the disassembly to see what it compiled for the offsets.
    5.46 - *
    5.47 -   void *foo;
    5.48 -   foo = currPr->stackPtr; \
    5.49 -   foo = currPr->framePtr; \
    5.50 -   foo = currPr->nextInstrPt; \
    5.51 -   foo = currPr->coreLoopStackPtr;
    5.52 -   foo = currPr->coreLoopFramePtr;
    5.53 -   foo = _VMSMasterEnv->coreLoopStartPt;
    5.54 -   foo = _VMSMasterEnv->coreLoopEndPt;
    5.55 -   foo = _VMSMasterEnv->masterLock;
    5.56 -
    5.57 - * VirtProcr  offsets:
    5.58 - * 0xc  stackPtr
    5.59 - * 0x10 framePtr
    5.60 - * 0x14 nextInstrPt
    5.61 - * 0x1c coreLoopFramePtr
    5.62 - * 0x20 coreLoopStackPtr
    5.63 - *
    5.64 - * _VMSMasterEnv  offsets:
    5.65 - * 0x24 coreLoopStartPt
    5.66 - * 0x28 coreLoopEndPt
    5.67 - * 0x30 masterLock
    5.68 - *
    5.69 - *For reference on the switch-VP assembly, the %%eax, %%ebx, %%ecx are
    5.70 - * general purpose registers -- the "clobber" at the end tells GCC that the
    5.71 - * values in the listed registers are overwritten inside the assembly, so
    5.72 - * that GCC doesn't rely on keeping values in registers across the assembly.
    5.73 - *The "input" tells GCC to generate the assembly form of the variable name.
    5.74 - *The "%0" and "%1" mean the first and second items in the "input" list at
    5.75 - * the bottom, respectively.  So, where %0 appears, GCC looks at the bottom,
    5.76 - * gets the first item it sees, generates the assembly for accessing that
    5.77 - * variable, and replaces %0 with that.
    5.78 - *
    5.79 - *%%ebp is the frame-ptr register and %%esp is the stack-ptr register
    5.80 - */
    5.81 -
    5.82 -
    5.83 -
    5.84 -//=========================== SlaveVP to CoreLoop ===========================
    5.85 -//
    5.86 -
    5.87 -
    5.88 -//============================== CoreLoop to VP =============================
    5.89 -//
    5.90 -      //Save the core loop's stack and frame pointers into virt procr struct
    5.91 -      // then switch to stack ptr and frame ptr of virt procr & jmp to it
    5.92 -      //This was a pain to get right because GCC converts the "(jmpPt)" to
    5.93 -      // frame-relative mem-op -- so generated machine code first changed the
    5.94 -      // frame pointer, then tried to jump to an addr stored on stack, which
    5.95 -      // it accessed as an offset from frame-ptr!  (wrong frame-ptr now)
    5.96 -      //Explicitly loading into eax before changing frame-ptr fixed it
    5.97 -      //Also, it turns "(currPr->coreLoopFramePtr)" into a temporary on the
    5.98 -      // stack, so "movl %%ebp, %0" saves to the temp, NOT the data-struc!
    5.99 -
   5.100 -
   5.101 -
   5.102 -   
   5.103 -#endif	/* _SwitchAnimators_H */
   5.104 -
     6.1 --- a/VMS.c	Thu Jun 16 14:41:15 2011 +0200
     6.2 +++ b/VMS.c	Wed Jun 22 16:12:27 2011 +0200
     6.3 @@ -12,7 +12,7 @@
     6.4  #include <sys/time.h>
     6.5  
     6.6  #include "VMS.h"
     6.7 -#include "SwitchAnimators.h"
     6.8 +#include "ProcrContext.h"
     6.9  #include "Queue_impl/BlockingQueue.h"
    6.10  #include "Histogram/Histogram.h"
    6.11  
    6.12 @@ -282,65 +282,6 @@
    6.13   }
    6.14  #endif
    6.15  
    6.16 -/*Create stack, then create __cdecl structure on it and put initialData and
    6.17 - * pointer to the new structure instance into the parameter positions on
    6.18 - * the stack
    6.19 - *Then put function pointer into nextInstrPt -- the stack is setup in std
    6.20 - * call structure, so jumping to function ptr is same as a GCC generated
    6.21 - * function call
    6.22 - *No need to save registers on old stack frame, because there's no old
    6.23 - * animator state to return to --
    6.24 - *
    6.25 - */
    6.26 -inline VirtProcr *
    6.27 -create_procr_helper( VirtProcr *newPr,       VirtProcrFnPtr  fnPtr,
    6.28 -                     void      *initialData, void           *stackLocs )
    6.29 - {
    6.30 -   void  *stackPtr;
    6.31 -
    6.32 -   newPr->startOfStack = stackLocs;
    6.33 -   newPr->procrID      = _VMSMasterEnv->numProcrsCreated++;
    6.34 -   newPr->initialData  = initialData;
    6.35 -   newPr->requests     = NULL;
    6.36 -   newPr->schedSlot    = NULL;
    6.37 -
    6.38 -   /*
    6.39 -    * Hardware dependent part           
    6.40 -    */
    6.41 -   //instead of calling the function directly, call a wrapper function to fetch
    6.42 -   //arguments from stack
    6.43 -   newPr->nextInstrPt  = (VirtProcrFnPtr)&startVirtProcrFn;
    6.44 -   
    6.45 -    //fnPtr takes two params -- void *initData & void *animProcr
    6.46 -    //alloc stack locations, make stackPtr be the highest addr minus room
    6.47 -    // for 2 params + return addr.  Return addr (NULL) is in loc pointed to
    6.48 -    // by stackPtr, initData at stackPtr + 8 bytes, animatingPr just above
    6.49 -   stackPtr = ( (void *)stackLocs + VIRT_PROCR_STACK_SIZE - 4*sizeof(void*));
    6.50 -   
    6.51 -      //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
    6.52 -   *((VirtProcr**)stackPtr + 2 ) = newPr; //rightmost param
    6.53 -   *((void**)stackPtr + 1 ) = initialData;  //next  param to left
    6.54 -   *((void**)stackPtr) = (void*)fnPtr;
    6.55 -   
    6.56 -   /*
    6.57 -    * end of Hardware dependent part           
    6.58 -    */
    6.59 -   
    6.60 -   newPr->stackPtr = stackPtr; //core loop will switch to this, then
    6.61 -   newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
    6.62 -
    6.63 -   //============================= MEASUREMENT STUFF ========================
    6.64 -   #ifdef STATS__TURN_ON_PROBES
    6.65 -   struct timeval timeStamp;
    6.66 -   gettimeofday( &(timeStamp), NULL);
    6.67 -   newPr->createPtInSecs = timeStamp.tv_sec +(timeStamp.tv_usec/1000000.0) -
    6.68 -                                               _VMSMasterEnv->createPtInSecs;
    6.69 -   #endif
    6.70 -   //========================================================================
    6.71 -
    6.72 -   return newPr;
    6.73 - }
    6.74 -
    6.75  inline VirtProcr *
    6.76  VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
    6.77   { VirtProcr *newPr;
     7.1 --- a/VMS.h	Thu Jun 16 14:41:15 2011 +0200
     7.2 +++ b/VMS.h	Wed Jun 22 16:12:27 2011 +0200
     7.3 @@ -564,7 +564,7 @@
     7.4  
     7.5  //=====
     7.6  
     7.7 -#include "SwitchAnimators.h"
     7.8 +#include "ProcrContext.h"
     7.9  #include "probes.h"
    7.10  #include "vutilities.h"
    7.11