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(__aarch64__)
16
17 #include <stddef.h>
18
19 #include "osfiber_asm_aarch64.h"
20
21 #include "marl/export.h"
22
23 MARL_EXPORT
marl_fiber_trampoline(void (* target)(void *),void * arg)24 void marl_fiber_trampoline(void (*target)(void*), void* arg) {
25 target(arg);
26 }
27
28 // __attribute__((weak)) doesn't work on MacOS.
29 #if defined(linux) || defined(__linux) || defined(__linux__)
30 // This is needed for HWSAan runtimes that don't have this commit:
31 // https://reviews.llvm.org/D149228.
32 __attribute__((weak)) void __hwasan_tag_memory(const volatile void *p,
33 unsigned char tag, size_t size);
34 __attribute((weak)) void *__hwasan_tag_pointer(const volatile void *p,
35 unsigned char tag);
36 #endif
37
38 MARL_EXPORT
marl_fiber_set_target(struct marl_fiber_context * ctx,void * stack,uint32_t stack_size,void (* target)(void *),void * arg)39 void marl_fiber_set_target(struct marl_fiber_context* ctx,
40 void* stack,
41 uint32_t stack_size,
42 void (*target)(void*),
43 void* arg) {
44
45 #if defined(linux) || defined(__linux) || defined(__linux__)
46 if (__hwasan_tag_memory && __hwasan_tag_pointer) {
47 stack = __hwasan_tag_pointer(stack, 0);
48 __hwasan_tag_memory(stack, 0, stack_size);
49 }
50 #endif
51 uintptr_t* stack_top = (uintptr_t*)((uint8_t*)(stack) + stack_size);
52 ctx->LR = (uintptr_t)&marl_fiber_trampoline;
53 ctx->r0 = (uintptr_t)target;
54 ctx->r1 = (uintptr_t)arg;
55 ctx->SP = ((uintptr_t)stack_top) & ~(uintptr_t)15;
56 }
57
58 #endif // defined(__aarch64__)
59