OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [sysc/] [src/] [GdbServerSC.cpp] - Blame information for rev 861

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 63 julius
// ----------------------------------------------------------------------------
2
 
3
// SystemC GDB RSP server: implementation
4
 
5
// Copyright (C) 2008  Embecosm Limited <info@embecosm.com>
6
 
7
// Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8
// Contributor Julius Baxter <julius@orsoc.se>
9
 
10
// This file is part of the GDB interface to the cycle accurate model of the
11
// OpenRISC 1000 based system-on-chip, ORPSoC, built using Verilator.
12
 
13
// This program is free software: you can redistribute it and/or modify it
14
// under the terms of the GNU Lesser General Public License as published by
15
// the Free Software Foundation, either version 3 of the License, or (at your
16
// option) any later version.
17
 
18
// This program is distributed in the hope that it will be useful, but WITHOUT
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
21
// License for more details.
22
 
23
// You should have received a copy of the GNU Lesser General Public License
24
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 
26
// ----------------------------------------------------------------------------
27
 
28
// $Id$
29
 
30
#include <iostream>
31
#include <iomanip>
32
 
33
#include "GdbServerSC.h"
34
#include "Utils.h"
35
 
36
#include <errno.h>
37
#include <fcntl.h>
38 861 stekern
#include <unistd.h>
39 462 julius
extern int monitor_to_gdb_pipe[2][2];   // [0][] - monitor to gdb, [1][] - gdb to monitor, [][0] - read, [][1] - write
40 63 julius
 
41
using std::cerr;
42
using std::dec;
43
using std::endl;
44
using std::hex;
45
 
46
using sc_core::sc_fifo;
47
using sc_core::sc_module_name;
48
using sc_core::sc_stop;
49
using sc_core::sc_time;
50
 
51 462 julius
SC_HAS_PROCESS(GdbServerSC);
52 63 julius
 
53
//-----------------------------------------------------------------------------
54
//! Constructor for the GDB RSP server.
55
 
56
//! We create a SC_THREAD which will act as the listener. Must be a
57
//! thread, since we need to wait for the actions to complete.
58
 
59
//! The current scan chain is marked as undefined.
60
 
61
//! This makes use of the Embecosm cycle accurate SystemC JTAG interface.
62
 
63
//! @see Embecosm Application Note 5 "Using JTAG with SystemC: Implementation
64
//!      of a Cycle Accurate Interface"
65
//!      (http://www.embecosm.com/download/ean5.html)
66
 
67
//! @todo We do not handle a user coded l.trap properly (i.e. one that is not
68
//!       a breakpoint). Effectively it is ignored, whereas we ought to set up
69
//!       the exception registers and redirect through the trap vector.
70
 
71
//! @param[in] name             Name of this module, passed to the parent
72
//!                             constructor. 
73
//! @param[in] _tapActionQueue  Pointer to fifo of actions to be performed by
74
//!                             the JTAG interface
75
//-----------------------------------------------------------------------------
76 462 julius
GdbServerSC::GdbServerSC(sc_module_name name,
77
                         uint32_t _flashStart,
78
                         uint32_t _flashEnd,
79
                         int rspPort,
80
                         sc_fifo <
81
                         TapAction * >*tapActionQueue):sc_module(name),
82
flashStart(_flashStart), flashEnd(_flashEnd)
83 63 julius
{
84 462 julius
        pkt = new RspPacket(RSP_PKT_MAX);
85
        rsp = new RspConnection(rspPort);
86
        debugUnit = new DebugUnitSC("debug-unit", tapActionQueue);
87
        mpHash = new MpHash();
88 63 julius
 
89 462 julius
        /* Setup the pipes between or1200 monitor module and GDB stub */
90
        pipe(monitor_to_gdb_pipe[0]);
91
        pipe(monitor_to_gdb_pipe[1]);
92
 
93
        // Set non-blocking reads
94
#ifdef O_NONBLOCK               /* The POSIX way */
95
        fcntl(monitor_to_gdb_pipe[0][0], F_SETFL, O_NONBLOCK);
96
        fcntl(monitor_to_gdb_pipe[1][0], F_SETFL, O_NONBLOCK);
97 63 julius
#elif defined (O_NDELAY)
98 462 julius
        fcntl(monitor_to_gdb_pipe[0][0], F_SETFL, O_NDELAY);
99
        fcntl(monitor_to_gdb_pipe[1][0], F_SETFL, O_NDELAY);
100 63 julius
#endif /* O_NONBLOCK */
101
 
102 462 julius
        SC_THREAD(rspServer);
103 63 julius
 
104 462 julius
}                               // GdbServerSC ()
105 63 julius
 
106
//-----------------------------------------------------------------------------
107
//! Destructor
108
 
109
//! Free up data structures
110
//-----------------------------------------------------------------------------
111 462 julius
GdbServerSC::~GdbServerSC()
112 63 julius
{
113 462 julius
        delete mpHash;
114
        delete debugUnit;
115
        delete rsp;
116
        delete pkt;
117 63 julius
 
118 462 julius
}                               // ~GdbServerSC
119 63 julius
 
120
//-----------------------------------------------------------------------------
121
//! SystemC thread to listen for RSP requests
122
 
123
//! JTAG actions will be queued as appropriate. Runs forever
124
 
125
//! We don't allow any actions until the target is out of its startup mode
126
//! (where the ROM is mapped into RAM space). This is determined by seeing if
127
//! the PPC is still in flash memory.
128
 
129
//! Have to use a thread, since we will end up waiting for actions to
130
//! complete.
131
//-----------------------------------------------------------------------------
132
void
133 462 julius
 GdbServerSC::rspServer()
134 63 julius
{
135 462 julius
        // Reset the debug unit, and wait for ORPSoC to be ready, by noting when it
136
        // accesses an address outside of flash. Note that we use NPC, not PPC since
137
        // at reset PPC is zero, and would trigger a false positive.
138 63 julius
 
139 462 julius
        debugUnit->resetDebugUnit();
140
        rsp_sigval = TARGET_SIGNAL_NONE;
141
        /*
142
           uint32_t npc;
143
           do
144
           {
145
           npc = debugUnit->readSpr (SPR_NPC);
146
           }
147
           while ((flashStart <= npc) && (npc <= flashEnd));
148
         */
149
        debugUnit->stall();
150
        targetStopped = true;
151
 
152
        // Make sure we are connected.
153
        while (!rsp->isConnected()) {
154
                // Reconnect and stall the processor on a new connection
155
                if (!rsp->rspConnect()) {
156
                        // Serious failure. Must abort execution.
157
                        cerr << "*** Unable to continue: ABORTING" << endl;
158
                        sc_stop();
159
                }
160
                // Stall the processor until we get a command to handle.
161
                if (!debugUnit->isStalled()) {
162
                        debugUnit->stall();
163
                }
164
 
165
                targetStopped = true;   // Processor now not running
166 63 julius
        }
167
 
168 462 julius
        // Loop processing commands forever
169
        while (true) {
170
                if (!rsp->isConnected()) {
171
                        sc_stop();
172
                        return;
173
                }
174
                // Wait until the target has stopped. In this simplified implementation,
175
                // the only reasons for stopping are hitting a breakpoint (l.trap),
176
                // hardware single stepping or hitting a non-breakpoint l.trap. This
177
                // last is not cleanly handled at the moment (we ought to redirect the
178
                // restart through the trap exception vector).
179
                while (!targetStopped) {
180 63 julius
 
181 462 julius
                        /* First check to see if the or1200 monitor module wants us to stall */
182
                        if (checkMonitorPipe())
183
                                break;
184 63 julius
 
185 462 julius
                        rspCheckForException();
186 63 julius
 
187 462 julius
                        if (debugUnit->isStalled()) {
188
                                targetStopped = true;
189
 
190
                                // If it's a breakpoint, then we need to back up one
191
                                // instruction, so on restart we execute the actual
192
                                // instruction.
193
                                uint32_t ppc = debugUnit->readSpr(SPR_PPC);
194
 
195
                                if (NULL != mpHash->lookup(BP_MEMORY, ppc)
196
                                    && rsp_sigval == TARGET_SIGNAL_TRAP) {
197
                                        writeNpc(ppc);
198
                                }
199
                                // Tell the client we've stopped.
200
                                rspReportException();
201
                        } else if (rsp->rspSocketPeek() > 0) {
202
                                if (rsp->rspSocketPeek() == 0x03)       // ETX, end of text control char
203
                                {
204
                                        // Got an interrupt command from GDB, this function should
205
                                        // pull the packet off the socket and stall the processor.
206
                                        // and then send a stop reply packet with signal 
207
                                        // TARGET_SIGNAL_NONE.
208
                                        rspInterrupt();
209
                                        targetStopped = true;   // Processor now not running
210
                                }
211
                        }
212 63 julius
                }
213 462 julius
 
214
                // Get a RSP client request
215
                rspClientRequest();
216 63 julius
        }
217 462 julius
}                               // rspServer ()
218 63 julius
 
219
//-----------------------------------------------------------------------------
220
//! Deal with a request from the GDB client session
221
 
222
//! In general, apart from the simplest requests, this function replies on
223
//! other functions to implement the functionality.
224
 
225
//! @note It is the responsibility of the recipient to delete the packet when
226
//!       it is finished with. It is permissible to reuse the packet for a
227
//!       reply.
228
 
229
//! @todo Is this the implementation of the 'D' packet really the intended
230
//!       meaning? Or does it just mean that only vAttach will be recognized
231
//!       after this?
232
 
