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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-6.8/] [gdb/] [remote-or1k.c] - Blame information for rev 178

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 24 jeremybenn
/* Remote debugging interface for various or1k debugging protocols.
2
   Currently supported or1k targets are: simulator, jtag, dummy.
3
 
4
   Copyright 1993-1995, 2000 Free Software Foundation, Inc.
5
   Copyright 2008 Embecosm Limited
6
 
7
   Contributed by Cygnus Support.  Written by Marko Mlinar
8
   <markom@opencores.org>
9
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
10
 
11
   This file is part of GDB.
12
 
13
   This program is free software; you can redistribute it and/or modify it
14
   under the terms of the GNU General Public License as published by the Free
15
   Software Foundation; either version 3 of the License, or (at your option)
16
   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 General Public License for
21
   more details.
22
 
23
   You should have received a copy of the GNU General Public License along
24
   with this program.  If not, see <http://www.gnu.org/licenses/>.
25
*/
26
 
27
/*--------------------------------------------------------------------------*/
28
/*!Updated for GDB 6.8 by Jeremy Bennett. All code converted to ANSI C style
29
   and in general to GDB format. All global OpenRISC specific functions and
30
   variables should now have a prefix of or1k_ or OR1K_.
31
 
32
   This original code defined three possible remote targets "jtag", "sim" and
33
   "dummy", all using the OpenRISC 1000 remote protocol. However only "jtag"
34
   is actually implemented, and in this version, all the redundant code is
35
   stripped out. The intention is that in time all remote debugging will be
36
   via the Remote Serial Protocol.
37
 
38
   Commenting compatible with Doxygen added throughout. */
39
/*---------------------------------------------------------------------------*/
40
 
41
 
42
#include "defs.h"
43
#include "inferior.h"
44
#include "bfd.h"
45
#include "symfile.h"
46
#include "gdb_wait.h"
47
#include "gdbcmd.h"
48
#include "gdbcore.h"
49
#include "target.h"
50
#include "gdb_string.h"
51
#include "event-loop.h"
52
#include "event-top.h"
53
#include "inf-loop.h"
54
#include "regcache.h"
55
 
56
#include <signal.h>
57
#include <sys/types.h>
58
#include <sys/stat.h>
59
#include <sys/ioctl.h>
60
#include <fcntl.h>
61
 
62
#include "or1k-tdep.h"
63
#include "or1k-jtag.h"
64
#include "exceptions.h"
65
#include "frame.h"
66
#include "opcode/or32.h"
67
#include "solib.h"
68
 
69
#include <arpa/inet.h>
70
 
71
 
72
/*! The current OR1K target */
73
static struct target_ops  or1k_target;
74
 
75
/*! Are we single stepping */
76
static int  or1k_is_single_stepping;
77
 
78
/*! Cached OR1K debug register values (ignores counters for now). */
79
static struct {
80
  unsigned long int  dvr[OR1K_MAX_MATCHPOINTS];
81
  struct {
82
    enum {
83
      OR1K_CT_DISABLED = 0,              /* Disabled */
84
      OR1K_CT_FETCH    = 1,             /* Compare to fetch EA */
85
      OR1K_CT_LEA      = 2,             /* Compare to load EA */
86
      OR1K_CT_SEA      = 3,             /* Compare to store EA */
87
      OR1K_CT_LDATA    = 4,             /* Compare to load data */
88
      OR1K_CT_SDATA    = 5,             /* Compare to store data */
89
      OR1K_CT_AEA      = 6,             /* Compare to load/store EA */
90
      OR1K_CT_ADATA    = 7              /* Compare to load/store data */
91
    }    ct;                            /* Compare to what? */
92
    int  sc;                            /* Signed comparision */
93
    enum {
94
      OR1K_CC_MASKED   = 0,
95
      OR1K_CC_EQ       = 1,
96
      OR1K_CC_LT       = 2,
97
      OR1K_CC_LE       = 3,
98
      OR1K_CC_GT       = 4,
99
      OR1K_CC_GE       = 5,
100
      OR1K_CC_NE       = 6,
101
      OR1K_CC_RESERVED = 7
102
    }    cc;                            /* Compare operation */
103
    int  dp;                            /* DVR/DCP present */
104
  }                  dcr[OR1K_MAX_MATCHPOINTS];
105
  unsigned long int  dmr1;
106
  unsigned long int  dmr2;
107
  unsigned long int  dsr;
108
  unsigned long int  drr;
109
} or1k_dbgcache;
110
 
111
/*! Old SIGINT handler.  */
112
static void (*or1k_old_intr_handler) (int) = NULL;
113
 
114
/* Forward declaration of global functions to access SPRs. */
115
 
116
ULONGEST  or1k_read_spr (unsigned int  regnum);
117
void      or1k_write_spr (unsigned int  regnum,
118
                          ULONGEST      data);
119
 
120
/* Forward declaration of support functions to handle user interrupt */
121
 
122
static void  or1k_interrupt_query();
123
static void  or1k_interrupt (int signo);
124
static void  or1k_interrupt_twice (int signo);
125
 
126
/* Forward declaration of support functions to handle breakpoints */
127
 
128
static unsigned char  or1k_gdb_to_dcr_type (enum target_hw_bp_type  gdb_type);
129
static int            or1k_set_breakpoint (CORE_ADDR  addr);
130
static int            or1k_clear_breakpoint (CORE_ADDR  addr);
131
static int            or1k_watchpoint_gc ();
132
static int            or1k_stopped_watchpoint_info (CORE_ADDR *addr_p,
133
                                                    int       *mp_p);
134
 
135
/* Forward declarations of support functions for the remote operations */
136
 
137
static int        or1k_regnum_to_sprnum (int regnum);
138
static void       or1k_commit_debug_registers();
139
static void       or1k_start_remote (struct ui_out *uiout,
140
                                     void          *arg);
141
 
142
/* Forward declarations of functions for the remote operations */
143
 
144
static void     or1k_files_info (struct target_ops *target);
145
static void     or1k_open (char *name,
146
                           int   from_tty);
147
static void     or1k_close (int quitting);
148
static void     or1k_detach (char *args,
149
                             int   from_tty);
150
static void     or1k_fetch_registers (struct regcache *regcache,
151
                                      int              regnum);
152
static void     or1k_store_registers (struct regcache *regcache,
153
                                      int              regnum);
154
static void     or1k_prepare_to_store (struct regcache *regcache);
155
static LONGEST  or1k_xfer_partial (struct target_ops  *ops,
156
                                   enum target_object  object,
157
                                   const char         *annex,
158
                                   gdb_byte           *readbuf,
159
                                   const gdb_byte     *writebuf,
160
                                   ULONGEST            offset,
161
                                   LONGEST             len);
162
static int      or1k_insert_breakpoint (struct bp_target_info *bpi);
163
static int      or1k_remove_breakpoint (struct bp_target_info *bpi);
164
static int      or1k_can_use_hw_matchpoint (int  type,
165
                                            int  count,
166
                                            int  othertype);
167
static int      or1k_insert_hw_breakpoint (struct bp_target_info *bpi);
168
static int      or1k_remove_hw_breakpoint (struct bp_target_info *bpi);
169
static int      or1k_insert_watchpoint (CORE_ADDR  addr,
170
                                        int        len,
171
                                        int        type);
172
static int      or1k_remove_watchpoint (CORE_ADDR  addr,
173
                                        int        len,
174
                                        int        type);
175
static int      or1k_stopped_by_watchpoint();
176
static int      or1k_stopped_data_address (struct target_ops *target,
177
                                           CORE_ADDR         *addr_p);
178
static int      or1k_region_ok_for_hw_watchpoint (CORE_ADDR  addr,
179
                                                  int        len);
180
static void     or1k_resume (ptid_t              ptid,
181
                             int                 step,
182
                             enum target_signal  sig);
183
static ptid_t   or1k_wait (ptid_t                    remote_ptid,
184
                           struct target_waitstatus *status);
185
static void     or1k_stop ();
186
static void     or1k_kill ();
187
static void     or1k_create_inferior (char  *execfile,
188
                                      char  *args,
189
                                      char **env,
190
                                      int    from_tty);
191
static void     or1k_mourn_inferior ();
192
static void     or1k_rcmd (char           *command,
193
                           struct ui_file *outbuf);
194
 
195
 
196
/*---------------------------------------------------------------------------*/
197
/*!Ask the user about an interrupt
198
 
199
  Ctrl-C has been received. */
200
/*---------------------------------------------------------------------------*/
201
 
202
static void
203
or1k_interrupt_query()
204
{
205
  target_terminal_ours ();
206
 
207
  if (query ("OpenRISC remote JTAG interrupted while waiting for the program\n"
208
             "Give up (and stop debugging it)? "))
209
    {
210
      const struct gdb_exception e = {
211
        .reason  = RETURN_QUIT,
212
        .error   = GENERIC_ERROR,
213
        .message = "OpenRISC remote JTAG debugging interrupted"
214
      };
215
 
216
      or1k_mourn_inferior ();
217
      throw_exception (e);
218
    }
219
 
220
  target_terminal_inferior ();
221
 
222
}       /* or1k_interrupt_query() */
223
 
224
 
225
/*---------------------------------------------------------------------------*/
226
/*!The command line interface's stop routine from an interrupt
227
 
228
   Attempt to stop the processor. Set a more severe interrupt routine, so that
229
   a second ctrl-C will force more aggressive behavior.
230
 
231
   @param[in] signo  The signal which triggered this handle (always SIGINT) */
