xref: /aosp_15_r20/external/swiftshader/third_party/marl/src/osfiber_ppc64.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(__powerpc64__)
16 
17 #include "osfiber_asm_ppc64.h"
18 
19 #include "marl/export.h"
20 
21 MARL_EXPORT
marl_fiber_trampoline(void (* target)(void *),void * arg)22 void marl_fiber_trampoline(void (*target)(void*), void* arg) {
23   target(arg);
24 }
25 
26 MARL_EXPORT
marl_fiber_set_target(struct marl_fiber_context * ctx,void * stack,uint32_t stack_size,void (* target)(void *),void * arg)27 void marl_fiber_set_target(struct marl_fiber_context* ctx,
28                            void* stack,
29                            uint32_t stack_size,
30                            void (*target)(void*),
31                            void* arg) {
32   uintptr_t stack_top = (uintptr_t)((uint8_t*)(stack) + stack_size - sizeof(uintptr_t));
33   if ((stack_top % 16) != 0) {
34     stack_top -= (stack_top % 16);
35   }
36 
37   // Write a backchain and subtract a minimum stack frame size (32/48)
38   *(uintptr_t*)stack_top = 0;
39 #if !defined(_CALL_ELF) || (_CALL_ELF != 2)
40   stack_top -= 48;
41   *(uintptr_t*)stack_top = stack_top + 48;
42 #else
43   stack_top -= 32;
44   *(uintptr_t*)stack_top = stack_top + 32;
45 #endif
46 
47   // Load registers
48   ctx->r1 = stack_top;
49 #if !defined(_CALL_ELF) || (_CALL_ELF != 2)
50   ctx->lr = ((const uintptr_t *)marl_fiber_trampoline)[0];
51   ctx->r2 = ((const uintptr_t *)marl_fiber_trampoline)[1];
52 #else
53   ctx->lr = (uintptr_t)marl_fiber_trampoline;
54 #endif
55   ctx->r3 = (uintptr_t)target;
56   ctx->r4 = (uintptr_t)arg;
57 
58   // Thread pointer must be saved in r13
59   __asm__ volatile("mr %0, 13\n" : "=r"(ctx->r13));
60 }
61 
62 #endif  // __powerpc64__
63