xref: /aosp_15_r20/external/swiftshader/third_party/marl/src/osfiber_x64.c (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2019 The Marl Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #if defined(__x86_64__)
16 
17 #include "osfiber_asm_x64.h"
18 
19 #include "marl/export.h"
20 #include "marl/sanitizers.h"
21 
22 // You can find an explanation of this code here:
23 // https://github.com/google/marl/issues/199
24 
MARL_UNDEFINED_SANITIZER_ONLY()25 MARL_UNDEFINED_SANITIZER_ONLY(__attribute__((no_sanitize("function"))))
26 MARL_EXPORT
27 void marl_fiber_trampoline(void (*target)(void*), void* arg) {
28   target(arg);
29 }
30 
31 MARL_EXPORT
marl_fiber_set_target(struct marl_fiber_context * ctx,void * stack,uint32_t stack_size,void (* target)(void *),void * arg)32 void marl_fiber_set_target(struct marl_fiber_context* ctx,
33                            void* stack,
34                            uint32_t stack_size,
35                            void (*target)(void*),
36                            void* arg) {
37   uintptr_t* stack_top = (uintptr_t*)((uint8_t*)(stack) + stack_size);
38   ctx->RIP = (uintptr_t)&marl_fiber_trampoline;
39   ctx->RDI = (uintptr_t)target;
40   ctx->RSI = (uintptr_t)arg;
41   ctx->RSP = (uintptr_t)&stack_top[-3];
42   stack_top[-2] = 0;  // No return target.
43 }
44 
45 #endif  // defined(__x86_64__)
46