232
/*---------------------------------------------------------------------------*/
233
 
234
static void
235
or1k_interrupt (int signo)
236
{
237
  quit_flag = 1;                /* For passive stops */
238
  or1k_stop ();                 /* Actively stall the processor */
239
 
240
  /* If this doesn't work, try more severe steps. */
241
  signal (signo, or1k_interrupt_twice);
242
 
243
}       /* or1k_interrupt() */
244
 
245
 
246
/*---------------------------------------------------------------------------*/
247
/*!More aggressive interrupt handler
248
 
249
   The user typed ^C twice. We ask if they want to kill everything. If they
250
   don't we put this signal handler back in place, so another ctrl-C will
251
   bring us back heer.
252
 
253
   @param[in] signo  The signal which triggered this handle (always SIGINT) */
254
/*---------------------------------------------------------------------------*/
255
 
256
static void
257
or1k_interrupt_twice (int signo)
258
{
259
  /* Restore the old interrupt handler */
260
  if (NULL != or1k_old_intr_handler)
261
    {
262
      signal (signo, or1k_old_intr_handler);
263
    }
264
 
265
  or1k_interrupt_query();
266
 
267
  /* If we carry on keep this as the signal handler */
268
  signal (signo, or1k_interrupt_twice);
269
 
270
}       /* or1k_interrupt_twice() */
271
 
272
 
273
/*---------------------------------------------------------------------------*/
274
/*!Translate GDB watchpoint type into data control register compare to bits
275
 
276
   @param[in] gdb_type  GDB watchpoint type
277
 
278
   @return  The corresponding data control register compare to bits */
279
/*---------------------------------------------------------------------------*/
280
 
281
static unsigned char
282
or1k_gdb_to_dcr_type (enum target_hw_bp_type  gdb_type)
283
{
284
  switch (gdb_type)
285
    {
286
    case hw_write:  return  OR1K_CT_SEA;
287
    case hw_read:   return  OR1K_CT_LEA;
288
    case hw_access: return  OR1K_CT_AEA;
289
 
290
    default:
291
      error  ("or1k_gdb_to_dcr_type: Invalid type %d\n", gdb_type );
292
      return  -1;
293
    }
294
}       /* or1k_gdb_to_dcr_type */
295
 
296
 
297
/*---------------------------------------------------------------------------*/
298
/*!Find the first free matchpoint
299
 
300
  @return  The first free matchpoint, or -1 if none is available. */
301
/*---------------------------------------------------------------------------*/
302
 
303
static int
304
or1k_first_free_matchpoint ()
305
{
306
  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
307
  int i;
308
 
309
  /* Search for unused matchpoint.  */
310
  for (i = 0; i < tdep->num_matchpoints; i++)
311
    {
312
      if (!or1k_dbgcache.dcr[i].dp)
313
        {
314
          return  i;
315
        }
316
    }
317
 
318
  return  -1;
319
 
320
}       /* or1k_first_free_matchpoint() */
321
 
322
 
323
/*---------------------------------------------------------------------------*/
324
/*!Set a breakpoint.
325
 
326
   @param[in] addr  The address at which to set the breakpoint
327
 
328
   @return  0 if the breakpoint was set, non-zero otherwise */
329
/*---------------------------------------------------------------------------*/
330
 
331
static int
332
or1k_set_breakpoint (CORE_ADDR  addr)
333
{
334
  int mp = or1k_first_free_matchpoint();
335
 
336
  if (mp < 0)
337
    {
338
      return  1;
339
    }
340
 
341
  /* Set the value register to the address where we should breakpoint and the
342
     control register for unsigned match to the fetched effective address. */
343
  or1k_dbgcache.dvr[mp]    = addr;
344
 
345
  or1k_dbgcache.dcr[mp].dp = 1;
346
  or1k_dbgcache.dcr[mp].cc = OR1K_CC_EQ;
347
  or1k_dbgcache.dcr[mp].sc = 0;
348
  or1k_dbgcache.dcr[mp].ct = OR1K_CT_FETCH;
349
 
350
  /* No chaining here. Watchpoint triggers a break */
351
  or1k_dbgcache.dmr1 &= ~(OR1K_DMR1_CW << (OR1K_DMR1_CW_SZ * mp));
352
  or1k_dbgcache.dmr2 |= (1 << (mp + OR1K_DMR2_WGB_OFF));
353
 
354
  return 0;
355
 
356
}       /* or1k_set_breakpoint() */
357
 
358
 
359
/*---------------------------------------------------------------------------*/
360
/*!See if a matchpoint has the given qualities
361
 
362
   The fields in the matchpoint DVR and DCR registers must match and the
363
   matchpoint must be in use.
364
 
365
   @param[in] mp    The matchpoint of interest
366
   @param[in] addr  The address to compare
367
   @param[in] cc    The condition code to compare
368
   @param[in] sc    The signedness to compare
369
   @param[in] ct    The comparision type to compare
370
 
371
   @return  1 (true) if the fields are the same and the matchpoint is in use */
372
/*---------------------------------------------------------------------------*/
373
 
374
static int
375
or1k_matchpoint_equal (int            mp,
376
                       CORE_ADDR      addr,
377
                       unsigned char  cc,
378
                       unsigned char  sc,
379
                       unsigned char  ct)
380
{
381
  int res =  or1k_dbgcache.dcr[mp].dp          &&
382
            (or1k_dbgcache.dcr[mp].cc == cc  ) &&
383
            (or1k_dbgcache.dcr[mp].sc == sc  ) &&
384
            (or1k_dbgcache.dcr[mp].ct == ct  ) &&
385
            (or1k_dbgcache.dvr[mp]    == addr);
386
 
387
  return  res;
388
 
389
}       /* or1k_matchpoint_equal() */
390
 
391
 
392
/*---------------------------------------------------------------------------*/
393
/*!Clear a breakpoint.
394
 
395
   @param[in] addr  The address at which to clear the breakpoint
396
 
397
   @return  0 if the breakpoint was cleared, non-zero otherwise */
398
/*---------------------------------------------------------------------------*/
399
 
400
static int
401
or1k_clear_breakpoint (CORE_ADDR  addr)
402
{
403
  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
404
  int                  mp;
405
 
406
  /* Search for matching breakpoint.  */
407
  for (mp = 0; mp < tdep->num_matchpoints; mp++)
408
    {
409
      if (or1k_matchpoint_equal (mp, addr, OR1K_CC_EQ, 0, OR1K_CT_FETCH))
410
        {
411
          break;
412
        }
413
    }
414
 
415
  if (mp >= tdep->num_matchpoints)
416
    {
417
      return 1;
418
    }
419
 
420
  /* Mark the matchpoint unused and clear its bits in the assign to counter,
421
     watchpoint generating break and breakpoint status bits in DMR2. */
422
  or1k_dbgcache.dcr[mp].dp = 0;
423
 
424
  or1k_dbgcache.dmr2 &= ~(1 << (mp + OR1K_DMR2_AWTC_OFF));
425
  or1k_dbgcache.dmr2 &= ~(1 << (mp + OR1K_DMR2_WGB_OFF));
426
  or1k_dbgcache.dmr2 &= ~(1 << (mp + OR1K_DMR2_WBS_OFF));
427
 
428
  return 0;
429
 
430
}       /* or1k_clear_breakpoint() */
431
 
432
 
433
/*---------------------------------------------------------------------------*/
434
/*!Garbage collect the HW watchpoints
435
 
436
   Some functions require more than one OpenRISC 1000 HW watchpoint to be
437
   chained together. Chained wathcpoints must be adjacent, but setting of
438
   breakpoints (which uses a single HW matchpoint/watchpoint) can lead to
439
   fragmentation of the watchpoint list.
440
 
441
   This function allows the currently used matchpoints to be shuffled up. In
442
   other words we have a GARBAGE COLLECTOR (omninous music, thunder
443
   clouds gather, lightning flashes).
444
 
445
   For added fun, watchpoints 8 and 9 cannot be moved, and hence nothing that
446
   depends on them.
447
 
448
   @return  The number of free watchpoints */
449
/*---------------------------------------------------------------------------*/
450
 
