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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-6.8/] [gdb/] [remote-or1k.c] - Blame information for rev 1739

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

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

powered by: WebSVN 2.1.0

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