summaryrefslogtreecommitdiff
path: root/cesar/ecos/packages/cygmon/current/misc/monitor.c
blob: 06c04ae93acc4d62ef7e4a80c68d58b612c8c920 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//==========================================================================
//
//      monitor.c
//
//      Monitor shell and main routines for CygMON the Wonder Monitor
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):    
// Contributors: gthomas, dmoseley
// Date:         1999-10-20
// Purpose:      Monitor shell and main routines for CygMON the Wonder Monitor
// Description:  
//               
//
//####DESCRIPTIONEND####
//
//=========================================================================

/* Platform-independent code for cygmon */

#include <stdlib.h>
#include <unistd.h>
#ifdef HAVE_BSP
#include <bsp/bsp.h>
#include <bsp/cpu.h>
#include <bsp/hex-utils.h>
#endif
#include <monitor.h>
#ifdef HAVE_BSP
#include "cpu_info.h"
#endif
#include <ledit.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>

#if USE_CYGMON_PROTOTYPES
/* Use common prototypes */
/* Some of the composed board.h files compose these
   prototypes redundently, but if they dont,
   these are the common definitions */
#include "fmt_util.h"   /* Interface to string formatting utilities */
#include "tservice.h"   /* Interface to target specific services */
#include "generic-stub.h" /* from libstub */
#endif /* USE_CYGMON_PROTOTYPES */

static int  cygmon_handle_exception (int sigval);
static void monitor_take_control (void);

#if CYGMON_SYSTEM_SERVICES
extern int process_syscall (int syscall_num);
#elif defined (USE_ECOS_HAL_EXCEPTIONS)
static int cygmon_process_syscall (int sigval);
#endif

int stub_is_active = 0;
mem_addr_t last_pc;

#ifdef HAVE_BSP

#if !defined(USE_ECOS_HAL_EXCEPTIONS)
static int mon_dbg_handler(int exc_nr, void *regs);
static int mon_kill_handler(int exc_nr, void *regs);
#endif // USE_ECOS_HAL_EXCEPTIONS

#ifndef USE_ECOS_HAL_BREAKPOINTS
#define __is_breakpoint_function() (get_pc() == (target_register_t)bsp_breakinsn)
#endif

#ifdef __ECOS__
/*
 * This global flag is used by generic-stub.c to communicate to us that we
 * are processing the breakpoint function within Cygmon itself.
 */
extern int processing_breakpoint_function;
#endif

/* Global pointer to current set of saved registers. */
void *mon_saved_regs;

/* Original BSP debug vector replaced by monitor. */
#if !defined(USE_ECOS_HAL_EXCEPTIONS)
static bsp_handler_t old_dbg_vec;
#endif // !defined(USE_ECOS_HAL_EXCEPTIONS)

#else
static int handle_signal (int signal_val);

__PFI user_signal_handler = NULL;
#endif

#if defined(__ECOS__)
#include <cyg/hal/hal_stub.h>
#endif

#if defined(__ECOS__) && !defined(PROCESS_EXCEPTION_VEC_PROTOTYPE_EXISTS)
#define PROCESS_EXCEPTION_VEC_PROTOTYPE_EXISTS
extern volatile __PFI (*__process_exception_vec)(int);
#endif


#ifdef MONITOR_CONTROL_INTERRUPTS
/* This is set if the user wants interrupts enabled in the monitor. */
static int monitor_interrupts_enabled;
#endif


