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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [remote-or1k.c] - Blame information for rev 363

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

Line No. Rev Author Line
1 106 markom
/* Remote debugging interface for various or1k debugging protocols.
2 118 markom
   Currently supported or1k targets are: simulator, jtag, dummy.
3
 
4 106 markom
   Copyright 1993-1995, 2000 Free Software Foundation, Inc.
5
   Contributed by Cygnus Support.  Written by Marko Mlinar
6
   <markom@opencores.org>
7
 
8
   This file is part of GDB.
9
 
10
   This program is free software; you can redistribute it and/or modify
11
   it under the terms of the GNU General Public License as published by
12
   the Free Software Foundation; either version 2 of the License, or
13
   (at your option) any later version.
14
 
15
   This program is distributed in the hope that it will be useful,
16
   but WITHOUT ANY WARRANTY; without even the implied warranty of
17
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
   GNU General Public License for more details.
19
 
20
   You should have received a copy of the GNU General Public License
21
   along with this program; if not, write to the Free Software
22
   Foundation, Inc., 59 Temple Place - Suite 330,
23
   Boston, MA 02111-1307, USA.  */
24
 
25
#include "defs.h"
26
#include "inferior.h"
27
#include "bfd.h"
28
#include "symfile.h"
29
#include "gdb_wait.h"
30
#include "gdbcmd.h"
31
#include "gdbcore.h"
32
#include "target.h"
33
#include "remote-utils.h"
34
#include "gdb_string.h"
35
#include "tm.h"
36
 
37
#include <signal.h>
38
#include <sys/types.h>
39
#include <sys/stat.h>
40
#include <sys/ioctl.h>
41
#include <fcntl.h>
42
 
43 362 markom
#define debug if (remote_debug) printf_unfiltered
44
 
45 125 chris
/* The following prototype is necessary or the compiler will not
46
   correctly promote the data argument to ULONGEST */
47
static void or1k_write_reg (unsigned int, ULONGEST);
48
 
49 118 markom
/* JTAG or1k target ops.  */
50 106 markom
extern void jtag_init PARAMS ((char * args));
51 122 markom
extern ULONGEST jtag_read_reg PARAMS ((unsigned int regno));
52
extern void jtag_write_reg PARAMS ((unsigned int regno, ULONGEST data));
53 106 markom
extern void jtag_done PARAMS ((void));
54 143 chris
extern int jtag_read_block PARAMS ((unsigned int regno, void* block, int nRegisters));
55
extern int jtag_write_block PARAMS ((unsigned int regno, void* block, int nRegisters));
56 113 markom
extern void jtag_set_chain PARAMS ((int chain));
57 106 markom
struct target_ops or1k_jtag_ops;
58
static struct or1k_target_ops or1k_target_jtag =
59
  {
60
    "jtag",
61
    jtag_init,
62
    jtag_done,
63
    jtag_read_reg,
64
    jtag_write_reg,
65 143 chris
    jtag_read_block,
66
    jtag_write_block,
67 113 markom
    jtag_set_chain,
68 106 markom
    NULL,
69
    &or1k_jtag_ops,
70
    OPS_MAGIC
71
  };
72
 
73 118 markom
/* simulator or1k target ops.  */
74 106 markom
struct target_ops or1k_sim_ops;
75
static struct or1k_target_ops or1k_target_sim =
76
  {
77
    "simulator",
78
    NULL,
79
    NULL,
80
    NULL,
81
    NULL,
82
    NULL,
83
    NULL,
84 143 chris
    NULL,
85
    NULL,
86 106 markom
    &or1k_sim_ops,
87
    OPS_MAGIC
88
  };
89
 
90 118 markom
/* dummy or1k target ops.  */
91 106 markom
struct target_ops or1k_dummy_ops;
92
static struct or1k_target_ops or1k_target_dummy =
93
  {
94
    "dummy",
95
    NULL,
96
    NULL,
97
    NULL,
98
    NULL,
99
    NULL,
100
    NULL,
101 143 chris
    NULL,
102
    NULL,
103 106 markom
    &or1k_dummy_ops,
104
    OPS_MAGIC
105
  };
106
 
107
const char *str_err[] =
108
  {
109
    "None", "CRC error"
110
  };
111
 
112
const char *status_name[] =
113
  {
114
    "UNDEFINED", "CONNECTING", "DISCONNECTING", "RUNNING", "STOPPED"
115
  };
116
 
117 113 markom
/* Names for matchpoint related stuff.  */
118
static char *ct_names[] =
119
  {
120
    "DIS", "IFEA", "LEA", "SEA", "AEA", "LDATA", "SDATA", "ADATA"
121
  };
122
 
123
static char *cc_names[] =
124
  {
125
    "&", "==", "<", "<=", ">", ">=", "!=", "ERR"
126
  };
127
 
128
static char *ch_names[] =
129
  {
130
    "ERR", "&", "|", "ERR"
131
  };
132
 
133 106 markom
/* Implementation specific information.  Set by or1k_initialize.  */
134
struct struct_or1k_implementation or1k_implementation;
135
 
136
/* Current target status.  */
137
static enum target_status or1k_status = TARGET_UNDEFINED;
138
 
139
/* The target vector.  */
140
struct target_ops or1k_dummy_ops, or1k_jtag_ops, or1k_sim_ops;
141
 
142
/* Currently active target description (if or1k_is_open == 1) */
143
static struct target_ops *current_ops;
144
 
145
/* Currently active or1k target operations.  */
146
static struct or1k_target_ops *current_or1k_target = NULL;
147
 
148
/* Set to 1 if the target is open.  */
149
static int or1k_is_open = 0;
150
 
151
/* Error last occured, zero = ok.  */
152 118 markom
int err = 0;
153 106 markom
 
154 362 markom
/* Nonzero, if we changed something (except DMR1 which is updated on every run anyway).  */
155 118 markom
int debug_regs_changed;
156
 
157 106 markom
/* Number of interrupts while waiting for process.  */
158
static int interrupt_count = 0;
159
 
160 113 markom
/* Reason of last stop.  */
161
static int hit_watchpoint = 0;
162
 
163 106 markom
/* Current register values.  */
164 118 markom
unsigned int dmr1 = 0;
165
unsigned int dmr2 = 0;
166
unsigned int dsr = 0;
167
unsigned int drr = 0;
168 106 markom
 
169 118 markom
/* Current matchpoints.  */
170
unsigned int dvr[MAX_MATCHPOINTS];
171
struct dcr_struct dcr[MAX_MATCHPOINTS];
172 106 markom
 
173 362 markom
/* Number of matchpoint users */
174 118 markom
int matchpoint_user_count[MAX_MATCHPOINTS] = {0};
175
 
176 362 markom
/* Old SIGINT handler.  */
177
static void (*ofunc) PARAMS ((int));
178
 
179
 
180 106 markom
/* Handle low-level error that we can't recover from.  Note that just
181
   error()ing out from target_wait or some such low-level place will cause
182
   all hell to break loose--the rest of GDB will tend to get left in an
183
   inconsistent state.  */
184
 
185
static NORETURN void
186
or1k_error (char *string,...)
187
{
188
  va_list args;
189
 
190
  va_start (args, string);
191
 
192
  target_terminal_ours ();
193
  wrap_here ("");               /* Force out any buffered output */
194
  gdb_flush (gdb_stdout);
195
  if (error_pre_print)
196
    fprintf_filtered (gdb_stderr, error_pre_print);
197
  vfprintf_filtered (gdb_stderr, string, args);
198
  fprintf_filtered (gdb_stderr, "\n");
199
  va_end (args);
200
  gdb_flush (gdb_stderr);
201
 
202
  /* Clean up in such a way that or1k_close won't try to talk to the
203
     board (it almost surely won't work since we weren't able to talk to
204
     it).  */
205
  or1k_is_open = 0;
206
 
207
  printf_unfiltered ("Ending remote or1k debugging.\n");
208
  target_mourn_inferior ();
209
 
210
  return_to_top_level (RETURN_ERROR);
211
}
212
 
213
const char *
214
or1k_err_name (e)
215
     int e;
216
{
217
  return str_err[e];
218
}
219
 
220
/* putc_readable - print a character, displaying non-printable chars in
221
   ^x notation or in hex.  */
222
 
223
static void
224
fputc_readable (ch, file)
225
     int ch;
226
     struct ui_file *file;
