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 #else
39 #error "Unsupported architecture"
40 #endif
41
42 struct co2_context {
43 uintptr_t regs[REG_NR];
44 };
45
46 int co2_save_context(struct co2_context* ctx);
47
48 void co2_restore_context(struct co2_context* ctx);
49
co2_switch_context(struct co2_context * from,struct co2_context * to)50 static inline void co2_switch_context(struct co2_context* from, struct co2_context* to)
51 {
52 if (co2_save_context(from) == 0) {
53 co2_restore_context(to);
54 }
55 }
56
57 int co2_init_context(struct co2_context* ctx, void (*func)(void*), void* arg, void* stack, size_t stack_size);
58
59 #ifdef __cplusplus
60 }
61 #endif
62 #endif /* CO2_INT_H */
63