Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : MUTEX_UNLOCK is invisible for MC
[simgrid.git] / src / xbt / win32_ucontext.c
1 /* Copyright (c) 2010-2012, 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /*
8  *      win32-ucontext: Unix ucontext_t operations on Windows platforms
9  *      Copyright(C) 2007 Panagiotis E. Hadjidoukas
10  *
11  *      Contact Email: phadjido@cs.uoi.gr, xdoukas@ceid.upatras.gr
12  *
13  *      win32-ucontext is free software; you can redistribute it and/or
14  *      modify it under the terms of the GNU Lesser General Public
15  *      License as published by the Free Software Foundation; either
16  *      version 2 of the License, or (at your option) any later version.
17  *
18  *      win32-ucontext is distributed in the hope that it will be useful,
19  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  *      Lesser General Public License for more details.
22  *
23  *      You should have received a copy of the GNU Lesser General Public
24  *      License along with SimGrid in the file LICENSE-LGPL-2.1;
25  *      if not, write to the Free Software Foundation, Inc.,
26  *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
27  */  
28     
29 #include "xbt/win32_ucontext.h"
30
31 int getcontext(ucontext_t * ucp) 
32 {
33   int ret;
34   
35       /* Retrieve the full machine context */ 
36       ucp->uc_mcontext.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
37   ret = GetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
38   return (ret == 0) ? -1 : 0;
39 }
40
41 int setcontext(const ucontext_t * ucp) 
42 {
43   int ret;
44   
45       /* Restore the full machine context (already set) */ 
46       ret = SetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
47   return (ret == 0) ? -1 : 0;
48 }
49
50 int makecontext(ucontext_t * ucp, void (*func) (), int argc, ...) 
51 {
52   int i;
53   va_list ap;
54   char *sp;
55
56    /* Stack grows down */
57       sp = (char *) (size_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size;
58   
59       /* Reserve stack space for the arguments (maximum possible: argc*(8 bytes per argument)) */ 
60       sp -= argc * sizeof(void*);
61   if (sp < (char *) ucp->uc_stack.ss_sp) {
62     
63         /* errno = ENOMEM; */ 
64         return -1;
65   }
66   
67       /* Set the instruction and the stack pointer */
68   #ifdef _I_X86_
69   ucp->uc_mcontext.Eip = (DWORD) func;
70   ucp->uc_mcontext.Esp = (DWORD) sp - sizeof(void*);
71   #elif defined _IA64_
72   #  error "_IA64_"
73   #elif defined _AMD64_
74   ucp->uc_mcontext.Rip = (DWORD64) func;
75   ucp->uc_mcontext.Rsp = (DWORD64) sp - sizeof(void*);
76   #else
77   #error "No architecture defined for Windows build. need either _AMD64_ or _I_X86_"
78   #endif
79
80       /* Save/Restore the full machine context */ 
81       ucp->uc_mcontext.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
82   
83       /* Copy the arguments */ 
84       va_start(ap, argc);
85   for (i = 0; i < argc; i++) {
86     memcpy(sp, ap, sizeof(void*));
87     ap += sizeof(void*);
88     sp += sizeof(void*);
89   }
90   va_end(ap);
91   return 0;
92 }
93
94 int swapcontext(ucontext_t * oucp, const ucontext_t * ucp) 
95 {
96   int ret;
97   if ((oucp == NULL) || (ucp == NULL)) {
98     
99         /*errno = EINVAL; */ 
100         return -1;
101   }
102   ret = getcontext(oucp);
103   if (ret == 0) {
104     ret = setcontext(ucp);
105   }
106   return ret;
107 }
108