/[gxemul]/trunk/src/cpu.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/cpu.c

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

revision 22 by dpavlin, Mon Oct 8 16:19:37 2007 UTC revision 24 by dpavlin, Mon Oct 8 16:19:56 2007 UTC
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: cpu.c,v 1.329 2006/01/16 04:48:08 debug Exp $   *  $Id: cpu.c,v 1.342 2006/06/22 13:27:03 debug Exp $
29   *   *
30   *  Common routines for CPU emulation. (Not specific to any CPU type.)   *  Common routines for CPU emulation. (Not specific to any CPU type.)
31   */   */
# Line 43  Line 43 
43    
44    
45  extern int quiet_mode;  extern int quiet_mode;
 extern int show_opcode_statistics;  
 extern int dyntrans_backend_enable;  
46    
47  static struct cpu_family *first_cpu_family = NULL;  static struct cpu_family *first_cpu_family = NULL;
48    
# Line 106  struct cpu *cpu_new(struct memory *mem, Line 104  struct cpu *cpu_new(struct memory *mem,
104          }          }
105    
106          fatal("\ncpu_new(): unknown cpu type '%s'\n", cpu_type_name);          fatal("\ncpu_new(): unknown cpu type '%s'\n", cpu_type_name);
107          exit(1);          return NULL;
 }  
   
   
 /*  
  *  cpu_show_full_statistics():  
  *  
  *  Show detailed statistics on opcode usage on each cpu.  
  */  
 void cpu_show_full_statistics(struct machine *m)  
 {  
         if (m->cpu_family == NULL ||  
             m->cpu_family->show_full_statistics == NULL)  
                 fatal("cpu_show_full_statistics(): NULL\n");  
         else  
                 m->cpu_family->show_full_statistics(m);  
108  }  }
109    
110    
# Line 166  void cpu_register_match(struct machine * Line 149  void cpu_register_match(struct machine *
149   *  tracing.   *  tracing.
150   */   */
151  int cpu_disassemble_instr(struct machine *m, struct cpu *cpu,  int cpu_disassemble_instr(struct machine *m, struct cpu *cpu,
152          unsigned char *instr, int running, uint64_t addr, int bintrans)          unsigned char *instr, int running, uint64_t addr)
153  {  {
154          if (m->cpu_family == NULL || m->cpu_family->disassemble_instr == NULL) {          if (m->cpu_family == NULL || m->cpu_family->disassemble_instr == NULL) {
155                  fatal("cpu_disassemble_instr(): NULL\n");                  fatal("cpu_disassemble_instr(): NULL\n");
156                  return 0;                  return 0;
157          } else          } else
158                  return m->cpu_family->disassemble_instr(cpu, instr,                  return m->cpu_family->disassemble_instr(cpu, instr,
159                      running, addr, bintrans);                      running, addr);
160  }  }
161    
162    
# Line 196  void cpu_register_dump(struct machine *m Line 179  void cpu_register_dump(struct machine *m
179    
180    
181  /*  /*
182     *  cpu_gdb_stub():
183     *
184     *  Execute a "remote GDB" command. Return value is a pointer to a newly
185     *  allocated response string, if the command was successfully executed. If
186     *  there was an error, NULL is returned.
187     */
188    char *cpu_gdb_stub(struct cpu *cpu, char *cmd)
189    {
190            if (cpu->machine->cpu_family == NULL ||
191                cpu->machine->cpu_family->gdb_stub == NULL) {
192                    fatal("cpu_gdb_stub(): NULL\n");
193                    return NULL;
194            } else
195                    return cpu->machine->cpu_family->gdb_stub(cpu, cmd);
196    }
197    
198    
199    /*
200   *  cpu_interrupt():   *  cpu_interrupt():
201   *   *
202   *  Assert an interrupt.   *  Assert an interrupt.
# Line 257  void cpu_functioncall_trace(struct cpu * Line 258  void cpu_functioncall_trace(struct cpu *
258                  fatal("%s", symbol);                  fatal("%s", symbol);
259          else {          else {
260                  if (cpu->is_32bit)                  if (cpu->is_32bit)
261                          fatal("0x%08x", (int)f);                          fatal("0x%"PRIx32, (uint32_t) f);
262                  else                  else
263                          fatal("0x%llx", (long long)f);                          fatal("0x%"PRIx64, (uint64_t) f);
264          }          }
265          fatal("(");          fatal("(");
266    
# Line 267  void cpu_functioncall_trace(struct cpu * Line 268  void cpu_functioncall_trace(struct cpu *
268                  cpu->machine->cpu_family->functioncall_trace(cpu, f, n_args);                  cpu->machine->cpu_family->functioncall_trace(cpu, f, n_args);
269    
270          fatal(")>\n");          fatal(")>\n");
271    
272    #ifdef PRINT_MEMORY_CHECKSUM
273            /*  Temporary hack for finding bugs:  */
274            fatal("call chksum=%016"PRIx64"\n", memory_checksum(cpu->mem));
275    #endif
276  }  }
277    
278    
# Line 297  void cpu_create_or_reset_tc(struct cpu * Line 303  void cpu_create_or_reset_tc(struct cpu *
303  {  {
304          size_t s = DYNTRANS_CACHE_SIZE + DYNTRANS_CACHE_MARGIN;          size_t s = DYNTRANS_CACHE_SIZE + DYNTRANS_CACHE_MARGIN;
305    
306          if (cpu->translation_cache == NULL) {          if (cpu->translation_cache == NULL)
307  #ifdef DYNTRANS_BACKEND                  cpu->translation_cache = zeroed_alloc(s);
                 if (dyntrans_backend_enable) {  
                         cpu->translation_cache = (unsigned char *) mmap(NULL,  
                             s, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON |  
                             MAP_PRIVATE, -1, 0);  
                         if (cpu->translation_cache == NULL) {  
                                 dyntrans_backend_enable = 0;  
                                 fatal("%\n%  WARNING! Dyntrans backend disabled"  
                                     ", because mmap() failed.\n%\n");  
                         }  
                 }  
 #endif  
                 if (cpu->translation_cache == NULL)  
                         cpu->translation_cache = zeroed_alloc(s);  
         }  
308    
309          /*  Create an empty table at the beginning of the translation cache:  */          /*  Create an empty table at the beginning of the translation cache:  */
310          memset(cpu->translation_cache, 0, sizeof(uint32_t)          memset(cpu->translation_cache, 0, sizeof(uint32_t)
# Line 425  void cpu_run_deinit(struct machine *mach Line 417  void cpu_run_deinit(struct machine *mach
417          if (machine->show_nr_of_instructions || !quiet_mode)          if (machine->show_nr_of_instructions || !quiet_mode)
418                  cpu_show_cycles(machine, 1);                  cpu_show_cycles(machine, 1);
419    
         if (show_opcode_statistics)  
                 cpu_show_full_statistics(machine);  
   
420          fflush(stdout);          fflush(stdout);
421  }  }
422    
# Line 446  void cpu_show_cycles(struct machine *mac Line 435  void cpu_show_cycles(struct machine *mac
435          char *symbol;          char *symbol;
436          int64_t mseconds, ninstrs, is, avg;          int64_t mseconds, ninstrs, is, avg;
437          struct timeval tv;          struct timeval tv;
438          int h, m, s, ms, d, instrs_per_cycle = 1;          int h, m, s, ms, d;
439    
440          static int64_t mseconds_last = 0;          static int64_t mseconds_last = 0;
441          static int64_t ninstrs_last = -1;          static int64_t ninstrs_last = -1;
442    
         switch (machine->arch) {  
         case ARCH_MIPS:  
                 instrs_per_cycle = machine->cpus[machine->bootstrap_cpu]->  
                     cd.mips.cpu_type.instrs_per_cycle;  
                 break;  
         }  
   
443          pc = machine->cpus[machine->bootstrap_cpu]->pc;          pc = machine->cpus[machine->bootstrap_cpu]->pc;
444    
445          gettimeofday(&tv, NULL);          gettimeofday(&tv, NULL);
# Line 470  void cpu_show_cycles(struct machine *mac Line 452  void cpu_show_cycles(struct machine *mac
452          if (mseconds - mseconds_last == 0)          if (mseconds - mseconds_last == 0)
453                  mseconds ++;                  mseconds ++;
454    
455          ninstrs = machine->ncycles_since_gettimeofday * instrs_per_cycle;          ninstrs = machine->ncycles_since_gettimeofday;
456    
457          if (machine->automatic_clock_adjustment) {          if (machine->automatic_clock_adjustment) {
458                  static int first_adjustment = 1;                  static int first_adjustment = 1;
459    
460                  /*  Current nr of cycles per second:  */                  /*  Current nr of cycles per second:  */
461                  int64_t cur_cycles_per_second = 1000 *                  int64_t cur_cycles_per_second = 1000 *
462                      (ninstrs-ninstrs_last) / (mseconds-mseconds_last)                      (ninstrs-ninstrs_last) / (mseconds-mseconds_last);
463                      / instrs_per_cycle;  
464                    /*  fatal("[ CYCLES PER SECOND = %"PRIi64" ]\n",
465                        cur_cycles_per_second);  */
466    
467                  if (cur_cycles_per_second < 1000000)                  if (cur_cycles_per_second < 1000000)
468                          cur_cycles_per_second = 1000000;                          cur_cycles_per_second = 1000000;
# Line 491  void cpu_show_cycles(struct machine *mac Line 475  void cpu_show_cycles(struct machine *mac
475                              cur_cycles_per_second) / 16;                              cur_cycles_per_second) / 16;
476                  }                  }
477    
478                  /*  debug("[ updating emulated_hz to %lli Hz ]\n",                  /*  fatal("[ updating emulated_hz to %"PRIi64" Hz ]\n",
479                      (long long)machine->emulated_hz);  */                      machine->emulated_hz);  */
480          }          }
481    
482    
# Line 500  void cpu_show_cycles(struct machine *mac Line 484  void cpu_show_cycles(struct machine *mac
484          if (!machine->show_nr_of_instructions && !forced)          if (!machine->show_nr_of_instructions && !forced)
485                  goto do_return;                  goto do_return;
486    
487          printf("[ %lli instrs",          printf("[ %"PRIi64" instrs", (int64_t)machine->ncycles);
             (long long)(machine->ncycles * instrs_per_cycle));  