227
{
228
  if (ch == '\n')
229
    fputc_unfiltered ('\n', file);
230
  else if (ch == '\r')
231
    fprintf_unfiltered (file, "\\r");
232
  else if (ch < 0x20)           /* ASCII control character */
233
    fprintf_unfiltered (file, "^%c", ch + '@');
234
  else if (ch >= 0x7f)          /* non-ASCII characters (rubout or greater) */
235
    fprintf_unfiltered (file, "[%02x]", ch & 0xff);
236
  else
237
    fputc_unfiltered (ch, file);
238
}
239
 
240
 
241
/* puts_readable - print a string, displaying non-printable chars in
242
   ^x notation or in hex.  */
243
 
244
static void
245
fputs_readable (string, file)
246
     char *string;
247
     struct ui_file *file;
248
{
249
  int c;
250
 
251
  while ((c = *string++) != '\0')
252
    fputc_readable (c, file);
253
}
254
 
255 113 markom
/* Sets scan chain.  */
256
 
257
static void
258
or1k_set_chain (chain)
259
     int chain;
260
{
261
  if (current_or1k_target != NULL && current_or1k_target->to_set_chain != NULL)
262
    current_or1k_target->to_set_chain (chain);
263
}
264
 
265 106 markom
/* Sets register/memory regno to data.  */
266
 
267 113 markom
static void
268 106 markom
or1k_write_reg (regno, data)
269
     unsigned int regno;
270 122 markom
     ULONGEST data;
271 106 markom
{
272 113 markom
  if (current_or1k_target != NULL && current_or1k_target->to_write_reg != NULL)
273
    current_or1k_target->to_write_reg (regno, data);
274 106 markom
}
275
 
276
/* Reads register/memory from regno.  */
277
 
278 122 markom
static ULONGEST
279 106 markom
or1k_read_reg (regno)
280
     unsigned int regno;
281
{
282 113 markom
  if (current_or1k_target != NULL && current_or1k_target->to_read_reg != NULL)
283
    return current_or1k_target->to_read_reg (regno);
284 106 markom
  else
285
    return 0x1234;
286
}
287
 
288 113 markom
/* Sets SPR register regno to data.  */
289
 
290
void
291
or1k_write_spr_reg (regno, data)
292
     unsigned int regno;
293
     unsigned int data;
294
{
295
  or1k_set_chain (SC_RISC_DEBUG);
296 362 markom
  or1k_write_reg (regno, (ULONGEST)data);
297 113 markom
}
298
 
299
/* Reads register SPR from regno.  */
300
 
301
unsigned int
302
or1k_read_spr_reg (regno)
303
     unsigned int regno;
304
{
305
  or1k_set_chain (SC_RISC_DEBUG);
306 362 markom
  return or1k_read_reg (regno);
307 113 markom
}
308
 
309 362 markom
/* Sets mem to data.  */
310
 
311
void
312
or1k_write_mem (addr, data)
313
     unsigned int addr;
314
     unsigned int data;
315
{
316
  or1k_set_chain (SC_WISHBONE);
317
  or1k_write_reg (addr, (ULONGEST)data);
318
}
319
 
320
/* Reads register SPR from regno.  */
321
 
322
unsigned int
323
or1k_read_mem (addr)
324
     unsigned int addr;
325
{
326
  or1k_set_chain (SC_WISHBONE);
327
  return or1k_read_reg (addr);
328
}
329
 
330 106 markom
/* Stalls the CPU.  */
331
 
332
static void
333
or1k_stall ()
334
{
335 113 markom
  int val;
336
  or1k_set_chain (SC_REGISTER);
337
  val = or1k_read_reg (JTAG_RISCOP);
338
  or1k_write_reg (JTAG_RISCOP, val | 1);
339 362 markom
  or1k_read_reg (JTAG_RISCOP);
340 118 markom
 
341
  /* Be cautious - disable trace.  */
342
  val = or1k_read_reg (JTAG_MODER);
343
  or1k_write_reg (JTAG_MODER, val & ~2);
344 106 markom
}
345
 
346
/* Unstalls the CPU.  */
347
 
348
static void
349
or1k_unstall ()
350
{
351 113 markom
  unsigned int val;
352 143 chris
 
353 207 chris
 
354 113 markom
  or1k_set_chain (SC_REGISTER);
355
  val = or1k_read_reg (JTAG_RISCOP);
356
  or1k_write_reg (JTAG_RISCOP, val & ~1);
357 362 markom
  or1k_read_reg (JTAG_RISCOP);
358 106 markom
}
359
 
360 113 markom
/* Resets the CPU and stalls it.  */
361 118 markom
 
362 106 markom
static void
363 113 markom
or1k_reset ()
364
{
365
  unsigned int val;
366 362 markom
  int i;
367
  debug ("%08x\n", or1k_read_reg (JTAG_RISCOP));
368 113 markom
  or1k_set_chain (SC_REGISTER);
369 118 markom
 
370
  /* Be cautious - disable trace.  */
371
  val = or1k_read_reg (JTAG_MODER);
372
  or1k_write_reg (JTAG_MODER, val & ~2);
373
 
374 113 markom
  val = or1k_read_reg (JTAG_RISCOP);
375
  val &= ~3;
376 118 markom
  /* Assert reset signal.  */
377
  or1k_write_reg (JTAG_RISCOP, val | 3);
378
 
379 362 markom
  /* Just do something */
380
  for (i = 0; i < 100; i++)
381
    or1k_read_reg (JTAG_RISCOP);
382
 
383 118 markom
  /* give it some time */
384
  usleep (1000);
385 125 chris
 
386 362 markom
  or1k_set_chain (SC_REGISTER);
387 118 markom
  /* Release reset signal, but keep in stall state.  */
388
  or1k_write_reg (JTAG_RISCOP, val | 1);
389 362 markom
  or1k_read_reg (JTAG_RISCOP);
390 113 markom
}
391
 
392 118 markom
/* Synchronizes debug registers in memory with those on target,
393
   if there is any change.  */
394
 
395 113 markom
static void
396 118 markom
or1k_commit_debug_registers ()
397
{
398
  int i;
399
  unsigned int u;
400
  if (!debug_regs_changed)
401
    return;
402
 
403
  /* Matchpoints (breakpoints, watchpoints).  */
404
  for (i = 0; i < NUM_MATCHPOINTS; i++)
405
    {
406
      unsigned int u;
407
      or1k_write_spr_reg (DVR0_SPRNUM + i, dvr[i]);
408
      memcpy (&u, &dcr[i], sizeof(dcr[i]));
409
      or1k_write_spr_reg (DCR0_SPRNUM + i, u);
410
    }
411
  or1k_write_spr_reg (DMR1_SPRNUM, dmr1);
412
  or1k_write_spr_reg (DMR2_SPRNUM, dmr2);
413
 
414
  /* Trace dependent.  */
415
  or1k_set_chain (SC_REGISTER);
416
  memcpy (&u, &or1k_htrace.trig, sizeof(or1k_htrace.trig));
417
  or1k_write_reg (JTAG_TSEL, u);
418
  memcpy (&u, &or1k_htrace.qual, sizeof(or1k_htrace.qual));
419
  or1k_write_reg (JTAG_QSEL, u);
420
  memcpy (&u, &or1k_htrace.stop, sizeof(or1k_htrace.stop));
421
  or1k_write_reg (JTAG_SSEL, u);
422
  debug_regs_changed = 0;
423
  for (i = 0; i < NUM_RECORDS; i++)
424
    {
425
      memcpy (&u, &or1k_htrace.recwp[i], sizeof(or1k_htrace.recwp[i]));
426
      or1k_write_reg (JTAG_RECWP0 + i, u);
427
    }
428
  memcpy (&u, &or1k_htrace.recbp, sizeof(or1k_htrace.recbp));
429
  or1k_write_reg (JTAG_RECBP0, u);
430
  memcpy (&u, &or1k_htrace.moder, sizeof(or1k_htrace.moder));
431
  or1k_write_reg (JTAG_MODER, u);
432
}
433
 
434
static void
435 106 markom
or1k_set_undefined_cleanups (arg)
436
     PTR arg;
437
{
438
  or1k_status = TARGET_UNDEFINED;
439
}
440
 
441
/* Initialize a new connection to the or1k board, and make sure we are
442
   really connected.  */
443
 
444
static void
445
or1k_init (args)
446
     char *args;
