/[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 4 by dpavlin, Mon Oct 8 16:18:00 2007 UTC revision 12 by dpavlin, Mon Oct 8 16:18:38 2007 UTC
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: emul.c,v 1.184 2005/04/20 04:43:52 debug Exp $   *  $Id: emul.c,v 1.225 2005/08/14 19:35:54 debug Exp $
29   *   *
30   *  Emulation startup and misc. routines.   *  Emulation startup and misc. routines.
31   */   */
# Line 46  Line 46 
46  #include "debugger.h"  #include "debugger.h"
47  #include "device.h"  #include "device.h"
48  #include "diskimage.h"  #include "diskimage.h"
49    #include "exec_elf.h"
50  #include "machine.h"  #include "machine.h"
51  #include "memory.h"  #include "memory.h"
52  #include "mips_cpu_types.h"  #include "mips_cpu_types.h"
# Line 66  extern int quiet_mode; Line 67  extern int quiet_mode;
67  extern struct emul *debugger_emul;  extern struct emul *debugger_emul;
68  extern struct diskimage *diskimages[];  extern struct diskimage *diskimages[];
69    
70    static char *diskimage_types[] = DISKIMAGE_TYPES;
71    
72    
73  /*  /*
74   *  add_dump_points():   *  add_dump_points():
# Line 131  static void fix_console(void) Line 134  static void fix_console(void)
134    
135    
136  /*  /*
137     *  iso_load_bootblock():
138     *
139     *  Try to load a kernel from an ISO 9660 disk image. iso_type is 1 for
140     *  "CD001" (standard), 2 for "CDW01" (ECMA), and 3 for "CDROM" (Sierra).
141     *
142     *  TODO: This function uses too many magic offsets and so on; it should be
143     *  cleaned up some day.
144     *
145     *  Returns 1 on success, 0 on failure.
146     */
147    static int iso_load_bootblock(struct machine *m, struct cpu *cpu,
148            int disk_id, int disk_type, int iso_type, unsigned char *buf,
149            int *n_loadp, char ***load_namesp)
150    {
151            char str[35];
152            int filenr, i, ofs, dirlen, res = 0, res2, iadd = 4;
153            int found_dir;
154            uint64_t dirofs;
155            uint64_t fileofs, filelen;
156            unsigned char *dirbuf = NULL, *dp;
157            unsigned char *match_entry = NULL;
158            char *p, *filename_orig;
159            char *filename = strdup(cpu->machine->boot_kernel_filename);
160            unsigned char *filebuf = NULL;
161            char *tmpfilename = NULL;
162            char **new_array;
163            int tmpfile_handle;
164    
165            if (filename == NULL) {
166                    fatal("out of memory\n");
167                    exit(1);
168            }
169            filename_orig = filename;
170    
171            debug("ISO9660 boot:\n");
172            debug_indentation(iadd);
173    
174            /*  Volume ID:  */
175            ofs = iso_type == 3? 48 : 40;
176            memcpy(str, buf + ofs, sizeof(str));
177            str[32] = '\0';  i = 31;
178            while (i >= 0 && str[i]==' ')
179                    str[i--] = '\0';
180            if (str[0])
181                    debug("\"%s\"", str);
182            else {
183                    /*  System ID:  */
184                    ofs = iso_type == 3? 16 : 8;
185                    memcpy(str, buf + ofs, sizeof(str));
186                    str[32] = '\0';  i = 31;
187                    while (i >= 0 && str[i]==' ')
188                            str[i--] = '\0';
189                    if (str[0])
190                            debug("\"%s\"", str);
191                    else
192                            debug("(no ID)");
193            }
194    
195            debug(":%s\n", filename);
196    
197    
198            /*
199             *  Traverse the directory structure to find the kernel.
200             */
201    
202            dirlen = buf[0x84] + 256*buf[0x85] + 65536*buf[0x86];
203            if (dirlen != buf[0x8b] + 256*buf[0x8a] + 65536*buf[0x89])
204                    fatal("WARNING: Root directory length mismatch?\n");
205    
206            dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) +
207                (buf[0x8f] << 24)) * 2048;
208    
209            /*  debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs);  */
210    
211            dirbuf = malloc(dirlen);
212            if (dirbuf == NULL) {
213                    fatal("out of memory in iso_load_bootblock()\n");
214                    exit(1);
215            }
216    
217            res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, dirbuf,
218                dirlen);
219            if (!res2) {
220                    fatal("Couldn't read the disk image. Aborting.\n");
221                    goto ret;
222            }
223    
224            found_dir = 1;  /*  Assume root dir  */
225            dp = dirbuf; filenr = 1;
226            p = NULL;
227            while (dp < dirbuf + dirlen) {
228                    int i, nlen = dp[0];
229                    int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + (dp[5] << 24);
230                    int y = dp[6] + (dp[7] << 8);
231                    char direntry[65];
232    
233                    dp += 8;
234    
235                    /*
236                     *  As long as there is an \ or / in the filename, then we
237                     *  have not yet found the directory.
238                     */
239                    p = strchr(filename, '/');
240                    if (p == NULL)
241                            p = strchr(filename, '\\');
242    
243                    /*  debug("%i%s: %i, %i, \"", filenr, filenr == found_dir?
244                        " [CURRENT]" : "", x, y);  */
245                    for (i=0; i<nlen && i<sizeof(direntry)-1; i++)
246                            if (dp[i]) {
247                                    direntry[i] = dp[i];
248                                    /*  debug("%c", dp[i]);  */
249                            } else
250                                    break;
251                    /*  debug("\"\n");  */
252                    direntry[i] = '\0';
253    
254                    /*  A directory name match?  */
255                    if (p != NULL && strncasecmp(filename, direntry, nlen) == 0
256                        && nlen == (size_t)p - (size_t)filename && found_dir == y) {
257                            found_dir = filenr;
258                            filename = p+1;
259                            dirofs = 2048 * (int64_t)x;
260                    }
261    
262                    dp += nlen;
263    
264                    /*  16-bit aligned lenght:  */
265                    if (nlen & 1)
266                            dp ++;
267    
268                    filenr ++;
269            }
270    
271            p = strchr(filename, '/');
272            if (p == NULL)
273                    p = strchr(filename, '\\');
274    
275            if (p != NULL) {
276                    char *blah = filename_orig;
277    
278                    fatal("could not find '%s' in /", filename);
279    
280                    /*  Print the first part of the filename:  */
281                    while (blah != filename)
282                            fatal("%c", *blah++);
283                    
284                    fatal("\n");
285                    goto ret;
286            }
287    
288            /*  debug("dirofs = 0x%llx\n", (long long)dirofs);  */
289    
290            /*  Free the old dirbuf, and allocate a new one:  */
291            free(dirbuf);
292            dirbuf = malloc(512);
293            if (dirbuf == NULL) {
294                    fatal("out of memory in iso_load_bootblock()\n");
295                    exit(1);
296            }
297    
298            for (;;) {
299                    int len, i;
300    
301                    /*  Too close to another sector? Then realign.  */
302                    if ((dirofs & 2047) + 70 > 2047) {
303                            dirofs = (dirofs | 2047) + 1;
304                            /*  debug("realign dirofs = 0x%llx\n", dirofs);  */
305                    }
306    
307                    res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs,
308                        dirbuf, 256);
309                    if (!res2) {
310                            fatal("Couldn't read the disk image. Aborting.\n");
311                            goto ret;
312                    }
313    
314                    dp = dirbuf;
315                    len = dp[0];
316                    if (len < 2)
317                            break;
318    
319                    /*
320                     *  TODO: Actually parse the directory entry!
321                     *
322                     *  Haha, this must be rewritten.
323                     */
324                    for (i=32; i<len; i++) {
325                            if (i < len - strlen(filename))
326                                    if (strncasecmp(filename, (char *)dp + i,
327                                        strlen(filename)) == 0) {
328                                            /*  The filename was found somewhere
329                                                in the directory entry.  */
330                                            if (match_entry != NULL) {
331                                                    fatal("TODO: I'm too lazy to"
332                                                        " implement a correct "
333                                                        "directory parser right "
334                                                        "now... (BUG)\n");
335                                                    exit(1);
336                                            }
337                                            match_entry = malloc(512);
338                                            if (match_entry == NULL) {
339                                                    fatal("out of memory\n");
340                                                    exit(1);
341                                            }
342                                            memcpy(match_entry, dp, 512);
343                                            break;
344                                    }
345                    }
346    
347                    dirofs += len;
348            }
349    
350            if (match_entry == NULL) {
351                    char *blah = filename_orig;
352    
353                    fatal("could not find '%s' in /", filename);
354    
355                    /*  Print the first part of the filename:  */
356                    while (blah != filename)
357                            fatal("%c", *blah++);
358                    
359                    fatal("\n");
360                    goto ret;
361            }
362    
363            fileofs = match_entry[2] + (match_entry[3] << 8) +
364                (match_entry[4] << 16) + (match_entry[5] << 24);
365            filelen = match_entry[10] + (match_entry[11] << 8) +
366                (match_entry[12] << 16) + (match_entry[13] << 24);
367            fileofs *= 2048;
368    
369            /*  debug("filelen=%llx fileofs=%llx\n", (long long)filelen,
370                (long long)fileofs);  */
371    
372            filebuf = malloc(filelen);
373            if (filebuf == NULL) {
374                    fatal("could not allocate %lli bytes to read the file"
375                        " from the disk image!\n", (long long)filelen);
376                    goto ret;
377            }
378    
379            tmpfilename = strdup("/tmp/gxemul.XXXXXXXXXXXX");
380    
381            debug("extracting %lli bytes into %s\n",
382                (long long)filelen, tmpfilename);
383    
384            res2 = diskimage_access(m, disk_id, disk_type, 0, fileofs, filebuf,
385                filelen);
386            if (!res2) {
387                    fatal("could not read the file from the disk image!\n");
388                    goto ret;
389            }
390    
391            tmpfile_handle = mkstemp(tmpfilename);
392            if (tmpfile_handle < 0) {
393                    fatal("could not create %s\n", tmpfilename);
394                    exit(1);
395            }
396            write(tmpfile_handle, filebuf, filelen);
397            close(tmpfile_handle);
398    
399            /*  Add the temporary filename to the load_namesp array:  */
400            (*n_loadp)++;
401            new_array = malloc(sizeof(char *) * (*n_loadp));
402            if (new_array == NULL) {
403                    fatal("out of memory\n");
404                    exit(1);
405            }
406            memcpy(new_array, *load_namesp, sizeof(char *) * (*n_loadp));
407            *load_namesp = new_array;
408    
409            /*  This adds a Backspace char in front of the filename; this
410                is a special hack which causes the file to be removed once
411                it has been loaded.  */
412            tmpfilename = realloc(tmpfilename, strlen(tmpfilename) + 2);
413            memmove(tmpfilename + 1, tmpfilename, strlen(tmpfilename) + 1);
414            tmpfilename[0] = 8;
415    
416            (*load_namesp)[*n_loadp - 1] = tmpfilename;
417    
418            res = 1;
419    
420    ret:
421            if (dirbuf != NULL)
422                    free(dirbuf);
423    
424            if (filebuf != NULL)
425                    free(filebuf);
426    
427            if (match_entry != NULL)
428                    free(match_entry);
429    
430            free(filename_orig);
431    
432            debug_indentation(-iadd);
433            return res;
434    }
435    
436    
437    /*
438   *  load_bootblock():   *  load_bootblock():
439   *   *
440   *  For some emulation modes, it is possible to boot from a harddisk image by   *  For some emulation modes, it is possible to boot from a harddisk image by
441   *  loading a bootblock from a specific disk offset into memory, and executing   *  loading a bootblock from a specific disk offset into memory, and executing
442   *  that, instead of requiring a separate kernel file.  It is then up to the   *  that, instead of requiring a separate kernel file.  It is then up to the
443   *  bootblock to load a kernel.   *  bootblock to load a kernel.
444     *
445     *  Returns 1 on success, 0 on failure.
446   */   */
447  static void load_bootblock(struct machine *m, struct cpu *cpu)  static int load_bootblock(struct machine *m, struct cpu *cpu,
448            int *n_loadp, char ***load_namesp)
449  {  {
450          int boot_disk_id;          int boot_disk_id, boot_disk_type = 0, n_blocks, res, readofs,
451                iso_type, retval = 0;
452          unsigned char minibuf[0x20];          unsigned char minibuf[0x20];
453          unsigned char *bootblock_buf;          unsigned char *bootblock_buf;
454          uint64_t bootblock_offset;          uint64_t bootblock_offset;
455          uint64_t bootblock_loadaddr, bootblock_pc;          uint64_t bootblock_loadaddr, bootblock_pc;
         int n_blocks, res, readofs;  
456    
457          boot_disk_id = diskimage_bootdev(m);          boot_disk_id = diskimage_bootdev(m, &boot_disk_type);
458          if (boot_disk_id < 0)          if (boot_disk_id < 0)
459                  return;                  return 0;
460    
461          switch (m->machine_type) {          switch (m->machine_type) {
462          case MACHINE_DEC:          case MACHINE_DEC:
# Line 169  static void load_bootblock(struct machin Line 476  static void load_bootblock(struct machin
476                   *  nr of blocks to read and offset are repeated until nr of                   *  nr of blocks to read and offset are repeated until nr of
477                   *  blocks to read is zero.                   *  blocks to read is zero.
478                   */                   */
479                  res = diskimage_access(m, boot_disk_id, 0, 0,                  res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0,
480                      minibuf, sizeof(minibuf));                      minibuf, sizeof(minibuf));
481    
482                  bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8)                  bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8)
# Line 196  static void load_bootblock(struct machin Line 503  static void load_bootblock(struct machin
503                  readofs = 0x18;                  readofs = 0x18;
504    
505                  for (;;) {                  for (;;) {
506                          res = diskimage_access(m, boot_disk_id, 0, readofs,                          res = diskimage_access(m, boot_disk_id, boot_disk_type,
507                              minibuf, sizeof(minibuf));                              0, readofs, minibuf, sizeof(minibuf));
508                          if (!res) {                          if (!res) {
509                                  printf("couldn't read disk?\n");                                  fatal("Couldn't read the disk image. "
510                                  exit(1);                                      "Aborting.\n");
511                                    return 0;
512                          }                          }
513    
514                          n_blocks = minibuf[0] + (minibuf[1] << 8)                          n_blocks = minibuf[0] + (minibuf[1] << 8)
# Line 225  static void load_bootblock(struct machin Line 533  static void load_bootblock(struct machin
533                                  exit(1);                                  exit(1);
534                          }                          }
535    
536                          res = diskimage_access(m, boot_disk_id, 0,                          res = diskimage_access(m, boot_disk_id, boot_disk_type,
537                              bootblock_offset, bootblock_buf, n_blocks * 512);                              0, bootblock_offset, bootblock_buf, n_blocks * 512);
538                          if (!res) {                          if (!res) {
539                                  fatal("WARNING: could not load bootblocks from"                                  fatal("WARNING: could not load bootblocks from"
540                                      " disk offset 0x%llx\n",                                      " disk offset 0x%llx\n",
# Line 242  static void load_bootblock(struct machin Line 550  static void load_bootblock(struct machin
550                  }                  }
551    
552                  debug(readofs == 0x18? ": no blocks?\n" : " blocks\n");                  debug(readofs == 0x18? ": no blocks?\n" : " blocks\n");
553                  break;                  return 1;
554    
555          case MACHINE_X86:          case MACHINE_X86:
556                  cpu->cd.x86.mode = 16;                  /*  TODO: "El Torito" etc?  */
557                  cpu->pc = 0x7c00;                  if (diskimage_is_a_cdrom(cpu->machine, boot_disk_id,
558                        boot_disk_type))
559                            break;
560    
561                  bootblock_buf = malloc(512);                  bootblock_buf = malloc(512);
562                  if (bootblock_buf == NULL) {                  if (bootblock_buf == NULL) {
# Line 254  static void load_bootblock(struct machin Line 564  static void load_bootblock(struct machin
564                          exit(1);                          exit(1);
565                  }                  }
566    
567                  res = diskimage_access(m, boot_disk_id, 0, 0,                  debug("loading PC bootsector from %s id %i\n",
568                        diskimage_types[boot_disk_type], boot_disk_id);
569    
570                    res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0,
571                      bootblock_buf, 512);                      bootblock_buf, 512);
572                  if (!res) {                  if (!res) {
573                          printf("Couldn't read the disk image. Aborting.\n");                          fatal("Couldn't read the disk image. Aborting.\n");
574                          exit(1);                          return 0;
575                  }                  }
576    
                 debug("loading PC bootsector from disk %i\n", boot_disk_id);  
577                  if (bootblock_buf[510] != 0x55 || bootblock_buf[511] != 0xaa)                  if (bootblock_buf[510] != 0x55 || bootblock_buf[511] != 0xaa)
578                          debug("WARNING! The 0x55,0xAA marker is missing! "                          debug("WARNING! The 0x55,0xAA marker is missing! "
579                              "Booting anyway.\n");                              "Booting anyway.\n");
580                  store_buf(cpu, 0x7c00, (char *)bootblock_buf, 512);                  store_buf(cpu, 0x7c00, (char *)bootblock_buf, 512);
581                  free(bootblock_buf);                  free(bootblock_buf);
                 break;  
582    
583          default:                  return 1;
584                  fatal("Booting from disk without a separate kernel "          }
585                      "doesn't work in this emulation mode.\n");  
586    
587            /*
588             *  Try reading a kernel manually from the disk. The code here
589             *  does not rely on machine-dependant boot blocks etc.
590             */
591            /*  ISO9660: (0x800 bytes at 0x8000)  */
592            bootblock_buf = malloc(0x800);
593            if (bootblock_buf == NULL) {
594                    fprintf(stderr, "Out of memory.\n");
595                  exit(1);                  exit(1);
596          }          }
597    
598            res = diskimage_access(m, boot_disk_id, boot_disk_type,
599                0, 0x8000, bootblock_buf, 0x800);
600            if (!res) {
601                    fatal("Couldn't read the disk image. Aborting.\n");
602                    return 0;
603            }
604    
605            iso_type = 0;
606            if (strncmp((char *)bootblock_buf+1, "CD001", 5) == 0)
607                    iso_type = 1;
608            if (strncmp((char *)bootblock_buf+1, "CDW01", 5) == 0)
609                    iso_type = 2;
610            if (strncmp((char *)bootblock_buf+1, "CDROM", 5) == 0)
611                    iso_type = 3;
612    
613            if (iso_type != 0) {
614                    /*  We can't load a kernel if the name
615                        isn't specified.  */
616                    if (cpu->machine->boot_kernel_filename == NULL ||
617                        cpu->machine->boot_kernel_filename[0] == '\0')
618                            fatal("\nISO9660 filesystem, but no kernel "
619                                "specified? (Use the -j option.)\n");
620                    else
621                            retval = iso_load_bootblock(m, cpu, boot_disk_id,
622                                boot_disk_type, iso_type, bootblock_buf,
623                                n_loadp, load_namesp);
624            }
625    
626            free(bootblock_buf);
627            return retval;
628  }  }
629    
630    
# Line 295  struct emul *emul_new(char *name) Line 646  struct emul *emul_new(char *name)
646    
647          /*  Sane default values:  */          /*  Sane default values:  */
648          e->n_machines = 0;          e->n_machines = 0;
649            e->next_serial_nr = 1;
650    
651          if (name != NULL) {          if (name != NULL) {
652                  e->name = strdup(name);                  e->name = strdup(name);
# Line 357  static void add_arc_components(struct ma Line 709  static void add_arc_components(struct ma
709    
710          len += 1048576 * m->memory_offset_in_mb;          len += 1048576 * m->memory_offset_in_mb;
711    
712          /*  NOTE/TODO: magic 12MB end of load program area  */          /*
713             *  NOTE/TODO: magic 12MB end of load program area
714             *
715             *  Hm. This breaks the old FreeBSD/MIPS snapshots...
716             */
717    #if 0
718          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
719              0x60000 + m->memory_offset_in_mb * 1048576,              0x60000 + m->memory_offset_in_mb * 1048576,
720              start-0x60000 - m->memory_offset_in_mb * 1048576,              start-0x60000 - m->memory_offset_in_mb * 1048576,
721              ARCBIOS_MEM_FreeMemory);              ARCBIOS_MEM_FreeMemory);
722    #endif
723          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
724              start, len, ARCBIOS_MEM_LoadedProgram);              start, len, ARCBIOS_MEM_LoadedProgram);
725    
726          scsicontroller = arcbios_get_scsicontroller();          scsicontroller = arcbios_get_scsicontroller(m);
727          if (scsicontroller == 0)          if (scsicontroller == 0)
728                  return;                  return;
729    
# Line 413  static void add_arc_components(struct ma Line 771  static void add_arc_components(struct ma
771                                  snprintf(component_string,                                  snprintf(component_string,
772                                      sizeof(component_string),                                      sizeof(component_string),
773                                      "scsi(0)cdrom(%i)", d->id);                                      "scsi(0)cdrom(%i)", d->id);
774                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
775                                      component_string, scsidevice);                                      component_string, scsidevice);
776    
777                                  snprintf(component_string,                                  snprintf(component_string,
778                                      sizeof(component_string),                                      sizeof(component_string),
779                                      "scsi(0)cdrom(%i)fdisk(0)", d->id);                                      "scsi(0)cdrom(%i)fdisk(0)", d->id);
780                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
781                                      component_string, scsidisk);                                      component_string, scsidisk);
782                          } else {                          } else {
783                                  snprintf(component_string,                                  snprintf(component_string,
784                                      sizeof(component_string),                                      sizeof(component_string),
785                                      "scsi(0)disk(%i)", d->id);                                      "scsi(0)disk(%i)", d->id);
786                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
787                                      component_string, scsidevice);                                      component_string, scsidevice);
788    
789                                  snprintf(component_string,                                  snprintf(component_string,
790                                      sizeof(component_string),                                      sizeof(component_string),
791                                      "scsi(0)disk(%i)rdisk(0)", d->id);                                      "scsi(0)disk(%i)rdisk(0)", d->id);
792                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
793                                      component_string, scsidisk);                                      component_string, scsidisk);
794                          }                          }
795                  }                  }
# Line 457  void emul_machine_setup(struct machine * Line 815  void emul_machine_setup(struct machine *
815          struct emul *emul;          struct emul *emul;
816          struct cpu *cpu;          struct cpu *cpu;
817          int i, iadd=4;          int i, iadd=4;
818          uint64_t addr, memory_amount, entrypoint = 0, gp = 0, toc = 0;          uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0;
819          int byte_order;          int byte_order;
820    
821          emul = m->emul;          emul = m->emul;
# Line 482  void emul_machine_setup(struct machine * Line 840  void emul_machine_setup(struct machine *
840    
841          m->cpu_family = cpu_family_ptr_by_number(m->arch);          m->cpu_family = cpu_family_ptr_by_number(m->arch);
842    
843            if (m->arch == ARCH_ALPHA)
844                    m->arch_pagesize = 8192;
845    
846            if (m->arch != ARCH_MIPS)
847                    m->bintrans_enable = 0;
848    
849          machine_memsize_fix(m);          machine_memsize_fix(m);
850    
851          /*          /*
# Line 502  void emul_machine_setup(struct machine * Line 866  void emul_machine_setup(struct machine *
866                  debug(" (offset by %iMB)", m->memory_offset_in_mb);                  debug(" (offset by %iMB)", m->memory_offset_in_mb);
867                  memory_amount += 1048576 * m->memory_offset_in_mb;                  memory_amount += 1048576 * m->memory_offset_in_mb;
868          }          }
869          m->memory = memory_new(memory_amount);          m->memory = memory_new(memory_amount, m->arch);
870          if (m->machine_type != MACHINE_USERLAND)          if (m->machine_type != MACHINE_USERLAND)
871                  debug("\n");                  debug("\n");
872    
# Line 544  void emul_machine_setup(struct machine * Line 908  void emul_machine_setup(struct machine *
908          }          }
909          debug("\n");          debug("\n");
910    
911    #if 0
912            /*  Special case: The Playstation Portable has an additional CPU:  */
913            if (m->machine_type == MACHINE_PSP) {
914                    debug("cpu%i: ", m->ncpus);
915                    m->cpus[m->ncpus] = cpu_new(m->memory, m,
916                        0  /*  use 0 here to show info with debug()  */,
917                        "Allegrex" /*  TODO  */);
918                    if (m->bintrans_enable)
919                            bintrans_init_cpu(m->cpus[m->ncpus]);
920                    debug("\n");
921                    m->ncpus ++;
922            }
923    #endif
924    
925          if (m->use_random_bootstrap_cpu)          if (m->use_random_bootstrap_cpu)
926                  m->bootstrap_cpu = random() % m->ncpus;                  m->bootstrap_cpu = random() % m->ncpus;
927          else          else
# Line 555  void emul_machine_setup(struct machine * Line 933  void emul_machine_setup(struct machine *
933          if (m->userland_emul != NULL) {          if (m->userland_emul != NULL) {
934                  useremul_name_to_useremul(cpu,                  useremul_name_to_useremul(cpu,
935                      m->userland_emul, NULL, NULL, NULL);                      m->userland_emul, NULL, NULL, NULL);
936                  cpu->memory_rw = userland_memory_rw;  
937                    switch (m->arch) {
938    #ifdef ENABLE_ALPHA
939                    case ARCH_ALPHA:
940                            cpu->memory_rw = alpha_userland_memory_rw;
941                            break;
942    #endif
943                    default:cpu->memory_rw = userland_memory_rw;
944                    }
945          }          }
946    
947          if (m->use_x11)          if (m->use_x11)
948                  x11_init(m);                  x11_init(m);
949    
950          /*  Fill memory with random bytes:  */          /*  Fill memory with random bytes:  */
         /*  TODO: This is MIPS-specific!  */  
