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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [jtag/] [gdb2.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1259 markom
#include <stdio.h>
2
#include <ctype.h>
3
#include <string.h>
4
#include <stdlib.h>
5
#include <unistd.h>
6
#include <stdarg.h>
7
 
8
/* Libraries for JTAG proxy server.  */
9
#include <sys/stat.h>
10
#include <sys/types.h>
11
#include <sys/socket.h>
12
#include <netinet/in.h>
13
#include <sys/select.h>
14
#include <sys/poll.h>
15
#include <fcntl.h>
16
#include <netdb.h>
17
#include <netinet/tcp.h>
18
#include <inttypes.h>
19
#include <errno.h>
20
 
21
#include "gdb.h" /* partially copied from gdb/config/or1k */
22 1380 phoenix
#include "jp.h"  /* just for debug() */
23 1259 markom
#include "jp2.h"
24
 
25
 
26
/* connection to jp2 routines */
27
int gdb_chain = -1;
28
 
29
int gdb_read_reg(unsigned long adr, unsigned long *data) {
30 1380 phoenix
  debug("rr %d\n", gdb_chain);
31 1259 markom
  switch (gdb_chain) {
32 1274 markom
    case SC_RISC_DEBUG: return dbg_cpu0_read(adr, data) ? ERR_CRC : ERR_NONE;
33
    case SC_REGISTER:   return dbg_cpu0_read_ctrl(adr, (unsigned char*)data) ? ERR_CRC : ERR_NONE;
34 1259 markom
    case SC_WISHBONE:   return dbg_wb_read32(adr, data) ? ERR_CRC : ERR_NONE;
35 1274 markom
    case SC_TRACE:      *data = 0; return 0;
36 1259 markom
    default:            return JTAG_PROXY_INVALID_CHAIN;
37
  }
38
}
39
 
40
int gdb_write_reg(unsigned long adr, unsigned long data) {
41 1380 phoenix
  debug("wr %d\n", gdb_chain);
42 1259 markom
  switch (gdb_chain) { /* remap registers, to be compatible with jp1 */
43
    case SC_RISC_DEBUG: if (adr == JTAG_RISCOP) adr = 0x00;
44 1274 markom
                        return dbg_cpu0_write(adr, data) ? ERR_CRC : ERR_NONE;
45
    case SC_REGISTER:   return dbg_cpu0_write_ctrl(adr, data) ? ERR_CRC : ERR_NONE;
46 1259 markom
    case SC_WISHBONE:   return dbg_wb_write32(adr, data) ? ERR_CRC : ERR_NONE;
47 1274 markom
    case SC_TRACE:      return 0;
48 1259 markom
    default:            return JTAG_PROXY_INVALID_CHAIN;
49
  }
50
}
51
 
52
int gdb_read_block(unsigned long adr, unsigned long *data, int len) {
53 1380 phoenix
  debug("rb %d\n", gdb_chain);
54 1259 markom
  switch (gdb_chain) {
55
    case SC_WISHBONE:   return dbg_wb_read_block32(adr, data, len) ? ERR_CRC : ERR_NONE;
56
    default:            return JTAG_PROXY_INVALID_CHAIN;
57
  }
58
}
59
 
60
int gdb_write_block(unsigned long adr, unsigned long *data, int len) {
61 1380 phoenix
  debug("wb %d\n", gdb_chain);
62 1259 markom
  switch (gdb_chain) {
63
    case SC_WISHBONE:   return dbg_wb_write_block32(adr, data, len) ? ERR_CRC : ERR_NONE;
64
    default:            return JTAG_PROXY_INVALID_CHAIN;
65
  }
66
}
67
 
68
int gdb_set_chain(int chain) {
69 1380 phoenix
  debug("set_chain %d\n", chain);
70 1259 markom
  switch (chain) {
71
    case SC_RISC_DEBUG:
72
    case SC_REGISTER:
73 1274 markom
    case SC_TRACE:
74 1259 markom
    case SC_WISHBONE:   gdb_chain = chain;
75
                        return ERR_NONE;
76
    default:            return JTAG_PROXY_INVALID_CHAIN;
77
  }
78
}
79
 
80
 
81
 
82
/************************
83
   JTAG Server Routines
84
 ************************/
85
 
86
unsigned int serverIP = 0;
87
unsigned int serverPort = 0;
88
unsigned int server_fd = 0;
89
unsigned int gdb_fd = 0;
90
static int gdb_read(void*, int);
91
static int gdb_write(void*, int);
92
static void ProtocolClean(int, int32_t);
93
 
94
static int tcp_level = 0;
95
 
96
/* Added by CZ 24/05/01 */
97
int GetServerSocket(const char* name, const char* proto, int port) {
98
  struct servent *service;
99
  struct protoent *protocol;
100
  struct sockaddr_in sa;
101
  struct hostent *hp;
102
  int sockfd;
103
  char myname[256];
104
  int flags;
105
  char sTemp[256];
106
 
107
  /* First, get the protocol number of TCP */
108
  if (!(protocol = getprotobyname(proto))) {
109
    sprintf(sTemp, "Unable to load protocol \"%s\"", proto);
110
    perror(sTemp);
111
    return 0;
112
  }
113
  tcp_level = protocol->p_proto; /* Save for later */
114
 
115
  /* If we weren't passed a non standard port, get the port
116
     from the services directory. */
117
  if (!port && (service = getservbyname(name, protocol->p_name)))
118
    port = ntohs(service->s_port);
119
 
120
  /* Create the socket using the TCP protocol */
121
  if ((sockfd = socket(PF_INET, SOCK_STREAM, protocol->p_proto)) < 0) {
122
    perror("Unable to create socket");
123
    return 0;
124
  }
125
 
126
  flags = 1;
127
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&flags, sizeof(int)) < 0) {
128
    sprintf(sTemp, "Can not set SO_REUSEADDR option on socket %d", sockfd);
129
    perror(sTemp);
130
    close(sockfd);
131
    return 0;
132
  }
