Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1a80e46a68df09af0155e90544455d8fcab8e87b
[simgrid.git] / src / kernel / context / ContextRaw.cpp
1 /* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "ContextRaw.hpp"
7 #include "context_private.hpp"
8 #include "mc/mc.h"
9 #include "src/simix/smx_private.hpp"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
12
13 // Raw context routines
14
15 typedef void (*rawctx_entry_point_t)(void *);
16
17 typedef void* raw_stack_t;
18 extern "C" raw_stack_t raw_makecontext(void* malloced_stack, int stack_size,
19                                    rawctx_entry_point_t entry_point, void* arg);
20 extern "C" void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context);
21
22 // TODO, we should handle FP, MMX and the x87 control-word (for x86 and x86_64)
23
24 #if SIMGRID_PROCESSOR_x86_64
25 __asm__ (
26 #if defined(__APPLE__)
27    ".text\n"
28    ".globl _raw_makecontext\n"
29    "_raw_makecontext:\n"
30 #elif defined(_WIN32)
31    ".text\n"
32    ".globl raw_makecontext\n"
33    "raw_makecontext:\n"
34 #else
35    ".text\n"
36    ".globl raw_makecontext\n"
37    ".type raw_makecontext,@function\n"
38    "raw_makecontext:\n"/* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
39 #endif
40    "   mov %rdi,%rax\n"      /* stack */
41    "   add %rsi,%rax\n"      /* size  */
42    "   andq $-16, %rax\n"    /* align stack */
43    "   movq $0,   -8(%rax)\n" /* @return for func */
44    "   mov %rdx,-16(%rax)\n" /* func */
45    "   mov %rcx,-24(%rax)\n" /* arg/rdi */
46    "   movq $0,  -32(%rax)\n" /* rsi */
47    "   movq $0,  -40(%rax)\n" /* rdx */
48    "   movq $0,  -48(%rax)\n" /* rcx */
49    "   movq $0,  -56(%rax)\n" /* r8  */
50    "   movq $0,  -64(%rax)\n" /* r9  */
51    "   movq $0,  -72(%rax)\n" /* rbp */
52    "   movq $0,  -80(%rax)\n" /* rbx */
53    "   movq $0,  -88(%rax)\n" /* r12 */
54    "   movq $0,  -96(%rax)\n" /* r13 */
55    "   movq $0, -104(%rax)\n" /* r14 */
56    "   movq $0, -112(%rax)\n" /* r15 */
57    "   sub $112,%rax\n"
58    "   ret\n"
59 );
60
61 __asm__ (
62 #if defined(__APPLE__)
63    ".text\n"
64    ".globl _raw_swapcontext\n"
65    "_raw_swapcontext:\n"
66 #elif defined(_WIN32)
67    ".text\n"
68    ".globl raw_swapcontext\n"
69    "raw_swapcontext:\n"
70 #else
71    ".text\n"
72    ".globl raw_swapcontext\n"
73    ".type raw_swapcontext,@function\n"
74    "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
75 #endif
76    "   push %rdi\n"
77    "   push %rsi\n"
78    "   push %rdx\n"
79    "   push %rcx\n"
80    "   push %r8\n"
81    "   push %r9\n"
82    "   push %rbp\n"
83    "   push %rbx\n"
84    "   push %r12\n"
85    "   push %r13\n"
86    "   push %r14\n"
87    "   push %r15\n"
88    "   mov %rsp,(%rdi)\n" /* old */
89    "   mov %rsi,%rsp\n" /* new */
90    "   pop %r15\n"
91    "   pop %r14\n"
92    "   pop %r13\n"
93    "   pop %r12\n"
94    "   pop %rbx\n"
95    "   pop %rbp\n"
96    "   pop %r9\n"
97    "   pop %r8\n"
98    "   pop %rcx\n"
99    "   pop %rdx\n"
100    "   pop %rsi\n"
101    "   pop %rdi\n"
102    "   ret\n"
103 );
104 #elif SIMGRID_PROCESSOR_i686
105 __asm__ (
106 #if defined(__APPLE__) || defined(_WIN32)
107    ".text\n"
108    ".globl _raw_makecontext\n"
109    "_raw_makecontext:\n"
110 #else
111    ".text\n"
112    ".globl raw_makecontext\n"
113    ".type raw_makecontext,@function\n"
114    "raw_makecontext:\n"
115 #endif
116    "   movl 4(%esp),%eax\n"   /* stack */
117    "   addl 8(%esp),%eax\n"   /* size  */
118    "   andl $-16, %eax\n"     /* align stack */
119    "   movl 12(%esp),%ecx\n"  /* func  */
120    "   movl 16(%esp),%edx\n"  /* arg   */
121    "   movl %edx, -4(%eax)\n"
122    "   movl $0,   -8(%eax)\n" /* @return for func */
123    "   movl %ecx,-12(%eax)\n"
124    "   movl $0,  -16(%eax)\n" /* ebp */
125    "   movl $0,  -20(%eax)\n" /* ebx */
126    "   movl $0,  -24(%eax)\n" /* esi */
127    "   movl $0,  -28(%eax)\n" /* edi */
128    "   subl $28,%eax\n"
129    "   retl\n"
130 );
131
132 __asm__ (
133 #if defined(__APPLE__) || defined(_WIN32)
134    ".text\n"
135    ".globl _raw_swapcontext\n"
136    "_raw_swapcontext:\n"
137 #else
138    ".text\n"
139    ".globl raw_swapcontext\n"
140    ".type raw_swapcontext,@function\n"
141    "raw_swapcontext:\n"
142 #endif
143    // Fetch the parameters:
144    "   movl 4(%esp),%eax\n" /* old (raw_stack_t*) */
145    "   movl 8(%esp),%edx\n" /* new (raw_stack_t)  */
146    // Save registers of the current context on the stack:
147    "   pushl %ebp\n"
148    "   pushl %ebx\n"
149    "   pushl %esi\n"
150    "   pushl %edi\n"
151    // Save the current context (stack pointer) in *old:
152    "   movl %esp,(%eax)\n"
153    // Switch to the stack of the new context:
154    "   movl %edx,%esp\n"
155    // Pop the values of the new context:
156    "   popl %edi\n"
157    "   popl %esi\n"
158    "   popl %ebx\n"
159    "   popl %ebp\n"
160    // Return using the return address of the new context:
161    "   retl\n"
162 );
163 #else
164
165
166 /* If you implement raw contexts for other processors, don't forget to
167    update the definition of HAVE_RAW_CONTEXTS in tools/cmake/CompleteInFiles.cmake */
168
169 raw_stack_t raw_makecontext(void* malloced_stack, int stack_size,
170                             rawctx_entry_point_t entry_point, void* arg) {
171    THROW_UNIMPLEMENTED;
172 }
173
174 void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context) {
175    THROW_UNIMPLEMENTED;
176 }
177
178 #endif
179
180 // ***** Method definitions
181
182 namespace simgrid {
183 namespace kernel {
184 namespace context {
185
186 // RawContextFactory
187
188 RawContextFactory::RawContextFactory() : ContextFactory("RawContextFactory"), parallel_(SIMIX_context_is_parallel())
189 {
190   RawContext::setMaestro(nullptr);
191   if (parallel_) {
192 #if HAVE_THREAD_CONTEXTS
193     // TODO: choose dynamically when SIMIX_context_get_parallel_threshold() > 1
194     ParallelRawContext::initialize();
195 #else
196     xbt_die("You asked for a parallel execution, but you don't have any threads.");
197 #endif
198   }
199 }
200
201 RawContextFactory::~RawContextFactory()
202 {
203 #if HAVE_THREAD_CONTEXTS
204   if (parallel_)
205     ParallelRawContext::finalize();
206 #endif
207 }
208
209 Context* RawContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
210                                            smx_actor_t process)
211 {
212 #if HAVE_THREAD_CONTEXTS
213   if (parallel_)
214     return this->new_context<ParallelRawContext>(std::move(code), cleanup_func, process);
215 #endif
216
217   return this->new_context<SerialRawContext>(std::move(code), cleanup_func, process);
218 }
219
220 void RawContextFactory::run_all()
221 {
222 #if HAVE_THREAD_CONTEXTS
223   if (parallel_)
224     ParallelRawContext::run_all();
225   else
226 #endif
227     SerialRawContext::run_all();
228 }
229
230 // RawContext
231
232 RawContext* RawContext::maestro_context_ = nullptr;
233
234 RawContext::RawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process)
235     : Context(std::move(code), cleanup, process)
236 {
237    if (has_code()) {
238      this->stack_ = SIMIX_context_stack_new();
239 #if PTH_STACKGROWTH == -1
240      ASAN_EVAL(this->asan_stack_ = static_cast<char*>(this->stack_) + smx_context_usable_stack_size);
241 #else
242      ASAN_EVAL(this->asan_stack_ = this->stack_);
243 #endif
244      this->stack_top_ = raw_makecontext(this->stack_, smx_context_usable_stack_size, RawContext::wrapper, this);
245    } else {
246      if (process != nullptr && maestro_context_ == nullptr)
247        maestro_context_ = this;
248      if (MC_is_active())
249        MC_ignore_heap(&maestro_context_->stack_top_, sizeof(maestro_context_->stack_top_));
250    }
251 }
252
253 RawContext::~RawContext()
254 {
255   SIMIX_context_stack_delete(this->stack_);
256 }
257
258 void RawContext::wrapper(void* arg)
259 {
260   RawContext* context = static_cast<RawContext*>(arg);
261   ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
262   try {
263     (*context)();
264     context->Context::stop();
265   } catch (StopRequest const&) {
266     XBT_DEBUG("Caught a StopRequest");
267   }
268   ASAN_EVAL(context->asan_stop_ = true);
269   context->suspend();
270 }
271
272 inline void RawContext::swap(RawContext* from, RawContext* to)
273 {
274   void* fake_stack = nullptr;
275   ASAN_EVAL(to->asan_ctx_ = from);
276   ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
277   raw_swapcontext(&from->stack_top_, to->stack_top_);
278   ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
279 }
280
281 void RawContext::stop()
282 {
283   Context::stop();
284   throw StopRequest();
285 }
286
287 // SerialRawContext
288
289 unsigned long SerialRawContext::process_index_; /* index of the next process to run in the list of runnable processes */
290
291 void SerialRawContext::suspend()
292 {
293   /* determine the next context */
294   SerialRawContext* next_context;
295   unsigned long int i = process_index_;
296   process_index_++;
297   if (i < simix_global->process_to_run.size()) {
298     /* execute the next process */
299     XBT_DEBUG("Run next process");
300     next_context = static_cast<SerialRawContext*>(simix_global->process_to_run[i]->context);
301   } else {
302     /* all processes were run, return to maestro */
303     XBT_DEBUG("No more process to run");
304     next_context = static_cast<SerialRawContext*>(RawContext::getMaestro());
305   }
306   SIMIX_context_set_current(next_context);
307   RawContext::swap(this, next_context);
308 }
309
310 void SerialRawContext::resume()
311 {
312   SIMIX_context_set_current(this);
313   RawContext::swap(RawContext::getMaestro(), this);
314 }
315
316 void SerialRawContext::run_all()
317 {
318   if (simix_global->process_to_run.empty())
319     return;
320   smx_actor_t first_process = simix_global->process_to_run.front();
321   process_index_            = 1;
322   static_cast<SerialRawContext*>(first_process->context)->resume();
323 }
324
325 // ParallelRawContext
326
327 #if HAVE_THREAD_CONTEXTS
328
329 simgrid::xbt::Parmap<smx_actor_t>* ParallelRawContext::parmap_;
330 std::atomic<uintptr_t> ParallelRawContext::threads_working_; /* number of threads that have started their work */
331 xbt_os_thread_key_t ParallelRawContext::worker_id_key_; /* thread-specific storage for the thread id */
332 std::vector<ParallelRawContext*> ParallelRawContext::workers_context_; /* space to save the worker context
333                                                                           in each thread */
334
335 void ParallelRawContext::initialize()
336 {
337   parmap_ = nullptr;
338   workers_context_.clear();
339   workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
340   xbt_os_thread_key_create(&worker_id_key_);
341 }
342
343 void ParallelRawContext::finalize()
344 {
345   delete parmap_;
346   parmap_ = nullptr;
347   workers_context_.clear();
348   xbt_os_thread_key_destroy(worker_id_key_);
349 }
350
351 void ParallelRawContext::run_all()
352 {
353   threads_working_ = 0;
354   if (parmap_ == nullptr)
355     parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
356   parmap_->apply(
357       [](smx_actor_t process) {
358         ParallelRawContext* context = static_cast<ParallelRawContext*>(process->context);
359         context->resume();
360       },
361       simix_global->process_to_run);
362 }
363
364 void ParallelRawContext::suspend()
365 {
366   /* determine the next context */
367   boost::optional<smx_actor_t> next_work = parmap_->next();
368   ParallelRawContext* next_context;
369   if (next_work) {
370     /* there is a next process to resume */
371     XBT_DEBUG("Run next process");
372     next_context = static_cast<ParallelRawContext*>(next_work.get()->context);
373   } else {
374     /* all processes were run, go to the barrier */
375     XBT_DEBUG("No more processes to run");
376     uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
377     next_context        = workers_context_[worker_id];
378     XBT_DEBUG("Restoring worker stack %zu (working threads = %zu)", worker_id, threads_working_.load());
379   }
380
381   SIMIX_context_set_current(next_context);
382   RawContext::swap(this, next_context);
383 }
384
385 void ParallelRawContext::resume()
386 {
387   uintptr_t worker_id = threads_working_.fetch_add(1, std::memory_order_relaxed);
388   xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
389   ParallelRawContext* worker_context = static_cast<ParallelRawContext*>(SIMIX_context_self());
390   workers_context_[worker_id]        = worker_context;
391   XBT_DEBUG("Saving worker stack %zu", worker_id);
392   SIMIX_context_set_current(this);
393   RawContext::swap(worker_context, this);
394 }
395
396 #endif
397
398 ContextFactory* raw_factory()
399 {
400   XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
401   return new RawContextFactory();
402 }
403 }}}