951          if (m->random_mem_contents) {          if (m->random_mem_contents) {
952                  for (i=0; i<m->physical_ram_in_mb * 1048576; i+=256) {                  for (i=0; i<m->physical_ram_in_mb * 1048576; i+=256) {
953                          unsigned char data[256];                          unsigned char data[256];
954                          unsigned int j;                          unsigned int j;
955                          for (j=0; j<sizeof(data); j++)                          for (j=0; j<sizeof(data); j++)
956                                  data[j] = random() & 255;                                  data[j] = random() & 255;
957                          addr = 0xffffffff80000000ULL + i;                          cpu->memory_rw(cpu, m->memory, i, data, sizeof(data),
958                          cpu->memory_rw(cpu, m->memory, addr, data, sizeof(data),                              MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS | PHYSICAL);
                             MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS);  
959                  }                  }
960          }          }
961    
         if ((m->machine_type == MACHINE_ARC ||  
             m->machine_type == MACHINE_SGI) && m->prom_emulation)  
                 arcbios_init();  
   
962          if (m->userland_emul != NULL) {          if (m->userland_emul != NULL) {
963                  /*                  /*
964                   *  For userland-only emulation, no machine emulation                   *  For userland-only emulation, no machine emulation
# Line 595  void emul_machine_setup(struct machine * Line 975  void emul_machine_setup(struct machine *
975    
976          /*  Load files (ROM code, boot code, ...) into memory:  */          /*  Load files (ROM code, boot code, ...) into memory:  */
977          if (n_load == 0) {          if (n_load == 0) {
978                  if (m->first_diskimage != NULL)                  if (m->first_diskimage != NULL) {
979                          load_bootblock(m, cpu);                          if (!load_bootblock(m, cpu, &n_load, &load_names)) {
980                  else {                                  fprintf(stderr, "\nNo executable files were"
981                                        " specified, and booting directly from disk"
982                                        " failed.\n");
983                                    exit(1);
984                            }
985                    } else {
986                          fprintf(stderr, "No executable file(s) loaded, and "                          fprintf(stderr, "No executable file(s) loaded, and "
987                              "we are not booting directly from a disk image."                              "we are not booting directly from a disk image."
988                              "\nAborting.\n");                              "\nAborting.\n");
# Line 606  void emul_machine_setup(struct machine * Line 991  void emul_machine_setup(struct machine *
991          }          }
992    
993          while (n_load > 0) {          while (n_load > 0) {
994                    FILE *tmp_f;
995                    char *name_to_load = *load_names;
996                    int remove_after_load = 0;
997    
998                    /*  Special hack for removing temporary files:  */
999                    if (name_to_load[0] == 8) {
1000                            name_to_load ++;
1001                            remove_after_load = 1;
1002                    }
1003    
1004                    /*
1005                     *  gzipped files are automagically gunzipped:
1006                     *  NOTE/TODO: This isn't secure. system() is used.
1007                     */
1008                    tmp_f = fopen(name_to_load, "r");
1009                    if (tmp_f != NULL) {
1010                            unsigned char buf[2];           /*  gzip header  */
1011                            memset(buf, 0, sizeof(buf));
1012                            fread(buf, 1, sizeof(buf), tmp_f);
1013                            if (buf[0]==0x1f && buf[1]==0x8b) {
1014                                    size_t zzlen = strlen(name_to_load)*2 + 100;
1015                                    char *zz = malloc(zzlen);
1016                                    debug("gunziping %s\n", name_to_load);
1017                                    /*
1018                                     *  gzip header found.  If this was a file
1019                                     *  extracted from, say, a CDROM image, then it
1020                                     *  already has a temporary name. Otherwise we
1021                                     *  have to gunzip into a temporary file.
1022                                     */
1023                                    if (remove_after_load) {
1024                                            snprintf(zz, zzlen, "mv %s %s.gz",
1025                                                name_to_load, name_to_load);
1026                                            system(zz);
1027                                            snprintf(zz, zzlen, "gunzip %s.gz",
1028                                                name_to_load);
1029                                            system(zz);
1030                                    } else {
1031                                            /*  gunzip into new temp file:  */
1032                                            int tmpfile_handle;
1033                                            char *new_temp_name =
1034                                                strdup("/tmp/gxemul.XXXXXXXXXXXX");
1035                                            tmpfile_handle = mkstemp(new_temp_name);
1036                                            close(tmpfile_handle);
1037                                            snprintf(zz, zzlen, "gunzip -c '%s' > "
1038                                                "%s", name_to_load, new_temp_name);
1039                                            system(zz);
1040                                            name_to_load = new_temp_name;
1041                                            remove_after_load = 1;
1042                                    }
1043                                    free(zz);
1044                            }
1045                            fclose(tmp_f);
1046                    }
1047    
1048                    /*
1049                     *  Ugly (but usable) hack for Playstation Portable:  If the
1050                     *  filename ends with ".pbp" and the file contains an ELF
1051                     *  header, then extract the ELF file into a temporary file.
1052                     */
1053                    if (strlen(name_to_load) > 4 && strcasecmp(name_to_load +
1054                        strlen(name_to_load) - 4, ".pbp") == 0 &&
1055                        (tmp_f = fopen(name_to_load, "r")) != NULL) {
1056                            off_t filesize, j, found=0;
1057                            unsigned char *buf;
1058                            fseek(tmp_f, 0, SEEK_END);
1059                            filesize = ftello(tmp_f);
1060                            fseek(tmp_f, 0, SEEK_SET);
1061                            buf = malloc(filesize);
1062                            if (buf == NULL) {
1063                                    fprintf(stderr, "out of memory while trying"
1064                                        " to read %s\n", name_to_load);
1065                                    exit(1);
1066                            }
1067                            fread(buf, 1, filesize, tmp_f);
1068                            fclose(tmp_f);
1069                            /*  Search for the ELF header, from offset 1 (!):  */
1070                            for (j=1; j<filesize - 4; j++)
1071                                    if (memcmp(buf + j, ELFMAG, SELFMAG) == 0) {
1072                                            found = j;
1073                                            break;
1074                                    }
1075                            if (found != 0) {
1076                                    int tmpfile_handle;
1077                                    char *new_temp_name =
1078                                        strdup("/tmp/gxemul.XXXXXXXXXXXX");
1079                                    debug("extracting ELF from %s (offset 0x%x)\n",
1080                                        name_to_load, (int)found);
1081                                    tmpfile_handle = mkstemp(new_temp_name);
1082                                    write(tmpfile_handle, buf + found,
1083                                        filesize - found);
1084                                    close(tmpfile_handle);
1085                                    name_to_load = new_temp_name;
1086                                    remove_after_load = 1;
1087                            }
1088                    }
1089    
1090                    /*  Special things required _before_ loading the file:  */
1091                    switch (m->arch) {
1092                    case ARCH_X86:
1093                            /*
1094                             *  X86 machines normally don't need to load any files,
1095                             *  they can boot from disk directly. Therefore, an x86
1096                             *  machine usually boots up in 16-bit real mode. When
1097                             *  loading a 32-bit (or even 64-bit) ELF, that's not
1098                             *  very nice, hence this special case.
1099                             */
1100                            pc_bios_simple_pmode_setup(cpu);
1101                            break;
1102                    }
1103    
1104                  byte_order = NO_BYTE_ORDER_OVERRIDE;                  byte_order = NO_BYTE_ORDER_OVERRIDE;
1105    
1106                  file_load(m, m->memory, *load_names, &entrypoint,                  /*
1107                     *  Load the file:  :-)
1108                     */
1109                    file_load(m, m->memory, name_to_load, &entrypoint,
1110                      m->arch, &gp, &byte_order, &toc);                      m->arch, &gp, &byte_order, &toc);
1111    
1112                    if (remove_after_load) {
1113                            debug("removing %s\n", name_to_load);
1114                            unlink(name_to_load);
1115                    }
1116    
1117                  if (byte_order != NO_BYTE_ORDER_OVERRIDE)                  if (byte_order != NO_BYTE_ORDER_OVERRIDE)
1118                          cpu->byte_order = byte_order;                          cpu->byte_order = byte_order;
1119    
# Line 631  void emul_machine_setup(struct machine * Line 1134  void emul_machine_setup(struct machine *
1134                          break;                          break;
1135    
1136                  case ARCH_PPC:                  case ARCH_PPC:
1137                            /*  See http://www.linuxbase.org/spec/ELF/ppc64/
1138                                spec/x458.html for more info.  */
1139                          cpu->cd.ppc.gpr[2] = toc;                          cpu->cd.ppc.gpr[2] = toc;
1140                            /*  TODO  */
1141                          break;                          break;
1142    
1143                  case ARCH_ALPHA:                  case ARCH_ALPHA:
1144                  case ARCH_HPPA:                          /*  For position-independant code:  */
1145                            cpu->cd.alpha.r[ALPHA_T12] = cpu->pc;
1146                            break;
1147    
1148                  case ARCH_SPARC:                  case ARCH_SPARC:
1149                  case ARCH_URISC:                          break;
1150    
1151                    case ARCH_IA64:
1152                            break;
1153    
1154                    case ARCH_M68K:
1155                            break;
1156    
1157                    case ARCH_ARM:
1158                            cpu->pc &= 0xfffffffc;
1159                            cpu->cd.arm.r[ARM_PC] = cpu->pc;
1160                          break;                          break;
1161    
1162                  case ARCH_X86:                  case ARCH_X86:
1163                          /*                          /*
1164                           *  NOTE: The toc field is used to indicate an ELF64                           *  NOTE: The toc field is used to indicate an ELF32
1165                           *  load, on AMD64!                           *  or ELF64 load.
1166                           */                           */
1167                          if (toc != 0) {                          switch (toc) {
1168                                  cpu->cd.x86.mode = 64;                          case 0: /*  16-bit? TODO  */
                         } else  
1169                                  cpu->pc &= 0xffffffffULL;                                  cpu->pc &= 0xffffffffULL;
1170                                    break;
1171                            case 1: /*  32-bit.  */
1172                                    cpu->pc &= 0xffffffffULL;
1173                                    break;
1174                            case 2: /*  64-bit:  TODO  */
1175                                    fatal("64-bit x86 load. TODO\n");
1176                                    exit(1);
1177                            }
1178                          break;                          break;
1179    
1180                  default:                  default:
# Line 702  void emul_machine_setup(struct machine * Line 1228  void emul_machine_setup(struct machine *
1228          if (m->machine_type == MACHINE_DEC &&          if (m->machine_type == MACHINE_DEC &&
1229              cpu->cd.mips.cpu_type.mmu_model == MMU3K)              cpu->cd.mips.cpu_type.mmu_model == MMU3K)
1230                  add_symbol_name(&m->symbol_context,                  add_symbol_name(&m->symbol_context,
1231                      0x9fff0000, 0x10000, "r2k3k_cache", 0);                      0x9fff0000, 0x10000, "r2k3k_cache", 0, 0);
1232    
1233          symbol_recalc_sizes(&m->symbol_context);          symbol_recalc_sizes(&m->symbol_context);
1234    
# Line 718  void emul_machine_setup(struct machine * Line 1244  void emul_machine_setup(struct machine *
1244          debug("starting cpu%i at ", m->bootstrap_cpu);          debug("starting cpu%i at ", m->bootstrap_cpu);
1245          switch (m->arch) {          switch (m->arch) {
1246          case ARCH_MIPS:          case ARCH_MIPS:
1247                  if (cpu->cd.mips.cpu_type.isa_level < 3 ||                  if (cpu->is_32bit) {
                     cpu->cd.mips.cpu_type.isa_level == 32) {  
1248                          debug("0x%08x", (int)m->cpus[                          debug("0x%08x", (int)m->cpus[
1249                              m->bootstrap_cpu]->pc);                              m->bootstrap_cpu]->pc);
1250                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)
# Line 740  void emul_machine_setup(struct machine * Line 1265  void emul_machine_setup(struct machine *
1265                  else                  else
1266                          debug("0x%016llx", (long long)entrypoint);                          debug("0x%016llx", (long long)entrypoint);
1267                  break;                  break;
1268          case ARCH_URISC:          case ARCH_ARM:
1269                  {                  /*  ARM cpus aren't 64-bit:  */
1270                          char tmps[100];                  debug("0x%08x", (int)entrypoint);
                         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];  
                         }  
   
                         sprintf(tmps, "0x%%0%illx", cpu->cd.urisc.wordlen / 4);  
                         debug(tmps, (long long)entrypoint);  
                         cpu->pc = entrypoint;  
                 }  
1271                  break;                  break;
1272          case ARCH_X86:          case ARCH_X86:
1273                  if (cpu->cd.x86.mode == 16)                  debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS],
1274                          debug("0x%04x:0x%04x", cpu->cd.x86.s[X86_S_CS],                      (long long)cpu->pc);
                             (int)cpu->pc);  
                 else if (cpu->cd.x86.mode == 32)  
                         debug("0x%08x", (int)cpu->pc);  
                 else  
                         debug("0x%016llx", (long long)cpu->pc);  
1275                  break;                  break;
1276          default:          default:
1277                  debug("0x%016llx", (long long)cpu->pc);                  debug("0x%016llx", (long long)cpu->pc);
# Line 831  void emul_simple_init(struct emul *emul) Line 1332  void emul_simple_init(struct emul *emul)
1332                  debug("Simple setup...\n");                  debug("Simple setup...\n");
1333                  debug_indentation(iadd);                  debug_indentation(iadd);
1334    
1335                  /*  Create a network:  */                  /*  Create a simple network:  */
1336                  emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY,                  emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY,
1337                      "10.0.0.0", 8);                      "10.0.0.0", 8, NULL, 0, 0);
1338          } else {          } else {
1339                  /*  Userland pseudo-machine:  */                  /*  Userland pseudo-machine:  */
1340                  debug("Syscall emulation (userland-only) setup...\n");                  debug("Syscall emulation (userland-only) setup...\n");
# Line 940  void emul_run(struct emul **emuls, int n Line 1441  void emul_run(struct emul **emuls, int n
1441                  if (e == NULL)                  if (e == NULL)
1442                          continue;                          continue;
1443                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
1444                          cpu_run_init(e, e->machines[j]);                          cpu_run_init(e->machines[j]);
1445          }          }
1446    
1447            /*  TODO: Generalize:  */
1448            if (emuls[0]->machines[0]->show_trace_tree)
1449                    cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0],
1450                        emuls[0]->machines[0]->cpus[0]->pc);
1451    
1452          /*          /*
1453           *  MAIN LOOP:           *  MAIN LOOP:
1454           *           *
# Line 975  void emul_run(struct emul **emuls, int n Line 1481  void emul_run(struct emul **emuls, int n
1481                  if (e == NULL)                  if (e == NULL)
1482                          continue;                          continue;
1483                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
1484                          cpu_run_deinit(e, e->machines[j]);                          cpu_run_deinit(e->machines[j]);
1485          }          }
1486    
1487          /*  force_debugger_at_exit flag set? Then enter the debugger:  */          /*  force_debugger_at_exit flag set? Then enter the debugger:  */

Legend:
Removed from v.4  
changed lines
  Added in v.12

  ViewVC Help
Powered by ViewVC 1.1.26