1 //===-- RegisterContextLinux_i386.cpp --------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===---------------------------------------------------------------------===//
9 
10 #include "RegisterContextLinux_i386.h"
11 #include "RegisterContextPOSIX_x86.h"
12 
13 using namespace lldb_private;
14 using namespace lldb;
15 
16 struct GPR {
17   uint32_t ebx;
18   uint32_t ecx;
19   uint32_t edx;
20   uint32_t esi;
21   uint32_t edi;
22   uint32_t ebp;
23   uint32_t eax;
24   uint32_t ds;
25   uint32_t es;
26   uint32_t fs;
27   uint32_t gs;
28   uint32_t orig_eax;
29   uint32_t eip;
30   uint32_t cs;
31   uint32_t eflags;
32   uint32_t esp;
33   uint32_t ss;
34 };
35 
36 struct FPR_i386 {
37   uint16_t fctrl;     // FPU Control Word (fcw)
38   uint16_t fstat;     // FPU Status Word (fsw)
39   uint8_t ftag;       // FPU Tag Word (ftw)
40   uint8_t reserved_1; // Reserved
41   uint16_t fop;       // Last Instruction Opcode (fop)
42   union {
43     struct {
44       uint64_t fip; // Instruction Pointer
45       uint64_t fdp; // Data Pointer
46     } x86_64;
47     struct {
48       uint32_t fioff; // FPU IP Offset (fip)
49       uint32_t fiseg; // FPU IP Selector (fcs)
50       uint32_t fooff; // FPU Operand Pointer Offset (foo)
51       uint32_t foseg; // FPU Operand Pointer Selector (fos)
52     } i386_; // Added _ in the end to avoid error with gcc defining i386 in some
53              // cases
54   } ptr;
55   uint32_t mxcsr;     // MXCSR Register State
56   uint32_t mxcsrmask; // MXCSR Mask
57   MMSReg stmm[8];     // 8*16 bytes for each FP-reg = 128 bytes
58   XMMReg xmm[8];      // 8*16 bytes for each XMM-reg = 128 bytes
59   uint32_t padding[56];
60 };
61 
62 struct UserArea {
63   GPR regs;               // General purpose registers.
64   int32_t fpvalid;        // True if FPU is being used.
65   FPR_i386 i387;          // FPU registers.
66   uint32_t tsize;         // Text segment size.
67   uint32_t dsize;         // Data segment size.
68   uint32_t ssize;         // Stack segment size.
69   uint32_t start_code;    // VM address of text.
70   uint32_t start_stack;   // VM address of stack bottom (top in rsp).
71   int32_t signal;         // Signal causing core dump.
72   int32_t reserved;       // Unused.
73   uint32_t ar0;           // Location of GPR's.
74   uint32_t fpstate;       // Location of FPR's. Should be a FXSTATE *, but this
75                           //  has to be 32-bits even on 64-bit systems.
76   uint32_t magic;         // Identifier for core dumps.
77   char u_comm[32];        // Command causing core dump.
78   uint32_t u_debugreg[8]; // Debug registers (DR0 - DR7).
79 };
80 
81 #define DR_SIZE sizeof(((UserArea *)NULL)->u_debugreg[0])
82 #define DR_0_OFFSET 0xFC
83 #define DR_OFFSET(reg_index) (DR_0_OFFSET + (reg_index * 4))
84 #define FPR_SIZE(reg) sizeof(((FPR_i386 *)NULL)->reg)
85 
86 //---------------------------------------------------------------------------
87 // Include RegisterInfos_i386 to declare our g_register_infos_i386 structure.
88 //---------------------------------------------------------------------------
89 #define DECLARE_REGISTER_INFOS_I386_STRUCT
90 #include "RegisterInfos_i386.h"
91 #undef DECLARE_REGISTER_INFOS_I386_STRUCT
92 
93 RegisterContextLinux_i386::RegisterContextLinux_i386(
94     const ArchSpec &target_arch)
95     : RegisterInfoInterface(target_arch) {
96   RegisterInfo orig_ax = {"orig_eax",
97                           NULL,
98                           sizeof(((GPR *)NULL)->orig_eax),
99                           (LLVM_EXTENSION offsetof(GPR, orig_eax)),
100                           eEncodingUint,
101                           eFormatHex,
102                           {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
103                            LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
104                            LLDB_INVALID_REGNUM},
105                           nullptr,
106                           nullptr,
107                           nullptr,
108                           0};
109   d_register_infos.push_back(orig_ax);
110 }
111 
112 size_t RegisterContextLinux_i386::GetGPRSize() const { return sizeof(GPR); }
113 
114 const RegisterInfo *RegisterContextLinux_i386::GetRegisterInfo() const {
115   switch (m_target_arch.GetMachine()) {
116   case llvm::Triple::x86:
117   case llvm::Triple::x86_64:
118     return g_register_infos_i386;
119   default:
120     assert(false && "Unhandled target architecture.");
121     return NULL;
122   }
123 }
124 
125 uint32_t RegisterContextLinux_i386::GetRegisterCount() const {
126   return static_cast<uint32_t>(sizeof(g_register_infos_i386) /
127                                sizeof(g_register_infos_i386[0]));
128 }
129 
130 uint32_t RegisterContextLinux_i386::GetUserRegisterCount() const {
131   return static_cast<uint32_t>(k_num_user_registers_i386);
132 }
133 
134 const std::vector<lldb_private::RegisterInfo> *
135 RegisterContextLinux_i386::GetDynamicRegisterInfoP() const {
136   return &d_register_infos;
137 }
138