488    
489          if (!machine->automatic_clock_adjustment) {          if (!machine->automatic_clock_adjustment) {
490                  d = machine->emulated_hz / 1000;                  d = machine->emulated_hz / 1000;
# Line 525  void cpu_show_cycles(struct machine *mac Line 508  void cpu_show_cycles(struct machine *mac
508                  is = 0;                  is = 0;
509          if (avg < 0)          if (avg < 0)
510                  avg = 0;                  avg = 0;
511          printf("; i/s=%lli avg=%lli", (long long)is, (long long)avg);          printf("; i/s=%"PRIi64" avg=%"PRIi64, is, avg);
512    
513          symbol = get_symbol_name(&machine->symbol_context, pc, &offset);          symbol = get_symbol_name(&machine->symbol_context, pc, &offset);
514    
515          if (machine->ncpus == 1) {          if (machine->ncpus == 1) {
516                  if (machine->cpus[machine->bootstrap_cpu]->is_32bit)                  if (machine->cpus[machine->bootstrap_cpu]->is_32bit)
517                          printf("; pc=0x%08x", (int)pc);                          printf("; pc=0x%08"PRIx32, (uint32_t) pc);
518                  else                  else
519                          printf("; pc=0x%016llx", (long long)pc);                          printf("; pc=0x%016"PRIx64, (uint64_t) pc);
520          }          }
521    
522          if (symbol != NULL)          if (symbol != NULL)
# Line 577  void cpu_run_init(struct machine *machin Line 560  void cpu_run_init(struct machine *machin
560          if (machine->a_few_cycles < 1)          if (machine->a_few_cycles < 1)
561                  machine->a_few_cycles = 1;                  machine->a_few_cycles = 1;
562    
563          if (ncpus > 1 && machine->max_random_cycles_per_chunk == 0)          if (ncpus > 1)
564                  machine->a_few_cycles = 1;                  machine->a_few_cycles = 1;
565    
566          /*  debug("cpu_run_init(): a_few_cycles = %i\n",          /*  debug("cpu_run_init(): a_few_cycles = %i\n",
# Line 654  struct cpu_family *cpu_family_ptr_by_num Line 637  struct cpu_family *cpu_family_ptr_by_num
637  }  }
638    
639    
 #ifdef DYNTRANS_BACKEND  
 /*  
  *  cpu_dtb_add_fixup():  
  *  
  *  Add a fixup entry to a currently ongoing dyntrans backend translation.  
  */  
 void cpu_dtb_add_fixup(struct cpu *cpu, int type, void *addr, size_t data)  
 {  
         struct dtb_fixup *fixup = malloc(sizeof (struct dtb_fixup));  
         if (fixup == NULL) {  
                 fprintf(stderr, "out of memory\n"),  
                 exit(1);  
         }  
   
         /*  memset(fixup, 0, sizeof(struct dtb_fixup));  */  
   
         fixup->next = cpu->translation_context.fixups;  
         cpu->translation_context.fixups = fixup;  
   
         fixup->type = type;  
         fixup->addr = addr;  
         fixup->data = data;  
   
         /*  printf("{ fixup added: host addr %p, data=%p }\n", addr,  
             (void *)data);  */  
 }  
   
   
 /*  
  *  cpu_dtb_do_fixups():  
  *  
  *  This function should be called when a chunk of code has been translated,  
  *  and post-fixup is to be applied (i.e. add data which for some reason was  
  *  not included in the generated code).  
  *  
  *  If no fixup is necessary for a specific host platform, then it still needs  
  *  an empty do_fixups routine here (just set done = 1).  
  */  
 void cpu_dtb_do_fixups(struct cpu *cpu)  
 {  
         for (;;) {  
                 int done = 0;  
                 size_t omit_addr;  
   
                 struct dtb_fixup *fixup = cpu->translation_context.fixups;  
                 if (fixup == NULL)  
                         break;  
   
                 cpu->translation_context.fixups = fixup->next;  
   
 #ifdef DYNTRANS_BACKEND_ALPHA  
                 /*  Add the data at the end of the new translation:  */  
 /*printf("%p %p\n", fixup->addr, fixup->data);*/  
                 omit_addr = (size_t)cpu->translation_context.p -  
                     (size_t)cpu->translation_context.translation_buffer;  
 /*printf("omit_addr = %016llx\n", (long long)omit_addr);*/  
                 omit_addr = ((omit_addr - 1) | (sizeof(uint64_t) - 1)) + 1;  
 /*printf("omit_addr = %016llx\n", (long long)omit_addr);*/  
                 {  
                         uint64_t *x = (void *)(omit_addr + (size_t)cpu->  
                             translation_context.translation_buffer);  
                         uint32_t *fixup_instr = (void *)fixup->addr;  
                         size_t ofs = omit_addr;  
                         if (ofs > 0x7fff) {  
                                 fatal("Alpha fixup > 0x7fff!\n");  
                                 exit(1);  
                         }  
                         *x = fixup->data;  
 /*printf("orig instr = 0x%08x\n", *fixup_instr);*/  
                         (*fixup_instr) &= ~0xffff;  
                         (*fixup_instr) |= ofs;  
 /*printf("new instr = 0x%08x\n", *fixup_instr);*/  
                 }  
                 omit_addr += sizeof(uint64_t);  
                 cpu->translation_context.p = (void *)  
                     ((size_t)cpu->translation_context.translation_buffer  
                     + omit_addr);  
                 done = 1;  
 #endif  /*  DYNTRANS_BACKEND_ALPHA  */  
   
                 if (!done)  
                         fatal("!!! cpu_dtb_do_fixups() not implemented yet"  
                             " for this host architecture!\n");  
         }  
 }  
   
 #endif  /*  DYNTRANS_BACKEND  */  
   
   
640  /*  /*
641   *  cpu_init():   *  cpu_init():
642   *   *

Legend:
Removed from v.22  
changed lines
  Added in v.24

  ViewVC Help
Powered by ViewVC 1.1.26