447
{
448
  struct cleanup *old_cleanups = make_cleanup (or1k_set_undefined_cleanups, NULL);
449
  int i;
450 115 markom
  unsigned int tmp;
451 118 markom
  FILE *f;
452 106 markom
 
453
  /* What is this code doing here?  I don't see any way it can happen, and
454
     it might mean or1k_initializing didn't get cleared properly.
455
     So I'll make it a warning.  */
456
 
457
  if (or1k_status == TARGET_CONNECTING)
458
    {
459
      warning ("internal error: or1k_initialize called twice");
460
      return;
461
    }
462
 
463
  or1k_status = TARGET_CONNECTING;
464
  if (current_or1k_target != NULL && current_or1k_target->to_init != NULL)
465
    current_or1k_target->to_init (args);
466 122 markom
 
467 362 markom
  debug("%08x\n", read_pc ());
468
  or1k_stall ();
469
  debug("%08x\n", read_pc ());
470
  usleep (1000);
471
 
472 106 markom
  /* Determine implementation configuration.  */
473 113 markom
  or1k_implementation.VR = or1k_read_spr_reg (VR_SPRNUM);
474
  or1k_implementation.UPR = or1k_read_spr_reg (UPR_SPRNUM);
475 118 markom
 
476 115 markom
  /* Determine number of gpr_regs.  */
477
  tmp = or1k_read_spr_reg (CPUCFGR_SPRNUM);
478
  or1k_implementation.num_gpr_regs = ((tmp >> 4) & 1)?(16):(32);
479 118 markom
 
480 115 markom
  /* Is any vector or floating point support present? */
481
  or1k_implementation.vf_present = ((tmp >> 7) & 7) != 0;
482
  or1k_implementation.num_vfpr_regs = (or1k_implementation.vf_present)?(32):(0);
483 106 markom
 
484 115 markom
  /* Determine max number of supported matchpoints.  */
485
  tmp = or1k_read_spr_reg (DCFGR_SPRNUM);
486
  or1k_implementation.num_matchpoints = tmp & 7;
487 113 markom
  or1k_implementation.num_used_matchpoints = 0;
488 115 markom
  or1k_implementation.has_counters = tmp & 4 == 1;
489 106 markom
 
490
  /* Is implementation supported? */
491
 
492
  /* First we should have system and debug groups implemented. */
493
  if (or1k_implementation.VR & (1 << SPR_SYSTEM_GROUP) == 0)
494
    error ("System group should be available in the or1k implementation.");
495
  if (or1k_implementation.VR & (1 << SPR_DEBUG_GROUP) == 0)
496
    error ("Debug group should be available in the or1k implementation.");
497 115 markom
  if (or1k_implementation.has_counters)
498
    warning ("Counters not supported.");
499 106 markom
 
500
  /* Delete break, watch, catch points.  */
501
  for(i = 0; i < NUM_MATCHPOINTS; i++)
502 118 markom
    {
503
      memset (&dcr[i], 0, sizeof (dcr[i]));
504
      matchpoint_user_count[i] = 0;
505
    }
506 106 markom
 
507
  dmr1 = 0;
508
  dmr2 = 0;
509 118 markom
  memset (&or1k_htrace, 0, sizeof (or1k_htrace));
510
 
511
  /* RECSELDEPEND = 0 does not match our trace scheme. */
512
  or1k_htrace.moder.rec_sel_dep = 1;
513
 
514
  debug_regs_changed = 1;
515
  or1k_commit_debug_registers ();
516
 
517 106 markom
  if (err != 0)
518
    error ("Cannot connect.");
519
 
520 362 markom
  /* Enable exceptions */
521
  or1k_write_spr_reg (SR_SPRNUM, or1k_read_spr_reg(SR_SPRNUM) | 0x2);
522
 
523 106 markom
  /* Stop when breakpoint occurs.  */
524 362 markom
  or1k_write_spr_reg (DSR_SPRNUM, 0x2000);
525 106 markom
 
526
  do_cleanups (old_cleanups);
527
 
528
  /* This should cause an error if not connected.  */
529
  or1k_fetch_registers (-1);
530
 
531
  set_current_frame (create_new_frame (read_fp (), read_pc ()));
532
  select_frame (get_current_frame (), 0);
533 118 markom
 
534
  /* Just empty it.  */
535
  if ((f = fopen (TRACE_FILENAME, "wb+")) == NULL)
536
    error ("Cannot open trace file.");
537
  fclose (f);
538
  trace_size = 0;
539 363 markom
  or1k_status = TARGET_STOPPED;
540 106 markom
}
541
 
542
/* Kill the process running on the board.  */
543
 
544
void
545
or1k_kill ()
546
{
547
  if (or1k_status != TARGET_RUNNING)
548
    return;
549
  or1k_status = TARGET_UNDEFINED;
550 113 markom
  or1k_reset();
551
  or1k_status = TARGET_STOPPED;
552 106 markom
 
553
  inferior_pid = 0;
554
}
555
 
556
/* Open a connection to the remote board.  */
557
 
558
static void
559
or1k_open (name, from_tty)
560
     char *name;
561
     int from_tty;
562
{
563
  or1k_init (name);
564
 
565
  /* Switch to using remote target now.  */
566
  current_ops = current_or1k_target->gdb_ops;
567
  or1k_is_open = 1;
568
  push_target (current_ops);
569
 
570
  /* FIXME: Should we call start_remote here?  */
571
 
572
/* This is really the job of start_remote however, that makes an assumption
573
   that the target is about to print out a status message of some sort.  That
574
   doesn't happen here (in fact, it may not be possible to get the monitor to
575
   send the appropriate packet).  */
576
 
577
  flush_cached_frames ();
578
  registers_changed ();
579
  stop_pc = read_pc ();
580
  set_current_frame (create_new_frame (read_fp (), stop_pc));
581
  select_frame (get_current_frame (), 0);
582
  print_stack_frame (selected_frame, -1, 1);
583
}
584
 
585 362 markom
/* This is the generic stop called via the target vector. When a target
586
   interrupt is requested, either by the command line or the GUI, we
587
   will eventually end up here. */
588
static void
589
or1k_stop ()
590
{
591
  /* Send a break or a ^C, depending on user preference.  */
592
  debug ("remote_stop called\n");
593
 
594
  /* We should not stop the target immediately, since it can be in an
595
     unfinished state.  So we do a single step.  This should not affect
596
     on anything.  */
597
  or1k_stall ();
598
  /* HW STEP.  Set DMR1_ST.  */
599
  dmr1 |= DMR1_ST;
600
  or1k_write_spr_reg (DMR1_SPRNUM, dmr1);
601
  dmr1 &= ~DMR1_ST;
602
  or1k_unstall ();
603
}
604
 
605 106 markom
/* Close a connection to the remote board.  */
606
 
607
static void
608
or1k_close (quitting)
609
     int quitting;
610
{
611
  if (or1k_is_open)
612
    {
613
      or1k_kill ();
614
      if (current_or1k_target != NULL && current_or1k_target->to_done != NULL)
615
        current_or1k_target->to_done ();
616
      current_or1k_target = NULL;
617
    }
618
  generic_mourn_inferior ();
619
}
620
 
621
/* Detach from the remote board.  */
622
 
623
static void
624
or1k_detach (args, from_tty)
625
     char *args;
626
     int from_tty;
627
{
628
  if (args)
629
    error ("Argument given to \"detach\" when remotely debugging.");
630
 
631
  pop_target ();
632
 
633
  or1k_close (1);
634
 
635
  if (from_tty)
636
    printf_unfiltered ("Ending remote or1k debugging.\n");
637
}
638
 
639 118 markom
/* Appends trace data to the trace file.  */
640
 
641
static void
642
or1k_read_trace ()
643
{
644
  struct htrace_data_struct data;
645 122 markom
  ULONGEST tmp;
646 118 markom
  int first = 1;
647
  FILE *fd;
648
  if ((fd = fopen (TRACE_FILENAME, "ab")) == NULL)
649
    {
650
      warning ("Cannot append to trace file.");
651
      return;
652
    }
653
 
654
  or1k_set_chain (SC_TRACE);
655
  while (1)
656
    {
657
      tmp = or1k_read_reg (0);
658
      memcpy (&data, &tmp, sizeof (data));
659
 
660
      /* Last record reached. */
661
      if (!data.valid)
662
        break;
663
      data.valid = first;
664
      first = 0;
665
      if (!fwrite (&data, sizeof (data), 1, fd))
666
        {
667
          warning ("Cannot write trace data");
668
          break;
669
        }
670
    }
671
  fclose (fd);
672
}
673
 
674 362 markom
/* ^C Interrupt handling */
675
 
