/[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 28 by dpavlin, Mon Oct 8 16:20:26 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2003-2005  Anders Gavare.  All rights reserved.   *  Copyright (C) 2003-2006  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.258 2006/07/20 21:52:59 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"
# Line 56  Line 55 
55  #include "x11.h"  #include "x11.h"
56    
57    
 extern int force_debugger_at_exit;  
   
58  extern int extra_argc;  extern int extra_argc;
59  extern char **extra_argv;  extern char **extra_argv;
60    
61  extern int verbose;  extern int verbose;
62  extern int quiet_mode;  extern int quiet_mode;
63    extern int force_debugger_at_exit;
64    extern int single_step;
65    extern int old_show_trace_tree;
66    extern int old_instruction_trace;
67    extern int old_quiet_mode;
68    extern int quiet_mode;
69    
70  extern struct emul *debugger_emul;  extern struct emul *debugger_emul;
71  extern struct diskimage *diskimages[];  extern struct diskimage *diskimages[];
72    
73    static char *diskimage_types[] = DISKIMAGE_TYPES;
74    
75    
76    static void print_separator(void)
77    {
78            int i = 79;
79            while (i-- > 0)
80                    debug("-");
81            debug("\n");
82    }
83    
84    
85  /*  /*
86   *  add_dump_points():   *  add_dump_points():
# Line 94  static void add_dump_points(struct machi Line 108  static void add_dump_points(struct machi
108                          uint64_t addr;                          uint64_t addr;
109                          int res = get_symbol_addr(&m->symbol_context,                          int res = get_symbol_addr(&m->symbol_context,
110                              m->breakpoint_string[i], &addr);                              m->breakpoint_string[i], &addr);
111                          if (!res)                          if (!res) {
112                                  fprintf(stderr,                                  fprintf(stderr,
113                                      "WARNING! Breakpoint '%s' could not be"                                      "ERROR! Breakpoint '%s' could not be"
114                                          " parsed\n",                                          " parsed\n",
115                                      m->breakpoint_string[i]);                                      m->breakpoint_string[i]);
116                          else {                          } else {
117                                  dp = addr;                                  dp = addr;
118                                  string_flag = 1;                                  string_flag = 1;
119                          }                          }
# Line 110  static void add_dump_points(struct machi Line 124  static void add_dump_points(struct machi
124                   *  were automatically converted into the correct address.                   *  were automatically converted into the correct address.
125                   */                   */
126    
127                  if ((dp >> 32) == 0 && ((dp >> 31) & 1))                  if (m->arch == ARCH_MIPS) {
128                          dp |= 0xffffffff00000000ULL;                          if ((dp >> 32) == 0 && ((dp >> 31) & 1))
129                                    dp |= 0xffffffff00000000ULL;
130                    }
131    
132                  m->breakpoint_addr[i] = dp;                  m->breakpoint_addr[i] = dp;
133    
134                  debug("breakpoint %i: 0x%016llx", i, (long long)dp);                  debug("breakpoint %i: 0x%llx", i, (long long)dp);
135                  if (string_flag)                  if (string_flag)
136                          debug(" (%s)", m->breakpoint_string[i]);                          debug(" (%s)", m->breakpoint_string[i]);
137                  debug("\n");                  debug("\n");
# Line 132  static void fix_console(void) Line 149  static void fix_console(void)
149    
150    
151  /*  /*
152     *  iso_load_bootblock():
153     *
154     *  Try to load a kernel from an ISO 9660 disk image. iso_type is 1 for
155     *  "CD001" (standard), 2 for "CDW01" (ECMA), and 3 for "CDROM" (Sierra).
156     *
157     *  TODO: This function uses too many magic offsets and so on; it should be
158     *  cleaned up some day.
159     *
160     *  Returns 1 on success, 0 on failure.
161     */
162    static int iso_load_bootblock(struct machine *m, struct cpu *cpu,
163            int disk_id, int disk_type, int iso_type, unsigned char *buf,
164            int *n_loadp, char ***load_namesp)
165    {
166            char str[35];
167            int filenr, i, ofs, dirlen, res = 0, res2, iadd = DEBUG_INDENTATION;
168            int found_dir;
169            uint64_t dirofs;
170            uint64_t fileofs, filelen;
171            unsigned char *dirbuf = NULL, *dp;
172            unsigned char *match_entry = NULL;
173            char *p, *filename_orig;
174            char *filename = strdup(cpu->machine->boot_kernel_filename);
175            unsigned char *filebuf = NULL;
176            char *tmpfname = NULL;
177            char **new_array;
178            int tmpfile_handle;
179    
180            if (filename == NULL) {
181                    fatal("out of memory\n");
182                    exit(1);
183            }
184            filename_orig = filename;
185    
186            debug("ISO9660 boot:\n");
187            debug_indentation(iadd);
188    
189            /*  Volume ID:  */
190            ofs = iso_type == 3? 48 : 40;
191            memcpy(str, buf + ofs, sizeof(str));
192            str[32] = '\0';  i = 31;
193            while (i >= 0 && str[i]==' ')
194                    str[i--] = '\0';
195            if (str[0])
196                    debug("\"%s\"", str);
197            else {
198                    /*  System ID:  */
199                    ofs = iso_type == 3? 16 : 8;
200                    memcpy(str, buf + ofs, sizeof(str));
201                    str[32] = '\0';  i = 31;
202                    while (i >= 0 && str[i]==' ')
203                            str[i--] = '\0';
204                    if (str[0])
205                            debug("\"%s\"", str);
206                    else
207                            debug("(no ID)");
208            }
209    
210            debug(":%s\n", filename);
211    
212    
213            /*
214             *  Traverse the directory structure to find the kernel.
215             */
216    
217            dirlen = buf[0x84] + 256*buf[0x85] + 65536*buf[0x86];
218            if (dirlen != buf[0x8b] + 256*buf[0x8a] + 65536*buf[0x89])
219                    fatal("WARNING: Root directory length mismatch?\n");
220    
221            dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) +
222                ((uint64_t)buf[0x8f] << 24)) * 2048;
223    
224            /*  debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs);  */
225    
226            dirbuf = malloc(dirlen);
227            if (dirbuf == NULL) {
228                    fatal("out of memory in iso_load_bootblock()\n");
229                    exit(1);
230            }
231    
232            res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, dirbuf,
233                dirlen);
234            if (!res2) {
235                    fatal("Couldn't read the disk image. Aborting.\n");
236                    goto ret;
237            }
238    
239            found_dir = 1;  /*  Assume root dir  */
240            dp = dirbuf; filenr = 1;
241            p = NULL;
242            while (dp < dirbuf + dirlen) {
243                    size_t i, nlen = dp[0];
244                    int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) +
245                        ((uint64_t)dp[5] << 24);
246                    int y = dp[6] + (dp[7] << 8);
247                    char direntry[65];
248    
249                    dp += 8;
250    
251                    /*
252                     *  As long as there is an \ or / in the filename, then we
253                     *  have not yet found the directory.
254                     */
255                    p = strchr(filename, '/');
256                    if (p == NULL)
257                            p = strchr(filename, '\\');
258    
259                    /*  debug("%i%s: %i, %i, \"", filenr, filenr == found_dir?
260                        " [CURRENT]" : "", x, y);  */
261                    for (i=0; i<nlen && i<sizeof(direntry)-1; i++)
262                            if (dp[i]) {
263                                    direntry[i] = dp[i];
264                                    /*  debug("%c", dp[i]);  */
265                            } else
266                                    break;
267                    /*  debug("\"\n");  */
268                    direntry[i] = '\0';
269    
270                    /*  A directory name match?  */
271                    if (p != NULL && strncasecmp(filename, direntry, nlen) == 0
272                        && nlen == (size_t)p - (size_t)filename && found_dir == y) {
273                            found_dir = filenr;
274                            filename = p+1;
275                            dirofs = 2048 * (int64_t)x;
276                    }
277    
278                    dp += nlen;
279    
280                    /*  16-bit aligned lenght:  */
281                    if (nlen & 1)
282                            dp ++;
283    
284                    filenr ++;
285            }
286    
287            p = strchr(filename, '/');
288            if (p == NULL)
289                    p = strchr(filename, '\\');
290    
291            if (p != NULL) {
292                    char *blah = filename_orig;
293    
294                    fatal("could not find '%s' in /", filename);
295    
296                    /*  Print the first part of the filename:  */
297                    while (blah != filename)
298                            fatal("%c", *blah++);
299                    
300                    fatal("\n");
301                    goto ret;
302            }
303    
304            /*  debug("dirofs = 0x%llx\n", (long long)dirofs);  */
305    
306            /*  Free the old dirbuf, and allocate a new one:  */
307            free(dirbuf);
308            dirbuf = malloc(512);
309            if (dirbuf == NULL) {
310                    fatal("out of memory in iso_load_bootblock()\n");
311                    exit(1);
312            }
313    
314            for (;;) {
315                    size_t len, i;
316    
317                    /*  Too close to another sector? Then realign.  */
318                    if ((dirofs & 2047) + 70 > 2047) {
319                            dirofs = (dirofs | 2047) + 1;
320                            /*  debug("realign dirofs = 0x%llx\n", dirofs);  */
321                    }
322    
323                    res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs,
324                        dirbuf, 256);
325                    if (!res2) {
326                            fatal("Couldn't read the disk image. Aborting.\n");
327                            goto ret;
328                    }
329    
330                    dp = dirbuf;
331                    len = dp[0];
332                    if (len < 2)
333                            break;
334    
335                    /*
336                     *  TODO: Actually parse the directory entry!
337                     *
338                     *  Haha, this must be rewritten.
339                     */
340                    for (i=32; i<len; i++) {
341                            if (i < len - strlen(filename))
342                                    if (strncasecmp(filename, (char *)dp + i,
343                                        strlen(filename)) == 0) {
344                                            /*  The filename was found somewhere
345                                                in the directory entry.  */
346                                            if (match_entry != NULL) {
347                                                    fatal("TODO: I'm too lazy to"
348                                                        " implement a correct "
349                                                        "directory parser right "
350                                                        "now... (BUG)\n");
351                                                    exit(1);
352                                            }
353                                            match_entry = malloc(512);
354                                            if (match_entry == NULL) {
355                                                    fatal("out of memory\n");
356                                                    exit(1);
357                                            }
358                                            memcpy(match_entry, dp, 512);
359                                            break;
360                                    }
361                    }
362    
363                    dirofs += len;
364            }
365    
366            if (match_entry == NULL) {
367                    char *blah = filename_orig;
368    
369                    fatal("could not find '%s' in /", filename);
370    
371                    /*  Print the first part of the filename:  */
372                    while (blah != filename)
373                            fatal("%c", *blah++);
374                    
375                    fatal("\n");
376                    goto ret;
377            }
378    
379            fileofs = match_entry[2] + (match_entry[3] << 8) +
380                (match_entry[4] << 16) + ((uint64_t)match_entry[5] << 24);
381            filelen = match_entry[10] + (match_entry[11] << 8) +
382                (match_entry[12] << 16) + ((uint64_t)match_entry[13] << 24);
383            fileofs *= 2048;
384    
385            /*  debug("filelen=%llx fileofs=%llx\n", (long long)filelen,
386                (long long)fileofs);  */
387    
388            filebuf = malloc(filelen);
389            if (filebuf == NULL) {
390                    fatal("could not allocate %lli bytes to read the file"
391                        " from the disk image!\n", (long long)filelen);
392                    goto ret;
393            }
394    
395            tmpfname = strdup("/tmp/gxemul.XXXXXXXXXXXX");
396    
397            res2 = diskimage_access(m, disk_id, disk_type, 0, fileofs, filebuf,
398                filelen);
399            if (!res2) {
400                    fatal("could not read the file from the disk image!\n");
401                    goto ret;
402            }
403    
404            tmpfile_handle = mkstemp(tmpfname);
405            if (tmpfile_handle < 0) {
406                    fatal("could not create %s\n", tmpfname);
407                    exit(1);
408            }
409            write(tmpfile_handle, filebuf, filelen);
410            close(tmpfile_handle);
411    
412            debug("extracted %lli bytes into %s\n", (long long)filelen, tmpfname);
413    
414            /*  Add the temporary filename to the load_namesp array:  */
415            (*n_loadp)++;
416            new_array = malloc(sizeof(char *) * (*n_loadp));
417            if (new_array == NULL) {
418                    fatal("out of memory\n");
419                    exit(1);
420            }
421            memcpy(new_array, *load_namesp, sizeof(char *) * (*n_loadp));
422            *load_namesp = new_array;
423    
424            /*  This adds a Backspace char in front of the filename; this
425                is a special hack which causes the file to be removed once
426                it has been loaded.  */
427            tmpfname = realloc(tmpfname, strlen(tmpfname) + 2);
428            memmove(tmpfname + 1, tmpfname, strlen(tmpfname) + 1);
429            tmpfname[0] = 8;
430    
431            (*load_namesp)[*n_loadp - 1] = tmpfname;
432    
433            res = 1;
434    
435    ret:
436            if (dirbuf != NULL)
437                    free(dirbuf);
438    
439            if (filebuf != NULL)
440                    free(filebuf);
441    
442            if (match_entry != NULL)
443                    free(match_entry);
444    
445            free(filename_orig);
446    
447            debug_indentation(-iadd);
448            return res;
449    }
450    
451    
452    /*
453     *  apple_load_bootblock():
454     *
455     *  Try to load a kernel from a disk image with an Apple Partition Table.
456     *
457     *  TODO: This function uses too many magic offsets and so on; it should be
458     *  cleaned up some day. See http://www.awprofessional.com/articles/
459     *      article.asp?p=376123&seqNum=3&rl=1  for some info on the Apple
460     *  partition format.
461     *
462     *  Returns 1 on success, 0 on failure.
463     */
464    static int apple_load_bootblock(struct machine *m, struct cpu *cpu,
465            int disk_id, int disk_type, int *n_loadp, char ***load_namesp)
466    {
467            unsigned char buf[0x8000];
468            int res, partnr, n_partitions = 0, n_hfs_partitions = 0;
469            uint64_t hfs_start, hfs_length;
470    
471            res = diskimage_access(m, disk_id, disk_type, 0, 0x0, buf, sizeof(buf));
472            if (!res) {
473                    fatal("apple_load_bootblock: couldn't read the disk "
474                        "image. Aborting.\n");
475                    return 0;
476            }
477    
478            partnr = 0;
479            do {
480                    int start, length;
481                    int ofs = 0x200 * (partnr + 1);
482                    if (partnr == 0)
483                            n_partitions = buf[ofs + 7];
484                    start = ((uint64_t)buf[ofs + 8] << 24) + (buf[ofs + 9] << 16) +
485                        (buf[ofs + 10] << 8) + buf[ofs + 11];
486                    length = ((uint64_t)buf[ofs+12] << 24) + (buf[ofs + 13] << 16) +
487                        (buf[ofs + 14] << 8) + buf[ofs + 15];
488    
489                    debug("partition %i: '%s', type '%s', start %i, length %i\n",
490                        partnr, buf + ofs + 0x10, buf + ofs + 0x30,
491                        start, length);
492    
493                    if (strcmp((char *)buf + ofs + 0x30, "Apple_HFS") == 0) {
494                            n_hfs_partitions ++;
495                            hfs_start = 512 * start;
496                            hfs_length = 512 * length;
497                    }
498    
499                    /*  Any more partitions?  */
500                    partnr ++;
501            } while (partnr < n_partitions);
502    
503            if (n_hfs_partitions == 0) {
504                    fatal("Error: No HFS partition found! TODO\n");
505                    return 0;
506            }
507            if (n_hfs_partitions >= 2) {
508                    fatal("Error: Too many HFS partitions found! TODO\n");
509                    return 0;
510            }
511    
512            return 0;
513    }
514    
515    
516    /*
517   *  load_bootblock():   *  load_bootblock():
518   *   *
519   *  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
520   *  loading a bootblock from a specific disk offset into memory, and executing   *  loading a bootblock from a specific disk offset into memory, and executing
521   *  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
522   *  bootblock to load a kernel.   *  bootblock to load a kernel.
523     *
524     *  Returns 1 on success, 0 on failure.
525   */   */
526  static void load_bootblock(struct machine *m, struct cpu *cpu)  static int load_bootblock(struct machine *m, struct cpu *cpu,
527            int *n_loadp, char ***load_namesp)
528  {  {
529          int boot_disk_id;          int boot_disk_id, boot_disk_type = 0, n_blocks, res, readofs,
530                iso_type, retval = 0;
531          unsigned char minibuf[0x20];          unsigned char minibuf[0x20];
532          unsigned char *bootblock_buf;          unsigned char *bootblock_buf;
533          uint64_t bootblock_offset;          uint64_t bootblock_offset;
534          uint64_t bootblock_loadaddr, bootblock_pc;          uint64_t bootblock_loadaddr, bootblock_pc;
         int n_blocks, res, readofs;  
535    
536          boot_disk_id = diskimage_bootdev(m);          boot_disk_id = diskimage_bootdev(m, &boot_disk_type);
537          if (boot_disk_id < 0)          if (boot_disk_id < 0)
538                  return;                  return 0;
539    
540          switch (m->machine_type) {          switch (m->machine_type) {
541          case MACHINE_DEC:          case MACHINE_PMAX:
542                  /*                  /*
543                   *  The first few bytes of a disk contains information about                   *  The first few bytes of a disk contains information about
544                   *  where the bootblock(s) are located. (These are all 32-bit                   *  where the bootblock(s) are located. (These are all 32-bit
# Line 170  static void load_bootblock(struct machin Line 555  static void load_bootblock(struct machin
555                   *  nr of blocks to read and offset are repeated until nr of                   *  nr of blocks to read and offset are repeated until nr of
556                   *  blocks to read is zero.                   *  blocks to read is zero.
557                   */                   */
558                  res = diskimage_access(m, boot_disk_id, 0, 0,                  res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0,
559                      minibuf, sizeof(minibuf));                      minibuf, sizeof(minibuf));
560    
561                  bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8)                  bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8)
562                    + (minibuf[0x12] << 16) + (minibuf[0x13] << 24);                    + (minibuf[0x12] << 16) + ((uint64_t)minibuf[0x13] << 24);
563    
564                  /*  Convert loadaddr to uncached:  */                  /*  Convert loadaddr to uncached:  */
565                  if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 &&                  if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 &&
# Line 185  static void load_bootblock(struct machin Line 570  static void load_bootblock(struct machin
570                  bootblock_loadaddr |= 0xffffffffa0000000ULL;                  bootblock_loadaddr |= 0xffffffffa0000000ULL;
571    
572                  bootblock_pc = minibuf[0x14] + (minibuf[0x15] << 8)                  bootblock_pc = minibuf[0x14] + (minibuf[0x15] << 8)
573                    + (minibuf[0x16] << 16) + (minibuf[0x17] << 24);                    + (minibuf[0x16] << 16) + ((uint64_t)minibuf[0x17] << 24);
574    
575                  bootblock_pc &= 0x0fffffffULL;                  bootblock_pc &= 0x0fffffffULL;
576                  bootblock_pc |= 0xffffffffa0000000ULL;                  bootblock_pc |= 0xffffffffa0000000ULL;
# Line 197  static void load_bootblock(struct machin Line 582  static void load_bootblock(struct machin
582                  readofs = 0x18;                  readofs = 0x18;
583    
584                  for (;;) {                  for (;;) {
585                          res = diskimage_access(m, boot_disk_id, 0, readofs,                          res = diskimage_access(m, boot_disk_id, boot_disk_type,
586                              minibuf, sizeof(minibuf));                              0, readofs, minibuf, sizeof(minibuf));
587                          if (!res) {                          if (!res) {
588                                  printf("couldn't read disk?\n");                                  fatal("Couldn't read the disk image. "
589                                  exit(1);                                      "Aborting.\n");
590                                    return 0;
591                          }                          }
592    
593                          n_blocks = minibuf[0] + (minibuf[1] << 8)                          n_blocks = minibuf[0] + (minibuf[1] << 8)
594                            + (minibuf[2] << 16) + (minibuf[3] << 24);                            + (minibuf[2] << 16) + ((uint64_t)minibuf[3] << 24);
595    
596                          bootblock_offset = (minibuf[4] + (minibuf[5] << 8)                          bootblock_offset = (minibuf[4] + (minibuf[5] << 8) +
597                            + (minibuf[6] << 16) + (minibuf[7] << 24)) * 512;                            (minibuf[6]<<16) + ((uint64_t)minibuf[7]<<24)) * 512;
598    
599                          if (n_blocks < 1)                          if (n_blocks < 1)
600                                  break;                                  break;
# Line 226  static void load_bootblock(struct machin Line 612  static void load_bootblock(struct machin
612                                  exit(1);                                  exit(1);
613                          }                          }
614    
615                          res = diskimage_access(m, boot_disk_id, 0,                          res = diskimage_access(m, boot_disk_id, boot_disk_type,
616                              bootblock_offset, bootblock_buf, n_blocks * 512);                              0, bootblock_offset, bootblock_buf, n_blocks * 512);
617                          if (!res) {                          if (!res) {
618                                  fatal("WARNING: could not load bootblocks from"                                  fatal("WARNING: could not load bootblocks from"
619                                      " disk offset 0x%llx\n",                                      " disk offset 0x%llx\n",
# Line 243  static void load_bootblock(struct machin Line 629  static void load_bootblock(struct machin
629                  }                  }
630    
631                  debug(readofs == 0x18? ": no blocks?\n" : " blocks\n");                  debug(readofs == 0x18? ": no blocks?\n" : " blocks\n");
632                  break;                  return 1;
633          default:  
634                  fatal("Booting from disk without a separate kernel "          case MACHINE_X86:
635                      "doesn't work in this emulation mode.\n");                  /*  TODO: "El Torito" etc?  */
636                    if (diskimage_is_a_cdrom(cpu->machine, boot_disk_id,
637                        boot_disk_type))
638                            break;
639    
640                    bootblock_buf = malloc(512);
641                    if (bootblock_buf == NULL) {
642                            fprintf(stderr, "Out of memory.\n");
643                            exit(1);
644                    }
645    
646                    debug("loading PC bootsector from %s id %i\n",
647                        diskimage_types[boot_disk_type], boot_disk_id);
648    
649                    res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0,
650                        bootblock_buf, 512);
651                    if (!res) {
652                            fatal("Couldn't read the disk image. Aborting.\n");
653                            return 0;
654                    }
655    
656                    if (bootblock_buf[510] != 0x55 || bootblock_buf[511] != 0xaa)
657                            debug("WARNING! The 0x55,0xAA marker is missing! "
658                                "Booting anyway.\n");
659                    store_buf(cpu, 0x7c00, (char *)bootblock_buf, 512);
660                    free(bootblock_buf);
661    
662                    return 1;
663            }
664    
665    
666            /*
667             *  Try reading a kernel manually from the disk. The code here
668             *  does not rely on machine-dependent boot blocks etc.
669             */
670            /*  ISO9660: (0x800 bytes at 0x8000)  */
671            bootblock_buf = malloc(0x800);
672            if (bootblock_buf == NULL) {
673                    fprintf(stderr, "Out of memory.\n");
674                  exit(1);                  exit(1);
675          }          }
676    
677            res = diskimage_access(m, boot_disk_id, boot_disk_type,
678                0, 0x8000, bootblock_buf, 0x800);
679            if (!res) {
680                    fatal("Couldn't read the disk image. Aborting.\n");
681                    return 0;
682            }
683    
684            iso_type = 0;
685            if (strncmp((char *)bootblock_buf+1, "CD001", 5) == 0)
686                    iso_type = 1;
687            if (strncmp((char *)bootblock_buf+1, "CDW01", 5) == 0)
688                    iso_type = 2;
689            if (strncmp((char *)bootblock_buf+1, "CDROM", 5) == 0)
690                    iso_type = 3;
691    
692            if (iso_type != 0) {
693                    /*  We can't load a kernel if the name
694                        isn't specified.  */
695                    if (cpu->machine->boot_kernel_filename == NULL ||
696                        cpu->machine->boot_kernel_filename[0] == '\0')
697                            fatal("\nISO9660 filesystem, but no kernel "
698                                "specified? (Use the -j option.)\n");
699                    else
700                            retval = iso_load_bootblock(m, cpu, boot_disk_id,
701                                boot_disk_type, iso_type, bootblock_buf,
702                                n_loadp, load_namesp);
703            }
704    
705            if (retval != 0)
706                    goto ret_ok;
707    
708            /*  Apple parition table:  */
709            res = diskimage_access(m, boot_disk_id, boot_disk_type,
710                0, 0x0, bootblock_buf, 0x800);
711            if (!res) {
712                    fatal("Couldn't read the disk image. Aborting.\n");
713                    return 0;
714            }
715            if (bootblock_buf[0x000] == 'E' && bootblock_buf[0x001] == 'R' &&
716                bootblock_buf[0x200] == 'P' && bootblock_buf[0x201] == 'M') {
717                    /*  We can't load a kernel if the name
718                        isn't specified.  */
719                    if (cpu->machine->boot_kernel_filename == NULL ||
720                        cpu->machine->boot_kernel_filename[0] == '\0')
721                            fatal("\nApple partition table, but no kernel "
722                                "specified? (Use the -j option.)\n");
723                    else
724                            retval = apple_load_bootblock(m, cpu, boot_disk_id,
725                                boot_disk_type, n_loadp, load_namesp);
726            }
727    
728    ret_ok:
729            free(bootblock_buf);
730            return retval;
731  }  }
732    
733    
# Line 270  struct emul *emul_new(char *name) Line 749  struct emul *emul_new(char *name)
749    
750          /*  Sane default values:  */          /*  Sane default values:  */
751          e->n_machines = 0;          e->n_machines = 0;
752            e->next_serial_nr = 1;
753    
754          if (name != NULL) {          if (name != NULL) {
755                  e->name = strdup(name);                  e->name = strdup(name);
# Line 332  static void add_arc_components(struct ma Line 812  static void add_arc_components(struct ma
812    
813          len += 1048576 * m->memory_offset_in_mb;          len += 1048576 * m->memory_offset_in_mb;
814    
815          /*  NOTE/TODO: magic 12MB end of load program area  */          /*
816             *  NOTE/TODO: magic 12MB end of load program area
817             *
818             *  Hm. This breaks the old FreeBSD/MIPS snapshots...
819             */
820    #if 0
821          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
822              0x60000 + m->memory_offset_in_mb * 1048576,              0x60000 + m->memory_offset_in_mb * 1048576,
823              start-0x60000 - m->memory_offset_in_mb * 1048576,              start-0x60000 - m->memory_offset_in_mb * 1048576,
824              ARCBIOS_MEM_FreeMemory);              ARCBIOS_MEM_FreeMemory);
825    #endif
826          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
827              start, len, ARCBIOS_MEM_LoadedProgram);              start, len, ARCBIOS_MEM_LoadedProgram);
828    
829          scsicontroller = arcbios_get_scsicontroller();          scsicontroller = arcbios_get_scsicontroller(m);
830          if (scsicontroller == 0)          if (scsicontroller == 0)
831                  return;                  return;
832    
# Line 388  static void add_arc_components(struct ma Line 874  static void add_arc_components(struct ma
874                                  snprintf(component_string,                                  snprintf(component_string,
875                                      sizeof(component_string),                                      sizeof(component_string),
876                                      "scsi(0)cdrom(%i)", d->id);                                      "scsi(0)cdrom(%i)", d->id);
877                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
878                                      component_string, scsidevice);                                      component_string, scsidevice);
879    
880                                  snprintf(component_string,                                  snprintf(component_string,
881                                      sizeof(component_string),                                      sizeof(component_string),
882                                      "scsi(0)cdrom(%i)fdisk(0)", d->id);                                      "scsi(0)cdrom(%i)fdisk(0)", d->id);
883                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
884                                      component_string, scsidisk);                                      component_string, scsidisk);
885                          } else {                          } else {
886                                  snprintf(component_string,                                  snprintf(component_string,
887                                      sizeof(component_string),                                      sizeof(component_string),
888                                      "scsi(0)disk(%i)", d->id);                                      "scsi(0)disk(%i)", d->id);
889                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
890                                      component_string, scsidevice);                                      component_string, scsidevice);
891    
892                                  snprintf(component_string,                                  snprintf(component_string,
893                                      sizeof(component_string),                                      sizeof(component_string),
894                                      "scsi(0)disk(%i)rdisk(0)", d->id);                                      "scsi(0)disk(%i)rdisk(0)", d->id);
895                                  arcbios_add_string_to_component(                                  arcbios_add_string_to_component(m,
896                                      component_string, scsidisk);                                      component_string, scsidisk);
897                          }                          }
898                  }                  }
# Line 429  static void add_arc_components(struct ma Line 915  static void add_arc_components(struct ma
915  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,
916          int n_devices, char **device_names)          int n_devices, char **device_names)
917  {  {
         struct emul *emul;  
918          struct cpu *cpu;          struct cpu *cpu;
919          int i, iadd=4;          int i, iadd = DEBUG_INDENTATION;
920          uint64_t addr, memory_amount, entrypoint = 0, gp = 0, toc = 0;          uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0;
921          int byte_order;          int byte_order;
922    
         emul = m->emul;  
   
923          debug("machine \"%s\":\n", m->name);          debug("machine \"%s\":\n", m->name);
924          debug_indentation(iadd);          debug_indentation(iadd);
925    
# Line 457  void emul_machine_setup(struct machine * Line 940  void emul_machine_setup(struct machine *
940    
941          m->cpu_family = cpu_family_ptr_by_number(m->arch);          m->cpu_family = cpu_family_ptr_by_number(m->arch);
942    
943            if (m->arch == ARCH_ALPHA)
944                    m->arch_pagesize = 8192;
945    
946          machine_memsize_fix(m);          machine_memsize_fix(m);
947    
948          /*          /*
# Line 477  void emul_machine_setup(struct machine * Line 963  void emul_machine_setup(struct machine *
963                  debug(" (offset by %iMB)", m->memory_offset_in_mb);                  debug(" (offset by %iMB)", m->memory_offset_in_mb);
964                  memory_amount += 1048576 * m->memory_offset_in_mb;                  memory_amount += 1048576 * m->memory_offset_in_mb;
965          }          }
966          m->memory = memory_new(memory_amount);          m->memory = memory_new(memory_amount, m->arch);
967          if (m->machine_type != MACHINE_USERLAND)          if (m->machine_type != MACHINE_USERLAND)
968                  debug("\n");                  debug("\n");
969    
# Line 504  void emul_machine_setup(struct machine * Line 990  void emul_machine_setup(struct machine *
990          }          }
991          memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus);          memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus);
992    
         /*  Initialize dynamic binary translation, if available:  */  
         if (m->bintrans_enable)  
                 bintrans_init(m, m->memory);  
   
993          debug("cpu0");          debug("cpu0");
994          if (m->ncpus > 1)          if (m->ncpus > 1)
995                  debug(" .. cpu%i", m->ncpus - 1);                  debug(" .. cpu%i", m->ncpus - 1);
996          debug(": ");          debug(": ");
997          for (i=0; i<m->ncpus; i++) {          for (i=0; i<m->ncpus; i++) {
998                  m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name);                  m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name);
999                  if (m->bintrans_enable)                  if (m->cpus[i] == NULL) {
1000                          bintrans_init_cpu(m->cpus[i]);                          fprintf(stderr, "Unable to create CPU object. "
1001                                "Aborting.");
1002                            exit(1);
1003                    }
1004          }          }
1005          debug("\n");          debug("\n");
1006    
1007    #if 0
1008            /*  Special case: The Playstation Portable has an additional CPU:  */
1009            if (m->machine_type == MACHINE_PSP) {
1010                    debug("cpu%i: ", m->ncpus);
1011                    m->cpus[m->ncpus] = cpu_new(m->memory, m,
1012                        0  /*  use 0 here to show info with debug()  */,
1013                        "Allegrex" /*  TODO  */);
1014                    debug("\n");
1015                    m->ncpus ++;
1016            }
1017    #endif
1018    
1019          if (m->use_random_bootstrap_cpu)          if (m->use_random_bootstrap_cpu)
1020                  m->bootstrap_cpu = random() % m->ncpus;                  m->bootstrap_cpu = random() % m->ncpus;
1021          else          else
# Line 530  void emul_machine_setup(struct machine * Line 1027  void emul_machine_setup(struct machine *
1027          if (m->userland_emul != NULL) {          if (m->userland_emul != NULL) {
1028                  useremul_name_to_useremul(cpu,                  useremul_name_to_useremul(cpu,
1029                      m->userland_emul, NULL, NULL, NULL);                      m->userland_emul, NULL, NULL, NULL);
1030                  cpu->memory_rw = userland_memory_rw;  
1031                    switch (m->arch) {
1032    #ifdef ENABLE_ALPHA
1033                    case ARCH_ALPHA:
1034                            cpu->memory_rw = alpha_userland_memory_rw;
1035                            break;
1036    #endif
1037                    default:cpu->memory_rw = userland_memory_rw;
1038                    }
1039          }          }
1040    
1041          if (m->use_x11)          if (m->use_x11)
1042                  x11_init(m);                  x11_init(m);
1043    
1044          /*  Fill memory with random bytes:  */          /*  Fill memory with random bytes:  */
         /*  TODO: This is MIPS-specific!  */  
