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 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #if defined(__aarch64__)
27 #define FFRT_REG_NR 22
28 #define FFRT_REG_LR 11
29 #define FFRT_REG_SP 13
30 #elif defined(__arm__)
31 #define FFRT_REG_NR 64
32 #define FFRT_REG_LR 1
33 #define FFRT_REG_SP 0
34 #elif defined(__x86_64__)
35 #define FFRT_REG_NR 8
36 #define FFRT_REG_LR 7
37 #define FFRT_REG_SP 6
38 #elif defined(__riscv) && __riscv_xlen == 64
39 // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/riscv/bits/setjmp.h;h=5dd7fa0120ab37c9ec5c4a854792c0935b9eddc1;hb=HEAD
40 #if defined __riscv_float_abi_double
41 #define FFRT_REG_NR 26
42 #else
43 #define FFRT_REG_NR 14
44 #endif
45 #define FFRT_REG_LR 0
46 #define FFRT_REG_SP 13
47 
48 #else
49 #error "Unsupported architecture"
50 #endif
51 
52 struct co2_context {
53     uintptr_t regs[FFRT_REG_NR];
54 };
55 
56 int co2_save_context(struct co2_context* ctx);
57 
58 void co2_restore_context(struct co2_context* ctx);
59 
co2_switch_context(struct co2_context* from, struct co2_context* to)60 static inline void co2_switch_context(struct co2_context* from, struct co2_context* to)
61 {
62     if (co2_save_context(from) == 0) {
63         co2_restore_context(to);
64     }
65 }
66 
67 int co2_init_context(struct co2_context* ctx, void (*func)(void*), void* arg, void* stack, size_t stack_size);
68 
69 #ifdef __cplusplus
70 }
71 #endif
72 #endif /* CO2_INT_H */
73