133
 
134
  /* The server should also be non blocking. Get the current flags. */
135 1322 phoenix
  if((flags = fcntl(sockfd, F_GETFL, 0)) < 0) {
136 1259 markom
    sprintf(sTemp, "Unable to get flags for socket %d", sockfd);
137
    perror(sTemp);
138
    close(sockfd);
139
    return 0;
140
  }
141
 
142
  /* Set the nonblocking flag */
143
  if(fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
144
    sprintf(sTemp, "Unable to set flags for socket %d to value 0x%08x",
145
                    sockfd, flags | O_NONBLOCK);
146
    perror(sTemp);
147
    close(sockfd);
148
    return 0;
149
  }
150
 
151
  /* Find out what our address is */
152
  memset(&sa, 0, sizeof(struct sockaddr_in));
153
  gethostname(myname, sizeof(myname));
154
  if(!(hp = gethostbyname(myname))) {
155
    perror("Unable to read hostname");
156
    close(sockfd);
157
    return 0;
158
  }
159
 
160
  /* Bind our socket to the appropriate address */
161
  sa.sin_family = hp->h_addrtype;
162
  sa.sin_port = htons(port);
163
  if(bind(sockfd, (struct sockaddr*)&sa, sizeof(struct sockaddr_in)) < 0) {
164
    sprintf(sTemp, "Unable to bind socket %d to port %d", sockfd, port);
165
    perror(sTemp);
166
    close(sockfd);
167
    return 0;
168
  }
169
  serverIP = sa.sin_addr.s_addr;
170
  flags = sizeof(struct sockaddr_in);
171
  if(getsockname(sockfd, (struct sockaddr*)&sa, &flags) < 0) {
172
    sprintf(sTemp, "Unable to get socket information for socket %d", sockfd);
173
    perror(sTemp);
174
    close(sockfd);
175
    return 0;
176
  }
177
  serverPort = ntohs(sa.sin_port);
178
 
179
  /* Set the backlog to 1 connections */
180
  if(listen(sockfd, 1) < 0) {
181
    sprintf(sTemp, "Unable to set backlog on socket %d to %d", sockfd, 1);
182
    perror(sTemp);
183
    close(sockfd);
184
    return 0;
185
  }
186
 
187
  return sockfd;
188
}
189
 
190
void HandleServerSocket(Boolean block) {
191
  struct pollfd fds[2];
192
  int n;
193
 
194
rebuild:
195
  n = 0;
196
  if(!server_fd && !gdb_fd) return;
197
 
198
  if(server_fd) {
199
    fds[n].fd = server_fd;
200
    fds[n].events = POLLIN;
201
    fds[n++].revents = 0;
202
  }
203
  if(gdb_fd) {
204
    fds[n].fd = gdb_fd;
205
    fds[n].events = POLLIN;
206
    fds[n++].revents = 0;
207
  }
208
 
209
  while(1) {
210
  switch(poll(fds, n, -1)) {
211
    case 0:
212
    case -1:
213
      if(errno == EINTR) continue;
214
      perror("poll");
215
      server_fd = 0;
216
      return;
217
    default:
218
      /* Make sure to handle the gdb port first! */
219
      if (gdb_fd && (fds[0].revents && !server_fd || fds[1].revents && server_fd)) {
220
        int revents = server_fd ? fds[1].revents : fds[0].revents;
221
        if (revents & POLLIN) GDBRequest();
222
        else {/* Error Occurred */
223
          fprintf(stderr, "Received flags 0x%08x on gdb socket. Shutting down.\n", revents);
224
          close(gdb_fd);
225
          gdb_fd = 0;
226
        }
227
      }
228
      if(fds[0].revents && server_fd) {
229
        if(fds[0].revents & POLLIN) {
230
          JTAGRequest();
231
          goto rebuild;
232
        } else { /* Error Occurred */
233
            fprintf(stderr, "Received flags 0x%08x on server. Shutting down.\n", fds[0].revents);
234
            close(server_fd);
235
            server_fd = 0;
236
            serverPort = 0;
237
            serverIP = 0;
238
            return;
239
          }
240
        }
241
      break;
242
    } /* End of switch statement */
243
  } /* End of while statement */
244
}
245
 
