1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "xptcprivate.h"
7 
8 #ifndef __AARCH64EL__
9 #error "Only little endian compatibility was tested"
10 #endif
11 
12 template<typename T> void
get_value_and_advance(T * aOutValue,void * & aStack)13 get_value_and_advance(T* aOutValue, void*& aStack) {
14 #ifdef __APPLE__
15     const size_t aligned_size = sizeof(T);
16 #else
17     const size_t aligned_size = 8;
18 #endif
19     // Ensure the pointer is aligned for the type
20     uintptr_t addr = (reinterpret_cast<uintptr_t>(aStack) + aligned_size - 1) & ~(aligned_size - 1);
21     memcpy(aOutValue, reinterpret_cast<void*>(addr), sizeof(T));
22     aStack = reinterpret_cast<void*>(addr + aligned_size);
23 }
24 
25 /*
26  * This is for AArch64 ABI
27  *
28  * When we're called, the "gp" registers are stored in gprData and
29  * the "fp" registers are stored in fprData. Each array has 8 regs
30  * but first reg in gprData is a placeholder for 'self'.
31  */
32 extern "C" nsresult ATTRIBUTE_USED
PrepareAndDispatch(nsXPTCStubBase * self,uint32_t methodIndex,void * args,uint64_t * gprData,double * fprData)33 PrepareAndDispatch(nsXPTCStubBase* self, uint32_t methodIndex, void* args,
34                    uint64_t *gprData, double *fprData)
35 {
36 #define PARAM_GPR_COUNT            8
37 #define PARAM_FPR_COUNT            8
38 
39     nsXPTCMiniVariant paramBuffer[PARAM_BUFFER_COUNT];
40     const nsXPTMethodInfo* info;
41 
42     NS_ASSERTION(self,"no self");
43 
44     self->mEntry->GetMethodInfo(uint16_t(methodIndex), &info);
45     NS_ASSERTION(info,"no method info");
46 
47     uint32_t paramCount = info->GetParamCount();
48 
49     const uint8_t indexOfJSContext = info->IndexOfJSContext();
50 
51     void* ap = args;
52     uint32_t next_gpr = 1; // skip first arg which is 'self'
53     uint32_t next_fpr = 0;
54     for (uint32_t i = 0; i < paramCount; i++) {
55         const nsXPTParamInfo& param = info->GetParam(i);
56         const nsXPTType& type = param.GetType();
57         nsXPTCMiniVariant* dp = &paramBuffer[i];
58 
59         if (i == indexOfJSContext) {
60             if (next_gpr < PARAM_GPR_COUNT)
61                 next_gpr++;
62             else {
63                 void* value;
64                 get_value_and_advance(&value, ap);
65             }
66         }
67 
68         if (param.IsOut() || !type.IsArithmetic()) {
69             if (next_gpr < PARAM_GPR_COUNT) {
70                 dp->val.p = (void*)gprData[next_gpr++];
71             } else {
72                 get_value_and_advance(&dp->val.p, ap);
73             }
74             continue;
75         }
76 
77         switch (type) {
78             case nsXPTType::T_I8:
79                 if (next_gpr < PARAM_GPR_COUNT) {
80                     dp->val.i8  = (int8_t)gprData[next_gpr++];
81                 } else {
82                     get_value_and_advance(&dp->val.i8, ap);
83                 }
84                 break;
85 
86             case nsXPTType::T_I16:
87                 if (next_gpr < PARAM_GPR_COUNT) {
88                     dp->val.i16  = (int16_t)gprData[next_gpr++];
89                 } else {
90                     get_value_and_advance(&dp->val.i16, ap);
91                 }
92                 break;
93 
94             case nsXPTType::T_I32:
95                 if (next_gpr < PARAM_GPR_COUNT) {
96                     dp->val.i32  = (int32_t)gprData[next_gpr++];
97                 } else {
98                     get_value_and_advance(&dp->val.i32, ap);
99                 }
100                 break;
101 
102             case nsXPTType::T_I64:
103                 if (next_gpr < PARAM_GPR_COUNT) {
104                     dp->val.i64  = (int64_t)gprData[next_gpr++];
105                 } else {
106                     get_value_and_advance(&dp->val.i64, ap);
107                 }
108                 break;
109 
110             case nsXPTType::T_U8:
111                 if (next_gpr < PARAM_GPR_COUNT) {
112                     dp->val.u8  = (uint8_t)gprData[next_gpr++];
113                 } else {
114                     get_value_and_advance(&dp->val.u8, ap);
115                 }
116                 break;
117 
118             case nsXPTType::T_U16:
119                 if (next_gpr < PARAM_GPR_COUNT) {
120                     dp->val.u16  = (uint16_t)gprData[next_gpr++];
121                 } else {
122                     get_value_and_advance(&dp->val.u16, ap);
123                 }
124                 break;
125 
126             case nsXPTType::T_U32:
127                 if (next_gpr < PARAM_GPR_COUNT) {
128                     dp->val.u32  = (uint32_t)gprData[next_gpr++];
129                 } else {
130                     get_value_and_advance(&dp->val.u32, ap);
131                 }
132                 break;
133 
134             case nsXPTType::T_U64:
135                 if (next_gpr < PARAM_GPR_COUNT) {
136                     dp->val.u64  = (uint64_t)gprData[next_gpr++];
137                 } else {
138                     get_value_and_advance(&dp->val.u64, ap);
139                 }
140                 break;
141 
142             case nsXPTType::T_FLOAT:
143                 if (next_fpr < PARAM_FPR_COUNT) {
144                     memcpy(&dp->val.f, &fprData[next_fpr++], sizeof(dp->val.f));
145                 } else {
146                     get_value_and_advance(&dp->val.f, ap);
147                 }
148                 break;
149 
150             case nsXPTType::T_DOUBLE:
151                 if (next_fpr < PARAM_FPR_COUNT) {
152                     memcpy(&dp->val.d, &fprData[next_fpr++], sizeof(dp->val.d));
153                 } else {
154                     get_value_and_advance(&dp->val.d, ap);
155                 }
156                 break;
157 
158             case nsXPTType::T_BOOL:
159                 if (next_gpr < PARAM_GPR_COUNT) {
160                     dp->val.b  = (bool)(uint8_t)gprData[next_gpr++];
161                 } else {
162                     uint8_t value;
163                     get_value_and_advance(&value, ap);
164                     dp->val.b = (bool)value;
165                 }
166                 break;
167 
168             case nsXPTType::T_CHAR:
169                 if (next_gpr < PARAM_GPR_COUNT) {
170                     dp->val.c  = (char)gprData[next_gpr++];
171                 } else {
172                     get_value_and_advance(&dp->val.c, ap);
173                 }
174                 break;
175 
176             case nsXPTType::T_WCHAR:
177                 if (next_gpr < PARAM_GPR_COUNT) {
178                     dp->val.wc  = (wchar_t)gprData[next_gpr++];
179                 } else {
180                     get_value_and_advance(&dp->val.wc, ap);
181                 }
182                 break;
183 
184             default:
185                 NS_ASSERTION(0, "bad type");
186                 break;
187         }
188     }
189 
190     nsresult result = self->mOuter->CallMethod((uint16_t)methodIndex, info,
191                                                paramBuffer);
192 
193     return result;
194 }
195 
196 #ifdef __APPLE__
197 #define GNU(str)
198 #define UNDERSCORE "__"
199 #else
200 #define GNU(str) str
201 #define UNDERSCORE "_"
202 #endif
203 
204 // Load w17 with the constant 'n' and branch to SharedStub().
205 # define STUB_ENTRY(n)                                                  \
206     __asm__ (                                                           \
207             ".text\n\t"                                                 \
208             ".align 2\n\t"                                              \
209             ".if "#n" < 10 \n\t"                                        \
210             ".globl  " UNDERSCORE "ZN14nsXPTCStubBase5Stub"#n"Ev \n\t"  \
211             GNU(".hidden _ZN14nsXPTCStubBase5Stub"#n"Ev \n\t")          \
212             GNU(".type   _ZN14nsXPTCStubBase5Stub"#n"Ev,@function \n\n")\
213             "" UNDERSCORE "ZN14nsXPTCStubBase5Stub"#n"Ev: \n\t"         \
214             ".elseif "#n" < 100 \n\t"                                   \
215             ".globl  " UNDERSCORE "ZN14nsXPTCStubBase6Stub"#n"Ev \n\t"  \
216             GNU(".hidden _ZN14nsXPTCStubBase6Stub"#n"Ev \n\t")          \
217             GNU(".type   _ZN14nsXPTCStubBase6Stub"#n"Ev,@function \n\n")\
218             "" UNDERSCORE "ZN14nsXPTCStubBase6Stub"#n"Ev: \n\t"         \
219             ".elseif "#n" < 1000 \n\t"                                  \
220             ".globl  " UNDERSCORE "ZN14nsXPTCStubBase7Stub"#n"Ev \n\t"  \
221             GNU(".hidden _ZN14nsXPTCStubBase7Stub"#n"Ev \n\t")          \
222             GNU(".type   _ZN14nsXPTCStubBase7Stub"#n"Ev,@function \n\n")\
223             UNDERSCORE "ZN14nsXPTCStubBase7Stub"#n"Ev: \n\t"            \
224             ".else  \n\t"                                               \
225             ".err   \"stub number "#n" >= 1000 not yet supported\"\n"   \
226             ".endif \n\t"                                               \
227             "mov    w17,#"#n" \n\t"                                     \
228             "b      SharedStub \n"                                      \
229 );
230 
231 #define SENTINEL_ENTRY(n)                              \
232     nsresult nsXPTCStubBase::Sentinel##n()             \
233 {                                                      \
234     NS_ASSERTION(0,"nsXPTCStubBase::Sentinel called"); \
235     return NS_ERROR_NOT_IMPLEMENTED;                   \
236 }
237 
238 #include "xptcstubsdef.inc"
239