233
//! @param[in] pkt  The received RSP packet
234
//-----------------------------------------------------------------------------
235 462 julius
void GdbServerSC::rspClientRequest()
236 63 julius
{
237 462 julius
        if (!rsp->getPkt(pkt)) {
238
                rsp->rspClose();        // Comms failure
239
                return;
240
        }
241
        //Uncomment the next line for the RSP client to print out every packet it gets from GDB
242
        //cerr << "rspClientRequest: " << pkt->data/*[0]*/ << endl;
243
        switch (pkt->data[0]) {
244
        case '!':
245
                // Request for extended remote mode
246
                pkt->packStr("OK");
247
                rsp->putPkt(pkt);
248
                return;
249 63 julius
 
250 462 julius
        case '?':
251
                // Return last signal ID
252
                rspReportException();
253
                return;
254 63 julius
 
255 462 julius
        case 'A':
256
                // Initialization of argv not supported
257
                cerr << "Warning: RSP 'A' packet not supported: ignored" <<
258
                    endl;
259
                pkt->packStr("E01");
260
                rsp->putPkt(pkt);
261
                return;
262 63 julius
 
263 462 julius
        case 'b':
264
                // Setting baud rate is deprecated
265
                cerr << "Warning: RSP 'b' packet is deprecated and not "
266
                    << "supported: ignored" << endl;
267
                return;
268 63 julius
 
269 462 julius
        case 'B':
270
                // Breakpoints should be set using Z packets
271
                cerr << "Warning: RSP 'B' packet is deprecated (use 'Z'/'z' "
272
                    << "packets instead): ignored" << endl;
273
                return;
274 63 julius
 
275 462 julius
        case 'c':
276
                // Continue
277
                rspContinue(EXCEPT_NONE);
278
                return;
279 63 julius
 
280 462 julius
        case 'C':
281
                // Continue with signal (in the packet)
282
                rspContinue();
283
                return;
284 63 julius
 
285 462 julius
        case 'd':
286
                // Disable debug using a general query
287
                cerr << "Warning: RSP 'd' packet is deprecated (define a 'Q' "
288
                    << "packet instead: ignored" << endl;
289
                return;
290 63 julius
 
291 462 julius
        case 'D':
292
                // Detach GDB. Do this by closing the client. The rules say that
293
                // execution should continue, so unstall the processor.
294
                pkt->packStr("OK");
295
                rsp->putPkt(pkt);
296
                rsp->rspClose();
297
                //debugUnit->unstall ();      
298
                return;
299 63 julius
 
300 462 julius
        case 'F':
301
                // File I/O is not currently supported
302
                cerr << "Warning: RSP file I/O not currently supported: 'F' "
303
                    << "packet ignored" << endl;
304
                return;
305 63 julius
 
306 462 julius
        case 'g':
307
                rspReadAllRegs();
308
                return;
309 63 julius
 
310 462 julius
        case 'G':
311
                rspWriteAllRegs();
312
                return;
313 63 julius
 
314 462 julius
        case 'H':
315
                // Set the thread number of subsequent operations. For now ignore
316
                // silently and just reply "OK"
317
                pkt->packStr("OK");
318
                rsp->putPkt(pkt);
319
                return;
320 63 julius
 
321 462 julius
        case 'i':
322
        case 'I':
323
                // Single cycle step not currently supported. Mark the target as
324
                // running, so that next time it will be detected as stopped (it is
325
                // still stalled in reality) and an ack sent back to the client.
326
                cerr << "Warning: RSP cycle stepping not supported: target "
327
                    << "stopped immediately" << endl;
328
                targetStopped = false;
329
                return;
330 63 julius
 
331 462 julius
        case 'k':
332
                // Kill request. Do nothing for now.
333
                return;
334 63 julius
 
335 462 julius
        case 'm':
336
                // Read memory (symbolic)
337
                rspReadMem();
338
                return;
339 63 julius
 
340 462 julius
        case 'M':
341
                // Write memory (symbolic)
342
                rspWriteMem();
343
                return;
344 63 julius
 
345 462 julius
        case 'p':
346
                // Read a register
347
                rspReadReg();
348
                return;
349 63 julius
 
350 462 julius
        case 'P':
351
                // Write a register
352
                rspWriteReg();
353
                return;
354 63 julius
 
355 462 julius
        case 'q':
356
                // Any one of a number of query packets
357
                rspQuery();
358
                return;
359 63 julius
 
360 462 julius
        case 'Q':
361
                // Any one of a number of set packets
362
                rspSet();
363
                return;
364 63 julius
 
365 462 julius
        case 'r':
366
                // Reset the system. Deprecated (use 'R' instead)
367
                cerr << "Warning: RSP 'r' packet is deprecated (use 'R' "
368
                    << "packet instead): ignored" << endl;
369
                return;
370 63 julius
 
371 462 julius
        case 'R':
372
                // Restart the program being debugged.
373
                rspRestart();
374
                return;
375 63 julius
 
376 462 julius
        case 's':
377
                // Single step one machine instruction.
378
                rspStep(EXCEPT_NONE);
379
                return;
380 63 julius
 
381 462 julius
        case 'S':
382
                // Single step one machine instruction.
383
                rspStep();
384
                return;
385 63 julius
 
386 462 julius
        case 't':
387
                // Search. This is not well defined in the manual and for now we don't
388
                // support it. No response is defined.
389
                cerr << "Warning: RSP 't' packet not supported: ignored"
390
                    << endl;
391
                return;
392 63 julius
 
393 462 julius
        case 'T':
394
                // Is the thread alive. We are bare metal, so don't have a thread
395
                // context. The answer is always "OK".
396
                pkt->packStr("OK");
397
                rsp->putPkt(pkt);
398
                return;
399 63 julius
 
400 462 julius
        case 'v':
401
                // Any one of a number of packets to control execution
402
                rspVpkt();
403
                return;
404 63 julius
 
405 462 julius
        case 'X':
406
                // Write memory (binary)
407
                rspWriteMemBin();
408
                return;
409 63 julius
 
410 462 julius
        case 'z':
411
                // Remove a breakpoint/watchpoint.
412
                rspRemoveMatchpoint();
413
                return;
414 63 julius
 
415 462 julius
        case 'Z':
416
                // Insert a breakpoint/watchpoint.
417
                rspInsertMatchpoint();
418
                return;
419 63 julius
 
420 462 julius
        default:
421
                // Unknown commands are ignored
422
                cerr << "Warning: Unknown RSP request" << pkt->data << endl;
423
                return;
424
        }
425
}                               // rspClientRequest ()
426 63 julius
 
427
//-----------------------------------------------------------------------------
428
//! Check if processor is stalled. If it is, read the DRR and return the
429
//! target signal code.
430
//-----------------------------------------------------------------------------
431 462 julius
void GdbServerSC::rspCheckForException()
432 63 julius
{
433
 
434 462 julius
        uint32_t drr;
435 63 julius
 
436 462 julius
        if (!debugUnit->isStalled()) {
437
                // Processor not stalled. Just return;
438
                return;
439
        }
440
 
441
        switch ((debugUnit->readSpr(SPR_DRR) & 0xffffffff)) {
442
        case SPR_DRR_RSTE:
443
                rsp_sigval = TARGET_SIGNAL_PWR;
444
                break;
445
        case SPR_DRR_BUSEE:
446
                rsp_sigval = TARGET_SIGNAL_BUS;
447
                break;
448
        case SPR_DRR_DPFE:
449
                rsp_sigval = TARGET_SIGNAL_SEGV;
450
                break;
451
        case SPR_DRR_IPFE:
452
                rsp_sigval = TARGET_SIGNAL_SEGV;
453
                break;
454
        case SPR_DRR_TTE:
455
                rsp_sigval = TARGET_SIGNAL_ALRM;
456
                break;
457
        case SPR_DRR_AE:
458
                rsp_sigval = TARGET_SIGNAL_BUS;
459
                break;
460
        case SPR_DRR_IIE:
461
                rsp_sigval = TARGET_SIGNAL_ILL;
462
                break;
463
        case SPR_DRR_IE:
464
                rsp_sigval = TARGET_SIGNAL_INT;
465
                break;
466
        case SPR_DRR_DME:
467
                rsp_sigval = TARGET_SIGNAL_SEGV;
468
                break;
469
        case SPR_DRR_IME:
470
                rsp_sigval = TARGET_SIGNAL_SEGV;
471
                break;
472
        case SPR_DRR_RE:
473
                rsp_sigval = TARGET_SIGNAL_FPE;
474
                break;
475
        case SPR_DRR_SCE:
476
                rsp_sigval = TARGET_SIGNAL_USR2;
477
                break;
478
        case SPR_DRR_FPE:
479
                rsp_sigval = TARGET_SIGNAL_FPE;
480
                break;
481
        case SPR_DRR_TE:
482
                rsp_sigval = TARGET_SIGNAL_TRAP;
483
                break;
484
 
485
        default:
486
                // This must be the case of single step (which does not set DRR)
487
                rsp_sigval = TARGET_SIGNAL_TRAP;
488
                break;
489
        }
490
 
491
        return;
492 63 julius
}
493
 
494
//-----------------------------------------------------------------------------
495
//! Send a packet acknowledging an exception has occurred
496
 
497
//! The only signal we ever see in this implementation is TRAP.
498
//-----------------------------------------------------------------------------
499 462 julius
void GdbServerSC::rspReportException()
500 63 julius
{
501 462 julius
        // Construct a signal received packet
502
        pkt->data[0] = 'S';
503
        pkt->data[1] = Utils::hex2Char(rsp_sigval >> 4);
504
        pkt->data[2] = Utils::hex2Char(rsp_sigval % 16);
505
        pkt->data[3] = '\0';
506
        pkt->setLen(strlen(pkt->data));
507 63 julius
 
508 462 julius
        rsp->putPkt(pkt);
509 63 julius
 
510 462 julius
}                               // rspReportException ()
511 63 julius
 
512
//-----------------------------------------------------------------------------
513
//! Handle a RSP continue request
514
 
515
//! This version is typically used for the 'c' packet, to continue without
516
//! signal, in which case EXCEPT_NONE is passed in as the exception to use.
517
 
518
//! At present other exceptions are not supported
519
 