451
static int
452
or1k_watchpoint_gc ()
453
{
454
  struct gdbarch_tdep *tdep          = gdbarch_tdep (current_gdbarch);
455
  unsigned long int    bits;
456
  int                  first_free;
457
  int                  first_used;
458
  int                  first_fixed;
459
 
460
  /* Find the last moveable watchpoint by starting from the top and working
461
     down until we find a point where the watchpoint is not dependent on its
462
     predecessor. */
463
 
464
  for (first_fixed = tdep->num_matchpoints; first_fixed > 0; first_fixed--)
465
    {
466
      bits = (or1k_dbgcache.dmr1 >> (OR1K_DMR1_CW_SZ * first_fixed)) &
467
             OR1K_DMR1_CW;
468
 
469
      if ((OR1K_DMR1_CW_AND != bits) && (OR1K_DMR1_CW_OR != bits))
470
        {
471
          break;                /* Chain is broken */
472
        }
473
    }
474
 
475
  /* Give up if there aren't two to be considered */
476
  if (first_fixed < 2)
477
    {
478
      return  first_fixed;
479
    }
480
 
481
  /* Move matchpoints, and hence unused HW watchpoints to the top (i.e to
482
     first_fixed - 1). If we move a matchpoint, then all the HW watchpoints
483
     above must be shuffled down. Although HW watchpoints refer to
484
     matchpoints, it is a fixed reference based on index. So long as the HW
485
     watchpoint and the matchpoint are moved together, and the ordering
486
     remains unchanged all will be OK. Any chained watchpoints will be
487
     adjacent, and will remain adjacent after this exercise. */
488
 
489
  first_free = 0;
490
  first_used = 0;
491
 
492
  while (1)
493
    {
494
      /* Find the first free matchpoint */
495
      for( ; first_free < first_fixed; first_free++ )
496
        {
497
          if (!(or1k_dbgcache.dcr[first_free].dp))
498
            {
499
              break;
500
            }
501
        }
502
 
503
      if (first_free > (first_fixed - 2))
504
        {
505
          return first_fixed - first_free;      /* No more to move */
506
        }
507
 
508
      /* Find the first fixed breakpoint */
509
      for (first_used = first_free + 1; first_used < first_fixed; first_used++)
510
        {
511
          if (or1k_dbgcache.dcr[first_used].dp)
512
            {
513
              break;
514
            }
515
        }
516
 
517
      if (first_used >  (first_fixed - 1))
518
        {
519
          return first_fixed - first_free;      /* No more to move */
520
        }
521
 
522
      /* Move matchpoint in DVR and DCR registers. */
523
      or1k_dbgcache.dvr[first_free]    = or1k_dbgcache.dvr[first_used];
524
      or1k_dbgcache.dcr[first_free]    = or1k_dbgcache.dcr[first_used];
525
      or1k_dbgcache.dcr[first_used].dp = 0;
526
 
527
      /* Copy chaining bits in DMR1.  */
528
      bits = (or1k_dbgcache.dmr1 >> (OR1K_DMR1_CW_SZ * first_used)) &
529
             OR1K_DMR1_CW;
530
      or1k_dbgcache.dmr1 &= ~(OR1K_DMR1_CW << (OR1K_DMR1_CW_SZ * first_used));
531
      or1k_dbgcache.dmr1 &= ~(OR1K_DMR1_CW << (OR1K_DMR1_CW_SZ * first_free));
532
      or1k_dbgcache.dmr1 |=  (bits         << (OR1K_DMR1_CW_SZ * first_free));
533
 
534
      /* Copy assign to counter, watchpoint generating break and breakpoint
535
         status bits in DMR2 */
536
 
537
      bits = (or1k_dbgcache.dmr2 >> (OR1K_DMR2_AWTC_OFF + first_used)) & 1;
538
      or1k_dbgcache.dmr2 &= ~(1    << (OR1K_DMR2_AWTC_OFF + first_used));
539
      or1k_dbgcache.dmr2 &= ~(1    << (OR1K_DMR2_AWTC_OFF + first_free));
540
      or1k_dbgcache.dmr2 |=  (bits << (OR1K_DMR2_AWTC_OFF + first_free));
541
 
542
      bits = (or1k_dbgcache.dmr2 >> (OR1K_DMR2_WGB_OFF + first_used)) & 1;
543
      or1k_dbgcache.dmr2 &= ~(1    << (OR1K_DMR2_WGB_OFF + first_used));
544
      or1k_dbgcache.dmr2 &= ~(1    << (OR1K_DMR2_WGB_OFF + first_free));
545
      or1k_dbgcache.dmr2 |=  (bits << (OR1K_DMR2_WGB_OFF + first_free));
546
 
547
      bits = (or1k_dbgcache.dmr2 >> (OR1K_DMR2_WBS_OFF + first_used)) & 1;
548
      or1k_dbgcache.dmr2 &= ~(1    << (OR1K_DMR2_WBS_OFF + first_used));
549
      or1k_dbgcache.dmr2 &= ~(1    << (OR1K_DMR2_WBS_OFF + first_free));
550
      or1k_dbgcache.dmr2 |=  (bits << (OR1K_DMR2_WBS_OFF + first_free));
551
 
552
      first_free++;
553
    }
554
}       /* or1k_watchpoint_gc() */
555
 
556
 
557
/*---------------------------------------------------------------------------*/
558
/*!Find if we were stopped by a watchpoint, and if so which address
559
 
560
   This is true if a triggered breakpoint was ANDed with the previous
561
   watchpoint in the chain. If so, the previous matchpoint has the start
562
   address.
563
 
564
   This is an internal utility for use by or1k_stopped_watchpoint() and
565
   or1k_stopped_data_address().
566
 
567
   @note This will only find the FIRST watchpoint triggered.
568
 
569
   @param[out] addr_p  Where to put the address associated with the
570
                       watchpoint if non-NULL
571
   @param[out] mp_p    Where to put the matchpoint which triggered the WP
572
 
573
   @return  1 (true) if we get the address, 0 (false) otherwise */
574
/*---------------------------------------------------------------------------*/
575
 
576
static int
577
or1k_stopped_watchpoint_info (CORE_ADDR *addr_p,
578
                              int       *mp_p)
579
{
580
  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
581
  int                  mp;
582
 
583
  /* Cannot be the first matchpoint (it has to AND with one previous) */
584
  for( mp = 1 ; mp < tdep->num_matchpoints ; mp++ )
585
    {
586
      if (((or1k_dbgcache.dmr2 >> (mp + OR1K_DMR2_WBS_OFF)) & 1) &&
587
          (OR1K_DMR1_CW_AND ==
588
           ((or1k_dbgcache.dmr1 >> (mp * OR1K_DMR1_CW_SZ)) & OR1K_DMR1_CW)))
589
        {
590
          if (NULL != addr_p)
591
            {
592
              *addr_p = or1k_dbgcache.dvr[mp - 1];
593
            }
594
 
595
          if (NULL != mp_p)
596
            {
597
              *mp_p = mp;
598
            }
599
 
600
          return  1;
601
        }
602
    }
603
 
604
  return  0;
605
 
606
}       /* or1k_stopped_watchpoint_info() */
607
 
608
 
609
/*---------------------------------------------------------------------------*/
610
/*!Convert a register number to SPR number
611
 
612
   OR1K debug unit has GPRs mapped to SPRs, which are not compact, so we are
613
   mapping them for GDB.
614
 
615
   Rewritten by CZ 12/09/01
616
 
617
   @param[in] regnum  The register
618
 
619
   @return  The corresponding SPR or -1 on failure */
620
/*---------------------------------------------------------------------------*/
621
 
622
static int
623
or1k_regnum_to_sprnum (int regnum)
624
{
625
  /* The GPRs map as a block */
626
  if (regnum < OR1K_MAX_GPR_REGS)
627
    {
628
      return  OR1K_SPR (OR1K_SPG_SYS, OR1K_SPG_SYS_GPR + regnum);
629
    }
630
 
631
  /* The "special" registers */
632
  switch (regnum)
633
    {
634
    case OR1K_PPC_REGNUM: return  OR1K_NPC_SPRNUM;
635
    case OR1K_NPC_REGNUM: return  OR1K_NPC_SPRNUM;
636
    case OR1K_SR_REGNUM:  return  OR1K_SR_SPRNUM;
637
 
638
    default:
639
      error ("or1k_regnum_to_sprnum: invalid register number!");
640
      break;
641
    }
642
 
643
  return -1;
644
 
645
}       /* or1k_regnum_to_sprnum() */
646
 
647
 
648
/*---------------------------------------------------------------------------*/
649
/*!Sync debug registers
650
 
651
   Synchronizes debug registers in memory with those on target. This used to
652
   track if there was any change, but there always is with debuggers, so now
653
   we just write them every time.
654
 
655
   The old code did a lot of this via low-level manipulation of the JTAG
656
   interface. For now it is replaced by straightward reading/writing of the
657
   SPRs. */
658
/*---------------------------------------------------------------------------*/
659
 
660
static void
661
or1k_commit_debug_registers()
662
{
663
  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
664
  int                  i;
665
 
666
  /* Matchpoints (breakpoints, watchpoints).  */
667
  for (i = 0; i < tdep->num_matchpoints; i++)
668
    {
669
      unsigned long int  dcr = 0;
670
 
671
      dcr |= or1k_dbgcache.dcr[i].dp ? OR1K_DCR_DP : 0;
672
      dcr |= (or1k_dbgcache.dcr[i].cc << OR1K_DCR_CC_OFF) & OR1K_DCR_CC;
673
      dcr |= or1k_dbgcache.dcr[i].sc ? OR1K_DCR_SC : 0;
674
      dcr |= (or1k_dbgcache.dcr[i].ct << OR1K_DCR_CT_OFF) & OR1K_DCR_CT;
675
 
676
      or1k_jtag_write_spr (OR1K_DVR0_SPRNUM + i, or1k_dbgcache.dvr[i]);
677
      or1k_jtag_write_spr (OR1K_DCR0_SPRNUM + i, dcr);
678
    }
679
 
680
  or1k_jtag_write_spr (OR1K_DMR1_SPRNUM, or1k_dbgcache.dmr1);
681
  or1k_jtag_write_spr (OR1K_DMR2_SPRNUM, or1k_dbgcache.dmr2);
682
  or1k_jtag_write_spr (OR1K_DSR_SPRNUM,  or1k_dbgcache.dsr);
683
  or1k_jtag_write_spr (OR1K_DRR_SPRNUM,  or1k_dbgcache.drr);
684
 
685
}       /* or1k_commit_debug_registers() */
686
 
