/[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 30 by dpavlin, Mon Oct 8 16:20:40 2007 UTC revision 38 by dpavlin, Mon Oct 8 16:21:53 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2005-2006  Anders Gavare.  All rights reserved.   *  Copyright (C) 2005-2007  Anders Gavare.  All rights reserved.
3   *   *
4   *  Redistribution and use in source and binary forms, with or without   *  Redistribution and use in source and binary forms, with or without
5   *  modification, are permitted provided that the following conditions are met:   *  modification, are permitted provided that the following conditions are met:
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: cpu.c,v 1.349 2006/07/26 23:21:47 debug Exp $   *  $Id: cpu.c,v 1.374 2007/04/10 17:26:19 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 40  Line 40 
40  #include "machine.h"  #include "machine.h"
41  #include "memory.h"  #include "memory.h"
42  #include "misc.h"  #include "misc.h"
43    #include "settings.h"
44    
45    
46    extern size_t dyntrans_cache_size;
47    extern int native_code_translation_enabled;
48    
49  static struct cpu_family *first_cpu_family = NULL;  static struct cpu_family *first_cpu_family = NULL;
50    
51    
# Line 50  static struct cpu_family *first_cpu_fami Line 54  static struct cpu_family *first_cpu_fami
54   *   *
55   *  Create a new cpu object.  Each family is tried in sequence until a   *  Create a new cpu object.  Each family is tried in sequence until a
56   *  CPU family recognizes the cpu_type_name.   *  CPU family recognizes the cpu_type_name.
57     *
58     *  If there was no match, NULL is returned. Otherwise, a pointer to an
59     *  initialized cpu struct is returned.
60   */   */
61  struct cpu *cpu_new(struct memory *mem, struct machine *machine,  struct cpu *cpu_new(struct memory *mem, struct machine *machine,
62          int cpu_id, char *name)          int cpu_id, char *name)
# Line 57  struct cpu *cpu_new(struct memory *mem, Line 64  struct cpu *cpu_new(struct memory *mem,
64          struct cpu *cpu;          struct cpu *cpu;
65          struct cpu_family *fp;          struct cpu_family *fp;
66          char *cpu_type_name;          char *cpu_type_name;
67            char tmpstr[30];
68    
69          if (name == NULL) {          if (name == NULL) {
70                  fprintf(stderr, "cpu_new(): cpu name = NULL?\n");                  fprintf(stderr, "cpu_new(): cpu name = NULL?\n");
# Line 71  struct cpu *cpu_new(struct memory *mem, Line 79  struct cpu *cpu_new(struct memory *mem,
79    
80          cpu = zeroed_alloc(sizeof(struct cpu));          cpu = zeroed_alloc(sizeof(struct cpu));
81    
82          cpu->memory_rw          = NULL;          cpu->path = malloc(strlen(machine->path) + 15);
83          cpu->name               = cpu_type_name;          if (cpu->path == NULL) {
84          cpu->mem                = mem;                  fprintf(stderr, "cpu_new(): out of memory\n");
85          cpu->machine            = machine;                  exit(1);
86          cpu->cpu_id             = cpu_id;          }
87          cpu->byte_order         = EMUL_LITTLE_ENDIAN;          snprintf(cpu->path, strlen(machine->path) + 15,
88          cpu->running            = 0;              "%s.cpu[%i]", machine->path, cpu_id);
89    
90            cpu->memory_rw  = NULL;
91            cpu->name       = cpu_type_name;
92            cpu->mem        = mem;
93            cpu->machine    = machine;
94            cpu->cpu_id     = cpu_id;
95            cpu->byte_order = EMUL_UNDEFINED_ENDIAN;
96            cpu->running    = 0;
97    
98            /*  Create settings, and attach to the machine:  */
99            cpu->settings = settings_new();
100            snprintf(tmpstr, sizeof(tmpstr), "cpu[%i]", cpu_id);
101            settings_add(machine->settings, tmpstr, 1,
102                SETTINGS_TYPE_SUBSETTINGS, 0, cpu->settings);
103    
104            settings_add(cpu->settings, "name", 0, SETTINGS_TYPE_STRING,
105                SETTINGS_FORMAT_STRING, (void *) &cpu->name);
106            settings_add(cpu->settings, "running", 0, SETTINGS_TYPE_INT,
107                SETTINGS_FORMAT_YESNO, (void *) &cpu->running);
108    
109          cpu_create_or_reset_tc(cpu);          cpu_create_or_reset_tc(cpu);
110    
# Line 107  struct cpu *cpu_new(struct memory *mem, Line 134  struct cpu *cpu_new(struct memory *mem,
134    
135          fp->init_tables(cpu);          fp->init_tables(cpu);
136    
137            if (cpu->byte_order == EMUL_UNDEFINED_ENDIAN) {
138                    fatal("\ncpu_new(): Internal bug: Endianness not set.\n");
139                    exit(1);
140            }
141    
142          return cpu;          return cpu;
143  }  }
144    
145    
146  /*  /*
147     *  cpu_destroy():
148     *
149     *  Destroy a cpu object.
150     */
151    void cpu_destroy(struct cpu *cpu)
152    {
153            settings_remove(cpu->settings, "name");
154            settings_remove(cpu->settings, "running");
155    
156            /*  Remove any remaining level-1 settings:  */
157            settings_remove_all(cpu->settings);
158    
159            settings_destroy(cpu->settings);
160    
161            if (cpu->path != NULL)
162                    free(cpu->path);
163    
164            /*  TODO: This assumes that zeroed_alloc() actually succeeded
165                with using mmap(), and not malloc()!  */
166            munmap((void *)cpu, sizeof(struct cpu));
167    }
168    
169    
170    /*
171   *  cpu_tlbdump():   *  cpu_tlbdump():
172   *   *
173   *  Called from the debugger to dump the TLB in a readable format.   *  Called from the debugger to dump the TLB in a readable format.
# Line 130  void cpu_tlbdump(struct machine *m, int Line 186  void cpu_tlbdump(struct machine *m, int
186    
187    
188  /*  /*
  *  cpu_register_match():  
  *  
  *  Used by the debugger.  
  */  
 void cpu_register_match(struct machine *m, char *name,  
         int writeflag, uint64_t *valuep, int *match_register)  
 {  
         if (m->cpu_family == NULL || m->cpu_family->register_match == NULL)  
                 fatal("cpu_register_match(): NULL\n");  
         else  
                 m->cpu_family->register_match(m, name, writeflag,  
                     valuep, match_register);  
 }  
   
   
 /*  
189   *  cpu_disassemble_instr():   *  cpu_disassemble_instr():
190   *   *
191   *  Convert an instruction word into human readable format, for instruction   *  Convert an instruction word into human readable format, for instruction
# Line 182  void cpu_register_dump(struct machine *m Line 222  void cpu_register_dump(struct machine *m
222    
223    
224  /*  /*
  *  cpu_gdb_stub():  
  *  
  *  Execute a "remote GDB" command. Return value is a pointer to a newly  
  *  allocated response string, if the command was successfully executed. If  
  *  there was an error, NULL is returned.  
  */  
 char *cpu_gdb_stub(struct cpu *cpu, char *cmd)  
 {  
         if (cpu->machine->cpu_family == NULL ||  
             cpu->machine->cpu_family->gdb_stub == NULL) {  
                 fatal("cpu_gdb_stub(): NULL\n");  
                 return NULL;  
         } else  
                 return cpu->machine->cpu_family->gdb_stub(cpu, cmd);  
 }  
   
   
 /*  
  *  cpu_interrupt():  
  *  
  *  Assert an interrupt.  
  *  Return value is 1 if the interrupt was asserted, 0 otherwise.  
  */  
 int cpu_interrupt(struct cpu *cpu, uint64_t irq_nr)  
 {  
         if (cpu->machine->cpu_family == NULL ||  
             cpu->machine->cpu_family->interrupt == NULL) {  
                 fatal("cpu_interrupt(): NULL\n");  
                 return 0;  
         } else  
                 return cpu->machine->cpu_family->interrupt(cpu, irq_nr);  
 }  
   
   
 /*  
  *  cpu_interrupt_ack():  
  *  
  *  Acknowledge an interrupt.  
  *  Return value is 1 if the interrupt was deasserted, 0 otherwise.  
  */  
 int cpu_interrupt_ack(struct cpu *cpu, uint64_t irq_nr)  
 {  
         if (cpu->machine->cpu_family == NULL ||  
             cpu->machine->cpu_family->interrupt_ack == NULL) {  
                 /*  debug("cpu_interrupt_ack(): NULL\n");  */  
                 return 0;  
         } else  
                 return cpu->machine->cpu_family->interrupt_ack(cpu, irq_nr);  
 }  
   
   
 /*  
225   *  cpu_functioncall_trace():   *  cpu_functioncall_trace():
226   *   *
227   *  This function should be called if machine->show_trace_tree is enabled, and   *  This function should be called if machine->show_trace_tree is enabled, and
# Line 304  void cpu_functioncall_trace_return(struc Line 292  void cpu_functioncall_trace_return(struc
292   */   */
293  void cpu_create_or_reset_tc(struct cpu *cpu)  void cpu_create_or_reset_tc(struct cpu *cpu)
294  {  {
295          size_t s = DYNTRANS_CACHE_SIZE + DYNTRANS_CACHE_MARGIN;          size_t s = dyntrans_cache_size + DYNTRANS_CACHE_MARGIN;
296    
297          if (cpu->translation_cache == NULL)          if (cpu->translation_cache == NULL) {
298                  cpu->translation_cache = zeroed_alloc(s);                  cpu->translation_cache = zeroed_alloc(s);
299    
300    #ifdef NATIVE_CODE_GENERATION
301                    if (native_code_translation_enabled) {
302                            mprotect(cpu->translation_cache, s,
303                                PROT_READ | PROT_WRITE | PROT_EXEC);
304                    }
305    #endif
306            }
307    
308    #ifdef NATIVE_CODE_GENERATION
309            if (native_code_translation_enabled && cpu->inr.inr_entries == NULL)
310                    cpu->inr.inr_entries = zeroed_alloc(
311                        sizeof(struct inr_entry) * INR_MAX_ENTRIES);
312    
313            cpu->inr.nr_inr_entries_used = 0;
314    #endif
315    
316          /*  Create an empty table at the beginning of the translation cache:  */          /*  Create an empty table at the beginning of the translation cache:  */
317          memset(cpu->translation_cache, 0, sizeof(uint32_t)          memset(cpu->translation_cache, 0, sizeof(uint32_t)
318              * N_BASE_TABLE_ENTRIES);              * N_BASE_TABLE_ENTRIES);
# Line 409  void cpu_run_deinit(struct machine *mach Line 413  void cpu_run_deinit(struct machine *mach
413  /*  /*
414   *  cpu_show_cycles():   *  cpu_show_cycles():
415   *   *
416   *  If automatic adjustment of clock interrupts is turned on, then recalculate   *  If show_nr_of_instructions is on, then print a line to stdout about how
417   *  emulated_hz.  Also, if show_nr_of_instructions is on, then print a   *  many instructions/cycles have been executed so far.
  *  line to stdout about how many instructions/cycles have been executed so  
  *  far.  
418   */   */
419  void cpu_show_cycles(struct machine *machine, int forced)  void cpu_show_cycles(struct machine *machine, int forced)
420  {  {
# Line 420  void cpu_show_cycles(struct machine *mac Line 422  void cpu_show_cycles(struct machine *mac
422          char *symbol;          char *symbol;
423          int64_t mseconds, ninstrs, is, avg;          int64_t mseconds, ninstrs, is, avg;
424          struct timeval tv;          struct timeval tv;
425          int h, m, s, ms, d;          struct cpu *cpu = machine->cpus[machine->bootstrap_cpu];
426    
427          static int64_t mseconds_last = 0;          static int64_t mseconds_last = 0;
428          static int64_t ninstrs_last = -1;          static int64_t ninstrs_last = -1;
429    
430          pc = machine->cpus[machine->bootstrap_cpu]->pc;          pc = cpu->pc;
431    
432          gettimeofday(&tv, NULL);          gettimeofday(&tv, NULL);
433          mseconds = (tv.tv_sec - machine->starttime.tv_sec) * 1000          mseconds = (tv.tv_sec - machine->starttime.tv_sec) * 1000
# Line 439  void cpu_show_cycles(struct machine *mac Line 441  void cpu_show_cycles(struct machine *mac
441    
442          ninstrs = machine->ninstrs_since_gettimeofday;          ninstrs = machine->ninstrs_since_gettimeofday;
443    
         if (machine->automatic_clock_adjustment) {  
                 static int first_adjustment = 1;  
   
                 /*  Current nr of cycles per second:  */  
                 int64_t cur_cycles_per_second = 1000 *  
                     (ninstrs-ninstrs_last) / (mseconds-mseconds_last);  
   
                 /*  fatal("[ CYCLES PER SECOND = %"PRIi64" ]\n",  
                     cur_cycles_per_second);  */  
   
                 if (cur_cycles_per_second < 1000000)  
                         cur_cycles_per_second = 1000000;  
   
                 if (first_adjustment) {  
                         machine->emulated_hz = cur_cycles_per_second;  
                         first_adjustment = 0;  
                 } else {  
                         machine->emulated_hz = (15 * machine->emulated_hz +  
                             cur_cycles_per_second) / 16;  
                 }  
   
                 /*  fatal("[ updating emulated_hz to %"PRIi64" Hz ]\n",  
                     machine->emulated_hz);  */  
         }  
   
   
444          /*  RETURN here, unless show_nr_of_instructions (-N) is turned on:  */          /*  RETURN here, unless show_nr_of_instructions (-N) is turned on:  */
445          if (!machine->show_nr_of_instructions && !forced)          if (!machine->show_nr_of_instructions && !forced)
446                  goto do_return;                  goto do_return;
447    
448          printf("[ %"PRIi64" instrs", (int64_t)machine->ninstrs);          printf("[ %"PRIi64" instrs", (int64_t)machine->ninstrs);
449    
         if (!machine->automatic_clock_adjustment) {  
                 d = machine->emulated_hz / 1000;  
                 if (d < 1)  
                         d = 1;  
                 ms = machine->ninstrs / d;  
                 h = ms / 3600000;  
                 ms -= 3600000 * h;  
                 m = ms / 60000;  
                 ms -= 60000 * m;  
                 s = ms / 1000;  
                 ms -= 1000 * s;  
   
                 printf(", emulated time = %02i:%02i:%02i.%03i; ", h, m, s, ms);  
         }  
   
450          /*  Instructions per second, and average so far:  */          /*  Instructions per second, and average so far:  */
451          is = 1000 * (ninstrs-ninstrs_last) / (mseconds-mseconds_last);          is = 1000 * (ninstrs-ninstrs_last) / (mseconds-mseconds_last);
452          avg = (long long)1000 * ninstrs / mseconds;          avg = (long long)1000 * ninstrs / mseconds;
# Line 493  void cpu_show_cycles(struct machine *mac Line 454  void cpu_show_cycles(struct machine *mac
454                  is = 0;                  is = 0;
455          if (avg < 0)          if (avg < 0)
456                  avg = 0;                  avg = 0;
457          printf("; i/s=%"PRIi64" avg=%"PRIi64, is, avg);  
458            if (cpu->has_been_idling) {
459                    printf("; idling");
460                    cpu->has_been_idling = 0;
461            } else
462                    printf("; i/s=%"PRIi64" avg=%"PRIi64, is, avg);
463    
464          symbol = get_symbol_name(&machine->symbol_context, pc, &offset);          symbol = get_symbol_name(&machine->symbol_context, pc, &offset);
465    
466          if (machine->ncpus == 1) {          if (machine->ncpus == 1) {
467                  if (machine->cpus[machine->bootstrap_cpu]->is_32bit)                  if (cpu->is_32bit)
468                          printf("; pc=0x%08"PRIx32, (uint32_t) pc);                          printf("; pc=0x%08"PRIx32, (uint32_t) pc);
469                  else                  else
470                          printf("; pc=0x%016"PRIx64, (uint64_t) pc);                          printf("; pc=0x%016"PRIx64, (uint64_t) pc);
# Line 601  struct cpu_family *cpu_family_ptr_by_num Line 567  struct cpu_family *cpu_family_ptr_by_num
567   *  cpu_init():   *  cpu_init():
568   *   *
569   *  Should be called before any other cpu_*() function.   *  Should be called before any other cpu_*() function.
570     *
571     *  TODO: Make this nicer by moving out the conditional stuff to
572     *  an automagically generated file? Or a define in config.h?
573   */   */
574  void cpu_init(void)  void cpu_init(void)
575  {  {
# Line 618  void cpu_init(void) Line 587  void cpu_init(void)
587          add_cpu_family(avr_cpu_family_init, ARCH_AVR);          add_cpu_family(avr_cpu_family_init, ARCH_AVR);
588  #endif  #endif
589    
 #ifdef ENABLE_HPPA  
         add_cpu_family(hppa_cpu_family_init, ARCH_HPPA);  
 #endif  
   
 #ifdef ENABLE_I960  
         add_cpu_family(i960_cpu_family_init, ARCH_I960);  
 #endif  
   
 #ifdef ENABLE_IA64  
         add_cpu_family(ia64_cpu_family_init, ARCH_IA64);  
 #endif  
   
590  #ifdef ENABLE_M68K  #ifdef ENABLE_M68K
591          add_cpu_family(m68k_cpu_family_init, ARCH_M68K);          add_cpu_family(m68k_cpu_family_init, ARCH_M68K);
592  #endif  #endif
# Line 649  void cpu_init(void) Line 606  void cpu_init(void)
606  #ifdef ENABLE_SPARC  #ifdef ENABLE_SPARC
607          add_cpu_family(sparc_cpu_family_init, ARCH_SPARC);          add_cpu_family(sparc_cpu_family_init, ARCH_SPARC);
608  #endif  #endif
   
 #ifdef ENABLE_TRANSPUTER  
         add_cpu_family(transputer_cpu_family_init, ARCH_TRANSPUTER);  
 #endif  
   
 #ifdef ENABLE_X86  
         add_cpu_family(x86_cpu_family_init, ARCH_X86);  
 #endif  
609  }  }
610    

Legend:
Removed from v.30  
changed lines
  Added in v.38

  ViewVC Help
Powered by ViewVC 1.1.26