Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not answer the request twice
[simgrid.git] / src / simix / smx_context_raw.c
1 /* context_raw - context switching with ucontextes from System V           */
2
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "simix/private.h"
10
11 #ifdef HAVE_VALGRIND_VALGRIND_H
12 #  include <valgrind/valgrind.h>
13 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
14
15 #ifdef _XBT_WIN32
16 #include "win32_ucontext.h"
17 #include "win32_ucontext.c"
18 #else
19 #include "ucontext.h"
20 #endif
21
22 /* lower this if you want to reduce the memory consumption  */
23 #ifndef CONTEXT_STACK_SIZE      /* allow lua to override this */
24 #define CONTEXT_STACK_SIZE 128*1024
25 #endif                          /*CONTEXT_STACK_SIZE */
26
27
28 typedef char * raw_stack_t;
29 typedef void (*rawctx_entry_point_t)(void *);
30
31 typedef struct s_smx_ctx_raw {
32   s_smx_ctx_base_t super;       /* Fields of super implementation */
33   char *malloced_stack; /* malloced area containing the stack */
34   raw_stack_t stack_top; /* pointer to stack top (within previous area) */
35   raw_stack_t old_stack_top; /* to whom I should return the control */
36 #ifdef HAVE_VALGRIND_VALGRIND_H
37   unsigned int valgrind_stack_id;       /* the valgrind stack id */
38 #endif
39 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
40
41 smx_ctx_raw_t maestro_raw_context;
42
43 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
44                                    rawctx_entry_point_t entry_point, void* arg);
45 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
46
47 #ifdef PROCESSOR_i686
48 __asm__ (
49    ".text\n"
50    ".globl raw_makecontext\n"
51    ".type raw_makecontext,@function\n"
52    "raw_makecontext:\n"
53    "   movl 4(%esp),%eax\n"   /* stack */
54    "   addl 8(%esp),%eax\n"   /* size  */
55    "   movl 12(%esp),%ecx\n"  /* func  */
56    "   movl 16(%esp),%edx\n"  /* arg   */
57    "   movl %edx, -4(%eax)\n"
58    "   movl $0,   -8(%eax)\n" /* @return for func */
59    "   movl %ecx,-12(%eax)\n"
60    "   movl $0,  -16(%eax)\n" /* ebp */
61    "   movl $0,  -20(%eax)\n" /* ebx */
62    "   movl $0,  -24(%eax)\n" /* esi */
63    "   movl $0,  -28(%eax)\n" /* edi */
64    "   subl $28,%eax\n"
65    "   retl\n"
66 );
67
68 __asm__ (
69    ".text\n"
70    ".globl raw_swapcontext\n"
71    ".type raw_swapcontext,@function\n"
72    "raw_swapcontext:\n"
73    "   movl 4(%esp),%eax\n" /* old */
74    "   movl 8(%esp),%edx\n" /* new */
75    "   pushl %ebp\n"
76    "   pushl %ebx\n"
77    "   pushl %esi\n"
78    "   pushl %edi\n"
79    "   movl %esp,(%eax)\n"
80    "   movl %edx,%esp\n"
81    "   popl %edi\n"
82    "   popl %esi\n"
83    "   popl %ebx\n"
84    "   popl %ebp\n"
85    "   retl\n"
86 );
87 #elif PROCESSOR_x86_64
88 __asm__ (
89    ".text\n"
90    ".globl raw_makecontext\n"
91    ".type raw_makecontext,@function\n"
92    "raw_makecontext:\n" /* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
93    "   movq %rdi,%rax\n"      /* stack */
94    "   addq %rsi,%rax\n"      /* size  */
95    "   movq $0,   -8(%rax)\n" /* @return for func */
96    "   movq %rdx,-16(%rax)\n" /* func */
97    "   movq %rcx,-24(%rax)\n" /* arg/rdi */
98    "   movq $0,  -32(%rax)\n" /* rsi */
99    "   movq $0,  -40(%rax)\n" /* rdx */
100    "   movq $0,  -48(%rax)\n" /* rcx */
101    "   movq $0,  -56(%rax)\n" /* r8  */
102    "   movq $0,  -64(%rax)\n" /* r9  */
103    "   movq $0,  -72(%rax)\n" /* rbp */
104    "   movq $0,  -80(%rax)\n" /* rbx */
105    "   movq $0,  -88(%rax)\n" /* r12 */
106    "   movq $0,  -96(%rax)\n" /* r13 */
107    "   movq $0, -104(%rax)\n" /* r14 */
108    "   movq $0, -112(%rax)\n" /* r15 */
109    "   subq $112,%rax\n"
110    "   retq\n"
111 );
112
113 __asm__ (
114    ".text\n"
115    ".globl raw_swapcontext\n"
116    ".type raw_swapcontext,@function\n"
117    "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
118    "   pushq %rdi\n"
119    "   pushq %rsi\n"
120    "   pushq %rdx\n"
121    "   pushq %rcx\n"
122    "   pushq %r8\n"
123    "   pushq %r9\n"
124    "   pushq %rbp\n"
125    "   pushq %rbx\n"
126    "   pushq %r12\n"
127    "   pushq %r13\n"
128    "   pushq %r14\n"
129    "   pushq %r15\n"
130    "   movq %rsp,(%rdi)\n" /* old */
131    "   movq %rsi,%rsp\n" /* new */
132    "   popq %r15\n"
133    "   popq %r14\n"
134    "   popq %r13\n"
135    "   popq %r12\n"
136    "   popq %rbx\n"
137    "   popq %rbp\n"
138    "   popq %r9\n"
139    "   popq %r8\n"
140    "   popq %rcx\n"
141    "   popq %rdx\n"
142    "   popq %rsi\n"
143    "   popq %rdi\n"
144    "   retq\n"
145 );
146 #else
147 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
148                             rawctx_entry_point_t entry_point, void* arg) {
149    THROW_UNIMPLEMENTED;
150 }
151
152 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
153    THROW_UNIMPLEMENTED;
154 }
155
156 #endif
157
158 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
159
160 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
161
162
163 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
164
165   return smx_ctx_base_factory_finalize(factory);
166 }
167
168 static smx_context_t
169 smx_ctx_raw_create_context_sized(size_t size, xbt_main_func_t code,
170     int argc, char **argv,
171     void_pfn_smxprocess_t cleanup_func,
172     void *data)
173 {
174
175   smx_ctx_raw_t context =
176       (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(size,
177           code,
178           argc,
179           argv,
180           cleanup_func,
181           data);
182
183   /* If the user provided a function for the process then use it
184      otherwise is the context for maestro */
185      if (code) {
186        context->malloced_stack = xbt_malloc0(CONTEXT_STACK_SIZE);
187        context->stack_top =
188            raw_makecontext(context->malloced_stack,CONTEXT_STACK_SIZE,
189                (void(*)(void*))smx_ctx_raw_wrapper,context);
190
191 #ifdef HAVE_VALGRIND_VALGRIND_H
192        context->valgrind_stack_id =
193            VALGRIND_STACK_REGISTER(context->malloced_stack,
194                context->malloced_stack + CONTEXT_STACK_SIZE);
195 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
196
197      }else{
198        maestro_raw_context = context;
199      }
200
201      return (smx_context_t) context;
202
203 }
204
205 static smx_context_t
206 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
207     void_pfn_smxprocess_t cleanup_func,
208     void *data)
209 {
210
211   return smx_ctx_raw_create_context_sized(sizeof(s_smx_ctx_raw_t),
212       code, argc, argv, cleanup_func,
213       data);
214
215 }
216
217 static void smx_ctx_raw_free(smx_context_t context)
218 {
219
220   if (context) {
221
222 #ifdef HAVE_VALGRIND_VALGRIND_H
223     VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
224         context)->valgrind_stack_id);
225 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
226
227   }
228   smx_ctx_base_free(context);
229 }
230
231 static void smx_ctx_raw_suspend(smx_context_t context)
232 {
233   smx_current_context = (smx_context_t)maestro_raw_context;
234   raw_swapcontext(
235       &((smx_ctx_raw_t) context)->stack_top,
236       ((smx_ctx_raw_t) context)->old_stack_top);
237 }
238
239 static void smx_ctx_raw_stop(smx_context_t context)
240 {
241   smx_ctx_base_stop(context);
242   smx_ctx_raw_suspend(context);
243 }
244
245 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
246
247   (context->super.code) (context->super.argc, context->super.argv);
248
249   smx_ctx_raw_stop((smx_context_t) context);
250 }
251
252 static void smx_ctx_raw_resume(smx_context_t context)
253 {
254   smx_current_context = context; 
255   raw_swapcontext(
256       &((smx_ctx_raw_t) context)->old_stack_top,
257       ((smx_ctx_raw_t) context)->stack_top);
258 }
259
260 static void smx_ctx_raw_runall(xbt_dynar_t processes)
261 {
262   smx_process_t process;
263   unsigned int cursor;
264
265   xbt_dynar_foreach(processes, cursor, process) {
266     DEBUG2("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
267     smx_ctx_raw_resume(process->context);
268   }
269   xbt_dynar_reset(processes);
270 }
271
272 static void smx_ctx_raw_resume_parallel(smx_context_t context)
273 {
274   xbt_os_thread_set_extra_data(context);
275   raw_swapcontext(
276       &((smx_ctx_raw_t) context)->old_stack_top,
277       ((smx_ctx_raw_t) context)->stack_top);
278   xbt_os_thread_set_extra_data(NULL);
279 }
280
281 static void smx_ctx_raw_runall_parallel(xbt_dynar_t processes)
282 {
283   return;
284 }
285
286 static smx_context_t smx_ctx_raw_self_parallel(void)
287 {
288   smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
289   return self_context ? self_context : (smx_context_t) maestro_raw_context;
290 }
291
292 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
293 {
294   VERB0("Using raw contexts. Because the glibc is just not good enough for us.");
295   smx_ctx_base_factory_init(factory);
296
297   (*factory)->finalize  = smx_ctx_raw_factory_finalize;
298   (*factory)->create_context = smx_ctx_raw_create_context;
299   /* Do not overload that method (*factory)->finalize */
300   (*factory)->free = smx_ctx_raw_free;
301   (*factory)->stop = smx_ctx_raw_stop;
302   (*factory)->suspend = smx_ctx_raw_suspend;
303   (*factory)->name = "smx_raw_context_factory";
304
305   if(_surf_parallel_contexts){
306 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
307     (*factory)->runall = smx_ctx_raw_runall_parallel;
308     (*factory)->self = smx_ctx_raw_self_parallel;
309 #else
310     THROW0(arg_error, 0, "No thread support for parallel context execution");
311 #endif
312   }else{
313     (*factory)->runall = smx_ctx_raw_runall;
314   }
315 }