687
 
688
/*---------------------------------------------------------------------------*/
689
/*!Start the remote target
690
 
691
   This is a wrapper for GDB's start_remote function, suitable for use with
692
   catch_exception.
693
 
694
   @param[in] uiout  UI handle - not used
695
   @param[in] arg_p  Reference to the generic argument supplied through
696
                     catch_exception. In this case a pointer to the from_tty
697
                     parameter. */
698
/*--------------------------------------------------------------------------*/
699
 
700
static void
701
or1k_start_remote (struct ui_out *uiout,
702
                   void          *arg_p)
703
{
704
  int  from_tty = *((int *)arg_p);
705
 
706
  start_remote( from_tty );
707
 
708
}       /* or1k_start_remote() */
709
 
710
 
711
/*---------------------------------------------------------------------------*/
712
/*!Print info on this target.
713
 
714
  @param[in] target  GDB ops for the target - ignored. */
715
/*---------------------------------------------------------------------------*/
716
 
717
static void
718
or1k_files_info (struct target_ops *target)
719
{
720
  const char *status_name[] =
721
    {
722
      "UNDEFINED",
723
      "CONNECTING",
724
      "DISCONNECTING",
725
      "RUNNING",
726
      "STOPPED"
727
    };
728
 
729
  char *file = "nothing";
730
 
731
  if (exec_bfd)
732
    {
733
      file = bfd_get_filename (exec_bfd);
734
    }
735
 
736
  printf_filtered ("File \"%s\"", file);
737
 
738
  if (exec_bfd)
739
    {
740
      printf_filtered ("attached to %s running program %s: ",
741
                       target_shortname, file);
742
    }
743
}       /* or1k_files_info() */
744
 
745
 
746
/*---------------------------------------------------------------------------*/
747
/*!Open a connection to the remote server
748
 
749
   Initialise the connection, then push the current target.
750
 
751
   @param[in] name      The arguments passed to the target command on GDB -
752
                        usually the address of the remote server
753
   @param[in] from_tty  1 (true) if the GDB session is being run from a
754
                        terminal and so can receive messages. 0 (false)
755
                        otherwise. */
756
/*---------------------------------------------------------------------------*/
757
 
758
static void
759
or1k_open (char *name,
760
           int   from_tty)
761
{
762
  struct gdbarch_tdep *tdep     = gdbarch_tdep (current_gdbarch);
763
  int                  i;
764
  unsigned int         tmp;
765
  struct gdb_exception ex;
766
 
767
  /* Check nothing's still running. This will call or1k_close if anything is,
768
     which will close the connection. Having done this, any remaining target
769
     instances should be unpushed from the stack */
770
  target_preopen (from_tty);
771
  unpush_target (&or1k_target);
772
 
773
  /* Initialize the connection to the remote target. */
774
  or1k_jtag_init (name);
775
 
776
  /* Make sure we have system and debug groups implemented. */
777
  tmp = or1k_jtag_read_spr (OR1K_UPR_SPRNUM);
778
  if (0 == (tmp & OR1K_SPR_UPR_UP))
779
    {
780
      error ("or1k_open: system group missing");
781
    }
782
  if (0 == (tmp & OR1K_SPR_UPR_DUP))
783
    {
784
      error ("or1k_open: debug group missing");
785
    }
786
 
787
  /* Determine number of gpr_regs (this is a bit simplified - the bit just
788
     means "less than 32", but we assume that means 16) and the number of
789
     supported watchpoints. */
790
  tmp                = or1k_jtag_read_spr (OR1K_CPUCFGR_SPRNUM);
791
  tdep->num_gpr_regs = (0 == (tmp & OR1K_SPR_CPUCFGR_CGF)) ? 32 : 16;
792
 
793
  tmp                   = or1k_jtag_read_spr (OR1K_DCFGR_SPRNUM);
794
  tdep->num_matchpoints = (tmp & OR1K_SPR_DCFGR_NDP) + 1;
795
 
796
  /* Stall the processor. A pause after stalling is appropriate for real
797
     hardware. */
798
  or1k_jtag_stall ();
799
  usleep (1000);
800
 
801
  /* Initialize the debug register cache. The DSR is set to stop when
802
     breakpoint occurs. The cache will be written out before unstalling the
803
     processor. */
804
  for (i = 0; i < tdep->num_matchpoints; i++)
805
    {
806
      or1k_dbgcache.dvr[i] = 0;
807
      memset (&or1k_dbgcache.dcr[i], 0, sizeof (or1k_dbgcache.dcr[i]));
808
    }
809
 
810
  or1k_dbgcache.dmr1 = 0;
811
  or1k_dbgcache.dmr2 = 0;
812
  or1k_dbgcache.dsr  = OR1K_DSR_TE;
813
  or1k_dbgcache.drr  = 0;
814
 
815
  /* The remote target connection stalls the target, so all aspects of a
816
     running connection are there, except it doesn't actually have
817
     execution. So we call target_mark_running, but then immediately mark it
818
     as not having execution */
819
  push_target (&or1k_target);
820
  target_mark_running (&or1k_target);
821
  target_has_execution = 0;
822
 
823
  /* Get rid of any old shared library symbols. */
824
  no_shared_libraries (NULL, 0);
825
 
826
  /* We don't have a concept of processs or threads, so we use the null PTID
827
     for the target. Set this here before the start_remote() code uses it. */
828
  inferior_ptid = null_ptid;
829
 
830
  /* Start the remote target. Uses our own wrapper, so we can wrap it to
831
     catch the exeception if it goes tits up. */
832
  ex = catch_exception (uiout, or1k_start_remote, &from_tty, RETURN_MASK_ALL);
833
  if (ex.reason < 0)
834
    {
835
      pop_target ();
836
      throw_exception (ex);
837
    }
838
 
839
}       /* or1k_open() */
840
 
841
 
842
/*---------------------------------------------------------------------------*/
843
/*!Close a connection to the remote server
844
 
845
   Use the target extra op is there is one. Don't mourn the inferior - it
846
   calls us, so we'll get a never ending loop!
847
 
848
   param[in] quitting  If true (1) GDB is going to shut down, so don't worry
849
   to wait for everything to complete. */
850
/*--------------------------------------------------------------------------*/
851
 
852
static void
853
or1k_close (int quitting)
854
{
855
  or1k_jtag_close ();
856
 
857
}       /* or1k_close() */
858
 
859
 
860
/*---------------------------------------------------------------------------*/
861
/*!Detach from the remote server
862
 
863
   There is only one remote connection, so no argument should be given.
864
 
865
   @param[in] args      Any args given to the detach command
866
   @param[in] from_tty  True (1) if this GDB session is run from a terminal,
867
   so messages can be output. */
868
/*---------------------------------------------------------------------------*/
869
 
870
static void
871
or1k_detach (char *args,
872
             int   from_tty)
873
{
874
  if (args)
875
    {
876
      error ("or1k_detach: \"detach\" takes no arg when remote debugging\n");
877
    }
878
 
879
  or1k_close (1);
880
 
881
  if (from_tty)
882
    {
883
      printf_unfiltered ("Ending remote or1k debugging\n");
884
    }
885
}       /* or1k_detach() */
886
 
887
 
888
/*---------------------------------------------------------------------------*/
889
/*!Fetch remote registers
890
 
891
   The remote registers are fetched into the register cache
892
 
893
   @param[in] regcache  The register cache to use
894
   @param[in] regnum    The desired register number, or -1 if all are wanted */
895
/*---------------------------------------------------------------------------*/
896
 
897
static void
898
or1k_fetch_registers (struct regcache *regcache,
899
                      int              regnum)
900
{
901
  unsigned long int  val;
902
  char               buf[sizeof (CORE_ADDR) ];
903
 
904
  if (-1 == regnum)
905
    {
906
      /* Get the lot */
907
      for (regnum = 0; regnum < OR1K_NUM_REGS; regnum++)
908
        {
909
          or1k_fetch_registers (regcache, regnum);
910
        }
911
      return;
912
    }
913
 
914
  if (regnum >= OR1K_NUM_REGS)
915
    {
916
      error ("or1k_fetch_registers: invalid register number");
917
    }
918
 
919
  /* Convert to SPRNUM and read.  */
920
  val = or1k_jtag_read_spr (or1k_regnum_to_sprnum (regnum));
921
 
922
  /* We got the number the register holds, but gdb expects to see a value in
923
     the target byte ordering.
924
 
925
     This is new for GDB 6.8. Not sure if I need get_thread_regcache() using
926
     the ptid instead here. The old code used supply_register(), but that no
927
     longer exists, so we hope regcache_raw_supply will do instead. */
928
 
929
  store_unsigned_integer (buf, sizeof (CORE_ADDR), val);
930
  regcache_raw_supply (regcache, regnum, buf);
931
 
932
}       /* or1k_fetch_registers() */
933
 
934
 
935
/*---------------------------------------------------------------------------*/
936
/*!Store remote registers
937
 
938
   The remote registers are written from the register cache
939
 
940
   @param[in] regcache  The register cache to use
941
   @param[in] regnum    The desired register number, or -1 if all are wanted */
942
/*---------------------------------------------------------------------------*/
943
 
