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

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [or1ksim/] [or1ksim-0.4.0/] [debug/] [gdbcomm.c] - Blame information for rev 403

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

Line No. Rev Author Line
1 19 jeremybenn
/* gdbcomm.c -- Communication routines for gdb
2
 
3
   Copyright (C) 2001 by Marko Mlinar, markom@opencores.org
4
   Copyright (C) 2008 Embecosm Limited
5
 
6
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
7
 
8
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
9
 
10
   This program is free software; you can redistribute it and/or modify it
11
   under the terms of the GNU General Public License as published by the Free
12
   Software Foundation; either version 3 of the License, or (at your option)
13
   any later version.
14
 
15
   This program is distributed in the hope that it will be useful, but WITHOUT
16
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18
   more details.
19
 
20
   You should have received a copy of the GNU General Public License along
21
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
 
23
/* This program is commented throughout in a fashion suitable for processing
24
   with Doxygen. */
25
 
26
 
27
/* Autoconf and/or portability configuration */
28
#include "config.h"
29
#include "port.h"
30
 
31
/* System includes */
32
#include <stdlib.h>
33
#include <unistd.h>
34
#include <stdio.h>
35
#include <sys/poll.h>
36
#include <sys/types.h>
37
#include <sys/socket.h>
38
#include <netinet/in.h>
39
#include <fcntl.h>
40
#include <netdb.h>
41
#include <netinet/tcp.h>
42
#include <errno.h>
43
 
44
/* Package includes */
45
#include "sim-config.h"
46
#include "gdb.h"
47
#include "gdbcomm.h"
48
#include "debug-unit.h"
49
#include "vapi.h"
50
 
51
 
52
/* Forward declaration of static functions */
53
static void  jtag_request ();
54
static void  gdb_request (void);
55
static void  protocol_clean (int, int32_t);
56
static int   get_server_socket (const char *name, const char *proto, int port);
57
static int   gdb_read (void *buf, int len);
58
static int   gdb_write (const void *buf, int len);
59
 
60
/* Data structures for this file */
61
static unsigned int  server_ip   = 0;
62
static unsigned int  server_port = 0;
63
static unsigned int  server_fd   = 0;
64
static unsigned int  gdb_fd      = 0;
65
static int           tcp_level   = 0;
66
 