246
void JTAGRequest() {
247
  struct sockaddr_in sa;
248
  struct sockaddr* addr = (struct sockaddr*)&sa;
249
  int n = sizeof(struct sockaddr_in);
250
  int fd = accept(server_fd, addr, &n);
251
  int on_off = 0; /* Turn off Nagel's algorithm on the socket */
252
  int flags;
253
  char sTemp[256];
254
 
255
  if(fd < 0) {
256
    /* This is valid, because a connection could have started,
257
       and then terminated due to a protocol error or user
258
       initiation before the accept could take place. */
259
    if(errno != EWOULDBLOCK && errno != EAGAIN) {
260
      perror("accept");
261
      close(server_fd);
262
      server_fd = 0;
263
      serverPort = 0;
264
      serverIP = 0;
265
    }
266
    return;
267
  }
268
 
269
  if(gdb_fd) {
270
    close(fd);
271
    return;
272
  }
273
 
274 1322 phoenix
  if((flags = fcntl(fd, F_GETFL, 0)) < 0) {
275 1259 markom
    sprintf(sTemp, "Unable to get flags for gdb socket %d", fd);
276
    perror(sTemp);
277
    close(fd);
278
    return;
279
  }
280
 
281
  if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
282
    sprintf(sTemp, "Unable to set flags for gdb socket %d to value 0x%08x",
283
      fd, flags | O_NONBLOCK);
284
    perror(sTemp);
285
    close(fd);
286
    return;
287
  }
288
 
289
  if(setsockopt(fd, tcp_level, TCP_NODELAY, &on_off, sizeof(int)) < 0) {
290
    sprintf(sTemp, "Unable to disable Nagel's algorithm for socket %d.\nsetsockopt", fd);
291
    perror(sTemp);
292
    close(fd);
293
    return;
294
  }
295
 
296
  gdb_fd = fd;
297
}
298
 
