-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
ArchitectureX8664.cpp
522 lines (427 loc) · 13.3 KB
/
ArchitectureX8664.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* Copyright 2012, Alex Smith, alex@alex-smith.me.uk.
* Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
* Copyright 2011-2012, Rene Gollent, rene@gollent.com.
* Distributed under the terms of the MIT License.
*/
#include "ArchitectureX8664.h"
#include <new>
#include <String.h>
#include <AutoDeleter.h>
#include "CfaContext.h"
#include "CpuStateX8664.h"
#include "DisassembledCode.h"
#include "FunctionDebugInfo.h"
#include "InstructionInfo.h"
#include "NoOpStackFrameDebugInfo.h"
#include "RegisterMap.h"
#include "StackFrame.h"
#include "Statement.h"
#include "TeamMemory.h"
#include "X86AssemblyLanguage.h"
#include "disasm/DisassemblerX8664.h"
static const int32 kFromDwarfRegisters[] = {
X86_64_REGISTER_RAX,
X86_64_REGISTER_RDX,
X86_64_REGISTER_RCX,
X86_64_REGISTER_RBX,
X86_64_REGISTER_RSI,
X86_64_REGISTER_RDI,
X86_64_REGISTER_RBP,
X86_64_REGISTER_RSP,
X86_64_REGISTER_R8,
X86_64_REGISTER_R9,
X86_64_REGISTER_R10,
X86_64_REGISTER_R11,
X86_64_REGISTER_R12,
X86_64_REGISTER_R13,
X86_64_REGISTER_R14,
X86_64_REGISTER_R15,
X86_64_REGISTER_RIP,
-1, -1, -1, -1, -1, -1, -1, -1, // xmm0-xmm7
-1, -1, -1, -1, -1, -1, -1, -1, // xmm8-xmm15
-1, -1, -1, -1, -1, -1, -1, -1, // st0-st7
-1, -1, -1, -1, -1, -1, -1, -1, // mm0-mm7
-1, // rflags
X86_64_REGISTER_ES,
X86_64_REGISTER_CS,
X86_64_REGISTER_SS,
X86_64_REGISTER_DS,
X86_64_REGISTER_FS,
X86_64_REGISTER_GS,
};
static const int32 kFromDwarfRegisterCount = sizeof(kFromDwarfRegisters) / 4;
// #pragma mark - ToDwarfRegisterMap
struct ArchitectureX8664::ToDwarfRegisterMap : RegisterMap {
ToDwarfRegisterMap()
{
// init the index array from the reverse map
memset(fIndices, -1, sizeof(fIndices));
for (int32 i = 0; i < kFromDwarfRegisterCount; i++) {
if (kFromDwarfRegisters[i] >= 0)
fIndices[kFromDwarfRegisters[i]] = i;
}
}
virtual int32 CountRegisters() const
{
return X86_64_REGISTER_COUNT;
}
virtual int32 MapRegisterIndex(int32 index) const
{
return index >= 0 && index < X86_64_REGISTER_COUNT ? fIndices[index] : -1;
}
private:
int32 fIndices[X86_64_REGISTER_COUNT];
};
// #pragma mark - FromDwarfRegisterMap
struct ArchitectureX8664::FromDwarfRegisterMap : RegisterMap {
virtual int32 CountRegisters() const
{
return kFromDwarfRegisterCount;
}
virtual int32 MapRegisterIndex(int32 index) const
{
return index >= 0 && index < kFromDwarfRegisterCount
? kFromDwarfRegisters[index] : -1;
}
};
// #pragma mark - ArchitectureX8664
ArchitectureX8664::ArchitectureX8664(TeamMemory* teamMemory)
:
Architecture(teamMemory, 8, false),
fAssemblyLanguage(NULL),
fToDwarfRegisterMap(NULL),
fFromDwarfRegisterMap(NULL)
{
}
ArchitectureX8664::~ArchitectureX8664()
{
if (fToDwarfRegisterMap != NULL)
fToDwarfRegisterMap->ReleaseReference();
if (fFromDwarfRegisterMap != NULL)
fFromDwarfRegisterMap->ReleaseReference();
if (fAssemblyLanguage != NULL)
fAssemblyLanguage->ReleaseReference();
}
status_t
ArchitectureX8664::Init()
{
fAssemblyLanguage = new(std::nothrow) X86AssemblyLanguage;
if (fAssemblyLanguage == NULL)
return B_NO_MEMORY;
try {
_AddIntegerRegister(X86_64_REGISTER_RIP, "rip", B_UINT64_TYPE,
REGISTER_TYPE_INSTRUCTION_POINTER, false);
_AddIntegerRegister(X86_64_REGISTER_RSP, "rsp", B_UINT64_TYPE,
REGISTER_TYPE_STACK_POINTER, true);
_AddIntegerRegister(X86_64_REGISTER_RBP, "rbp", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, true);
_AddIntegerRegister(X86_64_REGISTER_RAX, "rax", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, false);
_AddIntegerRegister(X86_64_REGISTER_RBX, "rbx", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, true);
_AddIntegerRegister(X86_64_REGISTER_RCX, "rcx", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, false);
_AddIntegerRegister(X86_64_REGISTER_RDX, "rdx", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, false);
_AddIntegerRegister(X86_64_REGISTER_RSI, "rsi", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, false);
_AddIntegerRegister(X86_64_REGISTER_RDI, "rdi", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, false);
_AddIntegerRegister(X86_64_REGISTER_R8, "r8", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, false);
_AddIntegerRegister(X86_64_REGISTER_R9, "r9", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, false);
_AddIntegerRegister(X86_64_REGISTER_R10, "r10", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, false);
_AddIntegerRegister(X86_64_REGISTER_R11, "r11", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, false);
_AddIntegerRegister(X86_64_REGISTER_R12, "r12", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, true);
_AddIntegerRegister(X86_64_REGISTER_R13, "r13", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, true);
_AddIntegerRegister(X86_64_REGISTER_R14, "r14", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, true);
_AddIntegerRegister(X86_64_REGISTER_R15, "r15", B_UINT64_TYPE,
REGISTER_TYPE_GENERAL_PURPOSE, true);
_AddIntegerRegister(X86_64_REGISTER_CS, "cs", B_UINT16_TYPE,
REGISTER_TYPE_SPECIAL_PURPOSE, true);
_AddIntegerRegister(X86_64_REGISTER_DS, "ds", B_UINT16_TYPE,
REGISTER_TYPE_SPECIAL_PURPOSE, true);
_AddIntegerRegister(X86_64_REGISTER_ES, "es", B_UINT16_TYPE,
REGISTER_TYPE_SPECIAL_PURPOSE, true);
_AddIntegerRegister(X86_64_REGISTER_FS, "fs", B_UINT16_TYPE,
REGISTER_TYPE_SPECIAL_PURPOSE, true);
_AddIntegerRegister(X86_64_REGISTER_GS, "gs", B_UINT16_TYPE,
REGISTER_TYPE_SPECIAL_PURPOSE, true);
_AddIntegerRegister(X86_64_REGISTER_SS, "ss", B_UINT16_TYPE,
REGISTER_TYPE_SPECIAL_PURPOSE, true);
} catch (std::bad_alloc) {
return B_NO_MEMORY;
}
fToDwarfRegisterMap = new(std::nothrow) ToDwarfRegisterMap;
fFromDwarfRegisterMap = new(std::nothrow) FromDwarfRegisterMap;
if (fToDwarfRegisterMap == NULL || fFromDwarfRegisterMap == NULL)
return B_NO_MEMORY;
return B_OK;
}
int32
ArchitectureX8664::StackGrowthDirection() const
{
return STACK_GROWTH_DIRECTION_NEGATIVE;
}
int32
ArchitectureX8664::CountRegisters() const
{
return fRegisters.Count();
}
const Register*
ArchitectureX8664::Registers() const
{
return fRegisters.Elements();
}
status_t
ArchitectureX8664::InitRegisterRules(CfaContext& context) const
{
status_t error = Architecture::InitRegisterRules(context);
if (error != B_OK)
return error;
// set up rule for RIP register
context.RegisterRule(fToDwarfRegisterMap->MapRegisterIndex(
X86_64_REGISTER_RIP))->SetToLocationOffset(0);
return B_OK;
}
status_t
ArchitectureX8664::GetDwarfRegisterMaps(RegisterMap** _toDwarf,
RegisterMap** _fromDwarf) const
{
if (_toDwarf != NULL) {
*_toDwarf = fToDwarfRegisterMap;
fToDwarfRegisterMap->AcquireReference();
}
if (_fromDwarf != NULL) {
*_fromDwarf = fFromDwarfRegisterMap;
fFromDwarfRegisterMap->AcquireReference();
}
return B_OK;
}
status_t
ArchitectureX8664::CreateCpuState(CpuState*& _state)
{
CpuStateX8664* state = new(std::nothrow) CpuStateX8664;
if (state == NULL)
return B_NO_MEMORY;
_state = state;
return B_OK;
}
status_t
ArchitectureX8664::CreateCpuState(const void* cpuStateData, size_t size,
CpuState*& _state)
{
if (size != sizeof(x86_64_debug_cpu_state))
return B_BAD_VALUE;
CpuStateX8664* state = new(std::nothrow) CpuStateX8664(
*(const x86_64_debug_cpu_state*)cpuStateData);
if (state == NULL)
return B_NO_MEMORY;
_state = state;
return B_OK;
}
status_t
ArchitectureX8664::CreateStackFrame(Image* image, FunctionDebugInfo* function,
CpuState* _cpuState, bool isTopFrame, StackFrame*& _frame,
CpuState*& _previousCpuState)
{
fprintf(stderr, "ArchitectureX8664::CreateStackFrame: TODO\n");
return B_UNSUPPORTED;
}
void
ArchitectureX8664::UpdateStackFrameCpuState(const StackFrame* frame,
Image* previousImage, FunctionDebugInfo* previousFunction,
CpuState* previousCpuState)
{
fprintf(stderr, "ArchitectureX8664::UpdateStackFrameCpuState: TODO\n");
}
status_t
ArchitectureX8664::ReadValueFromMemory(target_addr_t address, uint32 valueType,
BVariant& _value) const
{
uint8 buffer[64];
size_t size = BVariant::SizeOfType(valueType);
if (size == 0 || size > sizeof(buffer))
return B_BAD_VALUE;
ssize_t bytesRead = fTeamMemory->ReadMemory(address, buffer, size);
if (bytesRead < 0)
return bytesRead;
if ((size_t)bytesRead != size)
return B_ERROR;
// TODO: We need to swap endianess, if the host is big endian!
switch (valueType) {
case B_INT8_TYPE:
_value.SetTo(*(int8*)buffer);
return B_OK;
case B_UINT8_TYPE:
_value.SetTo(*(uint8*)buffer);
return B_OK;
case B_INT16_TYPE:
_value.SetTo(*(int16*)buffer);
return B_OK;
case B_UINT16_TYPE:
_value.SetTo(*(uint16*)buffer);
return B_OK;
case B_INT32_TYPE:
_value.SetTo(*(int32*)buffer);
return B_OK;
case B_UINT32_TYPE:
_value.SetTo(*(uint32*)buffer);
return B_OK;
case B_INT64_TYPE:
_value.SetTo(*(int64*)buffer);
return B_OK;
case B_UINT64_TYPE:
_value.SetTo(*(uint64*)buffer);
return B_OK;
case B_FLOAT_TYPE:
_value.SetTo(*(float*)buffer);
// TODO: float on the host might work differently!
return B_OK;
case B_DOUBLE_TYPE:
_value.SetTo(*(double*)buffer);
// TODO: double on the host might work differently!
return B_OK;
default:
return B_BAD_VALUE;
}
}
status_t
ArchitectureX8664::ReadValueFromMemory(target_addr_t addressSpace,
target_addr_t address, uint32 valueType, BVariant& _value) const
{
// n/a on this architecture
return B_BAD_VALUE;
}
status_t
ArchitectureX8664::DisassembleCode(FunctionDebugInfo* function,
const void* buffer, size_t bufferSize, DisassembledCode*& _sourceCode)
{
DisassembledCode* source = new(std::nothrow) DisassembledCode(
fAssemblyLanguage);
if (source == NULL)
return B_NO_MEMORY;
BReference<DisassembledCode> sourceReference(source, true);
// init disassembler
DisassemblerX8664 disassembler;
status_t error = disassembler.Init(function->Address(), buffer, bufferSize);
if (error != B_OK)
return error;
// add a function name line
BString functionName(function->PrettyName());
if (!source->AddCommentLine((functionName << ':').String()))
return B_NO_MEMORY;
// disassemble the instructions
BString line;
target_addr_t instructionAddress;
target_size_t instructionSize;
bool breakpointAllowed;
while (disassembler.GetNextInstruction(line, instructionAddress,
instructionSize, breakpointAllowed) == B_OK) {
// TODO: Respect breakpointAllowed!
if (!source->AddInstructionLine(line, instructionAddress,
instructionSize)) {
return B_NO_MEMORY;
}
}
_sourceCode = sourceReference.Detach();
return B_OK;
}
status_t
ArchitectureX8664::GetStatement(FunctionDebugInfo* function,
target_addr_t address, Statement*& _statement)
{
// TODO: This is not architecture dependent anymore!
// get the instruction info
InstructionInfo info;
status_t error = GetInstructionInfo(address, info);
if (error != B_OK)
return error;
// create a statement
ContiguousStatement* statement = new(std::nothrow) ContiguousStatement(
SourceLocation(-1), TargetAddressRange(info.Address(), info.Size()));
if (statement == NULL)
return B_NO_MEMORY;
_statement = statement;
return B_OK;
}
status_t
ArchitectureX8664::GetInstructionInfo(target_addr_t address,
InstructionInfo& _info)
{
// read the code
uint8 buffer[16];
// TODO: What's the maximum instruction size?
ssize_t bytesRead = fTeamMemory->ReadMemory(address, buffer,
sizeof(buffer));
if (bytesRead < 0)
return bytesRead;
// init disassembler
DisassemblerX8664 disassembler;
status_t error = disassembler.Init(address, buffer, bytesRead);
if (error != B_OK)
return error;
// disassemble the instruction
BString line;
target_addr_t instructionAddress;
target_size_t instructionSize;
bool breakpointAllowed;
error = disassembler.GetNextInstruction(line, instructionAddress,
instructionSize, breakpointAllowed);
if (error != B_OK)
return error;
// FIXME: Is this correct for x86_64? I'm not entirely sure.
instruction_type instructionType = INSTRUCTION_TYPE_OTHER;
if (buffer[0] == 0xff && (buffer[1] & 0x34) == 0x10) {
// absolute call with r/m32
instructionType = INSTRUCTION_TYPE_SUBROUTINE_CALL;
} else if (buffer[0] == 0xe8 && instructionSize == 5) {
// relative call with rel32 -- don't categorize the call with 0 as
// subroutine call, since it is only used to get the address of the GOT
if (buffer[1] != 0 || buffer[2] != 0 || buffer[3] != 0
|| buffer[4] != 0) {
instructionType = INSTRUCTION_TYPE_SUBROUTINE_CALL;
}
}
if (!_info.SetTo(instructionAddress, instructionSize, instructionType,
breakpointAllowed, line)) {
return B_NO_MEMORY;
}
return B_OK;
}
status_t
ArchitectureX8664::GetWatchpointDebugCapabilities(int32& _maxRegisterCount,
int32& _maxBytesPerRegister, uint8& _watchpointCapabilityFlags)
{
// Have 4 debug registers, 1 is required for breakpoint support, which
// leaves 3 available for watchpoints.
_maxRegisterCount = 3;
_maxBytesPerRegister = 8;
// x86 only supports write and read/write watchpoints.
_watchpointCapabilityFlags = WATCHPOINT_CAPABILITY_FLAG_WRITE
| WATCHPOINT_CAPABILITY_FLAG_READ_WRITE;
return B_OK;
}
void
ArchitectureX8664::_AddRegister(int32 index, const char* name,
uint32 bitSize, uint32 valueType, register_type type, bool calleePreserved)
{
if (!fRegisters.Add(Register(index, name, bitSize, valueType, type,
calleePreserved))) {
throw std::bad_alloc();
}
}
void
ArchitectureX8664::_AddIntegerRegister(int32 index, const char* name,
uint32 valueType, register_type type, bool calleePreserved)
{
_AddRegister(index, name, 8 * BVariant::SizeOfType(valueType), valueType,
type, calleePreserved);
}