diff VMS.c @ 70:f9b60012fd74

working ucontext version
author Merten Sach <msach@mailbox.tu-berlin.de>
date Fri, 27 May 2011 12:35:40 +0200
parents 11bfe9d136ed
children efb55f1b5fb9
line diff
     1.1 --- a/VMS.c	Wed May 11 16:13:24 2011 +0200
     1.2 +++ b/VMS.c	Fri May 27 12:35:40 2011 +0200
     1.3 @@ -261,6 +261,7 @@
     1.4        // the Master env and rest of VMS locations
     1.5   }
     1.6  
     1.7 +#ifdef SEQUENTIAL
     1.8  /*Only difference between version with an OS thread pinned to each core and
     1.9   * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
    1.10   */
    1.11 @@ -272,6 +273,7 @@
    1.12        coreLoop_Seq( NULL );
    1.13  
    1.14   }
    1.15 +#endif
    1.16  
    1.17  
    1.18  
    1.19 @@ -287,28 +289,30 @@
    1.20   */
    1.21  inline VirtProcr *
    1.22  create_procr_helper( VirtProcr *newPr,       VirtProcrFnPtr  fnPtr,
    1.23 -                     void      *initialData, char           *stackLocs )
    1.24 +                     void      *initialData, char           *stackLocs,
    1.25 +                     ucontext_t *contextBuf)
    1.26   {
    1.27 -   char  *stackPtr;
    1.28 -
    1.29 +   char  *stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x4);
    1.30 +    
    1.31 +   getcontext(contextBuf);
    1.32 +   contextBuf->uc_stack.ss_sp=stackLocs;
    1.33 +   contextBuf->uc_stack.ss_size=VIRT_PROCR_STACK_SIZE;   
    1.34 +   contextBuf->uc_stack.ss_flags=0;
    1.35 +   makecontext(contextBuf, fnPtr, 2, initialData, newPr);
    1.36 +   contextBuf->uc_link=NULL;
    1.37 +   
    1.38 +   newPr->procrID      = _VMSMasterEnv->numProcrsCreated++;
    1.39 +   newPr->savedVPStatus  = contextBuf;
    1.40 +   newPr->initialData  = initialData;
    1.41     newPr->startOfStack = stackLocs;
    1.42 -   newPr->procrID      = _VMSMasterEnv->numProcrsCreated++;
    1.43 -   newPr->nextInstrPt  = fnPtr;
    1.44 -   newPr->initialData  = initialData;
    1.45     newPr->requests     = NULL;
    1.46     newPr->schedSlot    = NULL;
    1.47 -
    1.48 -      //fnPtr takes two params -- void *initData & void *animProcr
    1.49 -      //alloc stack locations, make stackPtr be the highest addr minus room
    1.50 -      // for 2 params + return addr.  Return addr (NULL) is in loc pointed to
    1.51 -      // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
    1.52 -   stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
    1.53     
    1.54        //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
    1.55 -   *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
    1.56 -   *( (int *)stackPtr + 1 ) = (int) initialData;  //next  param to left
    1.57 -   newPr->stackPtr = stackPtr; //core loop will switch to this, then
    1.58 -   newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
    1.59 +   //*( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
    1.60 +   //*( (int *)stackPtr + 1 ) = (int) initialData;  //next  param to left
    1.61 +   //newPr->stackPtr = stackPtr; //core loop will switch to this, then
    1.62 +   //newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
    1.63  
    1.64     //============================= MEASUREMENT STUFF ========================
    1.65     #ifdef STATS__TURN_ON_PROBES
    1.66 @@ -326,13 +330,16 @@
    1.67  VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
    1.68   { VirtProcr *newPr;
    1.69     char      *stackLocs;
    1.70 +   ucontext_t *contextBuffer;
    1.71  
    1.72     newPr      = VMS__malloc( sizeof(VirtProcr) );
    1.73     stackLocs  = VMS__malloc( VIRT_PROCR_STACK_SIZE );
    1.74 +   contextBuffer  = VMS__malloc(sizeof(ucontext_t));
    1.75 +   
    1.76     if( stackLocs == 0 )
    1.77      { perror("VMS__malloc stack"); exit(1); }
    1.78  
    1.79 -   return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
    1.80 +   return create_procr_helper( newPr, fnPtr, initialData, stackLocs, contextBuffer );
    1.81   }
    1.82  
    1.83  /* "ext" designates that it's for use outside the VMS system -- should only
    1.84 @@ -343,13 +350,16 @@
    1.85  VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
    1.86   { VirtProcr *newPr;
    1.87     char      *stackLocs;
    1.88 +   ucontext_t *contextBuffer;
    1.89  
    1.90     newPr      = malloc( sizeof(VirtProcr) );
    1.91     stackLocs  = malloc( VIRT_PROCR_STACK_SIZE );
    1.92 +   contextBuffer  = VMS__malloc(sizeof(ucontext_t));
    1.93 +   
    1.94     if( stackLocs == 0 )
    1.95      { perror("malloc stack"); exit(1); }
    1.96  
    1.97 -   return create_procr_helper( newPr, fnPtr, initialData, stackLocs );
    1.98 +   return create_procr_helper( newPr, fnPtr, initialData, stackLocs, contextBuffer );
    1.99   }
   1.100  
   1.101  
   1.102 @@ -373,14 +383,7 @@
   1.103  void
   1.104  VMS__suspend_procr( VirtProcr *animatingPr )
   1.105   { 
   1.106 -
   1.107 -      //The request to master will cause this suspended virt procr to get
   1.108 -      // scheduled again at some future point -- to resume, core loop jumps
   1.109 -      // to the resume point (below), which causes restore of saved regs and
   1.110 -      // "return" from this call.
   1.111 -   animatingPr->nextInstrPt = &&ResumePt;
   1.112 -
   1.113 -      //return ownership of the virt procr and sched slot to Master virt pr
   1.114 +    //return ownership of the virt procr and sched slot to Master virt pr
   1.115     animatingPr->schedSlot->workIsDone = TRUE;
   1.116  
   1.117     //===========================  Measurement stuff ========================
   1.118 @@ -389,57 +392,14 @@
   1.119     saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
   1.120     #endif
   1.121     //=======================================================================
   1.122 -
   1.123 -/* VirtProcr  offsets:
   1.124 - * 0xc  stackPtr
   1.125 - * 0x10 framePtr
   1.126 - * 0x14 nextInstrPt
   1.127 - * 0x1c coreLoopFramePtr
   1.128 - * 0x20 coreLoopStackPtr
   1.129 - *
   1.130 - * _VMSMasterEnv  offsets:
   1.131 - * 0x24 coreLoopStartPt
   1.132 - * 0x28 coreLoopEndPt
   1.133 - * 0x30 masterLock
   1.134 - */
   1.135 -//   SwitchToCoreLoop( animatingPr )
   1.136 -   asm volatile("movl         %0,       %%ebx;  \
   1.137 -                 movl         %1,       %%ecx;  \
   1.138 -                 movl      %%esp,  0x0c(%%ecx); \
   1.139 -                 movl      %%ebp,  0x10(%%ecx); \
   1.140 -                 movl 0x24(%%ebx),      %%eax;  \
   1.141 -                 movl 0x20(%%ecx),      %%esp;  \
   1.142 -                 movl 0x1c(%%ecx),      %%ebp;  \
   1.143 -                 jmp                    %%eax"  \
   1.144 -   /* outputs */ :                              \
   1.145 -   /* inputs  */ : "g"(_VMSMasterEnv), "g"(animatingPr)                     \
   1.146 -   /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
   1.147 -                );
   1.148 -
   1.149 -//  asm volatile("mov  %0,%%ebx;       \
   1.150 -                 mov  %%ebx, %%eax;   \
   1.151 -                 add  $0xc,  %%eax;   \
   1.152 -                 movl %%esp, (%%eax); \
   1.153 -                 mov  %%ebx, %%eax;   \
   1.154 -                 add  $0x10, %%eax;   \
   1.155 -                 movl %%ebp, (%%eax); \
   1.156 -                 movl %1, %%eax;      \
   1.157 -                 movl %2, %%esp;      \
   1.158 -                 movl %3, %%ebp;      \
   1.159 -                 jmp  %%eax"          \
   1.160 -   /* outputs */ :                    \
   1.161 -   /* inputs  */ : "g"(animatingPr), "g" (jmpPt), "g" (coreLoopStackPtr), \
   1.162 -                   "g" (coreLoopFramePtr) \
   1.163 -   /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
   1.164 -                );
   1.165 +   
   1.166 +   swapcontext(animatingPr->savedVPStatus,animatingPr->savedCoreLoopStatus);
   1.167  
   1.168     //=======================================================================
   1.169 -ResumePt:
   1.170     #ifdef MEAS__TIME_STAMP_SUSP
   1.171        //NOTE: only take low part of count -- do sanity check when take diff
   1.172     saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
   1.173     #endif
   1.174 -
   1.175     return;
   1.176   }
   1.177  
   1.178 @@ -739,21 +699,15 @@
   1.179  void
   1.180  endOSThreadFn( void *initData, VirtProcr *animatingPr )
   1.181   { void *jmpPt, *coreLoopStackPtr, *coreLoopFramePtr;
   1.182 -
   1.183 -   jmpPt             = _VMSMasterEnv->coreLoopEndPt;
   1.184 -   coreLoopStackPtr  = animatingPr->coreLoopStackPtr;
   1.185 -   coreLoopFramePtr  = animatingPr->coreLoopFramePtr;
   1.186 -
   1.187 -
   1.188 -   asm volatile("movl %0, %%eax;      \
   1.189 -                 movl %1, %%esp;      \
   1.190 -                 movl %2, %%ebp;      \
   1.191 -                 jmp  %%eax    "      \
   1.192 -   /* outputs */ :                    \
   1.193 -   /* inputs  */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
   1.194 -   /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi"  \
   1.195 -                );
   1.196 - }
   1.197 + ucontext_t* contextBuf = animatingPr->savedCoreLoopStatus;
   1.198 + void *stackPtr = contextBuf->uc_mcontext.gregs[REG_ESP];
   1.199 + 
   1.200 + getcontext(contextBuf);
   1.201 + contextBuf->uc_stack.ss_sp=stackPtr;
   1.202 + makecontext(contextBuf,&terminateCoreLoop,1,animatingPr);
   1.203 + contextBuf->uc_link=NULL;
   1.204 + swapcontext(animatingPr->savedVPStatus,contextBuf);
   1.205 +}
   1.206  
   1.207  
   1.208  /*This is called from the startup & shutdown