520
//! @param[in] except  The OpenRISC 1000 exception to use
521
//-----------------------------------------------------------------------------
522 462 julius
void GdbServerSC::rspContinue(uint32_t except)
523 63 julius
{
524 462 julius
        uint32_t addr;          // Address to continue from, if any
525 63 julius
 
526 462 julius
        // Reject all except 'c' packets
527
        if ('c' != pkt->data[0]) {
528
                cerr <<
529
                    "Warning: Continue with signal not currently supported: " <<
530
                    "ignored" << endl;
531
                return;
532
        }
533
        // Get an address if we have one
534
        if (0 == strcmp("c", pkt->data)) {
535
                addr = readNpc();       // Default uses current NPC
536
        } else if (1 != sscanf(pkt->data, "c%lx", &addr)) {
537
                cerr << "Warning: RSP continue address " << pkt->data
538
                    << " not recognized: ignored" << endl;
539
                addr = readNpc();       // Default uses current NPC
540
        }
541 63 julius
 
542 462 julius
        rspContinue(addr, EXCEPT_NONE);
543 63 julius
 
544 462 julius
}                               // rspContinue ()
545 63 julius
 
546
//-----------------------------------------------------------------------------
547
//! Handle a RSP continue with signal request
548
 
549
//! @todo Currently does nothing. Will use the underlying generic continue
550
//!       function. 
551
//-----------------------------------------------------------------------------
552 462 julius
void GdbServerSC::rspContinue()
553 63 julius
{
554 462 julius
        cerr << "RSP continue with signal '" << pkt->data
555
            << "' received" << endl;
556 63 julius
 
557 462 julius
}                               // rspContinue ()
558 63 julius
 
559
//-----------------------------------------------------------------------------
560
//! Generic processing of a continue request
561
 
562
//! The signal may be EXCEPT_NONE if there is no exception to be
563
//! handled. Currently the exception is ignored.
564
 
565
//! The single step flag is cleared in the debug registers and then the
566
//! processor is unstalled.
567
 
568
//! @param[in] addr    Address from which to step
569
//! @param[in] except  The exception to use (if any)                         
570
//-----------------------------------------------------------------------------
571 462 julius
void GdbServerSC::rspContinue(uint32_t addr, uint32_t except)
572 63 julius
{
573 462 julius
        // Set the address as the value of the next program counter
574
        writeNpc(addr);
575 63 julius
 
576 462 julius
        /*
577
           // If we're continuing from a breakpoint, replace that instruction in the memory
578
           // ... actually no, I was wrong about this.
579
           if (NULL != mpHash->lookup (BP_MEMORY, addr) && rsp_sigval == TARGET_SIGNAL_TRAP)
580
           {
581
           MpEntry *entry = mpHash->lookup (BP_MEMORY, addr);
582
           debugUnit->writeMem32(entry->addr, entry->instr);
583
           }
584
         */
585 63 julius
 
586 462 julius
        // Clear Debug Reason Register and watchpoint break generation in Debug Mode
587
        // Register 2 for watchpoints that we triggered to stop this time.
588
        debugUnit->writeSpr(SPR_DRR, 0);
589
        if (rsp_sigval == TARGET_SIGNAL_TRAP) {
590
                /*
591
                   Disable last trap generation on watchpoint if this is why we stopped
592
                   last time.
593
                 */
594
                uint32_t temp_dmr2 = debugUnit->readSpr(SPR_DMR2);
595
                if (temp_dmr2 & SPR_DMR2_WBS) {
596
                        /*
597
                           One of these breakpoints is responsible for our stopping, so
598
                           disable it this time we start. GDB will send a packet re-enabling
599
                           it next time we continue.
600
                         */
601
                        debugUnit->writeSpr(SPR_DMR2,
602
                                            temp_dmr2 &
603
                                            ~((temp_dmr2 & SPR_DMR2_WBS) >>
604
                                              10));
605
                }
606
        }
607
        // Clear the single step trigger in Debug Mode Register 1 and set traps to
608
        // be handled by the debug unit in the Debug Stop Register
609
        debugUnit->andSpr(SPR_DMR1, ~SPR_DMR1_ST);
610
        debugUnit->orSpr(SPR_DSR, SPR_DSR_TE);
611 63 julius
 
612 462 julius
        // Unstall the processor. Note the GDB client is now waiting for a reply,
613
        // which it will get as soon as the processor stalls again.
614
        debugUnit->unstall();
615
        targetStopped = false;
616 63 julius
 
617 462 julius
}                               // rspContinue ()
618 63 julius
 
619
//------------------------------------------------------------------------------
620
//!Handle an interrupt from GDB
621
 
622
//! Detect an interrupt from GDB and stall the processor                        
623
//------------------------------------------------------------------------------
624 462 julius
void GdbServerSC::rspInterrupt()
625 63 julius
{
626 462 julius
        unsigned char c;
627 63 julius
 
628 462 julius
        c = rsp->getRspChar();
629
        if (c < 0) {
630
                // Had issues, just return
631
                return;
632
        }
633
        // Ensure this is a ETX control char (0x3), currently, we only call this
634
        // function when we've peeked and seen it, otherwise, ignore, return and pray
635
        // things go back to normal...
636
        if (c != 0x03) {
637
                cerr <<
638
                    "* Warning: Interrupt character expected but not found on socket"
639
                    << endl;
640
                return;
641
        }
642
        // Otherwise, it's an interrupt packet, stall the processor, and upon return
643
        // to the main handle_rsp() loop, it will inform GDB.
644 63 julius
 
645 462 julius
        debugUnit->stall();
646 63 julius
 
647 462 julius
        // Send a stop reply response, manually set rsp.sigval to TARGET_SIGNAL_NONE
648
        rsp_sigval = TARGET_SIGNAL_NONE;
649
        rspReportException();
650 63 julius
 
651 462 julius
        return;
652
 
653 63 julius
}
654
 
655
//-----------------------------------------------------------------------------
656
//! Handle a RSP read all registers request
657
 
658
//! The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PPC
659
//! (i.e. SPR PPC), NPC (i.e. SPR NPC) and SR (i.e. SPR SR). Each register is
660
//! returned as a sequence of bytes in target endian order.
661
 
662
//! Each byte is packed as a pair of hex digits.                             
663
//-----------------------------------------------------------------------------
664 462 julius
void GdbServerSC::rspReadAllRegs()
665 63 julius
{
666 462 julius
        // The GPRs
667
        for (int r = 0; r < max_gprs; r++) {
668
                Utils::reg2Hex(readGpr(r), &(pkt->data[r * 8]));
669
        }
670 63 julius
 
671 462 julius
        // PPC, NPC and SR
672
        Utils::reg2Hex(debugUnit->readSpr(SPR_PPC),
673
                       &(pkt->data[PPC_REGNUM * 8]));
674
        Utils::reg2Hex(readNpc(), &(pkt->data[NPC_REGNUM * 8]));
675
        Utils::reg2Hex(debugUnit->readSpr(SPR_SR), &(pkt->data[SR_REGNUM * 8]));
676 63 julius
 
677 462 julius
        // Finalize the packet and send it
678
        pkt->data[NUM_REGS * 8] = 0;
679
        pkt->setLen(NUM_REGS * 8);
680
        rsp->putPkt(pkt);
681 63 julius
 
682 462 julius
}                               // rspReadAllRegs ()
683 63 julius
 
684
//-----------------------------------------------------------------------------
685
//! Handle a RSP write all registers request
686
 
687
//! The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PPC
688
//! (i.e. SPR PPC), NPC (i.e. SPR NPC) and SR (i.e. SPR SR). Each register is
689
//! supplied as a sequence of bytes in target endian order.
690
 
691
//! Each byte is packed as a pair of hex digits.
692
 
693
//! @todo There is no error checking at present. Non-hex chars will generate a
694
//!       warning message, but there is no other check that the right amount
695
//!       of data is present. The result is always "OK".
696
//-----------------------------------------------------------------------------
697 462 julius
void GdbServerSC::rspWriteAllRegs()
698 63 julius
{
699 462 julius
        // The GPRs
700
        for (int r = 0; r < max_gprs; r++) {
701
                writeGpr(r, Utils::hex2Reg(&(pkt->data[r * 8])));
702
        }
703 63 julius
 
704 462 julius
        // PPC, NPC and SR
705
        debugUnit->writeSpr(SPR_PPC,
706
                            Utils::hex2Reg(&(pkt->data[PPC_REGNUM * 8])));
707
        debugUnit->writeSpr(SPR_SR,
708
                            Utils::hex2Reg(&(pkt->data[SR_REGNUM * 8])));
709
        writeNpc(Utils::hex2Reg(&(pkt->data[NPC_REGNUM * 8])));
710 63 julius
 
711 462 julius
        // Acknowledge (always OK for now).
712
        pkt->packStr("OK");
713
        rsp->putPkt(pkt);
714 63 julius
 
715 462 julius
}                               // rspWriteAllRegs ()
716 63 julius
 
717
//-----------------------------------------------------------------------------
718
//! Handle a RSP read memory (symbolic) request
719
 
720
//! Syntax is:
721
 
722
//!   m<addr>,<length>:
723
 
724
//! The response is the bytes, lowest address first, encoded as pairs of hex
725
//! digits.
726
 
