Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[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 "mc/mc.h"
8 #include "simgrid/Exception.hpp"
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 #elif defined(_WIN32)
32    ".text\n"
33    ".globl raw_makecontext\n"
34    "raw_makecontext:\n"
35 #else
36    ".text\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 */
40 #endif
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 */
58    "   sub $112,%rax\n"
59    "   ret\n"
60 );
61
62 __asm__ (
63 #if defined(__APPLE__)
64    ".text\n"
65    ".globl _raw_swapcontext\n"
66    "_raw_swapcontext:\n"
67 #elif defined(_WIN32)
68    ".text\n"
69    ".globl raw_swapcontext\n"
70    "raw_swapcontext:\n"
71 #else
72    ".text\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 */
76 #endif
77    "   push %rdi\n"
78    "   push %rsi\n"
79    "   push %rdx\n"
80    "   push %rcx\n"
81    "   push %r8\n"
82    "   push %r9\n"
83    "   push %rbp\n"
84    "   push %rbx\n"
85    "   push %r12\n"
86    "   push %r13\n"
87    "   push %r14\n"
88    "   push %r15\n"
89    "   mov %rsp,(%rdi)\n" /* old */
90    "   mov %rsi,%rsp\n" /* new */
91    "   pop %r15\n"
92    "   pop %r14\n"
93    "   pop %r13\n"
94    "   pop %r12\n"
95    "   pop %rbx\n"
96    "   pop %rbp\n"
97    "   pop %r9\n"
98    "   pop %r8\n"
99    "   pop %rcx\n"
100    "   pop %rdx\n"
101    "   pop %rsi\n"
102    "   pop %rdi\n"
103    "   ret\n"
104 );
105 #elif SIMGRID_PROCESSOR_i686
106 __asm__ (
107 #if defined(__APPLE__) || defined(_WIN32)
108    ".text\n"
109    ".globl _raw_makecontext\n"
110    "_raw_makecontext:\n"
111 #else
112    ".text\n"
113    ".globl raw_makecontext\n"
114    ".type raw_makecontext,@function\n"
115    "raw_makecontext:\n"
116 #endif
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 */
129    "   subl $28,%eax\n"
130    "   retl\n"
131 );
132
133 __asm__ (
134 #if defined(__APPLE__) || defined(_WIN32)
135    ".text\n"
136    ".globl _raw_swapcontext\n"
137    "_raw_swapcontext:\n"
138 #else
139    ".text\n"
140    ".globl raw_swapcontext\n"
141    ".type raw_swapcontext,@function\n"
142    "raw_swapcontext:\n"
143 #endif
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:
148    "   pushl %ebp\n"
149    "   pushl %ebx\n"
150    "   pushl %esi\n"
151    "   pushl %edi\n"
152    // Save the current context (stack pointer) in *old:
153    "   movl %esp,(%eax)\n"
154    // Switch to the stack of the new context:
155    "   movl %edx,%esp\n"
156    // Pop the values of the new context:
157    "   popl %edi\n"
158    "   popl %esi\n"
159    "   popl %ebx\n"
160    "   popl %ebp\n"
161    // Return using the return address of the new context:
162    "   retl\n"
163 );
164 #else
165 #error HAVE_RAW_CONTEXTS defined, but neither SIMGRID_PROCESSOR_x86_64 nor SIMGRID_PROCESSOR_i686. Please update the code.
166 #endif
167
168 #else /* not HAVE_RAW_CONTEXTS */
169
170 /* If you implement raw contexts for other processors, don't forget to
171    update the definition of HAVE_RAW_CONTEXTS in tools/cmake/CompleteInFiles.cmake */
172
173 raw_stack_t raw_makecontext(void* malloced_stack, int stack_size, rawctx_entry_point_t entry_point,
174                             simgrid::kernel::context::SwappedContext* arg)
175 {
176   THROW_UNIMPLEMENTED;
177 }
178
179 void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context)
180 {
181   THROW_UNIMPLEMENTED;
182 }
183
184 #endif
185
186 // ***** Method definitions
187
188 namespace simgrid::kernel::context {
189
190 // RawContextFactory
191
192 RawContext* RawContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor)
193 {
194   return this->new_context<RawContext>(std::move(code), actor, this);
195 }
196
197 // RawContext
198
199 RawContext::RawContext(std::function<void()>&& code, actor::ActorImpl* actor, SwappedContextFactory* factory)
200     : SwappedContext(std::move(code), actor, factory)
201 {
202   XBT_VERB("Creating a context of stack %uMb", actor->get_stacksize() / 1024 / 1024);
203   if (has_code()) {
204     this->stack_top_ = raw_makecontext(get_stack(), actor->get_stacksize(), smx_ctx_wrapper, this);
205   } else {
206     MC_ignore_heap(&stack_top_, sizeof stack_top_);
207   }
208 }
209
210 void RawContext::swap_into_for_real(SwappedContext* to_)
211 {
212   const RawContext* to = static_cast<RawContext*>(to_);
213   raw_swapcontext(&this->stack_top_, to->stack_top_);
214 }
215
216 ContextFactory* raw_factory()
217 {
218   XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
219   return new RawContextFactory();
220 }
221 } // namespace simgrid::kernel::context