#if NOMAIN
int monitor_main (int argc, char **argv) /* Suppress default main() junk */
#else
int main (int argc, char **argv)     
#endif
{
#ifdef HAVE_BSP
  int  cur_port;
  struct bsp_comm_info comm_info;
#else
  /* Set up exception handling traps */
  initialize_stub ();
#endif

  initialize_mon ();

#ifdef HAVE_BSP
  /* get info on debug channel */
  cur_port = bsp_set_debug_comm(-1);
  bsp_sysinfo(BSP_INFO_COMM, cur_port, &comm_info);

  /*
   * If we're using a network interface, don't install
   * the cygmon vectors. Currently, we only support stub
   * mode over a network connection.
   */
  if (comm_info.kind != BSP_COMM_ENET)
    {
      xprintf("\n");
      version ();

      /* replace original BSP debug and kill handler with ours */
#if !defined(USE_ECOS_HAL_EXCEPTIONS)
      old_dbg_vec = bsp_install_dbg_handler(mon_dbg_handler);
      (void)bsp_install_kill_handler(mon_kill_handler);
#else
      /* replace original BSP debug and kill handler with ours using eCos stuff */
      __process_exception_vec = (__PFI)cygmon_handle_exception;
      __process_syscall_vec = cygmon_process_syscall;
      __process_exit_vec = monitor_take_control;
#endif // __ECOS__
    }
  else
    {
      /* This forces the console to use the gdb channel. */
      bsp_set_console_comm(cur_port);
    }
#else
  xprintf("\n");
  version ();

  __process_exception_vec = cygmon_handle_exception;
  __process_exit_vec = monitor_take_control;
#if CYGMON_SYSTEM_SERVICES
  __process_syscall_vec = process_syscall;
#endif  
  __process_signal_vec = handle_signal;
  __init_vec = install_breakpoints;
  __cleanup_vec = clear_breakpoints;
#endif

#if 0
#ifdef __ECOS__
  __process_exception_vec = cygmon_handle_exception;
#endif
#endif

  while (1)
    {
      breakpoint ();
      if (switch_to_stub_flag)
        {
	  xprintf("Switching to stub\n");
	  switch_to_stub_flag = 0;
	}
    }

  /* never reached */
  exit (0);
}


/* Transfer control to gdb stub */
 
int 
transfer_to_stub ()
{
  /* Return back to the exception handler, but the exception handler
     should invoke the stub's exception handler instead of ours. */
#if defined(HAVE_BSP) && !defined(USE_ECOS_HAL_EXCEPTIONS)
  (void)bsp_install_dbg_handler(old_dbg_vec);
#else
  __switch_to_stub ();
#endif

  /* The stub is now active. */
  stub_is_active = 1;
  return -1;
}

void
clear_user_state (void)
{
#ifdef HAS_TIMER
  if (__timer_enabled ())
    __settimer (0, 0);
#endif

  clear_breakpoints ();

#ifndef HAVE_BSP
  user_signal_handler = NULL;
#endif

  __clear_single_step ();
}

static void
monitor_take_control (void)
{
  stub_is_active = 0;
  switch_to_stub_flag = 0;

  // Flush the unget state.  This is because the ecos stub and Cygmon track this
  // stuff separately.
  bsp_debug_ungetc('\0');

#ifdef INITIALIZE_MON_EACH_TIME
  // Call the per-stop initialization routine if it is defined.
  INITIALIZE_MON_EACH_TIME();
#endif

#if defined(HAVE_BSP) && !defined(USE_ECOS_HAL_EXCEPTIONS)
  /* replace original BSP debug trap handler with ours */
  (void)bsp_install_dbg_handler(mon_dbg_handler);
#else
  clear_user_state ();
  __process_exception_vec = cygmon_handle_exception;
#endif
}


#ifdef MONITOR_CONTROL_INTERRUPTS
void
monitor_enable_interrupts (void)
{
  monitor_interrupts_enabled = 1;
  enable_interrupts ();
}

void
monitor_disable_interrupts (void)
{
  monitor_interrupts_enabled = 0;
  disable_interrupts ();
}

int
monitor_interrupt_state (void)
{
  return monitor_interrupts_enabled;
}
#endif

#if defined(USE_ECOS_HAL_EXCEPTIONS)
externC HAL_SavedRegisters *_hal_registers;
extern int machine_syscall(HAL_SavedRegisters *regs);
static int
cygmon_process_syscall (int sigval)
{
    return machine_syscall(_hal_registers);
}
#endif