727
//! The length given is the number of bytes to be read.
728
//-----------------------------------------------------------------------------
729 462 julius
void GdbServerSC::rspReadMem()
730 63 julius
{
731 462 julius
        unsigned int addr;      // Where to read the memory
732
        int len;                // Number of bytes to read
733
        int off;                // Offset into the memory
734 63 julius
 
735 462 julius
        if (2 != sscanf(pkt->data, "m%x,%x:", &addr, &len)) {
736
                cerr << "Warning: Failed to recognize RSP read memory command: "
737
                    << pkt->data << endl;
738
                pkt->packStr("E01");
739
                rsp->putPkt(pkt);
740
                return;
741
        }
742
        // Make sure we won't overflow the buffer (2 chars per byte)
743
        if ((len * 2) >= pkt->getBufSize()) {
744
                cerr << "Warning: Memory read " << pkt->data
745
                    << " too large for RSP packet: truncated" << endl;
746
                len = (pkt->getBufSize() - 1) / 2;
747
        }
748
        //cerr << "rspReadMem: " << len << " bytes@0x"<< hex << addr << endl;
749
        // Refill the buffer with the reply
750
        for (off = 0; off < len; off++) {
751
                unsigned char ch = debugUnit->readMem8(addr + off);
752 63 julius
 
753 462 julius
                pkt->data[off * 2] = Utils::hex2Char(ch >> 4);
754
                pkt->data[off * 2 + 1] = Utils::hex2Char(ch & 0xf);
755
        }
756 63 julius
 
757 462 julius
        pkt->data[off * 2] = '\0';      // End of string
758
        pkt->setLen(strlen(pkt->data));
759
        rsp->putPkt(pkt);
760 63 julius
 
761 462 julius
}                               // rsp_read_mem ()
762 63 julius
 
763
//-----------------------------------------------------------------------------
764
//! Handle a RSP write memory (symbolic) request
765
 
766
//! Syntax is:
767
 
768
//!   m<addr>,<length>:<data>
769
 
770
//! The data is the bytes, lowest address first, encoded as pairs of hex
771
//! digits.
772
 
773
//! The length given is the number of bytes to be written.
774
//-----------------------------------------------------------------------------
775 462 julius
void GdbServerSC::rspWriteMem()
776 63 julius
{
777 462 julius
        uint32_t addr;          // Where to write the memory
778
        int len;                // Number of bytes to write
779 63 julius
 
780 462 julius
        if (2 != sscanf(pkt->data, "M%x,%x:", &addr, &len)) {
781
                cerr << "Warning: Failed to recognize RSP write memory "
782
                    << pkt->data << endl;
783
                pkt->packStr("E01");
784
                rsp->putPkt(pkt);
785
                return;
786
        }
787
        // Find the start of the data and check there is the amount we expect.
788
        char *symDat = (char *)(memchr(pkt->data, ':', pkt->getBufSize())) + 1;
789
        int datLen = pkt->getLen() - (symDat - pkt->data);
790 63 julius
 
791 462 julius
        // Sanity check
792
        if (len * 2 != datLen) {
793
                cerr << "Warning: Write of " << len *
794
                    2 << "digits requested, but " << datLen <<
795
                    " digits supplied: packet ignored" << endl;
796
                pkt->packStr("E01");
797
                rsp->putPkt(pkt);
798
                return;
799
        }
800
        // Write the bytes to memory (no check the address is OK here)
801
        for (int off = 0; off < len; off++) {
802
                uint8_t nyb1 = Utils::char2Hex(symDat[off * 2]);
803
                uint8_t nyb2 = Utils::char2Hex(symDat[off * 2 + 1]);
804 63 julius
 
805 462 julius
                if (!debugUnit->writeMem8(addr + off, (nyb1 << 4) | nyb2)) {
806
                        pkt->packStr("E01");
807
                        rsp->putPkt(pkt);
808
                        return;
809
                }
810 63 julius
        }
811
 
812 462 julius
        pkt->packStr("OK");
813
        rsp->putPkt(pkt);
814 63 julius
 
815 462 julius
}                               // rspWriteMem ()
816 63 julius
 
817
//-----------------------------------------------------------------------------
818
//! Read a single register
819
 
820
//! The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PC
821
//! (i.e. SPR NPC) and SR (i.e. SPR SR). The register is returned as a
822
//! sequence of bytes in target endian order.
823
 
824
//! Each byte is packed as a pair of hex digits.
825
//-----------------------------------------------------------------------------
826 462 julius
void GdbServerSC::rspReadReg()
827 63 julius
{
828 462 julius
        unsigned int regNum;
829 63 julius
 
830 462 julius
        // Break out the fields from the data
831
        if (1 != sscanf(pkt->data, "p%x", &regNum)) {
832
                cerr <<
833
                    "Warning: Failed to recognize RSP read register command: "
834
                    << pkt->data << endl;
835
                pkt->packStr("E01");
836
                rsp->putPkt(pkt);
837
                return;
838
        }
839
        // Get the relevant register
840
        if (regNum < max_gprs) {
841
                Utils::Utils::reg2Hex(readGpr(regNum), pkt->data);
842
        } else if (PPC_REGNUM == regNum) {
843
                Utils::Utils::reg2Hex(debugUnit->readSpr(SPR_PPC), pkt->data);
844
        } else if (NPC_REGNUM == regNum) {
845
                Utils::Utils::reg2Hex(readNpc(), pkt->data);
846
        } else if (SR_REGNUM == regNum) {
847
                Utils::Utils::reg2Hex(debugUnit->readSpr(SPR_SR), pkt->data);
848
        } else {
849
                // Error response if we don't know the register
850
                cerr << "Warning: Attempt to read unknown register" << regNum
851
                    << ": ignored" << endl;
852
                pkt->packStr("E01");
853
                rsp->putPkt(pkt);
854
                return;
855
        }
856 63 julius
 
857 462 julius
        pkt->setLen(strlen(pkt->data));
858
        rsp->putPkt(pkt);
859 63 julius
 
860 462 julius
}                               // rspWriteReg ()
861 63 julius
 
862
//-----------------------------------------------------------------------------
863
//! Write a single register
864
 
865
//! The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PC
866
//! (i.e. SPR NPC) and SR (i.e. SPR SR). The register is specified as a
867
//! sequence of bytes in target endian order.
868
 
869
//! Each byte is packed as a pair of hex digits.
870
//-----------------------------------------------------------------------------
871 462 julius
void GdbServerSC::rspWriteReg()
872 63 julius
{
873 462 julius
        unsigned int regNum;
874
        char valstr[9];         // Allow for EOS on the string
875 63 julius
 
876 462 julius
        // Break out the fields from the data
877
        if (2 != sscanf(pkt->data, "P%x=%8s", &regNum, valstr)) {
878
                cerr <<
879
                    "Warning: Failed to recognize RSP write register command "
880
                    << pkt->data << endl;
881
                pkt->packStr("E01");
882
                rsp->putPkt(pkt);
883
                return;
884
        }
885
        // Set the relevant register
886
        if (regNum < max_gprs) {
887
                writeGpr(regNum, Utils::hex2Reg(valstr));
888
        } else if (PPC_REGNUM == regNum) {
889
                debugUnit->writeSpr(SPR_PPC, Utils::hex2Reg(valstr));
890
        } else if (NPC_REGNUM == regNum) {
891
                writeNpc(Utils::hex2Reg(valstr));
892
        } else if (SR_REGNUM == regNum) {
893
                debugUnit->writeSpr(SPR_SR, Utils::hex2Reg(valstr));
894
        } else {
895
                // Error response if we don't know the register
896
                cerr << "Warning: Attempt to write unknown register " << regNum
897
                    << ": ignored" << endl;
898
                pkt->packStr("E01");
899
                rsp->putPkt(pkt);
900
                return;
901
        }
902 63 julius
 
903 462 julius
        pkt->packStr("OK");
904
        rsp->putPkt(pkt);
905 63 julius
 
906 462 julius
}                               // rspWriteReg ()
907 63 julius
 
908
//-----------------------------------------------------------------------------
909
//! Handle a RSP query request
910
//-----------------------------------------------------------------------------
911 462 julius
void GdbServerSC::rspQuery()
912 63 julius
{
913 462 julius
        if (0 == strcmp("qAttached", pkt->data)) {
914
                // We are always attaching to an existing process with the bare metal
915
                // embedded system. 
916
                pkt->packStr("1");
917
                rsp->putPkt(pkt);
918
        } else if (0 == strcmp("qC", pkt->data)) {
919
                // Return the current thread ID (unsigned hex). A null response
920
                // indicates to use the previously selected thread. We use the constant
921
                // OR1KSIM_TID to represent our single thread of control.
922
                sprintf(pkt->data, "QC%x", OR1KSIM_TID);
923
                pkt->setLen(strlen(pkt->data));
924
                rsp->putPkt(pkt);
925
        } else if (0 == strncmp("qCRC", pkt->data, strlen("qCRC"))) {
926
                // Return CRC of memory area
927
                cerr << "Warning: RSP CRC query not supported" << endl;
928
                pkt->packStr("E01");
929
                rsp->putPkt(pkt);
930
        } else if (0 == strcmp("qfThreadInfo", pkt->data)) {
931
                // Return info about active threads. We return just the constant
932
                // OR1KSIM_TID to represent our single thread of control.
933
                sprintf(pkt->data, "m%x", OR1KSIM_TID);
934
                pkt->setLen(strlen(pkt->data));
935
                rsp->putPkt(pkt);
936
        } else if (0 == strcmp("qsThreadInfo", pkt->data)) {
937
                // Return info about more active threads. We have no more, so return the
938
                // end of list marker, 'l'
939
                pkt->packStr("l");
940
                rsp->putPkt(pkt);
941
        } else if (0 ==
942
                   strncmp("qGetTLSAddr:", pkt->data, strlen("qGetTLSAddr:"))) {
943
                // We don't support this feature
944
                pkt->packStr("");
945
                rsp->putPkt(pkt);
946
        } else if (0 == strncmp("qL", pkt->data, strlen("qL"))) {
947
                // Deprecated and replaced by 'qfThreadInfo'
948
                cerr << "Warning: RSP qL deprecated: no info returned" << endl;
949
                pkt->packStr("qM001");
950
                rsp->putPkt(pkt);
951
        } else if (0 == strcmp("qOffsets", pkt->data)) {
952
                // Report any relocation
953
                pkt->packStr("Text=0;Data=0;Bss=0");
954
                rsp->putPkt(pkt);
955
        } else if (0 == strncmp("qP", pkt->data, strlen("qP"))) {
956
                // Deprecated and replaced by 'qThreadExtraInfo'
957
                cerr << "Warning: RSP qP deprecated: no info returned" << endl;
958
                pkt->packStr("");
959
                rsp->putPkt(pkt);
960
        } else if (0 == strncmp("qRcmd,", pkt->data, strlen("qRcmd,"))) {
961
                // This is used to interface to commands to do "stuff"
962
                rspCommand();
963
        } else if (0 == strncmp("qSupported", pkt->data, strlen("qSupported"))) {
964
                // Report a list of the features we support. For now we just ignore any
965
                // supplied specific feature queries, but in the future these may be
966
                // supported as well. Note that the packet size allows for 'G' + all the
967
                // registers sent to us, or a reply to 'g' with all the registers and an
968
                // EOS so the buffer is a well formed string.
969
                sprintf(pkt->data, "PacketSize=%x", pkt->getBufSize());
970
                pkt->setLen(strlen(pkt->data));
971
                rsp->putPkt(pkt);
972
        } else if (0 == strncmp("qSymbol:", pkt->data, strlen("qSymbol:"))) {
973
                // Offer to look up symbols. Nothing we want (for now). TODO. This just
974
                // ignores any replies to symbols we looked up, but we didn't want to
975
                // do that anyway!
976
                pkt->packStr("OK");
977
                rsp->putPkt(pkt);
978
        } else if (0 == strncmp("qThreadExtraInfo,", pkt->data,
979
                                strlen("qThreadExtraInfo,"))) {
980
                // Report that we are runnable, but the text must be hex ASCI
981
                // digits. For now do this by steam, reusing the original packet
982
                sprintf(pkt->data, "%02x%02x%02x%02x%02x%02x%02x%02x%02x",
983
                        'R', 'u', 'n', 'n', 'a', 'b', 'l', 'e', 0);
984
                pkt->setLen(strlen(pkt->data));
985
                rsp->putPkt(pkt);
986
        } else if (0 == strncmp("qTStatus", pkt->data, strlen("qTstatus"))) {
987
                // We don't support tracing, return empty packet
988
                pkt->packStr("");
989
                rsp->putPkt(pkt);
990
        } else if (0 == strncmp("qXfer:", pkt->data, strlen("qXfer:"))) {
991
                // For now we support no 'qXfer' requests, but these should not be
992
                // expected, since they were not reported by 'qSupported'
993
                cerr << "Warning: RSP 'qXfer' not supported: ignored" << endl;
994
                pkt->packStr("");
995
                rsp->putPkt(pkt);
996
        } else {
997
                cerr << "Unrecognized RSP query: ignored" << endl;
998
        }
999
}                               // rspQuery ()
1000 63 julius
 
