1*0b57cec5SDimitry Andric //===-- RegisterContextPOSIX_powerpc.cpp ----------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include <cerrno>
10*0b57cec5SDimitry Andric #include <cstdint>
11*0b57cec5SDimitry Andric #include <cstring>
12*0b57cec5SDimitry Andric
13*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
14*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
15*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
16*0b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
17*0b57cec5SDimitry Andric #include "lldb/Utility/DataExtractor.h"
18*0b57cec5SDimitry Andric #include "lldb/Utility/Endian.h"
19*0b57cec5SDimitry Andric #include "lldb/Utility/RegisterValue.h"
20*0b57cec5SDimitry Andric #include "lldb/Utility/Scalar.h"
21*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
22*0b57cec5SDimitry Andric
23*0b57cec5SDimitry Andric #include "RegisterContextPOSIX_powerpc.h"
24*0b57cec5SDimitry Andric
25*0b57cec5SDimitry Andric using namespace lldb_private;
26*0b57cec5SDimitry Andric using namespace lldb;
27*0b57cec5SDimitry Andric
28*0b57cec5SDimitry Andric static const uint32_t g_gpr_regnums[] = {
29*0b57cec5SDimitry Andric gpr_r0_powerpc, gpr_r1_powerpc, gpr_r2_powerpc, gpr_r3_powerpc,
30*0b57cec5SDimitry Andric gpr_r4_powerpc, gpr_r5_powerpc, gpr_r6_powerpc, gpr_r7_powerpc,
31*0b57cec5SDimitry Andric gpr_r8_powerpc, gpr_r9_powerpc, gpr_r10_powerpc, gpr_r11_powerpc,
32*0b57cec5SDimitry Andric gpr_r12_powerpc, gpr_r13_powerpc, gpr_r14_powerpc, gpr_r15_powerpc,
33*0b57cec5SDimitry Andric gpr_r16_powerpc, gpr_r17_powerpc, gpr_r18_powerpc, gpr_r19_powerpc,
34*0b57cec5SDimitry Andric gpr_r20_powerpc, gpr_r21_powerpc, gpr_r22_powerpc, gpr_r23_powerpc,
35*0b57cec5SDimitry Andric gpr_r24_powerpc, gpr_r25_powerpc, gpr_r26_powerpc, gpr_r27_powerpc,
36*0b57cec5SDimitry Andric gpr_r28_powerpc, gpr_r29_powerpc, gpr_r30_powerpc, gpr_r31_powerpc,
37*0b57cec5SDimitry Andric gpr_lr_powerpc, gpr_cr_powerpc, gpr_xer_powerpc, gpr_ctr_powerpc,
38*0b57cec5SDimitry Andric gpr_pc_powerpc,
39*0b57cec5SDimitry Andric };
40*0b57cec5SDimitry Andric
41*0b57cec5SDimitry Andric static const uint32_t g_fpr_regnums[] = {
42*0b57cec5SDimitry Andric fpr_f0_powerpc, fpr_f1_powerpc, fpr_f2_powerpc, fpr_f3_powerpc,
43*0b57cec5SDimitry Andric fpr_f4_powerpc, fpr_f5_powerpc, fpr_f6_powerpc, fpr_f7_powerpc,
44*0b57cec5SDimitry Andric fpr_f8_powerpc, fpr_f9_powerpc, fpr_f10_powerpc, fpr_f11_powerpc,
45*0b57cec5SDimitry Andric fpr_f12_powerpc, fpr_f13_powerpc, fpr_f14_powerpc, fpr_f15_powerpc,
46*0b57cec5SDimitry Andric fpr_f16_powerpc, fpr_f17_powerpc, fpr_f18_powerpc, fpr_f19_powerpc,
47*0b57cec5SDimitry Andric fpr_f20_powerpc, fpr_f21_powerpc, fpr_f22_powerpc, fpr_f23_powerpc,
48*0b57cec5SDimitry Andric fpr_f24_powerpc, fpr_f25_powerpc, fpr_f26_powerpc, fpr_f27_powerpc,
49*0b57cec5SDimitry Andric fpr_f28_powerpc, fpr_f29_powerpc, fpr_f30_powerpc, fpr_f31_powerpc,
50*0b57cec5SDimitry Andric fpr_fpscr_powerpc,
51*0b57cec5SDimitry Andric };
52*0b57cec5SDimitry Andric
53*0b57cec5SDimitry Andric static const uint32_t g_vmx_regnums[] = {
54*0b57cec5SDimitry Andric vmx_v0_powerpc, vmx_v1_powerpc, vmx_v2_powerpc, vmx_v3_powerpc,
55*0b57cec5SDimitry Andric vmx_v4_powerpc, vmx_v5_powerpc, vmx_v6_powerpc, vmx_v7_powerpc,
56*0b57cec5SDimitry Andric vmx_v8_powerpc, vmx_v9_powerpc, vmx_v10_powerpc, vmx_v11_powerpc,
57*0b57cec5SDimitry Andric vmx_v12_powerpc, vmx_v13_powerpc, vmx_v14_powerpc, vmx_v15_powerpc,
58*0b57cec5SDimitry Andric vmx_v16_powerpc, vmx_v17_powerpc, vmx_v18_powerpc, vmx_v19_powerpc,
59*0b57cec5SDimitry Andric vmx_v20_powerpc, vmx_v21_powerpc, vmx_v22_powerpc, vmx_v23_powerpc,
60*0b57cec5SDimitry Andric vmx_v24_powerpc, vmx_v25_powerpc, vmx_v26_powerpc, vmx_v27_powerpc,
61*0b57cec5SDimitry Andric vmx_v28_powerpc, vmx_v29_powerpc, vmx_v30_powerpc, vmx_v31_powerpc,
62*0b57cec5SDimitry Andric vmx_vrsave_powerpc, vmx_vscr_powerpc,
63*0b57cec5SDimitry Andric };
64*0b57cec5SDimitry Andric
65*0b57cec5SDimitry Andric // Number of register sets provided by this context.
66*0b57cec5SDimitry Andric enum { k_num_register_sets = 3 };
67*0b57cec5SDimitry Andric
68*0b57cec5SDimitry Andric static const RegisterSet g_reg_sets_powerpc[k_num_register_sets] = {
69*0b57cec5SDimitry Andric {"General Purpose Registers", "gpr", k_num_gpr_registers_powerpc,
70*0b57cec5SDimitry Andric g_gpr_regnums},
71*0b57cec5SDimitry Andric {"Floating Point Registers", "fpr", k_num_fpr_registers_powerpc,
72*0b57cec5SDimitry Andric g_fpr_regnums},
73*0b57cec5SDimitry Andric {"Altivec/VMX Registers", "vmx", k_num_vmx_registers_powerpc,
74*0b57cec5SDimitry Andric g_vmx_regnums},
75*0b57cec5SDimitry Andric };
76*0b57cec5SDimitry Andric
77*0b57cec5SDimitry Andric static_assert(k_first_gpr_powerpc == 0,
78*0b57cec5SDimitry Andric "GPRs must index starting at 0, or fix IsGPR()");
IsGPR(unsigned reg)79*0b57cec5SDimitry Andric bool RegisterContextPOSIX_powerpc::IsGPR(unsigned reg) {
80*0b57cec5SDimitry Andric return (reg <= k_last_gpr_powerpc); // GPR's come first.
81*0b57cec5SDimitry Andric }
82*0b57cec5SDimitry Andric
IsFPR(unsigned reg)83*0b57cec5SDimitry Andric bool RegisterContextPOSIX_powerpc::IsFPR(unsigned reg) {
84*0b57cec5SDimitry Andric return (reg >= k_first_fpr) && (reg <= k_last_fpr);
85*0b57cec5SDimitry Andric }
86*0b57cec5SDimitry Andric
IsVMX(unsigned reg)87*0b57cec5SDimitry Andric bool RegisterContextPOSIX_powerpc::IsVMX(unsigned reg) {
88*0b57cec5SDimitry Andric return (reg >= k_first_vmx) && (reg <= k_last_vmx);
89*0b57cec5SDimitry Andric }
90*0b57cec5SDimitry Andric
RegisterContextPOSIX_powerpc(Thread & thread,uint32_t concrete_frame_idx,RegisterInfoInterface * register_info)91*0b57cec5SDimitry Andric RegisterContextPOSIX_powerpc::RegisterContextPOSIX_powerpc(
92*0b57cec5SDimitry Andric Thread &thread, uint32_t concrete_frame_idx,
93*0b57cec5SDimitry Andric RegisterInfoInterface *register_info)
94*0b57cec5SDimitry Andric : RegisterContext(thread, concrete_frame_idx) {
95*0b57cec5SDimitry Andric m_register_info_up.reset(register_info);
96*0b57cec5SDimitry Andric }
97*0b57cec5SDimitry Andric
98*0b57cec5SDimitry Andric RegisterContextPOSIX_powerpc::~RegisterContextPOSIX_powerpc() = default;
99*0b57cec5SDimitry Andric
Invalidate()100*0b57cec5SDimitry Andric void RegisterContextPOSIX_powerpc::Invalidate() {}
101*0b57cec5SDimitry Andric
InvalidateAllRegisters()102*0b57cec5SDimitry Andric void RegisterContextPOSIX_powerpc::InvalidateAllRegisters() {}
103*0b57cec5SDimitry Andric
GetRegisterOffset(unsigned reg)104*0b57cec5SDimitry Andric unsigned RegisterContextPOSIX_powerpc::GetRegisterOffset(unsigned reg) {
105*0b57cec5SDimitry Andric assert(reg < k_num_registers_powerpc && "Invalid register number.");
106*0b57cec5SDimitry Andric return GetRegisterInfo()[reg].byte_offset;
107*0b57cec5SDimitry Andric }
108*0b57cec5SDimitry Andric
GetRegisterSize(unsigned reg)109*0b57cec5SDimitry Andric unsigned RegisterContextPOSIX_powerpc::GetRegisterSize(unsigned reg) {
110*0b57cec5SDimitry Andric assert(reg < k_num_registers_powerpc && "Invalid register number.");
111*0b57cec5SDimitry Andric return GetRegisterInfo()[reg].byte_size;
112*0b57cec5SDimitry Andric }
113*0b57cec5SDimitry Andric
GetRegisterCount()114*0b57cec5SDimitry Andric size_t RegisterContextPOSIX_powerpc::GetRegisterCount() {
115*0b57cec5SDimitry Andric size_t num_registers = k_num_registers_powerpc;
116*0b57cec5SDimitry Andric return num_registers;
117*0b57cec5SDimitry Andric }
118*0b57cec5SDimitry Andric
GetGPRSize()119*0b57cec5SDimitry Andric size_t RegisterContextPOSIX_powerpc::GetGPRSize() {
120*0b57cec5SDimitry Andric return m_register_info_up->GetGPRSize();
121*0b57cec5SDimitry Andric }
122*0b57cec5SDimitry Andric
GetRegisterInfo()123*0b57cec5SDimitry Andric const RegisterInfo *RegisterContextPOSIX_powerpc::GetRegisterInfo() {
124*0b57cec5SDimitry Andric // Commonly, this method is overridden and g_register_infos is copied and
125*0b57cec5SDimitry Andric // specialized. So, use GetRegisterInfo() rather than g_register_infos in
126*0b57cec5SDimitry Andric // this scope.
127*0b57cec5SDimitry Andric return m_register_info_up->GetRegisterInfo();
128*0b57cec5SDimitry Andric }
129*0b57cec5SDimitry Andric
130*0b57cec5SDimitry Andric const RegisterInfo *
GetRegisterInfoAtIndex(size_t reg)131*0b57cec5SDimitry Andric RegisterContextPOSIX_powerpc::GetRegisterInfoAtIndex(size_t reg) {
132*0b57cec5SDimitry Andric if (reg < k_num_registers_powerpc)
133*0b57cec5SDimitry Andric return &GetRegisterInfo()[reg];
134*0b57cec5SDimitry Andric else
135*0b57cec5SDimitry Andric return nullptr;
136*0b57cec5SDimitry Andric }
137*0b57cec5SDimitry Andric
GetRegisterSetCount()138*0b57cec5SDimitry Andric size_t RegisterContextPOSIX_powerpc::GetRegisterSetCount() {
139*0b57cec5SDimitry Andric size_t sets = 0;
140*0b57cec5SDimitry Andric for (size_t set = 0; set < k_num_register_sets; ++set) {
141*0b57cec5SDimitry Andric if (IsRegisterSetAvailable(set))
142*0b57cec5SDimitry Andric ++sets;
143*0b57cec5SDimitry Andric }
144*0b57cec5SDimitry Andric
145*0b57cec5SDimitry Andric return sets;
146*0b57cec5SDimitry Andric }
147*0b57cec5SDimitry Andric
GetRegisterSet(size_t set)148*0b57cec5SDimitry Andric const RegisterSet *RegisterContextPOSIX_powerpc::GetRegisterSet(size_t set) {
149*0b57cec5SDimitry Andric if (IsRegisterSetAvailable(set))
150*0b57cec5SDimitry Andric return &g_reg_sets_powerpc[set];
151*0b57cec5SDimitry Andric else
152*0b57cec5SDimitry Andric return nullptr;
153*0b57cec5SDimitry Andric }
154*0b57cec5SDimitry Andric
GetRegisterName(unsigned reg)155*0b57cec5SDimitry Andric const char *RegisterContextPOSIX_powerpc::GetRegisterName(unsigned reg) {
156*0b57cec5SDimitry Andric assert(reg < k_num_registers_powerpc && "Invalid register offset.");
157*0b57cec5SDimitry Andric return GetRegisterInfo()[reg].name;
158*0b57cec5SDimitry Andric }
159*0b57cec5SDimitry Andric
IsRegisterSetAvailable(size_t set_index)160*0b57cec5SDimitry Andric bool RegisterContextPOSIX_powerpc::IsRegisterSetAvailable(size_t set_index) {
161*0b57cec5SDimitry Andric size_t num_sets = k_num_register_sets;
162*0b57cec5SDimitry Andric
163*0b57cec5SDimitry Andric return (set_index < num_sets);
164*0b57cec5SDimitry Andric }
165*0b57cec5SDimitry Andric