The Pedigree Project 0.1
DwarfState.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef DWARFSTATE_H
21#define DWARFSTATE_H
22#include "pedigree/kernel/Log.h"
23#include "pedigree/kernel/processor/types.h"
24#include "pedigree/kernel/utilities/utility.h"
25
26#include <config.h>
27
30#define DWARF_MAX_REGISTERS 50
31
32// X86
33#define DWARF_REG_EAX 0
34#define DWARF_REG_ECX 1
35#define DWARF_REG_EDX 2
36#define DWARF_REG_EBX 3
37#define DWARF_REG_ESP 4
38#define DWARF_REG_EBP 5
39#define DWARF_REG_ESI 6
40#define DWARF_REG_EDI 7
41// X64
42#define DWARF_REG_RAX 0
43#define DWARF_REG_RDX 1
44#define DWARF_REG_RCX 2
45#define DWARF_REG_RBX 3
46#define DWARF_REG_RSI 4
47#define DWARF_REG_RDI 5
48#define DWARF_REG_RBP 6
49#define DWARF_REG_RSP 7
50#define DWARF_REG_R8 8
51#define DWARF_REG_R9 9
52#define DWARF_REG_R10 10
53#define DWARF_REG_R11 11
54#define DWARF_REG_R12 12
55#define DWARF_REG_R13 13
56#define DWARF_REG_R14 14
57#define DWARF_REG_R15 15
58#define DWARF_REG_RFLAGS 49
59// Watch out! Register numbering is seemingly random - x86 and x86_64 ones are
60// here: http://wikis.sun.com/display/SunStudio/Dwarf+Register+Numbering
66 public:
67 enum RegisterState {
68 SameValue = 0,
69 Undefined,
70 Offset,
71 ValOffset,
72 Register,
73 Expression,
74 ValExpression,
75 Architectural
76 };
77
79 : m_CfaState(ValOffset),
81 m_CfaOffset(0),
84 ByteSet(static_cast<void*>(m_RegisterStates), 0, sizeof(RegisterState) * DWARF_MAX_REGISTERS);
85 ByteSet(static_cast<void*>(m_R), 0, sizeof(uintptr_t) * DWARF_MAX_REGISTERS);
86 }
87 ~DwarfState() {}
88
92 DwarfState(const DwarfState& other)
93 : m_CfaState(other.m_CfaState),
95 m_CfaOffset(other.m_CfaOffset),
98 MemoryCopy(static_cast<void*>(m_RegisterStates),
99 static_cast<const void*>(other.m_RegisterStates),
100 sizeof(RegisterState) * DWARF_MAX_REGISTERS);
101 MemoryCopy(static_cast<void*>(m_R), static_cast<const void*>(other.m_R),
102 sizeof(uintptr_t) * DWARF_MAX_REGISTERS);
103 }
104
105 DwarfState& operator=(const DwarfState& other) {
106 m_CfaState = other.m_CfaState;
107 m_CfaRegister = other.m_CfaRegister;
108 m_CfaOffset = other.m_CfaOffset;
109 m_CfaExpression = other.m_CfaExpression;
110 m_ReturnAddress = other.m_ReturnAddress;
111 MemoryCopy(static_cast<void*>(m_RegisterStates),
112 static_cast<const void*>(other.m_RegisterStates),
113 sizeof(RegisterState) * DWARF_MAX_REGISTERS);
114 MemoryCopy(static_cast<void*>(m_R), static_cast<const void*>(other.m_R),
115 sizeof(uintptr_t) * DWARF_MAX_REGISTERS);
116 return *this;
117 }
118
119 processor_register_t getCfa(const DwarfState& initialState) const {
120 switch (m_CfaState) {
121 case ValOffset: {
122 return initialState.m_R[m_CfaRegister] + static_cast<ssize_t>(m_CfaOffset);
123 }
124 case ValExpression: {
125 WARNING("DwarfState::getCfa: Expression type not implemented.");
126 break;
127 }
128 default:
129 ERROR("CfaState invalid!");
130 return 0;
131 }
132
133 return 0;
134 }
135
136 processor_register_t getRegister(unsigned int nRegister, const DwarfState& initialState) const {
137 // NOTICE("GetRegister: r" << Dec << nRegister);
138 switch (m_RegisterStates[nRegister]) {
139 case Undefined:
140 // WARNING ("Request for undefined register: r" << Dec
141 // << nRegister);
142 break;
143 case SameValue:
144 // WARNING ("SameValue.");
145 return initialState.m_R[nRegister];
146 case Offset: {
147 // NOTICE("Offset: " << Hex << getCfa(initialState) <<
148 // ", " << m_R[nRegister]);
153 if (getCfa(initialState) < 0x2000) {
154 WARNING("Malformed CFA!");
155 return 0x0;
156 }
157 // "The previous value of this register is saved at the address
158 // CFA+N where CFA is the
159 // current CFA value and N is a signed offset."
160 return *reinterpret_cast<processor_register_t*>(getCfa(initialState) +
161 static_cast<ssize_t>(m_R[nRegister]));
162 }
163 case ValOffset: {
164 // WARNING ("ValOffset.");
165 // "The previous value of this register is the value CFA+N where
166 // CFA is the current
167 // CFA value and N is a signed offset."
168 return getCfa(initialState) + static_cast<ssize_t>(m_R[nRegister]);
169 }
170 case Register: {
171 // WARNING ("Register.");
172 // "The previous value of this register is stored in another
173 // register numbered R."
174 return initialState.m_R[nRegister];
175 }
176 case Expression: {
177 // WARNING ("Expression not implemented, r" << Dec <<
178 // nRegister);
179 return 0;
180 }
181 case ValExpression: {
182 // WARNING ("ValExpression not implemented, r" << Dec
183 // << nRegister);
184 return 0;
185 }
186 case Architectural:
187 // WARNING ("Request for 'architectural' register: r"
188 // << Dec << nRegister);
189 return 0;
190 }
191 return 0;
192 }
193
197 RegisterState m_RegisterStates[DWARF_MAX_REGISTERS];
198
202 processor_register_t m_R[DWARF_MAX_REGISTERS];
203
208 RegisterState m_CfaState;
213 processor_register_t m_CfaOffset;
218
223};
224
227#endif
processor_register_t getRegister(unsigned int nRegister, const DwarfState &initialState) const
Definition DwarfState.h:136
DwarfState(const DwarfState &other)
Definition DwarfState.h:92
processor_register_t m_R[DWARF_MAX_REGISTERS]
Definition DwarfState.h:202
RegisterState m_RegisterStates[DWARF_MAX_REGISTERS]
Definition DwarfState.h:197
RegisterState m_CfaState
Definition DwarfState.h:208
uint32_t m_CfaRegister
Definition DwarfState.h:212
uint8_t * m_CfaExpression
Definition DwarfState.h:217
uintptr_t m_ReturnAddress
Definition DwarfState.h:222