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 207

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

powered by: WebSVN 2.1.0

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