67
/* Added by CZ 24/05/01 */
68
static int
69
get_server_socket (const char *name, const char *proto, int port)
70
{
71
  struct servent *service;
72
  struct protoent *protocol;
73
  struct sockaddr_in sa;
74
  struct hostent *hp;
75
  int sockfd;
76
  char myname[256];
77
  int flags;
78
  char sTemp[256];
79
  socklen_t len;
80
 
81
  /* First, get the protocol number of TCP */
82
  if (!(protocol = getprotobyname (proto)))
83
    {
84
      sprintf (sTemp, "Unable to load protocol \"%s\"", proto);
85
      perror (sTemp);
86
      return 0;
87
    }
88
  tcp_level = protocol->p_proto;        /* Save for later */
89
 
90
  /* If we weren't passed a non standard port, get the port
91
     from the services directory. */
92
  if (!port)
93
    {
94
      if ((service = getservbyname (name, protocol->p_name)))
95
        port = ntohs (service->s_port);
96
    }
97
 
98
  /* Create the socket using the TCP protocol */
99
  if ((sockfd = socket (PF_INET, SOCK_STREAM, protocol->p_proto)) < 0)
100
    {
101
      perror ("Unable to create socket");
102
      return 0;
103
    }
104
 
105
  flags = 1;
106
  if (setsockopt
107
      (sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &flags,
108
       sizeof (int)) < 0)
109
    {
110
      sprintf (sTemp, "Can not set SO_REUSEADDR option on socket %d", sockfd);
111
      perror (sTemp);
112
      close (sockfd);
113
      return 0;
114
    }
115
 
116
  /* The server should also be non blocking. Get the current flags. */
117
  flags = fcntl (sockfd, F_GETFL, flags);
118
  if (flags < 0)
119
    {
120
      sprintf (sTemp, "Unable to get flags for socket %d", sockfd);
121
      perror (sTemp);
122
      close (sockfd);
123
      return 0;
124
    }
125
 
126
  /* Set the nonblocking flag */
127
  if (fcntl (sockfd, F_SETFL, flags | O_NONBLOCK) < 0)
128
    {
129
      sprintf (sTemp, "Unable to set flags for socket %d to value 0x%08x",
130
               sockfd, flags | O_NONBLOCK);
131
      perror (sTemp);
132
      close (sockfd);
133
      return 0;
134
    }
135
 
136
  /* Find out what our address is */
137
  memset (&sa, 0, sizeof (struct sockaddr_in));
138
  gethostname (myname, sizeof (myname));
139
  if (!(hp = gethostbyname (myname)))
140
    {
141
      perror ("Unable to read hostname");
142
      close (sockfd);
143
      return 0;
144
    }
145
 
146
  /* Bind our socket to the appropriate address */
147
  sa.sin_family = hp->h_addrtype;
148
  sa.sin_port = htons (port);
149
  if (bind (sockfd, (struct sockaddr *) &sa, sizeof (struct sockaddr_in)) < 0)
150
    {
151
      sprintf (sTemp, "Unable to bind socket %d to port %d", sockfd, port);
152
      perror (sTemp);
153
      close (sockfd);
154
      return 0;
155
    }
156
  server_ip = sa.sin_addr.s_addr;
157
  len = sizeof (struct sockaddr_in);
158
  if (getsockname (sockfd, (struct sockaddr *) &sa, &len) < 0)
159
    {
160
      sprintf (sTemp, "Unable to get socket information for socket %d",
161
               sockfd);
162
      perror (sTemp);
163
      close (sockfd);
164
      return 0;
165
    }
166
  server_port = ntohs (sa.sin_port);
167
 
168
  /* Set the backlog to 1 connections */
169
  if (listen (sockfd, 1) < 0)
170
    {
171
      sprintf (sTemp, "Unable to set backlog on socket %d to %d", sockfd, 1);
172
      perror (sTemp);
173
      close (sockfd);
174
      return 0;
175
    }
176
 
177
  return sockfd;
178
}
179
 
180
void
181
block_jtag ()
182
{
183
  struct pollfd fds[2];
184
  int n = 0;
185
 
186
  fds[n].fd = server_fd;
187
  fds[n].events = POLLIN;
188
  fds[n++].revents = 0;
189
  if (gdb_fd)
190
    {
191
      fds[n].fd = gdb_fd;
192
      fds[n].events = POLLIN;
193
      fds[n++].revents = 0;
194
    }
195
  poll (fds, n, -1);
196
}
197
 
