1 /* context_raw - context switching with ucontextes from System V */
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "simix/private.h"
10 #include "xbt/parmap.h"
13 #ifdef HAVE_VALGRIND_VALGRIND_H
14 # include <valgrind/valgrind.h>
15 #endif /* HAVE_VALGRIND_VALGRIND_H */
18 #include "win32_ucontext.h"
19 #include "win32_ucontext.c"
24 typedef char * raw_stack_t;
25 typedef void (*rawctx_entry_point_t)(void *);
27 typedef struct s_smx_ctx_raw {
28 s_smx_ctx_base_t super; /* Fields of super implementation */
29 char *malloced_stack; /* malloced area containing the stack */
30 raw_stack_t stack_top; /* pointer to stack top (within previous area) */
31 raw_stack_t old_stack_top; /* to whom I should return the control */
32 #ifdef HAVE_VALGRIND_VALGRIND_H
33 unsigned int valgrind_stack_id; /* the valgrind stack id */
35 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
37 smx_ctx_raw_t maestro_raw_context;
39 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
40 rawctx_entry_point_t entry_point, void* arg);
41 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
46 ".globl raw_makecontext\n"
47 ".type raw_makecontext,@function\n"
49 " movl 4(%esp),%eax\n" /* stack */
50 " addl 8(%esp),%eax\n" /* size */
51 " movl 12(%esp),%ecx\n" /* func */
52 " movl 16(%esp),%edx\n" /* arg */
53 " movl %edx, -4(%eax)\n"
54 " movl $0, -8(%eax)\n" /* @return for func */
55 " movl %ecx,-12(%eax)\n"
56 " movl $0, -16(%eax)\n" /* ebp */
57 " movl $0, -20(%eax)\n" /* ebx */
58 " movl $0, -24(%eax)\n" /* esi */
59 " movl $0, -28(%eax)\n" /* edi */
66 ".globl raw_swapcontext\n"
67 ".type raw_swapcontext,@function\n"
69 " movl 4(%esp),%eax\n" /* old */
70 " movl 8(%esp),%edx\n" /* new */
83 #elif PROCESSOR_x86_64
86 ".globl raw_makecontext\n"
87 ".type raw_makecontext,@function\n"
88 "raw_makecontext:\n" /* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
89 " movq %rdi,%rax\n" /* stack */
90 " addq %rsi,%rax\n" /* size */
91 " movq $0, -8(%rax)\n" /* @return for func */
92 " movq %rdx,-16(%rax)\n" /* func */
93 " movq %rcx,-24(%rax)\n" /* arg/rdi */
94 " movq $0, -32(%rax)\n" /* rsi */
95 " movq $0, -40(%rax)\n" /* rdx */
96 " movq $0, -48(%rax)\n" /* rcx */
97 " movq $0, -56(%rax)\n" /* r8 */
98 " movq $0, -64(%rax)\n" /* r9 */
99 " movq $0, -72(%rax)\n" /* rbp */
100 " movq $0, -80(%rax)\n" /* rbx */
101 " movq $0, -88(%rax)\n" /* r12 */
102 " movq $0, -96(%rax)\n" /* r13 */
103 " movq $0, -104(%rax)\n" /* r14 */
104 " movq $0, -112(%rax)\n" /* r15 */
111 ".globl raw_swapcontext\n"
112 ".type raw_swapcontext,@function\n"
113 "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
126 " movq %rsp,(%rdi)\n" /* old */
127 " movq %rsi,%rsp\n" /* new */
143 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
144 rawctx_entry_point_t entry_point, void* arg) {
148 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
154 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
156 static xbt_parmap_t parmap;
158 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
160 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
163 xbt_parmap_destroy(parmap);
164 return smx_ctx_base_factory_finalize(factory);
169 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
170 void_pfn_smxprocess_t cleanup_func,
174 smx_ctx_raw_t context =
175 (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(
176 sizeof(s_smx_ctx_raw_t),
183 /* If the user provided a function for the process then use it
184 otherwise is the context for maestro */
186 context->malloced_stack = xbt_malloc0(smx_context_stack_size);
188 raw_makecontext(context->malloced_stack, smx_context_stack_size,
189 (void(*)(void*))smx_ctx_raw_wrapper,context);
191 #ifdef HAVE_VALGRIND_VALGRIND_H
192 context->valgrind_stack_id =
193 VALGRIND_STACK_REGISTER(context->malloced_stack,
194 context->malloced_stack + smx_context_stack_size);
195 #endif /* HAVE_VALGRIND_VALGRIND_H */
198 maestro_raw_context = context;
201 return (smx_context_t) context;
204 static void smx_ctx_raw_free(smx_context_t context)
209 #ifdef HAVE_VALGRIND_VALGRIND_H
210 VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
211 context)->valgrind_stack_id);
212 #endif /* HAVE_VALGRIND_VALGRIND_H */
214 free(((smx_ctx_raw_t)context)->malloced_stack);
216 smx_ctx_base_free(context);
219 static void smx_ctx_raw_suspend(smx_context_t context)
221 smx_current_context = (smx_context_t)maestro_raw_context;
223 &((smx_ctx_raw_t) context)->stack_top,
224 ((smx_ctx_raw_t) context)->old_stack_top);
227 static void smx_ctx_raw_stop(smx_context_t context)
229 smx_ctx_base_stop(context);
230 smx_ctx_raw_suspend(context);
233 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
235 (context->super.code) (context->super.argc, context->super.argv);
237 smx_ctx_raw_stop((smx_context_t) context);
240 static void smx_ctx_raw_resume(smx_context_t context)
242 smx_current_context = context;
244 &((smx_ctx_raw_t) context)->old_stack_top,
245 ((smx_ctx_raw_t) context)->stack_top);
248 static void smx_ctx_raw_runall(xbt_dynar_t processes)
250 smx_process_t process;
253 xbt_dynar_foreach(processes, cursor, process) {
254 DEBUG2("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
255 smx_ctx_raw_resume(process->context);
257 xbt_dynar_reset(processes);
260 static void smx_ctx_raw_resume_parallel(smx_process_t process)
262 smx_ctx_raw_t context = (smx_ctx_raw_t)process->context;
263 xbt_os_thread_set_extra_data(context);
265 &context->old_stack_top,
267 xbt_os_thread_set_extra_data(NULL);
270 static void smx_ctx_raw_runall_parallel(xbt_dynar_t processes)
272 xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_raw_resume_parallel, processes);
273 xbt_dynar_reset(processes);
276 static smx_context_t smx_ctx_raw_self_parallel(void)
278 smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
279 return self_context ? self_context : (smx_context_t) maestro_raw_context;
282 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
284 VERB0("Using raw contexts. Because the glibc is just not good enough for us.");
285 smx_ctx_base_factory_init(factory);
287 (*factory)->finalize = smx_ctx_raw_factory_finalize;
288 (*factory)->create_context = smx_ctx_raw_create_context;
289 /* Do not overload that method (*factory)->finalize */
290 (*factory)->free = smx_ctx_raw_free;
291 (*factory)->stop = smx_ctx_raw_stop;
292 (*factory)->suspend = smx_ctx_raw_suspend;
293 (*factory)->name = "smx_raw_context_factory";
295 if (smx_parallel_contexts) {
296 #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
297 parmap = xbt_parmap_new(2);
298 (*factory)->runall = smx_ctx_raw_runall_parallel;
299 (*factory)->self = smx_ctx_raw_self_parallel;
301 THROW0(arg_error, 0, "No thread support for parallel context execution");
304 (*factory)->runall = smx_ctx_raw_runall;