/[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 10 by dpavlin, Mon Oct 8 16:18:27 2007 UTC revision 20 by dpavlin, Mon Oct 8 16:19:23 2007 UTC
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: emul.c,v 1.211 2005/06/26 11:36:28 debug Exp $   *  $Id: emul.c,v 1.238 2005/11/19 18:53:06 debug Exp $
29   *   *
30   *  Emulation startup and misc. routines.   *  Emulation startup and misc. routines.
31   */   */
# Line 112  static void add_dump_points(struct machi Line 112  static void add_dump_points(struct machi
112                   *  were automatically converted into the correct address.                   *  were automatically converted into the correct address.
113                   */                   */
114    
115                  if ((dp >> 32) == 0 && ((dp >> 31) & 1))                  if (m->arch == ARCH_MIPS) {
116                          dp |= 0xffffffff00000000ULL;                          if ((dp >> 32) == 0 && ((dp >> 31) & 1))
117                                    dp |= 0xffffffff00000000ULL;
118                    }
119    
120                  m->breakpoint_addr[i] = dp;                  m->breakpoint_addr[i] = dp;
121    
122                  debug("breakpoint %i: 0x%016llx", i, (long long)dp);                  debug("breakpoint %i: 0x%llx", i, (long long)dp);
123                  if (string_flag)                  if (string_flag)
124                          debug(" (%s)", m->breakpoint_string[i]);                          debug(" (%s)", m->breakpoint_string[i]);
125                  debug("\n");                  debug("\n");
# Line 435  ret: Line 438  ret:
438    
439    
440  /*  /*
441     *  apple_load_bootblock():
442     *
443     *  Try to load a kernel from a disk image with an Apple Partition Table.
444     *
445     *  TODO: This function uses too many magic offsets and so on; it should be
446     *  cleaned up some day. See http://www.awprofessional.com/articles/
447     *      article.asp?p=376123&seqNum=3&rl=1  for some info on the Apple
448     *  partition format.
449     *
450     *  Returns 1 on success, 0 on failure.
451     */
452    static int apple_load_bootblock(struct machine *m, struct cpu *cpu,
453            int disk_id, int disk_type, int *n_loadp, char ***load_namesp)
454    {
455            unsigned char buf[0x8000];
456            int res, partnr, n_partitions = 0, n_hfs_partitions = 0;
457            uint64_t hfs_start, hfs_length;
458    
459            res = diskimage_access(m, disk_id, disk_type, 0, 0x0, buf, sizeof(buf));
460            if (!res) {
461                    fatal("apple_load_bootblock: couldn't read the disk "
462                        "image. Aborting.\n");
463                    return 0;
464            }
465    
466            partnr = 0;
467            do {
468                    int start, length;
469                    int ofs = 0x200 * (partnr + 1);
470                    if (partnr == 0)
471                            n_partitions = buf[ofs + 7];
472                    start = (buf[ofs + 8] << 24) + (buf[ofs + 9] << 16) +
473                        (buf[ofs + 10] << 8) + buf[ofs + 11];
474                    length = (buf[ofs + 12] << 24) + (buf[ofs + 13] << 16) +
475                        (buf[ofs + 14] << 8) + buf[ofs + 15];
476    
477                    debug("partition %i: '%s', type '%s', start %i, length %i\n",
478                        partnr, buf + ofs + 0x10, buf + ofs + 0x30,
479                        start, length);
480    
481                    if (strcmp((char *)buf + ofs + 0x30, "Apple_HFS") == 0) {
482                            n_hfs_partitions ++;
483                            hfs_start = 512 * start;
484                            hfs_length = 512 * length;
485                    }
486    
487                    /*  Any more partitions?  */
488                    partnr ++;
489            } while (partnr < n_partitions);
490    
491            if (n_hfs_partitions == 0) {
492                    fatal("Error: No HFS partition found! TODO\n");
493                    return 0;
494            }
495            if (n_hfs_partitions >= 2) {
496                    fatal("Error: Too many HFS partitions found! TODO\n");
497                    return 0;
498            }
499    
500            return 0;
501    }
502    
503    
504    /*
505   *  load_bootblock():   *  load_bootblock():
506   *   *
507   *  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
# Line 586  static int load_bootblock(struct machine Line 653  static int load_bootblock(struct machine
653    
654          /*          /*
655           *  Try reading a kernel manually from the disk. The code here           *  Try reading a kernel manually from the disk. The code here
656           *  does not rely on machine-dependant boot blocks etc.           *  does not rely on machine-dependent boot blocks etc.
657           */           */
658          /*  ISO9660: (0x800 bytes at 0x8000)  */          /*  ISO9660: (0x800 bytes at 0x8000)  */
659          bootblock_buf = malloc(0x800);          bootblock_buf = malloc(0x800);
# Line 623  static int load_bootblock(struct machine Line 690  static int load_bootblock(struct machine
690                              n_loadp, load_namesp);                              n_loadp, load_namesp);
691          }          }
692    
693            if (retval != 0)
694                    goto ret_ok;
695    
696            /*  Apple parition table:  */
697            res = diskimage_access(m, boot_disk_id, boot_disk_type,
698                0, 0x0, bootblock_buf, 0x800);
699            if (!res) {
700                    fatal("Couldn't read the disk image. Aborting.\n");
701                    return 0;
702            }
703            if (bootblock_buf[0x000] == 'E' && bootblock_buf[0x001] == 'R' &&
704                bootblock_buf[0x200] == 'P' && bootblock_buf[0x201] == 'M') {
705                    /*  We can't load a kernel if the name
706                        isn't specified.  */
707                    if (cpu->machine->boot_kernel_filename == NULL ||
708                        cpu->machine->boot_kernel_filename[0] == '\0')
709                            fatal("\nApple partition table, but no kernel "
710                                "specified? (Use the -j option.)\n");
711                    else
712                            retval = apple_load_bootblock(m, cpu, boot_disk_id,
713                                boot_disk_type, n_loadp, load_namesp);
714            }
715    
716    ret_ok:
717          free(bootblock_buf);          free(bootblock_buf);
718          return retval;          return retval;
719  }  }
# Line 709  static void add_arc_components(struct ma Line 800  static void add_arc_components(struct ma
800    
801          len += 1048576 * m->memory_offset_in_mb;          len += 1048576 * m->memory_offset_in_mb;
802    
803          /*  NOTE/TODO: magic 12MB end of load program area  */          /*
804             *  NOTE/TODO: magic 12MB end of load program area
805             *
806             *  Hm. This breaks the old FreeBSD/MIPS snapshots...
807             */
808    #if 0
809          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
810              0x60000 + m->memory_offset_in_mb * 1048576,              0x60000 + m->memory_offset_in_mb * 1048576,
811              start-0x60000 - m->memory_offset_in_mb * 1048576,              start-0x60000 - m->memory_offset_in_mb * 1048576,
812              ARCBIOS_MEM_FreeMemory);              ARCBIOS_MEM_FreeMemory);
813    #endif
814          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
815              start, len, ARCBIOS_MEM_LoadedProgram);              start, len, ARCBIOS_MEM_LoadedProgram);
816    
# Line 834  void emul_machine_setup(struct machine * Line 931  void emul_machine_setup(struct machine *
931    
932          m->cpu_family = cpu_family_ptr_by_number(m->arch);          m->cpu_family = cpu_family_ptr_by_number(m->arch);
933    
934            if (m->arch == ARCH_ALPHA)
935                    m->arch_pagesize = 8192;
936    
937          if (m->arch != ARCH_MIPS)          if (m->arch != ARCH_MIPS)
938                  m->bintrans_enable = 0;                  m->bintrans_enable = 0;
939    
# Line 857  void emul_machine_setup(struct machine * Line 957  void emul_machine_setup(struct machine *
957                  debug(" (offset by %iMB)", m->memory_offset_in_mb);                  debug(" (offset by %iMB)", m->memory_offset_in_mb);
958                  memory_amount += 1048576 * m->memory_offset_in_mb;                  memory_amount += 1048576 * m->memory_offset_in_mb;
959          }          }
960          m->memory = memory_new(memory_amount);          m->memory = memory_new(memory_amount, m->arch);
961          if (m->machine_type != MACHINE_USERLAND)          if (m->machine_type != MACHINE_USERLAND)
962                  debug("\n");                  debug("\n");
963    
# Line 924  void emul_machine_setup(struct machine * Line 1024  void emul_machine_setup(struct machine *
1024          if (m->userland_emul != NULL) {          if (m->userland_emul != NULL) {
1025                  useremul_name_to_useremul(cpu,                  useremul_name_to_useremul(cpu,
1026                      m->userland_emul, NULL, NULL, NULL);                      m->userland_emul, NULL, NULL, NULL);
1027                  cpu->memory_rw = userland_memory_rw;  
1028                    switch (m->arch) {
1029    #ifdef ENABLE_ALPHA
1030                    case ARCH_ALPHA:
1031                            cpu->memory_rw = alpha_userland_memory_rw;
1032                            break;
1033    #endif
1034                    default:cpu->memory_rw = userland_memory_rw;
1035                    }
1036          }          }
1037    
1038          if (m->use_x11)          if (m->use_x11)
# Line 1103  void emul_machine_setup(struct machine * Line 1211  void emul_machine_setup(struct machine *
1211                  cpu->pc = entrypoint;                  cpu->pc = entrypoint;
1212    
1213                  switch (m->arch) {                  switch (m->arch) {
1214    
1215                    case ARCH_ALPHA:
1216                            /*  For position-independent code:  */
1217                            cpu->cd.alpha.r[ALPHA_T12] = cpu->pc;
1218                            break;
1219    
1220                    case ARCH_ARM:
1221                            if (cpu->pc & 3) {
1222                                    fatal("ARM: lowest bits of pc set: TODO\n");
1223                                    exit(1);
1224                            }
1225                            cpu->pc &= 0xfffffffc;
1226                            break;
1227    
1228                    case ARCH_AVR:
1229                            cpu->pc &= 0xfffff;
1230                            if (cpu->pc & 1) {
1231                                    fatal("AVR: lowest bit of pc set: TODO\n");
1232                                    exit(1);
1233                            }
1234                            break;
1235    
1236                    case ARCH_HPPA:
1237                            break;
1238    
1239                    case ARCH_I960:
1240                            break;
1241    
1242                    case ARCH_IA64:
1243                            break;
1244    
1245                    case ARCH_M68K:
1246                            break;
1247    
1248                  case ARCH_MIPS:                  case ARCH_MIPS:
1249                          if ((cpu->pc >> 32) == 0                          if ((cpu->pc >> 32) == 0 && (cpu->pc & 0x80000000ULL))
                             && (cpu->pc & 0x80000000ULL))  
1250                                  cpu->pc |= 0xffffffff00000000ULL;                                  cpu->pc |= 0xffffffff00000000ULL;
1251    
1252                          cpu->cd.mips.gpr[MIPS_GPR_GP] = gp;                          cpu->cd.mips.gpr[MIPS_GPR_GP] = gp;
# Line 1121  void emul_machine_setup(struct machine * Line 1262  void emul_machine_setup(struct machine *
1262                              spec/x458.html for more info.  */                              spec/x458.html for more info.  */
1263                          cpu->cd.ppc.gpr[2] = toc;                          cpu->cd.ppc.gpr[2] = toc;
1264                          /*  TODO  */                          /*  TODO  */
1265                            if (cpu->cd.ppc.bits == 32)
1266                                    cpu->pc &= 0xffffffffULL;
1267                          break;                          break;
1268    
1269                  case ARCH_ALPHA:                  case ARCH_SH:
1270                  case ARCH_HPPA:                          if (cpu->cd.sh.bits == 32)
1271                  case ARCH_SPARC:                                  cpu->pc &= 0xffffffffULL;
1272                  case ARCH_URISC:                          cpu->pc &= ~1;
1273                          break;                          break;
1274    
1275                  case ARCH_ARM:                  case ARCH_SPARC:
                         cpu->pc &= 0xfffffffc;  
                         cpu->cd.arm.r[ARM_PC] = cpu->pc;  
1276                          break;                          break;
1277    
1278                  case ARCH_X86:                  case ARCH_X86:
# Line 1203  void emul_machine_setup(struct machine * Line 1344  void emul_machine_setup(struct machine *
1344          if (m->machine_type == MACHINE_DEC &&          if (m->machine_type == MACHINE_DEC &&
1345              cpu->cd.mips.cpu_type.mmu_model == MMU3K)              cpu->cd.mips.cpu_type.mmu_model == MMU3K)
1346                  add_symbol_name(&m->symbol_context,                  add_symbol_name(&m->symbol_context,
1347                      0x9fff0000, 0x10000, "r2k3k_cache", 0);                      0x9fff0000, 0x10000, "r2k3k_cache", 0, 0);
1348    
1349          symbol_recalc_sizes(&m->symbol_context);          symbol_recalc_sizes(&m->symbol_context);
1350    
# Line 1216  void emul_machine_setup(struct machine * Line 1357  void emul_machine_setup(struct machine *
1357              m->machine_type == MACHINE_SGI) && m->prom_emulation)              m->machine_type == MACHINE_SGI) && m->prom_emulation)
1358                  add_arc_components(m);                  add_arc_components(m);
1359    
1360    
1361    #if 0
1362    if (m->machine_type == MACHINE_IQ80321) {
1363            store_32bit_word(cpu, 0xc0200000, 0);
1364            store_32bit_word(cpu, 0xc0200004, 0xd0000000);
1365    }
1366    #endif
1367    
1368    
1369          debug("starting cpu%i at ", m->bootstrap_cpu);          debug("starting cpu%i at ", m->bootstrap_cpu);
1370          switch (m->arch) {          switch (m->arch) {
1371    
1372            case ARCH_ARM:
1373                    /*  ARM cpus aren't 64-bit:  */
1374                    debug("0x%08x", (int)entrypoint);
1375                    break;
1376    
1377            case ARCH_AVR:
1378                    /*  Atmel AVR uses a 16-bit or 22-bit program counter:  */
1379                    debug("0x%04x", (int)entrypoint);
1380                    break;
1381    
1382          case ARCH_MIPS:          case ARCH_MIPS:
1383                  if (cpu->cd.mips.cpu_type.isa_level < 3 ||                  if (cpu->is_32bit) {
                     cpu->cd.mips.cpu_type.isa_level == 32) {  
1384                          debug("0x%08x", (int)m->cpus[                          debug("0x%08x", (int)m->cpus[
1385                              m->bootstrap_cpu]->pc);                              m->bootstrap_cpu]->pc);
1386                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)
# Line 1235  void emul_machine_setup(struct machine * Line 1395  void emul_machine_setup(struct machine *
1395                                      cpu->cd.mips.gpr[MIPS_GPR_GP]);                                      cpu->cd.mips.gpr[MIPS_GPR_GP]);
1396                  }                  }
1397                  break;                  break;
1398    
1399          case ARCH_PPC:          case ARCH_PPC:
1400                  if (cpu->cd.ppc.bits == 32)                  if (cpu->cd.ppc.bits == 32)
1401                          debug("0x%08x", (int)entrypoint);                          debug("0x%08x", (int)entrypoint);
1402                  else                  else
1403                          debug("0x%016llx", (long long)entrypoint);                          debug("0x%016llx", (long long)entrypoint);
1404                  break;                  break;
         case ARCH_ARM:  
                 /*  ARM cpus aren't 64-bit:  */  
                 debug("0x%08x", (int)entrypoint);  
                 break;  
         case ARCH_URISC:  
                 {  
                         char tmps[100];  
                         unsigned char buf[sizeof(uint64_t)];  
   
                         cpu->memory_rw(cpu, m->memory, 0, buf, sizeof(buf),  
                             MEM_READ, CACHE_NONE | NO_EXCEPTIONS);  
   
                         entrypoint = 0;  
                         for (i=0; i<cpu->cd.urisc.wordlen/8; i++) {  
                                 entrypoint <<= 8;  
                                 if (cpu->byte_order == EMUL_BIG_ENDIAN)  
                                         entrypoint += buf[i];  
                                 else  
                                         entrypoint += buf[cpu->  
                                             cd.urisc.wordlen/8 - 1 - i];  
                         }  
1405    
                         snprintf(tmps, sizeof(tmps), "0x%%0%illx",  
                             cpu->cd.urisc.wordlen / 4);  
                         debug(tmps, (long long)entrypoint);  
                         cpu->pc = entrypoint;  
                 }  
                 break;  
