15ffd83dbSDimitry Andric //===-- RegisterContextPOSIX_powerpc.cpp ----------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
9*5f7ddb14SDimitry Andric #include <cerrno>
10*5f7ddb14SDimitry Andric #include <cstdint>
110b57cec5SDimitry Andric #include <cstring>
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "lldb/Target/Process.h"
140b57cec5SDimitry Andric #include "lldb/Target/Target.h"
150b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
160b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
170b57cec5SDimitry Andric #include "lldb/Utility/DataExtractor.h"
180b57cec5SDimitry Andric #include "lldb/Utility/Endian.h"
190b57cec5SDimitry Andric #include "lldb/Utility/RegisterValue.h"
200b57cec5SDimitry Andric #include "lldb/Utility/Scalar.h"
210b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #include "RegisterContextPOSIX_powerpc.h"
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric using namespace lldb_private;
260b57cec5SDimitry Andric using namespace lldb;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric static const uint32_t g_gpr_regnums[] = {
290b57cec5SDimitry Andric     gpr_r0_powerpc,  gpr_r1_powerpc,  gpr_r2_powerpc,  gpr_r3_powerpc,
300b57cec5SDimitry Andric     gpr_r4_powerpc,  gpr_r5_powerpc,  gpr_r6_powerpc,  gpr_r7_powerpc,
310b57cec5SDimitry Andric     gpr_r8_powerpc,  gpr_r9_powerpc,  gpr_r10_powerpc, gpr_r11_powerpc,
320b57cec5SDimitry Andric     gpr_r12_powerpc, gpr_r13_powerpc, gpr_r14_powerpc, gpr_r15_powerpc,
330b57cec5SDimitry Andric     gpr_r16_powerpc, gpr_r17_powerpc, gpr_r18_powerpc, gpr_r19_powerpc,
340b57cec5SDimitry Andric     gpr_r20_powerpc, gpr_r21_powerpc, gpr_r22_powerpc, gpr_r23_powerpc,
350b57cec5SDimitry Andric     gpr_r24_powerpc, gpr_r25_powerpc, gpr_r26_powerpc, gpr_r27_powerpc,
360b57cec5SDimitry Andric     gpr_r28_powerpc, gpr_r29_powerpc, gpr_r30_powerpc, gpr_r31_powerpc,
370b57cec5SDimitry Andric     gpr_lr_powerpc,  gpr_cr_powerpc,  gpr_xer_powerpc, gpr_ctr_powerpc,
380b57cec5SDimitry Andric     gpr_pc_powerpc,
390b57cec5SDimitry Andric };
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric static const uint32_t g_fpr_regnums[] = {
420b57cec5SDimitry Andric     fpr_f0_powerpc,    fpr_f1_powerpc,  fpr_f2_powerpc,  fpr_f3_powerpc,
430b57cec5SDimitry Andric     fpr_f4_powerpc,    fpr_f5_powerpc,  fpr_f6_powerpc,  fpr_f7_powerpc,
440b57cec5SDimitry Andric     fpr_f8_powerpc,    fpr_f9_powerpc,  fpr_f10_powerpc, fpr_f11_powerpc,
450b57cec5SDimitry Andric     fpr_f12_powerpc,   fpr_f13_powerpc, fpr_f14_powerpc, fpr_f15_powerpc,
460b57cec5SDimitry Andric     fpr_f16_powerpc,   fpr_f17_powerpc, fpr_f18_powerpc, fpr_f19_powerpc,
470b57cec5SDimitry Andric     fpr_f20_powerpc,   fpr_f21_powerpc, fpr_f22_powerpc, fpr_f23_powerpc,
480b57cec5SDimitry Andric     fpr_f24_powerpc,   fpr_f25_powerpc, fpr_f26_powerpc, fpr_f27_powerpc,
490b57cec5SDimitry Andric     fpr_f28_powerpc,   fpr_f29_powerpc, fpr_f30_powerpc, fpr_f31_powerpc,
500b57cec5SDimitry Andric     fpr_fpscr_powerpc,
510b57cec5SDimitry Andric };
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric static const uint32_t g_vmx_regnums[] = {
540b57cec5SDimitry Andric     vmx_v0_powerpc,     vmx_v1_powerpc,   vmx_v2_powerpc,  vmx_v3_powerpc,
550b57cec5SDimitry Andric     vmx_v4_powerpc,     vmx_v5_powerpc,   vmx_v6_powerpc,  vmx_v7_powerpc,
560b57cec5SDimitry Andric     vmx_v8_powerpc,     vmx_v9_powerpc,   vmx_v10_powerpc, vmx_v11_powerpc,
570b57cec5SDimitry Andric     vmx_v12_powerpc,    vmx_v13_powerpc,  vmx_v14_powerpc, vmx_v15_powerpc,
580b57cec5SDimitry Andric     vmx_v16_powerpc,    vmx_v17_powerpc,  vmx_v18_powerpc, vmx_v19_powerpc,
590b57cec5SDimitry Andric     vmx_v20_powerpc,    vmx_v21_powerpc,  vmx_v22_powerpc, vmx_v23_powerpc,
600b57cec5SDimitry Andric     vmx_v24_powerpc,    vmx_v25_powerpc,  vmx_v26_powerpc, vmx_v27_powerpc,
610b57cec5SDimitry Andric     vmx_v28_powerpc,    vmx_v29_powerpc,  vmx_v30_powerpc, vmx_v31_powerpc,
620b57cec5SDimitry Andric     vmx_vrsave_powerpc, vmx_vscr_powerpc,
630b57cec5SDimitry Andric };
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric // Number of register sets provided by this context.
660b57cec5SDimitry Andric enum { k_num_register_sets = 3 };
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric static const RegisterSet g_reg_sets_powerpc[k_num_register_sets] = {
690b57cec5SDimitry Andric     {"General Purpose Registers", "gpr", k_num_gpr_registers_powerpc,
700b57cec5SDimitry Andric      g_gpr_regnums},
710b57cec5SDimitry Andric     {"Floating Point Registers", "fpr", k_num_fpr_registers_powerpc,
720b57cec5SDimitry Andric      g_fpr_regnums},
730b57cec5SDimitry Andric     {"Altivec/VMX Registers", "vmx", k_num_vmx_registers_powerpc,
740b57cec5SDimitry Andric      g_vmx_regnums},
750b57cec5SDimitry Andric };
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric static_assert(k_first_gpr_powerpc == 0,
780b57cec5SDimitry Andric               "GPRs must index starting at 0, or fix IsGPR()");
IsGPR(unsigned reg)790b57cec5SDimitry Andric bool RegisterContextPOSIX_powerpc::IsGPR(unsigned reg) {
800b57cec5SDimitry Andric   return (reg <= k_last_gpr_powerpc); // GPR's come first.
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
IsFPR(unsigned reg)830b57cec5SDimitry Andric bool RegisterContextPOSIX_powerpc::IsFPR(unsigned reg) {
840b57cec5SDimitry Andric   return (reg >= k_first_fpr) && (reg <= k_last_fpr);
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric 
IsVMX(unsigned reg)870b57cec5SDimitry Andric bool RegisterContextPOSIX_powerpc::IsVMX(unsigned reg) {
880b57cec5SDimitry Andric   return (reg >= k_first_vmx) && (reg <= k_last_vmx);
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric 
RegisterContextPOSIX_powerpc(Thread & thread,uint32_t concrete_frame_idx,RegisterInfoInterface * register_info)910b57cec5SDimitry Andric RegisterContextPOSIX_powerpc::RegisterContextPOSIX_powerpc(
920b57cec5SDimitry Andric     Thread &thread, uint32_t concrete_frame_idx,
930b57cec5SDimitry Andric     RegisterInfoInterface *register_info)
940b57cec5SDimitry Andric     : RegisterContext(thread, concrete_frame_idx) {
950b57cec5SDimitry Andric   m_register_info_up.reset(register_info);
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric 
98*5f7ddb14SDimitry Andric RegisterContextPOSIX_powerpc::~RegisterContextPOSIX_powerpc() = default;
990b57cec5SDimitry Andric 
Invalidate()1000b57cec5SDimitry Andric void RegisterContextPOSIX_powerpc::Invalidate() {}
1010b57cec5SDimitry Andric 
InvalidateAllRegisters()1020b57cec5SDimitry Andric void RegisterContextPOSIX_powerpc::InvalidateAllRegisters() {}
1030b57cec5SDimitry Andric 
GetRegisterOffset(unsigned reg)1040b57cec5SDimitry Andric unsigned RegisterContextPOSIX_powerpc::GetRegisterOffset(unsigned reg) {
1050b57cec5SDimitry Andric   assert(reg < k_num_registers_powerpc && "Invalid register number.");
1060b57cec5SDimitry Andric   return GetRegisterInfo()[reg].byte_offset;
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric 
GetRegisterSize(unsigned reg)1090b57cec5SDimitry Andric unsigned RegisterContextPOSIX_powerpc::GetRegisterSize(unsigned reg) {
1100b57cec5SDimitry Andric   assert(reg < k_num_registers_powerpc && "Invalid register number.");
1110b57cec5SDimitry Andric   return GetRegisterInfo()[reg].byte_size;
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric 
GetRegisterCount()1140b57cec5SDimitry Andric size_t RegisterContextPOSIX_powerpc::GetRegisterCount() {
1150b57cec5SDimitry Andric   size_t num_registers = k_num_registers_powerpc;
1160b57cec5SDimitry Andric   return num_registers;
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric 
GetGPRSize()1190b57cec5SDimitry Andric size_t RegisterContextPOSIX_powerpc::GetGPRSize() {
1200b57cec5SDimitry Andric   return m_register_info_up->GetGPRSize();
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
GetRegisterInfo()1230b57cec5SDimitry Andric const RegisterInfo *RegisterContextPOSIX_powerpc::GetRegisterInfo() {
1240b57cec5SDimitry Andric   // Commonly, this method is overridden and g_register_infos is copied and
1250b57cec5SDimitry Andric   // specialized. So, use GetRegisterInfo() rather than g_register_infos in
1260b57cec5SDimitry Andric   // this scope.
1270b57cec5SDimitry Andric   return m_register_info_up->GetRegisterInfo();
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric const RegisterInfo *
GetRegisterInfoAtIndex(size_t reg)1310b57cec5SDimitry Andric RegisterContextPOSIX_powerpc::GetRegisterInfoAtIndex(size_t reg) {
1320b57cec5SDimitry Andric   if (reg < k_num_registers_powerpc)
1330b57cec5SDimitry Andric     return &GetRegisterInfo()[reg];
1340b57cec5SDimitry Andric   else
1350b57cec5SDimitry Andric     return nullptr;
1360b57cec5SDimitry Andric }
1370b57cec5SDimitry Andric 
GetRegisterSetCount()1380b57cec5SDimitry Andric size_t RegisterContextPOSIX_powerpc::GetRegisterSetCount() {
1390b57cec5SDimitry Andric   size_t sets = 0;
1400b57cec5SDimitry Andric   for (size_t set = 0; set < k_num_register_sets; ++set) {
1410b57cec5SDimitry Andric     if (IsRegisterSetAvailable(set))
1420b57cec5SDimitry Andric       ++sets;
1430b57cec5SDimitry Andric   }
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   return sets;
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric 
GetRegisterSet(size_t set)1480b57cec5SDimitry Andric const RegisterSet *RegisterContextPOSIX_powerpc::GetRegisterSet(size_t set) {
1490b57cec5SDimitry Andric   if (IsRegisterSetAvailable(set))
1500b57cec5SDimitry Andric     return &g_reg_sets_powerpc[set];
1510b57cec5SDimitry Andric   else
1520b57cec5SDimitry Andric     return nullptr;
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric 
GetRegisterName(unsigned reg)1550b57cec5SDimitry Andric const char *RegisterContextPOSIX_powerpc::GetRegisterName(unsigned reg) {
1560b57cec5SDimitry Andric   assert(reg < k_num_registers_powerpc && "Invalid register offset.");
1570b57cec5SDimitry Andric   return GetRegisterInfo()[reg].name;
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric 
IsRegisterSetAvailable(size_t set_index)1600b57cec5SDimitry Andric bool RegisterContextPOSIX_powerpc::IsRegisterSetAvailable(size_t set_index) {
1610b57cec5SDimitry Andric   size_t num_sets = k_num_register_sets;
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   return (set_index < num_sets);
1640b57cec5SDimitry Andric }
165