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 REG_NR 22
28 #define REG_LR 11
29 #define REG_SP 13
30 #elif defined(__arm__)
31 #define REG_NR 64
32 #define REG_LR 1
33 #define REG_SP 0
34 #elif defined(__x86_64__)
35 #define REG_NR 8
36 #define REG_LR 7
37 #define REG_SP 6
38 #elif defined(_MSC_VER)
39 #define REG_NR 10
40 #define REG_LR 7
41 #define REG_SP 6
42 #else
43 #error "Unsupported architecture"
44 #endif
45
46 struct co2_context {
47 uintptr_t regs[REG_NR];
48 };
49
50 int co2_save_context(struct co2_context* ctx);
51
52 void co2_restore_context(struct co2_context* ctx);
53
co2_switch_context(struct co2_context * from,struct co2_context * to)54 static inline void co2_switch_context(struct co2_context* from, struct co2_context* to)
55 {
56 if (co2_save_context(from) == 0) {
57 co2_restore_context(to);
58 }
59 }
60
61 int co2_init_context(struct co2_context* ctx, void (*func)(void*), void* arg, void* stack, size_t stack_size);
62
63 #ifdef __cplusplus
64 }
65 #endif
66 #endif /* CO2_INT_H */
67