1045          if (m->random_mem_contents) {          if (m->random_mem_contents) {
1046                  for (i=0; i<m->physical_ram_in_mb * 1048576; i+=256) {                  for (i=0; i<m->physical_ram_in_mb * 1048576; i+=256) {
1047                          unsigned char data[256];                          unsigned char data[256];
1048                          unsigned int j;                          unsigned int j;
1049                          for (j=0; j<sizeof(data); j++)                          for (j=0; j<sizeof(data); j++)
1050                                  data[j] = random() & 255;                                  data[j] = random() & 255;
1051                          addr = 0xffffffff80000000ULL + i;                          cpu->memory_rw(cpu, m->memory, i, data, sizeof(data),
1052                          cpu->memory_rw(cpu, m->memory, addr, data, sizeof(data),                              MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS | PHYSICAL);
                             MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS);  
1053                  }                  }
1054          }          }
1055    
         if ((m->machine_type == MACHINE_ARC ||  
             m->machine_type == MACHINE_SGI) && m->prom_emulation)  
                 arcbios_init();  
   
1056          if (m->userland_emul != NULL) {          if (m->userland_emul != NULL) {
1057                  /*                  /*
1058                   *  For userland-only emulation, no machine emulation                   *  For userland-only emulation, no machine emulation
# Line 567  void emul_machine_setup(struct machine * Line 1066  void emul_machine_setup(struct machine *
1066          }          }
1067    
1068          diskimage_dump_info(m);          diskimage_dump_info(m);
1069            console_debug_dump(m);
1070    
1071          /*  Load files (ROM code, boot code, ...) into memory:  */          /*  Load files (ROM code, boot code, ...) into memory:  */
1072          if (n_load == 0) {          if (n_load == 0) {
1073                  if (m->first_diskimage != NULL)                  if (m->first_diskimage != NULL) {
1074                          load_bootblock(m, cpu);                          if (!load_bootblock(m, cpu, &n_load, &load_names)) {
1075                  else {                                  fprintf(stderr, "\nNo executable files were"
1076                                        " specified, and booting directly from disk"
1077                                        " failed.\n");
1078                                    exit(1);
1079                            }
1080                    } else {
1081                          fprintf(stderr, "No executable file(s) loaded, and "                          fprintf(stderr, "No executable file(s) loaded, and "
1082                              "we are not booting directly from a disk image."                              "we are not booting directly from a disk image."
1083                              "\nAborting.\n");                              "\nAborting.\n");
# Line 581  void emul_machine_setup(struct machine * Line 1086  void emul_machine_setup(struct machine *
1086          }          }
1087    
1088          while (n_load > 0) {          while (n_load > 0) {
1089                    FILE *tmp_f;
1090                    char *name_to_load = *load_names;
1091                    int remove_after_load = 0;
1092    
1093                    /*  Special hack for removing temporary files:  */
1094                    if (name_to_load[0] == 8) {
1095                            name_to_load ++;
1096                            remove_after_load = 1;
1097                    }
1098    
1099                    /*
1100                     *  gzipped files are automagically gunzipped:
1101                     *  NOTE/TODO: This isn't secure. system() is used.
1102                     */
1103                    tmp_f = fopen(name_to_load, "r");
1104                    if (tmp_f != NULL) {
1105                            unsigned char buf[2];           /*  gzip header  */
1106                            memset(buf, 0, sizeof(buf));
1107                            fread(buf, 1, sizeof(buf), tmp_f);
1108                            if (buf[0]==0x1f && buf[1]==0x8b) {
1109                                    size_t zzlen = strlen(name_to_load)*2 + 100;
1110                                    char *zz = malloc(zzlen);
1111                                    debug("gunziping %s\n", name_to_load);
1112                                    /*
1113                                     *  gzip header found.  If this was a file
1114                                     *  extracted from, say, a CDROM image, then it
1115                                     *  already has a temporary name. Otherwise we
1116                                     *  have to gunzip into a temporary file.
1117                                     */
1118                                    if (remove_after_load) {
1119                                            snprintf(zz, zzlen, "mv %s %s.gz",
1120                                                name_to_load, name_to_load);
1121                                            system(zz);
1122                                            snprintf(zz, zzlen, "gunzip %s.gz",
1123                                                name_to_load);
1124                                            system(zz);
1125                                    } else {
1126                                            /*  gunzip into new temp file:  */
1127                                            int tmpfile_handle;
1128                                            char *new_temp_name =
1129                                                strdup("/tmp/gxemul.XXXXXXXXXXXX");
1130                                            tmpfile_handle = mkstemp(new_temp_name);
1131                                            close(tmpfile_handle);
1132                                            snprintf(zz, zzlen, "gunzip -c '%s' > "
1133                                                "%s", name_to_load, new_temp_name);
1134                                            system(zz);
1135                                            name_to_load = new_temp_name;
1136                                            remove_after_load = 1;
1137                                    }
1138                                    free(zz);
1139                            }
1140                            fclose(tmp_f);
1141                    }
1142    
1143                    /*
1144                     *  Ugly (but usable) hack for Playstation Portable:  If the
1145                     *  filename ends with ".pbp" and the file contains an ELF
1146                     *  header, then extract the ELF file into a temporary file.
1147                     */
1148                    if (strlen(name_to_load) > 4 && strcasecmp(name_to_load +
1149                        strlen(name_to_load) - 4, ".pbp") == 0 &&
1150                        (tmp_f = fopen(name_to_load, "r")) != NULL) {
1151                            off_t filesize, j, found=0;
1152                            unsigned char *buf;
1153                            fseek(tmp_f, 0, SEEK_END);
1154                            filesize = ftello(tmp_f);
1155                            fseek(tmp_f, 0, SEEK_SET);
1156                            buf = malloc(filesize);
1157                            if (buf == NULL) {
1158                                    fprintf(stderr, "out of memory while trying"
1159                                        " to read %s\n", name_to_load);
1160                                    exit(1);
1161                            }
1162                            fread(buf, 1, filesize, tmp_f);
1163                            fclose(tmp_f);
1164                            /*  Search for the ELF header, from offset 1 (!):  */
1165                            for (j=1; j<filesize - 4; j++)
1166                                    if (memcmp(buf + j, ELFMAG, SELFMAG) == 0) {
1167                                            found = j;
1168                                            break;
1169                                    }
1170                            if (found != 0) {
1171                                    int tmpfile_handle;
1172                                    char *new_temp_name =
1173                                        strdup("/tmp/gxemul.XXXXXXXXXXXX");
1174                                    debug("extracting ELF from %s (offset 0x%x)\n",
1175                                        name_to_load, (int)found);
1176                                    tmpfile_handle = mkstemp(new_temp_name);
1177                                    write(tmpfile_handle, buf + found,
1178                                        filesize - found);
1179                                    close(tmpfile_handle);
1180                                    name_to_load = new_temp_name;
1181                                    remove_after_load = 1;
1182                            }
1183                    }
1184    
1185                    /*  Special things required _before_ loading the file:  */
1186                    switch (m->arch) {
1187                    case ARCH_X86:
1188                            /*
1189                             *  X86 machines normally don't need to load any files,
1190                             *  they can boot from disk directly. Therefore, an x86
1191                             *  machine usually boots up in 16-bit real mode. When
1192                             *  loading a 32-bit (or even 64-bit) ELF, that's not
1193                             *  very nice, hence this special case.
1194                             */
1195                            pc_bios_simple_pmode_setup(cpu);
1196                            break;
1197                    }
1198    
1199                  byte_order = NO_BYTE_ORDER_OVERRIDE;                  byte_order = NO_BYTE_ORDER_OVERRIDE;
1200    
1201                  file_load(m, m->memory, *load_names, &entrypoint,                  /*
1202                     *  Load the file:  :-)
1203                     */
1204                    file_load(m, m->memory, name_to_load, &entrypoint,
1205                      m->arch, &gp, &byte_order, &toc);                      m->arch, &gp, &byte_order, &toc);
1206    
1207                    if (remove_after_load) {
1208                            debug("removing %s\n", name_to_load);
1209                            unlink(name_to_load);
1210                    }
1211    
1212                  if (byte_order != NO_BYTE_ORDER_OVERRIDE)                  if (byte_order != NO_BYTE_ORDER_OVERRIDE)
1213                          cpu->byte_order = byte_order;                          cpu->byte_order = byte_order;
1214    
1215                  cpu->pc = entrypoint;                  cpu->pc = entrypoint;
1216    
1217                  switch (m->arch) {                  switch (m->arch) {
1218    
1219                    case ARCH_ALPHA:
1220                            /*  For position-independent code:  */
1221                            cpu->cd.alpha.r[ALPHA_T12] = cpu->pc;
1222                            break;
1223    
1224                    case ARCH_ARM:
1225                            if (cpu->pc & 3) {
1226                                    fatal("ARM: lowest bits of pc set: TODO\n");
1227                                    exit(1);
1228                            }
1229                            cpu->pc &= 0xfffffffc;
1230                            break;
1231    
1232                    case ARCH_AVR:
1233                            cpu->pc &= 0xfffff;
1234                            if (cpu->pc & 1) {
1235                                    fatal("AVR: lowest bit of pc set: TODO\n");
1236                                    exit(1);
1237                            }
1238                            break;
1239    
1240                    case ARCH_HPPA:
1241                            break;
1242    
1243                    case ARCH_I960:
1244                            break;
1245    
1246                    case ARCH_IA64:
1247                            break;
1248    
1249                    case ARCH_M68K:
1250                            break;
1251    
1252                  case ARCH_MIPS:                  case ARCH_MIPS:
1253                          if ((cpu->pc >> 32) == 0                          if ((cpu->pc >> 32) == 0 && (cpu->pc & 0x80000000ULL))
                             && (cpu->pc & 0x80000000ULL))  
1254                                  cpu->pc |= 0xffffffff00000000ULL;                                  cpu->pc |= 0xffffffff00000000ULL;
1255    
1256                          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 1260  void emul_machine_setup(struct machine *
1260                                  cpu->cd.mips.gpr[MIPS_GPR_GP] |=                                  cpu->cd.mips.gpr[MIPS_GPR_GP] |=
1261                                      0xffffffff00000000ULL;                                      0xffffffff00000000ULL;
1262                          break;                          break;
1263    
1264                  case ARCH_PPC:                  case ARCH_PPC:
1265                            /*  See http://www.linuxbase.org/spec/ELF/ppc64/
1266                                spec/x458.html for more info.  */
1267                          cpu->cd.ppc.gpr[2] = toc;                          cpu->cd.ppc.gpr[2] = toc;
1268                            /*  TODO  */
1269                            if (cpu->cd.ppc.bits == 32)
1270                                    cpu->pc &= 0xffffffffULL;
1271                          break;                          break;
1272                  case ARCH_SPARC:  
1273                    case ARCH_SH:
1274                            if (cpu->cd.sh.bits == 32)
1275                                    cpu->pc &= 0xffffffffULL;
1276                            cpu->pc &= ~1;
1277                          break;                          break;
1278                  case ARCH_URISC:  
1279                    case ARCH_SPARC:
1280                          break;                          break;
1281                  case ARCH_HPPA:  
1282                    case ARCH_TRANSPUTER:
1283                            cpu->pc &= 0xffffffffULL;
1284                          break;                          break;
1285                  case ARCH_ALPHA:  
1286                    case ARCH_X86:
1287                            /*
1288                             *  NOTE: The toc field is used to indicate an ELF32
1289                             *  or ELF64 load.
1290                             */
1291                            switch (toc) {
1292                            case 0: /*  16-bit? TODO  */
1293                                    cpu->pc &= 0xffffffffULL;
1294                                    break;
1295                            case 1: /*  32-bit.  */
1296                                    cpu->pc &= 0xffffffffULL;
1297                                    break;
1298                            case 2: /*  64-bit:  TODO  */
1299                                    fatal("64-bit x86 load. TODO\n");
1300                                    exit(1);
1301                            }
1302                          break;                          break;
1303    
1304                  default:                  default:
1305                          fatal("emul_machine_setup(): Internal error: "                          fatal("emul_machine_setup(): Internal error: "
1306                              "Unimplemented arch %i\n", m->arch);                              "Unimplemented arch %i\n", m->arch);
# Line 663  void emul_machine_setup(struct machine * Line 1349  void emul_machine_setup(struct machine *
1349          add_dump_points(m);          add_dump_points(m);
1350    
1351          /*  TODO: This is MIPS-specific!  */          /*  TODO: This is MIPS-specific!  */
1352          if (m->machine_type == MACHINE_DEC &&          if (m->machine_type == MACHINE_PMAX &&
1353              cpu->cd.mips.cpu_type.mmu_model == MMU3K)              cpu->cd.mips.cpu_type.mmu_model == MMU3K)
1354                  add_symbol_name(&m->symbol_context,                  add_symbol_name(&m->symbol_context,
1355                      0x9fff0000, 0x10000, "r2k3k_cache", 0);                      0x9fff0000, 0x10000, "r2k3k_cache", 0, 0);
1356    
1357          symbol_recalc_sizes(&m->symbol_context);          symbol_recalc_sizes(&m->symbol_context);
1358    
         if (m->max_random_cycles_per_chunk > 0)  
                 debug("using random cycle chunks (1 to %i cycles)\n",  
                     m->max_random_cycles_per_chunk);  
   
1359          /*  Special hack for ARC/SGI emulation:  */          /*  Special hack for ARC/SGI emulation:  */
1360          if ((m->machine_type == MACHINE_ARC ||          if ((m->machine_type == MACHINE_ARC ||
1361              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 1363  void emul_machine_setup(struct machine *
1363    
1364          debug("starting cpu%i at ", m->bootstrap_cpu);          debug("starting cpu%i at ", m->bootstrap_cpu);
1365          switch (m->arch) {          switch (m->arch) {
1366    
1367            case ARCH_ARM:
1368                    /*  ARM cpus aren't 64-bit:  */
1369                    debug("0x%08x", (int)entrypoint);
1370                    break;
1371    
1372            case ARCH_AVR:
1373                    /*  Atmel AVR uses a 16-bit or 22-bit program counter:  */
1374                    debug("0x%04x", (int)entrypoint);
1375                    break;
1376    
1377          case ARCH_MIPS:          case ARCH_MIPS:
1378                  if (cpu->cd.mips.cpu_type.isa_level < 3 ||                  if (cpu->is_32bit) {
                     cpu->cd.mips.cpu_type.isa_level == 32) {  
1379                          debug("0x%08x", (int)m->cpus[                          debug("0x%08x", (int)m->cpus[
1380                              m->bootstrap_cpu]->pc);                              m->bootstrap_cpu]->pc);
1381                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)
# Line 698  void emul_machine_setup(struct machine * Line 1390  void emul_machine_setup(struct machine *
1390                                      cpu->cd.mips.gpr[MIPS_GPR_GP]);                                      cpu->cd.mips.gpr[MIPS_GPR_GP]);
1391                  }                  }
1392                  break;                  break;
1393    
1394          case ARCH_PPC:          case ARCH_PPC:
1395                  if (cpu->cd.ppc.bits == 32)                  if (cpu->cd.ppc.bits == 32)
1396                          debug("0x%08x", (int)entrypoint);                          debug("0x%08x", (int)entrypoint);
1397                  else                  else
1398                          debug("0x%016llx", (long long)entrypoint);                          debug("0x%016llx", (long long)entrypoint);
1399                  break;                  break;
1400          case ARCH_URISC:  
1401                  {          case ARCH_X86:
1402                          char tmps[100];                  debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS],
1403                          unsigned char buf[sizeof(uint64_t)];                      (long long)cpu->pc);
   
                         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;  
                 }  
1404                  break;                  break;
1405    
1406          default:          default:
1407                  debug("0x%016llx", (long long)entrypoint);                  if (cpu->is_32bit)
1408                            debug("0x%08x", (int)cpu->pc);
1409                    else
1410                            debug("0x%016llx", (long long)cpu->pc);
1411          }          }
1412          debug("\n");          debug("\n");
1413    
# Line 743  void emul_machine_setup(struct machine * Line 1422  void emul_machine_setup(struct machine *
1422   */   */
1423  void emul_dumpinfo(struct emul *e)  void emul_dumpinfo(struct emul *e)
1424  {  {
1425          int j, nm, iadd = 4;          int j, nm, iadd = DEBUG_INDENTATION;
1426    
1427          if (e->net != NULL)          if (e->net != NULL)
1428                  net_dumpinfo(e->net);                  net_dumpinfo(e->net);
# Line 772  void emul_dumpinfo(struct emul *e) Line 1451  void emul_dumpinfo(struct emul *e)
1451   */   */
1452  void emul_simple_init(struct emul *emul)  void emul_simple_init(struct emul *emul)
1453  {  {
1454          int iadd=4;          int iadd = DEBUG_INDENTATION;
1455          struct machine *m;          struct machine *m;
1456    
1457          if (emul->n_machines != 1) {          if (emul->n_machines != 1) {
# Line 786  void emul_simple_init(struct emul *emul) Line 1465  void emul_simple_init(struct emul *emul)
1465                  debug("Simple setup...\n");                  debug("Simple setup...\n");
1466                  debug_indentation(iadd);                  debug_indentation(iadd);
1467    
1468                  /*  Create a network:  */                  /*  Create a simple network:  */
1469                  emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY,                  emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY,
1470                      "10.0.0.0", 8);                      "10.0.0.0", 8, NULL, 0, 0);
1471          } else {          } else {
1472                  /*  Userland pseudo-machine:  */                  /*  Userland pseudo-machine:  */
1473                  debug("Syscall emulation (userland-only) setup...\n");                  debug("Syscall emulation (userland-only) setup...\n");
# Line 809  void emul_simple_init(struct emul *emul) Line 1488  void emul_simple_init(struct emul *emul)
1488   */   */
1489  struct emul *emul_create_from_configfile(char *fname)  struct emul *emul_create_from_configfile(char *fname)
1490  {  {
1491          int iadd = 4;          int iadd = DEBUG_INDENTATION;
1492          struct emul *e = emul_new(fname);          struct emul *e = emul_new(fname);
         FILE *f;  
         char buf[128];  
         size_t len;  
1493    
1494          debug("Creating emulation from configfile \"%s\":\n", fname);          debug("Creating emulation from configfile \"%s\":\n", fname);
1495          debug_indentation(iadd);          debug_indentation(iadd);
1496    
1497          f = fopen(fname, "r");          emul_parse_config(e, fname);
         if (f == NULL) {  
                 perror(fname);  
                 exit(1);  
         }  
1498    
         /*  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);  
   
         emul_parse_config(e, f);  
   
         fclose(f);  
1499          debug_indentation(-iadd);          debug_indentation(-iadd);
1500          return e;          return e;
1501  }  }
# Line 863  void emul_run(struct emul **emuls, int n Line 1522  void emul_run(struct emul **emuls, int n
1522    
1523          atexit(fix_console);          atexit(fix_console);
1524    
         i = 79;  
         while (i-- > 0)  
                 debug("-");  
         debug("\n\n");  
   
1525          /*  Initialize the interactive debugger:  */          /*  Initialize the interactive debugger:  */
1526          debugger_init(emuls, n_emuls);          debugger_init(emuls, n_emuls);
1527    
1528            /*  Run any additional debugger commands before starting:  */
1529            for (i=0; i<n_emuls; i++) {
1530                    struct emul *emul = emuls[i];
1531                    if (emul->n_debugger_cmds > 0) {
1532                            int j;
1533                            if (i == 0)
1534                                    print_separator();
1535                            for (j = 0; j < emul->n_debugger_cmds; j ++) {
1536                                    debug("> %s\n", emul->debugger_cmds[j]);
1537                                    debugger_execute_cmd(emul->debugger_cmds[j],
1538                                        strlen(emul->debugger_cmds[j]));
1539                            }
1540                    }
1541            }
1542    
1543            print_separator();
1544            debug("\n");
1545    
1546    
1547          /*          /*
1548           *  console_init_main() makes sure that the terminal is in a           *  console_init_main() makes sure that the terminal is in a
1549           *  reasonable state.           *  reasonable state.
# Line 895  void emul_run(struct emul **emuls, int n Line 1568  void emul_run(struct emul **emuls, int n
1568                  if (e == NULL)                  if (e == NULL)
1569                          continue;                          continue;
1570                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
1571                          cpu_run_init(e, e->machines[j]);                          cpu_run_init(e->machines[j]);
1572          }          }
1573    
1574            /*  TODO: Generalize:  */
1575            if (emuls[0]->machines[0]->show_trace_tree)
1576                    cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0],
1577                        emuls[0]->machines[0]->cpus[0]->pc);
1578    
1579          /*          /*
1580           *  MAIN LOOP:           *  MAIN LOOP:
1581           *           *
# Line 907  void emul_run(struct emul **emuls, int n Line 1585  void emul_run(struct emul **emuls, int n
1585          while (go) {          while (go) {
1586                  go = 0;                  go = 0;
1587    
1588                  x11_check_event(emuls, n_emuls);                  /*  Flush X11 and serial console output every now and then:  */
1589                    if (emuls[0]->machines[0]->ninstrs >
1590                        emuls[0]->machines[0]->ninstrs_flush + (1<<19)) {
1591                            x11_check_event(emuls, n_emuls);
1592                            console_flush();
1593                            emuls[0]->machines[0]->ninstrs_flush =
1594                                emuls[0]->machines[0]->ninstrs;
1595                    }
1596    
1597                  for (i=0; i<n_emuls; i++) {                  if (emuls[0]->machines[0]->ninstrs >
1598                          e = emuls[i];                      emuls[0]->machines[0]->ninstrs_show + (1<<25)) {
1599                          if (e == NULL)                          emuls[0]->machines[0]->ninstrs_since_gettimeofday +=
1600                                  continue;                              (emuls[0]->machines[0]->ninstrs -
1601                                 emuls[0]->machines[0]->ninstrs_show);
1602                          for (j=0; j<e->n_machines; j++) {                          cpu_show_cycles(emuls[0]->machines[0], 0);
1603                                  /*  TODO: cpu_run() is a strange name, since                          emuls[0]->machines[0]->ninstrs_show =
1604                                      there can be multiple cpus in a machine  */                              emuls[0]->machines[0]->ninstrs;
1605                                  anything = cpu_run(e, e->machines[j]);                  }
1606                                  if (anything)  
1607                                          go = 1;                  if (single_step == ENTER_SINGLE_STEPPING) {
1608                          }                          /*  TODO: Cleanup!  */
1609                            old_instruction_trace =
1610                                emuls[0]->machines[0]->instruction_trace;
1611                            old_quiet_mode = quiet_mode;
1612                            old_show_trace_tree =
1613                                emuls[0]->machines[0]->show_trace_tree;
1614                            emuls[0]->machines[0]->instruction_trace = 1;
1615                            emuls[0]->machines[0]->show_trace_tree = 1;
1616                            quiet_mode = 0;
1617                            single_step = SINGLE_STEPPING;
1618                    }
1619    
1620                    if (single_step == SINGLE_STEPPING)
1621                            debugger();
1622    
1623                    e = emuls[0];   /*  Note: Only 1 emul supported now.  */
1624    
1625                    for (j=0; j<e->n_machines; j++) {
1626                            if (e->machines[j]->gdb.port > 0)
1627                                    debugger_gdb_check_incoming(e->machines[j]);
1628    
1629                            anything = machine_run(e->machines[j]);
1630                            if (anything)
1631                                    go = 1;
1632                  }                  }
1633          }          }
1634    
# Line 930  void emul_run(struct emul **emuls, int n Line 1638  void emul_run(struct emul **emuls, int n
1638                  if (e == NULL)                  if (e == NULL)
1639                          continue;                          continue;
1640                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
1641                          cpu_run_deinit(e, e->machines[j]);                          cpu_run_deinit(e->machines[j]);
1642          }          }
1643    
1644          /*  force_debugger_at_exit flag set? Then enter the debugger:  */          /*  force_debugger_at_exit flag set? Then enter the debugger:  */

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

  ViewVC Help
Powered by ViewVC 1.1.26