1 /* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved. */
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. */
6 #include "ContextRaw.hpp"
7 #include "context_private.hpp"
9 #include "simgrid/Exception.hpp"
10 #include "src/simix/smx_private.hpp"
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
14 // Raw context routines
16 typedef void (*rawctx_entry_point_t)(void *);
18 typedef void* raw_stack_t;
19 extern "C" raw_stack_t raw_makecontext(void* malloced_stack, int stack_size,
20 rawctx_entry_point_t entry_point, void* arg);
21 extern "C" void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context);
23 // TODO, we should handle FP, MMX and the x87 control-word (for x86 and x86_64)
25 #if SIMGRID_PROCESSOR_x86_64
27 #if defined(__APPLE__)
29 ".globl _raw_makecontext\n"
33 ".globl raw_makecontext\n"
37 ".globl raw_makecontext\n"
38 ".type raw_makecontext,@function\n"
39 "raw_makecontext:\n"/* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
41 " mov %rdi,%rax\n" /* stack */
42 " add %rsi,%rax\n" /* size */
43 " andq $-16, %rax\n" /* align stack */
44 " movq $0, -8(%rax)\n" /* @return for func */
45 " mov %rdx,-16(%rax)\n" /* func */
46 " mov %rcx,-24(%rax)\n" /* arg/rdi */
47 " movq $0, -32(%rax)\n" /* rsi */
48 " movq $0, -40(%rax)\n" /* rdx */
49 " movq $0, -48(%rax)\n" /* rcx */
50 " movq $0, -56(%rax)\n" /* r8 */
51 " movq $0, -64(%rax)\n" /* r9 */
52 " movq $0, -72(%rax)\n" /* rbp */
53 " movq $0, -80(%rax)\n" /* rbx */
54 " movq $0, -88(%rax)\n" /* r12 */
55 " movq $0, -96(%rax)\n" /* r13 */
56 " movq $0, -104(%rax)\n" /* r14 */
57 " movq $0, -112(%rax)\n" /* r15 */
63 #if defined(__APPLE__)
65 ".globl _raw_swapcontext\n"
69 ".globl raw_swapcontext\n"
73 ".globl raw_swapcontext\n"
74 ".type raw_swapcontext,@function\n"
75 "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
89 " mov %rsp,(%rdi)\n" /* old */
90 " mov %rsi,%rsp\n" /* new */
105 #elif SIMGRID_PROCESSOR_i686
107 #if defined(__APPLE__) || defined(_WIN32)
109 ".globl _raw_makecontext\n"
110 "_raw_makecontext:\n"
113 ".globl raw_makecontext\n"
114 ".type raw_makecontext,@function\n"
117 " movl 4(%esp),%eax\n" /* stack */
118 " addl 8(%esp),%eax\n" /* size */
119 " andl $-16, %eax\n" /* align stack */
120 " movl 12(%esp),%ecx\n" /* func */
121 " movl 16(%esp),%edx\n" /* arg */
122 " movl %edx, -4(%eax)\n"
123 " movl $0, -8(%eax)\n" /* @return for func */
124 " movl %ecx,-12(%eax)\n"
125 " movl $0, -16(%eax)\n" /* ebp */
126 " movl $0, -20(%eax)\n" /* ebx */
127 " movl $0, -24(%eax)\n" /* esi */
128 " movl $0, -28(%eax)\n" /* edi */
134 #if defined(__APPLE__) || defined(_WIN32)
136 ".globl _raw_swapcontext\n"
137 "_raw_swapcontext:\n"
140 ".globl raw_swapcontext\n"
141 ".type raw_swapcontext,@function\n"
144 // Fetch the parameters:
145 " movl 4(%esp),%eax\n" /* old (raw_stack_t*) */
146 " movl 8(%esp),%edx\n" /* new (raw_stack_t) */
147 // Save registers of the current context on the stack:
152 // Save the current context (stack pointer) in *old:
153 " movl %esp,(%eax)\n"
154 // Switch to the stack of the new context:
156 // Pop the values of the new context:
161 // Return using the return address of the new context:
167 /* If you implement raw contexts for other processors, don't forget to
168 update the definition of HAVE_RAW_CONTEXTS in tools/cmake/CompleteInFiles.cmake */
170 raw_stack_t raw_makecontext(void* malloced_stack, int stack_size,
171 rawctx_entry_point_t entry_point, void* arg) {
175 void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context) {
181 // ***** Method definitions
189 RawContextFactory::RawContextFactory() : SwappedContextFactory("RawContextFactory") {}
191 RawContextFactory::~RawContextFactory() = default;
193 Context* RawContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
197 return this->new_context<ParallelRawContext>(std::move(code), cleanup_func, process, this);
198 return this->new_context<RawContext>(std::move(code), cleanup_func, process, this);
203 RawContext::RawContext(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process,
204 SwappedContextFactory* factory)
205 : SwappedContext(std::move(code), cleanup, process, factory)
208 #if PTH_STACKGROWTH == -1
209 ASAN_ONLY(this->asan_stack_ = static_cast<char*>(this->stack_) + smx_context_usable_stack_size);
211 ASAN_ONLY(this->asan_stack_ = this->stack_);
213 this->stack_top_ = raw_makecontext(this->stack_, smx_context_usable_stack_size, RawContext::wrapper, this);
215 if (process != nullptr && get_maestro() == nullptr)
217 if (MC_is_active()) {
218 XBT_ATTRIB_UNUSED RawContext* maestro = static_cast<RawContext*>(get_maestro());
219 MC_ignore_heap(&maestro->stack_top_, sizeof(maestro->stack_top_));
224 RawContext::~RawContext() = default;
226 void RawContext::wrapper(void* arg)
228 RawContext* context = static_cast<RawContext*>(arg);
229 ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
232 } catch (StopRequest const&) {
233 XBT_DEBUG("Caught a StopRequest");
234 } catch (simgrid::Exception const& e) {
235 XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
238 context->Context::stop();
240 ASAN_ONLY(context->asan_stop_ = true);
244 void RawContext::swap_into(SwappedContext* to_)
246 RawContext* to = static_cast<RawContext*>(to_);
247 ASAN_ONLY(void* fake_stack = nullptr);
248 ASAN_ONLY(to->asan_ctx_ = from);
249 ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
250 raw_swapcontext(&this->stack_top_, to->stack_top_);
251 ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
254 void RawContext::stop()
260 // ParallelRawContext
262 void ParallelRawContext::run_all()
264 threads_working_ = 0;
265 if (parmap_ == nullptr)
266 parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
268 [](smx_actor_t process) {
269 ParallelRawContext* context = static_cast<ParallelRawContext*>(process->context_);
272 simix_global->process_to_run);
275 void ParallelRawContext::suspend()
277 /* determine the next context */
278 boost::optional<smx_actor_t> next_work = parmap_->next();
279 SwappedContext* next_context;
281 /* there is a next process to resume */
282 XBT_DEBUG("Run next process");
283 next_context = static_cast<ParallelRawContext*>(next_work.get()->context_);
285 /* all processes were run, go to the barrier */
286 XBT_DEBUG("No more processes to run");
287 next_context = workers_context_[worker_id_];
288 XBT_DEBUG("Restoring worker stack %zu (working threads = %zu)", worker_id_, threads_working_.load());
291 Context::set_current(next_context);
292 this->swap_into(next_context);
295 void ParallelRawContext::resume()
297 worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
298 SwappedContext* worker_context = static_cast<SwappedContext*>(self());
299 workers_context_[worker_id_] = worker_context;
300 XBT_DEBUG("Saving worker stack %zu", worker_id_);
301 Context::set_current(this);
302 worker_context->swap_into(this);
305 ContextFactory* raw_factory()
307 XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
308 return new RawContextFactory();