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 372

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

powered by: WebSVN 2.1.0

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