Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / kernel / context / ContextRaw.cpp
1 /* Copyright (c) 2009-2021. 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 #include "src/simix/smx_private.hpp"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
12
13 // Raw context routines
14
15 using rawctx_entry_point_t = void (*)(simgrid::kernel::context::SwappedContext*);
16
17 using raw_stack_t = void*;
18 extern "C" raw_stack_t raw_makecontext(void* malloced_stack, int stack_size, rawctx_entry_point_t entry_point,
19                                        simgrid::kernel::context::SwappedContext* 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, rawctx_entry_point_t entry_point,
170                             simgrid::kernel::context::SwappedContext* arg)
171 {
172   THROW_UNIMPLEMENTED;
173 }
174
175 void raw_swapcontext(raw_stack_t* old, raw_stack_t new_context)
176 {
177   THROW_UNIMPLEMENTED;
178 }
179
180 #endif
181
182 // ***** Method definitions
183
184 namespace simgrid {
185 namespace kernel {
186 namespace context {
187
188 // RawContextFactory
189
190 RawContext* RawContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor)
191 {
192   return this->new_context<RawContext>(std::move(code), actor, this);
193 }
194
195 // RawContext
196
197 RawContext::RawContext(std::function<void()>&& code, actor::ActorImpl* actor, SwappedContextFactory* factory)
198     : SwappedContext(std::move(code), actor, factory)
199 {
200   XBT_VERB("Creating a context of stack %uMb", actor->get_stacksize() / 1024 / 1024);
201   if (has_code()) {
202     this->stack_top_ = raw_makecontext(get_stack(), actor->get_stacksize(), smx_ctx_wrapper, this);
203   } else {
204     if (MC_is_active())
205       MC_ignore_heap(&stack_top_, sizeof(stack_top_));
206   }
207 }
208
209 void RawContext::swap_into_for_real(SwappedContext* to_)
210 {
211   const RawContext* to = static_cast<RawContext*>(to_);
212   raw_swapcontext(&this->stack_top_, to->stack_top_);
213 }
214
215 ContextFactory* raw_factory()
216 {
217   XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
218   return new RawContextFactory();
219 }
220 } // namespace context
221 } // namespace kernel
222 } // namespace simgrid