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