static int
cygmon_handle_exception (int sigval)
{
  target_register_t pc;

#ifdef MONITOR_CONTROL_INTERRUPTS
  if (monitor_interrupts_enabled)
    {
      if (! __in_interrupt)
	enable_interrupts ();
    }
#endif

#ifdef TARGET_EXCEPTION_CODE
  TARGET_EXCEPTION_CODE
#endif

#ifndef HAVE_BSP
  if (sigval != SIGKILL)
    if (handle_signal (sigval) == 0)
      return 0;
#endif

  clear_user_state ();

  /* We may want to tweak the PC to point at the faulting instruction,
     for example. (breakpoints on x86). */
#ifdef TARGET_ADJUST_PC
  TARGET_ADJUST_PC
#endif

  pc = get_pc();
  MAKE_STD_ADDR (pc, &last_pc);

#ifdef __ECOS__
  if ((sigval == SIGTRAP) && (__is_breakpoint_function() || processing_breakpoint_function))
#else
  if ((sigval == SIGTRAP) && __is_breakpoint_function())
#endif
    {
      /*
       * This is the initial breakpoint inserted by the BSP
       * Don't print anything for this as it is confusing
       */
    }
  else
    {
      if (sigval == SIGTRAP)
          xprintf ("Hit breakpoint");
      else
          xprintf ("Got signal %d", sigval);

      xprintf (" at 0x%s\n", int2str (pc, 16, sizeof (target_register_t) * 2));

#ifdef DISASSEMBLER
      if (!__is_breakpoint_function ())
	{
	  do_dis (&last_pc);
	  flush_dis ();
	}
#endif
    }

  monitor_take_control ();

  return monitor_loop ();
}

#ifndef HAVE_BSP
/* Returns 0 if the program should restart at the point at which the
   signal was received, -1 otherwise. */
static int
handle_signal (int signal)
{
  if (signal == SIGKILL)
    return -1;

  if (user_signal_handler != NULL)
    {
      int result = user_signal_handler (signal);
      switch (result)
	{
	  /* Don't ignore potential hardware signals. */
	case 3:
	  if (signal == SIGSEGV || signal == SIGBUS || signal == SIGFPE
	      || signal == SIGTRAP || signal == SIGILL)
	    return -1;

	case 0:
	  return 0;

	default:
	case 1:
	case 2:
	  return -1;
	}
    }
    return -1;
}
#endif


void
version (void)
{
#ifdef HAVE_BSP
  struct bsp_platform_info platform;
  struct bsp_mem_info      mem;
  int                      i;
  unsigned long            u, totmem, topmem;
#endif
  extern char *build_date;

  xprintf ("Cygmon, the Cygnus ROM monitor.\n");
  xprintf ("Copyright(c) 1997, 1998, 1999, 2000 Red Hat\n\n");
  xprintf ("Version: %s\nThis image was built on %s\n\n",
	   VERSION, build_date);

#ifdef HAVE_BSP
  bsp_sysinfo(BSP_INFO_PLATFORM, &platform);

  totmem = topmem = 0;
  i = 0;
  while (bsp_sysinfo(BSP_INFO_MEMORY, i++, &mem) == 0)
    {
      if (mem.kind == BSP_MEM_RAM)
        {
          totmem += mem.nbytes;
          u = (unsigned long)mem.virt_start + mem.nbytes;
	  if (u > topmem)
	    topmem = u;
        }
    }

  xprintf("CPU: %s\n", platform.cpu);
  xprintf("Board: %s\n", platform.board);
  if (*(platform.extra))
      xprintf("%s\n", platform.extra);
  xprintf("Total RAM: %d bytes\n", totmem);
  xprintf("Top of RAM: 0x%x\n", topmem);
#endif
}


#ifdef HAVE_BSP
#if !defined(USE_ECOS_HAL_EXCEPTIONS)
static int
mon_kill_handler(int exc_nr, void *regs)
{
    monitor_take_control();
    return 1;
}
#endif // !defined(USE_ECOS_HAL_EXCEPTIONS)

#if !defined(USE_ECOS_HAL_EXCEPTIONS)
static int
mon_dbg_handler(int exc_nr, void *regs)
{
    int sig;
    unsigned long cur_pc;

    mon_saved_regs = regs;
    sig = bsp_get_signal(exc_nr, regs);

    cygmon_handle_exception(sig);

    cur_pc = bsp_get_pc(regs);
    if (cur_pc == (unsigned long)bsp_breakinsn)
	bsp_skip_instruction(regs);

    if (!stub_is_active)
	install_breakpoints();

    return 1;
}
#endif // !defined(USE_ECOS_HAL_EXCEPTIONS)

#endif