1001
//-----------------------------------------------------------------------------
1002
//! Handle a RSP qRcmd request
1003
 
1004
//! The actual command follows the "qRcmd," in ASCII encoded to hex
1005
//-----------------------------------------------------------------------------
1006 462 julius
void GdbServerSC::rspCommand()
1007 63 julius
{
1008 462 julius
        char cmd[RSP_PKT_MAX];
1009 63 julius
 
1010 462 julius
        Utils::hex2Ascii(cmd, &(pkt->data[strlen("qRcmd,")]));
1011 63 julius
 
1012 462 julius
        // Work out which command it is
1013
        if (0 == strncmp("readspr ", cmd, strlen("readspr"))) {
1014
                unsigned int sprNum;
1015 63 julius
 
1016 462 julius
                // Parse and return error if we fail
1017
                if (1 != sscanf(cmd, "readspr %4x", &sprNum)) {
1018
                        cerr << "Warning: qRcmd " << cmd
1019
                            << "not recognized: ignored" << endl;
1020
                        pkt->packStr("E01");
1021
                        rsp->putPkt(pkt);
1022
                        return;
1023
                }
1024
                // SPR out of range
1025
                if (sprNum > MAX_SPRS) {
1026
                        cerr << "Warning: qRcmd readspr " << hex << sprNum
1027
                            << dec << " too large: ignored" << endl;
1028
                        pkt->packStr("E01");
1029
                        rsp->putPkt(pkt);
1030
                        return;
1031
                }
1032
                // Construct the reply
1033
                sprintf(cmd, "%8lx", debugUnit->readSpr(sprNum));
1034
                Utils::ascii2Hex(pkt->data, cmd);
1035
                pkt->setLen(strlen(pkt->data));
1036
                rsp->putPkt(pkt);
1037
        } else if (0 == strncmp("writespr ", cmd, strlen("writespr"))) {
1038
                unsigned int sprNum;
1039
                uint32_t val;
1040 63 julius
 
1041 462 julius
                // Parse and return error if we fail
1042
                if (2 != sscanf(cmd, "writespr %4x %8lx", &sprNum, &val)) {
1043
                        cerr << "Warning: qRcmd " << cmd <<
1044
                            " not recognized: ignored" << endl;
1045
                        pkt->packStr("E01");
1046
                        rsp->putPkt(pkt);
1047
                        return;
1048
                }
1049
                // SPR out of range
1050
                if (sprNum > MAX_SPRS) {
1051
                        cerr << "Warning: qRcmd writespr " << hex << sprNum
1052
                            << dec << " too large: ignored" << endl;
1053
                        pkt->packStr("E01");
1054
                        rsp->putPkt(pkt);
1055
                        return;
1056
                }
1057
                // Update the SPR and reply "OK"
1058
                debugUnit->writeSpr(sprNum, val);
1059
                pkt->packStr("OK");
1060
                rsp->putPkt(pkt);
1061 63 julius
        }
1062
 
1063 462 julius
}                               // rspCommand ()
1064 63 julius
 
1065
//-----------------------------------------------------------------------------
1066
//! Handle a RSP set request
1067
//-----------------------------------------------------------------------------
1068 462 julius
void GdbServerSC::rspSet()
1069 63 julius
{
1070 462 julius
        if (0 == strncmp("QPassSignals:", pkt->data, strlen("QPassSignals:"))) {
1071
                // Passing signals not supported
1072
                pkt->packStr("");
1073
                rsp->putPkt(pkt);
1074
        } else if ((0 == strncmp("QTDP", pkt->data, strlen("QTDP"))) ||
1075
                   (0 == strncmp("QFrame", pkt->data, strlen("QFrame"))) ||
1076
                   (0 == strcmp("QTStart", pkt->data)) ||
1077
                   (0 == strcmp("QTStop", pkt->data)) ||
1078
                   (0 == strcmp("QTinit", pkt->data)) ||
1079
                   (0 == strncmp("QTro", pkt->data, strlen("QTro")))) {
1080
                // All tracepoint features are not supported. This reply is really only
1081
                // needed to 'QTDP', since with that the others should not be
1082
                // generated.
1083
                pkt->packStr("");
1084
                rsp->putPkt(pkt);
1085
        } else {
1086
                cerr << "Unrecognized RSP set request: ignored" << endl;
1087
                delete pkt;
1088
        }
1089
}                               // rspSet ()
1090 63 julius
 
1091
//-----------------------------------------------------------------------------
1092
//! Handle a RSP restart request
1093
 
1094
//! For now we just put the program counter back to the reset vector. If we
1095
//! supported the vRun request, we should use the address specified
1096
//! there. There is no point in unstalling the processor, since we'll never
1097
//! get control back.
1098
//-----------------------------------------------------------------------------
1099 462 julius
void GdbServerSC::rspRestart()
1100 63 julius
{
1101 462 julius
        writeNpc(EXCEPT_RESET);
1102 63 julius
 
1103 462 julius
}                               // rspRestart ()
1104 63 julius
 
1105
//-----------------------------------------------------------------------------
1106
//! Handle a RSP step request
1107
 
1108
//! This version is typically used for the 's' packet, to continue without
1109
//! signal, in which case EXCEPT_NONE is passed in as the exception to use.
1110
 
1111
//! @param[in] except  The exception to use. Only EXCEPT_NONE should be set
1112
//!                    this way.
1113
//-----------------------------------------------------------------------------
1114 462 julius
void GdbServerSC::rspStep(uint32_t except)
1115 63 julius
{
1116 462 julius
        uint32_t addr;          // The address to step from, if any
1117 63 julius
 
1118 462 julius
        // Reject all except 's' packets
1119
        if ('s' != pkt->data[0]) {
1120
                cerr << "Warning: Step with signal not currently supported: "
1121
                    << "ignored" << endl;
1122
                return;
1123
        }
1124 63 julius
 
1125 462 julius
        if (0 == strcmp("s", pkt->data)) {
1126
                addr = readNpc();       // Default uses current NPC
1127
        } else if (1 != sscanf(pkt->data, "s%lx", &addr)) {
1128
                cerr << "Warning: RSP step address " << pkt->data
1129
                    << " not recognized: ignored" << endl;
1130
                addr = readNpc();       // Default uses current NPC
1131
        }
1132 63 julius
 
1133 462 julius
        rspStep(addr, EXCEPT_NONE);
1134 63 julius
 
1135 462 julius
}                               // rspStep ()
1136 63 julius
 
1137
//-----------------------------------------------------------------------------
1138
//! Handle a RSP step with signal request
1139
 
1140
//! @todo Currently null. Will use the underlying generic step function.
1141
//-----------------------------------------------------------------------------
1142 462 julius
void GdbServerSC::rspStep()
1143 63 julius
{
1144 462 julius
        cerr << "RSP step with signal '" << pkt->data << "' received" << endl;
1145 63 julius
 
1146 462 julius
}                               // rspStep ()
1147 63 julius
 
1148
//-----------------------------------------------------------------------------
1149
//! Generic processing of a step request
1150
 
1151
//! The signal may be EXCEPT_NONE if there is no exception to be
1152
//! handled. Currently the exception is ignored.
1153
 
1154
//! The single step flag is set in the debug registers and then the processor
1155
//! is unstalled.
1156
 
1157
//! @todo There appears to be a bug in the ORPSoC debug unit, whereby multiple
1158
//!       single steps make a mess of the pipeline, leading to multiple
1159
//!       executions of the same instruction. A fix would be to use l.trap (as
1160
//!       for continue) for any consecutive calls to step.
1161
 