299
void GDBRequest() {
300
  JTAGProxyWriteMessage msg_write;
301
  JTAGProxyReadMessage msg_read;
302
  JTAGProxyChainMessage msg_chain;
303
  JTAGProxyWriteResponse resp_write;
304
  JTAGProxyReadResponse resp_read;
305
  JTAGProxyChainResponse resp_chain;
306
  JTAGProxyBlockWriteMessage *msg_bwrite;
307
  JTAGProxyBlockReadMessage msg_bread;
308
  JTAGProxyBlockWriteResponse resp_bwrite;
309
  JTAGProxyBlockReadResponse *resp_bread;
310
  char *buf;
311
  unsigned long long data;
312
  uint32_t command, length;
313
  int len, i;
314
  int err = 0;
315
 
316
  /* First, we must read the incomming command */
317
  if(gdb_read(&command, sizeof(uint32_t)) < 0) {
318
    if(gdb_fd) {
319
      perror("gdb socket - 1");
320
      close(gdb_fd);
321
      gdb_fd = 0;
322
    }
323
    return;
324
  }
325
  if(gdb_read(&length, sizeof(uint32_t)) < 0) {
326
    if(gdb_fd) {
327
      perror("gdb socket - 2");
328
      close(gdb_fd);
329
      gdb_fd = 0;
330
    }
331
    return;
332
  }
333
  length = ntohl(length);
334
 
335
  /* Now, verify the protocol and implement the command */
336
  switch(ntohl(command)) {
337
    case JTAG_COMMAND_WRITE:
338
      if(length != sizeof(msg_write) - 8) {
339
        ProtocolClean(length, JTAG_PROXY_PROTOCOL_ERROR);
340
        return;
341
      }
342
      buf = (char*)&msg_write;
343
      if(gdb_read(&buf[8], length) < 0) {
344
        if(gdb_fd) {
345
          perror("gdb socket - 3");
346
          close(gdb_fd);
347
          gdb_fd = 0;
348
        }
349
        return;
350
      }
351
      msg_write.address = ntohl(msg_write.address);
352
      msg_write.data_H = ntohl(msg_write.data_H);
353
      msg_write.data_L = ntohl(msg_write.data_L);
354
      err = gdb_write_reg(msg_write.address, msg_write.data_L);
355
      resp_write.status = htonl(err);
356
      if(gdb_write(&resp_write, sizeof(resp_write)) < 0) {
357
        if(gdb_fd) {
358
          perror("gdb socket - 4");
359
          close(gdb_fd);
360
          gdb_fd = 0;
361
        }
362
        return;
363
      }
364
      break;
365
    case JTAG_COMMAND_READ:
366
      if(length != sizeof(msg_read) - 8) {
367
        ProtocolClean(length, JTAG_PROXY_PROTOCOL_ERROR);
368
        return;
369
      }
370
      buf = (char*)&msg_read;
371
      if(gdb_read(&buf[8], length) < 0) {
372
        if(gdb_fd) {
373
          perror("gdb socket - 5");
374
          close(gdb_fd);
375
          gdb_fd = 0;
376
        }
377
        return;
378
      }
379
      msg_read.address = ntohl(msg_read.address);
380
      err = gdb_read_reg(msg_read.address, (unsigned long *)&resp_read.data_L);
381
      resp_read.status = htonl(err);
382
      resp_read.data_H = 0;
383
      resp_read.data_L = htonl(resp_read.data_L);
384
      if(gdb_write(&resp_read, sizeof(resp_read)) < 0) {
385
        if(gdb_fd) {
386
          perror("gdb socket - 6");
387
          close(gdb_fd);
388
          gdb_fd = 0;
389
        }
390
        return;
391
        }
392
      break;
393
    case JTAG_COMMAND_BLOCK_WRITE:
394
      if(length < sizeof(JTAGProxyBlockWriteMessage)-8) {
395
        ProtocolClean(length, JTAG_PROXY_PROTOCOL_ERROR);
396
        return;
397
      }
398
      if(!(buf = (char*)malloc(8+length))) {
399
        ProtocolClean(length, JTAG_PROXY_OUT_OF_MEMORY);
400
        return;
401
      }
402
      msg_bwrite = (JTAGProxyBlockWriteMessage*)buf;
403
      if(gdb_read(&buf[8], length) < 0) {
404
        if(gdb_fd) {
405
          perror("gdb socket - 5");
406
          close(gdb_fd);
407
          gdb_fd = 0;
408
        }
409
        free(buf);
410
        return;
411
      }
412
      msg_bwrite->address = ntohl(msg_bwrite->address);
413
      msg_bwrite->nRegisters = ntohl(msg_bwrite->nRegisters);
414
      for(i=0;i<msg_bwrite->nRegisters;i++) {
415
        msg_bwrite->data[i] = ntohl(msg_bwrite->data[i]);
416
      }
417
      err = gdb_write_block(msg_bwrite->address, (unsigned long*)msg_bwrite->data, msg_bwrite->nRegisters * 4);
418
      resp_bwrite.status = htonl(err);
419
      free(buf);
420
      msg_bwrite = (JTAGProxyBlockWriteMessage *)NULL;
421
      buf = (char *)msg_bwrite;
422
      if(gdb_write(&resp_bwrite, sizeof(resp_bwrite)) < 0) {
423
        if(gdb_fd) {
424
          perror("gdb socket - 4");
425
          close(gdb_fd);
426
          gdb_fd = 0;
427
        }
428
        return;
429
      }
430
      break;
431
    case JTAG_COMMAND_BLOCK_READ:
432
      if(length != sizeof(msg_bread) - 8) {
433
        ProtocolClean(length, JTAG_PROXY_PROTOCOL_ERROR);
434
        return;
435
      }
436
      buf = (char*)&msg_bread;
437
      if(gdb_read(&buf[8], length) < 0) {
438
        if(gdb_fd) {
439
          perror("gdb socket - 5");
440
          close(gdb_fd);
441
          gdb_fd = 0;
442
        }
443
        return;
444
      }
445
      msg_bread.address = ntohl(msg_bread.address);
446
      msg_bread.nRegisters = ntohl(msg_bread.nRegisters);
447
      len = sizeof(JTAGProxyBlockReadResponse) + 4*(msg_bread.nRegisters-1);
448
      if(!(buf = (char*)malloc(len))) {
449
        ProtocolClean(0, JTAG_PROXY_OUT_OF_MEMORY);
450
        return;
451
      }
452
      resp_bread = (JTAGProxyBlockReadResponse*)buf;
453
      err = gdb_read_block(msg_bread.address, (unsigned long*)resp_bread->data, msg_bread.nRegisters * 4);
454
      for(i=0;i<msg_bread.nRegisters;i++) {
455
        /* Read previous, address next one. */
456
        resp_bread->data[i] = htonl(resp_bread->data[i]);
457
      }
458
      resp_bread->status = htonl(err);
459
      resp_bread->nRegisters = htonl(msg_bread.nRegisters);
460
      if(gdb_write(resp_bread, len) < 0) {
461
        if(gdb_fd) {
462
          perror("gdb socket - 6");
463
          close(gdb_fd);
464
          gdb_fd = 0;
465
        }
466
        free(buf);
467
        return;
468
      }
469
      free(buf);
470
      resp_bread = (JTAGProxyBlockReadResponse *)NULL;
471
      buf = (char *)resp_bread;
472
      break;
473
    case JTAG_COMMAND_CHAIN:
474
      if(length != sizeof(msg_chain) - 8) {
475
        ProtocolClean(length, JTAG_PROXY_PROTOCOL_ERROR);
476
        return;
477
      }
478
      buf = (char*)&msg_chain;
479
      if(gdb_read(&buf[8], sizeof(msg_chain)-8) < 0) {
480
        if(gdb_fd) {
481
          perror("gdb socket - 7");
482
          close(gdb_fd);
483
          gdb_fd = 0;
484
        }
485
        return;
486
      }
487
      msg_chain.chain = htonl(msg_chain.chain);
488
      err = gdb_set_chain(msg_chain.chain);
489
      resp_chain.status = htonl(err);
490
      if(gdb_write(&resp_chain, sizeof(resp_chain)) < 0) {
491
        if(gdb_fd) {
492
          perror("gdb socket - 8");
493
          close(gdb_fd);
494
          gdb_fd = 0;
495
        }
496
        return;
497
      }
498
      break;
499
    default:
500
      perror("Unknown JTAG command.");
501
      ProtocolClean(length, JTAG_PROXY_COMMAND_NOT_IMPLEMENTED);
502
      break;
503
  }
504
}
505
 
