1 | //===-- ABISysV_s390x.cpp -------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "ABISysV_s390x.h" |
10 | |
11 | #include "llvm/ADT/STLExtras.h" |
12 | #include "llvm/TargetParser/Triple.h" |
13 | |
14 | #include "lldb/Core/Module.h" |
15 | #include "lldb/Core/PluginManager.h" |
16 | #include "lldb/Core/Value.h" |
17 | #include "lldb/Symbol/UnwindPlan.h" |
18 | #include "lldb/Target/Process.h" |
19 | #include "lldb/Target/RegisterContext.h" |
20 | #include "lldb/Target/StackFrame.h" |
21 | #include "lldb/Target/Target.h" |
22 | #include "lldb/Target/Thread.h" |
23 | #include "lldb/Utility/ConstString.h" |
24 | #include "lldb/Utility/DataExtractor.h" |
25 | #include "lldb/Utility/LLDBLog.h" |
26 | #include "lldb/Utility/Log.h" |
27 | #include "lldb/Utility/RegisterValue.h" |
28 | #include "lldb/Utility/Status.h" |
29 | #include "lldb/ValueObject/ValueObjectConstResult.h" |
30 | #include "lldb/ValueObject/ValueObjectMemory.h" |
31 | #include "lldb/ValueObject/ValueObjectRegister.h" |
32 | #include <optional> |
33 | |
34 | using namespace lldb; |
35 | using namespace lldb_private; |
36 | |
37 | LLDB_PLUGIN_DEFINE_ADV(ABISysV_s390x, ABISystemZ) |
38 | |
39 | enum dwarf_regnums { |
40 | // General Purpose Registers |
41 | dwarf_r0_s390x = 0, |
42 | dwarf_r1_s390x, |
43 | dwarf_r2_s390x, |
44 | dwarf_r3_s390x, |
45 | dwarf_r4_s390x, |
46 | dwarf_r5_s390x, |
47 | dwarf_r6_s390x, |
48 | dwarf_r7_s390x, |
49 | dwarf_r8_s390x, |
50 | dwarf_r9_s390x, |
51 | dwarf_r10_s390x, |
52 | dwarf_r11_s390x, |
53 | dwarf_r12_s390x, |
54 | dwarf_r13_s390x, |
55 | dwarf_r14_s390x, |
56 | dwarf_r15_s390x, |
57 | // Floating Point Registers / Vector Registers 0-15 |
58 | dwarf_f0_s390x = 16, |
59 | dwarf_f2_s390x, |
60 | dwarf_f4_s390x, |
61 | dwarf_f6_s390x, |
62 | dwarf_f1_s390x, |
63 | dwarf_f3_s390x, |
64 | dwarf_f5_s390x, |
65 | dwarf_f7_s390x, |
66 | dwarf_f8_s390x, |
67 | dwarf_f10_s390x, |
68 | dwarf_f12_s390x, |
69 | dwarf_f14_s390x, |
70 | dwarf_f9_s390x, |
71 | dwarf_f11_s390x, |
72 | dwarf_f13_s390x, |
73 | dwarf_f15_s390x, |
74 | // Access Registers |
75 | dwarf_acr0_s390x = 48, |
76 | dwarf_acr1_s390x, |
77 | dwarf_acr2_s390x, |
78 | dwarf_acr3_s390x, |
79 | dwarf_acr4_s390x, |
80 | dwarf_acr5_s390x, |
81 | dwarf_acr6_s390x, |
82 | dwarf_acr7_s390x, |
83 | dwarf_acr8_s390x, |
84 | dwarf_acr9_s390x, |
85 | dwarf_acr10_s390x, |
86 | dwarf_acr11_s390x, |
87 | dwarf_acr12_s390x, |
88 | dwarf_acr13_s390x, |
89 | dwarf_acr14_s390x, |
90 | dwarf_acr15_s390x, |
91 | // Program Status Word |
92 | dwarf_pswm_s390x = 64, |
93 | dwarf_pswa_s390x, |
94 | // Vector Registers 16-31 |
95 | dwarf_v16_s390x = 68, |
96 | dwarf_v18_s390x, |
97 | dwarf_v20_s390x, |
98 | dwarf_v22_s390x, |
99 | dwarf_v17_s390x, |
100 | dwarf_v19_s390x, |
101 | dwarf_v21_s390x, |
102 | dwarf_v23_s390x, |
103 | dwarf_v24_s390x, |
104 | dwarf_v26_s390x, |
105 | dwarf_v28_s390x, |
106 | dwarf_v30_s390x, |
107 | dwarf_v25_s390x, |
108 | dwarf_v27_s390x, |
109 | dwarf_v29_s390x, |
110 | dwarf_v31_s390x, |
111 | }; |
112 | |
113 | // RegisterKind: EHFrame, DWARF, Generic, Process Plugin, LLDB |
114 | |
115 | #define DEFINE_REG(name, size, alt, generic) \ |
116 | { \ |
117 | #name, alt, size, 0, eEncodingUint, eFormatHex, \ |
118 | {dwarf_##name##_s390x, dwarf_##name##_s390x, generic, \ |
119 | LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM }, \ |
120 | nullptr, nullptr, nullptr, \ |
121 | } |
122 | |
123 | static const RegisterInfo g_register_infos[] = { |
124 | DEFINE_REG(r0, 8, nullptr, LLDB_INVALID_REGNUM), |
125 | DEFINE_REG(r1, 8, nullptr, LLDB_INVALID_REGNUM), |
126 | DEFINE_REG(r2, 8, nullptr, LLDB_REGNUM_GENERIC_ARG1), |
127 | DEFINE_REG(r3, 8, nullptr, LLDB_REGNUM_GENERIC_ARG2), |
128 | DEFINE_REG(r4, 8, nullptr, LLDB_REGNUM_GENERIC_ARG3), |
129 | DEFINE_REG(r5, 8, nullptr, LLDB_REGNUM_GENERIC_ARG4), |
130 | DEFINE_REG(r6, 8, nullptr, LLDB_REGNUM_GENERIC_ARG5), |
131 | DEFINE_REG(r7, 8, nullptr, LLDB_INVALID_REGNUM), |
132 | DEFINE_REG(r8, 8, nullptr, LLDB_INVALID_REGNUM), |
133 | DEFINE_REG(r9, 8, nullptr, LLDB_INVALID_REGNUM), |
134 | DEFINE_REG(r10, 8, nullptr, LLDB_INVALID_REGNUM), |
135 | DEFINE_REG(r11, 8, nullptr, LLDB_REGNUM_GENERIC_FP), |
136 | DEFINE_REG(r12, 8, nullptr, LLDB_INVALID_REGNUM), |
137 | DEFINE_REG(r13, 8, nullptr, LLDB_INVALID_REGNUM), |
138 | DEFINE_REG(r14, 8, nullptr, LLDB_INVALID_REGNUM), |
139 | DEFINE_REG(r15, 8, "sp" , LLDB_REGNUM_GENERIC_SP), |
140 | DEFINE_REG(acr0, 4, nullptr, LLDB_INVALID_REGNUM), |
141 | DEFINE_REG(acr1, 4, nullptr, LLDB_INVALID_REGNUM), |
142 | DEFINE_REG(acr2, 4, nullptr, LLDB_INVALID_REGNUM), |
143 | DEFINE_REG(acr3, 4, nullptr, LLDB_INVALID_REGNUM), |
144 | DEFINE_REG(acr4, 4, nullptr, LLDB_INVALID_REGNUM), |
145 | DEFINE_REG(acr5, 4, nullptr, LLDB_INVALID_REGNUM), |
146 | DEFINE_REG(acr6, 4, nullptr, LLDB_INVALID_REGNUM), |
147 | DEFINE_REG(acr7, 4, nullptr, LLDB_INVALID_REGNUM), |
148 | DEFINE_REG(acr8, 4, nullptr, LLDB_INVALID_REGNUM), |
149 | DEFINE_REG(acr9, 4, nullptr, LLDB_INVALID_REGNUM), |
150 | DEFINE_REG(acr10, 4, nullptr, LLDB_INVALID_REGNUM), |
151 | DEFINE_REG(acr11, 4, nullptr, LLDB_INVALID_REGNUM), |
152 | DEFINE_REG(acr12, 4, nullptr, LLDB_INVALID_REGNUM), |
153 | DEFINE_REG(acr13, 4, nullptr, LLDB_INVALID_REGNUM), |
154 | DEFINE_REG(acr14, 4, nullptr, LLDB_INVALID_REGNUM), |
155 | DEFINE_REG(acr15, 4, nullptr, LLDB_INVALID_REGNUM), |
156 | DEFINE_REG(pswm, 8, nullptr, LLDB_REGNUM_GENERIC_FLAGS), |
157 | DEFINE_REG(pswa, 8, nullptr, LLDB_REGNUM_GENERIC_PC), |
158 | DEFINE_REG(f0, 8, nullptr, LLDB_INVALID_REGNUM), |
159 | DEFINE_REG(f1, 8, nullptr, LLDB_INVALID_REGNUM), |
160 | DEFINE_REG(f2, 8, nullptr, LLDB_INVALID_REGNUM), |
161 | DEFINE_REG(f3, 8, nullptr, LLDB_INVALID_REGNUM), |
162 | DEFINE_REG(f4, 8, nullptr, LLDB_INVALID_REGNUM), |
163 | DEFINE_REG(f5, 8, nullptr, LLDB_INVALID_REGNUM), |
164 | DEFINE_REG(f6, 8, nullptr, LLDB_INVALID_REGNUM), |
165 | DEFINE_REG(f7, 8, nullptr, LLDB_INVALID_REGNUM), |
166 | DEFINE_REG(f8, 8, nullptr, LLDB_INVALID_REGNUM), |
167 | DEFINE_REG(f9, 8, nullptr, LLDB_INVALID_REGNUM), |
168 | DEFINE_REG(f10, 8, nullptr, LLDB_INVALID_REGNUM), |
169 | DEFINE_REG(f11, 8, nullptr, LLDB_INVALID_REGNUM), |
170 | DEFINE_REG(f12, 8, nullptr, LLDB_INVALID_REGNUM), |
171 | DEFINE_REG(f13, 8, nullptr, LLDB_INVALID_REGNUM), |
172 | DEFINE_REG(f14, 8, nullptr, LLDB_INVALID_REGNUM), |
173 | DEFINE_REG(f15, 8, nullptr, LLDB_INVALID_REGNUM), |
174 | }; |
175 | |
176 | static const uint32_t k_num_register_infos = std::size(g_register_infos); |
177 | |
178 | const lldb_private::RegisterInfo * |
179 | ABISysV_s390x::GetRegisterInfoArray(uint32_t &count) { |
180 | count = k_num_register_infos; |
181 | return g_register_infos; |
182 | } |
183 | |
184 | size_t ABISysV_s390x::GetRedZoneSize() const { return 0; } |
185 | |
186 | // Static Functions |
187 | |
188 | ABISP |
189 | ABISysV_s390x::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) { |
190 | if (arch.GetTriple().getArch() == llvm::Triple::systemz) { |
191 | return ABISP(new ABISysV_s390x(std::move(process_sp), MakeMCRegisterInfo(arch))); |
192 | } |
193 | return ABISP(); |
194 | } |
195 | |
196 | bool ABISysV_s390x::PrepareTrivialCall(Thread &thread, addr_t sp, |
197 | addr_t func_addr, addr_t return_addr, |
198 | llvm::ArrayRef<addr_t> args) const { |
199 | Log *log = GetLog(mask: LLDBLog::Expressions); |
200 | |
201 | if (log) { |
202 | StreamString s; |
203 | s.Printf(format: "ABISysV_s390x::PrepareTrivialCall (tid = 0x%" PRIx64 |
204 | ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64 |
205 | ", return_addr = 0x%" PRIx64, |
206 | thread.GetID(), (uint64_t)sp, (uint64_t)func_addr, |
207 | (uint64_t)return_addr); |
208 | |
209 | for (size_t i = 0; i < args.size(); ++i) |
210 | s.Printf(format: ", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1), |
211 | args[i]); |
212 | s.PutCString(cstr: ")" ); |
213 | log->PutString(str: s.GetString()); |
214 | } |
215 | |
216 | RegisterContext *reg_ctx = thread.GetRegisterContext().get(); |
217 | if (!reg_ctx) |
218 | return false; |
219 | |
220 | const RegisterInfo *pc_reg_info = |
221 | reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
222 | const RegisterInfo *sp_reg_info = |
223 | reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); |
224 | const RegisterInfo *ra_reg_info = reg_ctx->GetRegisterInfoByName(reg_name: "r14" , start_idx: 0); |
225 | ProcessSP process_sp(thread.GetProcess()); |
226 | |
227 | // Allocate a new stack frame and space for stack arguments if necessary |
228 | |
229 | addr_t arg_pos = 0; |
230 | if (args.size() > 5) { |
231 | sp -= 8 * (args.size() - 5); |
232 | arg_pos = sp; |
233 | } |
234 | |
235 | sp -= 160; |
236 | |
237 | // Process arguments |
238 | |
239 | for (size_t i = 0; i < args.size(); ++i) { |
240 | if (i < 5) { |
241 | const RegisterInfo *reg_info = reg_ctx->GetRegisterInfo( |
242 | reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + i); |
243 | LLDB_LOGF(log, "About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s" , |
244 | static_cast<uint64_t>(i + 1), args[i], reg_info->name); |
245 | if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, uval: args[i])) |
246 | return false; |
247 | } else { |
248 | Status error; |
249 | LLDB_LOGF(log, "About to write arg%" PRIu64 " (0x%" PRIx64 ") onto stack" , |
250 | static_cast<uint64_t>(i + 1), args[i]); |
251 | if (!process_sp->WritePointerToMemory(vm_addr: arg_pos, ptr_value: args[i], error)) |
252 | return false; |
253 | arg_pos += 8; |
254 | } |
255 | } |
256 | |
257 | // %r14 is set to the return address |
258 | |
259 | LLDB_LOGF(log, "Writing RA: 0x%" PRIx64, (uint64_t)return_addr); |
260 | |
261 | if (!reg_ctx->WriteRegisterFromUnsigned(reg_info: ra_reg_info, uval: return_addr)) |
262 | return false; |
263 | |
264 | // %r15 is set to the actual stack value. |
265 | |
266 | LLDB_LOGF(log, "Writing SP: 0x%" PRIx64, (uint64_t)sp); |
267 | |
268 | if (!reg_ctx->WriteRegisterFromUnsigned(reg_info: sp_reg_info, uval: sp)) |
269 | return false; |
270 | |
271 | // %pc is set to the address of the called function. |
272 | |
273 | LLDB_LOGF(log, "Writing PC: 0x%" PRIx64, (uint64_t)func_addr); |
274 | |
275 | if (!reg_ctx->WriteRegisterFromUnsigned(reg_info: pc_reg_info, uval: func_addr)) |
276 | return false; |
277 | |
278 | return true; |
279 | } |
280 | |
281 | static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width, |
282 | bool is_signed, Thread &thread, |
283 | uint32_t *argument_register_ids, |
284 | unsigned int ¤t_argument_register, |
285 | addr_t ¤t_stack_argument) { |
286 | if (bit_width > 64) |
287 | return false; // Scalar can't hold large integer arguments |
288 | |
289 | if (current_argument_register < 5) { |
290 | scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned( |
291 | reg: argument_register_ids[current_argument_register], fail_value: 0); |
292 | current_argument_register++; |
293 | if (is_signed) |
294 | scalar.SignExtend(bit_pos: bit_width); |
295 | } else { |
296 | uint32_t byte_size = (bit_width + (8 - 1)) / 8; |
297 | Status error; |
298 | if (thread.GetProcess()->ReadScalarIntegerFromMemory( |
299 | addr: current_stack_argument + 8 - byte_size, byte_size, is_signed, |
300 | scalar, error)) { |
301 | current_stack_argument += 8; |
302 | return true; |
303 | } |
304 | return false; |
305 | } |
306 | return true; |
307 | } |
308 | |
309 | bool ABISysV_s390x::GetArgumentValues(Thread &thread, ValueList &values) const { |
310 | unsigned int num_values = values.GetSize(); |
311 | unsigned int value_index; |
312 | |
313 | // Extract the register context so we can read arguments from registers |
314 | |
315 | RegisterContext *reg_ctx = thread.GetRegisterContext().get(); |
316 | |
317 | if (!reg_ctx) |
318 | return false; |
319 | |
320 | // Get the pointer to the first stack argument so we have a place to start |
321 | // when reading data |
322 | |
323 | addr_t sp = reg_ctx->GetSP(fail_value: 0); |
324 | |
325 | if (!sp) |
326 | return false; |
327 | |
328 | addr_t current_stack_argument = sp + 160; |
329 | |
330 | uint32_t argument_register_ids[5]; |
331 | |
332 | argument_register_ids[0] = |
333 | reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1) |
334 | ->kinds[eRegisterKindLLDB]; |
335 | argument_register_ids[1] = |
336 | reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2) |
337 | ->kinds[eRegisterKindLLDB]; |
338 | argument_register_ids[2] = |
339 | reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3) |
340 | ->kinds[eRegisterKindLLDB]; |
341 | argument_register_ids[3] = |
342 | reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4) |
343 | ->kinds[eRegisterKindLLDB]; |
344 | argument_register_ids[4] = |
345 | reg_ctx->GetRegisterInfo(reg_kind: eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG5) |
346 | ->kinds[eRegisterKindLLDB]; |
347 | |
348 | unsigned int current_argument_register = 0; |
349 | |
350 | for (value_index = 0; value_index < num_values; ++value_index) { |
351 | Value *value = values.GetValueAtIndex(idx: value_index); |
352 | |
353 | if (!value) |
354 | return false; |
355 | |
356 | // We currently only support extracting values with Clang QualTypes. Do we |
357 | // care about others? |
358 | CompilerType compiler_type = value->GetCompilerType(); |
359 | std::optional<uint64_t> bit_size = |
360 | llvm::expectedToOptional(E: compiler_type.GetBitSize(exe_scope: &thread)); |
361 | if (!bit_size) |
362 | return false; |
363 | bool is_signed; |
364 | |
365 | if (compiler_type.IsIntegerOrEnumerationType(is_signed)) { |
366 | ReadIntegerArgument(scalar&: value->GetScalar(), bit_width: *bit_size, is_signed, thread, |
367 | argument_register_ids, current_argument_register, |
368 | current_stack_argument); |
369 | } else if (compiler_type.IsPointerType()) { |
370 | ReadIntegerArgument(scalar&: value->GetScalar(), bit_width: *bit_size, is_signed: false, thread, |
371 | argument_register_ids, current_argument_register, |
372 | current_stack_argument); |
373 | } |
374 | } |
375 | |
376 | return true; |
377 | } |
378 | |
379 | Status ABISysV_s390x::SetReturnValueObject(lldb::StackFrameSP &frame_sp, |
380 | lldb::ValueObjectSP &new_value_sp) { |
381 | Status error; |
382 | if (!new_value_sp) { |
383 | error = Status::FromErrorString(str: "Empty value object for return value." ); |
384 | return error; |
385 | } |
386 | |
387 | CompilerType compiler_type = new_value_sp->GetCompilerType(); |
388 | if (!compiler_type) { |
389 | error = Status::FromErrorString(str: "Null clang type for return value." ); |
390 | return error; |
391 | } |
392 | |
393 | Thread *thread = frame_sp->GetThread().get(); |
394 | |
395 | bool is_signed; |
396 | uint32_t count; |
397 | bool is_complex; |
398 | |
399 | RegisterContext *reg_ctx = thread->GetRegisterContext().get(); |
400 | |
401 | bool set_it_simple = false; |
402 | if (compiler_type.IsIntegerOrEnumerationType(is_signed) || |
403 | compiler_type.IsPointerType()) { |
404 | const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(reg_name: "r2" , start_idx: 0); |
405 | |
406 | DataExtractor data; |
407 | Status data_error; |
408 | size_t num_bytes = new_value_sp->GetData(data, error&: data_error); |
409 | if (data_error.Fail()) { |
410 | error = Status::FromErrorStringWithFormat( |
411 | format: "Couldn't convert return value to raw data: %s" , |
412 | data_error.AsCString()); |
413 | return error; |
414 | } |
415 | lldb::offset_t offset = 0; |
416 | if (num_bytes <= 8) { |
417 | uint64_t raw_value = data.GetMaxU64(offset_ptr: &offset, byte_size: num_bytes); |
418 | |
419 | if (reg_ctx->WriteRegisterFromUnsigned(reg_info, uval: raw_value)) |
420 | set_it_simple = true; |
421 | } else { |
422 | error = Status::FromErrorString( |
423 | str: "We don't support returning longer than 64 bit " |
424 | "integer values at present." ); |
425 | } |
426 | } else if (compiler_type.IsFloatingPointType(count, is_complex)) { |
427 | if (is_complex) |
428 | error = Status::FromErrorString( |
429 | str: "We don't support returning complex values at present" ); |
430 | else { |
431 | std::optional<uint64_t> bit_width = |
432 | llvm::expectedToOptional(E: compiler_type.GetBitSize(exe_scope: frame_sp.get())); |
433 | if (!bit_width) { |
434 | error = Status::FromErrorString(str: "can't get type size" ); |
435 | return error; |
436 | } |
437 | if (*bit_width <= 64) { |
438 | const RegisterInfo *f0_info = reg_ctx->GetRegisterInfoByName(reg_name: "f0" , start_idx: 0); |
439 | RegisterValue f0_value; |
440 | DataExtractor data; |
441 | Status data_error; |
442 | size_t num_bytes = new_value_sp->GetData(data, error&: data_error); |
443 | if (data_error.Fail()) { |
444 | error = Status::FromErrorStringWithFormat( |
445 | format: "Couldn't convert return value to raw data: %s" , |
446 | data_error.AsCString()); |
447 | return error; |
448 | } |
449 | |
450 | unsigned char buffer[8]; |
451 | ByteOrder byte_order = data.GetByteOrder(); |
452 | |
453 | data.CopyByteOrderedData(src_offset: 0, src_len: num_bytes, dst: buffer, dst_len: 8, dst_byte_order: byte_order); |
454 | f0_value.SetBytes(bytes: buffer, length: 8, byte_order); |
455 | reg_ctx->WriteRegister(reg_info: f0_info, reg_value: f0_value); |
456 | set_it_simple = true; |
457 | } else { |
458 | // FIXME - don't know how to do long doubles yet. |
459 | error = Status::FromErrorString( |
460 | str: "We don't support returning float values > 64 bits at present" ); |
461 | } |
462 | } |
463 | } |
464 | |
465 | if (!set_it_simple) { |
466 | // Okay we've got a structure or something that doesn't fit in a simple |
467 | // register. We should figure out where it really goes, but we don't |
468 | // support this yet. |
469 | error = Status::FromErrorString( |
470 | str: "We only support setting simple integer and float " |
471 | "return types at present." ); |
472 | } |
473 | |
474 | return error; |
475 | } |
476 | |
477 | ValueObjectSP ABISysV_s390x::GetReturnValueObjectSimple( |
478 | Thread &thread, CompilerType &return_compiler_type) const { |
479 | ValueObjectSP return_valobj_sp; |
480 | Value value; |
481 | |
482 | if (!return_compiler_type) |
483 | return return_valobj_sp; |
484 | |
485 | // value.SetContext (Value::eContextTypeClangType, return_value_type); |
486 | value.SetCompilerType(return_compiler_type); |
487 | |
488 | RegisterContext *reg_ctx = thread.GetRegisterContext().get(); |
489 | if (!reg_ctx) |
490 | return return_valobj_sp; |
491 | |
492 | const uint32_t type_flags = return_compiler_type.GetTypeInfo(); |
493 | if (type_flags & eTypeIsScalar) { |
494 | value.SetValueType(Value::ValueType::Scalar); |
495 | |
496 | bool success = false; |
497 | if (type_flags & eTypeIsInteger) { |
498 | // Extract the register context so we can read arguments from registers. |
499 | std::optional<uint64_t> byte_size = |
500 | llvm::expectedToOptional(E: return_compiler_type.GetByteSize(exe_scope: &thread)); |
501 | if (!byte_size) |
502 | return return_valobj_sp; |
503 | uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned( |
504 | reg_info: reg_ctx->GetRegisterInfoByName(reg_name: "r2" , start_idx: 0), fail_value: 0); |
505 | const bool is_signed = (type_flags & eTypeIsSigned) != 0; |
506 | switch (*byte_size) { |
507 | default: |
508 | break; |
509 | |
510 | case sizeof(uint64_t): |
511 | if (is_signed) |
512 | value.GetScalar() = (int64_t)(raw_value); |
513 | else |
514 | value.GetScalar() = (uint64_t)(raw_value); |
515 | success = true; |
516 | break; |
517 | |
518 | case sizeof(uint32_t): |
519 | if (is_signed) |
520 | value.GetScalar() = (int32_t)(raw_value & UINT32_MAX); |
521 | else |
522 | value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX); |
523 | success = true; |
524 | break; |
525 | |
526 | case sizeof(uint16_t): |
527 | if (is_signed) |
528 | value.GetScalar() = (int16_t)(raw_value & UINT16_MAX); |
529 | else |
530 | value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX); |
531 | success = true; |
532 | break; |
533 | |
534 | case sizeof(uint8_t): |
535 | if (is_signed) |
536 | value.GetScalar() = (int8_t)(raw_value & UINT8_MAX); |
537 | else |
538 | value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX); |
539 | success = true; |
540 | break; |
541 | } |
542 | } else if (type_flags & eTypeIsFloat) { |
543 | if (type_flags & eTypeIsComplex) { |
544 | // Don't handle complex yet. |
545 | } else { |
546 | std::optional<uint64_t> byte_size = |
547 | llvm::expectedToOptional(E: return_compiler_type.GetByteSize(exe_scope: &thread)); |
548 | if (byte_size && *byte_size <= sizeof(long double)) { |
549 | const RegisterInfo *f0_info = reg_ctx->GetRegisterInfoByName(reg_name: "f0" , start_idx: 0); |
550 | RegisterValue f0_value; |
551 | if (reg_ctx->ReadRegister(reg_info: f0_info, reg_value&: f0_value)) { |
552 | DataExtractor data; |
553 | if (f0_value.GetData(data)) { |
554 | lldb::offset_t offset = 0; |
555 | if (*byte_size == sizeof(float)) { |
556 | value.GetScalar() = (float)data.GetFloat(offset_ptr: &offset); |
557 | success = true; |
558 | } else if (*byte_size == sizeof(double)) { |
559 | value.GetScalar() = (double)data.GetDouble(offset_ptr: &offset); |
560 | success = true; |
561 | } else if (*byte_size == sizeof(long double)) { |
562 | // Don't handle long double yet. |
563 | } |
564 | } |
565 | } |
566 | } |
567 | } |
568 | } |
569 | |
570 | if (success) |
571 | return_valobj_sp = ValueObjectConstResult::Create( |
572 | exe_scope: thread.GetStackFrameAtIndex(idx: 0).get(), value, name: ConstString("" )); |
573 | } else if (type_flags & eTypeIsPointer) { |
574 | unsigned r2_id = |
575 | reg_ctx->GetRegisterInfoByName(reg_name: "r2" , start_idx: 0)->kinds[eRegisterKindLLDB]; |
576 | value.GetScalar() = |
577 | (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(reg: r2_id, fail_value: 0); |
578 | value.SetValueType(Value::ValueType::Scalar); |
579 | return_valobj_sp = ValueObjectConstResult::Create( |
580 | exe_scope: thread.GetStackFrameAtIndex(idx: 0).get(), value, name: ConstString("" )); |
581 | } |
582 | |
583 | return return_valobj_sp; |
584 | } |
585 | |
586 | ValueObjectSP ABISysV_s390x::GetReturnValueObjectImpl( |
587 | Thread &thread, CompilerType &return_compiler_type) const { |
588 | ValueObjectSP return_valobj_sp; |
589 | |
590 | if (!return_compiler_type) |
591 | return return_valobj_sp; |
592 | |
593 | ExecutionContext exe_ctx(thread.shared_from_this()); |
594 | return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type); |
595 | if (return_valobj_sp) |
596 | return return_valobj_sp; |
597 | |
598 | RegisterContextSP reg_ctx_sp = thread.GetRegisterContext(); |
599 | if (!reg_ctx_sp) |
600 | return return_valobj_sp; |
601 | |
602 | if (return_compiler_type.IsAggregateType()) { |
603 | // FIXME: This is just taking a guess, r2 may very well no longer hold the |
604 | // return storage location. |
605 | // If we are going to do this right, when we make a new frame we should |
606 | // check to see if it uses a memory return, and if we are at the first |
607 | // instruction and if so stash away the return location. Then we would |
608 | // only return the memory return value if we know it is valid. |
609 | |
610 | unsigned r2_id = |
611 | reg_ctx_sp->GetRegisterInfoByName(reg_name: "r2" , start_idx: 0)->kinds[eRegisterKindLLDB]; |
612 | lldb::addr_t storage_addr = |
613 | (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(reg: r2_id, fail_value: 0); |
614 | return_valobj_sp = ValueObjectMemory::Create( |
615 | exe_scope: &thread, name: "" , address: Address(storage_addr, nullptr), ast_type: return_compiler_type); |
616 | } |
617 | |
618 | return return_valobj_sp; |
619 | } |
620 | |
621 | UnwindPlanSP ABISysV_s390x::CreateFunctionEntryUnwindPlan() { |
622 | UnwindPlan::Row row; |
623 | |
624 | // Our Call Frame Address is the stack pointer value + 160 |
625 | row.GetCFAValue().SetIsRegisterPlusOffset(reg_num: dwarf_r15_s390x, offset: 160); |
626 | |
627 | // The previous PC is in r14 |
628 | row.SetRegisterLocationToRegister(reg_num: dwarf_pswa_s390x, other_reg_num: dwarf_r14_s390x, can_replace: true); |
629 | |
630 | // All other registers are the same. |
631 | auto plan_sp = std::make_shared<UnwindPlan>(args: eRegisterKindDWARF); |
632 | plan_sp->AppendRow(row: std::move(row)); |
633 | plan_sp->SetSourceName("s390x at-func-entry default" ); |
634 | plan_sp->SetSourcedFromCompiler(eLazyBoolNo); |
635 | return plan_sp; |
636 | } |
637 | |
638 | UnwindPlanSP ABISysV_s390x::CreateDefaultUnwindPlan() { |
639 | // There's really no default way to unwind on s390x. Trust the .eh_frame CFI, |
640 | // which should always be good. |
641 | return nullptr; |
642 | } |
643 | |
644 | bool ABISysV_s390x::GetFallbackRegisterLocation( |
645 | const RegisterInfo *reg_info, |
646 | UnwindPlan::Row::AbstractRegisterLocation &unwind_regloc) { |
647 | // If a volatile register is being requested, we don't want to forward the |
648 | // next frame's register contents up the stack -- the register is not |
649 | // retrievable at this frame. |
650 | if (RegisterIsVolatile(reg_info)) { |
651 | unwind_regloc.SetUndefined(); |
652 | return true; |
653 | } |
654 | |
655 | return false; |
656 | } |
657 | |
658 | bool ABISysV_s390x::RegisterIsVolatile(const RegisterInfo *reg_info) { |
659 | return !RegisterIsCalleeSaved(reg_info); |
660 | } |
661 | |
662 | bool ABISysV_s390x::RegisterIsCalleeSaved(const RegisterInfo *reg_info) { |
663 | if (reg_info) { |
664 | // Preserved registers are : |
665 | // r6-r13, r15 |
666 | // f8-f15 |
667 | |
668 | const char *name = reg_info->name; |
669 | if (name[0] == 'r') { |
670 | switch (name[1]) { |
671 | case '6': // r6 |
672 | case '7': // r7 |
673 | case '8': // r8 |
674 | case '9': // r9 |
675 | return name[2] == '\0'; |
676 | |
677 | case '1': // r10, r11, r12, r13, r15 |
678 | if ((name[2] >= '0' && name[2] <= '3') || name[2] == '5') |
679 | return name[3] == '\0'; |
680 | break; |
681 | |
682 | default: |
683 | break; |
684 | } |
685 | } |
686 | if (name[0] == 'f') { |
687 | switch (name[1]) { |
688 | case '8': // r8 |
689 | case '9': // r9 |
690 | return name[2] == '\0'; |
691 | |
692 | case '1': // r10, r11, r12, r13, r14, r15 |
693 | if (name[2] >= '0' && name[2] <= '5') |
694 | return name[3] == '\0'; |
695 | break; |
696 | |
697 | default: |
698 | break; |
699 | } |
700 | } |
701 | |
702 | // Accept shorter-variant versions |
703 | if (name[0] == 's' && name[1] == 'p' && name[2] == '\0') // sp |
704 | return true; |
705 | if (name[0] == 'f' && name[1] == 'p' && name[2] == '\0') // fp |
706 | return true; |
707 | if (name[0] == 'p' && name[1] == 'c' && name[2] == '\0') // pc |
708 | return true; |
709 | } |
710 | return false; |
711 | } |
712 | |
713 | void ABISysV_s390x::Initialize() { |
714 | PluginManager::RegisterPlugin( |
715 | name: GetPluginNameStatic(), description: "System V ABI for s390x targets" , create_callback: CreateInstance); |
716 | } |
717 | |
718 | void ABISysV_s390x::Terminate() { |
719 | PluginManager::UnregisterPlugin(create_callback: CreateInstance); |
720 | } |
721 | |