• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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  *     http://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 
16 #ifndef CO2_INT_H
17 #define CO2_INT_H
18 
19 #include <stddef.h>
20 #include <stdint.h>
21 #include "c/type_def.h"
22 #include <assert.h>
23 #include "dfx/log/ffrt_log_api.h"
24 #ifdef TSAN_MODE
25     #include <sanitizer/tsan_interface.h>
26 #endif
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #if defined(__aarch64__)
33 #define FFRT_REG_NR 22
34 #define FFRT_REG_LR 11
35 #define FFRT_REG_SP 13
36 #elif defined(__arm__)
37 #define FFRT_REG_NR 64
38 #define FFRT_REG_LR 1
39 #define FFRT_REG_SP 0
40 #elif defined(__x86_64__)
41 #define FFRT_REG_NR 8
42 #define FFRT_REG_LR 7
43 #define FFRT_REG_SP 6
44 #elif defined(__riscv) && __riscv_xlen == 64
45 // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/riscv/bits/setjmp.h;h=5dd7fa0120ab37c9ec5c4a854792c0935b9eddc1;hb=HEAD
46 #if defined __riscv_float_abi_double
47 #define FFRT_REG_NR 26
48 #else
49 #define FFRT_REG_NR 14
50 #endif
51 #define FFRT_REG_LR 0
52 #define FFRT_REG_SP 13
53 
54 #else
55 #error "Unsupported architecture"
56 #endif
57 
58 int co2_save_context(ffrt_fiber_t* ctx);
59 
60 void co2_restore_context(ffrt_fiber_t* ctx);
61 
co2_switch_context(ffrt_fiber_t * from,ffrt_fiber_t * to)62 static inline void co2_switch_context(ffrt_fiber_t* from, ffrt_fiber_t* to)
63 {
64     if (co2_save_context(from) == 0) {
65         #ifdef TSAN_MODE
66             /* save current fiber to the source fiber */
67             if (from->tsanFiber == nullptr) {
68                 from->tsanFiber = __tsan_get_current_fiber();
69             }
70             assert(from->tsanFiber && "src fiber is nullptr");
71             if (to->tsanFiber == nullptr) {
72                 to->tsanFiber = __tsan_create_fiber(0);
73             }
74             assert(to->tsanFiber != from->tsanFiber);
75             FFRT_LOGD("[Before switching] TSAN from: %p to %p. TSan current fiber = %p\n",
76                 from->tsanFiber, to->tsanFiber, __tsan_get_current_fiber());
77             /* Switch to target fiber before the actual context switch
78              * Note: Investigate if passing `__tsan_switch_to_fiber_no_sync`
79              * and diabling happens before makes sense or not.
80              * when passing 0 as a flag to the fiber switching we establish
81              * a happens before relationship.
82              */
83             __tsan_switch_to_fiber(to->tsanFiber, 0);
84         #endif
85         co2_restore_context(to);
86         #ifdef TSAN_MODE
87         assert(to->tsanFiber == __tsan_get_current_fiber());
88         #endif
89     }
90 }
91 #ifdef  __cplusplus
92 }
93 #endif
94 #endif /* CO2_INT_H */
95