944
static void
945
or1k_store_registers (struct regcache *regcache,
946
                      int              regnum)
947
{
948
  ULONGEST         res;
949
 
950
  if (-1 == regnum)
951
    {
952
      for (regnum = 0; regnum < OR1K_NUM_REGS; regnum++)
953
        {
954
          or1k_store_registers (regcache, regnum);
955
        }
956
      return;
957
    }
958
 
959
  if (regnum >= OR1K_NUM_REGS)
960
    {
961
      error ("or1k_store_registers: invalid register number");
962
    }
963
 
964
  regcache = get_current_regcache();
965
  regcache_cooked_read_unsigned (regcache, regnum, &res);
966
  or1k_jtag_write_spr (or1k_regnum_to_sprnum (regnum), res);
967
 
968
}       /* or1k_store_registers() */
969
 
970
 
971
/*---------------------------------------------------------------------------*/
972
/*!Prepare to store registers
973
 
974
   This is a null function for the OpenRisc 1000 architecture
975
 
976
   @param[in] regcache  The register cache to use */
977
/*---------------------------------------------------------------------------*/
978
 
979
static void
980
or1k_prepare_to_store (struct regcache *regcache)
981
{
982
  return;
983
 
984
}       /* or1k_prepare_to_store() */
985
 
986
 
987
/*---------------------------------------------------------------------------*/
988
/*!Transfer some data to or from the target
989
 
990
   One and only one of readbuf or writebuf should be non-NULL, which indicates
991
   whether this is a read or write operation.
992
 
993
   OR1K reads and writes are in words, so we may need to do partial
994
   read/writes at each end of the transfer.
995
 
996
   @param[in]  ops       The target_ops vector to use
997
   @param[in]  object    The type of object to transfer
998
   @param[in]  annex     Additional object specific information (not used)
999
   @param[out] readbuf   Buffer for read data
1000
   @param[in]  writebuf  Buffer of data to write
1001
   @param[in]  offset    Offset into object
1002
   @param[in]  len       Max bytes to transer
1003
 
1004
   @return  Number of bytes transferred or -1 if not supported */
1005
/*---------------------------------------------------------------------------*/
1006
 
1007
LONGEST  or1k_xfer_partial (struct target_ops  *ops,
1008
                                 enum target_object  object,
1009
                                 const char         *annex,
1010
                                 gdb_byte           *readbuf,
1011
                                 const gdb_byte     *writebuf,
1012
                                 ULONGEST            offset,
1013
                                 LONGEST             len)
1014
{
1015
  /* Only memory transfer is currently supported */
1016
  if (TARGET_OBJECT_MEMORY != object)
1017
    {
1018
      return  -1;
1019
    }
1020
 
1021
  if (NULL == writebuf)
1022
    {
1023
      /* Must be a read */
1024
      gdb_assert (NULL != readbuf);
1025
 
1026
      return  or1k_jtag_read_mem (offset, readbuf, len);
1027
    }
1028
  else
1029
    {
1030
      return  or1k_jtag_write_mem (offset, writebuf, len);
1031
    }
1032
}       /* or1k_xfer_partial() */
1033
 
1034
 
1035
/*---------------------------------------------------------------------------*/
1036
/*!Insert a breakpoint
1037
 
1038
   Try to insert a breakpoint. Use hardware breakpoints if they are supported,
1039
   software breakpoints otherwise.
1040
 
1041
   @param[in] bpi  Break point info with details of the address and a cache to
1042
                   hold the existing memory value if overwriting.
1043
 
1044
                   @return  0 if the breakpoint was set, non-zero otherwise */
1045
/*---------------------------------------------------------------------------*/
1046
 
1047
static int
1048
or1k_insert_breakpoint (struct bp_target_info *bpi)
1049
{
1050
  if (0 == or1k_set_breakpoint (bpi->placed_address))
1051
    {
1052
      return 0;
1053
    }
1054
  else
1055
    {
1056
      return  memory_insert_breakpoint (bpi);
1057
    }
1058
}       /* or1k_insert_breakpoint() */
1059
 
1060
 
1061
/*---------------------------------------------------------------------------*/
1062
/*!Remove a breakpoint
1063
 
1064
   @param[in] bpi  Break point info with details of the address and a cache to
1065
                   hold the existing memory value if overwriting.
1066
 
1067
                   @return  0 if the breakpoint was cleared, non-zero otherwise */
1068
/*---------------------------------------------------------------------------*/
1069
 
1070
static int
1071
or1k_remove_breakpoint (struct bp_target_info *bpi)
1072
{
1073
  /* First try to remove HW breakpoint at address */
1074
  if (0 != or1k_clear_breakpoint (bpi->placed_address))
1075
    {
1076
      return  memory_remove_breakpoint (bpi);
1077
    }
1078
  else
1079
    {
1080
      return  0;
1081
    }
1082
}       /* or1k_remove_breakpoint() */
1083
 
1084
 
1085
/*---------------------------------------------------------------------------*/
1086
/*!Tell whether we can support a hardware breakpoint or watchpoint
1087
 
1088
   There are only a fixed number of hardware breakpoints available. Check if
1089
   there are any left. Hardware watchpoints need TWO matchpoints.
1090
 
1091
   This is only an approximation. An exact response would need full knowledge
1092
   of all the HW resources requested and would need to guarantee those
1093
   resources were not subsequently cannabalized for non-HW breakpoints and
1094
   watchpoints.
1095
 
1096
   The algorithm is to multiply count by 1 for breakpoints and 2 for
1097
   watchpoints and add 2 if othertype is set. Any matchpoints already used are
1098
   ignored (we cannot know if they relate to the question currently being
1099
   asked). This approach means we will never reject when there is HW
1100
   breakpoint resource available, but we may sometimes accept when there turns
1101
   out to be no HW resource available. But GDB handles that OK (see section
1102
   5.1.2 in the user guide).
1103
 
1104
   @param[in] type       What type of matchpoint are we asking about? This
1105
                         function is apparently only called for
1106
                         bp_hardware_watchpoint, bp_read_watchpoint,
1107
                         bp_write_watchpoint (but the enum is called
1108
                         access_watchpoint) or bp_hardware_breakpoint
1109
   @param[in] count      How many of this type of matchpoint have been used
1110
                         (including this one)
1111
   @param[in] othertype  1 (true) if this is a call about a watchpoint and
1112
                         watchpoints (but not breakpoints) of other types have
1113
                         been set.
1114
 
1115
   @return  the number of watchpoints of this type that can be set if count
1116
            watchpoints can be set, 0 if watchpoints of this type cannot be
1117
            set and negative if watchpoints of this type can be set, but there
1118
            are none available. */
1119
/*--------------------------------------------------------------------------*/
1120
 
1121
static int
1122
or1k_can_use_hw_matchpoint (int  type,
1123
                            int  count,
1124
                            int  othertype)
1125
{
1126
  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1127
 
1128
  switch (type)
1129
    {
1130
    case bp_hardware_breakpoint:
1131
      if (count <= tdep->num_matchpoints)
1132
        {
1133
          return  tdep->num_matchpoints;
1134
        }
1135
      else
1136
        {
1137
          return -1;
1138
        }
1139
 
1140
    case bp_hardware_watchpoint:
1141
    case bp_read_watchpoint:
1142
    case bp_access_watchpoint:
1143
      if ((2 * (othertype + count)) <= tdep->num_matchpoints)
1144
        {
1145
          return  tdep->num_matchpoints / (2 * (othertype + count));
1146
        }
1147
      else
1148
        {
1149
          return  -1;
1150
        }
1151
 
1152
    default:
1153
      warning( "Request to use unknown hardware matchpoint type: %d\n",
1154
               type );
1155
      return  0;
1156
    }
1157
 
1158
}       /* or1k_can_use_hw_matchpoint() */
1159
 
1160
 
1161
/*---------------------------------------------------------------------------*/
1162
/*!Insert a hardware breakpoint
1163
 
1164
   Try to insert a hardware breakpoint if they are supported.
1165
 
1166
   @param[in] bpi  Break point info with details of the address and a cache to
1167
                   hold the existing memory value if overwriting.
1168
 
1169
                   @return  0 if the breakpoint was set, non-zero otherwise */
1170
/*---------------------------------------------------------------------------*/
1171
 
1172
static int
1173
or1k_insert_hw_breakpoint (struct bp_target_info *bpi)
1174
{
1175
  return  or1k_set_breakpoint (bpi->placed_address);
1176
 
1177
}       /* or1k_insert_hw_breakpoint() */
1178
 
1179
 
1180
/*---------------------------------------------------------------------------*/
1181
/*!Remove a hardware breakpoint
1182
 
1183
   @param[in] bpi  Break point info with details of the address and a cache to
1184
                   hold the existing memory value if overwriting.
1185
 
1186
                   @return  0 if the breakpoint was cleared, non-zero otherwise */
1187
/*---------------------------------------------------------------------------*/
1188
 
1189
static int
1190
or1k_remove_hw_breakpoint (struct bp_target_info *bpi)
1191
{
1192
  return  or1k_clear_breakpoint (bpi->placed_address);
1193
 
1194
}       /* or1k_remove_hw_breakpoint() */
1195
 
1196
 