198
void
199
handle_server_socket (enum boolean block)
200
{
201
  struct pollfd fds[3];
202
  int n = 0;
203
  int timeout = block ? -1 : 0;
204
  enum boolean data_on_stdin = FALSE;
205
  int o_serv_fd = server_fd;
206
 
207
  if (!o_serv_fd && !gdb_fd)
208
    return;
209
 
210
  if (o_serv_fd)
211
    {
212
      fds[n].fd = o_serv_fd;
213
      fds[n].events = POLLIN;
214
      fds[n++].revents = 0;
215
    }
216
  if (gdb_fd)
217
    {
218
      fds[n].fd = gdb_fd;
219
      fds[n].events = POLLIN;
220
      fds[n++].revents = 0;
221
    }
222
  if (block)
223
    {
224
      fds[n].fd = 0;
225
      fds[n].events = POLLIN;
226
      fds[n++].revents = 0;
227
    }
228
 
229
  while (!data_on_stdin)
230
    {
231
      fds[0].revents = 0;
232
      fds[1].revents = 0;
233
      fds[2].revents = 0;
234
      switch (poll (fds, n, timeout))
235
        {
236
        case -1:
237
          if (errno == EINTR)
238
            continue;
239
          perror ("poll");
240
          server_fd = 0;
241
          break;
242
        case 0:          /* Nothing interesting going on */
243
          data_on_stdin = TRUE; /* Can only get here if nonblocking */
244
          break;
245
        default:
246
          /* Make sure to handle the gdb port first! */
247
          if ((fds[0].revents && gdb_fd && !o_serv_fd) ||
248
              (fds[1].revents && server_fd && gdb_fd))
249
            {
250
              int revents = o_serv_fd ? fds[1].revents : fds[0].revents;
251
 
252
              if (revents & POLLIN)
253
                gdb_request ();
254
              else              /* Error Occurred */
255
                {
256
                  fprintf (stderr,
257
                           "Received flags 0x%08x on gdb socket. Shutting down.\n",
258
                           revents);
259
                  close (gdb_fd);
260
                  gdb_fd = 0;
261
                }
262
            }
263
          if (fds[0].revents && o_serv_fd)
264
            {
265
              if (fds[0].revents & POLLIN)
266
                jtag_request ();
267
              else              /* Error Occurred */
268
                {
269
                  fprintf (stderr,
270
                           "Received flags 0x%08x on server. Shutting down.\n",
271
                           fds[0].revents);
272
                  close (o_serv_fd);
273
                  server_fd = 0;
274
                  server_port = 0;
275
                  server_ip = 0;
276
                }
277
            }
278
          if (fds[2].revents || (fds[1].revents && !gdb_fd))
279
            data_on_stdin = TRUE;
280
          break;
281
        }                       /* End of switch statement */
282
    }                           /* End of while statement */
283
}
284
 
285
static void
286
jtag_request (void)
287
{
288
  struct sockaddr_in sa;
289
  struct sockaddr *addr = (struct sockaddr *) &sa;
290
  socklen_t len = sizeof (struct sockaddr_in);
291
  int fd = accept (server_fd, addr, &len);
292
  int on_off = 0;                /* Turn off Nagel's algorithm on the socket */
293
  int flags;
294
  char sTemp[256];
295
 
296
  if (fd < 0)
297
    {
298
      /* This is valid, because a connection could have started,
299
         and then terminated due to a protocol error or user
300
         initiation before the accept could take place. */
301
      if (errno != EWOULDBLOCK && errno != EAGAIN)
302
        {
303
          perror ("accept");
304
          close (server_fd);
305
          server_fd = 0;
306
          server_port = 0;
307
          server_ip = 0;
308
        }
309
      return;
310
    }
311
 
312
  if (gdb_fd)
313
    {
314
      close (fd);
315
      return;
316
    }
317
 
318
  if ((flags = fcntl (fd, F_GETFL)) < 0) /* JPB */
319
    {
320
      sprintf (sTemp, "Unable to get flags for gdb socket %d", fd);
321
      perror (sTemp);
322
      close (fd);
323
      return;
324
    }
325
 
326
  if (fcntl (fd, F_SETFL, flags | O_NONBLOCK) < 0)
327
    {
328
      sprintf (sTemp, "Unable to set flags for gdb socket %d to value 0x%08x",
329
               fd, flags | O_NONBLOCK);
330
      perror (sTemp);
331
      close (fd);
332
      return;
333
    }
334
 
335
  if (setsockopt (fd, tcp_level, TCP_NODELAY, &on_off, sizeof (int)) < 0)
336
    {
337
      sprintf (sTemp,
338
               "Unable to disable Nagel's algorithm for socket %d.\nsetsockopt",
339
               fd);
340
      perror (sTemp);
341
      close (fd);
342
      return;
343
    }
344
 
345
  gdb_fd = fd;
346
}
347
 