1162
//! @param[in] addr    Address from which to step
1163
//! @param[in] except  The exception to use (if any)                         
1164
//-----------------------------------------------------------------------------
1165 462 julius
void GdbServerSC::rspStep(uint32_t addr, uint32_t except)
1166 63 julius
{
1167 462 julius
        // Set the address as the value of the next program counter
1168
        writeNpc(addr);
1169 63 julius
 
1170 462 julius
        /*
1171
           // If we're continuing from a breakpoint, replace that instruction in the memory
1172
           // ... actually no, I was wrong about this.
1173
           if (NULL != mpHash->lookup (BP_MEMORY, addr) && rsp_sigval == TARGET_SIGNAL_TRAP)
1174
           {
1175
           MpEntry *entry = mpHash->lookup (BP_MEMORY, addr);
1176
           debugUnit->writeMem32(entry->addr, entry->instr);
1177
           }
1178
         */
1179 63 julius
 
1180 462 julius
        // Clear Debug Reason Register and watchpoint break generation in Debug Mode
1181
        // Register 2 for watchpoints that we triggered to stop this time.
1182
        debugUnit->writeSpr(SPR_DRR, 0);
1183
        if (rsp_sigval == TARGET_SIGNAL_TRAP) {
1184
                /*
1185
                   Disable last trap generation on watchpoint if this is why we stopped
1186
                   last time.
1187
                 */
1188
                uint32_t temp_dmr2 = debugUnit->readSpr(SPR_DMR2);
1189
                if (temp_dmr2 & SPR_DMR2_WBS) {
1190
                        /*
1191
                           One of these breakpoints is responsible for our stopping, so
1192
                           disable it this time we start. GDB will send a packet re-enabling
1193
                           it next time we continue.
1194
                         */
1195
                        debugUnit->writeSpr(SPR_DMR2,
1196
                                            temp_dmr2 &
1197
                                            ~((temp_dmr2 & SPR_DMR2_WBS) >>
1198
                                              10));
1199
                }
1200
        }
1201
        // Set the single step trigger in Debug Mode Register 1 and set traps to be
1202
        // handled by the debug unit in the Debug Stop Register
1203
        debugUnit->orSpr(SPR_DMR1, SPR_DMR1_ST);
1204
        debugUnit->orSpr(SPR_DSR, SPR_DSR_TE);
1205 63 julius
 
1206 462 julius
        // Unstall the processor. Note the GDB client is now waiting for a reply,
1207
        // which it will get as soon as the processor stalls again.
1208
        debugUnit->unstall();
1209
        targetStopped = false;
1210 63 julius
 
1211 462 julius
}                               // rspStep ()
1212 63 julius
 
1213
//-----------------------------------------------------------------------------
1214
//! Handle a RSP 'v' packet
1215
 
1216
//! These are commands associated with executing the code on the target
1217
//-----------------------------------------------------------------------------
1218 462 julius
void GdbServerSC::rspVpkt()
1219 63 julius
{
1220 462 julius
        if (0 == strncmp("vAttach;", pkt->data, strlen("vAttach;"))) {
1221
                // Attaching is a null action, since we have no other process. We just
1222
                // return a stop packet (using TRAP) to indicate we are stopped.
1223
                pkt->packStr("S05");
1224
                rsp->putPkt(pkt);
1225
                return;
1226
        } else if (0 == strcmp("vCont?", pkt->data)) {
1227
                // For now we don't support this.
1228
                pkt->packStr("");
1229
                rsp->putPkt(pkt);
1230
                return;
1231
        } else if (0 == strncmp("vCont", pkt->data, strlen("vCont"))) {
1232
                // This shouldn't happen, because we've reported non-support via vCont?
1233
                // above
1234
                cerr << "Warning: RSP vCont not supported: ignored" << endl;
1235
                return;
1236
        } else if (0 == strncmp("vFile:", pkt->data, strlen("vFile:"))) {
1237
                // For now we don't support this.
1238
                cerr << "Warning: RSP vFile not supported: ignored" << endl;
1239
                pkt->packStr("");
1240
                rsp->putPkt(pkt);
1241
                return;
1242
        } else if (0 ==
1243
                   strncmp("vFlashErase:", pkt->data, strlen("vFlashErase:"))) {
1244
                // For now we don't support this.
1245
                cerr << "Warning: RSP vFlashErase not supported: ignored" <<
1246
                    endl;
1247
                pkt->packStr("E01");
1248
                rsp->putPkt(pkt);
1249
                return;
1250
        } else if (0 ==
1251
                   strncmp("vFlashWrite:", pkt->data, strlen("vFlashWrite:"))) {
1252
                // For now we don't support this.
1253
                cerr << "Warning: RSP vFlashWrite not supported: ignored" <<
1254
                    endl;
1255
                pkt->packStr("E01");
1256
                rsp->putPkt(pkt);
1257
                return;
1258
        } else if (0 == strcmp("vFlashDone", pkt->data)) {
1259
                // For now we don't support this.
1260
                cerr << "Warning: RSP vFlashDone not supported: ignored" <<
1261
                    endl;;
1262
                pkt->packStr("E01");
1263
                rsp->putPkt(pkt);
1264
                return;
1265
        } else if (0 == strncmp("vRun;", pkt->data, strlen("vRun;"))) {
1266
                // We shouldn't be given any args, but check for this
1267
                if (pkt->getLen() > strlen("vRun;")) {
1268
                        cerr << "Warning: Unexpected arguments to RSP vRun "
1269
                            "command: ignored" << endl;
1270
                }
1271
                // Restart the current program. However unlike a "R" packet, "vRun"
1272
                // should behave as though it has just stopped. We use signal 5 (TRAP).
1273
                rspRestart();
1274
                pkt->packStr("S05");
1275
                rsp->putPkt(pkt);
1276
        } else {
1277
                cerr << "Warning: Unknown RSP 'v' packet type " << pkt->data
1278
                    << ": ignored" << endl;
1279
                pkt->packStr("E01");
1280
                rsp->putPkt(pkt);
1281
                return;
1282 63 julius
        }
1283 462 julius
}                               // rspVpkt ()
1284 63 julius
 
1285
//-----------------------------------------------------------------------------
1286
//! Handle a RSP write memory (binary) request
1287
 
1288
//! Syntax is:
1289
 
1290
//!   X<addr>,<length>:
1291
 
1292
//! Followed by the specified number of bytes as raw binary. Response should be
1293
//! "OK" if all copied OK, E<nn> if error <nn> has occurred.
1294
 
1295
//! The length given is the number of bytes to be written. The data buffer has
1296
//! already been unescaped, so will hold this number of bytes.
1297
 
1298
//! The data is in model-endian format, so no transformation is needed.
1299
//-----------------------------------------------------------------------------
1300 462 julius
void GdbServerSC::rspWriteMemBin()
1301 63 julius
{
1302 462 julius
        uint32_t addr;          // Where to write the memory
1303
        int len;                // Number of bytes to write
1304 63 julius
 
1305 462 julius
        if (2 != sscanf(pkt->data, "X%x,%x:", &addr, &len)) {
1306
                cerr <<
1307
                    "Warning: Failed to recognize RSP write memory command: %s"
1308
                    << pkt->data << endl;
1309
                pkt->packStr("E01");
1310
                rsp->putPkt(pkt);
1311
                return;
1312
        }
1313
        // Find the start of the data and "unescape" it. Bindat must be unsigned, or
1314
        // all sorts of horrible sign extensions will happen when val is computed
1315
        // below!
1316
        uint8_t *bindat = (uint8_t *) (memchr(pkt->data, ':',
1317
                                              pkt->getBufSize())) + 1;
1318
        int off = (char *)bindat - pkt->data;
1319
        int newLen = Utils::rspUnescape((char *)bindat, pkt->getLen() - off);
1320 63 julius
 
1321 462 julius
        // Sanity check
1322
        if (newLen != len) {
1323
                int minLen = len < newLen ? len : newLen;
1324 63 julius
 
1325 462 julius
                cerr << "Warning: Write of " << len << " bytes requested, but "
1326
                    << newLen << " bytes supplied. " << minLen <<
1327
                    " will be written" << endl;
1328
                len = minLen;
1329
        }
1330
        // Write the bytes to memory. More efficent to do this in 32-bit chunks
1331
        int startBytes = addr & 0x3;
1332
        int endBytes = (addr + len) & 0x3;
1333 63 julius
 
1334 462 julius
        // First partial word. Access bindat in an endian independent fashion.
1335
        for (off = 0; off < startBytes; off++) {
1336
                if (!debugUnit->writeMem8(addr + off, bindat[off])) {
1337
                        pkt->packStr("E01");
1338
                        rsp->putPkt(pkt);
1339
                        return;
1340
                }
1341 63 julius
        }
1342
 
1343 462 julius
        // The bulk as words. Convert to model endian before writing.
1344
        for (off = startBytes; off < len; off += 4) {
1345
                uint32_t val = *((uint32_t *) (&(bindat[off])));
1346 63 julius
 
1347 462 julius
                if (!debugUnit->writeMem32(addr + off, Utils::htotl(val))) {
1348
                        pkt->packStr("E01");
1349
                        rsp->putPkt(pkt);
1350
                        return;
1351
                }
1352 63 julius
        }
1353
 
1354 462 julius
        // Last partial word. Access bindat in an endian independent fashion.
1355
        for (off = len - endBytes; off < len; off++) {
1356
                uint32_t base = (addr + len) & 0xfffffffc;
1357 63 julius
 
1358 462 julius
                if (!debugUnit->writeMem8(base + off, bindat[off])) {
1359
                        pkt->packStr("E01");
1360
                        rsp->putPkt(pkt);
1361
                        return;
1362
                }
1363 63 julius
        }
1364
 
1365 462 julius
        pkt->packStr("OK");
1366
        rsp->putPkt(pkt);
1367 63 julius
 
1368 462 julius
}                               // rspWriteMemBin ()
1369 63 julius
 
