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 362

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

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

powered by: WebSVN 2.1.0

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