• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-2024 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 PANDA_RUNTIME_FIBERS_ARCH_AMD64_CONTEXT_LAYOUT_H
17 #define PANDA_RUNTIME_FIBERS_ARCH_AMD64_CONTEXT_LAYOUT_H
18 
19 // NOLINTBEGIN(cppcoreguidelines-macro-usage)
20 
21 /**
22  * Memory layout of the saved context:
23  *
24  * GPRs: 9 x 8 = 72 bytes (RBX, RBP, 12, 13, 14, 15, RDI, RIP, RSP)
25  * Padding: 8 bytes
26  * FPENV: 28 bytes
27  * Padding: 4 bytes
28  * MXCSR: 4 bytes
29  * Padding: 12 bytes
30  * == TOTAL: 128 bytes ==
31  *
32  * OFFSET HEX | OFFSET DEC | SIZE | CONTENTS
33  * -----------------------------------------
34  * 0x0        | 0          | 8    | RBX
35  * 0x8        | 8          | 8    | RBP
36  * 0x10       | 16         | 8    | R12
37  * 0x18       | 24         | 8    | R13
38  * 0x20       | 32         | 8    | R14
39  * 0x28       | 40         | 8    | R15
40  * 0x30       | 48         | 8    | RDI
41  * 0x38       | 56         | 8    | RIP
42  * 0x40       | 64         | 8    | RSP
43  * 0x48       | 72         | 8    | (padding)
44  * 0x50       | 80         | 28   | FPENV
45  * 0x6c       | 108        | 4    | (padding)
46  * 0x70       | 112        | 4    | MXCSR*
47  * 0x74       | 116        | 12   | (padding)
48  *
49  * according to the SYSV ABI:
50  * (saving)
51  * ARGS: rdi (holds data pointer for the fiber function)
52  * CALLEE-SAVED: rsp, rbp, rbx, r12, r13, r14, r15
53  * SPECIAL: rip
54  *
55  * (skipping, because we emulate function call by the context switch)
56  * CALLER SAVED (temps): R10, R11
57  * ARGS: rsi, rdx, rcx, r8, r9
58  * ZERO: RAX
59  */
60 
61 #define FCTX_LEN_BYTES 128
62 
63 // gpr
64 #define FCTX_GPR_OFFSET_BYTES 0
65 #define FCTX_GPR_SIZE_BYTES 8
66 #define FCTX_GPR_OFFSET_BYTES_BY_INDEX(i) (FCTX_GPR_OFFSET_BYTES + FCTX_GPR_SIZE_BYTES * (i))
67 #define FCTX_GPR_OFFSET_BYTES_RBX FCTX_GPR_OFFSET_BYTES_BY_INDEX(0)
68 #define FCTX_GPR_OFFSET_BYTES_RBP FCTX_GPR_OFFSET_BYTES_BY_INDEX(1)
69 #define FCTX_GPR_OFFSET_BYTES_R12 FCTX_GPR_OFFSET_BYTES_BY_INDEX(2)
70 #define FCTX_GPR_OFFSET_BYTES_R13 FCTX_GPR_OFFSET_BYTES_BY_INDEX(3)
71 #define FCTX_GPR_OFFSET_BYTES_R14 FCTX_GPR_OFFSET_BYTES_BY_INDEX(4)
72 #define FCTX_GPR_OFFSET_BYTES_R15 FCTX_GPR_OFFSET_BYTES_BY_INDEX(5)
73 #define FCTX_GPR_OFFSET_BYTES_RDI FCTX_GPR_OFFSET_BYTES_BY_INDEX(6)
74 #define FCTX_GPR_OFFSET_BYTES_RIP FCTX_GPR_OFFSET_BYTES_BY_INDEX(7)
75 #define FCTX_GPR_OFFSET_BYTES_RSP FCTX_GPR_OFFSET_BYTES_BY_INDEX(8)
76 // fp
77 #define FCTX_FPENV_OFFSET_BYTES 80
78 #define FCTX_FP_OFFSET_BYTES_FPENV FCTX_FPENV_OFFSET_BYTES
79 #define FCTX_MXCSR_OFFSET_BYTES 112
80 #define FCTX_FP_OFFSET_BYTES_MXCSR FCTX_MXCSR_OFFSET_BYTES
81 
82 // NOLINTEND(cppcoreguidelines-macro-usage)
83 
84 #endif /* PANDA_RUNTIME_FIBERS_ARCH_AMD64_CONTEXT_LAYOUT_H */