1370
//-----------------------------------------------------------------------------
1371
//! Handle a RSP remove breakpoint or matchpoint request
1372
 
1373
//! For now only memory breakpoints are implemented, which are implemented by
1374
//! substituting a breakpoint at the specified address. The implementation must
1375
//! cope with the possibility of duplicate packets.
1376
 
1377
//! @todo This doesn't work with icache/immu yet
1378
//-----------------------------------------------------------------------------
1379 462 julius
void GdbServerSC::rspRemoveMatchpoint()
1380 63 julius
{
1381 462 julius
        MpType type;            // What sort of matchpoint
1382
        uint32_t addr;          // Address specified
1383
        uint32_t instr;         // Instruction value found
1384
        int len;                // Matchpoint length (not used)
1385 63 julius
 
1386 462 julius
        // Break out the instruction
1387
        if (3 != sscanf(pkt->data, "z%1d,%lx,%1d", (int *)&type, &addr, &len)) {
1388
                cerr << "Warning: RSP matchpoint deletion request not "
1389
                    << "recognized: ignored" << endl;
1390
                pkt->packStr("E01");
1391
                rsp->putPkt(pkt);
1392
                return;
1393 63 julius
        }
1394 462 julius
        // Sanity check that the length is 4
1395
        if (4 != len) {
1396
                cerr << "Warning: RSP matchpoint deletion length " << len
1397
                    << "not valid: 4 assumed" << endl;
1398
                len = 4;
1399 63 julius
        }
1400 462 julius
        // Sort out the type of matchpoint
1401
        switch (type) {
1402
        case BP_MEMORY:
1403
                // Memory breakpoint - replace the original instruction.
1404
                if (mpHash->remove(type, addr, &instr)) {
1405
                        //cerr << "rspRemoveMatchpoint at 0x" << hex << addr << " restoring instruction: 0x" << hex << instr <<endl;
1406
                        debugUnit->writeMem32(addr, instr);
1407
                }
1408 63 julius
 
1409 462 julius
                pkt->packStr("OK");
1410
                rsp->putPkt(pkt);
1411
                return;
1412 63 julius
 
1413 462 julius
        case BP_HARDWARE:
1414
                int off;
1415
                for (off = 0; off < 8; off++)
1416
                        if ((debugUnit->readSpr(SPR_DCR0 + off) == (0x23)) &&
1417
                            (debugUnit->readSpr(SPR_DVR0 + off) == addr))
1418
                                break;
1419
                if (off > 7) {
1420
                        pkt->packStr("E02");    // Failed ot find breakpoint
1421
                        rsp->putPkt(pkt);
1422
                        return;
1423
                }
1424
                // Clear DCR's CT and DVR, WGB bit
1425
                debugUnit->writeSpr(SPR_DCR0 + off, 0);
1426
                debugUnit->writeSpr(SPR_DVR0 + off, 0);
1427
                debugUnit->writeSpr(SPR_DMR2,
1428
                                    debugUnit->readSpr(SPR_DMR2) & ~((1 << off)
1429
                                                                     <<
1430
                                                                     SPR_DMR2_WGB_SHIFT));
1431
                pkt->packStr("OK");
1432
                rsp->putPkt(pkt);
1433
                return;
1434 63 julius
 
1435 462 julius
        case WP_WRITE:
1436
                {
1437
                        int off;
1438
                        for (off = 0; off < 8; off++) {
1439
                                if ((debugUnit->readSpr(SPR_DCR0 + off) ==
1440
                                     (0x63))
1441
                                    && (debugUnit->readSpr(SPR_DVR0 + off) ==
1442
                                        addr))
1443
                                        break;
1444
                        }
1445
                        if (off > 7) {
1446
                                pkt->packStr("E02");    // Failed ot find breakpoint
1447
                                rsp->putPkt(pkt);
1448
                                return;
1449
                        }
1450
                        // Clear DCR's CT and DVR, WGB bit
1451
                        debugUnit->writeSpr(SPR_DCR0 + off, 0);
1452
                        debugUnit->writeSpr(SPR_DVR0 + off, 0);
1453
                        debugUnit->writeSpr(SPR_DMR2,
1454
                                            debugUnit->readSpr(SPR_DMR2) &
1455
                                            ~((1 << off) <<
1456
                                              SPR_DMR2_WGB_SHIFT));
1457
                        pkt->packStr("OK");
1458
                        rsp->putPkt(pkt);
1459
                        return;
1460
                }
1461 63 julius
 
1462 462 julius
        case WP_READ:
1463
                {
1464
                        int off;
1465
                        for (off = 0; off < 8; off++) {
1466
                                if ((debugUnit->readSpr(SPR_DCR0 + off) ==
1467
                                     (0x43))
1468
                                    && (debugUnit->readSpr(SPR_DVR0 + off) ==
1469
                                        addr))
1470
                                        break;
1471
                        }
1472
                        if (off > 7) {
1473
                                pkt->packStr("E02");    // Failed ot find breakpoint
1474
                                rsp->putPkt(pkt);
1475
                                return;
1476
                        }
1477
                        // Clear DCR's CT and DVR, WGB bit
1478
                        debugUnit->writeSpr(SPR_DCR0 + off, 0);
1479
                        debugUnit->writeSpr(SPR_DVR0 + off, 0);
1480
                        debugUnit->writeSpr(SPR_DMR2,
1481
                                            debugUnit->readSpr(SPR_DMR2) &
1482
                                            ~((1 << off) <<
1483
                                              SPR_DMR2_WGB_SHIFT));
1484
                        pkt->packStr("OK");
1485
                        rsp->putPkt(pkt);
1486
                        return;
1487
                }
1488 63 julius
 
1489 462 julius
        case WP_ACCESS:
1490
                {
1491
                        int off;
1492
                        for (off = 0; off < 8; off++) {
1493
                                //printf("WP_ACCESS remove check off=%d DCR=0x%.8x DVR=0x%.8x\n",
1494
                                //off,debugUnit->readSpr (SPR_DCR0+off),debugUnit->readSpr (SPR_DVR0+off));
1495
                                if ((debugUnit->readSpr(SPR_DCR0 + off) ==
1496
                                     (0xc3))
1497
                                    && (debugUnit->readSpr(SPR_DVR0 + off) ==
1498
                                        addr))
1499
                                        break;
1500
                        }
1501
                        if (off > 7) {
1502
                                //printf("rspRemoveWatchpoint: WP_ACCESS remove ERROR, regpair %d for 0x%.8x\n",off, addr);
1503
                                pkt->packStr("E02");    // Failed ot find breakpoint
1504
                                rsp->putPkt(pkt);
1505
                                return;
1506
                        }
1507
                        //printf("rspRemoveWatchpoint: WP_ACCESS remove, regpair %d for 0x%.8x\n",off, addr);
1508
 
1509
                        // Clear DCR's CT and DVR, WGB bit
1510
                        debugUnit->writeSpr(SPR_DCR0 + off, 0);
1511
                        debugUnit->writeSpr(SPR_DVR0 + off, 0);
1512
                        debugUnit->writeSpr(SPR_DMR2,
1513
                                            debugUnit->readSpr(SPR_DMR2) &
1514
                                            ~((1 << off) <<
1515
                                              SPR_DMR2_WGB_SHIFT));
1516
                        pkt->packStr("OK");
1517
                        rsp->putPkt(pkt);
1518
                        return;
1519
                }
1520
        default:
1521
                cerr << "Warning: RSP matchpoint type " << type
1522
                    << " not recognized: ignored" << endl;
1523
                pkt->packStr("E01");
1524
                rsp->putPkt(pkt);
1525
                return;
1526
        }
1527
}                               // rspRemoveMatchpoint ()
1528
 
1529 63 julius
//---------------------------------------------------------------------------*/
1530
//! Handle a RSP insert breakpoint or matchpoint request
1531
 
1532
//! For now only memory breakpoints are implemented, which are implemented by
1533
//! substituting a breakpoint at the specified address. The implementation must
1534
//! cope with the possibility of duplicate packets.
1535
 
