Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
de85d2ff1aff1c8be309b815c2750fed9479b5a0
[simgrid.git] / src / kernel / context / ContextUnix.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 /* \file UContext.cpp Context switching with ucontexts from System V        */
7
8 #include "context_private.hpp"
9
10 #include "mc/mc.h"
11 #include "simgrid/Exception.hpp"
12 #include "src/mc/mc_ignore.hpp"
13 #include "src/simix/ActorImpl.hpp"
14 #include "src/simix/smx_private.hpp"
15
16 #include "ContextUnix.hpp"
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
19
20 /** Many integers are needed to store a pointer
21  *
22  * Support up to two ints. */
23 constexpr int CTX_ADDR_LEN = 2;
24
25 static_assert(sizeof(simgrid::kernel::context::UContext*) <= CTX_ADDR_LEN * sizeof(int),
26               "Ucontexts are not supported on this arch yet");
27
28 namespace simgrid {
29 namespace kernel {
30 namespace context {
31
32 // UContextFactory
33 Context* UContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process)
34 {
35   return new_context<UContext>(std::move(code), cleanup, process, this);
36 }
37
38
39 // UContext
40
41 UContext::UContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
42                    SwappedContextFactory* factory)
43     : SwappedContext(std::move(code), cleanup_func, process, factory)
44 {
45   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
46   if (has_code()) {
47     getcontext(&this->uc_);
48     this->uc_.uc_link = nullptr;
49     this->uc_.uc_stack.ss_sp   = sg_makecontext_stack_addr(this->stack_);
50     this->uc_.uc_stack.ss_size = sg_makecontext_stack_size(smx_context_usable_stack_size);
51 #if PTH_STACKGROWTH == -1
52     ASAN_ONLY(this->asan_stack_ = static_cast<char*>(this->stack_) + smx_context_usable_stack_size);
53 #else
54     ASAN_ONLY(this->asan_stack_ = this->stack_);
55 #endif
56     UContext::make_ctx(&this->uc_, UContext::smx_ctx_sysv_wrapper, this);
57   } else {
58     if (process != nullptr && get_maestro() == nullptr)
59       set_maestro(this);
60   }
61
62 #if SIMGRID_HAVE_MC
63   if (MC_is_active() && has_code()) {
64     MC_register_stack_area(this->stack_, process, &(this->uc_), smx_context_usable_stack_size);
65   }
66 #endif
67 }
68
69 // The name of this function is currently hardcoded in the code (as string).
70 // Do not change it without fixing those references as well.
71 void UContext::smx_ctx_sysv_wrapper(int i1, int i2)
72 {
73   // Rebuild the Context* pointer from the integers:
74   int ctx_addr[CTX_ADDR_LEN] = {i1, i2};
75   simgrid::kernel::context::UContext* context;
76   memcpy(&context, ctx_addr, sizeof context);
77
78   ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
79   try {
80     (*context)();
81   } catch (simgrid::kernel::context::Context::StopRequest const&) {
82     XBT_DEBUG("Caught a StopRequest");
83   } catch (simgrid::Exception const& e) {
84     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
85     throw;
86   }
87   context->Context::stop();
88   ASAN_ONLY(context->asan_stop_ = true);
89   context->suspend();
90 }
91
92 /** A better makecontext
93  *
94  * Makecontext expects integer arguments, we the context variable is decomposed into a serie of integers and each
95  * integer is passed as argument to makecontext.
96  */
97 void UContext::make_ctx(ucontext_t* ucp, void (*func)(int, int), UContext* arg)
98 {
99   int ctx_addr[CTX_ADDR_LEN]{};
100   memcpy(ctx_addr, &arg, sizeof arg);
101   makecontext(ucp, (void (*)())func, 2, ctx_addr[0], ctx_addr[1]);
102 }
103
104 void UContext::swap_into(SwappedContext* to_)
105 {
106   UContext* to = static_cast<UContext*>(to_);
107   ASAN_ONLY(void* fake_stack = nullptr);
108   ASAN_ONLY(to->asan_ctx_ = from);
109   ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
110   swapcontext(&this->uc_, &to->uc_);
111   ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
112 }
113
114
115 XBT_PRIVATE ContextFactory* sysv_factory()
116 {
117   XBT_VERB("Activating SYSV context factory");
118   return new UContextFactory();
119 }
120 }}} // namespace simgrid::kernel::context