aboutsummaryrefslogtreecommitdiff
path: root/sim/arm/kid.c
blob: 10e3f35821eb6a40d4471603fdd8746749cf9c2a (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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*  kid.c -- ARMulator RDP/RDI interface:  ARM6 Instruction Emulator.
    Copyright (C) 1994 Advanced RISC Machines Ltd.
 
    This program 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 of the License, or
    (at your option) any later version.
 
    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

/*****************************************************************/
/* The child process continues here...                           */
/* It waits on a pipe from the parent and translates the RDP     */
/* messages into RDI calls to the ARMulator passing RDP replies  */
/* back up a pipe to the parent.                                 */
/*****************************************************************/

#include <sys/types.h>
#include <signal.h>

#include "armdefs.h"
#include "dbg_conf.h"
#include "dbg_hif.h"
#include "dbg_rdi.h"
#include "gdbhost.h"
#include "communicate.h"

/* The pipes between the two processes */
extern int mumkid[2];
extern int kidmum[2];

/* The maximum number of file descriptors */
extern int nfds;

/* The machine name */
#define MAXHOSTNAMELENGTH 64
extern char localhost[MAXHOSTNAMELENGTH + 1];

/* The socket number */
extern unsigned int socketnumber;

/* RDI interface */
extern const struct RDIProcVec armul_rdi;

static int MYrdp_level = 0;

static int rdi_state = 0;

/**************************************************************/
/* Signal handler that terminates excecution in the ARMulator */
/**************************************************************/
void kid_handlesignal(int sig) {
#ifdef DEBUG
  fprintf(stderr, "Terminate ARMulator excecution\n");
#endif
  if (sig != SIGUSR1) {
    fprintf(stderr, "Unsupported signal.\n");
    return;
  }
  armul_rdi.info(RDISignal_Stop, (unsigned long *) 0, (unsigned long *) 0);
}

/********************************************************************/
/* Waits on a pipe from the socket demon for RDP and                */
/* acts as an RDP to RDI interpreter on the front of the ARMulator. */
/********************************************************************/
void kid() {
  char *p, *q;
  int i, j, k;
  long outofthebag;
  unsigned char c, d, message;
  ARMword x, y, z;
  struct sigaction action;
  PointHandle point;
  Dbg_ConfigBlock config;
  Dbg_HostosInterface hostif;
  struct Dbg_MCState *MCState;
  char command_line[256];
  struct fd_set readfds;
  
  /* Setup a signal handler for SIGUSR1 */
  action.sa_handler = kid_handlesignal;
  action.sa_mask = 0;
  action.sa_flags = 0;
  
  sigaction(SIGUSR1, &action, (struct sigaction *) 0);
  
  while (1)
  {
    /* Wait for ever */
    FD_ZERO(&readfds);
    FD_SET(mumkid[0], &readfds);
    
    i = select(nfds, &readfds,
	       (fd_set *) 0,
	       (fd_set *) 0,
	       (struct timeval *) 0);
    
    if (i < 0) {
      perror("select");
    }
    
    if (read(mumkid[0], &message, 1) < 1) {
      perror("read");
    }

    switch (message) {
    case RDP_Start :
      /* Open and/or Initialise */
      BAG_newbag();

      MYread_char(mumkid[0], &c); /* type */
      MYread_word(mumkid[0], &x); /* memorysize */
      if (c & 0x2) MYread_char(mumkid[0], &d); /* speed */
      config.processor = 0;
      config.memorysize = x;
      config.bytesex = (c & 0x4) ? RDISex_Big : RDISex_Little;
      if (c & 0x8) config.bytesex = RDISex_DontCare;

      hostif.dbgprint = myprint;
      hostif.dbgpause = mypause;
      hostif.dbgarg = stdout;
      hostif.writec = mywritec;
      hostif.readc = myreadc;
      hostif.write = mywrite;
      hostif.gets = mygets;
      hostif.reset = mypause; /* do nothing */
      hostif.resetarg = "Do I love resetting or what!\n";

      if (rdi_state)
      {
	/* we have restarted, so kill off the existing run.  */
	/* armul_rdi.close(); */
      }
      i = armul_rdi.open(c & 0x3, &config, &hostif, MCState);
      rdi_state = 1;

      MYwrite_char(kidmum[1], RDP_Return);
      MYwrite_char(kidmum[1], (unsigned char) i);

      x = ~0x4;
      armul_rdi.info(RDIVector_Catch, &x, 0);

      break;

    case RDP_End :
      /* Close and Finalise */
      i = armul_rdi.close();
      rdi_state = 0;
      MYwrite_char(kidmum[1], RDP_Return);
      MYwrite_char(kidmum[1], (unsigned char) i);
      break;

    case RDP_Read :
      /* Read Memory Address */      
      MYread_word(mumkid[0], &x); /* address */
      MYread_word(mumkid[0], &y); /* nbytes */
      p = (char *) malloc(y);
      i = armul_rdi.read(x, p, (unsigned *) &y);
      MYwrite_char(kidmum[1], RDP_Return);
      for (k = 0; k < y; k++)
	MYwrite_char(kidmum[1], p[k]);
      free(p);
      MYwrite_char(kidmum[1], (unsigned char) i);
      if (i)
	MYwrite_word(kidmum[1], y); /* number of bytes sent without error */
      break;

    case RDP_Write :
      /* Write Memory Address */
      MYread_word(mumkid[0], &x); /* address */
      MYread_word(mumkid[0], &y); /* nbytes */
      p = (char *) malloc(y);
      for (k = 0; k < y; k++)
	MYread_char(mumkid[0], &p[k]);
      i = armul_rdi.write(p, x, (unsigned *) &y);
      free(p);
      MYwrite_char(kidmum[1], RDP_Return);
      MYwrite_char(kidmum[1], (unsigned char) i);
      if (i)
	MYwrite_word(kidmum[1], y); /* number of bytes sent without error */
      break;

    case RDP_CPUread :
      /* Read CPU State */
      MYread_char(mumkid[0], &c); /* mode */
      MYread_word(mumkid[0], &x); /* mask */
      p = (char *) malloc(4 * RDINumCPURegs);
      i = armul_rdi.CPUread(c, x, (ARMword *) p);
      MYwrite_char(kidmum[1], RDP_Return);
      for (k = 1, j = 0; k != 0x80000000; k *= 2)
	if (k & x) MYwrite_word(kidmum[1], ((ARMword *) p)[j++]);
      free(p);
      if (i) MYwrite_char(kidmum[1], (unsigned char) j);
      MYwrite_char(kidmum[1], (unsigned char) i);
      break;

    case RDP_CPUwrite :
      /* Write CPU State */
      MYread_char(mumkid[0], &c); /* mode */
      MYread_word(mumkid[0], &x); /* mask */

      p = (char *) malloc(4 * RDINumCPURegs);
      for (k = 1, j = 0; k != 0x80000000; k *= 2)
	if (k & x) MYread_word(mumkid[0], &(((ARMword *) p)[j++]));
      i = armul_rdi.CPUwrite(c, x, (ARMword *) p);
      MYwrite_char(kidmum[1], RDP_Return);
      MYwrite_char(kidmum[1], (unsigned char) i);
      free(p);
      break;

    case RDP_CPread :
      /* Read Co-Processor State */
      MYread_char(mumkid[0], &c); /* CPnum */
      MYread_word(mumkid[0], &x); /* mask */
      p = q = (char *) malloc(16 * RDINumCPRegs);
      i = armul_rdi.CPread(c, x, (ARMword *) p);
      MYwrite_char(kidmum[1], RDP_Return);
      for (k = 1, j = 0; k != 0x80000000; k *= 2, j++)
	if (k & x) {
	  if ((c == 1 || c == 2) && k <= 128) {
	    MYwrite_FPword(kidmum[1], q);
	    q += 16;
	  }
	  else {
	    MYwrite_word(kidmum[1], *q);
	    q += 4;
	  }
	}
      free(p);
      if (i) MYwrite_char(kidmum[1], (unsigned char) j);
      MYwrite_char(kidmum[1], (unsigned char) i);
      break;

    case RDP_CPwrite :
      /* Write Co-Processor State */
      MYread_char(mumkid[0], &c); /* CPnum */
      MYread_word(mumkid[0], &x); /* mask */
      p = q = (char *) malloc(16 * RDINumCPURegs);
      for (k = 1, j = 0; k != 0x80000000; k *= 2, j++)
	if (k & x) {
	  if ((c == 1 || c == 2) && k <= 128) {
	    MYread_FPword(kidmum[1], q);
	    q += 16;
	  }
	  else {
	    MYread_word(mumkid[0], (ARMword *) q);
	    q += 4;
	  }
	}
      i = armul_rdi.CPwrite(c, x, (ARMword *) p);
      MYwrite_char(kidmum[1], RDP_Return);
      MYwrite_char(kidmum[1], (unsigned char) i);
      free(p);
      break;
      
    case RDP_SetBreak :
      /* Set Breakpoint */
      MYread_word(mumkid[0], &x); /* address */
      MYread_char(mumkid[0], &c); /* type */
      if ((c & 0xf) >= 5) MYread_word(mumkid[0], &y); /* bound */
      i = armul_rdi.setbreak(x, c, y, &point);
      if (!MYrdp_level) BAG_putpair((long) x, (long) point);
      MYwrite_char(kidmum[1], RDP_Return);
      if (MYrdp_level) MYwrite_word(kidmum[1], point);
      MYwrite_char(kidmum[1], (unsigned char) i);      
      break;

    case RDP_ClearBreak :
      /* Clear Breakpoint */
      MYread_word(mumkid[0], &point); /* PointHandle */
      if (!MYrdp_level) {
	BAG_getsecond((long) point, &outofthebag); /* swap pointhandle for address */
	BAG_killpair_byfirst(outofthebag);
	point = outofthebag;
      }
      i = armul_rdi.clearbreak(point);
      MYwrite_char(kidmum[1], RDP_Return);
      MYwrite_char(kidmum[1], (unsigned char) i);
      break;

    case RDP_SetWatch :
      /* Set Watchpoint */
      MYread_word(mumkid[0], &x); /* address */
      MYread_char(mumkid[0], &c); /* type */
      MYread_char(mumkid[0], &d); /* datatype */
      if ((c & 0xf) >= 5) MYread_word(mumkid[0], &y); /* bound */
      i = armul_rdi.setwatch(x, c, d, y, &point);
      MYwrite_char(kidmum[1], RDP_Return);
      MYwrite_word(kidmum[1], point);
      MYwrite_char(kidmum[1], (unsigned char) i);
      break;

    case RDP_ClearWatch :
      /* Clear Watchpoint */
      MYread_word(mumkid[0], &point); /* PointHandle */
      i = armul_rdi.clearwatch(point);
      MYwrite_char(kidmum[1], RDP_Return);
      MYwrite_char(kidmum[1], (unsigned char) i);
      break;

    case RDP_Execute :
      /* Excecute */
      
      MYread_char(mumkid[0], &c); /* return */

#ifdef DEBUG
      fprintf(stderr, "Starting execution\n");
#endif
      i = armul_rdi.execute(&point);
#ifdef DEBUG
      fprintf(stderr, "Completed execution\n");
#endif
      MYwrite_char(kidmum[1], RDP_Return);
      if (c & 0x80) MYwrite_word(kidmum[1], point);
      MYwrite_char(kidmum[1], (unsigned char) i);
      break;

    case RDP_Step :
      /* Step */
      MYread_char(mumkid[0], &c); /* return */
      MYread_word(mumkid[0], &x); /* ninstr */
      point = 0x87654321;
      i = armul_rdi.step(x, &point);
      MYwrite_char(kidmum[1], RDP_Return);
      if (c & 0x80) MYwrite_word(kidmum[1], point);
      MYwrite_char(kidmum[1], (unsigned char) i);
      break;

    case RDP_Info:
      /* Info */
      MYread_word (mumkid[0], &x);
      switch (x)
	{
	case RDIInfo_Target:
	  i = armul_rdi.info (RDIInfo_Target, &y, &z);
	  MYwrite_char (kidmum[1], RDP_Return);
	  MYwrite_word (kidmum[1], y); /* Loads of info... */
	  MYwrite_word (kidmum[1], z); /* Model */
	  MYwrite_char (kidmum[1], (unsigned char) i);
	  break;

	case RDISet_RDILevel:
	  MYread_word (mumkid[0], &x); /* arg1, debug level */
	  i = armul_rdi.info (RDISet_RDILevel, &x, 0);
	  if (i == RDIError_NoError)
	    MYrdp_level = x;
	  MYwrite_char (kidmum[1], RDP_Return);
	  MYwrite_char (kidmum[1], (unsigned char) i);
	  break;

	case RDISet_Cmdline:
	  for (p = command_line; MYread_char (mumkid[0], p), *p; p++)
	    ; /* String */
	  i = armul_rdi.info (RDISet_Cmdline,
			      (unsigned long *) command_line, 0);
	  MYwrite_char (kidmum[1], RDP_Return);
	  MYwrite_char (kidmum[1], (unsigned char) i);
	  break;

	case RDIInfo_Step:
	  i = armul_rdi.info (RDIInfo_Step, &x, 0);
	  MYwrite_char (kidmum[1], RDP_Return);
	  MYwrite_word (kidmum[1], x);
	  MYwrite_char (kidmum[1], (unsigned char) i);
	  break;

	case RDIVector_Catch:
	  MYread_word (mumkid[0], &x);
	  i = armul_rdi.info (RDIVector_Catch, &x, 0);
	  MYwrite_char (kidmum[1], RDP_Return);
	  MYwrite_char (kidmum[1], i);
	  break;

	case RDIInfo_Points:
	  i = armul_rdi.info (RDIInfo_Points, &x, 0);
	  MYwrite_char (kidmum[1], RDP_Return);
	  MYwrite_word (kidmum[1], x);
	  MYwrite_char (kidmum[1], (unsigned char) i);
	  break;

	default:
	  fprintf (stderr, "Unsupported info code %d\n", x);
	  break;
	}
      break;

    case RDP_OSOpReply:
      /* OS Operation Reply */
      MYwrite_char (kidmum[1], RDP_Fatal);
      break;

    case RDP_Reset:
      /* Reset */
      for (i = 0; i < 50; i++)
	MYwrite_char(kidmum[1], RDP_Reset);
      p = (char *) malloc(MAXHOSTNAMELENGTH + 5 + 20);
      sprintf(p, "Running on %s:%d\n", localhost, socketnumber);
      MYwrite_string(kidmum[1], p);
      free(p);
      
      break;
    default:
      fprintf (stderr, "Oh dear: Something is seriously wrong :-(\n");
      /* Hmm.. bad RDP operation */
      break;
    }
  }
}


/* Handles memory read operations until an OS Operation Reply Message is */
/* encounterd. It then returns the byte info value (0, 1, or 2) and fills  */
/* in 'putinr0' with the data if appropriate. */
int wait_for_osreply(ARMword *reply)
{
  char *p, *q;
  int i, j, k;
  unsigned char c, d, message;
  ARMword x, y, z;
  struct sigaction action;
  PointHandle point;
  Dbg_ConfigBlock config;
  Dbg_HostosInterface hostif;
  struct Dbg_MCState *MCState;
  char command_line[256];
  struct fd_set readfds;
  
#ifdef DEBUG
  fprintf(stderr, "wait_for_osreply ().\n");
#endif

  /* Setup a signal handler for SIGUSR1 */
  action.sa_handler = kid_handlesignal;
  action.sa_mask = 0;
  action.sa_flags = 0;
  
  sigaction(SIGUSR1, &action, (struct sigaction *) 0);
  
  while (1)
  {
    /* Wait for ever */
    FD_ZERO(&readfds);
    FD_SET(mumkid[0], &readfds);
    
    i = select(nfds, &readfds,
	       (fd_set *) 0,
	       (fd_set *) 0,
	       (struct timeval *) 0);
    
    if (i < 0) {
      perror("select");
    }
    
    if (read(mumkid[0], &message, 1) < 1) {
      perror("read");
    }
    
    switch (message) {
    case RDP_Read :
      /* Read Memory Address */
      MYread_word(mumkid[0], &x); /* address */
      MYread_word(mumkid[0], &y); /* nbytes */
      p = (char *) malloc(y);
      i = armul_rdi.read(x, p, (unsigned *) &y);
      MYwrite_char(kidmum[1], RDP_Return);
      for (k = 0; k < y; k++)
	MYwrite_char(kidmum[1], p[k]);
      free(p);
      MYwrite_char(kidmum[1], (unsigned char) i);
      if (i)
	MYwrite_word(kidmum[1], y); /* number of bytes sent without error */
      break;

    case RDP_Write :
      /* Write Memory Address */
      MYread_word(mumkid[0], &x); /* address */
      MYread_word(mumkid[0], &y); /* nbytes */
      p = (char *) malloc(y);
      for (k = 0; k < y; k++)
	MYread_char(mumkid[0], &p[k]);
      i = armul_rdi.write(p, x, (unsigned *) &y);
      free(p);
      MYwrite_char(kidmum[1], RDP_Return);
      MYwrite_char(kidmum[1], (unsigned char) i);
      if (i)
	MYwrite_word(kidmum[1], y); /* number of bytes sent without error */
      break;
      
    case RDP_OSOpReply :
      /* OS Operation Reply */
      MYread_char(mumkid[0], &c);
      if (c == 1) MYread_char(mumkid[0], (char *) reply);
      if (c == 2) MYread_word(mumkid[0], reply);
      return c;
      break;
      
    default :
      fprintf(stderr, "HELP! Unaccounted-for message during OS request. \n");
      MYwrite_char(kidmum[1], RDP_Fatal);
    }
  }
}