forked from sysprogs/msp430-gdbproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSP430EEMTarget.cpp
498 lines (415 loc) · 14.8 KB
/
MSP430EEMTarget.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
#include "stdafx.h"
#include "MSP430EEMTarget.h"
#include "TI/Inc/MSP430_EEM.h"
#include "SoftwareBreakpointManager.h"
#define REPORT_AND_RETURN(msg, result) { ReportLastMSP430Error(msg); return result; }
using namespace GDBServerFoundation;
using namespace MSP430Proxy;
//Breakpoint cookie format:
//32 bits: [type : 4] [reserved : 12] [user_data : 16]
//For RAM breakpoints user_data contains the original insn
//For hardware breakpoints user_data contains the handle returned by EEM API
enum {kBpCookieTypeMask = 0xF0000000,
kBpCookieTypeHardware = 0x10000000,
kBpCookieTypeSoftwareFLASH = 0x20000000,
kBpCookieTypeSoftwareRAM = 0x30000000,
kBpCookieDataMask = 0x0000FFFF};
#define MAKE_BP_COOKIE(type, data) (((type) & kBpCookieTypeMask) | ((data) & kBpCookieDataMask))
enum MSP430_MSG
{
WMX_SINGLESTEP = WM_USER + 1,
WMX_BREKAPOINT,
WMX_STORAGE,
WMX_STATE,
WMX_WARNING,
WMX_STOPPED,
};
bool MSP430Proxy::MSP430EEMTarget::Initialize(const GlobalSettings &settings)
{
if (!__super::Initialize(settings))
return false;
if (m_pBreakpointManager)
return false;
m_BreakpointPolicy = settings.SoftBreakPolicy;
m_BreakpointInstruction = settings.BreakpointInstruction;
MESSAGE_ID msgs = {0,};
msgs.uiMsgIdSingleStep = WMX_SINGLESTEP;
msgs.uiMsgIdBreakpoint = WMX_BREKAPOINT;
msgs.uiMsgIdStorage = WMX_STORAGE;
msgs.uiMsgIdState = WMX_STATE;
msgs.uiMsgIdWarning = WMX_WARNING;
msgs.uiMsgIdCPUStopped = WMX_STOPPED;
C_ASSERT(sizeof(LONG) == sizeof(this));
if (MSP430_EEM_Init(sEEMHandler, (LONG)this, &msgs) != STATUS_OK)
REPORT_AND_RETURN("Cannot enter EEM mode", false);
m_bEEMInitialized = true;
if (m_BreakpointPolicy != HardwareOnly)
{
BpParameter_t bkpt;
memset(&bkpt, 0, sizeof(bkpt));
bkpt.bpMode = BP_COMPLEX;
bkpt.lAddrVal = settings.BreakpointInstruction;
bkpt.bpType = BP_MDB;
bkpt.bpAccess = BP_FETCH;
bkpt.bpAction = BP_BRK;
bkpt.bpOperat = BP_EQUAL;
bkpt.bpCondition = BP_NO_COND;
bkpt.lMask = 0xffff;
if (MSP430_EEM_SetBreakpoint(&m_SoftwareBreakpointWrapperHandle, &bkpt) != STATUS_OK)
REPORT_AND_RETURN("Cannot create a MDB breakpoint for handling software breakpoints", false);
if (m_bVerbose)
printf("Registered software breakpoint support. Breakpoint instruction: 0x%x; meta-breakpoint handle: %d\n", m_BreakpointInstruction, m_SoftwareBreakpointWrapperHandle);
m_HardwareBreakpointsUsed++;
}
else
{
m_SoftwareBreakpointWrapperHandle = -1;
if (m_bVerbose)
printf("Warning: Software breakpoints disabled by configuration\n");
}
m_pBreakpointManager = new SoftwareBreakpointManager(m_DeviceInfo.mainStart, m_DeviceInfo.mainEnd, settings.BreakpointInstruction, settings.InstantBreakpointCleanup, settings.Verbose);
return true;
}
MSP430Proxy::MSP430EEMTarget::~MSP430EEMTarget()
{
delete m_pBreakpointManager;
if (m_SoftwareBreakpointWrapperHandle != -1)
{
BpParameter_t bkpt;
memset(&bkpt, 0, sizeof(bkpt));
bkpt.bpMode = BP_CLEAR;
STATUS_T status = MSP430_EEM_SetBreakpoint(&m_SoftwareBreakpointWrapperHandle, &bkpt);
if (m_bVerbose)
printf("Unregistering software breakpoint support => %d, bpHandle = %d\n", status, m_SoftwareBreakpointWrapperHandle);
}
}
void MSP430Proxy::MSP430EEMTarget::EEMNotificationHandler( MSP430_MSG wMsg, WPARAM wParam, LPARAM lParam )
{
if (m_bVerbose)
printf("EEM notification: 0x%x\n", wMsg);
switch(wMsg)
{
case WMX_BREKAPOINT:
case WMX_SINGLESTEP:
case WMX_STOPPED:
if (m_bVerbose)
printf("Target stop detected\n");
m_LastStopEvent = wMsg;
m_TargetStopped.Set();
break;
}
}
void MSP430Proxy::MSP430EEMTarget::sEEMHandler( UINT MsgId, UINT wParam, LONG lParam, LONG clientHandle )
{
C_ASSERT(sizeof(clientHandle) == sizeof(MSP430EEMTarget *));
((MSP430EEMTarget *)clientHandle)->EEMNotificationHandler((MSP430_MSG)MsgId, wParam, lParam);
}
void MSP430Proxy::MSP430EEMTarget::DoSendBreakInRequest()
{
LONG state = 0;
LONG cpuCycles = 0;
STATUS_T status = MSP430_State(&state, TRUE, &cpuCycles);
if (m_bVerbose)
printf("Break-in request: MSP430_State() => %d, state = %d, CPU cycles = %d\n", status, state, cpuCycles);
if (status != STATUS_OK)
ReportLastMSP430Error("Cannot stop device");
}
bool MSP430Proxy::MSP430EEMTarget::WaitForJTAGEvent()
{
for (;;)
{
if (m_bVerbose)
printf("Waiting for the target to stop (EEM event will be generated)...\n");
for (;;)
{
HANDLE events[] = {m_TargetStopped.GetHandle(), m_BreakInSemaphore.GetHandle()};
DWORD waitResult = WaitForMultipleObjects(2, events, FALSE, INFINITE);
if (waitResult == WAIT_OBJECT_0)
break; //Target stopped
else if (waitResult == (WAIT_OBJECT_0 + 1))
{
if (m_bVerbose)
printf("Handling break-in request from main worker thread...\n");
DoSendBreakInRequest();
//We have requested a break-in. Let's give the target some time to react.
for (;;)
{
if (WaitForSingleObject(m_TargetStopped.GetHandle(), 100) == WAIT_OBJECT_0)
{
if (m_bVerbose)
printf("Break-in handled normally. Target stop reported.\n");
break; //Done
}
LONG state = 0, cpuCycles = 0;
STATUS_T status = MSP430_State(&state, FALSE, &cpuCycles);
if (m_bVerbose)
printf("Post-break-in check: MSP430_State() => %d, state = %d, CPU cycles = %d\n", status, state, cpuCycles);
if (state == STOPPED)
{
if (m_bVerbose)
printf("Stop event not reported, but the CPU state is STOPPED. Exiting wait loop...\n");
break;
}
if (m_bVerbose)
printf("CPU state is not STOPPED. Continuing...\n");
}
break;
}
else
{
if (m_bVerbose)
printf("Unexpected wait result: 0x%x\n", waitResult);
break;
}
}
LONG regPC = 0;
if (MSP430_Read_Register(®PC, PC) != STATUS_OK)
REPORT_AND_RETURN("Cannot read PC register", false);
if (m_bVerbose)
printf("Target stopped, PC = 0x%x\n", regPC);
SoftwareBreakpointManager::BreakpointState bpState = m_pBreakpointManager->GetBreakpointState(regPC - 2);
if (m_RAMBreakpoints.IsBreakpointPresent((USHORT)regPC - 2))
bpState = SoftwareBreakpointManager::BreakpointActive;
switch(bpState)
{
case SoftwareBreakpointManager::BreakpointActive:
case SoftwareBreakpointManager::BreakpointInactive:
if (m_bVerbose)
printf("Found a software breakpoint at PC = 0x%X\n", regPC - 2);
regPC -= 2;
if (regPC == m_BreakpointAddrOfLastResumeOp)
{
//We have just continued from the breakpoint event by patching the original instruction into the instruction fetcher.
//We don't want to stop indefinitely here.
if (m_LastResumeMode != SINGLE_STEP)
{
if (m_bVerbose)
printf("Resuming execution after a software breakpoint\n");
if (!DoResumeTarget(m_LastResumeMode))
return false;
continue;
}
return true;
}
if (MSP430_Write_Register(®PC, PC) != STATUS_OK)
REPORT_AND_RETURN("Cannot read PC register", false);
if (bpState == SoftwareBreakpointManager::BreakpointInactive && m_LastResumeMode != SINGLE_STEP && !m_BreakInSemaphore.TryWait())
{
if (m_bVerbose)
printf("Breakpoint at PC = 0x%X is inactive. Skipping...\n", regPC);
//Skip the breakpoint
if (!DoResumeTarget(m_LastResumeMode))
return false;
continue;
}
return true;
case SoftwareBreakpointManager::NoBreakpoint:
default:
return true; //The stop is not related to a software breakpoint
}
}
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430EEMTarget::CreateBreakpoint( BreakpointType type, ULONGLONG Address, unsigned kind, OUT INT_PTR *pCookie )
{
switch(type)
{
case bptSoftwareBreakpoint:
switch(m_BreakpointPolicy)
{
case HardwareOnly:
return DoCreateCodeBreakpoint(true, Address, pCookie);
case HardwareThenSoftware:
return DoCreateCodeBreakpoint((m_HardwareBreakpointsUsed < m_DeviceInfo.nBreakpoints), Address, pCookie);
case SoftwareOnly:
default:
return DoCreateCodeBreakpoint(false, Address, pCookie);
}
case bptHardwareBreakpoint:
if (m_BreakpointPolicy == HardwareThenSoftware && m_bFLASHCommandsUsed)
return DoCreateCodeBreakpoint((m_HardwareBreakpointsUsed < m_DeviceInfo.nBreakpoints), Address, pCookie);
else
return DoCreateCodeBreakpoint(true, Address, pCookie);
case bptAccessWatchpoint:
case bptWriteWatchpoint:
case bptReadWatchpoint:
break;
default:
return kGDBNotSupported;
}
BpParameter_t bkpt;
memset(&bkpt, 0, sizeof(bkpt));
bkpt.bpMode = BP_COMPLEX;
bkpt.lAddrVal = (ULONG)Address;
bkpt.bpType = BP_MAB;
switch(type)
{
case bptAccessWatchpoint:
bkpt.bpAccess = BP_NO_FETCH;
break;
case bptReadWatchpoint:
bkpt.bpAccess = BP_READ;
break;
case bptWriteWatchpoint:
bkpt.bpAccess = BP_WRITE;
break;
}
bkpt.bpAction = BP_BRK;
bkpt.bpOperat = BP_EQUAL;
bkpt.bpCondition = BP_NO_COND;
bkpt.lMask = 0xffff;
WORD bpHandle = 0;
if (MSP430_EEM_SetBreakpoint(&bpHandle, &bkpt) != STATUS_OK)
REPORT_AND_RETURN("Cannot set an EEM breakpoint", kGDBUnknownError);
*pCookie = MAKE_BP_COOKIE(kBpCookieTypeHardware, bpHandle);
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430EEMTarget::RemoveBreakpoint( BreakpointType type, ULONGLONG Address, INT_PTR Cookie )
{
switch(type)
{
case bptSoftwareBreakpoint:
return DoRemoveCodeBreakpoint(false, Address, Cookie);
case bptHardwareBreakpoint:
case bptReadWatchpoint:
case bptWriteWatchpoint:
case bptAccessWatchpoint:
return DoRemoveCodeBreakpoint(true, Address, Cookie);
default:
return kGDBNotSupported;
}
return kGDBSuccess;
}
bool MSP430Proxy::MSP430EEMTarget::DoResumeTarget( RUN_MODES_t mode )
{
unsigned short originalInsn;
LONG regPC = 0;
if (MSP430_Read_Register(®PC, PC) != STATUS_OK)
REPORT_AND_RETURN("Cannot read PC register", false);
if (m_pBreakpointManager->GetOriginalInstruction(regPC, &originalInsn))
{
if (MSP430_Configure(SET_MDB_BEFORE_RUN, originalInsn) != STATUS_OK)
REPORT_AND_RETURN("Cannot resume from a software breakpoint", false);
m_BreakpointAddrOfLastResumeOp = regPC;
}
else
m_BreakpointAddrOfLastResumeOp = -1;
m_LastResumeMode = mode;
m_TargetStopped.Reset();
if (!m_pBreakpointManager->CommitBreakpoints())
{
printf("ERROR: Cannot commit software breakpoints\n");
return false;
}
if (!__super::DoResumeTarget(mode))
return false;
return true;
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430EEMTarget::SendBreakInRequestAsync()
{
if (m_bVerbose)
printf("Received an asynchronous break-in request.\n");
m_BreakInSemaphore.Signal();
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430EEMTarget::ReadTargetMemory( ULONGLONG Address, void *pBuffer, size_t *pSizeInBytes )
{
GDBStatus status = __super::ReadTargetMemory(Address, pBuffer, pSizeInBytes);
if (status != kGDBSuccess)
return status;
m_pBreakpointManager->HideOrRestoreBreakpointsInMemorySnapshot((unsigned)Address, pBuffer, *pSizeInBytes, true);
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430EEMTarget::WriteTargetMemory( ULONGLONG Address, const void *pBuffer, size_t sizeInBytes )
{
GDBStatus status = __super::WriteTargetMemory(Address, pBuffer, sizeInBytes);
if (status != kGDBSuccess)
return status;
// m_pBreakpointManager->HideOrRestoreBreakpointsInMemorySnapshot((unsigned)Address, pBuffer, *pSizeInBytes, false);
return kGDBSuccess;
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430EEMTarget::DoCreateCodeBreakpoint( bool hardware, ULONGLONG Address, INT_PTR *pCookie )
{
if (hardware)
{
BpParameter_t bkpt;
memset(&bkpt, 0, sizeof(bkpt));
bkpt.bpMode = BP_CODE;
bkpt.lAddrVal = (LONG)Address;
bkpt.bpCondition = BP_NO_COND;
bkpt.bpAction = BP_BRK;
WORD bpHandle = 0;
if (MSP430_EEM_SetBreakpoint(&bpHandle, &bkpt) != STATUS_OK)
REPORT_AND_RETURN("Cannot set an EEM breakpoint", kGDBUnknownError);
if (m_bVerbose)
printf("Created a hardware breakpoint #%d at 0x%x\n", bpHandle, (ULONG)Address);
m_HardwareBreakpointsUsed++;
C_ASSERT(sizeof(bpHandle) == 2);
*pCookie = MAKE_BP_COOKIE(kBpCookieTypeHardware, bpHandle);
return kGDBSuccess;
}
else
{
if (!IsFLASHAddress(Address))
{
ULONG addr = (ULONG)(Address & ~1);
if (m_RAMBreakpoints.IsBreakpointPresent((USHORT)addr))
{
printf("Cannot set a breakpoint at 0x%04x. Breakpoint already exists.\n", (unsigned)Address);
return kGDBUnknownError;
}
unsigned short insn;
if (MSP430_Read_Memory(addr, (char *)&insn, 2) != STATUS_OK)
REPORT_AND_RETURN("Cannot set a software breakpoint in RAM", kGDBUnknownError);
if (m_bVerbose)
printf("Setting a RAM breakpoint at 0x%x. Previous instruction is 0x%x\n", addr, insn);
*pCookie = MAKE_BP_COOKIE(kBpCookieTypeSoftwareRAM, insn);
insn = m_BreakpointInstruction;
if (MSP430_Write_Memory(addr, (char *)&insn, 2) != STATUS_OK)
REPORT_AND_RETURN("Cannot set a software breakpoint in RAM", kGDBUnknownError);
m_RAMBreakpoints.InsertBreakpoint((USHORT)addr);
}
else
{
if (!m_pBreakpointManager->SetBreakpoint((unsigned)Address))
return kGDBUnknownError;
*pCookie = MAKE_BP_COOKIE(kBpCookieTypeSoftwareFLASH, 0);
}
return kGDBSuccess;
}
}
GDBServerFoundation::GDBStatus MSP430Proxy::MSP430EEMTarget::DoRemoveCodeBreakpoint( bool hardware, ULONGLONG Address, INT_PTR Cookie )
{
if ((Cookie & kBpCookieTypeMask) == kBpCookieTypeHardware)
{
BpParameter_t bkpt;
memset(&bkpt, 0, sizeof(bkpt));
bkpt.bpMode = BP_CLEAR;
WORD bpHandle = (WORD)(Cookie & kBpCookieDataMask);
if (MSP430_EEM_SetBreakpoint(&bpHandle, &bkpt) != STATUS_OK)
REPORT_AND_RETURN("Cannot remove an EEM breakpoint", kGDBUnknownError);
if (m_bVerbose)
printf("Removed a hardware breakpoint #%d at 0x%x\n", (WORD)(Cookie & kBpCookieDataMask), (ULONG)Address);
m_HardwareBreakpointsUsed--;
return kGDBSuccess;
}
else
{
if (!IsFLASHAddress(Address))
{
ASSERT((Cookie & kBpCookieTypeMask) == kBpCookieTypeSoftwareRAM);
unsigned short originalINSN = (unsigned short)(Cookie & kBpCookieDataMask);
if (m_bVerbose)
printf("Deleting SRAM breakpoint at 0x%x. Restoring original instruction of 0x%x.\n", (ULONG)Address, originalINSN);
if (MSP430_Write_Memory(Address & ~1, (char *)&originalINSN, 2) != STATUS_OK)
REPORT_AND_RETURN("Cannot remove a software breakpoint from RAM", kGDBUnknownError);
m_RAMBreakpoints.RemoveBreakpoint((USHORT)(Address & ~1));
}
else
{
ASSERT((Cookie & kBpCookieTypeMask) == kBpCookieTypeSoftwareFLASH);
if (!m_pBreakpointManager->RemoveBreakpoint((unsigned)Address))
return kGDBUnknownError;
}
return kGDBSuccess;
}
}