506
static void ProtocolClean(int length, int32_t err) {
507
  char buf[4096];
508
 
509
  err = htonl(err);
510
  if((gdb_read(buf, length) < 0) || (gdb_write(&err, sizeof(err)) < 0) && gdb_fd) {
511
    perror("gdb socket - 9");
512
    close(gdb_fd);
513
    gdb_fd = 0;
514
  }
515
}
516
 
517
static int gdb_write(void* buf, int len) {
518
  int n;
519
  char* w_buf = (char*)buf;
520
  struct pollfd block;
521
 
522
  while(len) {
523
    if((n = write(gdb_fd, w_buf, len)) < 0) {
524
      switch(errno) {
525
        case EWOULDBLOCK: /* or EAGAIN */
526
          /* We've been called on a descriptor marked
527
             for nonblocking I/O. We better simulate
528
             blocking behavior. */
529
          block.fd = gdb_fd;
530
          block.events = POLLOUT;
531
          block.revents = 0;
532
          poll(&block, 1, -1);
533
          continue;
534
        case EINTR:
535
          continue;
536
        case EPIPE:
537
          close(gdb_fd);
538
          gdb_fd = 0;
539
          return -1;
540
        default:
541
          return -1;
542
        }
543
      } else {
544
        len -= n;
545
        w_buf += n;
546
      }
547
  }
548
  return 0;
549
}
550
 
551
static int gdb_read(void* buf, int len) {
552
  int n;
553
  char* r_buf = (char*)buf;
554
  struct pollfd block;
555
 
556
  while(len) {
557
    if((n = read(gdb_fd, r_buf, len)) < 0) {
558
      switch(errno) {
559
        case EWOULDBLOCK: /* or EAGAIN */
560
          /* We've been called on a descriptor marked
561
       for nonblocking I/O. We better simulate
562
       blocking behavior. */
563
          block.fd = gdb_fd;
564
          block.events = POLLIN;
565
          block.revents = 0;
566
          poll(&block, 1, -1);
567
          continue;
568
        case EINTR:
569
          continue;
570
        default:
571
          return -1;
572
        }
573
    } else if(n == 0) {
574
      close(gdb_fd);
575
      gdb_fd = 0;
576
      return -1;
577
    } else {
578
      len -= n;
579
      r_buf += n;
580
    }
581
  }
582
  return 0;
583
}

powered by: WebSVN 2.1.0

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