/[gxemul]/trunk/src/emul.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/src/emul.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by dpavlin, Mon Oct 8 16:17:48 2007 UTC revision 42 by dpavlin, Mon Oct 8 16:22:32 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2003-2005  Anders Gavare.  All rights reserved.   *  Copyright (C) 2003-2007  Anders Gavare.  All rights reserved.
3   *   *
4   *  Redistribution and use in source and binary forms, with or without   *  Redistribution and use in source and binary forms, with or without
5   *  modification, are permitted provided that the following conditions are met:   *  modification, are permitted provided that the following conditions are met:
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: emul.c,v 1.179 2005/03/14 19:14:04 debug Exp $   *  $Id: emul.c,v 1.297 2007/06/15 17:02:37 debug Exp $
29   *   *
30   *  Emulation startup and misc. routines.   *  Emulation startup and misc. routines.
31   */   */
# Line 39  Line 39 
39  #include <unistd.h>  #include <unistd.h>
40    
41  #include "arcbios.h"  #include "arcbios.h"
 #include "bintrans.h"  
42  #include "cpu.h"  #include "cpu.h"
 #include "cpu_mips.h"  
43  #include "emul.h"  #include "emul.h"
44  #include "console.h"  #include "console.h"
45  #include "debugger.h"  #include "debugger.h"
46  #include "device.h"  #include "device.h"
47  #include "diskimage.h"  #include "diskimage.h"
48    #include "exec_elf.h"
49  #include "machine.h"  #include "machine.h"
50  #include "memory.h"  #include "memory.h"
51  #include "mips_cpu_types.h"  #include "mips_cpu_types.h"
52  #include "misc.h"  #include "misc.h"
53  #include "net.h"  #include "net.h"
54  #include "sgi_arcbios.h"  #include "settings.h"
55    #include "timer.h"
56    #include "useremul.h"
57  #include "x11.h"  #include "x11.h"
58    
59    
 extern int force_debugger_at_exit;  
   