348
static void
349
gdb_request (void)
350
{
351
  struct jtr_read_message          msg_read;
352
  struct jtr_write_message         msg_write;
353
  struct jtr_read_block_message    msg_bread;
354
  struct jtr_write_block_message  *msg_bwrite;
355
  struct jtr_chain_message         msg_chain;
356
  struct jtr_read_response         resp_read;
357
  struct jtr_write_response        resp_write;
358
  struct jtr_read_block_response  *resp_bread;
359
  struct jtr_write_block_response  resp_bwrite;
360
  struct jtr_chain_response        resp_chain;
361
 
362
  char *buf;
363
  int err = 0;
364
  uint32_t command, length;
365
  int len, i;
366
 
367
  /* First, we must read the incomming command */
368
  if (gdb_read (&command, sizeof (uint32_t)) < 0)
369
    {
370
      if (gdb_fd)
371
        {
372
          perror ("gdb socket - 1");
373
          close (gdb_fd);
374
          gdb_fd = 0;
375
        }
376
      return;
377
    }
378
  if (gdb_read (&length, sizeof (uint32_t)) < 0)
379
    {
380
      if (gdb_fd)
381
        {
382
          perror ("gdb socket - 2");
383
          close (gdb_fd);
384
          gdb_fd = 0;
385
        }
386
      return;
387
    }
388
  length = ntohl (length);
389
 
390
  /* Now, verify the protocol and implement the command */
391
  switch (ntohl (command))
392
    {
393
    case OR1K_JTAG_COMMAND_WRITE:
394
      if (length != sizeof (msg_write) - 8)
395
        {
396
          protocol_clean (length, JTAG_PROXY_PROTOCOL_ERROR);
397
          return;
398
        }
399
      buf = (char *) &msg_write;
400
      if (gdb_read (&buf[8], length) < 0)
401
        {
402
          if (gdb_fd)
403
            {
404
              perror ("gdb socket - 3");
405
              close (gdb_fd);
406
              gdb_fd = 0;
407
            }
408
          return;
409
        }
410
      msg_write.address = ntohl (msg_write.address);
411
      msg_write.data_h = ntohl (msg_write.data_h);
412
      msg_write.data_l = ntohl (msg_write.data_l);
413
      err = debug_set_register (msg_write.address, msg_write.data_l);
414
      resp_write.status = htonl (err);
415
      if (gdb_write (&resp_write, sizeof (resp_write)) < 0)
416
        {
417
          if (gdb_fd)
418
            {
419
              perror ("gdb socket - 4");
420
              close (gdb_fd);
421
              gdb_fd = 0;
422
            }
423
          return;
424
        }
425
      break;
426
    case OR1K_JTAG_COMMAND_READ:
427
      if (length != sizeof (msg_read) - 8)
428
        {
429
          protocol_clean (length, JTAG_PROXY_PROTOCOL_ERROR);
430
          return;
431
        }
432
      buf = (char *) &msg_read;
433
      if (gdb_read (&buf[8], length) < 0)
434
        {
435
          if (gdb_fd)
436
            {
437
              perror ("gdb socket - 5");
438
              close (gdb_fd);
439
              gdb_fd = 0;
440
            }
441
          return;
442
        }
443
      msg_read.address = ntohl (msg_read.address);
444
      err = debug_get_register (msg_read.address, &resp_read.data_l);
445
      resp_read.status = htonl (err);
446
      resp_read.data_h = 0;
447
      resp_read.data_l = htonl (resp_read.data_l);
448
      if (gdb_write (&resp_read, sizeof (resp_read)) < 0)
449
        {
450
          if (gdb_fd)
451
            {
452
              perror ("gdb socket - 6");
453
              close (gdb_fd);
454
              gdb_fd = 0;
455
            }
456
          return;
457
        }
458
      break;
459
    case OR1K_JTAG_COMMAND_WRITE_BLOCK:
460
      if (length < sizeof (struct jtr_write_block_message) - 8)
461
        {
462
          protocol_clean (length, JTAG_PROXY_PROTOCOL_ERROR);
463
          return;
464
        }
465
      if (!(buf = (char *) malloc (8 + length)))
466
        {
467
          protocol_clean (length, JTAG_PROXY_OUT_OF_MEMORY);
468
          return;
469
        }
470
      msg_bwrite = (struct jtr_write_block_message *) buf;
471
      if (gdb_read (&buf[8], length) < 0)
472
        {
473
          if (gdb_fd)
474
            {
475
              perror ("gdb socket - 5");
476
              close (gdb_fd);
477
              gdb_fd = 0;
478
            }
479
          free (buf);
480
          return;
481
        }
482
      msg_bwrite->address = ntohl (msg_bwrite->address);
483
      msg_bwrite->num_regs = ntohl (msg_bwrite->num_regs);
484
      for (i = 0; i < msg_bwrite->num_regs; i++)
485
        {
486
          int t_err = 0;
487
 
488
          msg_bwrite->data[i] = ntohl (msg_bwrite->data[i]);
489
          t_err =
490
            debug_set_register (msg_bwrite->address + 4 * i,
491
                                msg_bwrite->data[i]);
492
          err = err ? err : t_err;
493
        }
494
      resp_bwrite.status = htonl (err);
495
      free (buf);
496
      buf = NULL;
497
      msg_bwrite = NULL;
498
      if (gdb_write (&resp_bwrite, sizeof (resp_bwrite)) < 0)
499
        {
500
          if (gdb_fd)
501
            {
502
              perror ("gdb socket - 4");
503
              close (gdb_fd);
504
              gdb_fd = 0;
505
            }
506
          return;
507
        }
508
      break;
509
    case OR1K_JTAG_COMMAND_READ_BLOCK:
510
      if (length != sizeof (msg_bread) - 8)
511
        {
512
          protocol_clean (length, JTAG_PROXY_PROTOCOL_ERROR);
513
          return;
514
        }
515
      buf = (char *) &msg_bread;
516
      if (gdb_read (&buf[8], length) < 0)
517
        {
518
          if (gdb_fd)
519
            {
520
              perror ("gdb socket - 5");
521
              close (gdb_fd);
522
              gdb_fd = 0;
523
            }
524
          return;
525
        }
526
      msg_bread.address = ntohl (msg_bread.address);
527
      msg_bread.num_regs = ntohl (msg_bread.num_regs);
528
      len =
529
        sizeof (struct jtr_read_block_response) + 4 * (msg_bread.num_regs - 1);
530
      if (!(buf = (char *) malloc (len)))
531
        {
532
          protocol_clean (0, JTAG_PROXY_OUT_OF_MEMORY);
533
          return;
534
        }
535
      resp_bread = (struct jtr_read_block_response *) buf;
536
      for (i = 0; i < msg_bread.num_regs; i++)
537
        {
538
          int t_err;
539
 
540
          t_err =
541
            debug_get_register (msg_bread.address + 4 * i,
542
                                &resp_bread->data[i]);
543
          resp_bread->data[i] = htonl (resp_bread->data[i]);
544
          err = err ? err : t_err;
545
        }
546
      resp_bread->status = htonl (err);
547
      resp_bread->num_regs = htonl (msg_bread.num_regs);
548
      if (gdb_write (resp_bread, len) < 0)
549
        {
550
          if (gdb_fd)
551
            {
552
              perror ("gdb socket - 6");
553
              close (gdb_fd);
554
              gdb_fd = 0;
555
            }
556
          free (buf);
557
          return;
558
        }
559
      free (buf);
560
      buf = NULL;
561
      resp_bread = NULL;
562
      break;
563
    case OR1K_JTAG_COMMAND_CHAIN:
564
      if (length != sizeof (msg_chain) - 8)
565
        {
566
          protocol_clean (length, JTAG_PROXY_PROTOCOL_ERROR);
567
          return;
568
        }
569
      buf = (char *) &msg_chain;
570
      if (gdb_read (&buf[8], sizeof (msg_chain) - 8) < 0)
571
        {
572
          if (gdb_fd)
573
            {
574
              perror ("gdb socket - 7");
575
              close (gdb_fd);
576
              gdb_fd = 0;
577
            }
578
          return;
579
        }
580
      msg_chain.chain = htonl (msg_chain.chain);
581
      err = debug_set_chain (msg_chain.chain);
582
      resp_chain.status = htonl (err);
583
      if (gdb_write (&resp_chain, sizeof (resp_chain)) < 0)
584
        {
585
          if (gdb_fd)
586
            {
587
              perror ("gdb socket - 8");
588
              close (gdb_fd);
589
              gdb_fd = 0;
590
            }
591
          return;
592
        }
593
      break;
594
    default:
595
      protocol_clean (length, JTAG_PROXY_COMMAND_NOT_IMPLEMENTED);
596
      break;
597
    }
598
}
599
 
