1acac075bSDimitry Andric //===-- RegisterContextPOSIX_ppc64le.cpp -------------------------*- C++-*-===//
2acac075bSDimitry Andric //
3acac075bSDimitry Andric // The LLVM Compiler Infrastructure
4acac075bSDimitry Andric //
5acac075bSDimitry Andric // This file is distributed under the University of Illinois Open Source
6acac075bSDimitry Andric // License. See LICENSE.TXT for details.
7acac075bSDimitry Andric //
8acac075bSDimitry Andric //===----------------------------------------------------------------------===//
9acac075bSDimitry Andric
10acac075bSDimitry Andric #include <cstring>
11acac075bSDimitry Andric #include <errno.h>
12acac075bSDimitry Andric #include <stdint.h>
13acac075bSDimitry Andric
144ba319b5SDimitry Andric #include "lldb/Target/Process.h"
15acac075bSDimitry Andric #include "lldb/Target/Target.h"
16acac075bSDimitry Andric #include "lldb/Target/Thread.h"
17acac075bSDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
18acac075bSDimitry Andric #include "lldb/Utility/DataExtractor.h"
19acac075bSDimitry Andric #include "lldb/Utility/Endian.h"
20*b5893f02SDimitry Andric #include "lldb/Utility/RegisterValue.h"
21*b5893f02SDimitry Andric #include "lldb/Utility/Scalar.h"
22acac075bSDimitry Andric #include "llvm/Support/Compiler.h"
23acac075bSDimitry Andric
24acac075bSDimitry Andric #include "RegisterContextPOSIX_ppc64le.h"
25acac075bSDimitry Andric
26acac075bSDimitry Andric using namespace lldb_private;
27acac075bSDimitry Andric using namespace lldb;
28acac075bSDimitry Andric
29acac075bSDimitry Andric static const uint32_t g_gpr_regnums[] = {
30acac075bSDimitry Andric gpr_r0_ppc64le, gpr_r1_ppc64le, gpr_r2_ppc64le, gpr_r3_ppc64le,
31acac075bSDimitry Andric gpr_r4_ppc64le, gpr_r5_ppc64le, gpr_r6_ppc64le, gpr_r7_ppc64le,
32acac075bSDimitry Andric gpr_r8_ppc64le, gpr_r9_ppc64le, gpr_r10_ppc64le, gpr_r11_ppc64le,
33acac075bSDimitry Andric gpr_r12_ppc64le, gpr_r13_ppc64le, gpr_r14_ppc64le, gpr_r15_ppc64le,
34acac075bSDimitry Andric gpr_r16_ppc64le, gpr_r17_ppc64le, gpr_r18_ppc64le, gpr_r19_ppc64le,
35acac075bSDimitry Andric gpr_r20_ppc64le, gpr_r21_ppc64le, gpr_r22_ppc64le, gpr_r23_ppc64le,
36acac075bSDimitry Andric gpr_r24_ppc64le, gpr_r25_ppc64le, gpr_r26_ppc64le, gpr_r27_ppc64le,
37acac075bSDimitry Andric gpr_r28_ppc64le, gpr_r29_ppc64le, gpr_r30_ppc64le, gpr_r31_ppc64le,
38acac075bSDimitry Andric gpr_pc_ppc64le, gpr_msr_ppc64le, gpr_origr3_ppc64le, gpr_ctr_ppc64le,
39acac075bSDimitry Andric gpr_lr_ppc64le, gpr_xer_ppc64le, gpr_cr_ppc64le, gpr_softe_ppc64le,
40acac075bSDimitry Andric gpr_trap_ppc64le,
41acac075bSDimitry Andric };
42acac075bSDimitry Andric
43acac075bSDimitry Andric static const uint32_t g_fpr_regnums[] = {
44acac075bSDimitry Andric fpr_f0_ppc64le, fpr_f1_ppc64le, fpr_f2_ppc64le, fpr_f3_ppc64le,
45acac075bSDimitry Andric fpr_f4_ppc64le, fpr_f5_ppc64le, fpr_f6_ppc64le, fpr_f7_ppc64le,
46acac075bSDimitry Andric fpr_f8_ppc64le, fpr_f9_ppc64le, fpr_f10_ppc64le, fpr_f11_ppc64le,
47acac075bSDimitry Andric fpr_f12_ppc64le, fpr_f13_ppc64le, fpr_f14_ppc64le, fpr_f15_ppc64le,
48acac075bSDimitry Andric fpr_f16_ppc64le, fpr_f17_ppc64le, fpr_f18_ppc64le, fpr_f19_ppc64le,
49acac075bSDimitry Andric fpr_f20_ppc64le, fpr_f21_ppc64le, fpr_f22_ppc64le, fpr_f23_ppc64le,
50acac075bSDimitry Andric fpr_f24_ppc64le, fpr_f25_ppc64le, fpr_f26_ppc64le, fpr_f27_ppc64le,
51acac075bSDimitry Andric fpr_f28_ppc64le, fpr_f29_ppc64le, fpr_f30_ppc64le, fpr_f31_ppc64le,
52acac075bSDimitry Andric fpr_fpscr_ppc64le,
53acac075bSDimitry Andric };
54acac075bSDimitry Andric
55acac075bSDimitry Andric static const uint32_t g_vmx_regnums[] = {
56acac075bSDimitry Andric vmx_vr0_ppc64le, vmx_vr1_ppc64le, vmx_vr2_ppc64le, vmx_vr3_ppc64le,
57acac075bSDimitry Andric vmx_vr4_ppc64le, vmx_vr5_ppc64le, vmx_vr6_ppc64le, vmx_vr7_ppc64le,
58acac075bSDimitry Andric vmx_vr8_ppc64le, vmx_vr9_ppc64le, vmx_vr10_ppc64le, vmx_vr11_ppc64le,
59acac075bSDimitry Andric vmx_vr12_ppc64le, vmx_vr13_ppc64le, vmx_vr14_ppc64le, vmx_vr15_ppc64le,
60acac075bSDimitry Andric vmx_vr16_ppc64le, vmx_vr17_ppc64le, vmx_vr18_ppc64le, vmx_vr19_ppc64le,
61acac075bSDimitry Andric vmx_vr20_ppc64le, vmx_vr21_ppc64le, vmx_vr22_ppc64le, vmx_vr23_ppc64le,
62acac075bSDimitry Andric vmx_vr24_ppc64le, vmx_vr25_ppc64le, vmx_vr26_ppc64le, vmx_vr27_ppc64le,
63acac075bSDimitry Andric vmx_vr28_ppc64le, vmx_vr29_ppc64le, vmx_vr30_ppc64le, vmx_vr31_ppc64le,
64acac075bSDimitry Andric vmx_vscr_ppc64le, vmx_vrsave_ppc64le,
65acac075bSDimitry Andric };
66acac075bSDimitry Andric
67acac075bSDimitry Andric static const uint32_t g_vsx_regnums[] = {
68acac075bSDimitry Andric vsx_vs0_ppc64le, vsx_vs1_ppc64le, vsx_vs2_ppc64le, vsx_vs3_ppc64le,
69acac075bSDimitry Andric vsx_vs4_ppc64le, vsx_vs5_ppc64le, vsx_vs6_ppc64le, vsx_vs7_ppc64le,
70acac075bSDimitry Andric vsx_vs8_ppc64le, vsx_vs9_ppc64le, vsx_vs10_ppc64le, vsx_vs11_ppc64le,
71acac075bSDimitry Andric vsx_vs12_ppc64le, vsx_vs13_ppc64le, vsx_vs14_ppc64le, vsx_vs15_ppc64le,
72acac075bSDimitry Andric vsx_vs16_ppc64le, vsx_vs17_ppc64le, vsx_vs18_ppc64le, vsx_vs19_ppc64le,
73acac075bSDimitry Andric vsx_vs20_ppc64le, vsx_vs21_ppc64le, vsx_vs22_ppc64le, vsx_vs23_ppc64le,
74acac075bSDimitry Andric vsx_vs24_ppc64le, vsx_vs25_ppc64le, vsx_vs26_ppc64le, vsx_vs27_ppc64le,
75acac075bSDimitry Andric vsx_vs28_ppc64le, vsx_vs29_ppc64le, vsx_vs30_ppc64le, vsx_vs31_ppc64le,
76acac075bSDimitry Andric vsx_vs32_ppc64le, vsx_vs33_ppc64le, vsx_vs34_ppc64le, vsx_vs35_ppc64le,
77acac075bSDimitry Andric vsx_vs36_ppc64le, vsx_vs37_ppc64le, vsx_vs38_ppc64le, vsx_vs39_ppc64le,
78acac075bSDimitry Andric vsx_vs40_ppc64le, vsx_vs41_ppc64le, vsx_vs42_ppc64le, vsx_vs43_ppc64le,
79acac075bSDimitry Andric vsx_vs44_ppc64le, vsx_vs45_ppc64le, vsx_vs46_ppc64le, vsx_vs47_ppc64le,
80acac075bSDimitry Andric vsx_vs48_ppc64le, vsx_vs49_ppc64le, vsx_vs50_ppc64le, vsx_vs51_ppc64le,
81acac075bSDimitry Andric vsx_vs52_ppc64le, vsx_vs53_ppc64le, vsx_vs54_ppc64le, vsx_vs55_ppc64le,
82acac075bSDimitry Andric vsx_vs56_ppc64le, vsx_vs57_ppc64le, vsx_vs58_ppc64le, vsx_vs59_ppc64le,
83acac075bSDimitry Andric vsx_vs60_ppc64le, vsx_vs61_ppc64le, vsx_vs62_ppc64le, vsx_vs63_ppc64le,
84acac075bSDimitry Andric };
85acac075bSDimitry Andric
86acac075bSDimitry Andric // Number of register sets provided by this context.
87acac075bSDimitry Andric enum { k_num_register_sets = 4 };
88acac075bSDimitry Andric
89acac075bSDimitry Andric static const RegisterSet g_reg_sets_ppc64le[k_num_register_sets] = {
90acac075bSDimitry Andric {"General Purpose Registers", "gpr", k_num_gpr_registers_ppc64le,
91acac075bSDimitry Andric g_gpr_regnums},
92acac075bSDimitry Andric {"Floating Point Registers", "fpr", k_num_fpr_registers_ppc64le,
93acac075bSDimitry Andric g_fpr_regnums},
94acac075bSDimitry Andric {"Altivec/VMX Registers", "vmx", k_num_vmx_registers_ppc64le,
95acac075bSDimitry Andric g_vmx_regnums},
96acac075bSDimitry Andric {"VSX Registers", "vsx", k_num_vsx_registers_ppc64le, g_vsx_regnums},
97acac075bSDimitry Andric };
98acac075bSDimitry Andric
IsGPR(unsigned reg)99acac075bSDimitry Andric bool RegisterContextPOSIX_ppc64le::IsGPR(unsigned reg) {
100acac075bSDimitry Andric return (reg <= k_last_gpr_ppc64le); // GPR's come first.
101acac075bSDimitry Andric }
102acac075bSDimitry Andric
IsFPR(unsigned reg)103acac075bSDimitry Andric bool RegisterContextPOSIX_ppc64le::IsFPR(unsigned reg) {
104acac075bSDimitry Andric return (reg >= k_first_fpr_ppc64le) && (reg <= k_last_fpr_ppc64le);
105acac075bSDimitry Andric }
106acac075bSDimitry Andric
IsVMX(unsigned reg)107acac075bSDimitry Andric bool RegisterContextPOSIX_ppc64le::IsVMX(unsigned reg) {
108acac075bSDimitry Andric return (reg >= k_first_vmx_ppc64le) && (reg <= k_last_vmx_ppc64le);
109acac075bSDimitry Andric }
110acac075bSDimitry Andric
IsVSX(unsigned reg)111acac075bSDimitry Andric bool RegisterContextPOSIX_ppc64le::IsVSX(unsigned reg) {
112acac075bSDimitry Andric return (reg >= k_first_vsx_ppc64le) && (reg <= k_last_vsx_ppc64le);
113acac075bSDimitry Andric }
114acac075bSDimitry Andric
RegisterContextPOSIX_ppc64le(Thread & thread,uint32_t concrete_frame_idx,RegisterInfoInterface * register_info)115acac075bSDimitry Andric RegisterContextPOSIX_ppc64le::RegisterContextPOSIX_ppc64le(
116acac075bSDimitry Andric Thread &thread, uint32_t concrete_frame_idx,
117acac075bSDimitry Andric RegisterInfoInterface *register_info)
118acac075bSDimitry Andric : RegisterContext(thread, concrete_frame_idx) {
119acac075bSDimitry Andric m_register_info_ap.reset(register_info);
120acac075bSDimitry Andric }
121acac075bSDimitry Andric
InvalidateAllRegisters()122acac075bSDimitry Andric void RegisterContextPOSIX_ppc64le::InvalidateAllRegisters() {}
123acac075bSDimitry Andric
GetRegisterOffset(unsigned reg)124acac075bSDimitry Andric unsigned RegisterContextPOSIX_ppc64le::GetRegisterOffset(unsigned reg) {
125acac075bSDimitry Andric assert(reg < k_num_registers_ppc64le && "Invalid register number.");
126acac075bSDimitry Andric return GetRegisterInfo()[reg].byte_offset;
127acac075bSDimitry Andric }
128acac075bSDimitry Andric
GetRegisterSize(unsigned reg)129acac075bSDimitry Andric unsigned RegisterContextPOSIX_ppc64le::GetRegisterSize(unsigned reg) {
130acac075bSDimitry Andric assert(reg < k_num_registers_ppc64le && "Invalid register number.");
131acac075bSDimitry Andric return GetRegisterInfo()[reg].byte_size;
132acac075bSDimitry Andric }
133acac075bSDimitry Andric
GetRegisterCount()134acac075bSDimitry Andric size_t RegisterContextPOSIX_ppc64le::GetRegisterCount() {
135acac075bSDimitry Andric size_t num_registers = k_num_registers_ppc64le;
136acac075bSDimitry Andric return num_registers;
137acac075bSDimitry Andric }
138acac075bSDimitry Andric
GetGPRSize()139acac075bSDimitry Andric size_t RegisterContextPOSIX_ppc64le::GetGPRSize() {
140acac075bSDimitry Andric return m_register_info_ap->GetGPRSize();
141acac075bSDimitry Andric }
142acac075bSDimitry Andric
GetRegisterInfo()143acac075bSDimitry Andric const RegisterInfo *RegisterContextPOSIX_ppc64le::GetRegisterInfo() {
144acac075bSDimitry Andric // Commonly, this method is overridden and g_register_infos is copied and
1454ba319b5SDimitry Andric // specialized. So, use GetRegisterInfo() rather than g_register_infos in
1464ba319b5SDimitry Andric // this scope.
147acac075bSDimitry Andric return m_register_info_ap->GetRegisterInfo();
148acac075bSDimitry Andric }
149acac075bSDimitry Andric
150acac075bSDimitry Andric const RegisterInfo *
GetRegisterInfoAtIndex(size_t reg)151acac075bSDimitry Andric RegisterContextPOSIX_ppc64le::GetRegisterInfoAtIndex(size_t reg) {
152acac075bSDimitry Andric if (reg < k_num_registers_ppc64le)
153acac075bSDimitry Andric return &GetRegisterInfo()[reg];
154acac075bSDimitry Andric else
155acac075bSDimitry Andric return NULL;
156acac075bSDimitry Andric }
157acac075bSDimitry Andric
GetRegisterSetCount()158acac075bSDimitry Andric size_t RegisterContextPOSIX_ppc64le::GetRegisterSetCount() {
159acac075bSDimitry Andric size_t sets = 0;
160acac075bSDimitry Andric for (size_t set = 0; set < k_num_register_sets; ++set) {
161acac075bSDimitry Andric if (IsRegisterSetAvailable(set))
162acac075bSDimitry Andric ++sets;
163acac075bSDimitry Andric }
164acac075bSDimitry Andric
165acac075bSDimitry Andric return sets;
166acac075bSDimitry Andric }
167acac075bSDimitry Andric
GetRegisterSet(size_t set)168acac075bSDimitry Andric const RegisterSet *RegisterContextPOSIX_ppc64le::GetRegisterSet(size_t set) {
169acac075bSDimitry Andric if (IsRegisterSetAvailable(set))
170acac075bSDimitry Andric return &g_reg_sets_ppc64le[set];
171acac075bSDimitry Andric else
172acac075bSDimitry Andric return NULL;
173acac075bSDimitry Andric }
174acac075bSDimitry Andric
GetRegisterName(unsigned reg)175acac075bSDimitry Andric const char *RegisterContextPOSIX_ppc64le::GetRegisterName(unsigned reg) {
176acac075bSDimitry Andric assert(reg < k_num_registers_ppc64le && "Invalid register offset.");
177acac075bSDimitry Andric return GetRegisterInfo()[reg].name;
178acac075bSDimitry Andric }
179acac075bSDimitry Andric
GetByteOrder()180acac075bSDimitry Andric lldb::ByteOrder RegisterContextPOSIX_ppc64le::GetByteOrder() {
181acac075bSDimitry Andric // Get the target process whose privileged thread was used for the register
182acac075bSDimitry Andric // read.
183acac075bSDimitry Andric lldb::ByteOrder byte_order = eByteOrderInvalid;
184acac075bSDimitry Andric Process *process = CalculateProcess().get();
185acac075bSDimitry Andric
186acac075bSDimitry Andric if (process)
187acac075bSDimitry Andric byte_order = process->GetByteOrder();
188acac075bSDimitry Andric return byte_order;
189acac075bSDimitry Andric }
190acac075bSDimitry Andric
IsRegisterSetAvailable(size_t set_index)191acac075bSDimitry Andric bool RegisterContextPOSIX_ppc64le::IsRegisterSetAvailable(size_t set_index) {
192acac075bSDimitry Andric size_t num_sets = k_num_register_sets;
193acac075bSDimitry Andric
194acac075bSDimitry Andric return (set_index < num_sets);
195acac075bSDimitry Andric }
196acac075bSDimitry Andric
1974ba319b5SDimitry Andric // Used when parsing DWARF and EH frame information and any other object file
1984ba319b5SDimitry Andric // sections that contain register numbers in them.
ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind,uint32_t num)199acac075bSDimitry Andric uint32_t RegisterContextPOSIX_ppc64le::ConvertRegisterKindToRegisterNumber(
200acac075bSDimitry Andric lldb::RegisterKind kind, uint32_t num) {
201acac075bSDimitry Andric const uint32_t num_regs = GetRegisterCount();
202acac075bSDimitry Andric
203acac075bSDimitry Andric assert(kind < kNumRegisterKinds);
204acac075bSDimitry Andric for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
205acac075bSDimitry Andric const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_idx);
206acac075bSDimitry Andric
207acac075bSDimitry Andric if (reg_info->kinds[kind] == num)
208acac075bSDimitry Andric return reg_idx;
209acac075bSDimitry Andric }
210acac075bSDimitry Andric
211acac075bSDimitry Andric return LLDB_INVALID_REGNUM;
212acac075bSDimitry Andric }
213