1197
/*---------------------------------------------------------------------------*/
1198
/*!Set a data watchpoint.
1199
 
1200
   A GDB data watchpoint uses a pair of HW watchpoints. The first looks for
1201
   accesses greater than or equal to the start address, the second looks for
1202
   accesses less than or equal to the end address. This allows watching of any
1203
   length of object.
1204
 
1205
   However because of the way the OpenRISC 1000 chains its watchpoints, a
1206
   suitable adjacent pair must be found. So the watchpoint garbage collector
1207
   is used to shuffle them all up.
1208
 
1209
   @param[in] addr  Address to be watched
1210
   @param[in] len   Length of the entity to be watched
1211
   @param[in] type  hw_write (0) for a write watchpoint, hw_read (1) for a
1212
                    read watchpoint, or hw_access (2) for a read/write
1213
                    watchpoint.
1214
 
1215
   @return  0 if the watchpoint was set, -1 otherwise (it must be -1, not just
1216
   any non-zero number, since breakpoint.c tests explicitly for -1 */
1217
/*--------------------------------------------------------------------------*/
1218
 
1219
static int
1220
or1k_insert_watchpoint (CORE_ADDR  addr,
1221
                        int        len,
1222
                        int        type)
1223
{
1224
  unsigned char        dcr_ct = or1k_gdb_to_dcr_type (type);
1225
  int                  first_free;
1226
 
1227
  if (len < 1)
1228
    {
1229
      return  -1;
1230
    }
1231
 
1232
  /* Garbage collect and see if we have two adjacent watchpoints available. */
1233
 
1234
  if( or1k_watchpoint_gc() < 2 )
1235
    {
1236
      return  -1;
1237
    }
1238
 
1239
 
1240
  /* Set lower bound at first free matchpoint. */
1241
  first_free               = or1k_first_free_matchpoint();
1242
 
1243
  or1k_dbgcache.dvr[first_free]     = addr;
1244
  or1k_dbgcache.dcr[first_free].dp  = 1;
1245
  or1k_dbgcache.dcr[first_free].cc  = OR1K_CC_GE;
1246
  or1k_dbgcache.dcr[first_free].sc  = 0;
1247
  or1k_dbgcache.dcr[first_free].ct  = dcr_ct;
1248
 
1249
  /* Set upper watchpoint bound at next matchpoint. GC guarantees
1250
     it is free. */
1251
  first_free++;
1252
 
1253
  or1k_dbgcache.dvr[first_free]     = addr + len - 1;
1254
  or1k_dbgcache.dcr[first_free].dp  = 1;
1255
  or1k_dbgcache.dcr[first_free].cc  = OR1K_CC_LE;
1256
  or1k_dbgcache.dcr[first_free].sc  = 0;
1257
  or1k_dbgcache.dcr[first_free].ct  = dcr_ct;
1258
 
1259
  /* Set && chaining of the second matchpoint to the first. This is the only
1260
     one which should generate a breakpoint, since it only occurs when BOTH
1261
     matchpoints trigger. */
1262
  or1k_dbgcache.dmr1 &= ~(OR1K_DMR1_CW     << (OR1K_DMR1_CW_SZ * first_free));
1263
  or1k_dbgcache.dmr1 |=  (OR1K_DMR1_CW_AND << (OR1K_DMR1_CW_SZ * first_free));
1264
  or1k_dbgcache.dmr2 |= (1 << (first_free + OR1K_DMR2_WGB_OFF));
1265
 
1266
  return 0;
1267
 
1268
}       /* or1k_insert_watchpoint() */
1269
 
1270
 
1271
/*---------------------------------------------------------------------------*/
1272
/*!Remove a data watchpoint.
1273
 
1274
   @param[in] addr  Address being watched
1275
   @param[in] len   Length of the entity being watched
1276
   @param[in] type  0 for a write watchpoint, 1 for a read watchpoint, or 2
1277
                    for a read/write watchpoint
1278
 
1279
                    @return  0 if the watchpoint was cleared, non-zero otherwise */
1280
/*---------------------------------------------------------------------------*/
1281
 
1282
static int
1283
or1k_remove_watchpoint (CORE_ADDR  addr,
1284
                        int        len,
1285
                        int        type)
1286
{
1287
  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1288
  unsigned char        dcr_ct;
1289
  CORE_ADDR            end_addr;
1290
  int                  mp;
1291
 
1292
  if (len < 1)
1293
    {
1294
      return -1;
1295
    }
1296
 
1297
  dcr_ct   = or1k_gdb_to_dcr_type (type);
1298
  end_addr = addr + len - 1;
1299
 
1300
  /* Find the right one. PS. Don't be tempted to compare mp against
1301
     tdep->num_matchpoints - 1. It's unsigned and can be 0, so you'd go
1302
     on for ever! */
1303
  for (mp = 0; (mp + 1) < tdep->num_matchpoints; mp++)
1304
    {
1305
      if (or1k_matchpoint_equal( mp,     addr,     OR1K_CC_GE, 0, dcr_ct) &&
1306
          or1k_matchpoint_equal( mp + 1, end_addr, OR1K_CC_LE, 0, dcr_ct))
1307
        {
1308
          break;
1309
        }
1310
    }
1311
 
1312
  if ((mp + 1) >= tdep->num_matchpoints)
1313
    {
1314
      return -1;
1315
    }
1316
 
1317
  /* Mark each matchpoint unused and clear the bits for the second watchpoint
1318
     in the assign to counter, watchpoint generating break and breakpoint
1319
     status bits in DMR2. */
1320
 
1321
  or1k_dbgcache.dcr[mp].dp = 0;
1322
 
1323
  mp++;
1324
  or1k_dbgcache.dcr[mp].dp = 0;
1325
  or1k_dbgcache.dmr2 &= ~(1 << (mp + OR1K_DMR2_AWTC_OFF));
1326
  or1k_dbgcache.dmr2 &= ~(1 << (mp + OR1K_DMR2_WGB_OFF));
1327
  or1k_dbgcache.dmr2 &= ~(1 << (mp + OR1K_DMR2_WBS_OFF));
1328
 
1329
  return 0;
1330
 
1331
}       /* or1k_remove_watchpoint() */
1332
 
1333
 
1334
/*---------------------------------------------------------------------------*/
1335
/*!Report if we stopped due to a watchpoint
1336
 
1337
   All the information is calculated by or1k_stopped_data_address, so we must
1338
   reuse that.
1339
 
1340
   @return  1 (true) if we were stopped by a watchpoint, 0 (false) otherwise */
1341
/*---------------------------------------------------------------------------*/
1342
 
1343
static int
1344
or1k_stopped_by_watchpoint()
1345
{
1346
  return  or1k_stopped_watchpoint_info (NULL, NULL);
1347
 
1348
}       /* or1k_stopped_by_watchpoint() */
1349
 
1350
 
1351
/*---------------------------------------------------------------------------*/
1352
/*!Address for which we were stopped by a remote watchpoint
1353
 
1354
   This is true if a triggered breakpoint was ANDed with the previous
1355
   watchpoint in the chain. If so, the previous matchpoint has the start
1356
   address.
1357
 
1358
   This function is called once when a watchpoint is hit, and must clear the
1359
   watchpoint status
1360
 
1361
   @param[in]  target  The target_ops we are using. Not clear why we need
1362
                       this!
1363
   @param[out] addr_p  Where to put the address associated with the
1364
                       watchpoint.
1365
 
1366
                       @return  1 (true) if we get the address, 0 (false) otherwise */
1367
/*---------------------------------------------------------------------------*/
1368
 
1369
static int
1370
or1k_stopped_data_address (struct target_ops *target,
1371
                           CORE_ADDR         *addr_p)
1372
{
1373
  int  mp;
1374
 
1375
  /* Clear the result info if there was a watchpoint */
1376
  if( or1k_stopped_watchpoint_info (addr_p, &mp))
1377
    {
1378
      or1k_dbgcache.dmr2 &= ~(1 << (mp + OR1K_DMR2_WBS_OFF));
1379
      return  1;
1380
    }
1381
  else
1382
    {
1383
      return  0;
1384
    }
1385
}       /* or1k_stopped_data_address() */
1386
 
1387
 
1388
/*---------------------------------------------------------------------------*/
1389
/*!Can we put a HW watchpoint of the given size at the given address
1390
 
1391
   We can always use a HW watchpoint, so long as there are HW watchpoints
1392
   available. This doesn't have to check availablility (see
1393
   or1k_can_use_hw_matchpoint().
1394
 
1395
   @param[in] addr  The address of interest
1396
   @param[in] len   The length of the region to watch
1397
 
1398
   @return  1 (true) to indicate we always can do a HW watchpoint */
1399
/*---------------------------------------------------------------------------*/
1400
 
1401
static int
1402
or1k_region_ok_for_hw_watchpoint (CORE_ADDR  addr,
1403
                                  int        len)
1404
{
1405
  return  1;
1406
 
1407
}       /* or1k_region_ok_for_hw_watchpoint */
1408
 
1409
 
1410
/*---------------------------------------------------------------------------*/
1411
/*!Resume execution of the target process.
1412
 
1413
   "step" says whether to single-step or to run free; "siggnal" is the signal
1414
   value (e.g. SIGINT) to be given to the target, or zero for no signal.
1415
 
1416
   When we enter this routine the target should be stalled.
1417
 
1418
   In general GDB sorts out issues of restarting across breakpoints. However
1419
   have to deal with the special case where a breakpoint occurred in the delay
1420
   slot of a branch instruction. In this case the branch should be restarted,
1421
   BUT ONLY if the branch had originally been executed.
1422
 
1423
   For now we ignore this case (it really is very hard.
1424
 
1425
   @param[in]  ptid  The process or thread ID - ignored here
1426
   @param[in]  step  If true (1) single step the target
1427
   @param[in]  sig   Signal to give to the target */