600
static void
601
protocol_clean (int length, int32_t err)
602
{
603
  char buf[4096];
604
 
605
  err = htonl (err);
606
  if ((gdb_read (buf, length) < 0) ||
607
      ((gdb_write (&err, sizeof (err)) < 0) && gdb_fd))
608
    {
609
      perror ("gdb socket - 9");
610
      close (gdb_fd);
611
      gdb_fd = 0;
612
    }
613
}
614
 
615
static int
616
gdb_write (const void *buf, int len)
617
{
618
  int n, log_n = 0;
619
  const char *w_buf = (const char *) buf;
620
  const uint32_t *log_buf = (const uint32_t *) buf;
621
  struct pollfd block;
622
 
623
  while (len)
624
    {
625
      if ((n = write (gdb_fd, w_buf, len)) < 0)
626
        {
627
          switch (errno)
628
            {
629
            case EWOULDBLOCK:   /* or EAGAIN */
630
              /* We've been called on a descriptor marked
631
                 for nonblocking I/O. We better simulate
632
                 blocking behavior. */
633
              block.fd = gdb_fd;
634
              block.events = POLLOUT;
635
              block.revents = 0;
636
              poll (&block, 1, -1);
637
              continue;
638
            case EINTR:
639
              continue;
640
            case EPIPE:
641
              close (gdb_fd);
642
              gdb_fd = 0;
643
              return -1;
644
            default:
645
              return -1;
646
            }
647
        }
648
      else
649
        {
650
          len -= n;
651
          w_buf += n;
652
          if (config.debug.vapi_id)
653
            for (log_n += n; log_n >= 4; log_n -= 4, ++log_buf)
654
              vapi_write_log_file (VAPI_COMMAND_SEND, config.debug.vapi_id,
655
                                   ntohl (*log_buf));
656
        }
657
    }
658
  return 0;
659
}
660
 