60  extern int extra_argc;  extern int extra_argc;
61  extern char **extra_argv;  extern char **extra_argv;
62    
63  extern int verbose;  extern int verbose;
64  extern int quiet_mode;  extern int quiet_mode;
65    extern int force_debugger_at_exit;
66  extern struct emul *debugger_emul;  extern int single_step;
67  extern struct diskimage *diskimages[];  extern int old_show_trace_tree;
68    extern int old_instruction_trace;
69    extern int old_quiet_mode;
70    extern int quiet_mode;
71    extern int native_code_translation_enabled;
72    
73    
74  /*  /*
75   *  add_dump_points():   *  add_breakpoints():
76   *   *
77   *  Take the strings breakpoint_string[] and convert to addresses   *  Take the strings breakpoint_string[] and convert to addresses
78   *  (and store them in breakpoint_addr[]).   *  (and store them in breakpoint_addr[]).
79   *   *
80   *  TODO: This function should be moved elsewhere.   *  TODO: This function should be moved elsewhere.
81   */   */
82  static void add_dump_points(struct machine *m)  static void add_breakpoints(struct machine *m)
83  {  {
84          int i;          int i;
85          int string_flag;          int string_flag;
86          uint64_t dp;          uint64_t dp;
87    
88          for (i=0; i<m->n_breakpoints; i++) {          for (i=0; i<m->breakpoints.n; i++) {
89                  string_flag = 0;                  string_flag = 0;
90                  dp = strtoull(m->breakpoint_string[i], NULL, 0);                  dp = strtoull(m->breakpoints.string[i], NULL, 0);
91    
92                  /*                  /*
93                   *  If conversion resulted in 0, then perhaps it is a                   *  If conversion resulted in 0, then perhaps it is a
# Line 93  static void add_dump_points(struct machi Line 96  static void add_dump_points(struct machi
96                  if (dp == 0) {                  if (dp == 0) {
97                          uint64_t addr;                          uint64_t addr;
98                          int res = get_symbol_addr(&m->symbol_context,                          int res = get_symbol_addr(&m->symbol_context,
99                              m->breakpoint_string[i], &addr);                              m->breakpoints.string[i], &addr);
100                          if (!res)                          if (!res) {
101                                  fprintf(stderr,                                  fprintf(stderr,
102                                      "WARNING! Breakpoint '%s' could not be"                                      "ERROR! Breakpoint '%s' could not be"
103                                          " parsed\n",                                          " parsed\n",
104                                      m->breakpoint_string[i]);                                      m->breakpoints.string[i]);
105                          else {                                  exit(1);
106                            } else {
107                                  dp = addr;                                  dp = addr;
108                                  string_flag = 1;                                  string_flag = 1;
109                          }                          }
# Line 110  static void add_dump_points(struct machi Line 114  static void add_dump_points(struct machi
114                   *  were automatically converted into the correct address.                   *  were automatically converted into the correct address.
115                   */                   */
116    
117                  if ((dp >> 32) == 0 && ((dp >> 31) & 1))                  if (m->arch == ARCH_MIPS) {
118                          dp |= 0xffffffff00000000ULL;                          if ((dp >> 32) == 0 && ((dp >> 31) & 1))
119                  m->breakpoint_addr[i] = dp;                                  dp |= 0xffffffff00000000ULL;
120                    }
121    
122                    m->breakpoints.addr[i] = dp;
123    
124                  debug("breakpoint %i: 0x%016llx", i, (long long)dp);                  debug("breakpoint %i: 0x%llx", i, (long long)dp);
125                  if (string_flag)                  if (string_flag)
126                          debug(" (%s)", m->breakpoint_string[i]);                          debug(" (%s)", m->breakpoints.string[i]);
127                  debug("\n");                  debug("\n");
128          }          }
129  }  }
# Line 127  static void add_dump_points(struct machi Line 134  static void add_dump_points(struct machi
134   */   */
135  static void fix_console(void)  static void fix_console(void)
136  {  {
137          console_deinit();          console_deinit_main();
138  }  }
139    
140    
141  /*  /*
142   *  load_bootblock():   *  emul_new():
143   *   *
144   *  For some emulation modes, it is possible to boot from a harddisk image by   *  Returns a reasonably initialized struct emul.
  *  loading a bootblock from a specific disk offset into memory, and executing  
  *  that, instead of requiring a separate kernel file.  It is then up to the  
  *  bootblock to load a kernel.  
145   */   */
146  static void load_bootblock(struct machine *m, struct cpu *cpu)  struct emul *emul_new(char *name, int id)
147  {  {
148          int boot_disk_id;          struct emul *e;
         unsigned char minibuf[0x20];  
         unsigned char *bootblock_buf;  
         uint64_t bootblock_offset;  
         uint64_t bootblock_loadaddr, bootblock_pc;  
         int n_blocks, res, readofs;  
   
         boot_disk_id = diskimage_bootdev(m);  
         if (boot_disk_id < 0)  
                 return;  
   
         switch (m->machine_type) {  
         case MACHINE_DEC:  
                 /*  
                  *  The first few bytes of a disk contains information about  
                  *  where the bootblock(s) are located. (These are all 32-bit  
                  *  little-endian words.)  
                  *  
                  *  Offset 0x10 = load address  
                  *         0x14 = initial PC value  
                  *         0x18 = nr of 512-byte blocks to read  
                  *         0x1c = offset on disk to where the bootblocks  
                  *                are (in 512-byte units)  
                  *         0x20 = nr of blocks to read...  
                  *         0x24 = offset...  
                  *  
                  *  nr of blocks to read and offset are repeated until nr of  
                  *  blocks to read is zero.  
                  */  
                 res = diskimage_access(m, boot_disk_id, 0, 0,  
                     minibuf, sizeof(minibuf));  
   
                 bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8)  
                   + (minibuf[0x12] << 16) + (minibuf[0x13] << 24);  
   
                 /*  Convert loadaddr to uncached:  */  
                 if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 &&  
                     (bootblock_loadaddr & 0xf0000000ULL) != 0xa0000000)  
                         fatal("\nWARNING! Weird load address 0x%08x.\n\n",  
                             (int)bootblock_loadaddr);  
                 bootblock_loadaddr &= 0x0fffffffULL;  
                 bootblock_loadaddr |= 0xffffffffa0000000ULL;  
   
                 bootblock_pc = minibuf[0x14] + (minibuf[0x15] << 8)  
                   + (minibuf[0x16] << 16) + (minibuf[0x17] << 24);  
   
                 bootblock_pc &= 0x0fffffffULL;  
                 bootblock_pc |= 0xffffffffa0000000ULL;  
                 cpu->pc = bootblock_pc;  
   
                 debug("DEC boot: loadaddr=0x%08x, pc=0x%08x",  
                     (int)bootblock_loadaddr, (int)bootblock_pc);  
   
                 readofs = 0x18;  
   
                 for (;;) {  
                         res = diskimage_access(m, boot_disk_id, 0, readofs,  
                             minibuf, sizeof(minibuf));  
                         if (!res) {  
                                 printf("couldn't read disk?\n");  
                                 exit(1);  
                         }  
   
                         n_blocks = minibuf[0] + (minibuf[1] << 8)  
                           + (minibuf[2] << 16) + (minibuf[3] << 24);  
   
                         bootblock_offset = (minibuf[4] + (minibuf[5] << 8)  
                           + (minibuf[6] << 16) + (minibuf[7] << 24)) * 512;  
149    
150                          if (n_blocks < 1)          CHECK_ALLOCATION(e = malloc(sizeof(struct emul)));
151                                  break;          memset(e, 0, sizeof(struct emul));
152    
153                          debug(readofs == 0x18? ": %i" : " + %i", n_blocks);          CHECK_ALLOCATION(e->path = malloc(15));
154            snprintf(e->path, 15, "emul[%i]", id);
155    
156                          if (n_blocks * 512 > 65536)          e->settings = settings_new();
                                 fatal("\nWARNING! Unusually large bootblock "  
                                     "(%i bytes)\n\n", n_blocks * 512);  
157    
158                          bootblock_buf = malloc(n_blocks * 512);          settings_add(e->settings, "n_machines", 0,
159                          if (bootblock_buf == NULL) {              SETTINGS_TYPE_INT, SETTINGS_FORMAT_DECIMAL,
160                                  fprintf(stderr, "out of memory in "              (void *) &e->n_machines);
                                     "load_bootblock()\n");  
                                 exit(1);  
                         }  
161    
162                          res = diskimage_access(m, boot_disk_id, 0,          /*  TODO: More settings?  */
                             bootblock_offset, bootblock_buf, n_blocks * 512);  
                         if (!res) {  
                                 fatal("WARNING: could not load bootblocks from"  
                                     " disk offset 0x%llx\n",  
                                     (long long)bootblock_offset);  
                         }  
163    
164                          store_buf(cpu, bootblock_loadaddr,          /*  Sane default values:  */
165                              (char *)bootblock_buf, n_blocks * 512);          e->n_machines = 0;
166            e->next_serial_nr = 1;
                         bootblock_loadaddr += 512*n_blocks;  
                         free(bootblock_buf);  
                         readofs += 8;  
                 }  
167    
168                  debug(readofs == 0x18? ": no blocks?\n" : " blocks\n");          if (name != NULL) {
169                  break;                  CHECK_ALLOCATION(e->name = strdup(name));
170          default:                  settings_add(e->settings, "name", 0,
171                  fatal("Booting from disk without a separate kernel "                      SETTINGS_TYPE_STRING, SETTINGS_FORMAT_STRING,
172                      "doesn't work in this emulation mode.\n");                      (void *) &e->name);
                 exit(1);  
173          }          }
174    
175            return e;
176  }  }
177    
178    
179  /*  /*
180   *  emul_new():   *  emul_destroy():
181   *   *
182   *  Returns a reasonably initialized struct emul.   *  Destroys a previously created emul object.
183   */   */
184  struct emul *emul_new(char *name)  void emul_destroy(struct emul *emul)
185  {  {
186          struct emul *e;          int i;
187          e = malloc(sizeof(struct emul));  
188          if (e == NULL) {          if (emul->name != NULL) {
189                  fprintf(stderr, "out of memory in emul_new()\n");                  settings_remove(emul->settings, "name");
190                  exit(1);                  free(emul->name);
191          }          }
192    
193          memset(e, 0, sizeof(struct emul));          for (i=0; i<emul->n_machines; i++)
194                    machine_destroy(emul->machines[i]);
195    
196          /*  Sane default values:  */          if (emul->machines != NULL)
197          e->n_machines = 0;                  free(emul->machines);
198    
199          if (name != NULL) {          /*  Remove any remaining level-1 settings:  */
200                  e->name = strdup(name);          settings_remove_all(emul->settings);
201                  if (e->name == NULL) {          settings_destroy(emul->settings);
                         fprintf(stderr, "out of memory in emul_new()\n");  
                         exit(1);  
                 }  
         }  
202    
203          return e;          free(emul);
204  }  }
205    
206    
# Line 294  struct emul *emul_new(char *name) Line 215  struct emul *emul_new(char *name)
215  struct machine *emul_add_machine(struct emul *e, char *name)  struct machine *emul_add_machine(struct emul *e, char *name)
216  {  {
217          struct machine *m;          struct machine *m;
218            char tmpstr[20];
219            int i;
220    
221          m = machine_new(name, e);          m = machine_new(name, e, e->n_machines);
222          m->serial_nr = (e->next_serial_nr ++);          m->serial_nr = (e->next_serial_nr ++);
223    
224          e->n_machines ++;          i = e->n_machines ++;
225          e->machines = realloc(e->machines,  
226              sizeof(struct machine *) * e->n_machines);          CHECK_ALLOCATION(e->machines = realloc(e->machines,
227          if (e->machines == NULL) {              sizeof(struct machine *) * e->n_machines));
228                  fprintf(stderr, "emul_add_machine(): out of memory\n");  
229                  exit(1);          e->machines[i] = m;
230          }  
231            snprintf(tmpstr, sizeof(tmpstr), "machine[%i]", i);
232            settings_add(e->settings, tmpstr, 1, SETTINGS_TYPE_SUBSETTINGS, 0,
233                e->machines[i]->settings);
234    
         e->machines[e->n_machines - 1] = m;  
235          return m;          return m;
236  }  }
237    
# Line 332  static void add_arc_components(struct ma Line 257  static void add_arc_components(struct ma
257    
258          len += 1048576 * m->memory_offset_in_mb;          len += 1048576 * m->memory_offset_in_mb;
259    
260          /*  NOTE/TODO: magic 12MB end of load program area  */          /*
261             *  NOTE/TODO: magic 12MB end of load program area
262             *
263             *  Hm. This breaks the old FreeBSD/MIPS snapshots...
264             */
265    #if 0
266          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
267              0x60000 + m->memory_offset_in_mb * 1048576,              0x60000 + m->memory_offset_in_mb * 1048576,
268              start-0x60000 - m->memory_offset_in_mb * 1048576,              start-0x60000 - m->memory_offset_in_mb * 1048576,
269              ARCBIOS_MEM_FreeMemory);              ARCBIOS_MEM_FreeMemory);
270    #endif
271          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
272              start, len, ARCBIOS_MEM_LoadedProgram);              start, len, ARCBIOS_MEM_LoadedProgram);
273    
274          scsicontroller = arcbios_get_scsicontroller();          scsicontroller = arcbios_get_scsicontroller(m);
275          if (scsicontroller == 0)          if (scsicontroller == 0)
276                  return;                  return;
277    
# Line 388  static void add_arc_components(struct ma Line 319  static void add_arc_components(struct ma
319                                  snprintf(component_string,                                  snprintf(component_string,
320                                      sizeof(component_string),                                      sizeof(component_string),
321                                      "scsi(0)cdrom(%i)", d->id);                                      "scsi(0)cdrom(%i)", d->id);
322                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
323                                      component_string, scsidevice);                                      component_string, scsidevice);
324    
325                                  snprintf(component_string,                                  snprintf(component_string,
326                                      sizeof(component_string),                                      sizeof(component_string),
327                                      "scsi(0)cdrom(%i)fdisk(0)", d->id);                                      "scsi(0)cdrom(%i)fdisk(0)", d->id);
328                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
329                                      component_string, scsidisk);                                      component_string, scsidisk);
330                          } else {                          } else {
331                                  snprintf(component_string,                                  snprintf(component_string,
332                                      sizeof(component_string),                                      sizeof(component_string),
333                                      "scsi(0)disk(%i)", d->id);                                      "scsi(0)disk(%i)", d->id);
334                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
335                                      component_string, scsidevice);                                      component_string, scsidevice);
336    
337                                  snprintf(component_string,                                  snprintf(component_string,
338                                      sizeof(component_string),                                      sizeof(component_string),
339                                      "scsi(0)disk(%i)rdisk(0)", d->id);                                      "scsi(0)disk(%i)rdisk(0)", d->id);
340                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
341                                      component_string, scsidisk);                                      component_string, scsidisk);
342                          }                          }
343                  }                  }
# Line 429  static void add_arc_components(struct ma Line 360  static void add_arc_components(struct ma
360  void emul_machine_setup(struct machine *m, int n_load, char **load_names,  void emul_machine_setup(struct machine *m, int n_load, char **load_names,
361          int n_devices, char **device_names)          int n_devices, char **device_names)
362  {  {
         struct emul *emul;  
363          struct cpu *cpu;          struct cpu *cpu;
364          int i, iadd=4;          int i, iadd = DEBUG_INDENTATION;
365          uint64_t addr, memory_amount, entrypoint = 0, gp = 0, toc = 0;          uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0;
366          int byte_order;          int byte_order;
367    
         emul = m->emul;  
   
368          debug("machine \"%s\":\n", m->name);          debug("machine \"%s\":\n", m->name);
369          debug_indentation(iadd);          debug_indentation(iadd);
370    
# Line 457  void emul_machine_setup(struct machine * Line 385  void emul_machine_setup(struct machine *
385    
386          m->cpu_family = cpu_family_ptr_by_number(m->arch);          m->cpu_family = cpu_family_ptr_by_number(m->arch);
387    
388            if (m->arch == ARCH_ALPHA)
389                    m->arch_pagesize = 8192;
390    
391          machine_memsize_fix(m);          machine_memsize_fix(m);
392    
393          /*          /*
# Line 477  void emul_machine_setup(struct machine * Line 408  void emul_machine_setup(struct machine *
408                  debug(" (offset by %iMB)", m->memory_offset_in_mb);                  debug(" (offset by %iMB)", m->memory_offset_in_mb);
409                  memory_amount += 1048576 * m->memory_offset_in_mb;                  memory_amount += 1048576 * m->memory_offset_in_mb;
410          }          }
411          m->memory = memory_new(memory_amount);          m->memory = memory_new(memory_amount, m->arch);
412          if (m->machine_type != MACHINE_USERLAND)          if (m->machine_type != MACHINE_USERLAND)
413                  debug("\n");                  debug("\n");
414    
# Line 488  void emul_machine_setup(struct machine * Line 419  void emul_machine_setup(struct machine *
419                  /*  TODO: This should be moved elsewhere...  */                  /*  TODO: This should be moved elsewhere...  */
420                  if (m->machine_type == MACHINE_BEBOX)                  if (m->machine_type == MACHINE_BEBOX)
421                          m->ncpus = 2;                          m->ncpus = 2;
                 else if (m->machine_type == MACHINE_ARC &&  
                     m->machine_subtype == MACHINE_ARC_NEC_R96)  
                         m->ncpus = 2;  
                 else if (m->machine_type == MACHINE_ARC &&  
                     m->machine_subtype == MACHINE_ARC_NEC_R98)  
                         m->ncpus = 4;  
422                  else                  else
423                          m->ncpus = 1;                          m->ncpus = 1;
424          }          }
         m->cpus = malloc(sizeof(struct cpu *) * m->ncpus);  
         if (m->cpus == NULL) {  
                 fprintf(stderr, "out of memory\n");  
                 exit(1);  
         }  
         memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus);  
