Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove the stateful model-checking from the archive. It's not working anymore
[simgrid.git] / src / kernel / context / ContextRaw.cpp
1 /* Copyright (c) 2009-2023. 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 "simgrid/Exception.hpp"
8 #include "src/mc/mc.h"
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_context);
11
12 // Raw context routines
13
14 using rawctx_entry_point_t = void (*)(simgrid::kernel::context::SwappedContext*);
15
16 using raw_stack_t = void*;
17 extern "C" raw_stack_t raw_makecontext(void* malloced_stack, int stack_size, rawctx_entry_point_t entry_point,
18                                        simgrid::kernel::context::SwappedContext* arg);
19 extern "C" void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context);
20
21 // TODO, we should handle FP, MMX and the x87 control-word (for x86 and x86_64)
22
23 #if HAVE_RAW_CONTEXTS
24
25 #if SIMGRID_PROCESSOR_x86_64
26 __asm__(
27 #if defined(__APPLE__)
28     ".text\n"
29     ".globl _raw_makecontext\n"
30     "_raw_makecontext:\n"
31 #else
32     ".text\n"
33     ".globl raw_makecontext\n"
34     ".type raw_makecontext,@function\n"
35     "raw_makecontext:\n" /* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
36 #endif
37     "   mov %rdi,%rax\n"       /* stack */
38     "   add %rsi,%rax\n"       /* size  */
39     "   andq $-16, %rax\n"     /* align stack */
40     "   movq $0,   -8(%rax)\n" /* @return for func */
41     "   mov %rdx,-16(%rax)\n"  /* func */
42     "   mov %rcx,-24(%rax)\n"  /* arg/rdi */
43     "   movq $0,  -32(%rax)\n" /* rsi */
44     "   movq $0,  -40(%rax)\n" /* rdx */
45     "   movq $0,  -48(%rax)\n" /* rcx */
46     "   movq $0,  -56(%rax)\n" /* r8  */
47     "   movq $0,  -64(%rax)\n" /* r9  */
48     "   movq $0,  -72(%rax)\n" /* rbp */
49     "   movq $0,  -80(%rax)\n" /* rbx */
50     "   movq $0,  -88(%rax)\n" /* r12 */
51     "   movq $0,  -96(%rax)\n" /* r13 */
52     "   movq $0, -104(%rax)\n" /* r14 */
53     "   movq $0, -112(%rax)\n" /* r15 */
54     "   sub $112,%rax\n"
55     "   ret\n");
56
57 __asm__(
58 #if defined(__APPLE__)
59     ".text\n"
60     ".globl _raw_swapcontext\n"
61     "_raw_swapcontext:\n"
62 #else
63     ".text\n"
64     ".globl raw_swapcontext\n"
65     ".type raw_swapcontext,@function\n"
66     "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
67 #endif
68     "   push %rdi\n"
69     "   push %rsi\n"
70     "   push %rdx\n"
71     "   push %rcx\n"
72     "   push %r8\n"
73     "   push %r9\n"
74     "   push %rbp\n"
75     "   push %rbx\n"
76     "   push %r12\n"
77     "   push %r13\n"
78     "   push %r14\n"
79     "   push %r15\n"
80     "   mov %rsp,(%rdi)\n" /* old */
81     "   mov %rsi,%rsp\n"   /* new */
82     "   pop %r15\n"
83     "   pop %r14\n"
84     "   pop %r13\n"
85     "   pop %r12\n"
86     "   pop %rbx\n"
87     "   pop %rbp\n"
88     "   pop %r9\n"
89     "   pop %r8\n"
90     "   pop %rcx\n"
91     "   pop %rdx\n"
92     "   pop %rsi\n"
93     "   pop %rdi\n"
94     "   ret\n");
95 #elif SIMGRID_PROCESSOR_i686
96 __asm__(
97 #if defined(__APPLE__)
98     ".text\n"
99     ".globl _raw_makecontext\n"
100     "_raw_makecontext:\n"
101 #else
102     ".text\n"
103     ".globl raw_makecontext\n"
104     ".type raw_makecontext,@function\n"
105     "raw_makecontext:\n"
106 #endif
107     "   movl 4(%esp),%eax\n"  /* stack */
108     "   addl 8(%esp),%eax\n"  /* size  */
109     "   andl $-16, %eax\n"    /* align stack */
110     "   movl 12(%esp),%ecx\n" /* func  */
111     "   movl 16(%esp),%edx\n" /* arg   */
112     "   movl %edx, -4(%eax)\n"
113     "   movl $0,   -8(%eax)\n" /* @return for func */
114     "   movl %ecx,-12(%eax)\n"
115     "   movl $0,  -16(%eax)\n" /* ebp */
116     "   movl $0,  -20(%eax)\n" /* ebx */
117     "   movl $0,  -24(%eax)\n" /* esi */
118     "   movl $0,  -28(%eax)\n" /* edi */
119     "   subl $28,%eax\n"
120     "   retl\n");
121
122 __asm__(
123 #if defined(__APPLE__)
124     ".text\n"
125     ".globl _raw_swapcontext\n"
126     "_raw_swapcontext:\n"
127 #else
128     ".text\n"
129     ".globl raw_swapcontext\n"
130     ".type raw_swapcontext,@function\n"
131     "raw_swapcontext:\n"
132 #endif
133     // Fetch the parameters:
134     "   movl 4(%esp),%eax\n" /* old (raw_stack_t*) */
135     "   movl 8(%esp),%edx\n" /* new (raw_stack_t)  */
136     // Save registers of the current context on the stack:
137     "   pushl %ebp\n"
138     "   pushl %ebx\n"
139     "   pushl %esi\n"
140     "   pushl %edi\n"
141     // Save the current context (stack pointer) in *old:
142     "   movl %esp,(%eax)\n"
143     // Switch to the stack of the new context:
144     "   movl %edx,%esp\n"
145     // Pop the values of the new context:
146     "   popl %edi\n"
147     "   popl %esi\n"
148     "   popl %ebx\n"
149     "   popl %ebp\n"
150     // Return using the return address of the new context:
151     "   retl\n");
152 #else
153 #error HAVE_RAW_CONTEXTS defined, but neither SIMGRID_PROCESSOR_x86_64 nor SIMGRID_PROCESSOR_i686. Please update the code.
154 #endif
155
156 #else /* not HAVE_RAW_CONTEXTS */
157
158 /* If you implement raw contexts for other processors, don't forget to
159    update the definition of HAVE_RAW_CONTEXTS in tools/cmake/CompleteInFiles.cmake */
160
161 raw_stack_t raw_makecontext(void* malloced_stack, int stack_size, rawctx_entry_point_t entry_point,
162                             simgrid::kernel::context::SwappedContext* arg)
163 {
164   THROW_UNIMPLEMENTED;
165 }
166
167 void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context)
168 {
169   THROW_UNIMPLEMENTED;
170 }
171
172 #endif
173
174 // ***** Method definitions
175
176 namespace simgrid::kernel::context {
177
178 // RawContextFactory
179
180 RawContext* RawContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor)
181 {
182   return this->new_context<RawContext>(std::move(code), actor, this);
183 }
184
185 // RawContext
186
187 RawContext::RawContext(std::function<void()>&& code, actor::ActorImpl* actor, SwappedContextFactory* factory)
188     : SwappedContext(std::move(code), actor, factory)
189 {
190   XBT_VERB("Creating a context of stack %uMb", actor->get_stacksize() / 1024 / 1024);
191   if (has_code()) {
192     this->stack_top_ = raw_makecontext(get_stack(), actor->get_stacksize(), smx_ctx_wrapper, this);
193   }
194 }
195
196 void RawContext::swap_into_for_real(SwappedContext* to_)
197 {
198   const RawContext* to = static_cast<RawContext*>(to_);
199   raw_swapcontext(&this->stack_top_, to->stack_top_);
200 }
201
202 ContextFactory* raw_factory()
203 {
204   XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
205   return new RawContextFactory();
206 }
207 } // namespace simgrid::kernel::context