661
static int
662
gdb_read (void *buf, int len)
663
{
664
  int n, log_n = 0;
665
  char *r_buf = (char *) buf;
666
  uint32_t *log_buf = (uint32_t *) buf;
667
  struct pollfd block;
668
 
669
  while (len)
670
    {
671
      if ((n = read (gdb_fd, r_buf, len)) < 0)
672
        {
673
          switch (errno)
674
            {
675
            case EWOULDBLOCK:   /* or EAGAIN */
676
              /* We've been called on a descriptor marked
677
                 for nonblocking I/O. We better simulate
678
                 blocking behavior. */
679
              block.fd = gdb_fd;
680
              block.events = POLLIN;
681
              block.revents = 0;
682
              poll (&block, 1, -1);
683
              continue;
684
            case EINTR:
685
              continue;
686
            default:
687
              return -1;
688
            }
689
        }
690
      else if (n == 0)
691
        {
692
          close (gdb_fd);
693
          gdb_fd = 0;
694
          return -1;
695
        }
696
      else
697
        {
698
          len -= n;
699
          r_buf += n;
700
          if (config.debug.vapi_id)
701
            for (log_n += n; log_n >= 4; log_n -= 4, ++log_buf)
702
              vapi_write_log_file (VAPI_COMMAND_REQUEST, config.debug.vapi_id,
703
                                   ntohl (*log_buf));
704
        }
705
    }
706
  return 0;
707
}
708
 
709
void
710
gdbcomm_init ()
711
{
712
  server_port = config.debug.server_port;
713
  if ((server_fd = get_server_socket ("or1ksim", "tcp", server_port)))
714
    PRINTF ("JTAG Proxy server started on port %d\n", server_port);
715
  else
716
    PRINTF ("Cannot start JTAG proxy server on port %d\n", server_port);
717
}

powered by: WebSVN 2.1.0

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