1536
//! @todo This doesn't work with icache/immu yet
1537
//---------------------------------------------------------------------------*/
1538 462 julius
void GdbServerSC::rspInsertMatchpoint()
1539 63 julius
{
1540 462 julius
        MpType type;            // What sort of matchpoint
1541
        uint32_t addr;          // Address specified
1542
        int len;                // Matchpoint length (not used)
1543 63 julius
 
1544 462 julius
        // Break out the instruction
1545
        if (3 != sscanf(pkt->data, "Z%1d,%lx,%1d", (int *)&type, &addr, &len)) {
1546
                cerr << "Warning: RSP matchpoint insertion request not "
1547
                    << "recognized: ignored" << endl;
1548
                pkt->packStr("E01");
1549
                rsp->putPkt(pkt);
1550
                return;
1551
        }
1552
        // Sanity check that the length is 4
1553
        if (4 != len) {
1554
                cerr << "Warning: RSP matchpoint insertion length " << len
1555
                    << "not valid: 4 assumed" << endl;
1556
                len = 4;
1557
        }
1558
        // Sort out the type of matchpoint
1559
        switch (type) {
1560
        case BP_MEMORY:
1561
                // Memory breakpoint - substitute a TRAP instruction
1562
                mpHash->add(type, addr, debugUnit->readMem32(addr));
1563
                debugUnit->writeMem32(addr, OR1K_TRAP_INSTR);
1564
                pkt->packStr("OK");
1565
                rsp->putPkt(pkt);
1566
                return;
1567 63 julius
 
1568 462 julius
        case BP_HARDWARE:
1569
                {
1570
                        int off;
1571
                        for (off = 0; off < 8; off++)
1572
                                if (!
1573
                                    (debugUnit->readSpr(SPR_DCR0 + off) &
1574
                                     SPR_DCR_CT_MASK))
1575
                                        break;
1576
                        if (off > 7) {
1577
                                pkt->packStr("");       // No room
1578
                                rsp->putPkt(pkt);
1579
                                return;
1580
                        }
1581
                        // CC = equal, CT = Instruction fetch EA, set WGB bit
1582
                        debugUnit->writeSpr(SPR_DCR0 + off, 0x22);
1583
                        debugUnit->writeSpr(SPR_DVR0 + off, addr);
1584
                        debugUnit->writeSpr(SPR_DMR2,
1585
                                            debugUnit->readSpr(SPR_DMR2) |
1586
                                            ((1 << off) << SPR_DMR2_WGB_SHIFT));
1587
                        pkt->packStr("OK");
1588
                        rsp->putPkt(pkt);
1589
                        return;
1590
                }
1591 63 julius
 
1592 462 julius
        case WP_WRITE:
1593
                {
1594
                        int off;
1595
                        for (off = 0; off < 8; off++)
1596
                                if (!
1597
                                    (debugUnit->readSpr(SPR_DCR0 + off) &
1598
                                     SPR_DCR_CT_MASK))
1599
                                        break;
1600
                        //printf("rspInsertWatchpoint: WP_WRITE, regpair %d for 0x%.8x\n",off, addr);
1601
                        if (off > 7) {
1602
                                pkt->packStr("");       // No room
1603
                                rsp->putPkt(pkt);
1604
                                return;
1605
                        }
1606
                        // CC = equal, CT = Store EA, set WGB bit
1607
                        debugUnit->writeSpr(SPR_DCR0 + off, 0x62);
1608
                        debugUnit->writeSpr(SPR_DVR0 + off, addr);
1609
                        debugUnit->writeSpr(SPR_DMR2,
1610
                                            debugUnit->readSpr(SPR_DMR2) |
1611
                                            ((1 << off) << SPR_DMR2_WGB_SHIFT));
1612
                        pkt->packStr("OK");
1613
                        rsp->putPkt(pkt);
1614
                        return;
1615
                }
1616 63 julius
 
1617 462 julius
        case WP_READ:
1618
                {
1619
                        int off;
1620
                        for (off = 0; off < 8; off++)
1621
                                if (!
1622
                                    (debugUnit->readSpr(SPR_DCR0 + off) &
1623
                                     SPR_DCR_CT_MASK))
1624
                                        break;
1625
                        //printf("rspInsertWatchpoint: WP_WRITE, regpair %d for 0x%.8x\n",off, addr);
1626
                        if (off > 7) {
1627
                                pkt->packStr("");       // No room
1628
                                rsp->putPkt(pkt);
1629
                                return;
1630
                        }
1631
                        // CC = equal, CT = Load EA, set WGB bit
1632
                        debugUnit->writeSpr(SPR_DCR0 + off, 0x42);
1633
                        debugUnit->writeSpr(SPR_DVR0 + off, addr);
1634
                        debugUnit->writeSpr(SPR_DMR2,
1635
                                            debugUnit->readSpr(SPR_DMR2) |
1636
                                            ((1 << off) << SPR_DMR2_WGB_SHIFT));
1637
                        pkt->packStr("OK");
1638
                        rsp->putPkt(pkt);
1639
                        return;
1640
                }
1641 63 julius
 
1642 462 julius
                pkt->packStr("");       // Not supported
1643
                rsp->putPkt(pkt);
1644
                return;
1645 63 julius
 
1646 462 julius
        case WP_ACCESS:
1647
                {
1648
                        int off;
1649
                        for (off = 0; off < 8; off++)
1650
                                if (!
1651
                                    (debugUnit->readSpr(SPR_DCR0 + off) &
1652
                                     SPR_DCR_CT_MASK))
1653
                                        break;
1654
                        //printf("rspInsertWatchpoint: WP_ACCESS, regpair %d for 0x%.8x\n",off, addr);
1655
                        if (off > 7) {
1656
                                pkt->packStr("");       // No room
1657
                                rsp->putPkt(pkt);
1658
                                return;
1659
                        }
1660
                        // CC = equal, CT = Load/Store EA, set WGB bit
1661
                        debugUnit->writeSpr(SPR_DCR0 + off, 0xc2);
1662
                        debugUnit->writeSpr(SPR_DVR0 + off, addr);
1663
                        debugUnit->writeSpr(SPR_DMR2,
1664
                                            debugUnit->readSpr(SPR_DMR2) |
1665
                                            ((1 << off) << SPR_DMR2_WGB_SHIFT));
1666
                        pkt->packStr("OK");
1667
                        rsp->putPkt(pkt);
1668
                        return;
1669
                }
1670 63 julius
 
1671 462 julius
        default:
1672
                cerr << "Warning: RSP matchpoint type " << type
1673
                    << "not recognized: ignored" << endl;
1674
                pkt->packStr("E01");
1675
                rsp->putPkt(pkt);
1676
                return;
1677
        }
1678
}                               // rspInsertMatchpoint ()
1679 63 julius
 
1680
//-----------------------------------------------------------------------------
1681
//! Read the value of the Next Program Counter (a SPR)
1682
 
1683
//! A convenience routine.
1684
 
1685
//! Setting the NPC flushes the pipeline, so subsequent reads will return
1686
//! zero until the processor has refilled the pipeline. This will not be
1687
//! happening if the processor is stalled (as it is when GDB had control).
1688
 
1689
//! However for debugging we always want to know what the effective value of
1690
//! the NPC will be (i.e. the value that will be used once the pipeline has
1691
//! refilled). Fortunately SPR cacheing in the debug unit silently solves this
1692
//! for us.
1693
 
1694
//! @return  The value of the NPC
1695
//-----------------------------------------------------------------------------
1696 462 julius
uint32_t GdbServerSC::readNpc()
1697 63 julius
{
1698 462 julius
        return debugUnit->readSpr(SPR_NPC);
1699 63 julius
 
1700 462 julius
}                               // readNpc ()
1701 63 julius
 
1702
//-----------------------------------------------------------------------------
1703
//! Write the value of the Next Program Counter (a SPR)
1704
 
1705
//! A convenience function.
1706
 
1707
//! Setting the NPC flushes the pipeline, so subsequent reads will return
1708
//! zero until the processor has refilled the pipeline. This will not be
1709
//! happening if the processor is stalled (as it is when GDB had control).
1710
 
1711
//! However for debugging we always want to know what the effective value of
1712
//! the NPC will be (i.e. the value that will be used once the pipeline has
1713
//! refilled). Fortunately SPR cacheing in the debug unit silently solves this
1714
//! for us.
1715
 
1716
//! There is one other caveat for the NPC. We do not wish to write it (whether
1717
//! or not it is cached) if it has not changed. So unlike all other SPRs we
1718
//! always read it first before writing.
1719
 
1720
//! @param[in]  The address to write into the NPC
1721
//-----------------------------------------------------------------------------
1722 462 julius
void GdbServerSC::writeNpc(uint32_t addr)
1723 63 julius
{
1724 462 julius
        if (addr != readNpc()) {
1725
                debugUnit->writeSpr(SPR_NPC, addr);
1726
        }
1727
}                               // writeNpc ()
1728 63 julius
 
1729
//-----------------------------------------------------------------------------
1730
//! Read the value of an OpenRISC 1000 General Purpose Register
1731
 
1732
//! A convenience function. This is just a wrapper for reading a SPR, since
1733
//! the GPR's are mapped into SPR space
1734
 
1735
//! @param[in]  regNum  The GPR to read
1736
 
1737
//! @return  The value of the GPR
1738
//-----------------------------------------------------------------------------
1739 462 julius
uint32_t GdbServerSC::readGpr(int regNum)
1740 63 julius
{
1741 462 julius
        return debugUnit->readSpr(SPR_GPR0 + regNum);
1742 63 julius
 
1743 462 julius
}                               // readGpr ()
1744 63 julius
 
1745
//-----------------------------------------------------------------------------
1746
//! Write the value of an OpenRISC 1000 General Purpose Register
1747
 
1748
//! A convenience function. This is just a wrapper for writing a SPR, since
1749
//! the GPR's are mapped into SPR space
1750
 
1751
//! @param[in]  regNum  The GPR to read
1752
 
1753
//! @return  The value of the GPR
1754
//-----------------------------------------------------------------------------
1755 462 julius
void GdbServerSC::writeGpr(int regNum, uint32_t value)
1756 63 julius
{
1757 462 julius
        debugUnit->writeSpr(SPR_GPR0 + regNum, value);
1758 63 julius
 
1759 462 julius
}                               // writeGpr ()
1760 63 julius
 
1761
//-----------------------------------------------------------------------------
1762
//! Check if we received anything via the pipe from the or1200 monitor
1763
 
1764
//! We stall the processor, and behave in a manner similar to if an interrupt
1765
//! had been received. Perhaps the sigval should be set differently/more
1766
//! more appropriately.
1767
//! Read from the pipe should be NON-blocking.
1768
 
1769
//! @return  false if nothing received, else true
1770
//-----------------------------------------------------------------------------
1771 462 julius
bool GdbServerSC::checkMonitorPipe()
1772 63 julius
{
1773 462 julius
        char readChar;
1774
        int n = read(monitor_to_gdb_pipe[0][0], &readChar, sizeof(char));
1775
        if (!(((n < 0) && (errno == EAGAIN)) || (n == 0)) && !targetStopped) {
1776
                debugUnit->stall();
1777
                // Send a stop reply response, manually set rsp.sigval to TARGET_SIGNAL_NONE
1778
                rsp_sigval = TARGET_SIGNAL_NONE;
1779
                rspReportException();
1780
                targetStopped = true;   // Processor now not running
1781
                write(monitor_to_gdb_pipe[1][1], &readChar, sizeof(char));
1782
                return true;
1783
        }
1784
 
1785
        return false;
1786
 
1787
}                               // checkMonitorPipe ()

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.