1428
/*---------------------------------------------------------------------------*/
1429
 
1430
static void
1431
or1k_resume (ptid_t              ptid,
1432
             int                 step,
1433
             enum target_signal  sig)
1434
{
1435
  /* Note if we are single stepping. For use when waiting */
1436
  or1k_is_single_stepping = step;
1437
 
1438
  /* The debug reason register and the watchpoint break status
1439
     are both cleared before resuming. */
1440
  or1k_dbgcache.drr   = 0;
1441
  or1k_dbgcache.dmr2 &= ~OR1K_DMR2_WBS_MASK;
1442
 
1443
  /* Set single stepping if required */
1444
  if (step)
1445
    {
1446
      or1k_dbgcache.dmr1 |= OR1K_DMR1_ST;
1447
    }
1448
  else
1449
    {
1450
      or1k_dbgcache.dmr1 &= ~OR1K_DMR1_ST;
1451
    }
1452
 
1453
  or1k_commit_debug_registers();
1454
 
1455
  /* We can now continue normally, independent of step. If this is called for
1456
     the first time due to a run command, we won't actually be executing, so
1457
     start the target running. */
1458
 
1459
  or1k_jtag_write_spr( OR1K_NPC_SPRNUM, read_pc());
1460
  target_has_execution = 1;
1461
  or1k_jtag_unstall ();
1462
 
1463
}       /* or1k_resume() */
1464
 
1465
 
1466
/*---------------------------------------------------------------------------*/
1467
/*!Wait until the remote server stops, and return a wait status.
1468
 
1469
   Seems to assume that the remote target is identified solely by PID with no
1470
   thread or lightweight thread component.
1471
 
1472
   This is mostly a case of just sorting out the type of exception. GDB will
1473
   automatically sort out restarting with breakpoints removed etc.
1474
 
1475
   @note The old code differentiated a high priority and a low priority
1476
         interrupt, which no longer exists on the OR1K. The old code also
1477
         omitted tick timer and floating point exceptions, which have been
1478
         added.
1479
 
1480
   @param[in]  remote_ptid  The process/thread ID of the remote target
1481
   @param[out] status       Status of the waiting process
1482
 
1483
   @return  The remote_ptid unchanged */
1484
/*---------------------------------------------------------------------------*/
1485
 
1486
static ptid_t
1487
or1k_wait (ptid_t                    remote_ptid,
1488
           struct target_waitstatus *status)
1489
{
1490
  unsigned long int  addr;
1491
  char               buf[OR1K_INSTLEN];
1492
  int                res;
1493
 
1494
  /* Set new signal handler (so we can interrupt with ctrl-C) and then wait
1495
     for the OR1K to stall. */
1496
  or1k_old_intr_handler = signal (SIGINT, or1k_interrupt);
1497
  or1k_jtag_wait (or1k_is_single_stepping);
1498
  signal (SIGINT, or1k_old_intr_handler);
1499
 
1500
  /* Registers and frame caches are now all unreliable */
1501
  registers_changed();
1502
 
1503
  /* Refresh the debug registers that may have changed */
1504
  or1k_dbgcache.drr  = or1k_jtag_read_spr (OR1K_DRR_SPRNUM);
1505
  or1k_dbgcache.dmr2 = or1k_jtag_read_spr (OR1K_DMR2_SPRNUM);
1506
 
1507
  /* If we got a trap, then it was a breakpoint/watchpoint of some sort (but
1508
     not single step, which does not set the trap exception). We need to back
1509
     up the program counter, so the instruction will be re-executed. This
1510
     must be the value of the PPC, in case we have just done a branch. */
1511
 
1512
  if (OR1K_DRR_TE == (or1k_dbgcache.drr & OR1K_DRR_TE))
1513
    {
1514
      write_pc (or1k_jtag_read_spr (OR1K_PPC_SPRNUM));
1515
    }
1516
 
1517
  /* Single step does not set trap exception, so we set it manually to
1518
     simplify our code. */
1519
  if (OR1K_DMR1_ST == (or1k_dbgcache.dmr1 & OR1K_DMR1_ST))
1520
    {
1521
      or1k_dbgcache.drr |= OR1K_DRR_TE;
1522
    }
1523
 
1524
  status->kind = TARGET_WAITKIND_STOPPED;
1525
 
1526
  /* Map OR1K exception to GDB signal. If multiple signals are set, only the
1527
     least significant in the DRR is used (the expression here yields the LSB
1528
     in a 2's complement machine) */
1529
  switch (or1k_dbgcache.drr & (-or1k_dbgcache.drr))
1530
    {
1531
    case OR1K_DRR_RSTE:  status->value.sig = TARGET_SIGNAL_PWR;  break;
1532
    case OR1K_DRR_BUSEE: status->value.sig = TARGET_SIGNAL_BUS;  break;
1533
    case OR1K_DRR_DPFE:  status->value.sig = TARGET_SIGNAL_SEGV; break;
1534
    case OR1K_DRR_IPFE:  status->value.sig = TARGET_SIGNAL_SEGV; break;
1535
    case OR1K_DRR_TTE:   status->value.sig = TARGET_SIGNAL_ALRM; break;
1536
    case OR1K_DRR_AE:    status->value.sig = TARGET_SIGNAL_BUS;  break;
1537
    case OR1K_DRR_IIE:   status->value.sig = TARGET_SIGNAL_ILL;  break;
1538
    case OR1K_DRR_INTE:  status->value.sig = TARGET_SIGNAL_INT;  break;
1539
    case OR1K_DRR_DME:   status->value.sig = TARGET_SIGNAL_SEGV; break;
1540
    case OR1K_DRR_IME:   status->value.sig = TARGET_SIGNAL_SEGV; break;
1541
    case OR1K_DRR_RE:    status->value.sig = TARGET_SIGNAL_FPE;  break;
1542
    case OR1K_DRR_SCE:   status->value.sig = TARGET_SIGNAL_USR2; break;
1543
    case OR1K_DRR_FPE:   status->value.sig = TARGET_SIGNAL_FPE;  break;
1544
    case OR1K_DRR_TE:    status->value.sig = TARGET_SIGNAL_TRAP; break;
1545
 
1546
    default:
1547
      /* This just means the target stopped without raising any
1548
         interrupt. This happens at reset and exit. The latter we distinquish
1549
         by looking at the instruction just executed, to see if it is
1550
         "l.nop NOP_EXIT". If this is the case, get the result from GPR 3 and
1551
         set the NPC to the reset vector, so a new start will behave well. */
1552
 
1553
      addr = or1k_jtag_read_spr (OR1K_PPC_SPRNUM);
1554
      res  = read_memory_nobpt (addr, buf, OR1K_INSTLEN);
1555
      if (0 != res)
1556
        {
1557
          memory_error (res, addr);
1558
        }
1559
 
1560
      if (OR1K_NOP_EXIT ==
1561
          (unsigned long int)(extract_unsigned_integer (buf, OR1K_INSTLEN)))
1562
        {
1563
          ULONGEST  val;
1564
 
1565
          regcache_cooked_read_unsigned(get_current_regcache(),
1566
                                        OR1K_FIRST_ARG_REGNUM, &val);
1567
          status->value.integer = val;
1568
          status->kind          = TARGET_WAITKIND_EXITED;
1569
          write_pc (OR1K_RESET_VECTOR);
1570
        }
1571
      else
1572
        {
1573
          status->value.sig     = TARGET_SIGNAL_DEFAULT;
1574
        }
1575
      break;
1576
    }
1577
 
1578
  /* Clear the reason register */
1579
  or1k_dbgcache.drr = 0;
1580
 
1581
  return remote_ptid;
1582
 
1583
}       /* or1k_wait() */
1584
 
1585
 
1586
/*---------------------------------------------------------------------------*/
1587
/*!Stop the remote process
1588
 
1589
   This is the generic stop called via the target vector. When a target
1590
   interrupt is requested, either by the command line or the GUI, we will
1591
   eventually end up here.
1592
 
1593
   Just stall the processor, put it into single step mode and unstall. */
1594
/*---------------------------------------------------------------------------*/
1595
 
1596
static void
1597
or1k_stop ()
1598
{
1599
  /* We should not stop the target immediately, since it can be in an
1600
     unfinished state.  So we do a single step.  This should not affect
1601
     anything.  */
1602
  or1k_jtag_stall();
1603
 
1604
  /* HW STEP.  Set OR1K_DMR1_ST.  */
1605
  or1k_dbgcache.dmr1 |= OR1K_DMR1_ST;
1606
  or1k_commit_debug_registers();
1607
 
1608
  or1k_jtag_unstall();
1609
 
1610
}       /* or1k_stop () */
1611
 
1612
 
1613
/*---------------------------------------------------------------------------*/
1614
/*!Kill the process running on the board
1615
 
1616
   We just ignore the target. Mourning the inferior will cause the connection
1617
   to be closed. */
1618
/*---------------------------------------------------------------------------*/
1619
 