1406          case ARCH_X86:          case ARCH_X86:
1407                  debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS],                  debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS],
1408                      (long long)cpu->pc);                      (long long)cpu->pc);
1409                  break;                  break;
1410    
1411          default:          default:
1412                  debug("0x%016llx", (long long)cpu->pc);                  debug("0x%016llx", (long long)cpu->pc);
1413          }          }
# Line 1441  void emul_run(struct emul **emuls, int n Line 1576  void emul_run(struct emul **emuls, int n
1576                  if (e == NULL)                  if (e == NULL)
1577                          continue;                          continue;
1578                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
1579                          cpu_run_init(e, e->machines[j]);                          cpu_run_init(e->machines[j]);
1580          }          }
1581    
1582            /*  TODO: Generalize:  */
1583            if (emuls[0]->machines[0]->show_trace_tree)
1584                    cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0],
1585                        emuls[0]->machines[0]->cpus[0]->pc);
1586    
1587          /*          /*
1588           *  MAIN LOOP:           *  MAIN LOOP:
1589           *           *
# Line 1476  void emul_run(struct emul **emuls, int n Line 1616  void emul_run(struct emul **emuls, int n
1616                  if (e == NULL)                  if (e == NULL)
1617                          continue;                          continue;
1618                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
1619                          cpu_run_deinit(e, e->machines[j]);                          cpu_run_deinit(e->machines[j]);
1620          }          }
1621    
1622          /*  force_debugger_at_exit flag set? Then enter the debugger:  */          /*  force_debugger_at_exit flag set? Then enter the debugger:  */

Legend:
Removed from v.10  
changed lines
  Added in v.20

  ViewVC Help
Powered by ViewVC 1.1.26