1 /* Copyright (c) 2010-2012, 2014. The SimGrid Team.
2 * All rights reserved. */
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. */
8 * win32-ucontext: Unix ucontext_t operations on Windows platforms
\r
9 * Copyright(C) 2007 Panagiotis E. Hadjidoukas
\r
11 * Contact Email: phadjido@cs.uoi.gr, xdoukas@ceid.upatras.gr
\r
13 * win32-ucontext is free software; you can redistribute it and/or
\r
14 * modify it under the terms of the GNU Lesser General Public
\r
15 * License as published by the Free Software Foundation; either
\r
16 * version 2 of the License, or (at your option) any later version.
\r
18 * win32-ucontext is distributed in the hope that it will be useful,
\r
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
21 * Lesser General Public License for more details.
\r
23 * You should have received a copy of the GNU Lesser General Public
\r
24 * License along with SimGrid in the file LICENSE-LGPL-2.1;
\r
25 * if not, write to the Free Software Foundation, Inc.,
\r
26 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
\r
29 #include "win32_ucontext.h"
\r
31 int getcontext(ucontext_t * ucp)
\r
35 /* Retrieve the full machine context */
\r
36 ucp->uc_mcontext.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
\r
37 ret = GetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
\r
38 return (ret == 0) ? -1 : 0;
\r
41 int setcontext(const ucontext_t * ucp)
\r
45 /* Restore the full machine context (already set) */
\r
46 ret = SetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
\r
47 return (ret == 0) ? -1 : 0;
\r
50 int makecontext(ucontext_t * ucp, void (*func) (), int argc, ...)
\r
56 /* Stack grows down */
\r
57 sp = (char *) (size_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size;
\r
59 /* Reserve stack space for the arguments (maximum possible: argc*(8 bytes per argument)) */
\r
60 sp -= argc * sizeof(void*);
\r
61 if (sp < (char *) ucp->uc_stack.ss_sp) {
\r
63 /* errno = ENOMEM; */
\r
67 /* Set the instruction and the stack pointer */
\r
69 ucp->uc_mcontext.Eip = (DWORD) func;
\r
70 ucp->uc_mcontext.Esp = (DWORD) sp - sizeof(void*);
\r
76 ucp->uc_mcontext.Rip = (DWORD64) func;
\r
77 ucp->uc_mcontext.Rsp = (DWORD64) sp - sizeof(void*);
\r
80 /* Save/Restore the full machine context */
\r
81 ucp->uc_mcontext.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
\r
83 /* Copy the arguments */
\r
85 for (i = 0; i < argc; i++) {
\r
86 memcpy(sp, ap, sizeof(void*));
\r
87 ap += sizeof(void*);
\r
88 sp += sizeof(void*);
\r
94 int swapcontext(ucontext_t * oucp, const ucontext_t * ucp)
\r
97 if ((oucp == NULL) || (ucp == NULL)) {
\r
99 /*errno = EINVAL; */
\r
102 ret = getcontext(oucp);
\r
104 ret = setcontext(ucp);
\r