676
/* Ask the user what to do when an interrupt is received.  */
677
static void
678
interrupt_query ()
679
{
680
  target_terminal_ours ();
681
 
682
  if (query ("Interrupted while waiting for the program.\n\
683
Give up (and stop debugging it)? "))
684
    {
685
      target_mourn_inferior ();
686
      return_to_top_level (RETURN_QUIT);
687
    }
688
 
689
  target_terminal_inferior ();
690
}
691
 
692
static void or1k_interrupt_twice (int signo);
693
 
694
 
695
/* The command line interface's stop routine. This function is installed
696
   as a signal handler for SIGINT. The first time a user requests a
697
   stop, we call remote_stop to send a break or ^C. If there is no
698
   response from the target (it didn't stop when the user requested it),
699
   we ask the user if he'd like to detach from the target. */
700
static void
701
or1k_interrupt (signo)
702
     int signo;
703
{
704
        debug ("interrupt");
705
  /* If this doesn't work, try more severe steps. */
706
  signal (signo, or1k_interrupt_twice);
707
 
708
  debug ("remote_interrupt called\n");
709
 
710
  interrupt_count++;
711
}
712
 
713
/* The user typed ^C twice.  */
714
 
715
static void
716
or1k_interrupt_twice (signo)
717
     int signo;
718
{
719
        debug ("interrupt2");
720
  signal (signo, ofunc);
721
  interrupt_query ();
722
  signal (signo, or1k_interrupt);
723
}
724
 
725 106 markom
/* Resume execution of the target process.  STEP says whether to single-step
726
   or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
727
   to the target, or zero for no signal.  */
728
 
729
static void
730
or1k_resume (pid, step, siggnal)
731
     int pid, step;
732
     enum target_signal siggnal;
733 362 markom
{
734
        debug ("pc = %08x\n", read_pc());
735
  debug ("resume %i, %i, %i\n",step, siggnal, or1k_status);
736 106 markom
  if (or1k_status != TARGET_STOPPED)
737
    if (or1k_status == TARGET_RUNNING)
738
      error ("Program is already running.");
739
    else
740
      error ("The program is not being run.");
741
 
742 150 chris
 
743 106 markom
  /* Clear reason register for later.  */
744 113 markom
  or1k_write_spr_reg (DRR_SPRNUM, 0);
745 106 markom
 
746 362 markom
  or1k_commit_debug_registers ();
747
 
748 150 chris
  /* Else clause added by CZ 26/06/01 */
749 106 markom
  if (step)
750
    {
751
      /* HW STEP.  Set DMR1_ST.  */
752
      dmr1 |= DMR1_ST;
753 113 markom
      or1k_write_spr_reg (DMR1_SPRNUM, dmr1);
754 106 markom
      dmr1 &= ~DMR1_ST;
755
    }
756 150 chris
  else
757
    {
758
      dmr1 &= ~DMR1_ST;
759
      or1k_write_spr_reg (DMR1_SPRNUM, dmr1);
760
    }
761 106 markom
 
762 207 chris
  /* We can now continue normally, independent of step */
763 106 markom
  or1k_unstall ();
764
  or1k_status = TARGET_RUNNING;
765 362 markom
  debug ("-resume %i, %i, %i\n",step, siggnal, or1k_status);
766 106 markom
}
767
 
768
/* Wait until the remote stops, and return a wait status.  */
769
 
770
static int
771
or1k_wait (pid, status)
772
     int pid;
773
     struct target_waitstatus *status;
774
{
775 362 markom
  unsigned long val;
776
  unsigned long pc;
777
  char buf[MAX_REGISTER_RAW_SIZE];
778 106 markom
  interrupt_count = 0;
779
 
780 362 markom
  debug ("pc = %08x\n", read_pc());
781
  debug ("wait %i %i\n", pid, or1k_status);
782 106 markom
  /* If we have not sent a single step or continue command, then the
783
     board is waiting for us to do something.  Return a status
784
     indicating that it is stopped.  */
785
  if (or1k_status != TARGET_RUNNING)
786
    {
787
      if (or1k_status != TARGET_STOPPED)
788
        error("Target in invalid state.");
789
      status->kind = TARGET_WAITKIND_STOPPED;
790
      status->value.sig = TARGET_SIGNAL_TRAP;
791
      return 0;
792
    }
793
 
794
  if (err)
795
    or1k_error ("Remote failure: %s", or1k_err_name (err));
796
 
797 362 markom
  /* Set new signal handler */
798
  ofunc = signal (SIGINT, or1k_interrupt);
799 106 markom
 
800 362 markom
  /* Wait for risc to stop.  */
801
  do {
802
    or1k_set_chain (SC_REGISTER);
803
    val = or1k_read_reg (JTAG_RISCOP);
804
 
805
    /* When we press Ctrl-C, interrupt count is set, but we must wait
806
         for or1k_read_reg to finish, otherwise we would interrupt transaction.  */
807
    if (interrupt_count)
808
        or1k_stop ();
809 106 markom
 
810 362 markom
    usleep (10);
811
    debug ("%i", val);
812
  } while ((val & 1) == 0);
813
 
814
  drr = or1k_read_spr_reg (DRR_SPRNUM);
815
 
816
  /* Restore old INT signal handler */
817
  signal (SIGINT, ofunc);
818
 
819
  /* If we encounter breakpoint, drr is not set, so we set it manually.  */
820
  if (!drr)
821
    drr |= DRR_BE;
822
 
823
  status->kind = TARGET_WAITKIND_STOPPED;
824
 
825
  debug ("epcr0 = %08x\n", or1k_read_spr_reg (EPCR0_SPRNUM));
826
  debug ("drr = %08x\n", drr);
827
 
828 363 markom
  registers_changed ();
829 362 markom
  pc = read_pc ();
830 363 markom
  debug ("pc = %08x\n", pc);
831 362 markom
 
832 106 markom
  if (drr & DRR_RSTE)
833
    status->value.sig = TARGET_SIGNAL_REALTIME_33;
834
  else if (drr & DRR_BUSEE)
835
    status->value.sig = TARGET_SIGNAL_BUS;
836
  else if (drr & DRR_DPFE)
837
    status->value.sig = TARGET_SIGNAL_REALTIME_34;
838
  else if (drr & DRR_IPFE)
839
    status->value.sig = TARGET_SIGNAL_REALTIME_35;
840
  else if (drr & DRR_LPINTE)
841
    status->value.sig = TARGET_SIGNAL_INT;
842
  else if (drr & DRR_AE)
843
    status->value.sig = TARGET_SIGNAL_REALTIME_36;
844
  else if (drr & DRR_IIE)
845
    status->value.sig = TARGET_SIGNAL_ILL;
846
  else if (drr & DRR_HPINTE)
847
    status->value.sig = TARGET_SIGNAL_INT;
848
  else if (drr & DRR_DME)
849
    status->value.sig = TARGET_SIGNAL_REALTIME_37;
850
  else if (drr & DRR_IME)
851
    status->value.sig = TARGET_SIGNAL_REALTIME_38;
852
  else if (drr & DRR_RE)
853
    status->value.sig = TARGET_SIGNAL_REALTIME_39;
854
  else if (drr & DRR_SCE)
855
    status->value.sig = TARGET_SIGNAL_REALTIME_40;
856
  else if (drr & DRR_BE)
857
    status->value.sig = TARGET_SIGNAL_TRAP;
858 363 markom
  else if (drr & DRR_TE)
859 106 markom
    {
860 362 markom
      /* PC has already stepped over the l.trap instruction.  */
861 363 markom
      pc -= 8;
862 362 markom
      status->value.sig = TARGET_SIGNAL_TRAP;
863
    } else {
864 106 markom
      status->value.sig = TARGET_SIGNAL_UNKNOWN;
865
      warning ("Invalid exception occured.");
866
    }
867
 
868 362 markom
  /* Write into PC flushes the pipeline! */
869
  /* We got the number the register holds, but gdb expects to see a
870
     value in the target byte ordering.  */
871 363 markom
  write_pc (pc);
872
  /*or1k_write_spr_reg (PC_SPRNUM, pc);
873 362 markom
  store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), pc);
874 363 markom
  supply_register (PC_REGNUM, buf);*/
875 362 markom
 
876 113 markom
  /* Log remote stop.  */
877 106 markom
  or1k_status = TARGET_STOPPED;
878 113 markom
 
879
  /* Determine what caused trap - breakpoint or watchpoint.  */
880
  if (status->value.sig == TARGET_SIGNAL_TRAP)
881
    {
882
      /* Search all active breakpoints for a match.  */
883
      CORE_ADDR pc = read_pc ();
884
      int breakpoint = 0;
885
      int i;
886 150 chris
      unsigned char break_bytes[4] = BRK_INSTR_STRUCT;
887
      unsigned long b_insn = ntohl(*((unsigned long*)break_bytes));
888 151 chris
      unsigned long value = pc;
889 143 chris
 
890 113 markom
      for (i = 0; i < or1k_implementation.num_used_matchpoints; i++)
891 118 markom
        if (dvr[i] == pc && dcr[i].dp && dcr[i].cc == CC_EQUAL
892
            && !dcr[i].sc && dcr[i].ct == CT_FETCH)
893 113 markom
          {
894
            breakpoint = 1;
895
            break;
896
          }
897
      hit_watchpoint = !breakpoint;
898 143 chris
 
899
      /* Cause the trap/breakpoint exception to be ignored. This is
900
         the behavior of the simulator when the PC value is changed
901
         by a write command. All pending exceptions are cleared and
902 150 chris
         the simulator continues at the PC value specified. We need
903
         to do this if the instruction at the current PC has the
904
         value BRK_INSTR_STRUCT */
905
 
906 362 markom
      if(b_insn == or1k_read_mem((pc & 3)))
907 150 chris
        {
908
          or1k_write_spr_reg(PC_SPRNUM,value);
909
        }
910 113 markom
    }
911
  else
912
    hit_watchpoint = 0;
913 106 markom
 
914
  /* If the stop PC is in the _exit function, assume
915
     we hit the 'break 0x3ff' instruction in _exit, so this
916
     is not a normal breakpoint.  */
917
  {
918
    char *func_name;
919
    CORE_ADDR func_start;
920
    CORE_ADDR pc = read_pc ();
921
 
922
    find_pc_partial_function (pc, &func_name, &func_start, NULL);
923
    if (func_name != NULL && strcmp (func_name, "_exit") == 0
924
        && func_start == pc)
925
      status->kind = TARGET_WAITKIND_EXITED;
926
  }
927 118 markom
 
928
  or1k_read_trace ();
929 362 markom
  debug ("-wait %i %i\n", pid, or1k_status);
930 106 markom
  return 0;
931
}
932
 
933 113 markom
/* Fetch a word from the target board.  All memory accesses to the
934
   remote board are word aligned.  */
935 106 markom
 
936 118 markom
unsigned int
937 106 markom
or1k_fetch_word (addr)
938
     CORE_ADDR addr;
939
{
940 113 markom
  if (addr & 3)
941
    {
942
      int subaddr = addr & 3;
943
      unsigned char buf[8];
944
      unsigned int low, high;
945
      addr >>= 2;
946 362 markom
      low = or1k_read_mem (addr << 2);
947
      high = or1k_read_reg ((addr + 1) << 2);
948 113 markom
      memcpy (&buf[0], &low, 4);
949
      memcpy (&buf[4], &high, 4);
950
      memcpy (&low, &buf[subaddr], 4);
951
      return low;
952
    }
953
  else
954
    {
955 362 markom
      return or1k_read_mem (addr);
956 113 markom
    }
957 106 markom
}
958
 
959
/* Store a word to the target board.  Returns errno code or zero for
960 113 markom
   success.  All memory accesses to the remote board are word aligned.  */
961 118 markom
 
962 106 markom
static int
963
or1k_store_word (addr, val)
964
     CORE_ADDR addr;
965
     unsigned int val;
966
{
967 113 markom
  if (addr & 3)
968
    {
969
      int subaddr = addr & 3;
970
      unsigned char buf[8];
971
      unsigned int low, high;
972
      addr >>= 2;
973 362 markom
      low = or1k_read_mem (addr << 2);
974
      high = or1k_read_mem ((addr + 1) << 2);
975 113 markom
      memcpy (&buf[0], &low, 4);
976
      memcpy (&buf[4], &high, 4);
977
      memcpy (&buf[subaddr], &val, 4);
978
      memcpy (&low, &buf[0], 4);
979
      memcpy (&high, &buf[4], 4);
980 362 markom
      or1k_write_mem (addr << 2, low);
981
      or1k_write_mem ((addr + 1) << 2, high);
982 113 markom
    }
983
  else
984 362 markom
    {
985
      or1k_write_mem (addr, val);
986 113 markom
    }
987 106 markom
  return err;
988
}
989
 
990
/* Fetch the remote registers.  */
991
 
992
void
993
or1k_fetch_registers (regno)
994
     int regno;
995
{
996
  unsigned int val;
997
 
998
  if (regno == -1)
999
    {
1000
      for (regno = 0; regno < NUM_REGS; regno++)
1001
        or1k_fetch_registers (regno);
1002
      return;
1003
    }
1004
 
1005
  if (regno >= NUM_REGS)
1006
    error("Invalid register number!");
1007
 
1008
  /* Convert to SPRNUM and read.  */
1009 113 markom
  val = or1k_read_spr_reg (REGNUM_TO_SPRNUM(regno));
1010 106 markom
 
1011
  {
1012
    char buf[MAX_REGISTER_RAW_SIZE];
1013
 
1014
    /* We got the number the register holds, but gdb expects to see a
1015
       value in the target byte ordering.  */
1016
    store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1017
    supply_register (regno, buf);
1018
  }
1019
  if (err)
1020 118 markom
    or1k_error ("Can't read register %d(%i): %s", regno,
1021 362 markom
                REGNUM_TO_SPRNUM(regno), or1k_err_name (err));
1022 106 markom
}
1023
 
1024
/* Fetch and return instruction from the specified location.  */
1025
 
1026
unsigned int
1027
or1k_fetch_instruction (addr)
1028
     CORE_ADDR addr;
1029
{
1030
  char buf[OR1K_INSTLEN];
1031
  int status;
1032
 
1033
  status = read_memory_nobpt (addr, buf, OR1K_INSTLEN);
1034
  if (status)
1035
    memory_error (status, addr);
1036
  return extract_unsigned_integer (buf, OR1K_INSTLEN);
1037
}
1038
 
1039 113 markom
/* Currently not needed.  */
1040
 
1041 106 markom
static void
1042
or1k_prepare_to_store ()
1043
{
1044
}
1045
 
1046
/* Store remote register(s).  */
1047
 
1048
static void
1049
or1k_store_registers (regno)
1050
     int regno;
1051
{
1052
  if (regno == -1)
1053
    {
1054
      for (regno = 0; regno < NUM_REGS; regno++)
1055
        or1k_store_registers (regno);
1056
      return;
1057
    }
1058
 
1059
  if (regno >= NUM_REGS)
1060
    error("Invalid register number!");
1061
 
1062 362 markom
  or1k_write_spr_reg (REGNUM_TO_SPRNUM(regno), read_register (regno));
1063 106 markom
  if (err)
1064 113 markom
    or1k_error ("Can't write register %d(%i): %s", regno, REGNUM_TO_SPRNUM(regno), or1k_err_name (err));
1065 106 markom
}
1066
 
1067
/* Read or write LEN bytes from inferior memory at MEMADDR,
1068
   transferring to or from debugger address MYADDR.  Write to inferior
1069
   if SHOULD_WRITE is nonzero.  Returns length of data written or
1070
   read; 0 for error.  Note that protocol gives us the correct value
1071
   for a longword, since it transfers values in ASCII.  We want the
1072
   byte values, so we have to swap the longword values.  */
1073
 
1074
static int
1075
or1k_xfer_memory (memaddr, myaddr, len, write, ignore)
1076
     CORE_ADDR memaddr;
1077
     char *myaddr;
1078
     int len;
1079
     int write;
1080
     struct target_ops *ignore;
1081
{
1082
  register int i;
1083
  /* Round starting address down to longword boundary.  */
1084
  register CORE_ADDR addr = memaddr & ~3;
1085
  /* Round ending address up; get number of longwords that makes.  */
1086
  register int count = (((memaddr + len) - addr) + 3) / 4;
1087
  /* Allocate buffer of that many longwords.  */
1088
  register char *buffer = alloca (count * 4);
1089
  int status;
1090
 
1091 143 chris
  int block_xfer_size = 256; /* CZ 21/06/01 ... number of 32 bit words */
1092
  int nBlocks = (count + block_xfer_size -1)/block_xfer_size;
1093
  int terminate = 0;  /* Terminate the printing of '*'s... */
1094
 
1095 362 markom
#ifdef DEBUG_JTAG
1096
  debug ("xfer_memory %s addr=%x, len=%i, \n", write?"write":"read", memaddr, len);
1097
  fflush(stdout);
1098
#endif
1099
 
1100
#if 0
1101 106 markom
  if (memaddr >= MEM_SPACE)
1102
    error("Invalid address");
1103 362 markom
#endif
1104 106 markom
 
1105 135 chris
  /* (CZ 21/06/01 -- because upper layers which know nothing about
1106
     Or1k or JTAG call this function directly, it is always necessary
1107
     to set the chain to point to the Debug Unit. Otherwise, it may
1108 143 chris
     be pointing to the Development Interface chain, in which case
1109 135 chris
     we're going to get bupkiss... */
1110
 
1111 106 markom
  if (write)
1112
    {
1113
      /* Fill start and end extra bytes of buffer with existing data.  */
1114
      if (addr != memaddr || len < 4)
1115
        {
1116
          /* Need part of initial word -- fetch it.  */
1117
          store_unsigned_integer (&buffer[0], 4, or1k_fetch_word (addr));
1118
        }
1119
 
1120
      if (count > 1)
1121
        {
1122
          /* Need part of last word -- fetch it.  FIXME: we do this even
1123
             if we don't need it.  */
1124
          store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1125
                                  or1k_fetch_word (addr + (count - 1) * 4));
1126
        }
1127
 
1128
      /* Copy data to be written over corresponding part of buffer */
1129
      memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1130
 
1131 143 chris
      /* CZ: rewrote the block transfer routines to make the code
1132
         a little more efficient for implementations that can handle
1133
         variable sized scan chains. Might be useful in the future.
1134
         Certainly makes downloads to the simulator more efficient. */
1135
      for(i=0;i<nBlocks;i++,count-=block_xfer_size,addr += block_xfer_size*4)
1136 106 markom
        {
1137 143 chris
          int j;
1138
          int n = count < block_xfer_size ? count : block_xfer_size;
1139
          unsigned long *__buf;
1140
 
1141
          if(!(__buf = (unsigned long*)malloc(n*sizeof(unsigned long))))
1142 106 markom
            {
1143 143 chris
              errno = ERR_MEM;
1144
              return 0;
1145
            }
1146
 
1147
          for(j=0;j<n;j++)
1148
            __buf[j] = (unsigned long)extract_unsigned_integer(&buffer[(i * block_xfer_size +j)*4], 4);
1149 362 markom
          or1k_set_chain (SC_WISHBONE);
1150 143 chris
          status = or1k_store_block(addr,__buf,n);
1151
          free(__buf);
1152
          if(n == block_xfer_size)
1153
            {
1154 362 markom
              debug ("*");
1155 106 markom
              gdb_flush (gdb_stdout);
1156
            }
1157
          if (status)
1158
            {
1159
              errno = status;
1160
              return 0;
1161
            }
1162
          /* FIXME: Do we want a QUIT here?  */
1163
        }
1164 143 chris
      if (terminate)
1165 362 markom
        debug ("\n");
1166 106 markom
    }
1167
  else
1168
    {
1169 143 chris
      for(i=0;i<nBlocks;i++,count-=block_xfer_size,addr += block_xfer_size*4)
1170 106 markom
        {
1171 143 chris
          int j;
1172
          int n = count < block_xfer_size ? count : block_xfer_size;
1173
          unsigned long *__buf;
1174
 
1175 362 markom
    or1k_set_chain (SC_WISHBONE);
1176 143 chris
          __buf = (unsigned long*)malloc(n*sizeof(unsigned long));
1177
          status = or1k_load_block(addr,__buf,n);
1178
          if (!status)
1179
            for(j=0;j<n;j++)
1180
              store_unsigned_integer (&buffer[(i * block_xfer_size +j)*4], 4, __buf[j]);
1181
          else
1182
            errno = status;
1183
          free(__buf);
1184
 
1185
          if(status)
1186
            return 0;
1187 106 markom
        }
1188
      /* Copy appropriate bytes out of the buffer.  */
1189
      memcpy (myaddr, buffer + (memaddr & 3), len);
1190
    }
1191
  return len;
1192
}
1193
 
1194 143 chris
int or1k_load_block(CORE_ADDR addr,void* buffer,int nRegisters)
1195
{
1196
  int i=0;
1197 362 markom
  unsigned int regno = addr;
1198 143 chris
 
1199
  if (current_or1k_target != NULL && current_or1k_target->to_read_block != NULL)
1200
    return current_or1k_target->to_read_block (regno,buffer,nRegisters);
1201
  else
1202
    for(i=0;i<nRegisters;i++)
1203
      ((unsigned long*)buffer)[i] = 0x1234;
1204
  return 0;
1205
}
1206
 
1207
int or1k_store_block(CORE_ADDR addr,void* buffer,int nRegisters)
1208
{
1209 362 markom
  unsigned int regno = addr;
1210
 
1211 143 chris
  if (current_or1k_target != NULL && current_or1k_target->to_write_block != NULL)
1212
    return current_or1k_target->to_write_block (regno,buffer,nRegisters);
1213
  return 0;
1214
}
1215
 
1216 106 markom
/* Print info on this target.  */
1217
 
1218
static void
1219
or1k_files_info (ignore)
1220
     struct target_ops *ignore;
1221
{
1222
  char *file = "nothing";
1223
 
1224
  if (exec_bfd)
1225
    file = bfd_get_filename (exec_bfd);
1226
 
1227
  printf_filtered ("or1k_files_info: file \"%s\"\n", file);
1228
 
1229
  if (exec_bfd)
1230
    {
1231
      printf_filtered ("\tAttached to %s running program %s\n",
1232
                       target_shortname, file);
1233
    }
1234
  /* Print target info. */
1235
  printf_filtered ("Status: %s\n", status_name[or1k_status]);
1236
}
1237
 
1238
/* Tell whether we can support a hardware breakpoint.  */
1239 118 markom
 
1240 106 markom
static int
1241
or1k_can_use_hardware_breakpoint ()
1242
{
1243
  int i;
1244 118 markom
 
1245 106 markom
  /* Search for unused breakpoint.  */
1246 113 markom
  return or1k_implementation.num_used_matchpoints < or1k_implementation.num_matchpoints;
1247 106 markom
}
1248
 
1249
/* Insert a breakpoint.  On targets that don't have built-in breakpoint
1250
   support, we read the contents of the target location and stash it,
1251
   then overwrite it with a breakpoint instruction.  ADDR is the target
1252
   location in the target machine.  CONTENTS_CACHE is a pointer to
1253
   memory allocated for saving the target contents.  It is guaranteed
1254
   by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1255
   is accomplished via BREAKPOINT_MAX).  */
1256
 
1257 113 markom
int
1258 106 markom
or1k_insert_breakpoint (addr, contents_cache)
1259
     CORE_ADDR addr;
1260
     char *contents_cache;
1261
{
1262
  if (or1k_can_use_hardware_breakpoint())
1263
    return set_breakpoint (addr);
1264
  else
1265
    return memory_insert_breakpoint (addr, contents_cache);
1266
}
1267
 
1268 113 markom
int
1269 106 markom
or1k_remove_breakpoint (addr, contents_cache)
1270
     CORE_ADDR addr;
1271
     char *contents_cache;
1272
{
1273
  /* First try to remove HW breakpoint at address */
1274
  if (clear_breakpoint (addr))
1275
    return memory_remove_breakpoint (addr, contents_cache);
1276
  else
1277
    return 0;
1278
}
1279
 
1280 113 markom
/* Tell whether this target can support a hardware breakpoint.  CNT
1281
   is the number of hardware breakpoints already installed.  This
1282
   implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro.
1283
   Lower bound is estimated. !!! Can we estimate better? */
1284
 
1285
int
1286
or1k_can_use_hardware_watchpoint (bp_type, cnt)
1287
     enum bptype bp_type;
1288
     int cnt;
1289
{
1290
  /* Are there at least two matchpoints left for watch? - estimate lower bound  */
1291
  return cnt + ((bp_type == bp_hardware_watchpoint)?(1):(0))
1292
    <= or1k_implementation.num_matchpoints;
1293
}
1294
 
1295 118 markom
/* Moves matchpoint.  This is very tricky - we have to update
1296
   all references to matchpoint indexes.  We assume here that
1297
   matchpoint with index to is unused! */
1298
 
1299
static void
1300
move_matchpoint (int from, int to)
1301
{
1302
  int i, j, tmp, chaining;
1303
  if (matchpoint_user_count[to] != 0)
1304
    error ("Internal: Destination matchpoint still has users");
1305
  matchpoint_user_count[to] = matchpoint_user_count[from];
1306
  matchpoint_user_count[from] = 0;
1307
  debug_regs_changed = 1;
1308
 
1309
  dvr[to] = dvr[from];
1310
  dcr[to] = dcr[from];
1311
  dcr[from].dp = 0;
1312
 
1313
  /* Copy chaining bits.  */
1314
  chaining = dmr1 & (3 << (2 * from));
1315
  dmr1 &= ~(3 << (2 * to));
1316
  dmr1 |= chaining << (2 * to);
1317
  dmr1 &= ~(3 << (2 * from));
1318
 
1319
  /* Copy watchpoint bits */
1320
  tmp = dmr2 & (1 << from);
1321
  dmr2 &= 1 << to;
1322
  dmr2 |= tmp << to;
1323
  dmr2 &= 1 << from;
1324
 
1325
  /* Update hwatch table.  Here we assume that matchpoint
1326
     group is connected (it cannot be implemented in HW
1327
     otherwise), so if we move first, we will have to move
1328
     others later.  */
1329
  for (i = 0; i < num_hw_watches; i++)
1330
    if (or1k_hwatch[i].matchpoint_start == from)
1331
      or1k_hwatch[i].matchpoint_start = to;
1332
 
1333
  /* Update htrace struct.  */
1334
  tmp = or1k_htrace.trig.wp_trig & (1 << from);
1335
  or1k_htrace.trig.wp_trig &= 1 << to;
1336
  or1k_htrace.trig.wp_trig |= tmp << to;
1337
  or1k_htrace.trig.wp_trig &= 1 << from;
1338
 
1339
  tmp = or1k_htrace.qual.wp_trig & (1 << from);
1340
  or1k_htrace.qual.wp_trig &= 1 << to;
1341
  or1k_htrace.qual.wp_trig |= tmp << to;
1342
  or1k_htrace.qual.wp_trig &= 1 << from;
1343
 
1344
  tmp = or1k_htrace.stop.wp_trig & (1 << from);
1345
  or1k_htrace.stop.wp_trig &= 1 << to;
1346
  or1k_htrace.stop.wp_trig |= tmp << to;
1347
  or1k_htrace.stop.wp_trig &= 1 << from;
1348
 
1349
  for (i = 0; i < MAX_MATCHPOINTS; i++)
1350
    {
1351
      tmp = or1k_htrace.wp_record_uses[i] & (1 << from);
1352
      or1k_htrace.wp_record_uses[i] &= 1 << to;
1353
      or1k_htrace.wp_record_uses[i] |= tmp << to;
1354
      or1k_htrace.wp_record_uses[i] &= 1 << from;
1355
    }
1356
 
1357
  /* Do we need to move other references also? */
1358
}
1359
 
1360 113 markom
/* Sifts unused matchpoints to higher indexses.  */
1361
 
1362 118 markom
void
1363 113 markom
sift_matchpoints ()
1364
{
1365
  int i, first_free = 0;
1366
  for (i = 0; i < or1k_implementation.num_matchpoints; i++)
1367
    if (dcr[i].dp)
1368
      {
1369 118 markom
        /* Move references.  */
1370
        move_matchpoint (i, first_free);
1371
 
1372 113 markom
        first_free++;
1373
      }
1374 118 markom
 
1375
  /* Unused matchpoints should be disabled by move_matchpoint,
1376
     so we are done here.  */
1377 113 markom
}
1378
 
1379
/* Translates gdb watchpoint type into one in DCR register.  */
1380
 
1381
static int
1382
translate_type (gdb_type)
1383
     int gdb_type;
1384
{
1385
  switch (gdb_type)
1386
    {
1387
    case 0:
1388
      return CT_SDATA;
1389
    case 1:
1390
      return CT_LDATA;
1391
    case 2:
1392
      return CT_ADATA;
1393
    default:
1394
      error ("Invalid type.");
1395
    }
1396
}
1397
 
1398
/* Set a data watchpoint.  ADDR and LEN should be obvious.  TYPE is 0
1399
   for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1400
   watchpoint. */
1401
 
1402
int
1403
or1k_insert_watchpoint (addr, len, type)
1404
     CORE_ADDR addr;
1405
     int len;
1406
     int type;
1407
{
1408
  int i;
1409 118 markom
 
1410 113 markom
  if (len < 1)
1411
    return -1;
1412
 
1413
  type = translate_type (type);
1414
 
1415
  /* Moves unused watchpoints to the top.  */
1416
  sift_matchpoints ();
1417 118 markom
 
1418 113 markom
  /* Place at first free matchpoint.  */
1419
  i = or1k_implementation.num_used_matchpoints;
1420
  dvr[i] = addr;
1421
  dcr[i].dp = 1;
1422
  dcr[i].cc = CC_GREATE;
1423
  dcr[i].sc = 0;
1424
  dcr[i].ct = type;
1425 118 markom
 
1426 113 markom
  /* Set && chaining here.  */
1427
  dmr1 &= ~(3 << (2 * i));
1428 118 markom
  dmr1 |= CHAINING_AND << (2 * i);
1429
 
1430 113 markom
  /* Set upper watchpoint bound.  */
1431
  i++;
1432
  dvr[i] = addr + len - 1;
1433
  dcr[i].dp = 1;
1434
  dcr[i].cc = CC_LESSE;
1435
  dcr[i].sc = 0;
1436
  dcr[i].ct = type;
1437 118 markom
 
1438 113 markom
  /* Matchpoints will cause breakpoints */
1439 118 markom
  dmr2 |= (1 << i);
1440 113 markom
  or1k_implementation.num_used_matchpoints += 2;
1441 118 markom
  debug_regs_changed = 1;
1442 113 markom
  return 0;
1443
}
1444
 
1445
/* Removes a data watchpoint.  ADDR and LEN should be obvious.  TYPE is 0
1446
   for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1447
   watchpoint. */
1448 118 markom
 
1449 113 markom
int
1450
or1k_remove_watchpoint (addr, len, type)
1451
     CORE_ADDR addr;
1452
     int len;
1453
     int type;
1454
{
1455
  int i, found = -1;
1456 118 markom
 
1457 113 markom
  if (len < 1)
1458
    return -1;
1459
 
1460
  type = translate_type (type);
1461
 
1462
  /* Find the right one.  */
1463
  for (i = 0; i < or1k_implementation.num_used_matchpoints; i++)
1464
    if (dvr[i] == addr && dcr[i].dp && dcr[i].cc == CC_GREATE && !dcr[i].sc && dcr[i].ct == type
1465
        && dvr[i + 1] == addr + len - 1 && dcr[i + 1].dp && dcr[i + 1].cc == CC_LESSE
1466
        && !dcr[i + 1].sc && dcr[i + 1].ct == type)
1467
      {
1468
        found = i;
1469
        break;
1470
      }
1471
 
1472
  if (found < 0)
1473
    return -1;
1474
 
1475
  dcr[found].dp = 0;
1476
  dcr[found + 1].dp = 0;
1477
 
1478
  /* Matchpoints will not cause breakpoints anymore. */
1479 118 markom
  dmr2 &= ~(1 << i);
1480 113 markom
  or1k_implementation.num_used_matchpoints -= 2;
1481 118 markom
  debug_regs_changed = 1;
1482 113 markom
  return 0;
1483
}
1484
 
1485
int
1486
or1k_stopped_by_watchpoint (void)
1487
{
1488 143 chris
  /* For now, no watchpoints */
1489
  return 0;
1490
 
1491
  /* return hit_watchpoint; */
1492 113 markom
}
1493
 
1494 106 markom
/* Insert a breakpoint.  */
1495
 
1496
int
1497
set_breakpoint (addr)
1498
     CORE_ADDR addr;
1499
{
1500
  int i;
1501 118 markom
 
1502 106 markom
  /* Search for unused breakpoint.  */
1503
  for (i = 0; i < NUM_MATCHPOINTS; i++)
1504
    if (dcr[i].dp == 0) break;
1505
  if (i >= NUM_MATCHPOINTS) return 1;
1506
  dvr[i] = addr;
1507
  dcr[i].dp = 1;
1508
  dcr[i].cc = CC_EQUAL;
1509
  dcr[i].sc = 0;
1510
  dcr[i].ct = CT_FETCH;
1511 113 markom
  or1k_implementation.num_used_matchpoints++;
1512
 
1513
  /* No chaining here.  */
1514
  dmr1 &= ~(3 << (2*i));
1515 118 markom
 
1516 113 markom
  /* Matchpoints will cause breakpoints */
1517 118 markom
  dmr2 |= (1 << i);
1518
  debug_regs_changed = 1;
1519 106 markom
  return 0;
1520
}
1521
 
1522
/* Clear a breakpoint.  */
1523
 
1524
int
1525
clear_breakpoint (addr)
1526
     CORE_ADDR addr;
1527
{
1528
  int i;
1529 118 markom
 
1530 106 markom
  /* Search for matching breakpoint.  */
1531
  for (i = 0; i < NUM_MATCHPOINTS; i++)
1532
    if ((dcr[i].dp == 1) && (dvr[i] == addr) && (dcr[i].cc == CC_EQUAL)
1533
        && (dcr[i].sc == 0) && (dcr[i].ct == CT_FETCH)) break;
1534
 
1535
  if (i >= NUM_MATCHPOINTS) return 1;
1536
  dcr[i].dp = 0;
1537 118 markom
 
1538 113 markom
  /* Matchpoints will cause breakpoints */
1539 118 markom
  dmr2 &= ~(1 << i);
1540 113 markom
  or1k_implementation.num_used_matchpoints--;
1541 118 markom
  debug_regs_changed = 1;
1542 106 markom
  return 0;
1543
}
1544
 
1545
/* Start running on the target board.  */
1546
 
1547
static void
1548
or1k_create_inferior (execfile, args, env)
1549
     char *execfile;
1550
     char *args;
1551
     char **env;
1552
{
1553
  CORE_ADDR entry_pt;
1554
 
1555
  if (args && *args)
1556
    {
1557 118 markom
      warning ("Can't pass arguments to remote OR1K board; arguments ignored.");
1558
 
1559 106 markom
      /* And don't try to use them on the next "run" command.  */
1560
      execute_command ("set args", 0);
1561
    }
1562
 
1563
  if (execfile == 0 || exec_bfd == 0)
1564
    error ("No executable file specified");
1565
 
1566
  or1k_kill ();
1567
  remove_breakpoints ();
1568
 
1569
  entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1570
  init_wait_for_inferior ();
1571
 
1572
  /* FIXME: Should we set inferior_pid here?  */
1573 113 markom
 
1574 118 markom
  /* Needed to get correct instruction in cache */
1575
  insert_breakpoints ();
1576 106 markom
  clear_proceed_status ();
1577
  or1k_status = TARGET_STOPPED;
1578
  proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
1579
}
1580
 
1581
/* Clean up after a process.  Actually nothing to do.  */
1582
 
1583
static void
1584
or1k_mourn_inferior ()
1585
{
1586
  generic_mourn_inferior ();
1587
}
1588
 
1589
static void
1590
or1k_dummy_open (name, from_tty)
1591
     char *name;
1592
     int from_tty;
1593
{
1594
  target_preopen (from_tty);
1595
  if (or1k_is_open)
1596
    unpush_target (current_ops);
1597
  current_or1k_target = &or1k_target_dummy;
1598
  or1k_open (name, from_tty);
1599
}
1600
 
1601
static void
1602
or1k_jtag_open (name, from_tty)
1603
     char *name;
1604
     int from_tty;
1605
{
1606
  target_preopen (from_tty);
1607
  if (or1k_is_open)
1608
    unpush_target (current_ops);
1609
  current_or1k_target = &or1k_target_jtag;
1610
  or1k_open (name, from_tty);
1611
}
1612
 
1613
static void
1614
or1k_sim_open (name, from_tty)
1615
     char *name;
1616
     int from_tty;
1617
{
1618
  /* target_preopen (from_tty); - do we need this ? */
1619
  if (or1k_is_open)
1620
    unpush_target (current_ops);
1621
  current_or1k_target = &or1k_target_sim;
1622
  or1k_open (name, from_tty);
1623
}
1624
 
1625
/* Executes command on the target.  */
1626
 
1627
void
1628
or1k_sim_cmd (char *args, int from_tty)
1629
{
1630
  if (current_or1k_target != NULL && current_or1k_target->to_exec_command != NULL)
1631
    current_or1k_target->to_exec_command (args, from_tty);
1632
  else
1633
    error ("Command not supported on this target. ");
1634
}
1635
 
1636 113 markom
/* Displays matchpoints usage.  */
1637
 
1638 106 markom
void
1639 113 markom
info_matchpoints_command (char *args, int from_tty)
1640
{
1641
  int i;
1642
  for (i = 0; i < or1k_implementation.num_matchpoints; i++)
1643
    {
1644
      printf_filtered ("WP%i ", i);
1645
      if (dcr[i].dp)
1646
        {
1647
          int chaining = (dmr1 << 2*i) & 3;
1648
          printf_filtered ("= %s ", ct_names[dcr[i].ct]);
1649
          if (dcr[i]. sc)
1650
            printf_filtered ("s%s %i", cc_names[dcr[i].cc], (int)dvr[i]);
1651
          else
1652
            printf_filtered ("%s %u", cc_names[dcr[i].cc], (unsigned int)dvr[i]);
1653
          if (chaining)
1654
            printf_filtered ("%s WP%i", ch_names[chaining], i - 1);
1655
        }
1656
      else
1657
        printf_filtered ("NOT USED");
1658
      if ((dmr2 >> i) & 1)
1659
        printf_filtered (", causes breakpoint");
1660
      if ((dmr2 >> (i + 11)) & 1)
1661
        printf_filtered (", increments counter");
1662
      printf_filtered ("\n");
1663
    }
1664
}
1665
 
1666
void
1667 106 markom
_initialize_remote_or1k ()
1668
{
1669
  /* Initialize the fields in or1k_ops that are common to all targets.  */
1670
  or1k_dummy_ops.to_close = or1k_close;
1671
  or1k_dummy_ops.to_detach = or1k_detach;
1672
  or1k_dummy_ops.to_resume = or1k_resume;
1673 362 markom
  or1k_dummy_ops.to_wait = or1k_wait;
1674 106 markom
  or1k_dummy_ops.to_fetch_registers = or1k_fetch_registers;
1675
  or1k_dummy_ops.to_store_registers = or1k_store_registers;
1676
  or1k_dummy_ops.to_prepare_to_store = or1k_prepare_to_store;
1677
  or1k_dummy_ops.to_xfer_memory = or1k_xfer_memory;
1678
  or1k_dummy_ops.to_files_info = or1k_files_info;
1679
  or1k_dummy_ops.to_insert_breakpoint = or1k_insert_breakpoint;
1680
  or1k_dummy_ops.to_remove_breakpoint = or1k_remove_breakpoint;
1681
  or1k_dummy_ops.to_kill = or1k_kill;
1682
  or1k_dummy_ops.to_load = generic_load;
1683
  or1k_dummy_ops.to_create_inferior = or1k_create_inferior;
1684
  or1k_dummy_ops.to_mourn_inferior = or1k_mourn_inferior;
1685
  or1k_dummy_ops.to_stratum = process_stratum;
1686 362 markom
  or1k_dummy_ops.to_stop = or1k_stop;
1687 118 markom
 
1688
  /* We can access memory while program is running.  */
1689
  or1k_dummy_ops.to_has_all_memory = 0;
1690
 
1691 106 markom
  or1k_dummy_ops.to_has_memory = 1;
1692
  or1k_dummy_ops.to_has_stack = 1;
1693
  or1k_dummy_ops.to_has_registers = 1;
1694
  or1k_dummy_ops.to_has_execution = 1;
1695
  or1k_dummy_ops.to_magic = OPS_MAGIC;
1696
 
1697
  /* Copy the common fields to all target vectors.  */
1698
  or1k_jtag_ops = or1k_sim_ops = or1k_dummy_ops;
1699 362 markom
 
1700
  /* Initialize target-specific fields in the target vectors adn add targets.  */
1701 106 markom
  or1k_jtag_ops.to_shortname = "jtag";
1702
  or1k_jtag_ops.to_longname = "Remote or1k debugging over JTAG port";
1703 125 chris
  or1k_jtag_ops.to_doc = "Debug a board using the OR1K remote debugging protocol"
1704
    " over a parallel line.\nThe argument is the parallel port it is connected "
1705
    "to, or, if it is formatted\nas a URL of the form jtag://<hostname>:<port>,"
1706
    " then the argument refers to\na remote JTAG proxy server.\n";
1707 362 markom
  or1k_jtag_ops.to_open = or1k_jtag_open;
1708 106 markom
  add_target (&or1k_jtag_ops);
1709
 
1710
  or1k_dummy_ops.to_shortname = "dummy";
1711
  or1k_dummy_ops.to_longname = "Dummy target";
1712
  or1k_dummy_ops.to_doc = "Actually no real target attached - more like /dev/null.\n";
1713
  or1k_dummy_ops.to_open = or1k_dummy_open;
1714
  add_target (&or1k_dummy_ops);
1715
 
1716
  or1k_sim_ops.to_shortname = "sim";
1717
  or1k_sim_ops.to_longname = "Remote or1k debugging using architecture simulator";
1718
  or1k_sim_ops.to_doc = "Debug using an architecture simulator.\n";
1719
  or1k_sim_ops.to_open = or1k_sim_open;
1720
  add_target (&or1k_sim_ops);
1721 113 markom
  add_info ("matchpoints", info_matchpoints_command, "Show current matchpoints allocation status.");
1722 106 markom
}

powered by: WebSVN 2.1.0

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