CACAO
ucontext.c
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (c) 2003, 2004 *
3  * Panagiotis E. Hadjidoukas (peh@hpclab.ceid.upatras.gr) *
4  * HPCLab - University of Patras. All Rights Reserved. *
5  * Unix ucontext_t on Windows Operating System, May 2004 (revision 1) *
6  * *
7  * The author disclaims all warranties with regard to this software including *
8  * all implied warranties of merchantability and fitness for a particular *
9  * purpose. In no event shall HPCLab be liable for any special, indirect, *
10  * or consequential damages or any damages whatsoever resulting from *
11  * loss of use, data or profits, whether in action of contract negligence, *
12  * or other tortious action, arising out of or in connection with the use *
13  * or performance of this software. *
14  ********************************************************************************/
15 
16 #include "ucontext.h"
17 
19 {
20  int ret;
21 
22  /* Retrieve the full machine context */
23  ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
24  ret = GetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
25 
26  return (ret == 0) ? -1: 0;
27 }
28 
29 int setcontext(const ucontext_t *ucp)
30 {
31  int ret;
32 
33  /* Restore the full machine context (already set) */
34  ret = SetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
35 
36  return (ret == 0) ? -1: 0;
37 }
38 
39 int makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)
40 {
41  int i;
42  va_list ap;
43  char *sp;
44 
45  /* Stack grows down */
46  sp = (char *) (size_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size;
47 
48  /* Reserve stack space for the arguments (maximum possible: argc*(8 bytes per argument)) */
49  sp -= argc*8;
50 
51  if ( sp < (char *)ucp->uc_stack.ss_sp) {
52  /* errno = ENOMEM;*/
53  return -1;
54  }
55 
56  /* Set the instruction and the stack pointer */
57  ucp->uc_mcontext.Eip = (unsigned long) func;
58  ucp->uc_mcontext.Esp = (unsigned long) sp - 4;
59 
60  /* Save/Restore the full machine context */
61  ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
62 
63  /* Copy the arguments */
64  va_start (ap, argc);
65  for (i=0; i<argc; i++) {
66  memcpy(sp, ap, 8);
67  ap +=8;
68  sp += 8;
69  }
70  va_end(ap);
71 
72  return 0;
73 }
74 
75 int swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
76 {
77  int ret;
78 
79  if ((oucp == NULL) || (ucp == NULL)) {
80  /*errno = EINVAL;*/
81  return -1;
82  }
83 
84  ret = getcontext(oucp);
85  if (ret == 0) {
86  ret = setcontext(ucp);
87  }
88  return ret;
89 }
stack_t uc_stack
Definition: md-os.cpp:41
int makecontext(ucontext_t *ucp, void(*func)(), int argc,...)
Definition: ucontext.c:39
struct sigcontext uc_mcontext
Definition: md-os.cpp:42
size_t ss_size
Definition: ucontext.h:23
void * ss_sp
Definition: ucontext.h:22
MIIterator i
#define sp
Definition: md-asm.hpp:81
int getcontext(ucontext_t *ucp)
Definition: ucontext.c:18
int setcontext(const ucontext_t *ucp)
Definition: ucontext.c:29
int swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
Definition: ucontext.c:75