425    
426          /*  Initialize dynamic binary translation, if available:  */          CHECK_ALLOCATION(m->cpus = malloc(sizeof(struct cpu *) * m->ncpus));
427          if (m->bintrans_enable)          memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus);
                 bintrans_init(m, m->memory);  
428    
429          debug("cpu0");          debug("cpu0");
430          if (m->ncpus > 1)          if (m->ncpus > 1)
# Line 514  void emul_machine_setup(struct machine * Line 432  void emul_machine_setup(struct machine *
432          debug(": ");          debug(": ");
433          for (i=0; i<m->ncpus; i++) {          for (i=0; i<m->ncpus; i++) {
434                  m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name);                  m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name);
435                  if (m->bintrans_enable)                  if (m->cpus[i] == NULL) {
436                          bintrans_init_cpu(m->cpus[i]);                          fprintf(stderr, "Unable to create CPU object. "
437                                "Aborting.");
438                            exit(1);
439                    }
440          }          }
441          debug("\n");          debug("\n");
442    
# Line 530  void emul_machine_setup(struct machine * Line 451  void emul_machine_setup(struct machine *
451          if (m->userland_emul != NULL) {          if (m->userland_emul != NULL) {
452                  useremul_name_to_useremul(cpu,                  useremul_name_to_useremul(cpu,
453                      m->userland_emul, NULL, NULL, NULL);                      m->userland_emul, NULL, NULL, NULL);
454                  cpu->memory_rw = userland_memory_rw;  
455                    switch (m->arch) {
456    
457                    case ARCH_ALPHA:
458                            cpu->memory_rw = alpha_userland_memory_rw;
459                            break;
460    
461                    default:
462                            cpu->memory_rw = userland_memory_rw;
463                    }
464          }          }
465    
466          if (m->use_x11)          if (m->x11_md.in_use)
467                  x11_init(m);                  x11_init(m);
468    
469          /*  Fill memory with random bytes:  */          /*  Fill memory with random bytes:  */
         /*  TODO: This is MIPS-specific!  */  