1620
static void
1621
or1k_kill ()
1622
{
1623
  or1k_mourn_inferior ();
1624
 
1625
}       /* or1k_kill () */
1626
 
1627
 
1628
/*---------------------------------------------------------------------------*/
1629
/*!Start running on the target board by creating an inferior process
1630
 
1631
   @param[in] execfile  What to run
1632
   @param[in] args      Arguments to pass to the inferior process
1633
   @param[in] env       Environment for the inferior process
1634
   @param[in] from_tty  1 (true) if this session can write to the terminal */
1635
/*---------------------------------------------------------------------------*/
1636
 
1637
static void
1638
or1k_create_inferior (char  *execfile,
1639
                      char  *args,
1640
                      char **env,
1641
                      int    from_tty)
1642
{
1643
  CORE_ADDR entry_pt;
1644
 
1645
  if ((NULL != args) && ('\0' != args[0]))
1646
    {
1647
      warning ("or1k_create_inferior: "
1648
               "can't pass arguments to remote OR1K board - ignored");
1649
 
1650
      /* And don't try to use them on the next "run" command.  */
1651
      execute_command ("set args", from_tty);
1652
    }
1653
 
1654
  if ((NULL == execfile) || (NULL == exec_bfd))
1655
    {
1656
      warning (
1657
        "or1k_create_inferior: no executable file symbols specified\n"
1658
        "execution will proceed without symbol table. Use the \"file\"\n"
1659
        "command to rectify this.\n");
1660
    }
1661
 
1662
  /* The code won't actually start executing until it is unstalled in
1663
     or1k_resume(), so we'll call target_mark_running() there. Tidy up from
1664
     last time we were running. */
1665
  init_wait_for_inferior ();
1666
 
1667
}       /* or1k_create_inferior () */
1668
 
1669
 
1670
/*---------------------------------------------------------------------------*/
1671
/*!Mourn the inferior process
1672
 
1673
   We don't atually want to unpush the target - we should be able to call run
1674
   again to restart it. Use the generic mourn and mark it as exited (it it was
1675
   running). */
1676
/*---------------------------------------------------------------------------*/
1677
 
1678
static void
1679
or1k_mourn_inferior ()
1680
{
1681
  generic_mourn_inferior ();
1682
 
1683
  if( target_has_execution )
1684
    {
1685
      target_mark_exited (&or1k_target);
1686
    }
1687
}       /* or1k_mourn_inferior () */
1688
 
1689
 
1690
/*---------------------------------------------------------------------------*/
1691
/*!Send a command to the remote target
1692
 
1693
   The target can't actually run any remote commands, in the sense of having a
1694
   command interpreter. However we use this as a GDB "standard" way of
1695
   wrapping up commands to access the SPRs.
1696
 
1697
   Current commands supported are:
1698
   - readspr  <regno>
1699
   - writespr <regno> <value>
1700
 
1701
   The output is either the value read (for successful read) or OK (for
1702
   successful write) or E01 to indicate and wrror. All values are represented
1703
   as unsigned hex.
1704
 
1705
   @param[in]  command  The command to execute
1706
   @param[out] output   The result of the command                            */
1707
/*---------------------------------------------------------------------------*/
1708
 
1709
static void  or1k_rcmd (char           *command,
1710
                        struct ui_file *outbuf)
1711
{
1712
  /* Sort out which command */
1713
  if (0 == strncmp ("readspr", command, strlen ("readspr")))
1714
    {
1715
      unsigned int  regno;              /* SPR to read */
1716
 
1717
      if (1 != sscanf (command, "readspr %4x", &regno))
1718
        {
1719
          error ("or1k_rcmd: unrecognized readspr");
1720
          fputs_unfiltered ("E01", outbuf);
1721
        }
1722
      else
1723
        {
1724
          char      strbuf[sizeof ("ffffffff")];
1725
          ULONGEST  data = or1k_jtag_read_spr (regno);
1726
 
1727
          sprintf (strbuf, "%8llx", (long long unsigned int)data);
1728
          fputs_unfiltered (strbuf, outbuf);
1729
        }
1730
    }
1731
  else if (0 == strncmp ("writespr", command, strlen ("writespr")))
1732
    {
1733
      unsigned int  regno;              /* SPR to write */
1734
      long long unsigned int      data;         /* Value to write */
1735
 
1736
      if (2 != sscanf (command, "writespr %4x %8llx", &regno, &data))
1737
        {
1738
          error ("or1k_rcmd: unrecognized writespr");
1739
          fputs_unfiltered ("E01", outbuf);
1740
        }
1741
      else
1742
        {
1743
          or1k_jtag_write_spr (regno, data);
1744
          fputs_unfiltered ("OK", outbuf);
1745
        }
1746
    }
1747
  else
1748
    {
1749
      error ("or1k_rcmd: unrecognized remote command");
1750
      fputs_unfiltered ("E01", outbuf);
1751
    }
1752
}       /* or1k_rcmd () */
1753
 
1754
 
1755
/*---------------------------------------------------------------------------*/
1756
/*!Main entry point for the remote OpenRISC JTAG protocol
1757
 
1758
   Initializes the target ops for each version of the remote
1759
   protocol. Currently only the JTAG protocol is implemented.
1760
 
1761
   @note Earlier versions of this code provided for a "sim" and "dummy"
1762
         target. However these never had any functionality, so have been
1763
         deleted.
1764
 
1765
         @return  New CRC */
1766
/*---------------------------------------------------------------------------*/
1767
 
1768
  void
1769
_initialize_remote_or1k()
1770
{
1771
  /* Set up the JTAG ops. */
1772
 
1773
  or1k_target.to_shortname                   = "jtag";
1774
  or1k_target.to_longname                    =
1775
    "OpenRISC 1000 debugging over JTAG port";
1776
  or1k_target.to_doc                         =
1777
    "Debug a board using the OpenRISC 1000 JTAG debugging protocol. The\n"
1778
    "target may be connected directly via the parallel port, or may be a\n"
1779
    "remote connection over TCP/IP to another machine, an instance of the\n"
1780
    "Or1ksim architectural simulator, or physical hardware connected by USB\n"
1781
    "with its own standalone control program.\n"
1782
    "\n"
1783
    "The argument is the parallel port device (e.g. /dev/jp) to which it is\n"
1784
    "connected locally or a URL of the form jtag://<hostname>:<port> or\n"
1785
    "jtag://<hostname>:<service> identifying the remote connection.";
1786
 
1787
  or1k_target.to_files_info                  = or1k_files_info;
1788
  or1k_target.to_stratum                     = process_stratum;
1789
  or1k_target.to_has_all_memory              = 1;
1790
  or1k_target.to_has_memory                  = 1;
1791
  or1k_target.to_has_stack                   = 1;
1792
  or1k_target.to_has_registers               = 1;
1793
  or1k_target.to_has_execution               = 0;
1794
  or1k_target.to_have_steppable_watchpoint   = 0;
1795
  or1k_target.to_have_continuable_watchpoint = 0;
1796
 
1797
  or1k_target.to_open                        = or1k_open;
1798
  or1k_target.to_close                       = or1k_close;
1799
  or1k_target.to_detach                      = or1k_detach;
1800
 
1801
  or1k_target.to_fetch_registers             = or1k_fetch_registers;
1802
  or1k_target.to_store_registers             = or1k_store_registers;
1803
  or1k_target.to_prepare_to_store            = or1k_prepare_to_store;
1804
  or1k_target.to_load                        = generic_load;
1805
  or1k_target.to_xfer_partial                = or1k_xfer_partial;
1806
 
1807
  or1k_target.to_insert_breakpoint           = or1k_insert_breakpoint;
1808
  or1k_target.to_remove_breakpoint           = or1k_remove_breakpoint;
1809
  or1k_target.to_can_use_hw_breakpoint       = or1k_can_use_hw_matchpoint;
1810
  or1k_target.to_insert_hw_breakpoint        = or1k_insert_hw_breakpoint;
1811
  or1k_target.to_remove_hw_breakpoint        = or1k_remove_hw_breakpoint;
1812
  or1k_target.to_insert_watchpoint           = or1k_insert_watchpoint;
1813
  or1k_target.to_remove_watchpoint           = or1k_remove_watchpoint;
1814
  or1k_target.to_stopped_by_watchpoint       = or1k_stopped_by_watchpoint;
1815
  or1k_target.to_stopped_data_address        = or1k_stopped_data_address;
1816
  or1k_target.to_region_ok_for_hw_watchpoint = or1k_region_ok_for_hw_watchpoint;
1817
 
1818
  or1k_target.to_resume                      = or1k_resume;
1819
  or1k_target.to_wait                        = or1k_wait;
1820
  or1k_target.to_stop                        = or1k_stop;
1821
  or1k_target.to_kill                        = or1k_kill;
1822
  or1k_target.to_create_inferior             = or1k_create_inferior;
1823
  or1k_target.to_mourn_inferior              = or1k_mourn_inferior;
1824
 
1825
  or1k_target.to_rcmd                        = or1k_rcmd;
1826
 
1827
  or1k_target.to_magic                       = OPS_MAGIC;
1828
 
1829
  /* Tell GDB about the new target */
1830
 
1831
  add_target (&or1k_target);
1832
 
1833
}       /* _initialize_remote_or1k() */
1834
 
1835
 
1836
/* EOF */

powered by: WebSVN 2.1.0

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