470          if (m->random_mem_contents) {          if (m->random_mem_contents) {
471                  for (i=0; i<m->physical_ram_in_mb * 1048576; i+=256) {                  for (i=0; i<m->physical_ram_in_mb * 1048576; i+=256) {
472                          unsigned char data[256];                          unsigned char data[256];
473                          unsigned int j;                          unsigned int j;
474                          for (j=0; j<sizeof(data); j++)                          for (j=0; j<sizeof(data); j++)
475                                  data[j] = random() & 255;                                  data[j] = random() & 255;
476                          addr = 0xffffffff80000000ULL + i;                          cpu->memory_rw(cpu, m->memory, i, data, sizeof(data),
477                          cpu->memory_rw(cpu, m->memory, addr, data, sizeof(data),                              MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS | PHYSICAL);
                             MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS);  
478                  }                  }
479          }          }
480    
         if ((m->machine_type == MACHINE_ARC ||  
             m->machine_type == MACHINE_SGI) && m->prom_emulation)  
                 arcbios_init();  
   
481          if (m->userland_emul != NULL) {          if (m->userland_emul != NULL) {
482                  /*                  /*
483                   *  For userland-only emulation, no machine emulation                   *  For userland-only emulation, no machine emulation
# Line 567  void emul_machine_setup(struct machine * Line 491  void emul_machine_setup(struct machine *
491          }          }
492    
493          diskimage_dump_info(m);          diskimage_dump_info(m);
494            console_debug_dump(m);
495    
496          /*  Load files (ROM code, boot code, ...) into memory:  */          /*  Load files (ROM code, boot code, ...) into memory:  */
497          if (n_load == 0) {          if (n_load == 0) {
498                  if (m->first_diskimage != NULL)                  if (m->first_diskimage != NULL) {
499                          load_bootblock(m, cpu);                          if (!load_bootblock(m, cpu, &n_load, &load_names)) {
500                  else {                                  fprintf(stderr, "\nNo executable files were"
501                                        " specified, and booting directly from disk"
502                                        " failed.\n");
503                                    exit(1);
504                            }
505                    } else {
506                          fprintf(stderr, "No executable file(s) loaded, and "                          fprintf(stderr, "No executable file(s) loaded, and "
507                              "we are not booting directly from a disk image."                              "we are not booting directly from a disk image."
508                              "\nAborting.\n");                              "\nAborting.\n");
# Line 581  void emul_machine_setup(struct machine * Line 511  void emul_machine_setup(struct machine *
511          }          }
512    
513          while (n_load > 0) {          while (n_load > 0) {
514                    FILE *tmp_f;
515                    char *name_to_load = *load_names;
516                    int remove_after_load = 0;
517    
518                    /*  Special hack for removing temporary files:  */
519                    if (name_to_load[0] == 8) {
520                            name_to_load ++;
521                            remove_after_load = 1;
522                    }
523    
524                    /*
525                     *  gzipped files are automagically gunzipped:
526                     *  NOTE/TODO: This isn't secure. system() is used.
527                     */
528                    tmp_f = fopen(name_to_load, "r");
529                    if (tmp_f != NULL) {
530                            unsigned char buf[2];           /*  gzip header  */
531                            memset(buf, 0, sizeof(buf));
532                            fread(buf, 1, sizeof(buf), tmp_f);
533                            if (buf[0]==0x1f && buf[1]==0x8b) {
534                                    size_t zzlen = strlen(name_to_load)*2 + 100;
535                                    char *zz;
536    
537                                    CHECK_ALLOCATION(zz = malloc(zzlen));
538                                    debug("gunziping %s\n", name_to_load);
539    
540                                    /*
541                                     *  gzip header found.  If this was a file
542                                     *  extracted from, say, a CDROM image, then it
543                                     *  already has a temporary name. Otherwise we
544                                     *  have to gunzip into a temporary file.
545                                     */
546                                    if (remove_after_load) {
547                                            snprintf(zz, zzlen, "mv %s %s.gz",
548                                                name_to_load, name_to_load);
549                                            system(zz);
550                                            snprintf(zz, zzlen, "gunzip %s.gz",
551                                                name_to_load);
552                                            system(zz);
553                                    } else {
554                                            /*  gunzip into new temp file:  */
555                                            int tmpfile_handle;
556                                            char *new_temp_name;
557                                            CHECK_ALLOCATION(new_temp_name =
558                                                strdup("/tmp/gxemul.XXXXXXXXXXXX"));
559                                            tmpfile_handle = mkstemp(new_temp_name);
560                                            close(tmpfile_handle);
561                                            snprintf(zz, zzlen, "gunzip -c '%s' > "
562                                                "%s", name_to_load, new_temp_name);
563                                            system(zz);
564                                            name_to_load = new_temp_name;
565                                            remove_after_load = 1;
566                                    }
567                                    free(zz);
568                            }
569                            fclose(tmp_f);
570                    }
571    
572                  byte_order = NO_BYTE_ORDER_OVERRIDE;                  byte_order = NO_BYTE_ORDER_OVERRIDE;
573    
574                  file_load(m, m->memory, *load_names, &entrypoint,                  /*
575                     *  Load the file:  :-)
576                     */
577                    file_load(m, m->memory, name_to_load, &entrypoint,
578                      m->arch, &gp, &byte_order, &toc);                      m->arch, &gp, &byte_order, &toc);
579    
580                    if (remove_after_load) {
581                            debug("removing %s\n", name_to_load);
582                            unlink(name_to_load);
583                    }
584    
585                  if (byte_order != NO_BYTE_ORDER_OVERRIDE)                  if (byte_order != NO_BYTE_ORDER_OVERRIDE)
586                          cpu->byte_order = byte_order;                          cpu->byte_order = byte_order;
587    
588                  cpu->pc = entrypoint;                  cpu->pc = entrypoint;
589    
590                  switch (m->arch) {                  switch (m->arch) {
591    
592                    case ARCH_ALPHA:
593                            /*  For position-independent code:  */
594                            cpu->cd.alpha.r[ALPHA_T12] = cpu->pc;
595                            break;
596    
597                    case ARCH_ARM:
598                            if (cpu->pc & 3) {
599                                    fatal("ARM: lowest bits of pc set: TODO\n");
600                                    exit(1);
601                            }
602                            cpu->pc &= 0xfffffffc;
603                            break;
604    
605                    case ARCH_M88K:
606                            if (cpu->pc & 3) {
607                                    fatal("M88K: lowest bits of pc set: TODO\n");
608                                    exit(1);
609                            }
610                            cpu->pc &= 0xfffffffc;
611                            break;
612    
613                  case ARCH_MIPS:                  case ARCH_MIPS:
614                          if ((cpu->pc >> 32) == 0                          if ((cpu->pc >> 32) == 0 && (cpu->pc & 0x80000000ULL))
                             && (cpu->pc & 0x80000000ULL))  
615                                  cpu->pc |= 0xffffffff00000000ULL;                                  cpu->pc |= 0xffffffff00000000ULL;
616    
617                          cpu->cd.mips.gpr[MIPS_GPR_GP] = gp;                          cpu->cd.mips.gpr[MIPS_GPR_GP] = gp;
# Line 604  void emul_machine_setup(struct machine * Line 621  void emul_machine_setup(struct machine *
621                                  cpu->cd.mips.gpr[MIPS_GPR_GP] |=                                  cpu->cd.mips.gpr[MIPS_GPR_GP] |=
622                                      0xffffffff00000000ULL;                                      0xffffffff00000000ULL;
623                          break;                          break;
624    
625                  case ARCH_PPC:                  case ARCH_PPC:
626                            /*  See http://www.linuxbase.org/spec/ELF/ppc64/
627                                spec/x458.html for more info.  */
628                          cpu->cd.ppc.gpr[2] = toc;                          cpu->cd.ppc.gpr[2] = toc;
629                            /*  TODO  */
630                            if (cpu->cd.ppc.bits == 32)
631                                    cpu->pc &= 0xffffffffULL;
632                          break;                          break;
633                  case ARCH_SPARC:  
634                          break;                  case ARCH_SH:
635                  case ARCH_URISC:                          if (cpu->cd.sh.cpu_type.bits == 32)
636                          break;                                  cpu->pc &= 0xffffffffULL;
637                  case ARCH_HPPA:                          cpu->pc &= ~1;
638                          break;                          break;
639                  case ARCH_ALPHA:  
640                    case ARCH_SPARC:
641                          break;                          break;
642    
643                  default:                  default:
644                          fatal("emul_machine_setup(): Internal error: "                          fatal("emul_machine_setup(): Internal error: "
645                              "Unimplemented arch %i\n", m->arch);                              "Unimplemented arch %i\n", m->arch);
# Line 650  void emul_machine_setup(struct machine * Line 675  void emul_machine_setup(struct machine *
675                  useremul_setup(cpu, n_load, load_names);                  useremul_setup(cpu, n_load, load_names);
676    
677          /*  Startup the bootstrap CPU:  */          /*  Startup the bootstrap CPU:  */
678          cpu->bootstrap_cpu_flag = 1;          cpu->running = 1;
         cpu->running            = 1;  
679    
680          /*  ... or pause all CPUs, if start_paused is set:  */          /*  ... or pause all CPUs, if start_paused is set:  */
681          if (m->start_paused) {          if (m->start_paused) {
# Line 659  void emul_machine_setup(struct machine * Line 683  void emul_machine_setup(struct machine *
683                          m->cpus[i]->running = 0;                          m->cpus[i]->running = 0;
684          }          }
685    
686          /*  Add PC dump points:  */          /*  Parse and add breakpoints:  */
687          add_dump_points(m);          add_breakpoints(m);
688    
689          /*  TODO: This is MIPS-specific!  */          /*  TODO: This is MIPS-specific!  */
690          if (m->machine_type == MACHINE_DEC &&          if (m->machine_type == MACHINE_PMAX &&
691              cpu->cd.mips.cpu_type.mmu_model == MMU3K)              cpu->cd.mips.cpu_type.mmu_model == MMU3K)
692                  add_symbol_name(&m->symbol_context,                  add_symbol_name(&m->symbol_context,
693                      0x9fff0000, 0x10000, "r2k3k_cache", 0);                      0x9fff0000, 0x10000, "r2k3k_cache", 0, 0);
694    
695          symbol_recalc_sizes(&m->symbol_context);          symbol_recalc_sizes(&m->symbol_context);
696    
         if (m->max_random_cycles_per_chunk > 0)  
                 debug("using random cycle chunks (1 to %i cycles)\n",  
                     m->max_random_cycles_per_chunk);  
   
697          /*  Special hack for ARC/SGI emulation:  */          /*  Special hack for ARC/SGI emulation:  */
698          if ((m->machine_type == MACHINE_ARC ||          if ((m->machine_type == MACHINE_ARC ||
699              m->machine_type == MACHINE_SGI) && m->prom_emulation)              m->machine_type == MACHINE_SGI) && m->prom_emulation)
# Line 681  void emul_machine_setup(struct machine * Line 701  void emul_machine_setup(struct machine *
701    
702          debug("starting cpu%i at ", m->bootstrap_cpu);          debug("starting cpu%i at ", m->bootstrap_cpu);
703          switch (m->arch) {          switch (m->arch) {
704    
705            case ARCH_ARM:
706                    /*  ARM cpus aren't 64-bit:  */
707                    debug("0x%08"PRIx32, (uint32_t) entrypoint);
708                    break;
709    
710          case ARCH_MIPS:          case ARCH_MIPS:
711                  if (cpu->cd.mips.cpu_type.isa_level < 3 ||                  if (cpu->is_32bit) {
712                      cpu->cd.mips.cpu_type.isa_level == 32) {                          debug("0x%08"PRIx32, (uint32_t)
713                          debug("0x%08x", (int)m->cpus[                              m->cpus[m->bootstrap_cpu]->pc);
                             m->bootstrap_cpu]->pc);  
714                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)
715                                  debug(" (gp=0x%08x)", (int)m->cpus[                                  debug(" (gp=0x%08"PRIx32")", (uint32_t)
716                                      m->bootstrap_cpu]->cd.mips.gpr[                                      m->cpus[m->bootstrap_cpu]->cd.mips.gpr[
717                                      MIPS_GPR_GP]);                                      MIPS_GPR_GP]);
718                  } else {                  } else {
719                          debug("0x%016llx", (long long)m->cpus[                          debug("0x%016"PRIx64, (uint64_t)
720                              m->bootstrap_cpu]->pc);                              m->cpus[m->bootstrap_cpu]->pc);
721                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)
722                                  debug(" (gp=0x%016llx)", (long long)                                  debug(" (gp=0x%016"PRIx64")", (uint64_t)
723                                      cpu->cd.mips.gpr[MIPS_GPR_GP]);                                      cpu->cd.mips.gpr[MIPS_GPR_GP]);
724                  }                  }
725                  break;                  break;
726    
727          case ARCH_PPC:          case ARCH_PPC:
728                  if (cpu->cd.ppc.bits == 32)                  if (cpu->cd.ppc.bits == 32)
729                          debug("0x%08x", (int)entrypoint);                          debug("0x%08"PRIx32, (uint32_t) entrypoint);
730                  else                  else
731                          debug("0x%016llx", (long long)entrypoint);                          debug("0x%016"PRIx64, (uint64_t) entrypoint);
732                  break;                  break;
         case ARCH_URISC:  
                 {  
                         char tmps[100];  
                         unsigned char buf[sizeof(uint64_t)];  
   
                         cpu->memory_rw(cpu, m->memory, 0, buf, sizeof(buf),  
                             MEM_READ, CACHE_NONE | NO_EXCEPTIONS);  
   
                         entrypoint = 0;  
                         for (i=0; i<cpu->cd.urisc.wordlen/8; i++) {  
                                 entrypoint <<= 8;  
                                 if (cpu->byte_order == EMUL_BIG_ENDIAN)  
                                         entrypoint += buf[i];  
                                 else  
                                         entrypoint += buf[cpu->  
                                             cd.urisc.wordlen/8 - 1 - i];  
                         }  
733    
                         sprintf(tmps, "0x%%0%illx", cpu->cd.urisc.wordlen / 4);  
                         debug(tmps, (long long)entrypoint);  
                         cpu->pc = entrypoint;  
                 }  
                 break;  
734          default:          default:
735                  debug("0x%016llx", (long long)entrypoint);                  if (cpu->is_32bit)
736                            debug("0x%08"PRIx32, (uint32_t) cpu->pc);
737                    else
738                            debug("0x%016"PRIx64, (uint64_t) cpu->pc);
739          }          }
740          debug("\n");          debug("\n");
741    
# Line 743  void emul_machine_setup(struct machine * Line 750  void emul_machine_setup(struct machine *
750   */   */
751  void emul_dumpinfo(struct emul *e)  void emul_dumpinfo(struct emul *e)
752  {  {
753          int j, nm, iadd = 4;          int j, nm, iadd = DEBUG_INDENTATION;
754    
755          if (e->net != NULL)          if (e->net != NULL)
756                  net_dumpinfo(e->net);                  net_dumpinfo(e->net);
# Line 772  void emul_dumpinfo(struct emul *e) Line 779  void emul_dumpinfo(struct emul *e)
779   */   */
780  void emul_simple_init(struct emul *emul)  void emul_simple_init(struct emul *emul)
781  {  {
782          int iadd=4;          int iadd = DEBUG_INDENTATION;
783          struct machine *m;          struct machine *m;
784    
785          if (emul->n_machines != 1) {          if (emul->n_machines != 1) {
# Line 786  void emul_simple_init(struct emul *emul) Line 793  void emul_simple_init(struct emul *emul)
793                  debug("Simple setup...\n");                  debug("Simple setup...\n");
794                  debug_indentation(iadd);                  debug_indentation(iadd);
795    
796                  /*  Create a network:  */                  /*  Create a simple network:  */
797                  emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY,                  emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY,
798                      "10.0.0.0", 8);                      NET_DEFAULT_IPV4_MASK,
799                        NET_DEFAULT_IPV4_LEN,
800                        NULL, 0, 0, NULL);
801          } else {          } else {
802                  /*  Userland pseudo-machine:  */                  /*  Userland pseudo-machine:  */
803                  debug("Syscall emulation (userland-only) setup...\n");                  debug("Syscall emulation (userland-only) setup...\n");
# Line 807  void emul_simple_init(struct emul *emul) Line 816  void emul_simple_init(struct emul *emul)
816   *   *
817   *  Create an emul struct by reading settings from a configuration file.   *  Create an emul struct by reading settings from a configuration file.
818   */   */
819  struct emul *emul_create_from_configfile(char *fname)  struct emul *emul_create_from_configfile(char *fname, int id)
820  {  {
821          int iadd = 4;          int iadd = DEBUG_INDENTATION;
822          struct emul *e = emul_new(fname);          struct emul *e = emul_new(fname, id);
         FILE *f;  
         char buf[128];  
         size_t len;  
823    
824          debug("Creating emulation from configfile \"%s\":\n", fname);          debug("Creating emulation from configfile \"%s\":\n", fname);
825          debug_indentation(iadd);          debug_indentation(iadd);
826    
827          f = fopen(fname, "r");          emul_parse_config(e, fname);
         if (f == NULL) {  
                 perror(fname);  
                 exit(1);  
         }  
   
         /*  Read header: (must be !!gxemul)  */  
         len = fread(buf, 1, 8, f);  
         if (len != 8 || strncmp(buf, "!!gxemul", 8) != 0) {  
                 fprintf(stderr, "%s: must start with '!!gxemul'\n", fname);  
                 exit(1);  
         }  
   
         /*  Restart from beginning:  */  
         rewind(f);  
828    
         emul_parse_config(e, f);  
   
         fclose(f);  
829          debug_indentation(-iadd);          debug_indentation(-iadd);
830          return e;          return e;
831  }  }
# Line 863  void emul_run(struct emul **emuls, int n Line 852  void emul_run(struct emul **emuls, int n
852    
853          atexit(fix_console);          atexit(fix_console);
854    
         i = 79;  
         while (i-- > 0)  
                 debug("-");  
         debug("\n\n");  
   
855          /*  Initialize the interactive debugger:  */          /*  Initialize the interactive debugger:  */
856          debugger_init(emuls, n_emuls);          debugger_init(emuls, n_emuls);
857    
858            /*  Run any additional debugger commands before starting:  */
859            for (i=0; i<n_emuls; i++) {
860                    struct emul *emul = emuls[i];
861                    if (emul->n_debugger_cmds > 0) {
862                            int j;
863                            if (i == 0)
864                                    print_separator_line();
865                            for (j = 0; j < emul->n_debugger_cmds; j ++) {
866                                    debug("> %s\n", emul->debugger_cmds[j]);
867                                    debugger_execute_cmd(emul->debugger_cmds[j],
868                                        strlen(emul->debugger_cmds[j]));
869                            }
870                    }
871            }
872    
873            print_separator_line();
874            debug("\n");
875    
876    
877          /*          /*
878           *  console_init_main() makes sure that the terminal is in a           *  console_init_main() makes sure that the terminal is in a
879           *  reasonable state.           *  reasonable state.
# Line 895  void emul_run(struct emul **emuls, int n Line 898  void emul_run(struct emul **emuls, int n
898                  if (e == NULL)                  if (e == NULL)
899                          continue;                          continue;
900                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
901                          cpu_run_init(e, e->machines[j]);                          cpu_run_init(e->machines[j]);
902          }          }
903    
904            /*  TODO: Generalize:  */
905            if (emuls[0]->machines[0]->show_trace_tree)
906                    cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0],
907                        emuls[0]->machines[0]->cpus[0]->pc);
908    
909            /*  Start emulated clocks:  */
910            timer_start();
911    
912          /*          /*
913           *  MAIN LOOP:           *  MAIN LOOP:
914           *           *
915           *  Run all emulations in parallel, running each machine in           *  Run all emulations in parallel, running instructions from each
916           *  each emulation.           *  cpu in each machine in each emulation.
917           */           */
918          while (go) {          while (go) {
919                    struct cpu *bootcpu = emuls[0]->machines[0]->cpus[
920                        emuls[0]->machines[0]->bootstrap_cpu];
921    
922                  go = 0;                  go = 0;
923    
924                  x11_check_event(emuls, n_emuls);                  /*  Flush X11 and serial console output every now and then:  */
925                    if (bootcpu->ninstrs > bootcpu->ninstrs_flush + (1<<19)) {
926                            x11_check_event(emuls, n_emuls);
927                            console_flush();
928                            bootcpu->ninstrs_flush = bootcpu->ninstrs;
929                    }
930    
931                    if (bootcpu->ninstrs > bootcpu->ninstrs_show + (1<<25)) {
932                            bootcpu->ninstrs_since_gettimeofday +=
933                                (bootcpu->ninstrs - bootcpu->ninstrs_show);
934                            cpu_show_cycles(emuls[0]->machines[0], 0);
935                            bootcpu->ninstrs_show = bootcpu->ninstrs;
936                    }
937    
938                    if (single_step == ENTER_SINGLE_STEPPING) {
939                            /*  TODO: Cleanup!  */
940                            old_instruction_trace =
941                                emuls[0]->machines[0]->instruction_trace;
942                            old_quiet_mode = quiet_mode;
943                            old_show_trace_tree =
944                                emuls[0]->machines[0]->show_trace_tree;
945                            emuls[0]->machines[0]->instruction_trace = 1;
946                            emuls[0]->machines[0]->show_trace_tree = 1;
947                            quiet_mode = 0;
948                            single_step = SINGLE_STEPPING;
949                    }
950    
951                    if (single_step == SINGLE_STEPPING)
952                            debugger();
953    
954                  for (i=0; i<n_emuls; i++) {                  for (i=0; i<n_emuls; i++) {
955                          e = emuls[i];                          e = emuls[i];
                         if (e == NULL)  
                                 continue;  
956    
957                          for (j=0; j<e->n_machines; j++) {                          for (j=0; j<e->n_machines; j++) {
958                                  /*  TODO: cpu_run() is a strange name, since                                  anything = machine_run(e->machines[j]);
                                     there can be multiple cpus in a machine  */  
                                 anything = cpu_run(e, e->machines[j]);  
959                                  if (anything)                                  if (anything)
960                                          go = 1;                                          go = 1;
961                          }                          }
962                  }                  }
963          }          }
964    
965            /*  Stop any running timers:  */
966            timer_stop();
967    
968          /*  Deinitialize all CPUs in all machines in all emulations:  */          /*  Deinitialize all CPUs in all machines in all emulations:  */
969          for (i=0; i<n_emuls; i++) {          for (i=0; i<n_emuls; i++) {
970                  e = emuls[i];                  e = emuls[i];
971                  if (e == NULL)                  if (e == NULL)
972                          continue;                          continue;
973                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
974                          cpu_run_deinit(e, e->machines[j]);                          cpu_run_deinit(e->machines[j]);
975          }          }
976    
977          /*  force_debugger_at_exit flag set? Then enter the debugger:  */          /*  force_debugger_at_exit flag set? Then enter the debugger:  */
# Line 940  void emul_run(struct emul **emuls, int n Line 981  void emul_run(struct emul **emuls, int n
981                  debugger();                  debugger();
982          }          }
983    
984          /*  Any machine using X11? Then we should wait before exiting:  */          /*  Any machine using X11? Then wait before exiting:  */
985          n = 0;          n = 0;
986          for (i=0; i<n_emuls; i++)          for (i=0; i<n_emuls; i++)
987                  for (j=0; j<emuls[i]->n_machines; j++)                  for (j=0; j<emuls[i]->n_machines; j++)
988                          if (emuls[i]->machines[j]->use_x11)                          if (emuls[i]->machines[j]->x11_md.in_use)
989                                  n++;                                  n++;
990          if (n > 0) {          if (n > 0) {
991                  printf("Press enter to quit.\n");                  printf("Press enter to quit.\n");
992                  while (!console_charavail(MAIN_CONSOLE)) {                  while (!console_charavail(MAIN_CONSOLE)) {
993                          x11_check_event(emuls, n_emuls);                          x11_check_event(emuls, n_emuls);
994                          usleep(1);                          usleep(10000);
995                  }                  }
996                  console_readchar(MAIN_CONSOLE);                  console_readchar(MAIN_CONSOLE);
997          }          }
998    
999          console_deinit();          console_deinit_main();
1000  }  }
1001    

Legend:
Removed from v.2  
changed lines
  Added in v.42

  ViewVC Help
Powered by ViewVC 1.1.26